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