Commit 37112950 authored by Tommy Steimel's avatar Tommy Steimel Committed by Commit Bot

Add ability to mute chrome:// pages

Bug: 787743, 785711, 782134
Change-Id: I77fe9a7192697ebe82734231ae709c2094b57b62
Reviewed-on: https://chromium-review.googlesource.com/791813
Commit-Queue: Tommy Steimel <steimel@chromium.org>
Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#520203}
parent 0d3eb1e1
......@@ -10,6 +10,7 @@
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/common/url_constants.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#if !defined(OS_ANDROID)
......@@ -71,6 +72,14 @@ void SoundContentSettingObserver::MuteOrUnmuteIfNecessary() {
if (reason == TabMutedReason::EXTENSION)
return;
// Don't unmute a chrome:// URL if the tab has been explicitly muted on a
// chrome:// URL.
if (reason == TabMutedReason::CONTENT_SETTING_CHROME &&
web_contents()->GetLastCommittedURL().SchemeIs(
content::kChromeUIScheme)) {
return;
}
chrome::SetTabAudioMuted(web_contents(), mute,
TabMutedReason::CONTENT_SETTING, std::string());
#endif // defined(OS_ANDROID)
......
......@@ -24,6 +24,7 @@ constexpr char kURL2[] = "http://youtube.com/";
constexpr char kSiteMutedEvent[] = "Media.SiteMuted";
constexpr char kSiteMutedReason[] = "MuteReason";
#if !defined(OS_ANDROID)
constexpr char kChromeURL[] = "chrome://dino";
constexpr char kExtensionId[] = "extensionid";
#endif
......@@ -185,6 +186,41 @@ TEST_F(SoundContentSettingObserverTest, DontUnmuteWhenMutedForMediaCapture) {
NavigateAndCommit(GURL(kURL2));
EXPECT_TRUE(web_contents()->IsAudioMuted());
}
TEST_F(SoundContentSettingObserverTest, DontUnmuteChromeTabWhenMuted) {
NavigateAndCommit(GURL(kChromeURL));
EXPECT_FALSE(web_contents()->IsAudioMuted());
SetMuteStateForReason(true, TabMutedReason::CONTENT_SETTING_CHROME);
EXPECT_TRUE(web_contents()->IsAudioMuted());
NavigateAndCommit(GURL(kChromeURL));
EXPECT_TRUE(web_contents()->IsAudioMuted());
}
TEST_F(SoundContentSettingObserverTest,
UnmuteChromeTabWhenNavigatingToNonChromeUrl) {
NavigateAndCommit(GURL(kChromeURL));
EXPECT_FALSE(web_contents()->IsAudioMuted());
SetMuteStateForReason(true, TabMutedReason::CONTENT_SETTING_CHROME);
EXPECT_TRUE(web_contents()->IsAudioMuted());
NavigateAndCommit(GURL(kURL1));
EXPECT_FALSE(web_contents()->IsAudioMuted());
}
TEST_F(SoundContentSettingObserverTest,
UnmuteNonChromeTabWhenNavigatingToChromeUrl) {
NavigateAndCommit(GURL(kURL1));
EXPECT_FALSE(web_contents()->IsAudioMuted());
ChangeSoundContentSettingTo(CONTENT_SETTING_BLOCK);
EXPECT_TRUE(web_contents()->IsAudioMuted());
NavigateAndCommit(GURL(kChromeURL));
EXPECT_FALSE(web_contents()->IsAudioMuted());
}
#endif // !defined(OS_ANDROID)
TEST_F(SoundContentSettingObserverTest,
......
......@@ -446,6 +446,7 @@ std::unique_ptr<api::tabs::MutedInfo> ExtensionTabUtil::CreateMutedInfo(
break;
case TabMutedReason::AUDIO_INDICATOR:
case TabMutedReason::CONTENT_SETTING:
case TabMutedReason::CONTENT_SETTING_CHROME:
case TabMutedReason::CONTEXT_MENU:
info->reason = api::tabs::MUTED_INFO_REASON_USER;
break;
......
......@@ -21,6 +21,7 @@
#include "chrome/grit/generated_resources.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/theme_provider.h"
......@@ -427,24 +428,40 @@ void SetSitesMuted(const TabStripModel& tab_strip,
for (int tab_index : indices) {
content::WebContents* web_contents = tab_strip.GetWebContentsAt(tab_index);
GURL url = web_contents->GetLastCommittedURL();
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
HostContentSettingsMap* settings =
HostContentSettingsMapFactory::GetForProfile(profile);
ContentSetting setting =
mute ? CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW;
if (setting == settings->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_SOUND, nullptr)) {
setting = CONTENT_SETTING_DEFAULT;
if (url.SchemeIs(content::kChromeUIScheme)) {
// chrome:// URLs don't have content settings but can be muted, so just
// mute the WebContents.
SetTabAudioMuted(web_contents, mute,
TabMutedReason::CONTENT_SETTING_CHROME, std::string());
} else {
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
HostContentSettingsMap* settings =
HostContentSettingsMapFactory::GetForProfile(profile);
ContentSetting setting =
mute ? CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW;
if (setting == settings->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_SOUND, nullptr)) {
setting = CONTENT_SETTING_DEFAULT;
}
settings->SetContentSettingDefaultScope(
url, url, CONTENT_SETTINGS_TYPE_SOUND, std::string(), setting);
}
settings->SetContentSettingDefaultScope(
url, url, CONTENT_SETTINGS_TYPE_SOUND, std::string(), setting);
}
}
bool IsSiteMuted(const TabStripModel& tab_strip, const int index) {
content::WebContents* web_contents = tab_strip.GetWebContentsAt(index);
GURL url = web_contents->GetLastCommittedURL();
// chrome:// URLs don't have content settings but can be muted, so just check
// the current muted state and TabMutedReason of the WebContents.
if (url.SchemeIs(content::kChromeUIScheme)) {
return web_contents->IsAudioMuted() &&
GetTabAudioMutedReason(web_contents) ==
TabMutedReason::CONTENT_SETTING_CHROME;
}
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
HostContentSettingsMap* settings =
......
......@@ -37,12 +37,13 @@ enum class TabAlertState {
};
enum class TabMutedReason {
NONE, // The tab has never been muted or unmuted.
CONTEXT_MENU, // Mute/Unmute chosen from tab context menu.
AUDIO_INDICATOR, // Mute toggled via tab-strip audio icon.
MEDIA_CAPTURE, // Media recording/capture was started.
EXTENSION, // Mute state changed via extension API.
CONTENT_SETTING, // The sound content setting was set to BLOCK.
NONE, // The tab has never been muted or unmuted.
CONTEXT_MENU, // Mute/Unmute chosen from tab context menu.
AUDIO_INDICATOR, // Mute toggled via tab-strip audio icon.
MEDIA_CAPTURE, // Media recording/capture was started.
EXTENSION, // Mute state changed via extension API.
CONTENT_SETTING, // The sound content setting was set to BLOCK.
CONTENT_SETTING_CHROME, // Mute toggled on chrome:// URL.
};
enum class TabMutedResult {
......
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