| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <deque> | |
| 9 #include <string> | 8 #include <string> |
| 10 #include <utility> | 9 #include <utility> |
| 11 #include <vector> | 10 #include <vector> |
| 12 | 11 |
| 13 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 14 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
| 15 #include "base/gtest_prod_util.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "chrome/browser/chromeos/settings/cros_settings_provider.h" | 14 #include "chrome/browser/chromeos/settings/cros_settings_provider.h" |
| 18 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 15 #include "chrome/browser/chromeos/settings/ownership_service.h" |
| 19 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | 16 #include "chrome/browser/chromeos/settings/signed_settings_migration_helper.h" |
| 17 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 20 #include "chrome/browser/prefs/pref_value_map.h" | 18 #include "chrome/browser/prefs/pref_value_map.h" |
| 21 #include "chrome/browser/prefs/pref_value_map.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 22 | 20 |
| 23 namespace base { | 21 namespace base { |
| 24 class Value; | 22 class Value; |
| 25 } | 23 } |
| 26 | 24 |
| 27 namespace enterprise_management { | 25 namespace enterprise_management { |
| 28 class ChromeDeviceSettingsProto; | 26 class ChromeDeviceSettingsProto; |
| 29 } // namespace enterprise_management | 27 } // namespace enterprise_management |
| 30 | 28 |
| 31 namespace chromeos { | 29 namespace chromeos { |
| 32 | 30 |
| 33 // CrosSettingsProvider implementation that works with device settings. | 31 // CrosSettingsProvider implementation that works with SignedSettings. |
| 34 class DeviceSettingsProvider : public CrosSettingsProvider, | 32 class DeviceSettingsProvider : public CrosSettingsProvider, |
| 35 public DeviceSettingsService::Observer { | 33 public content::NotificationObserver { |
| 36 public: | 34 public: |
| 37 DeviceSettingsProvider(const NotifyObserversCallback& notify_cb, | 35 DeviceSettingsProvider(const NotifyObserversCallback& notify_cb, |
| 38 DeviceSettingsService* device_settings_service); | 36 SignedSettingsHelper* signed_settings_helper); |
| 39 virtual ~DeviceSettingsProvider(); | 37 virtual ~DeviceSettingsProvider(); |
| 40 | 38 |
| 41 // CrosSettingsProvider implementation. | 39 // CrosSettingsProvider implementation. |
| 42 virtual const base::Value* Get(const std::string& path) const OVERRIDE; | 40 virtual const base::Value* Get(const std::string& path) const OVERRIDE; |
| 43 virtual TrustedStatus PrepareTrustedValues( | 41 virtual TrustedStatus PrepareTrustedValues( |
| 44 const base::Closure& callback) OVERRIDE; | 42 const base::Closure& callback) OVERRIDE; |
| 45 virtual bool HandlesSetting(const std::string& path) const OVERRIDE; | 43 virtual bool HandlesSetting(const std::string& path) const OVERRIDE; |
| 44 virtual void Reload() OVERRIDE; |
| 46 | 45 |
| 47 private: | 46 private: |
| 48 // CrosSettingsProvider implementation: | 47 // CrosSettingsProvider implementation: |
| 49 virtual void DoSet(const std::string& path, | 48 virtual void DoSet(const std::string& path, |
| 50 const base::Value& value) OVERRIDE; | 49 const base::Value& value) OVERRIDE; |
| 51 | 50 |
| 52 // DeviceSettingsService::Observer implementation: | 51 // content::NotificationObserver implementation: |
| 53 virtual void OwnershipStatusChanged() OVERRIDE; | 52 virtual void Observe(int type, |
| 54 virtual void DeviceSettingsUpdated() OVERRIDE; | 53 const content::NotificationSource& source, |
| 54 const content::NotificationDetails& details) OVERRIDE; |
| 55 |
| 56 const enterprise_management::PolicyData policy() const; |
| 55 | 57 |
| 56 // Populates in-memory cache from the local_state cache that is used to store | 58 // Populates in-memory cache from the local_state cache that is used to store |
| 57 // device settings before the device is owned and to speed up policy | 59 // signed settings before the device is owned and to speed up policy |
| 58 // availability before the policy blob is fetched on boot. | 60 // availability before the policy blob is fetched on boot. |
| 59 void RetrieveCachedData(); | 61 void RetrieveCachedData(); |
| 60 | 62 |
| 61 // Stores a value from the |pending_changes_| queue in the device settings. | 63 // Stores a value from the |pending_changes_| queue in the signed settings. |
| 62 // If the device is not owned yet the data ends up only in the local_state | 64 // If the device is not owned yet the data ends up only in the local_state |
| 63 // cache and is serialized once ownership is acquired. | 65 // cache and is serialized once ownership is acquired. |
| 64 void SetInPolicy(); | 66 void SetInPolicy(); |
| 65 | 67 |
| 68 // Finalizes stores to the policy file if the cache is dirty. |
| 69 void FinishSetInPolicy( |
| 70 SignedSettings::ReturnCode code, |
| 71 const enterprise_management::PolicyFetchResponse& policy); |
| 72 |
| 66 // Decode the various groups of policies. | 73 // Decode the various groups of policies. |
| 67 void DecodeLoginPolicies( | 74 void DecodeLoginPolicies( |
| 68 const enterprise_management::ChromeDeviceSettingsProto& policy, | 75 const enterprise_management::ChromeDeviceSettingsProto& policy, |
| 69 PrefValueMap* new_values_cache) const; | 76 PrefValueMap* new_values_cache) const; |
| 70 void DecodeKioskPolicies( | 77 void DecodeKioskPolicies( |
| 71 const enterprise_management::ChromeDeviceSettingsProto& policy, | 78 const enterprise_management::ChromeDeviceSettingsProto& policy, |
| 72 PrefValueMap* new_values_cache) const; | 79 PrefValueMap* new_values_cache) const; |
| 73 void DecodeNetworkPolicies( | 80 void DecodeNetworkPolicies( |
| 74 const enterprise_management::ChromeDeviceSettingsProto& policy, | 81 const enterprise_management::ChromeDeviceSettingsProto& policy, |
| 75 PrefValueMap* new_values_cache) const; | 82 PrefValueMap* new_values_cache) const; |
| 76 void DecodeReportingPolicies( | 83 void DecodeReportingPolicies( |
| 77 const enterprise_management::ChromeDeviceSettingsProto& policy, | 84 const enterprise_management::ChromeDeviceSettingsProto& policy, |
| 78 PrefValueMap* new_values_cache) const; | 85 PrefValueMap* new_values_cache) const; |
| 79 void DecodeGenericPolicies( | 86 void DecodeGenericPolicies( |
| 80 const enterprise_management::ChromeDeviceSettingsProto& policy, | 87 const enterprise_management::ChromeDeviceSettingsProto& policy, |
| 81 PrefValueMap* new_values_cache) const; | 88 PrefValueMap* new_values_cache) const; |
| 82 | 89 |
| 83 // Parses the policy data and fills in |values_cache_|. | 90 // Parses the policy cache and fills the cache of base::Value objects. |
| 84 void UpdateValuesCache( | 91 void UpdateValuesCache(); |
| 85 const enterprise_management::PolicyData& policy_data, | |
| 86 const enterprise_management::ChromeDeviceSettingsProto& settings); | |
| 87 | 92 |
| 88 // Applies the metrics policy and if not set migrates the legacy file. | 93 // Applies the metrics policy and if not set migrates the legacy file. |
| 89 void ApplyMetricsSetting(bool use_file, bool new_value); | 94 void ApplyMetricsSetting(bool use_file, bool new_value) const; |
| 90 | 95 |
| 91 // Applies the data roaming policy. | 96 // Applies the data roaming policy. |
| 92 void ApplyRoamingSetting(bool new_value); | 97 void ApplyRoamingSetting(bool new_value) const; |
| 93 | 98 |
| 94 // Applies any changes of the policies that are not handled by the respective | 99 // Applies any changes of the policies that are not handled by the respective |
| 95 // subsystems. | 100 // subsystems. |
| 96 void ApplySideEffects( | 101 void ApplySideEffects() const; |
| 97 const enterprise_management::ChromeDeviceSettingsProto& settings); | |
| 98 | 102 |
| 99 // In case of missing policy blob we should verify if this is upgrade of | 103 // In case of missing policy blob we should verify if this is upgrade of |
| 100 // machine owned from pre version 12 OS and the user never touched the device | 104 // machine owned from pre version 12 OS and the user never touched the device |
| 101 // settings. In this case revert to defaults and let people in until the owner | 105 // settings. In this case revert to defaults and let people in until the owner |
| 102 // comes and changes that. | 106 // comes and changes that. |
| 103 bool MitigateMissingPolicy(); | 107 bool MitigateMissingPolicy(); |
| 104 | 108 |
| 109 // Called right before boolean property is changed. |
| 110 void OnBooleanPropertyChange(const std::string& path, bool new_value); |
| 111 |
| 105 // Checks if the current cache value can be trusted for being representative | 112 // Checks if the current cache value can be trusted for being representative |
| 106 // for the disk cache. | 113 // for the disk cache. |
| 107 TrustedStatus RequestTrustedEntity(); | 114 TrustedStatus RequestTrustedEntity(); |
| 108 | 115 |
| 109 // Invokes UpdateFromService() to synchronize with |device_settings_service_|, | 116 // Called right after signed value was checked. |
| 110 // then triggers the next store operation if applicable. | 117 void OnPropertyRetrieve(const std::string& path, |
| 111 void UpdateAndProceedStoring(); | 118 const base::Value* value, |
| 119 bool use_default_value); |
| 112 | 120 |
| 113 // Re-reads state from |device_settings_service_|, adjusts | 121 // Callback of StorePolicyOp for ordinary policy stores. |
| 114 // |trusted_status_| and calls UpdateValuesCache() if applicable. Returns true | 122 void OnStorePolicyCompleted(SignedSettings::ReturnCode code); |
| 115 // if new settings have been loaded. | |
| 116 bool UpdateFromService(); | |
| 117 | 123 |
| 118 // Sends |device_settings_| to |device_settings_service_| for signing and | 124 // Callback of RetrievePolicyOp for ordinary policy [re]loads. |
| 119 // storage in session_manager. | 125 void OnRetrievePolicyCompleted( |
| 120 void StoreDeviceSettings(); | 126 SignedSettings::ReturnCode code, |
| 127 const enterprise_management::PolicyFetchResponse& policy); |
| 121 | 128 |
| 122 // Checks the current ownership status to see whether the device owner is | 129 // These setters are for test use only. |
| 123 // logged in and writes the data accumulated in |migration_values_| to proper | 130 void set_ownership_status(OwnershipService::Status status) { |
| 124 // device settings. | 131 ownership_status_ = status; |
| 125 void AttemptMigration(); | 132 } |
| 133 void set_trusted_status(TrustedStatus status) { |
| 134 trusted_status_ = status; |
| 135 } |
| 136 void set_retries_left(int retries) { |
| 137 retries_left_ = retries; |
| 138 } |
| 126 | 139 |
| 127 // Pending callbacks that need to be invoked after settings verification. | 140 // Pending callbacks that need to be invoked after settings verification. |
| 128 std::vector<base::Closure> callbacks_; | 141 std::vector<base::Closure> callbacks_; |
| 129 | 142 |
| 130 DeviceSettingsService* device_settings_service_; | 143 SignedSettingsHelper* signed_settings_helper_; |
| 131 mutable PrefValueMap migration_values_; | 144 OwnershipService::Status ownership_status_; |
| 145 mutable scoped_ptr<SignedSettingsMigrationHelper> migration_helper_; |
| 132 | 146 |
| 147 content::NotificationRegistrar registrar_; |
| 148 |
| 149 // In order to guard against occasional failure to fetch a property |
| 150 // we allow for some number of retries. |
| 151 int retries_left_; |
| 152 |
| 153 enterprise_management::PolicyData policy_; |
| 133 TrustedStatus trusted_status_; | 154 TrustedStatus trusted_status_; |
| 134 DeviceSettingsService::OwnershipStatus ownership_status_; | |
| 135 | 155 |
| 136 // The device settings as currently reported through the CrosSettingsProvider | |
| 137 // interface. This may be different from the actual current device settings | |
| 138 // (which can be obtained from |device_settings_service_|) in case the device | |
| 139 // does not have an owner yet or there are pending changes that have not yet | |
| 140 // been written to session_manager. | |
| 141 enterprise_management::ChromeDeviceSettingsProto device_settings_; | |
| 142 | |
| 143 // A cache of values, indexed by the settings keys served through the | |
| 144 // CrosSettingsProvider interface. This is always kept in sync with the raw | |
| 145 // data found in |device_settings_|. | |
| 146 PrefValueMap values_cache_; | 156 PrefValueMap values_cache_; |
| 147 | 157 |
| 148 // This is a queue for set requests, because those need to be sequential. | 158 // This is a queue for set requests, because those need to be sequential. |
| 149 typedef std::pair<std::string, base::Value*> PendingQueueElement; | 159 typedef std::pair<std::string, base::Value*> PendingQueueElement; |
| 150 std::deque<PendingQueueElement> pending_changes_; | 160 std::vector<PendingQueueElement> pending_changes_; |
| 151 | |
| 152 // Weak pointer factory for creating store operation callbacks. | |
| 153 base::WeakPtrFactory<DeviceSettingsProvider> store_callback_factory_; | |
| 154 | 161 |
| 155 friend class DeviceSettingsProviderTest; | 162 friend class DeviceSettingsProviderTest; |
| 156 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, | 163 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, |
| 157 InitializationTestUnowned); | 164 InitializationTestUnowned); |
| 158 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, | 165 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, |
| 159 PolicyFailedPermanentlyNotification); | 166 PolicyFailedPermanentlyNotification); |
| 160 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, PolicyLoadNotification); | 167 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, PolicyLoadNotification); |
| 161 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProvider); | 168 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProvider); |
| 162 }; | 169 }; |
| 163 | 170 |
| 164 } // namespace chromeos | 171 } // namespace chromeos |
| 165 | 172 |
| 166 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_ | 173 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_ |
| OLD | NEW |