Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / device_settings_provider.h
blobff468842b85cb79cc34ed8b6cbd4b3d8b0900931
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_
8 #include <deque>
9 #include <string>
10 #include <utility>
11 #include <vector>
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/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"
22 namespace base {
23 class Value;
26 namespace enterprise_management {
27 class ChromeDeviceSettingsProto;
28 } // namespace enterprise_management
30 namespace chromeos {
32 // CrosSettingsProvider implementation that works with device settings.
33 class DeviceSettingsProvider : public CrosSettingsProvider,
34 public DeviceSettingsService::Observer {
35 public:
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;
49 private:
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.
66 void SetInPolicy();
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);
99 void ApplyRoamingSettingFromProto(
100 const enterprise_management::ChromeDeviceSettingsProto& settings);
102 // Applies any changes of the policies that are not handled by the respective
103 // subsystems.
104 void ApplySideEffects(
105 const enterprise_management::ChromeDeviceSettingsProto& settings);
107 // In case of missing policy blob we should verify if this is upgrade of
108 // machine owned from pre version 12 OS and the user never touched the device
109 // settings. In this case revert to defaults and let people in until the owner
110 // comes and changes that.
111 bool MitigateMissingPolicy();
113 // Checks if the current cache value can be trusted for being representative
114 // for the disk cache.
115 TrustedStatus RequestTrustedEntity();
117 // Invokes UpdateFromService() to synchronize with |device_settings_service_|,
118 // then triggers the next store operation if applicable.
119 void UpdateAndProceedStoring();
121 // Re-reads state from |device_settings_service_|, adjusts
122 // |trusted_status_| and calls UpdateValuesCache() if applicable. Returns true
123 // if new settings have been loaded.
124 bool UpdateFromService();
126 // Sends |device_settings_| to |device_settings_service_| for signing and
127 // storage in session_manager.
128 void StoreDeviceSettings();
130 // Checks the current ownership status to see whether the device owner is
131 // logged in and writes the data accumulated in |migration_values_| to proper
132 // device settings.
133 void AttemptMigration();
135 // Pending callbacks that need to be invoked after settings verification.
136 std::vector<base::Closure> callbacks_;
138 DeviceSettingsService* device_settings_service_;
139 mutable PrefValueMap migration_values_;
141 TrustedStatus trusted_status_;
142 DeviceSettingsService::OwnershipStatus ownership_status_;
144 // The device settings as currently reported through the CrosSettingsProvider
145 // interface. This may be different from the actual current device settings
146 // (which can be obtained from |device_settings_service_|) in case the device
147 // does not have an owner yet or there are pending changes that have not yet
148 // been written to session_manager.
149 enterprise_management::ChromeDeviceSettingsProto device_settings_;
151 // A cache of values, indexed by the settings keys served through the
152 // CrosSettingsProvider interface. This is always kept in sync with the raw
153 // data found in |device_settings_|.
154 PrefValueMap values_cache_;
156 // This is a queue for set requests, because those need to be sequential.
157 typedef std::pair<std::string, base::Value*> PendingQueueElement;
158 std::deque<PendingQueueElement> pending_changes_;
160 // Weak pointer factory for creating store operation callbacks.
161 base::WeakPtrFactory<DeviceSettingsProvider> store_callback_factory_;
163 friend class DeviceSettingsProviderTest;
164 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest,
165 InitializationTestUnowned);
166 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest,
167 PolicyFailedPermanentlyNotification);
168 FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, PolicyLoadNotification);
169 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProvider);
172 } // namespace chromeos
174 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_