-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathbrave_appearance_handler.cc
134 lines (119 loc) · 4.94 KB
/
brave_appearance_handler.cc
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "brave/browser/ui/webui/settings/brave_appearance_handler.h"
#include "base/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "brave/browser/new_tab/new_tab_shows_options.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/browser/themes/brave_dark_mode_utils.h"
#include "brave/common/pref_names.h"
#include "brave/components/ntp_background_images/common/pref_names.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_ui.h"
BraveAppearanceHandler::BraveAppearanceHandler() {
local_state_change_registrar_.Init(g_browser_process->local_state());
local_state_change_registrar_.Add(
kBraveDarkMode,
base::Bind(&BraveAppearanceHandler::OnBraveDarkModeChanged,
base::Unretained(this)));
}
BraveAppearanceHandler::~BraveAppearanceHandler() = default;
// TODO(simonhong): Use separate handler for NTP settings.
void BraveAppearanceHandler::RegisterMessages() {
profile_ = Profile::FromWebUI(web_ui());
profile_state_change_registrar_.Init(profile_->GetPrefs());
profile_state_change_registrar_.Add(
kNewTabPageShowsOptions,
base::BindRepeating(&BraveAppearanceHandler::OnPreferenceChanged,
base::Unretained(this)));
profile_state_change_registrar_.Add(
prefs::kHomePageIsNewTabPage,
base::BindRepeating(&BraveAppearanceHandler::OnPreferenceChanged,
base::Unretained(this)));
profile_state_change_registrar_.Add(
prefs::kHomePage,
base::BindRepeating(&BraveAppearanceHandler::OnPreferenceChanged,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setBraveThemeType",
base::BindRepeating(&BraveAppearanceHandler::SetBraveThemeType,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getBraveThemeType",
base::BindRepeating(&BraveAppearanceHandler::GetBraveThemeType,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getNewTabShowsOptionsList",
base::BindRepeating(&BraveAppearanceHandler::GetNewTabShowsOptionsList,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"shouldShowNewTabDashboardSettings",
base::BindRepeating(
&BraveAppearanceHandler::ShouldShowNewTabDashboardSettings,
base::Unretained(this)));
}
void BraveAppearanceHandler::SetBraveThemeType(const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
AllowJavascript();
int int_type;
args->GetInteger(0, &int_type);
dark_mode::SetBraveDarkModeType(
static_cast<dark_mode::BraveDarkModeType>(int_type));
}
void BraveAppearanceHandler::GetBraveThemeType(const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
AllowJavascript();
// GetBraveThemeType() should be used because settings option displays all
// available options including default.
ResolveJavascriptCallback(
args->GetList()[0],
base::Value(static_cast<int>(dark_mode::GetBraveDarkModeType())));
}
void BraveAppearanceHandler::OnBraveDarkModeChanged() {
// GetBraveThemeType() should be used because settings option displays all
// available options including default.
if (IsJavascriptAllowed()) {
FireWebUIListener(
"brave-theme-type-changed",
base::Value(static_cast<int>(dark_mode::GetBraveDarkModeType())));
}
}
void BraveAppearanceHandler::OnBackgroundPreferenceChanged(
const std::string& pref_name) {
brave::RecordSponsoredImagesEnabledP3A(profile_);
}
void BraveAppearanceHandler::OnPreferenceChanged(const std::string& pref_name) {
if (IsJavascriptAllowed()) {
if (pref_name == kNewTabPageShowsOptions ||
pref_name == prefs::kHomePage ||
pref_name == prefs::kHomePageIsNewTabPage) {
FireWebUIListener(
"show-new-tab-dashboard-settings-changed",
base::Value(brave::ShouldNewTabShowDashboard(profile_)));
return;
}
}
}
void BraveAppearanceHandler::GetNewTabShowsOptionsList(
const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
AllowJavascript();
ResolveJavascriptCallback(args->GetList()[0],
brave::GetNewTabShowsOptionsList(profile_));
}
void BraveAppearanceHandler::ShouldShowNewTabDashboardSettings(
const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
AllowJavascript();
ResolveJavascriptCallback(
args->GetList()[0],
base::Value(brave::ShouldNewTabShowDashboard(profile_)));
}