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