Commit 3fc4c3c5 authored by Ehimare Okoyomon's avatar Ehimare Okoyomon Committed by Commit Bot

Make CookieControlsService class to be used for multiple platforms

Put all common cookie controls methods into a service so android can
use these as well. Then move the web-specific support to new
CookieControlsHandler that implements the web version.

Bug: 1040091
Change-Id: I422664a2d8e11e94cb4102c5cb8aa4494f83db3b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2019109
Commit-Queue: Ehimare Okoyomon <eokoyomon@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarChristian Dullweber <dullweber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737934}
parent 5b2070ca
...@@ -95,6 +95,10 @@ jumbo_static_library("ui") { ...@@ -95,6 +95,10 @@ jumbo_static_library("ui") {
"confirm_bubble.h", "confirm_bubble.h",
"cookie_controls/cookie_controls_controller.cc", "cookie_controls/cookie_controls_controller.cc",
"cookie_controls/cookie_controls_controller.h", "cookie_controls/cookie_controls_controller.h",
"cookie_controls/cookie_controls_service.cc",
"cookie_controls/cookie_controls_service.h",
"cookie_controls/cookie_controls_service_factory.cc",
"cookie_controls/cookie_controls_service_factory.h",
"crypto_module_password_dialog.h", "crypto_module_password_dialog.h",
"cryptuiapi_shim.h", "cryptuiapi_shim.h",
"enterprise_startup_dialog.h", "enterprise_startup_dialog.h",
......
// Copyright 2020 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/ui/cookie_controls/cookie_controls_service.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 "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"
#include "components/content_settings/core/common/content_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"
#include "url/gurl.h"
CookieControlsService::CookieControlsService(Profile* profile)
: profile_(profile) {
Init();
}
CookieControlsService::~CookieControlsService() = default;
void CookieControlsService::Init() {
pref_change_registrar_.Init(profile_->GetPrefs());
pref_change_registrar_.Add(
prefs::kCookieControlsMode,
base::Bind(&CookieControlsService::OnThirdPartyCookieBlockingPrefChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kBlockThirdPartyCookies,
base::Bind(&CookieControlsService::OnThirdPartyCookieBlockingPrefChanged,
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(
&CookieControlsService::OnThirdPartyCookieBlockingPolicyChanged,
base::Unretained(this)));
}
void CookieControlsService::Shutdown() {
pref_change_registrar_.RemoveAll();
policy_registrar_.reset();
}
void CookieControlsService::HandleCookieControlsToggleChanged(bool checked) {
profile_->GetPrefs()->SetInteger(
prefs::kCookieControlsMode,
static_cast<int>(
checked ? content_settings::CookieControlsMode::kIncognitoOnly
: content_settings::CookieControlsMode::kOff));
base::RecordAction(
checked ? base::UserMetricsAction("CookieControls.NTP.Enabled")
: base::UserMetricsAction("CookieControls.NTP.Disabled"));
}
bool CookieControlsService::ShouldHideCookieControlsUI() {
return !base::FeatureList::IsEnabled(
content_settings::kImprovedCookieControls);
}
bool CookieControlsService::ShouldEnforceCookieControls() {
return GetCookieControlsEnforcement() !=
CookieControlsEnforcement::kNoEnforcement;
}
CookieControlsEnforcement
CookieControlsService::GetCookieControlsEnforcement() {
if (profile_->GetPrefs()->IsManagedPreference(prefs::kBlockThirdPartyCookies))
return CookieControlsEnforcement::kEnforcedByPolicy;
if (profile_->GetPrefs()->GetBoolean(prefs::kBlockThirdPartyCookies))
return CookieControlsEnforcement::kEnforcedByCookieSetting;
return CookieControlsEnforcement::kNoEnforcement;
}
bool CookieControlsService::GetToggleCheckedValue() {
return CookieSettingsFactory::GetForProfile(profile_)
->ShouldBlockThirdPartyCookies();
}
void CookieControlsService::OnThirdPartyCookieBlockingPrefChanged() {
for (Observer& obs : observers_)
obs.OnThirdPartyCookieBlockingPrefChanged();
}
void CookieControlsService::OnThirdPartyCookieBlockingPolicyChanged(
const base::Value* previous,
const base::Value* current) {
for (Observer& obs : observers_)
obs.OnThirdPartyCookieBlockingPolicyChanged();
}
// Copyright 2020 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_UI_COOKIE_CONTROLS_COOKIE_CONTROLS_SERVICE_H_
#define CHROME_BROWSER_UI_COOKIE_CONTROLS_COOKIE_CONTROLS_SERVICE_H_
#include <memory>
#include "base/observer_list.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "url/gurl.h"
class Profile;
namespace base {
class Value;
} // namespace base
namespace policy {
class PolicyChangeRegistrar;
}
enum class CookieControlsEnforcement {
kNoEnforcement,
kEnforcedByPolicy,
kEnforcedByCookieSetting
};
// // Handles the global state for cookie settings changes and observation.
class CookieControlsService : public KeyedService {
public:
class Observer : public base::CheckedObserver {
public:
virtual void OnThirdPartyCookieBlockingPrefChanged() {}
virtual void OnThirdPartyCookieBlockingPolicyChanged() {}
};
~CookieControlsService() override;
void Init();
void Shutdown() override;
void HandleCookieControlsToggleChanged(bool checked);
// Whether cookie controls UI should be hidden in incognito ntp.
bool ShouldHideCookieControlsUI();
// Whether cookie controls should appear enforced.
bool ShouldEnforceCookieControls();
CookieControlsEnforcement GetCookieControlsEnforcement();
bool GetToggleCheckedValue();
void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
private:
friend class CookieControlsServiceFactory;
// Use |CookieControlsServiceFactory::GetForProfile(..)| to get
// an instance of this service.
explicit CookieControlsService(Profile* profile);
void OnThirdPartyCookieBlockingPrefChanged();
void OnThirdPartyCookieBlockingPolicyChanged(const base::Value* previous,
const base::Value* current);
Profile* profile_;
PrefChangeRegistrar pref_change_registrar_;
std::unique_ptr<policy::PolicyChangeRegistrar> policy_registrar_;
base::ObserverList<Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(CookieControlsService);
};
#endif // CHROME_BROWSER_UI_COOKIE_CONTROLS_COOKIE_CONTROLS_SERVICE_H_
// Copyright 2020 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/ui/cookie_controls/cookie_controls_service_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/cookie_controls/cookie_controls_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
// static
CookieControlsService* CookieControlsServiceFactory::GetForProfile(
Profile* profile) {
return static_cast<CookieControlsService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
// static
CookieControlsServiceFactory* CookieControlsServiceFactory::GetInstance() {
return base::Singleton<CookieControlsServiceFactory>::get();
}
// static
KeyedService* CookieControlsServiceFactory::BuildInstanceFor(Profile* profile) {
return new CookieControlsService(profile);
}
CookieControlsServiceFactory::CookieControlsServiceFactory()
: BrowserContextKeyedServiceFactory(
"CookieControlsService",
BrowserContextDependencyManager::GetInstance()) {}
CookieControlsServiceFactory::~CookieControlsServiceFactory() = default;
content::BrowserContext* CookieControlsServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
// The incognito profile has its own CookieSettings. Therefore, it should
// get its own CookieControlsService.
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}
KeyedService* CookieControlsServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* profile) const {
return BuildInstanceFor(Profile::FromBrowserContext(profile));
}
// Copyright 2020 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_UI_COOKIE_CONTROLS_COOKIE_CONTROLS_SERVICE_FACTORY_H_
#define CHROME_BROWSER_UI_COOKIE_CONTROLS_COOKIE_CONTROLS_SERVICE_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class Profile;
class CookieControlsService;
// Factory to get or create an instance of CookieControlsService from
// a Profile.
class CookieControlsServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static CookieControlsService* GetForProfile(Profile* profile);
static CookieControlsServiceFactory* GetInstance();
// Used to create instances for testing.
static KeyedService* BuildInstanceFor(Profile* profile);
private:
friend struct base::DefaultSingletonTraits<CookieControlsServiceFactory>;
CookieControlsServiceFactory();
~CookieControlsServiceFactory() override;
// BrowserContextKeyedServiceFactory:
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* profile) const override;
};
#endif // CHROME_BROWSER_UI_COOKIE_CONTROLS_COOKIE_CONTROLS_SERVICE_FACTORY_H_
// Copyright 2020 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/ui/cookie_controls/cookie_controls_service.h"
#include "chrome/browser/ui/cookie_controls/cookie_controls_service_factory.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.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/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_web_ui.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "components/policy/core/common/policy_service.h"
// Handles requests for prefs::kCookieControlsMode retrival/update.
class CookieControlsServiceObserver : public CookieControlsService::Observer {
public:
explicit CookieControlsServiceObserver(Profile* profile) {
service_ = CookieControlsServiceFactory::GetForProfile(profile);
service_->AddObserver(this);
checked_ = false;
}
~CookieControlsServiceObserver() override = default;
CookieControlsService* GetService() { return service_; }
void SetChecked(bool checked) { checked_ = checked; }
bool GetChecked() { return checked_; }
// CookieControlsService::Observer
void OnThirdPartyCookieBlockingPrefChanged() override {
SetChecked(service_->GetToggleCheckedValue());
}
private:
CookieControlsService* service_;
bool checked_;
DISALLOW_COPY_AND_ASSIGN(CookieControlsServiceObserver);
};
class CookieControlsServiceTest : public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
feature_list_.InitAndEnableFeature(
content_settings::kImprovedCookieControls);
web_ui_.set_web_contents(web_contents());
}
void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); }
protected:
content::TestWebUI web_ui_;
std::unique_ptr<CookieControlsServiceObserver> observer_;
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_F(CookieControlsServiceTest, HandleCookieControlsToggleChanged) {
Profile* profile = Profile::FromBrowserContext(
web_ui_.GetWebContents()->GetBrowserContext());
observer_ = std::make_unique<CookieControlsServiceObserver>(profile);
EXPECT_EQ(
static_cast<int>(content_settings::CookieControlsMode::kIncognitoOnly),
profile->GetPrefs()->GetInteger(prefs::kCookieControlsMode));
// Set toggle value to false
observer_->SetChecked(true);
observer_->GetService()->HandleCookieControlsToggleChanged(false);
EXPECT_EQ(static_cast<int>(content_settings::CookieControlsMode::kOff),
profile->GetPrefs()->GetInteger(prefs::kCookieControlsMode));
EXPECT_EQ(observer_->GetChecked(), false);
// Set toggle value to true
observer_->GetService()->HandleCookieControlsToggleChanged(true);
EXPECT_EQ(
static_cast<int>(content_settings::CookieControlsMode::kIncognitoOnly),
profile->GetPrefs()->GetInteger(prefs::kCookieControlsMode));
// TestingProfile does not have a PolicyService for incognito and this
// should not create a checked value of "true" in normal mode.
EXPECT_EQ(observer_->GetChecked(), false);
}
...@@ -7,42 +7,21 @@ ...@@ -7,42 +7,21 @@
#include <utility> #include <utility>
#include "base/bind.h" #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 "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 "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/cookie_settings.h" #include "chrome/browser/ui/cookie_controls/cookie_controls_service.h"
#include "components/content_settings/core/common/features.h" #include "chrome/browser/ui/cookie_controls/cookie_controls_service_factory.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"
namespace { namespace {
static const char* kSettingsIcon = "cr:settings_icon"; static const char* kSettingsIcon = "cr:settings_icon";
static const char* kPolicyIcon = "cr20:domain"; static const char* kPolicyIcon = "cr20:domain";
enum class CookieControlsEnforcement {
kNoEnforcement,
kEnforcedByPolicy,
kEnforcedByCookieSetting
};
CookieControlsEnforcement GetCookieControlsEnforcement(const Profile* profile) {
if (profile->GetPrefs()->IsManagedPreference(prefs::kBlockThirdPartyCookies))
return CookieControlsEnforcement::kEnforcedByPolicy;
if (profile->GetPrefs()->GetBoolean(prefs::kBlockThirdPartyCookies))
return CookieControlsEnforcement::kEnforcedByCookieSetting;
return CookieControlsEnforcement::kNoEnforcement;
}
} // namespace } // namespace
CookieControlsHandler::CookieControlsHandler() {} CookieControlsHandler::CookieControlsHandler(Profile* profile) {
service_ = CookieControlsServiceFactory::GetForProfile(profile);
}
CookieControlsHandler::~CookieControlsHandler() {} CookieControlsHandler::~CookieControlsHandler() = default;
void CookieControlsHandler::RegisterMessages() { void CookieControlsHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback( web_ui()->RegisterMessageCallback(
...@@ -58,85 +37,30 @@ void CookieControlsHandler::RegisterMessages() { ...@@ -58,85 +37,30 @@ void CookieControlsHandler::RegisterMessages() {
} }
void CookieControlsHandler::OnJavascriptAllowed() { void CookieControlsHandler::OnJavascriptAllowed() {
Profile* profile = Profile::FromWebUI(web_ui()); service_->AddObserver(this);
pref_change_registrar_.Init(profile->GetPrefs());
pref_change_registrar_.Add(
prefs::kCookieControlsMode,
base::Bind(&CookieControlsHandler::SendCookieControlsUIChanges,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kBlockThirdPartyCookies,
base::Bind(&CookieControlsHandler::SendCookieControlsUIChanges,
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() { void CookieControlsHandler::OnJavascriptDisallowed() {
pref_change_registrar_.RemoveAll(); service_->RemoveObserver(this);
policy_registrar_.reset();
} }
void CookieControlsHandler::HandleCookieControlsToggleChanged( void CookieControlsHandler::HandleCookieControlsToggleChanged(
const base::ListValue* args) { const base::ListValue* args) {
bool checked; bool checked;
CHECK(args->GetBoolean(0, &checked)); CHECK(args->GetBoolean(0, &checked));
Profile* profile = Profile::FromWebUI(web_ui()); service_->HandleCookieControlsToggleChanged(checked);
profile->GetPrefs()->SetInteger(
prefs::kCookieControlsMode,
static_cast<int>(
checked ? content_settings::CookieControlsMode::kIncognitoOnly
: content_settings::CookieControlsMode::kOff));
base::RecordAction(
checked ? base::UserMetricsAction("CookieControls.NTP.Enabled")
: base::UserMetricsAction("CookieControls.NTP.Disabled"));
} }
void CookieControlsHandler::HandleObserveCookieControlsSettingsChanges( void CookieControlsHandler::HandleObserveCookieControlsSettingsChanges(
const base::ListValue* args) { const base::ListValue* args) {
AllowJavascript(); AllowJavascript();
SendCookieControlsUIChanges(); OnThirdPartyCookieBlockingPrefChanged();
}
bool CookieControlsHandler::GetToggleCheckedValue(const Profile* profile) {
return CookieSettingsFactory::GetForProfile(const_cast<Profile*>(profile))
->ShouldBlockThirdPartyCookies();
}
void CookieControlsHandler::SendCookieControlsUIChanges() {
Profile* profile = Profile::FromWebUI(web_ui());
base::DictionaryValue dict;
dict.SetBoolKey("enforced", ShouldEnforceCookieControls(profile));
dict.SetBoolKey("checked", GetToggleCheckedValue(profile));
dict.SetStringKey("icon", GetEnforcementIcon(profile));
FireWebUIListener("cookie-controls-changed", dict);
}
void CookieControlsHandler::OnThirdPartyCookieBlockingPolicyChanged(
const base::Value* previous,
const base::Value* current) {
SendCookieControlsUIChanges();
} }
bool CookieControlsHandler::ShouldHideCookieControlsUI(const Profile* profile) { const char* CookieControlsHandler::GetEnforcementIcon(Profile* profile) {
return !base::FeatureList::IsEnabled( CookieControlsService* service =
content_settings::kImprovedCookieControls); CookieControlsServiceFactory::GetForProfile(profile);
} switch (service->GetCookieControlsEnforcement()) {
bool CookieControlsHandler::ShouldEnforceCookieControls(
const Profile* profile) {
return GetCookieControlsEnforcement(profile) !=
CookieControlsEnforcement::kNoEnforcement;
}
const char* CookieControlsHandler::GetEnforcementIcon(const Profile* profile) {
switch (GetCookieControlsEnforcement(profile)) {
case CookieControlsEnforcement::kEnforcedByPolicy: case CookieControlsEnforcement::kEnforcedByPolicy:
return kPolicyIcon; return kPolicyIcon;
case CookieControlsEnforcement::kEnforcedByCookieSetting: case CookieControlsEnforcement::kEnforcedByCookieSetting:
...@@ -145,3 +69,20 @@ const char* CookieControlsHandler::GetEnforcementIcon(const Profile* profile) { ...@@ -145,3 +69,20 @@ const char* CookieControlsHandler::GetEnforcementIcon(const Profile* profile) {
return ""; return "";
} }
} }
void CookieControlsHandler::OnThirdPartyCookieBlockingPrefChanged() {
SendCookieControlsUIChanges();
}
void CookieControlsHandler::OnThirdPartyCookieBlockingPolicyChanged() {
SendCookieControlsUIChanges();
}
void CookieControlsHandler::SendCookieControlsUIChanges() {
Profile* profile = Profile::FromWebUI(web_ui());
base::DictionaryValue dict;
dict.SetBoolKey("enforced", service_->ShouldEnforceCookieControls());
dict.SetBoolKey("checked", service_->GetToggleCheckedValue());
dict.SetStringKey("icon", CookieControlsHandler::GetEnforcementIcon(profile));
FireWebUIListener("cookie-controls-changed", dict);
}
...@@ -7,25 +7,19 @@ ...@@ -7,25 +7,19 @@
#include <memory> #include <memory>
#include "components/prefs/pref_change_registrar.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/cookie_controls/cookie_controls_service.h"
#include "content/public/browser/web_ui_message_handler.h" #include "content/public/browser/web_ui_message_handler.h"
class CookieControlsHandlerTest;
class Profile;
namespace base { namespace base {
class ListValue; class ListValue;
class Value;
} // namespace base } // namespace base
namespace policy {
class PolicyChangeRegistrar;
}
// Handles requests for prefs::kCookieControlsMode retrival/update. // Handles requests for prefs::kCookieControlsMode retrival/update.
class CookieControlsHandler : public content::WebUIMessageHandler { class CookieControlsHandler : public content::WebUIMessageHandler,
public CookieControlsService::Observer {
public: public:
CookieControlsHandler(); explicit CookieControlsHandler(Profile* profile);
~CookieControlsHandler() override; ~CookieControlsHandler() override;
// WebUIMessageHandler // WebUIMessageHandler
...@@ -34,32 +28,19 @@ class CookieControlsHandler : public content::WebUIMessageHandler { ...@@ -34,32 +28,19 @@ class CookieControlsHandler : public content::WebUIMessageHandler {
void OnJavascriptDisallowed() override; void OnJavascriptDisallowed() override;
void HandleCookieControlsToggleChanged(const base::ListValue* args); void HandleCookieControlsToggleChanged(const base::ListValue* args);
void HandleObserveCookieControlsSettingsChanges(const base::ListValue* args); void HandleObserveCookieControlsSettingsChanges(const base::ListValue* args);
static const char* GetEnforcementIcon(Profile* profile);
// Whether cookie controls UI should be hidden in incognito ntp. // CookieControlsService::Observer
static bool ShouldHideCookieControlsUI(const Profile* profile); void OnThirdPartyCookieBlockingPrefChanged() override;
void OnThirdPartyCookieBlockingPolicyChanged() override;
// Whether cookie controls should appear enforced.
static bool ShouldEnforceCookieControls(const Profile* profile);
static const char* GetEnforcementIcon(const Profile* profile);
static bool GetToggleCheckedValue(const Profile* profile);
private: private:
friend class CookieControlsHandlerTest;
// Updates cookie controls UI when third-party cookie blocking setting has // Updates cookie controls UI when third-party cookie blocking setting has
// changed. // changed.
void SendCookieControlsUIChanges(); void SendCookieControlsUIChanges();
void OnThirdPartyCookieBlockingPolicyChanged(const base::Value* previous, CookieControlsService* service_;
const base::Value* current);
PrefChangeRegistrar pref_change_registrar_;
std::unique_ptr<policy::PolicyChangeRegistrar> policy_registrar_;
DISALLOW_COPY_AND_ASSIGN(CookieControlsHandler); DISALLOW_COPY_AND_ASSIGN(CookieControlsHandler);
}; };
......
// Copyright 2019 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/ui/webui/ntp/cookie_controls_handler.h"
#include "base/values.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/test_web_ui.h"
class CookieControlsHandlerTest : public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
web_ui_.set_web_contents(web_contents());
handler_ = std::make_unique<CookieControlsHandler>();
handler_->set_web_ui(&web_ui_);
}
void TearDown() override {
handler_->set_web_ui(nullptr);
ChromeRenderViewHostTestHarness::TearDown();
}
protected:
content::TestWebUI web_ui_;
std::unique_ptr<CookieControlsHandler> handler_;
};
TEST_F(CookieControlsHandlerTest, HandleCookieControlsToggleChanged) {
EXPECT_EQ(
static_cast<int>(content_settings::CookieControlsMode::kIncognitoOnly),
Profile::FromWebUI(&web_ui_)->GetPrefs()->GetInteger(
prefs::kCookieControlsMode));
base::ListValue args_false;
args_false.AppendBoolean(false);
handler_->HandleCookieControlsToggleChanged(&args_false);
EXPECT_EQ(static_cast<int>(content_settings::CookieControlsMode::kOff),
Profile::FromWebUI(&web_ui_)->GetPrefs()->GetInteger(
prefs::kCookieControlsMode));
base::ListValue args_true;
args_true.AppendBoolean(true);
handler_->HandleCookieControlsToggleChanged(&args_true);
EXPECT_EQ(
static_cast<int>(content_settings::CookieControlsMode::kIncognitoOnly),
Profile::FromWebUI(&web_ui_)->GetPrefs()->GetInteger(
prefs::kCookieControlsMode));
}
...@@ -59,7 +59,7 @@ NewTabUI::NewTabUI(content::WebUI* web_ui) : content::WebUIController(web_ui) { ...@@ -59,7 +59,7 @@ NewTabUI::NewTabUI(content::WebUI* web_ui) : content::WebUIController(web_ui) {
if (!profile->IsGuestSession()) { if (!profile->IsGuestSession()) {
web_ui->AddMessageHandler(std::make_unique<ThemeHandler>()); web_ui->AddMessageHandler(std::make_unique<ThemeHandler>());
web_ui->AddMessageHandler(std::make_unique<CookieControlsHandler>()); web_ui->AddMessageHandler(std::make_unique<CookieControlsHandler>(profile));
} }
// content::URLDataSource assumes the ownership of the html source. // content::URLDataSource assumes the ownership of the html source.
......
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
#include "chrome/browser/themes/theme_service.h" #include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/apps/app_info_dialog.h" #include "chrome/browser/ui/apps/app_info_dialog.h"
#include "chrome/browser/ui/cookie_controls/cookie_controls_service.h"
#include "chrome/browser/ui/cookie_controls/cookie_controls_service_factory.h"
#include "chrome/browser/ui/layout_constants.h" #include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/webui/app_launcher_login_handler.h" #include "chrome/browser/ui/webui/app_launcher_login_handler.h"
#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
...@@ -303,8 +305,10 @@ void NTPResourceCache::CreateNewTabIncognitoHTML() { ...@@ -303,8 +305,10 @@ void NTPResourceCache::CreateNewTabIncognitoHTML() {
replacements["learnMoreLink"] = kLearnMoreIncognitoUrl; replacements["learnMoreLink"] = kLearnMoreIncognitoUrl;
replacements["title"] = l10n_util::GetStringUTF8(IDS_NEW_TAB_TITLE); replacements["title"] = l10n_util::GetStringUTF8(IDS_NEW_TAB_TITLE);
replacements["hideCookieControls"] = replacements["hideCookieControls"] =
CookieControlsHandler::ShouldHideCookieControlsUI(profile_) ? "hidden" CookieControlsServiceFactory::GetForProfile(profile_)
: ""; ->ShouldHideCookieControlsUI()
? "hidden"
: "";
replacements["cookieControlsTitle"] = replacements["cookieControlsTitle"] =
l10n_util::GetStringUTF8(IDS_NEW_TAB_OTR_THIRD_PARTY_COOKIE); l10n_util::GetStringUTF8(IDS_NEW_TAB_OTR_THIRD_PARTY_COOKIE);
replacements["cookieControlsDescription"] = replacements["cookieControlsDescription"] =
...@@ -312,13 +316,16 @@ void NTPResourceCache::CreateNewTabIncognitoHTML() { ...@@ -312,13 +316,16 @@ void NTPResourceCache::CreateNewTabIncognitoHTML() {
// Ensure passing off-the-record profile; |profile_| might not be incognito. // Ensure passing off-the-record profile; |profile_| might not be incognito.
DCHECK(profile_->HasOffTheRecordProfile()); DCHECK(profile_->HasOffTheRecordProfile());
replacements["cookieControlsToggleChecked"] = replacements["cookieControlsToggleChecked"] =
CookieControlsHandler::GetToggleCheckedValue( CookieControlsServiceFactory::GetForProfile(
profile_->GetOffTheRecordProfile()) profile_->GetOffTheRecordProfile())
->GetToggleCheckedValue()
? "checked" ? "checked"
: ""; : "";
replacements["hideTooltipIcon"] = replacements["hideTooltipIcon"] =
CookieControlsHandler::ShouldEnforceCookieControls(profile_) ? "" CookieControlsServiceFactory::GetForProfile(profile_)
: "hidden"; ->ShouldEnforceCookieControls()
? ""
: "hidden";
replacements["cookieControlsToolTipIcon"] = replacements["cookieControlsToolTipIcon"] =
CookieControlsHandler::GetEnforcementIcon(profile_); CookieControlsHandler::GetEnforcementIcon(profile_);
replacements["cookieControlsTooltipText"] = l10n_util::GetStringFUTF8( replacements["cookieControlsTooltipText"] = l10n_util::GetStringFUTF8(
......
...@@ -3450,6 +3450,8 @@ test("unit_tests") { ...@@ -3450,6 +3450,8 @@ test("unit_tests") {
"../browser/ui/blocked_content/popup_opener_tab_helper_unittest.cc", "../browser/ui/blocked_content/popup_opener_tab_helper_unittest.cc",
"../browser/ui/blocked_content/safe_browsing_triggered_popup_blocker_unittest.cc", "../browser/ui/blocked_content/safe_browsing_triggered_popup_blocker_unittest.cc",
"../browser/ui/chrome_select_file_policy_unittest.cc", "../browser/ui/chrome_select_file_policy_unittest.cc",
"../browser/ui/cookie_controls/cookie_controls_controller_unittest.cc",
"../browser/ui/cookie_controls/cookie_controls_service_unittest.cc",
"../browser/ui/find_bar/find_backend_unittest.cc", "../browser/ui/find_bar/find_backend_unittest.cc",
"../browser/ui/login/login_handler_unittest.cc", "../browser/ui/login/login_handler_unittest.cc",
"../browser/ui/page_info/page_info_unittest.cc", "../browser/ui/page_info/page_info_unittest.cc",
...@@ -3563,7 +3565,6 @@ test("unit_tests") { ...@@ -3563,7 +3565,6 @@ test("unit_tests") {
"../browser/ui/webui/devtools_ui_data_source_unittest.cc", "../browser/ui/webui/devtools_ui_data_source_unittest.cc",
"../browser/ui/webui/discards/graph_dump_impl_unittest.cc", "../browser/ui/webui/discards/graph_dump_impl_unittest.cc",
"../browser/ui/webui/favicon_source_unittest.cc", "../browser/ui/webui/favicon_source_unittest.cc",
"../browser/ui/webui/ntp/cookie_controls_handler_unittest.cc",
"../browser/ui/webui/signin/sync_confirmation_handler_unittest.cc", "../browser/ui/webui/signin/sync_confirmation_handler_unittest.cc",
"../browser/upgrade_detector/build_state_unittest.cc", "../browser/upgrade_detector/build_state_unittest.cc",
"../browser/upgrade_detector/get_installed_version_win_unittest.cc", "../browser/upgrade_detector/get_installed_version_win_unittest.cc",
...@@ -4050,7 +4051,6 @@ test("unit_tests") { ...@@ -4050,7 +4051,6 @@ test("unit_tests") {
"../browser/ui/content_settings/content_setting_bubble_model_unittest.cc", "../browser/ui/content_settings/content_setting_bubble_model_unittest.cc",
"../browser/ui/content_settings/content_setting_image_model_unittest.cc", "../browser/ui/content_settings/content_setting_image_model_unittest.cc",
"../browser/ui/content_settings/content_setting_media_image_model_unittest.mm", "../browser/ui/content_settings/content_setting_media_image_model_unittest.mm",
"../browser/ui/cookie_controls/cookie_controls_controller_unittest.cc",
"../browser/ui/exclusive_access/fullscreen_controller_state_unittest.cc", "../browser/ui/exclusive_access/fullscreen_controller_state_unittest.cc",
"../browser/ui/extensions/extension_action_view_controller_unittest.cc", "../browser/ui/extensions/extension_action_view_controller_unittest.cc",
"../browser/ui/extensions/extension_installed_waiter_unittest.cc", "../browser/ui/extensions/extension_installed_waiter_unittest.cc",
......
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