| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/user_cros_settings_provider.h" | 5 #include "chrome/browser/chromeos/user_cros_settings_provider.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 10 #include "base/hash_tables.h" | 12 #include "base/hash_tables.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 13 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 14 #include "base/task.h" | 16 #include "base/task.h" |
| 15 #include "base/values.h" | 17 #include "base/values.h" |
| 16 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/chromeos/cros/cros_library.h" | 19 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 18 #include "chrome/browser/chromeos/cros/login_library.h" | 20 #include "chrome/browser/chromeos/cros/login_library.h" |
| 19 #include "chrome/browser/chromeos/cros/network_library.h" | 21 #include "chrome/browser/chromeos/cros/network_library.h" |
| 20 #include "chrome/browser/chromeos/cros_settings.h" | 22 #include "chrome/browser/chromeos/cros_settings.h" |
| 21 #include "chrome/browser/chromeos/cros_settings_names.h" | 23 #include "chrome/browser/chromeos/cros_settings_names.h" |
| 22 #include "chrome/browser/chromeos/login/ownership_service.h" | 24 #include "chrome/browser/chromeos/login/ownership_service.h" |
| 23 #include "chrome/browser/chromeos/login/ownership_status_checker.h" | |
| 24 #include "chrome/browser/chromeos/login/user_manager.h" | 25 #include "chrome/browser/chromeos/login/user_manager.h" |
| 25 #include "chrome/browser/policy/browser_policy_connector.h" | 26 #include "chrome/browser/policy/browser_policy_connector.h" |
| 26 #include "chrome/browser/prefs/pref_service.h" | 27 #include "chrome/browser/prefs/pref_service.h" |
| 27 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 28 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 28 #include "chrome/browser/ui/options/options_util.h" | 29 #include "chrome/browser/ui/options/options_util.h" |
| 29 #include "chrome/common/chrome_notification_types.h" | |
| 30 #include "chrome/installer/util/google_update_settings.h" | 30 #include "chrome/installer/util/google_update_settings.h" |
| 31 #include "content/browser/browser_thread.h" | 31 #include "content/browser/browser_thread.h" |
| 32 | 32 |
| 33 namespace chromeos { | 33 namespace chromeos { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 const char kTrueIncantation[] = "true"; | 37 const char kTrueIncantation[] = "true"; |
| 38 const char kFalseIncantation[] = "false"; | 38 const char kFalseIncantation[] = "false"; |
| 39 const char kTrustedSuffix[] = "/trusted"; | 39 const char kTrustedSuffix[] = "/trusted"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 51 | 51 |
| 52 const char* kStringSettings[] = { | 52 const char* kStringSettings[] = { |
| 53 kDeviceOwner, | 53 kDeviceOwner, |
| 54 kReleaseChannel | 54 kReleaseChannel |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 const char* kListSettings[] = { | 57 const char* kListSettings[] = { |
| 58 kAccountsPrefUsers | 58 kAccountsPrefUsers |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 // This class provides the means to migrate settings to the signed settings | 61 // Only write the property if the owner is the current logged on user. |
| 62 // store. It does one of three things - store the settings in the policy blob | 62 void StartStorePropertyOpIfOwner(const std::string& name, |
| 63 // immediately if the current user is the owner. Uses the | 63 const std::string& value, |
| 64 // SignedSettingsTempStorage if there is no owner yet, or waits for an | 64 SignedSettingsHelper::Callback* callback) { |
| 65 // OWNERSHIP_CHECKED notification to delay the storing until the owner has | 65 if (OwnershipService::GetSharedInstance()->CurrentUserIsOwner()) { |
| 66 // logged in. | 66 BrowserThread::PostTask(BrowserThread::UI, |
| 67 class MigrationHelper : public NotificationObserver { | 67 FROM_HERE, |
| 68 public: | 68 base::Bind( |
| 69 explicit MigrationHelper() : callback_(NULL) { | 69 &SignedSettingsHelper::StartStorePropertyOp, |
| 70 registrar_.Add(this, chrome::NOTIFICATION_OWNERSHIP_CHECKED, | 70 base::Unretained(SignedSettingsHelper::Get()), |
| 71 NotificationService::AllSources()); | 71 name, |
| 72 value, |
| 73 callback)); |
| 72 } | 74 } |
| 73 | 75 } |
| 74 void set_callback(SignedSettingsHelper::Callback* callback) { | |
| 75 callback_ = callback; | |
| 76 } | |
| 77 | |
| 78 void AddMigrationValue(const std::string& path, const std::string& value) { | |
| 79 migration_values_[path] = value; | |
| 80 } | |
| 81 | |
| 82 void MigrateValues(void) { | |
| 83 ownership_checker_.reset(new OwnershipStatusChecker(NewCallback( | |
| 84 this, &MigrationHelper::DoMigrateValues))); | |
| 85 } | |
| 86 | |
| 87 // NotificationObserver overrides: | |
| 88 virtual void Observe(int type, | |
| 89 const NotificationSource& source, | |
| 90 const NotificationDetails& details) OVERRIDE { | |
| 91 if (type == chrome::NOTIFICATION_OWNERSHIP_CHECKED) | |
| 92 MigrateValues(); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 void DoMigrateValues(OwnershipService::Status status, | |
| 97 bool current_user_is_owner) { | |
| 98 ownership_checker_.reset(NULL); | |
| 99 | |
| 100 // We can call StartStorePropertyOp in two cases - either if the owner is | |
| 101 // currently logged in and the policy can be updated immediately or if there | |
| 102 // is no owner yet in which case the value will be temporarily stored in the | |
| 103 // SignedSettingsTempStorage until the device is owned. If none of these | |
| 104 // cases is met then we will wait for user change notification and retry. | |
| 105 if (current_user_is_owner || status != OwnershipService::OWNERSHIP_TAKEN) { | |
| 106 std::map<std::string, std::string>::const_iterator i; | |
| 107 for (i = migration_values_.begin(); i != migration_values_.end(); ++i) { | |
| 108 // Queue all values for storing. | |
| 109 SignedSettingsHelper::Get()->StartStorePropertyOp(i->first, i->second, | |
| 110 callback_); | |
| 111 } | |
| 112 migration_values_.clear(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 NotificationRegistrar registrar_; | |
| 117 scoped_ptr<OwnershipStatusChecker> ownership_checker_; | |
| 118 SignedSettingsHelper::Callback* callback_; | |
| 119 | |
| 120 std::map<std::string, std::string> migration_values_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(MigrationHelper); | |
| 123 }; | |
| 124 | 76 |
| 125 bool IsControlledBooleanSetting(const std::string& pref_path) { | 77 bool IsControlledBooleanSetting(const std::string& pref_path) { |
| 126 // TODO(nkostylev): Using std::find for 4 value array generates this warning | 78 // TODO(nkostylev): Using std::find for 4 value array generates this warning |
| 127 // in chroot stl_algo.h:231: error: array subscript is above array bounds. | 79 // in chroot stl_algo.h:231: error: array subscript is above array bounds. |
| 128 // GCC 4.4.3 | 80 // GCC 4.4.3 |
| 129 return (pref_path == kAccountsPrefAllowNewUser) || | 81 return (pref_path == kAccountsPrefAllowNewUser) || |
| 130 (pref_path == kAccountsPrefAllowGuest) || | 82 (pref_path == kAccountsPrefAllowGuest) || |
| 131 (pref_path == kAccountsPrefShowUserNamesOnSignIn) || | 83 (pref_path == kAccountsPrefShowUserNamesOnSignIn) || |
| 132 (pref_path == kSignedDataRoamingEnabled) || | 84 (pref_path == kSignedDataRoamingEnabled) || |
| 133 (pref_path == kStatsReportingPref); | 85 (pref_path == kStatsReportingPref); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 } | 282 } |
| 331 } | 283 } |
| 332 | 284 |
| 333 private: | 285 private: |
| 334 // upper bound for number of retries to fetch a signed setting. | 286 // upper bound for number of retries to fetch a signed setting. |
| 335 static const int kNumRetriesLimit = 9; | 287 static const int kNumRetriesLimit = 9; |
| 336 | 288 |
| 337 UserCrosSettingsTrust() | 289 UserCrosSettingsTrust() |
| 338 : ownership_service_(OwnershipService::GetSharedInstance()), | 290 : ownership_service_(OwnershipService::GetSharedInstance()), |
| 339 retries_left_(kNumRetriesLimit) { | 291 retries_left_(kNumRetriesLimit) { |
| 340 migration_helper_.set_callback(this); | |
| 341 // Start prefetching Boolean and String preferences. | 292 // Start prefetching Boolean and String preferences. |
| 342 Reload(); | 293 Reload(); |
| 343 } | 294 } |
| 344 | 295 |
| 345 virtual ~UserCrosSettingsTrust() { | 296 virtual ~UserCrosSettingsTrust() { |
| 346 if (BrowserThread::CurrentlyOn(BrowserThread::UI) && | 297 if (BrowserThread::CurrentlyOn(BrowserThread::UI) && |
| 347 CrosLibrary::Get()->EnsureLoaded()) { | 298 CrosLibrary::Get()->EnsureLoaded()) { |
| 348 // Cancels all pending callbacks from us. | 299 // Cancels all pending callbacks from us. |
| 349 SignedSettingsHelper::Get()->CancelCallback(this); | 300 SignedSettingsHelper::Get()->CancelCallback(this); |
| 350 } | 301 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 } | 345 } |
| 395 } else if (path == kStatsReportingPref) { | 346 } else if (path == kStatsReportingPref) { |
| 396 bool stats_consent = (use_value == USE_VALUE_SUPPLIED) ? value : false; | 347 bool stats_consent = (use_value == USE_VALUE_SUPPLIED) ? value : false; |
| 397 // TODO(pastarmovj): Remove this once migration is not needed anymore. | 348 // TODO(pastarmovj): Remove this once migration is not needed anymore. |
| 398 // If the value is not set we should try to migrate legacy consent file. | 349 // If the value is not set we should try to migrate legacy consent file. |
| 399 if (use_value == USE_VALUE_DEFAULT) { | 350 if (use_value == USE_VALUE_DEFAULT) { |
| 400 // Loading consent file state causes us to do blocking IO on UI thread. | 351 // Loading consent file state causes us to do blocking IO on UI thread. |
| 401 // Temporarily allow it until we fix http://crbug.com/62626 | 352 // Temporarily allow it until we fix http://crbug.com/62626 |
| 402 base::ThreadRestrictions::ScopedAllowIO allow_io; | 353 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 403 stats_consent = GoogleUpdateSettings::GetCollectStatsConsent(); | 354 stats_consent = GoogleUpdateSettings::GetCollectStatsConsent(); |
| 404 // Make sure the values will get eventually written to the policy file. | 355 // Only store settings if the owner is logged on, otherwise the write |
| 405 migration_helper_.AddMigrationValue( | 356 // will fail, triggering another read and we'll end up in an infinite |
| 406 path, stats_consent ? "true" : "false"); | 357 // loop. Owner check needs to be done on the FILE thread. |
| 407 migration_helper_.MigrateValues(); | 358 BrowserThread::PostTask(BrowserThread::FILE, |
| 359 FROM_HERE, |
| 360 base::Bind(&StartStorePropertyOpIfOwner, path, |
| 361 stats_consent ? "true" : "false", |
| 362 this)); |
| 408 UpdateCacheBool(path, stats_consent, USE_VALUE_SUPPLIED); | 363 UpdateCacheBool(path, stats_consent, USE_VALUE_SUPPLIED); |
| 409 LOG(WARNING) << "No metrics policy set will revert to checking " | 364 LOG(WARNING) << "No metrics policy set will revert to checking " |
| 410 << "consent file which is " | 365 << "consent file which is " |
| 411 << (stats_consent ? "on." : "off."); | 366 << (stats_consent ? "on." : "off."); |
| 412 } | 367 } |
| 413 // TODO(pastarmovj): Remove this once we don't need to regenerate the | 368 // TODO(pastarmovj): Remove this once we don't need to regenerate the |
| 414 // consent file for the GUID anymore. | 369 // consent file for the GUID anymore. |
| 415 VLOG(1) << "Metrics policy is being set to : " << stats_consent | 370 VLOG(1) << "Metrics policy is being set to : " << stats_consent |
| 416 << "(reason : " << use_value << ")"; | 371 << "(reason : " << use_value << ")"; |
| 417 OptionsUtil::ResolveMetricsReportingEnabled(stats_consent); | 372 OptionsUtil::ResolveMetricsReportingEnabled(stats_consent); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 | 484 |
| 530 // Reload the whitelist on settings op failure. | 485 // Reload the whitelist on settings op failure. |
| 531 if (code != SignedSettings::SUCCESS) | 486 if (code != SignedSettings::SUCCESS) |
| 532 CrosSettings::Get()->FireObservers(kAccountsPrefUsers); | 487 CrosSettings::Get()->FireObservers(kAccountsPrefUsers); |
| 533 } | 488 } |
| 534 | 489 |
| 535 // Pending callbacks that need to be invoked after settings verification. | 490 // Pending callbacks that need to be invoked after settings verification. |
| 536 base::hash_map< std::string, std::vector< Task* > > callbacks_; | 491 base::hash_map< std::string, std::vector< Task* > > callbacks_; |
| 537 | 492 |
| 538 OwnershipService* ownership_service_; | 493 OwnershipService* ownership_service_; |
| 539 MigrationHelper migration_helper_; | |
| 540 | 494 |
| 541 // In order to guard against occasional failure to fetch a property | 495 // In order to guard against occasional failure to fetch a property |
| 542 // we allow for some number of retries. | 496 // we allow for some number of retries. |
| 543 int retries_left_; | 497 int retries_left_; |
| 544 | 498 |
| 545 friend class SignedSettingsHelper; | 499 friend class SignedSettingsHelper; |
| 546 friend struct DefaultSingletonTraits<UserCrosSettingsTrust>; | 500 friend struct DefaultSingletonTraits<UserCrosSettingsTrust>; |
| 547 | 501 |
| 548 DISALLOW_COPY_AND_ASSIGN(UserCrosSettingsTrust); | 502 DISALLOW_COPY_AND_ASSIGN(UserCrosSettingsTrust); |
| 549 }; | 503 }; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 if (cached_whitelist_update->Remove(email_value, NULL)) | 698 if (cached_whitelist_update->Remove(email_value, NULL)) |
| 745 prefs->ScheduleSavePersistentPrefs(); | 699 prefs->ScheduleSavePersistentPrefs(); |
| 746 } | 700 } |
| 747 | 701 |
| 748 // static | 702 // static |
| 749 void UserCrosSettingsProvider::UpdateCachedOwner(const std::string& email) { | 703 void UserCrosSettingsProvider::UpdateCachedOwner(const std::string& email) { |
| 750 UpdateCacheString(kDeviceOwner, email, USE_VALUE_SUPPLIED); | 704 UpdateCacheString(kDeviceOwner, email, USE_VALUE_SUPPLIED); |
| 751 } | 705 } |
| 752 | 706 |
| 753 } // namespace chromeos | 707 } // namespace chromeos |
| OLD | NEW |