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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
// Copyright 2015 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/chromeos/arc/arc_settings_bridge_impl.h"
#include <algorithm>
#include "base/json/json_writer.h"
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/pref_names.h"
#include "components/arc/common/settings.mojom.h"
using ::chromeos::system::TimezoneSettings;
namespace arc {
namespace fontsizes {
double ConvertFontSizeChromeToAndroid(int default_size,
int default_fixed_size,
int minimum_size) {
// kWebKitDefaultFixedFontSize is automatically set to be 3 pixels smaller
// than kWebKitDefaultFontSize when Chrome's settings page's main font
// dropdown control is adjusted. If the user specifically sets a higher
// fixed font size we will want to take into account the adjustment.
default_fixed_size += 3;
int max_chrome_size =
std::max(std::max(default_fixed_size, default_size), minimum_size);
double android_scale = kAndroidFontScaleSmall;
if (max_chrome_size >= kChromeFontSizeVeryLarge) {
android_scale = kAndroidFontScaleHuge;
} else if (max_chrome_size >= kChromeFontSizeLarge) {
android_scale = kAndroidFontScaleLarge;
} else if (max_chrome_size >= kChromeFontSizeNormal) {
android_scale = kAndroidFontScaleNormal;
}
return android_scale;
}
} // namespace fontsizes
ArcSettingsBridgeImpl::~ArcSettingsBridgeImpl() {
ArcBridgeService* bridge_service = ArcBridgeService::Get();
DCHECK(bridge_service);
bridge_service->RemoveObserver(this);
}
void ArcSettingsBridgeImpl::StartObservingBridgeServiceChanges() {
ArcBridgeService* bridge_service = ArcBridgeService::Get();
DCHECK(bridge_service);
bridge_service->AddObserver(this);
}
void ArcSettingsBridgeImpl::StartObservingSettingsChanges() {
Profile* profile = ProfileManager::GetActiveUserProfile();
registrar_.Init(profile->GetPrefs());
AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize);
AddPrefToObserve(prefs::kWebKitDefaultFontSize);
AddPrefToObserve(prefs::kWebKitMinimumFontSize);
AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled);
TimezoneSettings::GetInstance()->AddObserver(this);
}
void ArcSettingsBridgeImpl::SyncAllPrefs() const {
SyncFontSize();
SyncLocale();
SyncSpokenFeedbackEnabled();
SyncTimeZone();
}
void ArcSettingsBridgeImpl::StopObservingSettingsChanges() {
registrar_.RemoveAll();
TimezoneSettings::GetInstance()->RemoveObserver(this);
}
void ArcSettingsBridgeImpl::AddPrefToObserve(const std::string& pref_name) {
registrar_.Add(pref_name, base::Bind(&ArcSettingsBridgeImpl::OnPrefChanged,
base::Unretained(this)));
}
void ArcSettingsBridgeImpl::OnPrefChanged(const std::string& pref_name) const {
if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) {
SyncSpokenFeedbackEnabled();
} else if (pref_name == prefs::kWebKitDefaultFixedFontSize ||
pref_name == prefs::kWebKitDefaultFontSize ||
pref_name == prefs::kWebKitMinimumFontSize) {
SyncFontSize();
} else {
LOG(ERROR) << "Unknown pref changed.";
}
}
void ArcSettingsBridgeImpl::OnStateChanged(ArcBridgeService::State state) {
// ArcBridgeService::State::READY is emitted before ArcSettings app is ready
// to send broadcasts. Instead we wait for the SettingsInstance to be ready.
if (state == ArcBridgeService::State::STOPPING) {
StopObservingSettingsChanges();
}
}
void ArcSettingsBridgeImpl::OnSettingsInstanceReady() {
StartObservingSettingsChanges();
SyncAllPrefs();
}
void ArcSettingsBridgeImpl::TimezoneChanged(const icu::TimeZone& timezone) {
SyncTimeZone();
}
int ArcSettingsBridgeImpl::GetIntegerPref(const std::string& pref_name) const {
const PrefService::Preference* pref =
registrar_.prefs()->FindPreference(pref_name);
DCHECK(pref);
int val = -1;
bool value_exists = pref->GetValue()->GetAsInteger(&val);
DCHECK(value_exists);
return val;
}
void ArcSettingsBridgeImpl::SyncFontSize() const {
int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize);
int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize);
int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize);
double android_scale = fontsizes::ConvertFontSizeChromeToAndroid(
default_size, default_fixed_size, minimum_size);
base::DictionaryValue extras;
extras.SetDouble("scale", android_scale);
SendSettingsBroadcast("org.chromium.arc.settings.SET_FONT_SCALE", extras);
}
void ArcSettingsBridgeImpl::SyncSpokenFeedbackEnabled() const {
const PrefService::Preference* pref = registrar_.prefs()->FindPreference(
prefs::kAccessibilitySpokenFeedbackEnabled);
DCHECK(pref);
bool enabled = false;
bool value_exists = pref->GetValue()->GetAsBoolean(&enabled);
DCHECK(value_exists);
base::DictionaryValue extras;
extras.SetBoolean("enabled", enabled);
SendSettingsBroadcast("org.chromium.arc.settings.SET_SPOKEN_FEEDBACK_ENABLED",
extras);
}
void ArcSettingsBridgeImpl::SyncLocale() const {
const PrefService::Preference* pref =
registrar_.prefs()->FindPreference(prefs::kApplicationLocale);
DCHECK(pref);
std::string locale;
bool value_exists = pref->GetValue()->GetAsString(&locale);
DCHECK(value_exists);
base::DictionaryValue extras;
extras.SetString("locale", locale);
SendSettingsBroadcast("org.chromium.arc.settings.SET_LOCALE", extras);
}
void ArcSettingsBridgeImpl::SyncTimeZone() const {
TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance();
base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID();
base::DictionaryValue extras;
extras.SetString("olsonTimeZone", timezoneID);
SendSettingsBroadcast("org.chromium.arc.settings.SET_TIME_ZONE", extras);
}
void ArcSettingsBridgeImpl::SendSettingsBroadcast(
const std::string& action,
const base::DictionaryValue& extras) const {
ArcBridgeService* bridge_service = ArcBridgeService::Get();
if (!bridge_service ||
bridge_service->state() != ArcBridgeService::State::READY) {
LOG(ERROR) << "Bridge service is not ready.";
return;
}
std::string extras_json;
bool write_success = base::JSONWriter::Write(extras, &extras_json);
DCHECK(write_success);
bridge_service->settings_instance()->SendBroadcast(
action, "org.chromium.arc.settings",
"org.chromium.arc.settings.SettingsReceiver", extras_json);
}
} // namespace arc
|