Commit 4e2d8a75 authored by Huanzhong Huang's avatar Huanzhong Huang Committed by Commit Bot

Observe policy changes for cookie controls UI behavior

In the goal of cookie controls UI getting hidden when (the original)
third-party cookie blocking is either managed or turned on by User,
this change covers the (previously overlooked) cases where 3p cookie
blocking's managed state goes from managed to unmanaged and vice versa.

Bug: 967668
Change-Id: I299b4054fe9b89287f7bf39ccd726454f3aaeda8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1866713
Commit-Queue: Huanzhong Huang <huanzhong@chromium.org>
Reviewed-by: default avatarDan Beam <dbeam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#707682}
parent 040c0ebb
......@@ -4,15 +4,20 @@
#include "chrome/browser/ui/webui/ntp/cookie_controls_handler.h"
#include <utility>
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/values.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
CookieControlsHandler::CookieControlsHandler() {}
......@@ -43,10 +48,19 @@ void CookieControlsHandler::OnJavascriptAllowed() {
prefs::kBlockThirdPartyCookies,
base::Bind(&CookieControlsHandler::OnThirdPartyCookieBlockingChanged,
base::Unretained(this)));
policy_registrar_ = std::make_unique<policy::PolicyChangeRegistrar>(
profile->GetProfilePolicyConnector()->policy_service(),
policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()));
policy_registrar_->Observe(
policy::key::kBlockThirdPartyCookies,
base::BindRepeating(
&CookieControlsHandler::OnThirdPartyCookieBlockingPolicyChanged,
base::Unretained(this)));
}
void CookieControlsHandler::OnJavascriptDisallowed() {
pref_change_registrar_.RemoveAll();
policy_registrar_.reset();
}
void CookieControlsHandler::HandleCookieControlsToggleChanged(
......@@ -88,6 +102,12 @@ void CookieControlsHandler::OnThirdPartyCookieBlockingChanged() {
base::Value(ShouldHideCookieControlsUI(profile)));
}
void CookieControlsHandler::OnThirdPartyCookieBlockingPolicyChanged(
const base::Value* previous,
const base::Value* current) {
OnThirdPartyCookieBlockingChanged();
}
bool CookieControlsHandler::ShouldHideCookieControlsUI(const Profile* profile) {
return !base::FeatureList::IsEnabled(
content_settings::kImprovedCookieControls) ||
......
......@@ -5,6 +5,8 @@
#ifndef CHROME_BROWSER_UI_WEBUI_NTP_COOKIE_CONTROLS_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_NTP_COOKIE_CONTROLS_HANDLER_H_
#include <memory>
#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/web_ui_message_handler.h"
......@@ -13,6 +15,11 @@ class Profile;
namespace base {
class ListValue;
class Value;
} // namespace base
namespace policy {
class PolicyChangeRegistrar;
}
// Handles requests for prefs::kCookieControlsMode retrival/update.
......@@ -45,8 +52,13 @@ class CookieControlsHandler : public content::WebUIMessageHandler {
// changed.
void OnThirdPartyCookieBlockingChanged();
void OnThirdPartyCookieBlockingPolicyChanged(const base::Value* previous,
const base::Value* current);
PrefChangeRegistrar pref_change_registrar_;
std::unique_ptr<policy::PolicyChangeRegistrar> policy_registrar_;
DISALLOW_COPY_AND_ASSIGN(CookieControlsHandler);
};
......
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