blob: 177f5093f926338b5b239122f62e791260e9e7eb [file] [log] [blame]
// 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/bind.h"
#include "base/feature_list.h"
#include "base/values.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/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)));
}
void CookieControlsHandler::OnJavascriptDisallowed() {
pref_change_registrar_.RemoveAll();
}
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));
}
void CookieControlsHandler::HandleObserveCookieControlsSettingsChanges(
const base::ListValue* args) {
AllowJavascript();
OnCookieControlsChanged();
OnThirdPartyCookieBlockingChanged();
}
void CookieControlsHandler::OnCookieControlsChanged() {
Profile* profile = Profile::FromWebUI(web_ui());
int mode = profile->GetPrefs()->GetInteger(prefs::kCookieControlsMode);
base::Value checked(
mode == static_cast<int>(content_settings::CookieControlsMode::kOff)
? false
: true);
FireWebUIListener("cookie-controls-changed", checked);
}
void CookieControlsHandler::OnThirdPartyCookieBlockingChanged() {
Profile* profile = Profile::FromWebUI(web_ui());
FireWebUIListener("third-party-cookie-blocking-changed",
base::Value(ShouldHideCookieControlsUI(profile)));
}
bool CookieControlsHandler::ShouldHideCookieControlsUI(const Profile* profile) {
return !base::FeatureList::IsEnabled(
content_settings::kImprovedCookieControls) ||
profile->GetPrefs()->IsManagedPreference(
prefs::kBlockThirdPartyCookies) ||
profile->GetPrefs()->GetBoolean(prefs::kBlockThirdPartyCookies);
}