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