device_settings_provider.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/policy/proto/chrome_device_policy.pb.h"
19#include "chrome/browser/chromeos/settings/cros_settings_provider.h"
20#include "chrome/browser/chromeos/settings/device_settings_service.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 DecodeReportingPolicies(
79      const enterprise_management::ChromeDeviceSettingsProto& policy,
80      PrefValueMap* new_values_cache) const;
81  void DecodeGenericPolicies(
82      const enterprise_management::ChromeDeviceSettingsProto& policy,
83      PrefValueMap* new_values_cache) const;
84
85  // Parses the policy data and fills in |values_cache_|.
86  void UpdateValuesCache(
87      const enterprise_management::PolicyData& policy_data,
88      const enterprise_management::ChromeDeviceSettingsProto& settings,
89      TrustedStatus trusted_status);
90
91  // Applies the metrics policy and if not set migrates the legacy file.
92  void ApplyMetricsSetting(bool use_file, bool new_value);
93
94  // Applies the data roaming policy.
95  void ApplyRoamingSetting(bool new_value);
96
97  // Applies any changes of the policies that are not handled by the respective
98  // subsystems.
99  void ApplySideEffects(
100      const enterprise_management::ChromeDeviceSettingsProto& settings);
101
102  // In case of missing policy blob we should verify if this is upgrade of
103  // machine owned from pre version 12 OS and the user never touched the device
104  // settings. In this case revert to defaults and let people in until the owner
105  // comes and changes that.
106  bool MitigateMissingPolicy();
107
108  // Checks if the current cache value can be trusted for being representative
109  // for the disk cache.
110  TrustedStatus RequestTrustedEntity();
111
112  // Invokes UpdateFromService() to synchronize with |device_settings_service_|,
113  // then triggers the next store operation if applicable.
114  void UpdateAndProceedStoring();
115
116  // Re-reads state from |device_settings_service_|, adjusts
117  // |trusted_status_| and calls UpdateValuesCache() if applicable. Returns true
118  // if new settings have been loaded.
119  bool UpdateFromService();
120
121  // Sends |device_settings_| to |device_settings_service_| for signing and
122  // storage in session_manager.
123  void StoreDeviceSettings();
124
125  // Checks the current ownership status to see whether the device owner is
126  // logged in and writes the data accumulated in |migration_values_| to proper
127  // device settings.
128  void AttemptMigration();
129
130  // Pending callbacks that need to be invoked after settings verification.
131  std::vector<base::Closure> callbacks_;
132
133  DeviceSettingsService* device_settings_service_;
134  mutable PrefValueMap migration_values_;
135
136  TrustedStatus trusted_status_;
137  DeviceSettingsService::OwnershipStatus ownership_status_;
138
139  // The device settings as currently reported through the CrosSettingsProvider
140  // interface. This may be different from the actual current device settings
141  // (which can be obtained from |device_settings_service_|) in case the device
142  // does not have an owner yet or there are pending changes that have not yet
143  // been written to session_manager.
144  enterprise_management::ChromeDeviceSettingsProto device_settings_;
145
146  // A cache of values, indexed by the settings keys served through the
147  // CrosSettingsProvider interface. This is always kept in sync with the raw
148  // data found in |device_settings_|.
149  PrefValueMap values_cache_;
150
151  // This is a queue for set requests, because those need to be sequential.
152  typedef std::pair<std::string, base::Value*> PendingQueueElement;
153  std::deque<PendingQueueElement> pending_changes_;
154
155  // Weak pointer factory for creating store operation callbacks.
156  base::WeakPtrFactory<DeviceSettingsProvider> store_callback_factory_;
157
158  friend class DeviceSettingsProviderTest;
159  FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest,
160                           InitializationTestUnowned);
161  FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest,
162                           PolicyFailedPermanentlyNotification);
163  FRIEND_TEST_ALL_PREFIXES(DeviceSettingsProviderTest, PolicyLoadNotification);
164  DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProvider);
165};
166
167}  // namespace chromeos
168
169#endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_PROVIDER_H_
170