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

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

Issue 2441563002: arc: Create intermediate directories in c/b/c/arc (Closed)
Patch Set: Re-rebase to ToT Created 4 years, 1 month 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/arc_settings_service.h"
6
7 #include <string>
8
9 #include "base/gtest_prod_util.h"
10 #include "base/json/json_writer.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
15 #include "chrome/browser/chromeos/net/onc_utils.h"
16 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
17 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/common/pref_names.h"
20 #include "chromeos/network/network_handler.h"
21 #include "chromeos/network/network_state.h"
22 #include "chromeos/network/network_state_handler.h"
23 #include "chromeos/network/network_state_handler_observer.h"
24 #include "chromeos/settings/cros_settings_names.h"
25 #include "chromeos/settings/timezone_settings.h"
26 #include "components/arc/arc_bridge_service.h"
27 #include "components/arc/intent_helper/font_size_util.h"
28 #include "components/prefs/pref_change_registrar.h"
29 #include "components/prefs/pref_service.h"
30 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
31 #include "components/proxy_config/proxy_config_dictionary.h"
32 #include "components/proxy_config/proxy_config_pref_names.h"
33 #include "device/bluetooth/bluetooth_adapter.h"
34 #include "device/bluetooth/bluetooth_adapter_factory.h"
35 #include "net/proxy/proxy_config.h"
36
37 using ::chromeos::CrosSettings;
38 using ::chromeos::system::TimezoneSettings;
39
40 namespace {
41
42 constexpr uint32_t kMinVersionForSendBroadcast = 1;
43
44 bool GetHttpProxyServer(const ProxyConfigDictionary* proxy_config_dict,
45 std::string* host,
46 int* port) {
47 std::string proxy_rules_string;
48 if (!proxy_config_dict->GetProxyServer(&proxy_rules_string))
49 return false;
50
51 net::ProxyConfig::ProxyRules proxy_rules;
52 proxy_rules.ParseFromString(proxy_rules_string);
53
54 const net::ProxyList* proxy_list = nullptr;
55 if (proxy_rules.type == net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY) {
56 proxy_list = &proxy_rules.single_proxies;
57 } else if (proxy_rules.type ==
58 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME) {
59 proxy_list = proxy_rules.MapUrlSchemeToProxyList(url::kHttpScheme);
60 }
61 if (!proxy_list || proxy_list->IsEmpty())
62 return false;
63
64 const net::ProxyServer& server = proxy_list->Get();
65 *host = server.host_port_pair().host();
66 *port = server.host_port_pair().port();
67 return !host->empty() && *port;
68 }
69
70 // Returns whether kProxy pref proxy config is applied.
71 bool IsPrefProxyConfigApplied() {
72 net::ProxyConfig config;
73 Profile* profile = ProfileManager::GetActiveUserProfile();
74 return PrefProxyConfigTrackerImpl::PrefPrecedes(
75 PrefProxyConfigTrackerImpl::ReadPrefConfig(profile->GetPrefs(), &config));
76 }
77
78 } // namespace
79
80 namespace arc {
81
82 // Listens to changes for select Chrome settings (prefs) that Android cares
83 // about and sends the new values to Android to keep the state in sync.
84 class ArcSettingsServiceImpl
85 : public chromeos::system::TimezoneSettings::Observer,
86 public device::BluetoothAdapter::Observer,
87 public ArcAuthService::Observer,
88 public chromeos::NetworkStateHandlerObserver {
89 public:
90 explicit ArcSettingsServiceImpl(ArcBridgeService* arc_bridge_service);
91 ~ArcSettingsServiceImpl() override;
92
93 // Called when a Chrome pref we have registered an observer for has changed.
94 // Obtains the new pref value and sends it to Android.
95 void OnPrefChanged(const std::string& pref_name) const;
96
97 // TimezoneSettings::Observer:
98 void TimezoneChanged(const icu::TimeZone& timezone) override;
99
100 // BluetoothAdapter::Observer:
101 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
102 bool powered) override;
103
104 // ArcAuthService::Observer:
105 void OnInitialStart() override;
106
107 // NetworkStateHandlerObserver:
108 void DefaultNetworkChanged(const chromeos::NetworkState* network) override;
109
110 private:
111 // Registers to observe changes for Chrome settings we care about.
112 void StartObservingSettingsChanges();
113
114 // Stops listening for Chrome settings changes.
115 void StopObservingSettingsChanges();
116
117 // Retrieves Chrome's state for the settings that need to be synced on each
118 // Android boot and send it to Android.
119 void SyncRuntimeSettings() const;
120 // Send settings that need to be synced only on Android first start to
121 // Android.
122 void SyncInitialSettings() const;
123 void SyncFontSize() const;
124 void SyncLocale() const;
125 void SyncProxySettings() const;
126 void SyncReportingConsent() const;
127 void SyncSpokenFeedbackEnabled() const;
128 void SyncTimeZone() const;
129 void SyncUse24HourClock() const;
130 void SyncBackupEnabled() const;
131 void SyncLocationServiceEnabled() const;
132 void SyncAccessibilityVirtualKeyboardEnabled() const;
133
134 void OnBluetoothAdapterInitialized(
135 scoped_refptr<device::BluetoothAdapter> adapter);
136
137 // Registers to listen to a particular perf.
138 void AddPrefToObserve(const std::string& pref_name);
139
140 // Returns the integer value of the pref. pref_name must exist.
141 int GetIntegerPref(const std::string& pref_name) const;
142
143 // Sends boolean pref broadcast to the delegate.
144 void SendBoolPrefSettingsBroadcast(const std::string& pref_name,
145 const std::string& action) const;
146
147 // Sends a broadcast to the delegate.
148 void SendSettingsBroadcast(const std::string& action,
149 const base::DictionaryValue& extras) const;
150
151 // Manages pref observation registration.
152 PrefChangeRegistrar registrar_;
153
154 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
155 reporting_consent_subscription_;
156 ArcBridgeService* const arc_bridge_service_;
157
158 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
159
160 // WeakPtrFactory to use for callback for getting the bluetooth adapter.
161 base::WeakPtrFactory<ArcSettingsServiceImpl> weak_factory_;
162
163 DISALLOW_COPY_AND_ASSIGN(ArcSettingsServiceImpl);
164 };
165
166 ArcSettingsServiceImpl::ArcSettingsServiceImpl(
167 ArcBridgeService* arc_bridge_service)
168 : arc_bridge_service_(arc_bridge_service), weak_factory_(this) {
169 StartObservingSettingsChanges();
170 SyncRuntimeSettings();
171 DCHECK(ArcAuthService::Get());
172 ArcAuthService::Get()->AddObserver(this);
173 }
174
175 ArcSettingsServiceImpl::~ArcSettingsServiceImpl() {
176 StopObservingSettingsChanges();
177
178 ArcAuthService* arc_auth_service = ArcAuthService::Get();
179 if (arc_auth_service)
180 arc_auth_service->RemoveObserver(this);
181
182 if (bluetooth_adapter_)
183 bluetooth_adapter_->RemoveObserver(this);
184 }
185
186 void ArcSettingsServiceImpl::StartObservingSettingsChanges() {
187 Profile* profile = ProfileManager::GetActiveUserProfile();
188 registrar_.Init(profile->GetPrefs());
189
190 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize);
191 AddPrefToObserve(prefs::kWebKitDefaultFontSize);
192 AddPrefToObserve(prefs::kWebKitMinimumFontSize);
193 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled);
194 AddPrefToObserve(prefs::kUse24HourClock);
195 AddPrefToObserve(prefs::kArcBackupRestoreEnabled);
196 AddPrefToObserve(proxy_config::prefs::kProxy);
197 AddPrefToObserve(prefs::kDeviceOpenNetworkConfiguration);
198 AddPrefToObserve(prefs::kOpenNetworkConfiguration);
199 AddPrefToObserve(prefs::kAccessibilityVirtualKeyboardEnabled);
200
201 reporting_consent_subscription_ = CrosSettings::Get()->AddSettingsObserver(
202 chromeos::kStatsReportingPref,
203 base::Bind(&ArcSettingsServiceImpl::SyncReportingConsent,
204 base::Unretained(this)));
205
206 TimezoneSettings::GetInstance()->AddObserver(this);
207
208 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
209 device::BluetoothAdapterFactory::GetAdapter(
210 base::Bind(&ArcSettingsServiceImpl::OnBluetoothAdapterInitialized,
211 weak_factory_.GetWeakPtr()));
212 }
213
214 chromeos::NetworkHandler::Get()->network_state_handler()->AddObserver(
215 this, FROM_HERE);
216 }
217
218 void ArcSettingsServiceImpl::OnBluetoothAdapterInitialized(
219 scoped_refptr<device::BluetoothAdapter> adapter) {
220 DCHECK(adapter);
221 bluetooth_adapter_ = adapter;
222 bluetooth_adapter_->AddObserver(this);
223
224 AdapterPoweredChanged(adapter.get(), adapter->IsPowered());
225 }
226
227 void ArcSettingsServiceImpl::OnInitialStart() {
228 SyncInitialSettings();
229 }
230
231 void ArcSettingsServiceImpl::SyncRuntimeSettings() const {
232 SyncFontSize();
233 SyncLocale();
234 SyncProxySettings();
235 SyncReportingConsent();
236 SyncSpokenFeedbackEnabled();
237 SyncTimeZone();
238 SyncUse24HourClock();
239 SyncAccessibilityVirtualKeyboardEnabled();
240
241 const PrefService* const prefs =
242 ProfileManager::GetActiveUserProfile()->GetPrefs();
243 if (prefs->IsManagedPreference(prefs::kArcBackupRestoreEnabled))
244 SyncBackupEnabled();
245 if (prefs->IsManagedPreference(prefs::kArcLocationServiceEnabled))
246 SyncLocationServiceEnabled();
247 }
248
249 void ArcSettingsServiceImpl::SyncInitialSettings() const {
250 SyncBackupEnabled();
251 SyncLocationServiceEnabled();
252 }
253
254 void ArcSettingsServiceImpl::StopObservingSettingsChanges() {
255 registrar_.RemoveAll();
256 reporting_consent_subscription_.reset();
257
258 TimezoneSettings::GetInstance()->RemoveObserver(this);
259 chromeos::NetworkHandler::Get()->network_state_handler()->RemoveObserver(
260 this, FROM_HERE);
261 }
262
263 void ArcSettingsServiceImpl::AddPrefToObserve(const std::string& pref_name) {
264 registrar_.Add(pref_name, base::Bind(&ArcSettingsServiceImpl::OnPrefChanged,
265 base::Unretained(this)));
266 }
267
268 void ArcSettingsServiceImpl::AdapterPoweredChanged(
269 device::BluetoothAdapter* adapter,
270 bool powered) {
271 base::DictionaryValue extras;
272 extras.SetBoolean("enable", powered);
273 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_BLUETOOTH_STATE",
274 extras);
275 }
276
277 void ArcSettingsServiceImpl::OnPrefChanged(const std::string& pref_name) const {
278 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) {
279 SyncSpokenFeedbackEnabled();
280 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize ||
281 pref_name == prefs::kWebKitDefaultFontSize ||
282 pref_name == prefs::kWebKitMinimumFontSize) {
283 SyncFontSize();
284 } else if (pref_name == prefs::kUse24HourClock) {
285 SyncUse24HourClock();
286 } else if (pref_name == proxy_config::prefs::kProxy) {
287 SyncProxySettings();
288 } else if (pref_name == prefs::kDeviceOpenNetworkConfiguration ||
289 pref_name == prefs::kOpenNetworkConfiguration) {
290 // Only update proxy settings if kProxy pref is not applied.
291 if (IsPrefProxyConfigApplied()) {
292 LOG(ERROR) << "Open Network Configuration proxy settings are not applied,"
293 << " because kProxy preference is configured.";
294 return;
295 }
296 SyncProxySettings();
297 } else if (pref_name == prefs::kAccessibilityVirtualKeyboardEnabled) {
298 SyncAccessibilityVirtualKeyboardEnabled();
299 } else {
300 LOG(ERROR) << "Unknown pref changed.";
301 }
302 }
303
304 void ArcSettingsServiceImpl::TimezoneChanged(const icu::TimeZone& timezone) {
305 SyncTimeZone();
306 }
307
308 int ArcSettingsServiceImpl::GetIntegerPref(const std::string& pref_name) const {
309 const PrefService::Preference* pref =
310 registrar_.prefs()->FindPreference(pref_name);
311 DCHECK(pref);
312 int val = -1;
313 bool value_exists = pref->GetValue()->GetAsInteger(&val);
314 DCHECK(value_exists);
315 return val;
316 }
317
318 void ArcSettingsServiceImpl::SyncFontSize() const {
319 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize);
320 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize);
321 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize);
322
323 double android_scale = ConvertFontSizeChromeToAndroid(
324 default_size, default_fixed_size, minimum_size);
325
326 base::DictionaryValue extras;
327 extras.SetDouble("scale", android_scale);
328 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_FONT_SCALE",
329 extras);
330 }
331
332 void ArcSettingsServiceImpl::SendBoolPrefSettingsBroadcast(
333 const std::string& pref_name,
334 const std::string& action) const {
335 const PrefService::Preference* pref =
336 registrar_.prefs()->FindPreference(pref_name);
337 DCHECK(pref);
338 bool enabled = false;
339 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled);
340 DCHECK(value_exists);
341 base::DictionaryValue extras;
342 extras.SetBoolean("enabled", enabled);
343 extras.SetBoolean("managed", !pref->IsUserModifiable());
344 SendSettingsBroadcast(action, extras);
345 }
346
347 void ArcSettingsServiceImpl::SyncSpokenFeedbackEnabled() const {
348 SendBoolPrefSettingsBroadcast(
349 prefs::kAccessibilitySpokenFeedbackEnabled,
350 "org.chromium.arc.intent_helper.SET_SPOKEN_FEEDBACK_ENABLED");
351 }
352
353 void ArcSettingsServiceImpl::SyncLocale() const {
354 const PrefService::Preference* pref =
355 registrar_.prefs()->FindPreference(prefs::kApplicationLocale);
356 DCHECK(pref);
357 std::string locale;
358 bool value_exists = pref->GetValue()->GetAsString(&locale);
359 DCHECK(value_exists);
360 base::DictionaryValue extras;
361 extras.SetString("locale", locale);
362 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_LOCALE", extras);
363 }
364
365 void ArcSettingsServiceImpl::SyncReportingConsent() const {
366 bool consent = false;
367 CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, &consent);
368 base::DictionaryValue extras;
369 extras.SetBoolean("reportingConsent", consent);
370 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_REPORTING_CONSENT",
371 extras);
372 }
373
374 void ArcSettingsServiceImpl::SyncTimeZone() const {
375 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance();
376 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID();
377 base::DictionaryValue extras;
378 extras.SetString("olsonTimeZone", timezoneID);
379 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_TIME_ZONE", extras);
380 }
381
382 void ArcSettingsServiceImpl::SyncUse24HourClock() const {
383 const PrefService::Preference* pref =
384 registrar_.prefs()->FindPreference(prefs::kUse24HourClock);
385 DCHECK(pref);
386 bool use24HourClock = false;
387 bool value_exists = pref->GetValue()->GetAsBoolean(&use24HourClock);
388 DCHECK(value_exists);
389 base::DictionaryValue extras;
390 extras.SetBoolean("use24HourClock", use24HourClock);
391 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_USE_24_HOUR_CLOCK",
392 extras);
393 }
394
395 void ArcSettingsServiceImpl::SyncProxySettings() const {
396 std::unique_ptr<ProxyConfigDictionary> proxy_config_dict =
397 chromeos::ProxyConfigServiceImpl::GetActiveProxyConfigDictionary(
398 ProfileManager::GetActiveUserProfile()->GetPrefs());
399 if (!proxy_config_dict)
400 return;
401
402 ProxyPrefs::ProxyMode mode;
403 if (!proxy_config_dict || !proxy_config_dict->GetMode(&mode))
404 mode = ProxyPrefs::MODE_DIRECT;
405
406 base::DictionaryValue extras;
407 extras.SetString("mode", ProxyPrefs::ProxyModeToString(mode));
408
409 switch (mode) {
410 case ProxyPrefs::MODE_DIRECT:
411 break;
412 case ProxyPrefs::MODE_SYSTEM:
413 VLOG(1) << "The system mode is not translated.";
414 return;
415 case ProxyPrefs::MODE_AUTO_DETECT:
416 extras.SetString("pacUrl", "http://wpad/wpad.dat");
417 break;
418 case ProxyPrefs::MODE_PAC_SCRIPT: {
419 std::string pac_url;
420 if (!proxy_config_dict->GetPacUrl(&pac_url)) {
421 LOG(ERROR) << "No pac URL for pac_script proxy mode.";
422 return;
423 }
424 extras.SetString("pacUrl", pac_url);
425 break;
426 }
427 case ProxyPrefs::MODE_FIXED_SERVERS: {
428 std::string host;
429 int port = 0;
430 if (!GetHttpProxyServer(proxy_config_dict.get(), &host, &port)) {
431 LOG(ERROR) << "No Http proxy server is sent.";
432 return;
433 }
434 extras.SetString("host", host);
435 extras.SetInteger("port", port);
436
437 std::string bypass_list;
438 if (proxy_config_dict->GetBypassList(&bypass_list) &&
439 !bypass_list.empty()) {
440 extras.SetString("bypassList", bypass_list);
441 }
442 break;
443 }
444 default:
445 LOG(ERROR) << "Incorrect proxy mode.";
446 return;
447 }
448
449 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_PROXY", extras);
450 }
451
452 void ArcSettingsServiceImpl::SyncBackupEnabled() const {
453 SendBoolPrefSettingsBroadcast(
454 prefs::kArcBackupRestoreEnabled,
455 "org.chromium.arc.intent_helper.SET_BACKUP_ENABLED");
456 }
457
458 void ArcSettingsServiceImpl::SyncLocationServiceEnabled() const {
459 SendBoolPrefSettingsBroadcast(
460 prefs::kArcLocationServiceEnabled,
461 "org.chromium.arc.intent_helper.SET_LOCATION_SERVICE_ENABLED");
462 }
463
464 void ArcSettingsServiceImpl::SyncAccessibilityVirtualKeyboardEnabled() const {
465 SendBoolPrefSettingsBroadcast(
466 prefs::kAccessibilityVirtualKeyboardEnabled,
467 "org.chromium.arc.intent_helper.SET_SHOW_IME_WITH_HARD_KEYBOARD");
468 }
469
470 void ArcSettingsServiceImpl::SendSettingsBroadcast(
471 const std::string& action,
472 const base::DictionaryValue& extras) const {
473 auto* instance = arc_bridge_service_->intent_helper()->GetInstanceForMethod(
474 "SendBroadcast", kMinVersionForSendBroadcast);
475 if (!instance)
476 return;
477 std::string extras_json;
478 bool write_success = base::JSONWriter::Write(extras, &extras_json);
479 DCHECK(write_success);
480
481 instance->SendBroadcast(action, "org.chromium.arc.intent_helper",
482 "org.chromium.arc.intent_helper.SettingsReceiver",
483 extras_json);
484 }
485
486 void ArcSettingsServiceImpl::DefaultNetworkChanged(
487 const chromeos::NetworkState* network) {
488 // kProxy pref has more priority than the default network update.
489 // If a default network is changed to the network with ONC policy with proxy
490 // settings, it should be translated here.
491 if (network && !IsPrefProxyConfigApplied())
492 SyncProxySettings();
493 }
494
495 ArcSettingsService::ArcSettingsService(ArcBridgeService* bridge_service)
496 : ArcService(bridge_service) {
497 arc_bridge_service()->intent_helper()->AddObserver(this);
498 }
499
500 ArcSettingsService::~ArcSettingsService() {
501 arc_bridge_service()->intent_helper()->RemoveObserver(this);
502 }
503
504 void ArcSettingsService::OnInstanceReady() {
505 impl_.reset(new ArcSettingsServiceImpl(arc_bridge_service()));
506 }
507
508 void ArcSettingsService::OnInstanceClosed() {
509 impl_.reset();
510 }
511
512 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_settings_service.h ('k') | chrome/browser/chromeos/arc/arc_settings_service_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698