Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: chrome/browser/chromeos/settings/device_settings_service.h

Issue 10828032: Add DeviceSettingsService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase, move to chrome/browser/chromeos/settings. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_SERVICE_H_
7
8 #include <deque>
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/synchronization/lock.h"
18 #include "chrome/browser/policy/cloud_policy_validator.h"
19 #include "chromeos/dbus/session_manager_client.h"
20
21 namespace base {
22 template <typename T> struct DefaultLazyInstanceTraits;
23 }
24
25 namespace crypto {
26 class RSAPrivateKey;
27 }
28
29 namespace enterprise_management {
30 class ChromeDeviceSettingsProto;
31 class PolicyData;
32 }
33
34 namespace chromeos {
35
36 class OwnerKeyUtil;
37 class SessionManagerOperation;
38
39 // Keeps the public and private halves of the owner key. Both may be missing,
40 // but if the private key is present, the public half will be as well. This
41 // class is immutable and refcounted in order to allow safe access from any
42 // thread.
43 class OwnerKey : public base::RefCountedThreadSafe<OwnerKey> {
44 public:
45 OwnerKey(scoped_ptr<std::vector<uint8> > public_key,
46 scoped_ptr<crypto::RSAPrivateKey> private_key);
47
48 const std::vector<uint8>* public_key() {
49 return public_key_.get();
50 }
51 crypto::RSAPrivateKey* private_key() {
52 return private_key_.get();
53 }
54
55 private:
56 friend class base::RefCountedThreadSafe<OwnerKey>;
57 ~OwnerKey();
58
59 scoped_ptr<std::vector<uint8> > public_key_;
60 scoped_ptr<crypto::RSAPrivateKey> private_key_;
61
62 DISALLOW_COPY_AND_ASSIGN(OwnerKey);
63 };
64
65 // Deals with the low-level interface to Chromium OS device settings. Device
66 // settings are stored in a protobuf that's protected by a cryptographic
67 // signature generated by a key in the device owner's possession. Key and
68 // settings are brokered by the session_manager daemon.
69 //
70 // The purpose of DeviceSettingsService is to keep track of the current key and
71 // settings blob. For reading and writing device settings, use CrosSettings
72 // instead, which provides a high-level interface that allows for manipulation
73 // of individual settings.
74 //
75 // DeviceSettingsService generates notifications for key and policy update
76 // events so interested parties can reload state as appropriate.
77 class DeviceSettingsService : public SessionManagerClient::Observer {
78 public:
79 // Indicates ownership status of the device.
80 enum OwnershipStatus {
81 // Listed in upgrade order.
82 OWNERSHIP_UNKNOWN = 0,
83 OWNERSHIP_NONE,
84 OWNERSHIP_TAKEN
85 };
86
87 typedef base::Callback<void(OwnershipStatus, bool)> OwnershipStatusCallback;
88
89 // Status codes for Store().
90 enum Status {
91 STORE_SUCCESS,
92 STORE_KEY_UNAVAILABLE, // Owner key not yet configured.
93 STORE_POLICY_ERROR, // Failure constructing the settings blob.
94 STORE_OPERATION_FAILED, // IPC to session_manager daemon failed.
95 STORE_NO_POLICY, // No settings blob present.
96 STORE_INVALID_POLICY, // Invalid settings blob.
97 STORE_VALIDATION_ERROR, // Policy validation failure.
98 };
99
100 // Observer interface.
101 class Observer {
102 public:
103 virtual ~Observer();
104
105 // Indicates device ownership status changes.
106 virtual void OwnershipStatusChanged() = 0;
107
108 // Gets call after updates to the device settings.
109 virtual void DeviceSettingsUpdated() = 0;
110 };
111
112 // Creates a device settings service instance. This is meant for unit tests,
113 // production code uses the singleton returned by Get() below.
114 DeviceSettingsService();
115 ~DeviceSettingsService();
116
117 // Returns the singleton instance.
118 static DeviceSettingsService* Get();
119
120 // To be called on startup once threads are initialized and DBus is ready.
121 void Initialize(SessionManagerClient* session_manager_client,
122 scoped_refptr<OwnerKeyUtil> owner_key_util);
123
124 // Prevents the service from making further calls to session_manager_client
125 // and stops any pending operations.
126 void Shutdown();
127
128 // Returns the currently active device settings. Returns NULL if the device
129 // settings have not been retrieved from session_manager yet.
130 const enterprise_management::PolicyData* policy_data() {
131 return policy_data_.get();
132 }
133 const enterprise_management::ChromeDeviceSettingsProto*
134 device_settings() const {
135 return device_settings_.get();
136 }
137
138 // Returns the currently used owner key.
139 scoped_refptr<OwnerKey> GetOwnerKey();
140
141 // Returns the status generated by the last operation.
142 Status status() {
143 return store_status_;
144 }
145
146 // Triggers an attempt to pull the public half of the owner key from disk and
147 // load the device settings.
148 void Load();
149
150 // Signs |settings| with the private half of the owner key and sends the
151 // resulting policy blob to session manager for storage. The result of the
152 // operation is reported through |callback|. If successful, the updated device
153 // settings are present in policy_data() and device_settings() when the
154 // callback runs.
155 void SignAndStore(
156 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> settings,
157 const base::Closure& callback);
158
159 // Stores a policy blob to session_manager. The result of the operation is
160 // reported through |callback|. If successful, the updated device settings are
161 // present in policy_data() and device_settings() when the callback runs.
162 void Store(const std::string& policy_blob, const base::Closure& callback);
163
164 // This method can be run either on FILE or UI threads. If |blocking| flag
165 // is specified then it is guaranteed to return either OWNERSHIP_NONE or
166 // OWNERSHIP_TAKEN (and not OWNERSHIP_UNKNOWN), however in this case it may
167 // occasionally block doing I/O.
168 //
169 // TODO(mnissler, pastarmovj): Remove the blocking flag and only allow this on
170 // the UI thread.
Chris Masone 2012/08/03 16:28:25 File an issue describing what work this will entai
Mattias Nissler (ping if slow) 2012/08/06 21:28:14 I checked again, and there's only one dependency,
171 OwnershipStatus GetOwnershipStatus(bool blocking);
172
173 // Determines the ownership status and reports the result to |callback|. This
174 // is guaranteed to never return OWNERSHIP_UNKNOWN.
175 void GetOwnershipStatusAsync(const OwnershipStatusCallback& callback);
Chris Masone 2012/08/03 16:28:25 Async is guaranteed never to return OWNERSHIP_UNKN
Mattias Nissler (ping if slow) 2012/08/06 21:28:14 Why is that surprising? GetOwnershipStatusAsync()
176
177 // Checks whether we have the private owner key.
178 bool HasPrivateOwnerKey();
179
180 // Sets the identity of the user that's interacting with the service. This is
181 // relevant only for writing settings through SignAndStore().
182 void SetUsername(const std::string& username);
183 const std::string& GetUsername() const;
184
185 // Adds an observer.
186 void AddObserver(Observer* observer);
187 // Removes and observer.
Chris Masone 2012/08/03 16:28:25 s/and/an/
Mattias Nissler (ping if slow) 2012/08/06 21:28:14 Done.
188 void RemoveObserver(Observer* observer);
189
190 // SessionManagerClient::Observer:
191 virtual void OwnerKeySet(bool success) OVERRIDE;
192 virtual void PropertyChangeComplete(bool success) OVERRIDE;
193
194 private:
195 // Enqueues a new operation. Takes ownership of |operation| and starts it
196 // right away if there is no active operation currently.
197 void Enqueue(SessionManagerOperation* operation);
198
199 // Enqueues a load operation.
200 void EnqueueLoad(bool force_key_load);
201
202 // Makes sure there's a reload operation so changes to the settings (and key,
203 // in case force_key_load is set) are getting picked up.
204 void EnsureReload(bool force_key_load);
205
206 // Runs the next pending operation.
207 void StartNextOperation();
208
209 // Updates status, policy data and owner key from a finished operation.
210 // Starts the next pending operation if available.
211 void HandleCompletedOperation(const base::Closure& callback,
212 SessionManagerOperation* operation,
213 Status status);
214
215 // Sets ownership status. May be called on either thread.
216 void SetOwnershipStatus(OwnershipStatus new_status);
217
218 SessionManagerClient* session_manager_client_;
219 scoped_refptr<OwnerKeyUtil> owner_key_util_;
220
221 base::WeakPtrFactory<DeviceSettingsService> weak_factory_;
222
223 Status store_status_;
224
225 OwnershipStatus ownership_status_;
226 base::Lock ownership_status_lock_;
227 std::vector<OwnershipStatusCallback> pending_ownership_status_callbacks_;
228
229 std::string username_;
230 scoped_refptr<OwnerKey> owner_key_;
231
232 scoped_ptr<enterprise_management::PolicyData> policy_data_;
233 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> device_settings_;
234
235 // The queue of pending operations. The first operation on the queue is
236 // currently active; it gets removed and destroyed once it completes.
237 std::deque<SessionManagerOperation*> pending_operations_;
238
239 ObserverList<Observer, true> observers_;
240
241 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsService);
242 };
243
244 } // namespace chromeos
245
246 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698