device_settings_provider.h revision 868fa2fe829687343ffae624259930155e16dbd8
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/chromeos/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  // Returns true if |path| is handled by this provider.
41  static bool IsDeviceSetting(const std::string& name);
42
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;
48
49 private:
50  // CrosSettingsProvider implementation:
51  virtual void DoSet(const std::string& path,
52                     const base::Value& value) OVERRIDE;
53
54  // DeviceSettingsService::Observer implementation:
55  virtual void OwnershipStatusChanged() OVERRIDE;
56  virtual void DeviceSettingsUpdated() OVERRIDE;
57
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();
62
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();
67
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;
87
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);
93
94  // Applies the metrics policy and if not set migrates the legacy file.
95  void ApplyMetricsSetting(bool use_file, bool new_value);
96
97  // Applies the data roaming policy.
98  void ApplyRoamingSetting(bool new_value);
99
100  // Applies any changes of the policies that are not handled by the respective
101  // subsystems.
102  void ApplySideEffects(
103      const enterprise_management::ChromeDeviceSettingsProto& settings);
104
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();
110
111  // Checks if the current cache value can be trusted for being representative
112  // for the disk cache.
113  TrustedStatus RequestTrustedEntity();
114
115  // Invokes UpdateFromService() to synchronize with |device_settings_service_|,
116  // then triggers the next store operation if applicable.
117  void UpdateAndProceedStoring();
118
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();
123
124  // Sends |device_settings_| to |device_settings_service_| for signing and
125  // storage in session_manager.
126  void StoreDeviceSettings();
127
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
130  // device settings.
131  void AttemptMigration();
132
133  // Pending callbacks that need to be invoked after settings verification.
134  std::vector<base::Closure> callbacks_;
135
136  DeviceSettingsService* device_settings_service_;
137  mutable PrefValueMap migration_values_;
138
139  TrustedStatus trusted_status_;
140  DeviceSettingsService::OwnershipStatus ownership_status_;
141
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_;
148
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_;
153
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_;
157
158  // Weak pointer factory for creating store operation callbacks.
159  base::WeakPtrFactory<DeviceSettingsProvider> store_callback_factory_;
160
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);
168};
169
170}  // namespace chromeos
171
172#endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_
173