NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / device_settings_service.h
blob34c99b713a461fd20622cbfb66c19241eb1bb928
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_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_SERVICE_H_
8 #include <deque>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/linked_ptr.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/observer_list.h"
19 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
20 #include "chromeos/dbus/session_manager_client.h"
21 #include "components/ownership/owner_settings_service.h"
22 #include "components/policy/core/common/cloud/cloud_policy_validator.h"
23 #include "crypto/scoped_nss_types.h"
24 #include "policy/proto/device_management_backend.pb.h"
26 namespace crypto {
27 class RSAPrivateKey;
30 namespace ownership {
31 class OwnerKeyUtil;
32 class PublicKey;
35 namespace chromeos {
37 class SessionManagerOperation;
39 // Deals with the low-level interface to Chromium OS device settings. Device
40 // settings are stored in a protobuf that's protected by a cryptographic
41 // signature generated by a key in the device owner's possession. Key and
42 // settings are brokered by the session_manager daemon.
44 // The purpose of DeviceSettingsService is to keep track of the current key and
45 // settings blob. For reading and writing device settings, use CrosSettings
46 // instead, which provides a high-level interface that allows for manipulation
47 // of individual settings.
49 // DeviceSettingsService generates notifications for key and policy update
50 // events so interested parties can reload state as appropriate.
51 class DeviceSettingsService : public SessionManagerClient::Observer {
52 public:
53 // Indicates ownership status of the device.
54 enum OwnershipStatus {
55 // Listed in upgrade order.
56 OWNERSHIP_UNKNOWN = 0,
57 OWNERSHIP_NONE,
58 OWNERSHIP_TAKEN
61 typedef base::Callback<void(OwnershipStatus)> OwnershipStatusCallback;
63 // Status codes for Store().
64 enum Status {
65 STORE_SUCCESS,
66 STORE_KEY_UNAVAILABLE, // Owner key not yet configured.
67 STORE_POLICY_ERROR, // Failure constructing the settings blob.
68 STORE_OPERATION_FAILED, // IPC to session_manager daemon failed.
69 STORE_NO_POLICY, // No settings blob present.
70 STORE_INVALID_POLICY, // Invalid settings blob.
71 STORE_VALIDATION_ERROR, // Unrecoverable policy validation failure.
72 STORE_TEMP_VALIDATION_ERROR, // Temporary policy validation failure.
75 // Observer interface.
76 class Observer {
77 public:
78 virtual ~Observer();
80 // Indicates device ownership status changes.
81 virtual void OwnershipStatusChanged() = 0;
83 // Gets call after updates to the device settings.
84 virtual void DeviceSettingsUpdated() = 0;
86 virtual void OnDeviceSettingsServiceShutdown() = 0;
89 // Manage singleton instance.
90 static void Initialize();
91 static bool IsInitialized();
92 static void Shutdown();
93 static DeviceSettingsService* Get();
95 // Creates a device settings service instance. This is meant for unit tests,
96 // production code uses the singleton returned by Get() above.
97 DeviceSettingsService();
98 ~DeviceSettingsService() override;
100 // To be called on startup once threads are initialized and DBus is ready.
101 void SetSessionManager(SessionManagerClient* session_manager_client,
102 scoped_refptr<ownership::OwnerKeyUtil> owner_key_util);
104 // Prevents the service from making further calls to session_manager_client
105 // and stops any pending operations.
106 void UnsetSessionManager();
108 SessionManagerClient* session_manager_client() const {
109 return session_manager_client_;
112 // Returns the currently active device settings. Returns NULL if the device
113 // settings have not been retrieved from session_manager yet.
114 const enterprise_management::PolicyData* policy_data() {
115 return policy_data_.get();
117 const enterprise_management::ChromeDeviceSettingsProto*
118 device_settings() const {
119 return device_settings_.get();
122 // Returns the currently used owner key.
123 scoped_refptr<ownership::PublicKey> GetPublicKey();
125 // Returns the status generated by the last operation.
126 Status status() {
127 return store_status_;
130 // Triggers an attempt to pull the public half of the owner key from disk and
131 // load the device settings.
132 void Load();
134 // Stores a policy blob to session_manager. The result of the operation is
135 // reported through |callback|. If successful, the updated device settings are
136 // present in policy_data() and device_settings() when the callback runs.
137 void Store(scoped_ptr<enterprise_management::PolicyFetchResponse> policy,
138 const base::Closure& callback);
140 // Returns the ownership status. May return OWNERSHIP_UNKNOWN if the disk
141 // hasn't been checked yet.
142 OwnershipStatus GetOwnershipStatus();
144 // Determines the ownership status and reports the result to |callback|. This
145 // is guaranteed to never return OWNERSHIP_UNKNOWN.
146 void GetOwnershipStatusAsync(const OwnershipStatusCallback& callback);
148 // Checks whether we have the private owner key.
150 // DEPRECATED (ygorshenin@, crbug.com/433840): this method should
151 // not be used since private key is a profile-specific resource and
152 // should be checked and used in a profile-aware manner, through
153 // OwnerSettingsService.
154 bool HasPrivateOwnerKey();
156 // Sets the identity of the user that's interacting with the service. This is
157 // relevant only for writing settings through SignAndStore().
159 // TODO (ygorshenin@, crbug.com/433840): get rid of the method when
160 // write path for device settings will be removed from
161 // DeviceSettingsProvider and all existing clients will be switched
162 // to OwnerSettingsServiceChromeOS.
163 void InitOwner(const std::string& username,
164 const base::WeakPtr<ownership::OwnerSettingsService>&
165 owner_settings_service);
167 const std::string& GetUsername() const;
169 ownership::OwnerSettingsService* GetOwnerSettingsService() const;
171 // Adds an observer.
172 void AddObserver(Observer* observer);
173 // Removes an observer.
174 void RemoveObserver(Observer* observer);
176 // SessionManagerClient::Observer:
177 void OwnerKeySet(bool success) override;
178 void PropertyChangeComplete(bool success) override;
180 private:
181 friend class OwnerSettingsServiceChromeOS;
183 // Enqueues a new operation. Takes ownership of |operation| and starts it
184 // right away if there is no active operation currently.
185 void Enqueue(const linked_ptr<SessionManagerOperation>& operation);
187 // Enqueues a load operation.
188 void EnqueueLoad(bool force_key_load);
190 // Makes sure there's a reload operation so changes to the settings (and key,
191 // in case force_key_load is set) are getting picked up.
192 void EnsureReload(bool force_key_load);
194 // Runs the next pending operation.
195 void StartNextOperation();
197 // Updates status, policy data and owner key from a finished operation.
198 // Starts the next pending operation if available.
199 void HandleCompletedOperation(const base::Closure& callback,
200 SessionManagerOperation* operation,
201 Status status);
203 // Updates status and invokes the callback immediately.
204 void HandleError(Status status, const base::Closure& callback);
206 SessionManagerClient* session_manager_client_;
207 scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
209 Status store_status_;
211 std::vector<OwnershipStatusCallback> pending_ownership_status_callbacks_;
213 std::string username_;
214 scoped_refptr<ownership::PublicKey> public_key_;
215 base::WeakPtr<ownership::OwnerSettingsService> owner_settings_service_;
217 scoped_ptr<enterprise_management::PolicyData> policy_data_;
218 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> device_settings_;
220 // The queue of pending operations. The first operation on the queue is
221 // currently active; it gets removed and destroyed once it completes.
222 std::deque<linked_ptr<SessionManagerOperation>> pending_operations_;
224 ObserverList<Observer> observers_;
226 // For recoverable load errors how many retries are left before we give up.
227 int load_retries_left_;
229 base::WeakPtrFactory<DeviceSettingsService> weak_factory_;
231 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsService);
234 // Helper class for tests. Initializes the DeviceSettingsService singleton on
235 // construction and tears it down again on destruction.
236 class ScopedTestDeviceSettingsService {
237 public:
238 ScopedTestDeviceSettingsService();
239 ~ScopedTestDeviceSettingsService();
241 private:
242 DISALLOW_COPY_AND_ASSIGN(ScopedTestDeviceSettingsService);
245 } // namespace chromeos
247 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_SERVICE_H_