Commit 2f976a67 authored by Tommy Steimel's avatar Tommy Steimel Committed by Commit Bot

Add new content setting for sound

This CL does not display the setting on any UI.
This setting is used to mute websites.

Bug: 742999
Change-Id: I4c15c809795b523b90a26f4b42b3d0b9d612d695
Reviewed-on: https://chromium-review.googlesource.com/570807Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Commit-Queue: Tommy Steimel <steimel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487550}
parent 978f6fdb
...@@ -297,6 +297,8 @@ split_static_library("browser") { ...@@ -297,6 +297,8 @@ split_static_library("browser") {
"content_settings/local_shared_objects_container.h", "content_settings/local_shared_objects_container.h",
"content_settings/mixed_content_settings_tab_helper.cc", "content_settings/mixed_content_settings_tab_helper.cc",
"content_settings/mixed_content_settings_tab_helper.h", "content_settings/mixed_content_settings_tab_helper.h",
"content_settings/sound_content_setting_observer.cc",
"content_settings/sound_content_setting_observer.h",
"content_settings/tab_specific_content_settings.cc", "content_settings/tab_specific_content_settings.cc",
"content_settings/tab_specific_content_settings.h", "content_settings/tab_specific_content_settings.h",
"content_settings/web_site_settings_uma_util.cc", "content_settings/web_site_settings_uma_util.cc",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/content_settings/sound_content_setting_observer.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/navigation_handle.h"
DEFINE_WEB_CONTENTS_USER_DATA_KEY(SoundContentSettingObserver);
SoundContentSettingObserver::SoundContentSettingObserver(
content::WebContents* contents)
: content::WebContentsObserver(contents), observer_(this) {
host_content_settings_map_ = HostContentSettingsMapFactory::GetForProfile(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
observer_.Add(host_content_settings_map_);
}
SoundContentSettingObserver::~SoundContentSettingObserver() = default;
void SoundContentSettingObserver::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
if (navigation_handle->IsInMainFrame() && navigation_handle->HasCommitted() &&
!navigation_handle->IsSameDocument()) {
MuteOrUnmuteIfNecessary();
}
}
void SoundContentSettingObserver::OnContentSettingChanged(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
std::string resource_identifier) {
if (content_type == CONTENT_SETTINGS_TYPE_SOUND)
MuteOrUnmuteIfNecessary();
}
void SoundContentSettingObserver::MuteOrUnmuteIfNecessary() {
web_contents()->SetAudioMuted(GetCurrentContentSetting() ==
CONTENT_SETTING_BLOCK);
}
ContentSetting SoundContentSettingObserver::GetCurrentContentSetting() {
GURL url = web_contents()->GetLastCommittedURL();
return host_content_settings_map_->GetContentSetting(
url, url, CONTENT_SETTINGS_TYPE_SOUND, std::string());
}
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CONTENT_SETTINGS_SOUND_CONTENT_SETTING_OBSERVER_H_
#define CHROME_BROWSER_CONTENT_SETTINGS_SOUND_CONTENT_SETTING_OBSERVER_H_
#include "base/scoped_observer.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "components/content_settings/core/common/content_settings.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
class HostContentSettingsMap;
class SoundContentSettingObserver
: public content::WebContentsObserver,
public content::WebContentsUserData<SoundContentSettingObserver>,
public content_settings::Observer {
public:
~SoundContentSettingObserver() override;
// content::WebContentsObserver implementation.
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
// content_settings::Observer implementation.
void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
std::string resource_identifier) override;
private:
explicit SoundContentSettingObserver(content::WebContents* web_contents);
friend class content::WebContentsUserData<SoundContentSettingObserver>;
void MuteOrUnmuteIfNecessary();
ContentSetting GetCurrentContentSetting();
HostContentSettingsMap* host_content_settings_map_;
ScopedObserver<HostContentSettingsMap, content_settings::Observer> observer_;
DISALLOW_COPY_AND_ASSIGN(SoundContentSettingObserver);
};
#endif // CHROME_BROWSER_CONTENT_SETTINGS_SOUND_CONTENT_SETTING_OBSERVER_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/content_settings/sound_content_setting_observer.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr char kURL1[] = "http://google.com";
constexpr char kURL2[] = "http://youtube.com";
} // anonymous namespace
class SoundContentSettingObserverTest : public ChromeRenderViewHostTestHarness {
public:
SoundContentSettingObserverTest() = default;
~SoundContentSettingObserverTest() override = default;
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
SoundContentSettingObserver::CreateForWebContents(web_contents());
host_content_settings_map_ = HostContentSettingsMapFactory::GetForProfile(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()));
NavigateAndCommit(GURL(kURL1));
}
protected:
void ChangeSoundContentSettingTo(ContentSetting setting) {
GURL url = web_contents()->GetLastCommittedURL();
host_content_settings_map_->SetContentSettingDefaultScope(
url, url, CONTENT_SETTINGS_TYPE_SOUND, std::string(), setting);
}
void ChangeDefaultSoundContentSettingTo(ContentSetting setting) {
host_content_settings_map_->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_SOUND, setting);
}
private:
HostContentSettingsMap* host_content_settings_map_;
DISALLOW_COPY_AND_ASSIGN(SoundContentSettingObserverTest);
};
TEST_F(SoundContentSettingObserverTest, AudioMutingUpdatesWithContentSetting) {
EXPECT_FALSE(web_contents()->IsAudioMuted());
// Block site.
ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
EXPECT_TRUE(web_contents()->IsAudioMuted());
// Allow site.
ChangeSoundContentSettingTo(CONTENT_SETTING_ALLOW);
EXPECT_FALSE(web_contents()->IsAudioMuted());
// Set site to default.
ChangeSoundContentSettingTo(CONTENT_SETTING_DEFAULT);
EXPECT_FALSE(web_contents()->IsAudioMuted());
// Block by default.
ChangeDefaultSoundContentSettingTo(CONTENT_SETTING_BLOCK);
EXPECT_TRUE(web_contents()->IsAudioMuted());
// Should not affect site if explicitly allowed.
ChangeSoundContentSettingTo(CONTENT_SETTING_ALLOW);
EXPECT_FALSE(web_contents()->IsAudioMuted());
// Change site back to default.
ChangeSoundContentSettingTo(CONTENT_SETTING_DEFAULT);
EXPECT_TRUE(web_contents()->IsAudioMuted());
// Allow by default.
ChangeDefaultSoundContentSettingTo(CONTENT_SETTING_ALLOW);
EXPECT_FALSE(web_contents()->IsAudioMuted());
}
TEST_F(SoundContentSettingObserverTest, AudioMutingUpdatesWithNavigation) {
EXPECT_FALSE(web_contents()->IsAudioMuted());
// Block for kURL1.
ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
EXPECT_TRUE(web_contents()->IsAudioMuted());
// Should not be muted for kURL2.
NavigateAndCommit(GURL(kURL2));
EXPECT_FALSE(web_contents()->IsAudioMuted());
// Should be muted for kURL1.
NavigateAndCommit(GURL(kURL1));
EXPECT_TRUE(web_contents()->IsAudioMuted());
}
...@@ -3089,6 +3089,7 @@ test("unit_tests") { ...@@ -3089,6 +3089,7 @@ test("unit_tests") {
"../browser/content_settings/host_content_settings_map_unittest.cc", "../browser/content_settings/host_content_settings_map_unittest.cc",
"../browser/content_settings/mock_settings_observer.cc", "../browser/content_settings/mock_settings_observer.cc",
"../browser/content_settings/mock_settings_observer.h", "../browser/content_settings/mock_settings_observer.h",
"../browser/content_settings/sound_content_setting_observer_unittest.cc",
"../browser/content_settings/tab_specific_content_settings_unittest.cc", "../browser/content_settings/tab_specific_content_settings_unittest.cc",
"../browser/custom_handlers/protocol_handler_registry_unittest.cc", "../browser/custom_handlers/protocol_handler_registry_unittest.cc",
"../browser/data_usage/tab_id_annotator_unittest.cc", "../browser/data_usage/tab_id_annotator_unittest.cc",
......
...@@ -274,6 +274,14 @@ void ContentSettingsRegistry::Init() { ...@@ -274,6 +274,14 @@ void ContentSettingsRegistry::Init() {
WebsiteSettingsRegistry::PLATFORM_ANDROID, WebsiteSettingsRegistry::PLATFORM_ANDROID,
ContentSettingsInfo::INHERIT_IF_LESS_PERMISSIVE); ContentSettingsInfo::INHERIT_IF_LESS_PERMISSIVE);
Register(CONTENT_SETTINGS_TYPE_SOUND, "sound", CONTENT_SETTING_ALLOW,
WebsiteSettingsInfo::UNSYNCABLE, WhitelistedSchemes(),
ValidSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_BLOCK),
WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE,
WebsiteSettingsRegistry::DESKTOP |
WebsiteSettingsRegistry::PLATFORM_ANDROID,
ContentSettingsInfo::INHERIT_IF_LESS_PERMISSIVE);
Register(CONTENT_SETTINGS_TYPE_ADS, "subresource-filter", Register(CONTENT_SETTINGS_TYPE_ADS, "subresource-filter",
CONTENT_SETTING_BLOCK, WebsiteSettingsInfo::UNSYNCABLE, CONTENT_SETTING_BLOCK, WebsiteSettingsInfo::UNSYNCABLE,
WhitelistedSchemes(), WhitelistedSchemes(),
......
...@@ -28,7 +28,7 @@ struct HistogramValue { ...@@ -28,7 +28,7 @@ struct HistogramValue {
// content settings type name instead. // content settings type name instead.
// //
// The array size must be explicit for the static_asserts below. // The array size must be explicit for the static_asserts below.
constexpr size_t kNumHistogramValues = 29; constexpr size_t kNumHistogramValues = 30;
constexpr HistogramValue kHistogramValue[kNumHistogramValues] = { constexpr HistogramValue kHistogramValue[kNumHistogramValues] = {
{CONTENT_SETTINGS_TYPE_COOKIES, 0}, {CONTENT_SETTINGS_TYPE_COOKIES, 0},
{CONTENT_SETTINGS_TYPE_IMAGES, 1}, {CONTENT_SETTINGS_TYPE_IMAGES, 1},
...@@ -59,6 +59,7 @@ constexpr HistogramValue kHistogramValue[kNumHistogramValues] = { ...@@ -59,6 +59,7 @@ constexpr HistogramValue kHistogramValue[kNumHistogramValues] = {
{CONTENT_SETTINGS_TYPE_ADS_DATA, 33}, {CONTENT_SETTINGS_TYPE_ADS_DATA, 33},
{CONTENT_SETTINGS_TYPE_PASSWORD_PROTECTION, 34}, {CONTENT_SETTINGS_TYPE_PASSWORD_PROTECTION, 34},
{CONTENT_SETTINGS_TYPE_MEDIA_ENGAGEMENT, 35}, {CONTENT_SETTINGS_TYPE_MEDIA_ENGAGEMENT, 35},
{CONTENT_SETTINGS_TYPE_SOUND, 36},
}; };
} // namespace } // namespace
......
...@@ -71,6 +71,10 @@ enum ContentSettingsType { ...@@ -71,6 +71,10 @@ enum ContentSettingsType {
// specific origin. // specific origin.
CONTENT_SETTINGS_TYPE_MEDIA_ENGAGEMENT, CONTENT_SETTINGS_TYPE_MEDIA_ENGAGEMENT,
// Website setting which stores whether or not the site can play audible
// sound. This will not block playback but instead the user will not hear it.
CONTENT_SETTINGS_TYPE_SOUND,
CONTENT_SETTINGS_NUM_TYPES, CONTENT_SETTINGS_NUM_TYPES,
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment