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