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

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: Moved the boot notification to notification/ Created 4 years, 2 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/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
133 void OnBluetoothAdapterInitialized(
134 scoped_refptr<device::BluetoothAdapter> adapter);
135
136 // Registers to listen to a particular perf.
137 void AddPrefToObserve(const std::string& pref_name);
138
139 // Returns the integer value of the pref. pref_name must exist.
140 int GetIntegerPref(const std::string& pref_name) const;
141
142 // Sends boolean pref broadcast to the delegate.
143 void SendBoolPrefSettingsBroadcast(const std::string& pref_name,
144 const std::string& action) const;
145
146 // Sends a broadcast to the delegate.
147 void SendSettingsBroadcast(const std::string& action,
148 const base::DictionaryValue& extras) const;
149
150 // Manages pref observation registration.
151 PrefChangeRegistrar registrar_;
152
153 std::unique_ptr<chromeos::CrosSettings::ObserverSubscription>
154 reporting_consent_subscription_;
155 ArcBridgeService* const arc_bridge_service_;
156
157 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
158
159 // WeakPtrFactory to use for callback for getting the bluetooth adapter.
160 base::WeakPtrFactory<ArcSettingsServiceImpl> weak_factory_;
161
162 DISALLOW_COPY_AND_ASSIGN(ArcSettingsServiceImpl);
163 };
164
165 ArcSettingsServiceImpl::ArcSettingsServiceImpl(
166 ArcBridgeService* arc_bridge_service)
167 : arc_bridge_service_(arc_bridge_service), weak_factory_(this) {
168 StartObservingSettingsChanges();
169 SyncRuntimeSettings();
170 DCHECK(ArcAuthService::Get());
171 ArcAuthService::Get()->AddObserver(this);
172 }
173
174 ArcSettingsServiceImpl::~ArcSettingsServiceImpl() {
175 StopObservingSettingsChanges();
176
177 ArcAuthService* arc_auth_service = ArcAuthService::Get();
178 if (arc_auth_service)
179 arc_auth_service->RemoveObserver(this);
180
181 if (bluetooth_adapter_)
182 bluetooth_adapter_->RemoveObserver(this);
183 }
184
185 void ArcSettingsServiceImpl::StartObservingSettingsChanges() {
186 Profile* profile = ProfileManager::GetActiveUserProfile();
187 registrar_.Init(profile->GetPrefs());
188
189 AddPrefToObserve(prefs::kWebKitDefaultFixedFontSize);
190 AddPrefToObserve(prefs::kWebKitDefaultFontSize);
191 AddPrefToObserve(prefs::kWebKitMinimumFontSize);
192 AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled);
193 AddPrefToObserve(prefs::kUse24HourClock);
194 AddPrefToObserve(prefs::kArcBackupRestoreEnabled);
195 AddPrefToObserve(proxy_config::prefs::kProxy);
196 AddPrefToObserve(prefs::kDeviceOpenNetworkConfiguration);
197 AddPrefToObserve(prefs::kOpenNetworkConfiguration);
198
199 reporting_consent_subscription_ = CrosSettings::Get()->AddSettingsObserver(
200 chromeos::kStatsReportingPref,
201 base::Bind(&ArcSettingsServiceImpl::SyncReportingConsent,
202 base::Unretained(this)));
203
204 TimezoneSettings::GetInstance()->AddObserver(this);
205
206 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
207 device::BluetoothAdapterFactory::GetAdapter(
208 base::Bind(&ArcSettingsServiceImpl::OnBluetoothAdapterInitialized,
209 weak_factory_.GetWeakPtr()));
210 }
211
212 chromeos::NetworkHandler::Get()->network_state_handler()->AddObserver(
213 this, FROM_HERE);
214 }
215
216 void ArcSettingsServiceImpl::OnBluetoothAdapterInitialized(
217 scoped_refptr<device::BluetoothAdapter> adapter) {
218 DCHECK(adapter);
219 bluetooth_adapter_ = adapter;
220 bluetooth_adapter_->AddObserver(this);
221
222 AdapterPoweredChanged(adapter.get(), adapter->IsPowered());
223 }
224
225 void ArcSettingsServiceImpl::OnInitialStart() {
226 SyncInitialSettings();
227 }
228
229 void ArcSettingsServiceImpl::SyncRuntimeSettings() const {
230 SyncFontSize();
231 SyncLocale();
232 SyncProxySettings();
233 SyncReportingConsent();
234 SyncSpokenFeedbackEnabled();
235 SyncTimeZone();
236 SyncUse24HourClock();
237
238 const PrefService* const prefs =
239 ProfileManager::GetActiveUserProfile()->GetPrefs();
240 if (prefs->IsManagedPreference(prefs::kArcBackupRestoreEnabled))
241 SyncBackupEnabled();
242 if (prefs->IsManagedPreference(prefs::kArcLocationServiceEnabled))
243 SyncLocationServiceEnabled();
244 }
245
246 void ArcSettingsServiceImpl::SyncInitialSettings() const {
247 SyncBackupEnabled();
248 SyncLocationServiceEnabled();
249 }
250
251 void ArcSettingsServiceImpl::StopObservingSettingsChanges() {
252 registrar_.RemoveAll();
253 reporting_consent_subscription_.reset();
254
255 TimezoneSettings::GetInstance()->RemoveObserver(this);
256 chromeos::NetworkHandler::Get()->network_state_handler()->RemoveObserver(
257 this, FROM_HERE);
258 }
259
260 void ArcSettingsServiceImpl::AddPrefToObserve(const std::string& pref_name) {
261 registrar_.Add(pref_name, base::Bind(&ArcSettingsServiceImpl::OnPrefChanged,
262 base::Unretained(this)));
263 }
264
265 void ArcSettingsServiceImpl::AdapterPoweredChanged(
266 device::BluetoothAdapter* adapter,
267 bool powered) {
268 base::DictionaryValue extras;
269 extras.SetBoolean("enable", powered);
270 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_BLUETOOTH_STATE",
271 extras);
272 }
273
274 void ArcSettingsServiceImpl::OnPrefChanged(const std::string& pref_name) const {
275 if (pref_name == prefs::kAccessibilitySpokenFeedbackEnabled) {
276 SyncSpokenFeedbackEnabled();
277 } else if (pref_name == prefs::kWebKitDefaultFixedFontSize ||
278 pref_name == prefs::kWebKitDefaultFontSize ||
279 pref_name == prefs::kWebKitMinimumFontSize) {
280 SyncFontSize();
281 } else if (pref_name == prefs::kUse24HourClock) {
282 SyncUse24HourClock();
283 } else if (pref_name == proxy_config::prefs::kProxy) {
284 SyncProxySettings();
285 } else if (pref_name == prefs::kDeviceOpenNetworkConfiguration ||
286 pref_name == prefs::kOpenNetworkConfiguration) {
287 // Only update proxy settings if kProxy pref is not applied.
288 if (IsPrefProxyConfigApplied()) {
289 LOG(ERROR) << "Open Network Configuration proxy settings are not applied,"
290 << " because kProxy preference is configured.";
291 return;
292 }
293 SyncProxySettings();
294 } else {
295 LOG(ERROR) << "Unknown pref changed.";
296 }
297 }
298
299 void ArcSettingsServiceImpl::TimezoneChanged(const icu::TimeZone& timezone) {
300 SyncTimeZone();
301 }
302
303 int ArcSettingsServiceImpl::GetIntegerPref(const std::string& pref_name) const {
304 const PrefService::Preference* pref =
305 registrar_.prefs()->FindPreference(pref_name);
306 DCHECK(pref);
307 int val = -1;
308 bool value_exists = pref->GetValue()->GetAsInteger(&val);
309 DCHECK(value_exists);
310 return val;
311 }
312
313 void ArcSettingsServiceImpl::SyncFontSize() const {
314 int default_size = GetIntegerPref(prefs::kWebKitDefaultFontSize);
315 int default_fixed_size = GetIntegerPref(prefs::kWebKitDefaultFixedFontSize);
316 int minimum_size = GetIntegerPref(prefs::kWebKitMinimumFontSize);
317
318 double android_scale = ConvertFontSizeChromeToAndroid(
319 default_size, default_fixed_size, minimum_size);
320
321 base::DictionaryValue extras;
322 extras.SetDouble("scale", android_scale);
323 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_FONT_SCALE",
324 extras);
325 }
326
327 void ArcSettingsServiceImpl::SendBoolPrefSettingsBroadcast(
328 const std::string& pref_name,
329 const std::string& action) const {
330 const PrefService::Preference* pref =
331 registrar_.prefs()->FindPreference(pref_name);
332 DCHECK(pref);
333 bool enabled = false;
334 bool value_exists = pref->GetValue()->GetAsBoolean(&enabled);
335 DCHECK(value_exists);
336 base::DictionaryValue extras;
337 extras.SetBoolean("enabled", enabled);
338 extras.SetBoolean("managed", !pref->IsUserModifiable());
339 SendSettingsBroadcast(action, extras);
340 }
341
342 void ArcSettingsServiceImpl::SyncSpokenFeedbackEnabled() const {
343 SendBoolPrefSettingsBroadcast(
344 prefs::kAccessibilitySpokenFeedbackEnabled,
345 "org.chromium.arc.intent_helper.SET_SPOKEN_FEEDBACK_ENABLED");
346 }
347
348 void ArcSettingsServiceImpl::SyncLocale() const {
349 const PrefService::Preference* pref =
350 registrar_.prefs()->FindPreference(prefs::kApplicationLocale);
351 DCHECK(pref);
352 std::string locale;
353 bool value_exists = pref->GetValue()->GetAsString(&locale);
354 DCHECK(value_exists);
355 base::DictionaryValue extras;
356 extras.SetString("locale", locale);
357 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_LOCALE", extras);
358 }
359
360 void ArcSettingsServiceImpl::SyncReportingConsent() const {
361 bool consent = false;
362 CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, &consent);
363 base::DictionaryValue extras;
364 extras.SetBoolean("reportingConsent", consent);
365 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_REPORTING_CONSENT",
366 extras);
367 }
368
369 void ArcSettingsServiceImpl::SyncTimeZone() const {
370 TimezoneSettings* timezone_settings = TimezoneSettings::GetInstance();
371 base::string16 timezoneID = timezone_settings->GetCurrentTimezoneID();
372 base::DictionaryValue extras;
373 extras.SetString("olsonTimeZone", timezoneID);
374 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_TIME_ZONE", extras);
375 }
376
377 void ArcSettingsServiceImpl::SyncUse24HourClock() const {
378 const PrefService::Preference* pref =
379 registrar_.prefs()->FindPreference(prefs::kUse24HourClock);
380 DCHECK(pref);
381 bool use24HourClock = false;
382 bool value_exists = pref->GetValue()->GetAsBoolean(&use24HourClock);
383 DCHECK(value_exists);
384 base::DictionaryValue extras;
385 extras.SetBoolean("use24HourClock", use24HourClock);
386 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_USE_24_HOUR_CLOCK",
387 extras);
388 }
389
390 void ArcSettingsServiceImpl::SyncProxySettings() const {
391 std::unique_ptr<ProxyConfigDictionary> proxy_config_dict =
392 chromeos::ProxyConfigServiceImpl::GetActiveProxyConfigDictionary(
393 ProfileManager::GetActiveUserProfile()->GetPrefs());
394 if (!proxy_config_dict)
395 return;
396
397 ProxyPrefs::ProxyMode mode;
398 if (!proxy_config_dict || !proxy_config_dict->GetMode(&mode))
399 mode = ProxyPrefs::MODE_DIRECT;
400
401 base::DictionaryValue extras;
402 extras.SetString("mode", ProxyPrefs::ProxyModeToString(mode));
403
404 switch (mode) {
405 case ProxyPrefs::MODE_DIRECT:
406 break;
407 case ProxyPrefs::MODE_SYSTEM:
408 VLOG(1) << "The system mode is not translated.";
409 return;
410 case ProxyPrefs::MODE_AUTO_DETECT:
411 extras.SetString("pacUrl", "http://wpad/wpad.dat");
412 break;
413 case ProxyPrefs::MODE_PAC_SCRIPT: {
414 std::string pac_url;
415 if (!proxy_config_dict->GetPacUrl(&pac_url)) {
416 LOG(ERROR) << "No pac URL for pac_script proxy mode.";
417 return;
418 }
419 extras.SetString("pacUrl", pac_url);
420 break;
421 }
422 case ProxyPrefs::MODE_FIXED_SERVERS: {
423 std::string host;
424 int port = 0;
425 if (!GetHttpProxyServer(proxy_config_dict.get(), &host, &port)) {
426 LOG(ERROR) << "No Http proxy server is sent.";
427 return;
428 }
429 extras.SetString("host", host);
430 extras.SetInteger("port", port);
431
432 std::string bypass_list;
433 if (proxy_config_dict->GetBypassList(&bypass_list) &&
434 !bypass_list.empty()) {
435 extras.SetString("bypassList", bypass_list);
436 }
437 break;
438 }
439 default:
440 LOG(ERROR) << "Incorrect proxy mode.";
441 return;
442 }
443
444 SendSettingsBroadcast("org.chromium.arc.intent_helper.SET_PROXY", extras);
445 }
446
447 void ArcSettingsServiceImpl::SyncBackupEnabled() const {
448 SendBoolPrefSettingsBroadcast(
449 prefs::kArcBackupRestoreEnabled,
450 "org.chromium.arc.intent_helper.SET_BACKUP_ENABLED");
451 }
452
453 void ArcSettingsServiceImpl::SyncLocationServiceEnabled() const {
454 SendBoolPrefSettingsBroadcast(
455 prefs::kArcLocationServiceEnabled,
456 "org.chromium.arc.intent_helper.SET_LOCATION_SERVICE_ENABLED");
457 }
458
459 void ArcSettingsServiceImpl::SendSettingsBroadcast(
460 const std::string& action,
461 const base::DictionaryValue& extras) const {
462 auto* instance = arc_bridge_service_->intent_helper()->GetInstanceForMethod(
463 "SendBroadcast", kMinVersionForSendBroadcast);
464 if (!instance)
465 return;
466 std::string extras_json;
467 bool write_success = base::JSONWriter::Write(extras, &extras_json);
468 DCHECK(write_success);
469
470 instance->SendBroadcast(action, "org.chromium.arc.intent_helper",
471 "org.chromium.arc.intent_helper.SettingsReceiver",
472 extras_json);
473 }
474
475 void ArcSettingsServiceImpl::DefaultNetworkChanged(
476 const chromeos::NetworkState* network) {
477 // kProxy pref and ONC policy have more priority than the default network
478 // update.
479 Profile* profile = ProfileManager::GetActiveUserProfile();
480 if (network && !IsPrefProxyConfigApplied() &&
481 !chromeos::onc::HasPolicyForNetwork(
482 profile->GetPrefs(), g_browser_process->local_state(), *network)) {
483 SyncProxySettings();
484 }
485 }
486
487 ArcSettingsService::ArcSettingsService(ArcBridgeService* bridge_service)
488 : ArcService(bridge_service) {
489 arc_bridge_service()->intent_helper()->AddObserver(this);
490 }
491
492 ArcSettingsService::~ArcSettingsService() {
493 arc_bridge_service()->intent_helper()->RemoveObserver(this);
494 }
495
496 void ArcSettingsService::OnInstanceReady() {
497 impl_.reset(new ArcSettingsServiceImpl(arc_bridge_service()));
498 }
499
500 void ArcSettingsService::OnInstanceClosed() {
501 impl_.reset();
502 }
503
504 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698