1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
// 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 <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() {}
CookieControlsHandler::~CookieControlsHandler() {}
void CookieControlsHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"cookieControlsToggleChanged",
base::BindRepeating(
&CookieControlsHandler::HandleCookieControlsToggleChanged,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"observeCookieControlsSettingsChanges",
base::BindRepeating(
&CookieControlsHandler::HandleObserveCookieControlsSettingsChanges,
base::Unretained(this)));
}
void CookieControlsHandler::OnJavascriptAllowed() {
Profile* profile = Profile::FromWebUI(web_ui());
pref_change_registrar_.Init(profile->GetPrefs());
pref_change_registrar_.Add(
prefs::kCookieControlsMode,
base::Bind(&CookieControlsHandler::OnCookieControlsChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
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(
const base::ListValue* args) {
bool checked;
CHECK(args->GetBoolean(0, &checked));
Profile* profile = Profile::FromWebUI(web_ui());
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(
const base::ListValue* args) {
AllowJavascript();
OnCookieControlsChanged();
OnThirdPartyCookieBlockingChanged();
}
void CookieControlsHandler::OnCookieControlsChanged() {
Profile* profile = Profile::FromWebUI(web_ui());
FireWebUIListener("cookie-controls-changed",
base::Value(GetToggleCheckedValue(profile)));
}
bool CookieControlsHandler::GetToggleCheckedValue(const Profile* profile) {
int mode = profile->GetPrefs()->GetInteger(prefs::kCookieControlsMode);
return mode != static_cast<int>(content_settings::CookieControlsMode::kOff);
}
void CookieControlsHandler::OnThirdPartyCookieBlockingChanged() {
Profile* profile = Profile::FromWebUI(web_ui());
FireWebUIListener("third-party-cookie-blocking-changed",
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) ||
profile->GetPrefs()->IsManagedPreference(
prefs::kBlockThirdPartyCookies) ||
profile->GetPrefs()->GetBoolean(prefs::kBlockThirdPartyCookies);
}
|