Commit da5ec5da authored by Huanzhong Huang's avatar Huanzhong Huang Committed by Commit Bot

Cookie controls enforcement

Currently, cookie controls is hidden when the original third-party
cookie blocking is enforced or turned on by the user.
This change make it so that cookie controls always stays; in the above
situations, cookie controls is disabled, toggle set in accordance with
third-party cookie blocking, with an tooltip icon for more information.

Bug: 1019706
Change-Id: I3e7892bcda7eaf70c227a0280219307699cf3bd7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1881558
Commit-Queue: Huanzhong Huang <huanzhong@chromium.org>
Reviewed-by: default avatarDan Beam <dbeam@chromium.org>
Reviewed-by: default avatarJochen Eisinger <jochen@chromium.org>
Reviewed-by: default avatarChristian Dullweber <dullweber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712118}
parent d496d027
......@@ -113,14 +113,15 @@ em {
#cookie-controls-description {
flex: 1;
padding-inline-end: 20px;
padding-inline-end: 12px;
}
#cookie-controls-description em {
display: block;
}
#cookie-controls-toggle {
#cookie-controls-toggle,
#cookie-controls-tooltip-icon-wrapper {
flex: none;
}
......@@ -128,6 +129,16 @@ em {
width: 34px;
}
#cookie-controls-tooltip-icon-wrapper {
/* calc(var(--cr-icon-size) + var(--cr-controlled-by-spacing)) */
/* Using actual numbers because the above definitions are lazy-loaded. */
width: 44px;
}
#cookie-controls-tooltip-icon-wrapper > a {
color: inherit;
}
/** Layout ------------------------------------------------------------------ */
/* Align the content, icon, and title to to the center. */
......
......@@ -35,6 +35,14 @@ document.write('<link id="incognitothemecss" rel="stylesheet" ' +
<em>$i18n{cookieControlsTitle}</em>
$i18n{cookieControlsDescription}
</div>
<div id="cookie-controls-tooltip-icon-wrapper" $i18n{hideTooltipIcon}>
<a href="chrome://settings/content/cookies">
<cr-tooltip-icon icon-aria-label="$i18n{cookieControlsTitle}"
icon-class="cr20:domain"
tooltip-text="$i18n{cookieControlsTooltipText}">
</cr-tooltip-icon>
</a>
</div>
<cr-toggle id="cookie-controls-toggle"
aria-label="$i18n{cookieControlsTitle}"
$i18n{cookieControlsToggleChecked} dark></cr-toggle>
......@@ -44,7 +52,8 @@ document.write('<link id="incognitothemecss" rel="stylesheet" ' +
<script src="chrome://resources/js/cr.js"></script>
<script src="chrome://resources/js/util.js"></script>
<script src="incognito_tab.js"></script>
<!-- Lazy-load cr_toggle to avoid performance penalty introduced by loading Polymer -->
<!-- Lazy-load cr_elements to avoid performance penalty introduced by loading Polymer -->
<script type="module" src="chrome://resources/cr_elements/cr_toggle/cr_toggle.m.js" async></script>
<script type="module" src="chrome://resources/cr_elements/policy/cr_tooltip_icon.m.js" async></script>
</body>
</html>
......@@ -11,13 +11,11 @@ window.addEventListener('load', function() {
});
chrome.send('observeThemeChanges');
cr.addWebUIListener('cookie-controls-changed', checked => {
$('cookie-controls-toggle').checked = checked;
cr.addWebUIListener('cookie-controls-changed', dict => {
$('cookie-controls-tooltip-icon-wrapper').hidden = !dict.enforced;
$('cookie-controls-toggle').disabled = dict.enforced;
$('cookie-controls-toggle').checked = dict.checked;
});
cr.addWebUIListener(
'third-party-cookie-blocking-changed', shouldHideCookieControls => {
$('cookie-controls').hidden = shouldHideCookieControls;
});
$('cookie-controls-toggle').addEventListener('change', event => {
chrome.send('cookieControlsToggleChanged', [event.detail]);
});
......
......@@ -11,6 +11,7 @@
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/values.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/cookie_settings.h"
......@@ -42,11 +43,11 @@ void CookieControlsHandler::OnJavascriptAllowed() {
pref_change_registrar_.Init(profile->GetPrefs());
pref_change_registrar_.Add(
prefs::kCookieControlsMode,
base::Bind(&CookieControlsHandler::OnCookieControlsChanged,
base::Bind(&CookieControlsHandler::SendCookieControlsUIChanges,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kBlockThirdPartyCookies,
base::Bind(&CookieControlsHandler::OnThirdPartyCookieBlockingChanged,
base::Bind(&CookieControlsHandler::SendCookieControlsUIChanges,
base::Unretained(this)));
policy_registrar_ = std::make_unique<policy::PolicyChangeRegistrar>(
profile->GetProfilePolicyConnector()->policy_service(),
......@@ -81,37 +82,36 @@ void CookieControlsHandler::HandleCookieControlsToggleChanged(
void CookieControlsHandler::HandleObserveCookieControlsSettingsChanges(
const base::ListValue* args) {
AllowJavascript();
OnCookieControlsChanged();
OnThirdPartyCookieBlockingChanged();
}
void CookieControlsHandler::OnCookieControlsChanged() {
Profile* profile = Profile::FromWebUI(web_ui());
FireWebUIListener("cookie-controls-changed",
base::Value(GetToggleCheckedValue(profile)));
SendCookieControlsUIChanges();
}
bool CookieControlsHandler::GetToggleCheckedValue(const Profile* profile) {
int mode = profile->GetPrefs()->GetInteger(prefs::kCookieControlsMode);
return mode != static_cast<int>(content_settings::CookieControlsMode::kOff);
return CookieSettingsFactory::GetForProfile(const_cast<Profile*>(profile))
->ShouldBlockThirdPartyCookies();
}
void CookieControlsHandler::OnThirdPartyCookieBlockingChanged() {
void CookieControlsHandler::SendCookieControlsUIChanges() {
Profile* profile = Profile::FromWebUI(web_ui());
FireWebUIListener("third-party-cookie-blocking-changed",
base::Value(ShouldHideCookieControlsUI(profile)));
base::DictionaryValue dict;
dict.SetBoolKey("enforced", ShouldEnforceCookieControls(profile));
dict.SetBoolKey("checked", GetToggleCheckedValue(profile));
FireWebUIListener("cookie-controls-changed", dict);
}
void CookieControlsHandler::OnThirdPartyCookieBlockingPolicyChanged(
const base::Value* previous,
const base::Value* current) {
OnThirdPartyCookieBlockingChanged();
SendCookieControlsUIChanges();
}
bool CookieControlsHandler::ShouldHideCookieControlsUI(const Profile* profile) {
return !base::FeatureList::IsEnabled(
content_settings::kImprovedCookieControls) ||
profile->GetPrefs()->IsManagedPreference(
content_settings::kImprovedCookieControls);
}
bool CookieControlsHandler::ShouldEnforceCookieControls(
const Profile* profile) {
return profile->GetPrefs()->IsManagedPreference(
prefs::kBlockThirdPartyCookies) ||
profile->GetPrefs()->GetBoolean(prefs::kBlockThirdPartyCookies);
}
......@@ -40,17 +40,17 @@ class CookieControlsHandler : public content::WebUIMessageHandler {
// Whether cookie controls UI should be hidden in incognito ntp.
static bool ShouldHideCookieControlsUI(const Profile* profile);
// Whether cookie controls should appear enforced.
static bool ShouldEnforceCookieControls(const Profile* profile);
static bool GetToggleCheckedValue(const Profile* profile);
private:
friend class CookieControlsHandlerTest;
// Updates cookie controls UI when underlying setting has changed.
void OnCookieControlsChanged();
// Updates cookie controls UI when third-party cookie blocking setting has
// changed.
void OnThirdPartyCookieBlockingChanged();
void SendCookieControlsUIChanges();
void OnThirdPartyCookieBlockingPolicyChanged(const base::Value* previous,
const base::Value* current);
......
......@@ -5,6 +5,7 @@
#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/memory/ref_counted_memory.h"
......@@ -18,6 +19,7 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/themes/theme_properties.h"
......@@ -40,6 +42,8 @@
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/google/core/common/google_util.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/strings/grit/components_strings.h"
......@@ -163,8 +167,18 @@ NTPResourceCache::NTPResourceCache(Profile* profile)
callback);
profile_pref_change_registrar_.Add(prefs::kNtpShownPage, callback);
profile_pref_change_registrar_.Add(prefs::kHideWebStoreIcon, callback);
profile_pref_change_registrar_.Add(prefs::kCookieControlsMode, callback);
profile_pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, callback);
theme_observer_.Add(ui::NativeTheme::GetInstanceForNativeUi());
policy_change_registrar_ = std::make_unique<policy::PolicyChangeRegistrar>(
profile->GetProfilePolicyConnector()->policy_service(),
policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()));
policy_change_registrar_->Observe(
policy::key::kBlockThirdPartyCookies,
base::BindRepeating(&NTPResourceCache::OnPolicyChanged,
base::Unretained(this)));
}
NTPResourceCache::~NTPResourceCache() {}
......@@ -295,8 +309,20 @@ void NTPResourceCache::CreateNewTabIncognitoHTML() {
l10n_util::GetStringUTF8(IDS_SETTINGS_SITE_SETTINGS_THIRD_PARTY_COOKIE);
replacements["cookieControlsDescription"] = l10n_util::GetStringUTF8(
IDS_SETTINGS_SITE_SETTINGS_THIRD_PARTY_COOKIE_SUBLABEL);
// Ensure passing off-the-record profile; |profile_| might not be incognito.
DCHECK(profile_->HasOffTheRecordProfile());
replacements["cookieControlsToggleChecked"] =
CookieControlsHandler::GetToggleCheckedValue(profile_) ? "checked" : "";
CookieControlsHandler::GetToggleCheckedValue(
profile_->GetOffTheRecordProfile())
? "checked"
: "";
replacements["hideTooltipIcon"] =
CookieControlsHandler::ShouldEnforceCookieControls(profile_) ? ""
: "hidden";
replacements["cookieControlsTooltipText"] = l10n_util::GetStringFUTF8(
IDS_NEW_TAB_OTR_COOKIE_CONTROLS_CONTROLLED_TOOLTIP_TEXT,
l10n_util::GetStringUTF16(IDS_SETTINGS_SITE_SETTINGS_THIRD_PARTY_COOKIE),
l10n_util::GetStringUTF16(IDS_SETTINGS_SITE_SETTINGS_COOKIES));
const ui::ThemeProvider& tp =
ThemeService::GetThemeProviderForProfile(profile_);
......@@ -607,3 +633,8 @@ void NTPResourceCache::CreateNewTabCSS() {
ReplaceTemplateExpressions(*new_tab_theme_css, substitutions);
new_tab_css_ = base::RefCountedString::TakeString(&css_string);
}
void NTPResourceCache::OnPolicyChanged(const base::Value* previous,
const base::Value* current) {
new_tab_incognito_html_ = nullptr;
}
......@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_
#define CHROME_BROWSER_UI_WEBUI_NTP_NTP_RESOURCE_CACHE_H_
#include <memory>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
......@@ -27,6 +29,10 @@ namespace content {
class RenderProcessHost;
}
namespace policy {
class PolicyChangeRegistrar;
}
// This class keeps a cache of NTP resources (HTML and CSS) so we don't have to
// regenerate them all the time.
// Note: This is only used for incognito and guest mode NTPs (NewTabUI), as well
......@@ -61,6 +67,8 @@ class NTPResourceCache : public content::NotificationObserver,
void OnPreferenceChanged();
void OnPolicyChanged(const base::Value* previous, const base::Value* current);
// Invalidates the NTPResourceCache.
void Invalidate();
......@@ -97,6 +105,8 @@ class NTPResourceCache : public content::NotificationObserver,
ScopedObserver<ui::NativeTheme, ui::NativeThemeObserver> theme_observer_{
this};
std::unique_ptr<policy::PolicyChangeRegistrar> policy_change_registrar_;
DISALLOW_COPY_AND_ASSIGN(NTPResourceCache);
};
......
......@@ -16,6 +16,8 @@ per-file dom_distiller_strings.grdp=file://components/dom_distiller/OWNERS
per-file error_page_strings.grdp=file://components/error_page/OWNERS
per-file management_strings.grdp=file://docs/privacy/OWNERS
per-file media_message_center_strings.grdp=file://components/media_message_center/OWNERS
per-file new_or_sad_tab_strings.grdp=file://chrome/browser/ui/webui/ntp/OWNERS
per-file new_or_sad_tab_strings.grdp=file://chrome/browser/resources/ntp4/OWNERS
per-file ntp_snippets_strings.grdp=file://components/ntp_snippets/OWNERS
per-file omnibox_strings.grdp=file://components/omnibox/OWNERS
per-file page_info_strings.grdp=file://chrome/browser/ui/page_info/OWNERS
......
......@@ -160,5 +160,9 @@
<ph name="LIST_ITEM">&lt;li&gt;</ph>Your internet service provider
<ph name="END_LIST">&lt;/ul&gt;</ph>
</message>
<message name="IDS_NEW_TAB_OTR_COOKIE_CONTROLS_CONTROLLED_TOOLTIP_TEXT"
desc="Text displayed in the settings tooltip when third-party cookie blocking is managed or turned on by the user.">
This setting is enforced by &quot;<ph name="ENFORCING_SETTING">$1<ex>Block third-party cookies</ex></ph>&quot; in &quot;<ph name="SETTINGS_PAGE">$2<ex>Cookies and site data</ex></ph>&quot;
</message>
</grit-part>
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