Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(605)

Side by Side Diff: chrome/browser/chromeos/arc/settings_bridge.cc

Issue 1843563003: Move arc_intent_helper_bridge.h from c/b/chromeos/ to components/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address a comment Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/arc/settings_bridge.h"
6
7 #include <algorithm>
8
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "chrome/common/pref_names.h"
12 #include "chromeos/settings/cros_settings_names.h"
13 #include "components/prefs/pref_service.h"
14
15 using ::chromeos::CrosSettings;
16 using ::chromeos::system::TimezoneSettings;
17
18 namespace arc {
19
20 namespace fontsizes {
21
22 double ConvertFontSizeChromeToAndroid(int default_size,
23 int default_fixed_size,
24 int minimum_size) {
25 // kWebKitDefaultFixedFontSize is automatically set to be 3 pixels smaller
26 // than kWebKitDefaultFontSize when Chrome's settings page's main font
27 // dropdown control is adjusted. If the user specifically sets a higher
28 // fixed font size we will want to take into account the adjustment.
29 default_fixed_size += 3;
30 int max_chrome_size =
31 std::max(std::max(default_fixed_size, default_size), minimum_size);
32
33 double android_scale = kAndroidFontScaleSmall;
34 if (max_chrome_size >= kChromeFontSizeVeryLarge) {
35 android_scale = kAndroidFontScaleHuge;
36 } else if (max_chrome_size >= kChromeFontSizeLarge) {
37 android_scale = kAndroidFontScaleLarge;
38 } else if (max_chrome_size >= kChromeFontSizeNormal) {
39 android_scale = kAndroidFontScaleNormal;
40 }
41
42 return android_scale;
43 }
44
45 } // namespace fontsizes
46
47 SettingsBridge::SettingsBridge(SettingsBridge::Delegate* delegate)
48 : delegate_(delegate) {
49 DCHECK(delegate_);
50 StartObservingSettingsChanges();
51 SyncAllPrefs();
52 }
53
54 SettingsBridge::~SettingsBridge() {
55 StopObservingSettingsChanges();
56 }
57
58 void SettingsBridge::StartObservingSettingsChanges() {
59 Profile* profile = ProfileManager::GetActiveUserProfile();
60 registrar_.Init(profile->GetPrefs());
61
62 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize);
63 AddPrefToObserve(prefs::kWebKitDefaultFontSize);
64 AddPrefToObserve(prefs::kWebKitMinimumFontSize);
65 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled);
66 AddPrefToObserve(prefs::kUse24HourClock);
67
68 reporting_consent_subscription_ = CrosSettings::Get()->AddSettingsObserver(
69 chromeos::kStatsReportingPref,
70 base::Bind(&SettingsBridge::SyncReportingConsent,
71 base::Unretained(this)));
72
73 TimezoneSettings::GetInstance()->AddObserver(this);
74 }
75
76 void SettingsBridge::SyncAllPrefs() const {
77 SyncFontSize();
78 SyncLocale();
79 SyncReportingConsent();
80 SyncSpokenFeedbackEnabled();
81 SyncTimeZone();
82 SyncUse24HourClock();
83 }
84
85 void SettingsBridge::StopObservingSettingsChanges() {
86 registrar_.RemoveAll();
87 reporting_consent_subscription_.reset();
88
89 TimezoneSettings::GetInstance()->RemoveObserver(this);
90 }
91
92 void SettingsBridge::AddPrefToObserve(const std::string& pref_name) {
93 registrar_.Add(pref_name, base::Bind(&SettingsBridge::OnPrefChanged,
94 base::Unretained(this)));
95 }
96
97 void SettingsBridge::OnPrefChanged(const std::string& pref_name) const {
98 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) {
99 SyncSpokenFeedbackEnabled();
100 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize ||
101 pref_name == prefs::kWebKitDefaultFontSize ||
102 pref_name == prefs::kWebKitMinimumFontSize) {
103 SyncFontSize();
104 } else if (pref_name == prefs::kUse24HourClock) {
105 SyncUse24HourClock();
106 } else {
107 LOG(ERROR) << "Unknown pref changed.";
108 }
109 }
110
111 void SettingsBridge::TimezoneChanged(const icu::TimeZone& timezone) {
112 SyncTimeZone();
113 }
114
115 int SettingsBridge::GetIntegerPref(const std::string& pref_name) const {
116 const PrefService::Preference* pref =
117 registrar_.prefs()->FindPreference(pref_name);
118 DCHECK(pref);
119 int val = -1;
120 bool value_exists = pref->GetValue()->GetAsInteger(&val);
121 DCHECK(value_exists);
122 return val;
123 }
124
125 void SettingsBridge::SyncFontSize() const {
126 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize);
127 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize);
128 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize);
129
130 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid(
131 default_size, default_fixed_size, minimum_size);
132
133 base::DictionaryValue extras;
134 extras.SetDouble("scale", android_scale);
135 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_FONT_SCALE",
136 extras);
137 }
138
139 void SettingsBridge::SyncSpokenFeedbackEnabled() const {
140 const PrefService::Preference* pref = registrar_.prefs()->FindPreference(
141 prefs::kAccessibilitySpokenFeedbackEnabled);
142 DCHECK(pref);
143 bool enabled = false;
144 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled);
145 DCHECK(value_exists);
146 base::DictionaryValue extras;
147 extras.SetBoolean("enabled", enabled);
148 SendSettingsBroadcast(
149 "org.chromium.arc.intent_helper.SET_SPOKEN_FEEDBACK_ENABLED", extras);
150 }
151
152 void SettingsBridge::SyncLocale() const {
153 const PrefService::Preference* pref =
154 registrar_.prefs()->FindPreference(prefs::kApplicationLocale);
155 DCHECK(pref);
156 std::string locale;
157 bool value_exists = pref->GetValue()->GetAsString(&locale);
158 DCHECK(value_exists);
159 base::DictionaryValue extras;
160 extras.SetString("locale", locale);
161 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_LOCALE", extras);
162 }
163
164 void SettingsBridge::SyncReportingConsent() const {
165 bool consent = false;
166 CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, &consent);
167 base::DictionaryValue extras;
168 extras.SetBoolean("reportingConsent", consent);
169 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_REPORTING_CONSENT",
170 extras);
171 }
172
173 void SettingsBridge::SyncTimeZone() const {
174 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance();
175 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID();
176 base::DictionaryValue extras;
177 extras.SetString("olsonTimeZone", timezoneID);
178 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_TIME_ZONE", extras);
179 }
180
181 void SettingsBridge::SyncUse24HourClock() const {
182 const PrefService::Preference* pref =
183 registrar_.prefs()->FindPreference(prefs::kUse24HourClock);
184 DCHECK(pref);
185 bool use24HourClock = false;
186 bool value_exists = pref->GetValue()->GetAsBoolean(&use24HourClock);
187 DCHECK(value_exists);
188 base::DictionaryValue extras;
189 extras.SetBoolean("use24HourClock", use24HourClock);
190 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_USE_24_HOUR_CLOCK",
191 extras);
192 }
193
194 void SettingsBridge::SendSettingsBroadcast(
195 const std::string& action,
196 const base::DictionaryValue& extras) const {
197 delegate_->OnBroadcastNeeded(action, extras);
198 }
199
200 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/settings_bridge.h ('k') | chrome/browser/chromeos/arc/settings_bridge_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698