OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/arc/settings_bridge.h" | 5 #include "chrome/browser/chromeos/arc/arc_settings_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | |
8 | 9 |
10 #include "base/gtest_prod_util.h" | |
11 #include "base/json/json_writer.h" | |
9 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/values.h" | |
14 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
10 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
11 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
12 #include "chromeos/settings/cros_settings_names.h" | 17 #include "chromeos/settings/cros_settings_names.h" |
18 #include "chromeos/settings/timezone_settings.h" | |
19 #include "components/prefs/pref_change_registrar.h" | |
13 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
14 | 21 |
15 using ::chromeos::CrosSettings; | 22 using ::chromeos::CrosSettings; |
16 using ::chromeos::system::TimezoneSettings; | 23 using ::chromeos::system::TimezoneSettings; |
17 | 24 |
18 namespace arc { | 25 namespace arc { |
19 | 26 |
20 namespace fontsizes { | 27 namespace fontsizes { |
21 | 28 |
22 double ConvertFontSizeChromeToAndroid(int default_size, | 29 double ConvertFontSizeChromeToAndroid(int default_size, |
(...skipping 14 matching lines...) Expand all Loading... | |
37 android_scale = kAndroidFontScaleLarge; | 44 android_scale = kAndroidFontScaleLarge; |
38 } else if (max_chrome_size >= kChromeFontSizeNormal) { | 45 } else if (max_chrome_size >= kChromeFontSizeNormal) { |
39 android_scale = kAndroidFontScaleNormal; | 46 android_scale = kAndroidFontScaleNormal; |
40 } | 47 } |
41 | 48 |
42 return android_scale; | 49 return android_scale; |
43 } | 50 } |
44 | 51 |
45 } // namespace fontsizes | 52 } // namespace fontsizes |
46 | 53 |
47 SettingsBridge::SettingsBridge(SettingsBridge::Delegate* delegate) | 54 // Listens to changes for select Chrome settings (prefs) that Android cares |
48 : delegate_(delegate) { | 55 // about and sends the new values to Android to keep the state in sync. |
49 DCHECK(delegate_); | 56 class ArcSettingsServiceImpl |
57 : public chromeos::system::TimezoneSettings::Observer { | |
58 public: | |
59 explicit ArcSettingsServiceImpl(ArcBridgeService* arc_bridge_service); | |
60 ~ArcSettingsServiceImpl() override; | |
61 | |
62 // Called when a Chrome pref we have registered an observer for has changed. | |
63 // Obtains the new pref value and sends it to Android. | |
64 void OnPrefChanged(const std::string& pref_name) const; | |
65 | |
66 // TimezoneSettings::Observer | |
67 void TimezoneChanged(const icu::TimeZone& timezone) override; | |
68 | |
69 private: | |
70 // Registers to observe changes for Chrome settings we care about. | |
71 void StartObservingSettingsChanges(); | |
72 | |
73 // Stops listening for Chrome settings changes. | |
74 void StopObservingSettingsChanges(); | |
75 | |
76 // Retrives Chrome's state for the settings and send it to Android. | |
77 void SyncAllPrefs() const; | |
78 void SyncFontSize() const; | |
79 void SyncLocale() const; | |
80 void SyncReportingConsent() const; | |
81 void SyncSpokenFeedbackEnabled() const; | |
82 void SyncTimeZone() const; | |
83 void SyncUse24HourClock() const; | |
84 | |
85 // Registers to listen to a particular perf. | |
86 void AddPrefToObserve(const std::string& pref_name); | |
87 | |
88 // Returns the integer value of the pref. pref_name must exist. | |
89 int GetIntegerPref(const std::string& pref_name) const; | |
90 | |
91 // Sends a broadcast to the delegate. | |
92 void SendSettingsBroadcast(const std::string& action, | |
93 const base::DictionaryValue& extras) const; | |
94 | |
95 // Manages pref observation registration. | |
96 PrefChangeRegistrar registrar_; | |
97 | |
98 scoped_ptr<chromeos::CrosSettings::ObserverSubscription> | |
99 reporting_consent_subscription_; | |
100 ArcBridgeService* const arc_bridge_service_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(ArcSettingsServiceImpl); | |
103 }; | |
104 | |
105 ArcSettingsServiceImpl::ArcSettingsServiceImpl( | |
106 ArcBridgeService* arc_bridge_service) | |
107 : arc_bridge_service_(arc_bridge_service) { | |
50 StartObservingSettingsChanges(); | 108 StartObservingSettingsChanges(); |
51 SyncAllPrefs(); | 109 SyncAllPrefs(); |
52 } | 110 } |
53 | 111 |
54 SettingsBridge::~SettingsBridge() { | 112 ArcSettingsServiceImpl::~ArcSettingsServiceImpl() { |
55 StopObservingSettingsChanges(); | 113 StopObservingSettingsChanges(); |
56 } | 114 } |
57 | 115 |
58 void SettingsBridge::StartObservingSettingsChanges() { | 116 void ArcSettingsServiceImpl::StartObservingSettingsChanges() { |
59 Profile* profile = ProfileManager::GetActiveUserProfile(); | 117 Profile* profile = ProfileManager::GetActiveUserProfile(); |
60 registrar_.Init(profile->GetPrefs()); | 118 registrar_.Init(profile->GetPrefs()); |
61 | 119 |
62 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize); | 120 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize); |
63 AddPrefToObserve(prefs::kWebKitDefaultFontSize); | 121 AddPrefToObserve(prefs::kWebKitDefaultFontSize); |
64 AddPrefToObserve(prefs::kWebKitMinimumFontSize); | 122 AddPrefToObserve(prefs::kWebKitMinimumFontSize); |
65 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled); | 123 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled); |
66 AddPrefToObserve(prefs::kUse24HourClock); | 124 AddPrefToObserve(prefs::kUse24HourClock); |
67 | 125 |
68 reporting_consent_subscription_ = CrosSettings::Get()->AddSettingsObserver( | 126 reporting_consent_subscription_ = CrosSettings::Get()->AddSettingsObserver( |
69 chromeos::kStatsReportingPref, | 127 chromeos::kStatsReportingPref, |
70 base::Bind(&SettingsBridge::SyncReportingConsent, | 128 base::Bind(&ArcSettingsServiceImpl::SyncReportingConsent, |
71 base::Unretained(this))); | 129 base::Unretained(this))); |
72 | 130 |
73 TimezoneSettings::GetInstance()->AddObserver(this); | 131 TimezoneSettings::GetInstance()->AddObserver(this); |
74 } | 132 } |
75 | 133 |
76 void SettingsBridge::SyncAllPrefs() const { | 134 void ArcSettingsServiceImpl::SyncAllPrefs() const { |
77 SyncFontSize(); | 135 SyncFontSize(); |
78 SyncLocale(); | 136 SyncLocale(); |
79 SyncReportingConsent(); | 137 SyncReportingConsent(); |
80 SyncSpokenFeedbackEnabled(); | 138 SyncSpokenFeedbackEnabled(); |
81 SyncTimeZone(); | 139 SyncTimeZone(); |
82 SyncUse24HourClock(); | 140 SyncUse24HourClock(); |
83 } | 141 } |
84 | 142 |
85 void SettingsBridge::StopObservingSettingsChanges() { | 143 void ArcSettingsServiceImpl::StopObservingSettingsChanges() { |
86 registrar_.RemoveAll(); | 144 registrar_.RemoveAll(); |
87 reporting_consent_subscription_.reset(); | 145 reporting_consent_subscription_.reset(); |
88 | 146 |
89 TimezoneSettings::GetInstance()->RemoveObserver(this); | 147 TimezoneSettings::GetInstance()->RemoveObserver(this); |
90 } | 148 } |
91 | 149 |
92 void SettingsBridge::AddPrefToObserve(const std::string& pref_name) { | 150 void ArcSettingsServiceImpl::AddPrefToObserve(const std::string& pref_name) { |
93 registrar_.Add(pref_name, base::Bind(&SettingsBridge::OnPrefChanged, | 151 registrar_.Add(pref_name, base::Bind(&ArcSettingsServiceImpl::OnPrefChanged, |
94 base::Unretained(this))); | 152 base::Unretained(this))); |
95 } | 153 } |
96 | 154 |
97 void SettingsBridge::OnPrefChanged(const std::string& pref_name) const { | 155 void ArcSettingsServiceImpl::OnPrefChanged(const std::string& pref_name) const { |
98 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) { | 156 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) { |
99 SyncSpokenFeedbackEnabled(); | 157 SyncSpokenFeedbackEnabled(); |
100 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize || | 158 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize || |
101 pref_name == prefs::kWebKitDefaultFontSize || | 159 pref_name == prefs::kWebKitDefaultFontSize || |
102 pref_name == prefs::kWebKitMinimumFontSize) { | 160 pref_name == prefs::kWebKitMinimumFontSize) { |
103 SyncFontSize(); | 161 SyncFontSize(); |
104 } else if (pref_name == prefs::kUse24HourClock) { | 162 } else if (pref_name == prefs::kUse24HourClock) { |
105 SyncUse24HourClock(); | 163 SyncUse24HourClock(); |
106 } else { | 164 } else { |
107 LOG(ERROR) << "Unknown pref changed."; | 165 LOG(ERROR) << "Unknown pref changed."; |
108 } | 166 } |
109 } | 167 } |
110 | 168 |
111 void SettingsBridge::TimezoneChanged(const icu::TimeZone& timezone) { | 169 void ArcSettingsServiceImpl::TimezoneChanged(const icu::TimeZone& timezone) { |
112 SyncTimeZone(); | 170 SyncTimeZone(); |
113 } | 171 } |
114 | 172 |
115 int SettingsBridge::GetIntegerPref(const std::string& pref_name) const { | 173 int ArcSettingsServiceImpl::GetIntegerPref(const std::string& pref_name) const { |
116 const PrefService::Preference* pref = | 174 const PrefService::Preference* pref = |
117 registrar_.prefs()->FindPreference(pref_name); | 175 registrar_.prefs()->FindPreference(pref_name); |
118 DCHECK(pref); | 176 DCHECK(pref); |
119 int val = -1; | 177 int val = -1; |
120 bool value_exists = pref->GetValue()->GetAsInteger(&val); | 178 bool value_exists = pref->GetValue()->GetAsInteger(&val); |
121 DCHECK(value_exists); | 179 DCHECK(value_exists); |
122 return val; | 180 return val; |
123 } | 181 } |
124 | 182 |
125 void SettingsBridge::SyncFontSize() const { | 183 void ArcSettingsServiceImpl::SyncFontSize() const { |
126 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize); | 184 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize); |
127 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize); | 185 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize); |
128 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize); | 186 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize); |
129 | 187 |
130 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid( | 188 double android_scale = fontsizes::ConvertFontSizeChromeToAndroid( |
131 default_size, default_fixed_size, minimum_size); | 189 default_size, default_fixed_size, minimum_size); |
132 | 190 |
133 base::DictionaryValue extras; | 191 base::DictionaryValue extras; |
134 extras.SetDouble("scale", android_scale); | 192 extras.SetDouble("scale", android_scale); |
135 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_FONT_SCALE", | 193 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_FONT_SCALE", |
136 extras); | 194 extras); |
137 } | 195 } |
138 | 196 |
139 void SettingsBridge::SyncSpokenFeedbackEnabled() const { | 197 void ArcSettingsServiceImpl::SyncSpokenFeedbackEnabled() const { |
140 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( | 198 const PrefService::Preference* pref = registrar_.prefs()->FindPreference( |
141 prefs::kAccessibilitySpokenFeedbackEnabled); | 199 prefs::kAccessibilitySpokenFeedbackEnabled); |
142 DCHECK(pref); | 200 DCHECK(pref); |
143 bool enabled = false; | 201 bool enabled = false; |
144 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled); | 202 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled); |
145 DCHECK(value_exists); | 203 DCHECK(value_exists); |
146 base::DictionaryValue extras; | 204 base::DictionaryValue extras; |
147 extras.SetBoolean("enabled", enabled); | 205 extras.SetBoolean("enabled", enabled); |
148 SendSettingsBroadcast( | 206 SendSettingsBroadcast( |
149 "org.chromium.arc.intent_helper.SET_SPOKEN_FEEDBACK_ENABLED", extras); | 207 "org.chromium.arc.intent_helper.SET_SPOKEN_FEEDBACK_ENABLED", extras); |
150 } | 208 } |
151 | 209 |
152 void SettingsBridge::SyncLocale() const { | 210 void ArcSettingsServiceImpl::SyncLocale() const { |
153 const PrefService::Preference* pref = | 211 const PrefService::Preference* pref = |
154 registrar_.prefs()->FindPreference(prefs::kApplicationLocale); | 212 registrar_.prefs()->FindPreference(prefs::kApplicationLocale); |
155 DCHECK(pref); | 213 DCHECK(pref); |
156 std::string locale; | 214 std::string locale; |
157 bool value_exists = pref->GetValue()->GetAsString(&locale); | 215 bool value_exists = pref->GetValue()->GetAsString(&locale); |
158 DCHECK(value_exists); | 216 DCHECK(value_exists); |
159 base::DictionaryValue extras; | 217 base::DictionaryValue extras; |
160 extras.SetString("locale", locale); | 218 extras.SetString("locale", locale); |
161 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_LOCALE", extras); | 219 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_LOCALE", extras); |
162 } | 220 } |
163 | 221 |
164 void SettingsBridge::SyncReportingConsent() const { | 222 void ArcSettingsServiceImpl::SyncReportingConsent() const { |
165 bool consent = false; | 223 bool consent = false; |
166 CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, &consent); | 224 CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, &consent); |
167 base::DictionaryValue extras; | 225 base::DictionaryValue extras; |
168 extras.SetBoolean("reportingConsent", consent); | 226 extras.SetBoolean("reportingConsent", consent); |
169 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_REPORTING_CONSENT", | 227 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_REPORTING_CONSENT", |
170 extras); | 228 extras); |
171 } | 229 } |
172 | 230 |
173 void SettingsBridge::SyncTimeZone() const { | 231 void ArcSettingsServiceImpl::SyncTimeZone() const { |
174 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance(); | 232 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance(); |
175 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID(); | 233 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID(); |
176 base::DictionaryValue extras; | 234 base::DictionaryValue extras; |
177 extras.SetString("olsonTimeZone", timezoneID); | 235 extras.SetString("olsonTimeZone", timezoneID); |
178 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_TIME_ZONE", extras); | 236 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_TIME_ZONE", extras); |
179 } | 237 } |
180 | 238 |
181 void SettingsBridge::SyncUse24HourClock() const { | 239 void ArcSettingsServiceImpl::SyncUse24HourClock() const { |
182 const PrefService::Preference* pref = | 240 const PrefService::Preference* pref = |
183 registrar_.prefs()->FindPreference(prefs::kUse24HourClock); | 241 registrar_.prefs()->FindPreference(prefs::kUse24HourClock); |
184 DCHECK(pref); | 242 DCHECK(pref); |
185 bool use24HourClock = false; | 243 bool use24HourClock = false; |
186 bool value_exists = pref->GetValue()->GetAsBoolean(&use24HourClock); | 244 bool value_exists = pref->GetValue()->GetAsBoolean(&use24HourClock); |
187 DCHECK(value_exists); | 245 DCHECK(value_exists); |
188 base::DictionaryValue extras; | 246 base::DictionaryValue extras; |
189 extras.SetBoolean("use24HourClock", use24HourClock); | 247 extras.SetBoolean("use24HourClock", use24HourClock); |
190 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_USE_24_HOUR_CLOCK", | 248 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_USE_24_HOUR_CLOCK", |
191 extras); | 249 extras); |
192 } | 250 } |
193 | 251 |
194 void SettingsBridge::SendSettingsBroadcast( | 252 void ArcSettingsServiceImpl::SendSettingsBroadcast( |
195 const std::string& action, | 253 const std::string& action, |
196 const base::DictionaryValue& extras) const { | 254 const base::DictionaryValue& extras) const { |
197 delegate_->OnBroadcastNeeded(action, extras); | 255 if (arc_bridge_service_->state() != ArcBridgeService::State::READY) { |
Luis Héctor Chávez
2016/03/30 17:20:45
nit: It's probably better if you check for |arc_br
Yusuke Sato
2016/03/30 18:25:43
Done.
| |
256 LOG(ERROR) << "Bridge service is not ready."; | |
257 return; | |
258 } | |
259 | |
260 std::string extras_json; | |
261 bool write_success = base::JSONWriter::Write(extras, &extras_json); | |
262 DCHECK(write_success); | |
263 | |
264 if (arc_bridge_service_->intent_helper_version() >= 1) { | |
265 arc_bridge_service_->intent_helper_instance()->SendBroadcast( | |
266 action, "org.chromium.arc.intent_helper", | |
267 "org.chromium.arc.intent_helper.SettingsReceiver", extras_json); | |
268 } | |
269 } | |
270 | |
271 ArcSettingsService::ArcSettingsService(ArcBridgeService* bridge_service) | |
272 : ArcService(bridge_service) { | |
273 arc_bridge_service()->AddObserver(this); | |
274 } | |
275 | |
276 ArcSettingsService::~ArcSettingsService() { | |
277 arc_bridge_service()->RemoveObserver(this); | |
278 } | |
279 | |
280 void ArcSettingsService::OnIntentHelperInstanceReady() { | |
281 impl_.reset(new ArcSettingsServiceImpl(arc_bridge_service())); | |
282 } | |
283 | |
284 void ArcSettingsService::OnIntentHelperInstanceClosed() { | |
285 impl_.reset(); | |
198 } | 286 } |
199 | 287 |
200 } // namespace arc | 288 } // namespace arc |
OLD | NEW |