Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_LOGIN_DEVICE_SETTINGS_SERVICE_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_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 and reload state as appropriate. | |
|
pastarmovj
2012/07/30 12:19:19
Hmm something is missing in this sentence.
Mattias Nissler (ping if slow)
2012/07/31 13:02:15
Done.
| |
| 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(); | |
|
pastarmovj
2012/07/30 12:19:19
Isn't it better to create another static method li
Mattias Nissler (ping if slow)
2012/07/31 13:02:15
How would you call a constructor by coincidence? C
pastarmovj
2012/08/01 08:46:52
What I meant by "coincidence" is somebody doing th
| |
| 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 void Shutdown(); | |
|
pastarmovj
2012/07/30 12:19:19
Please document with one line. Now it feels more l
Mattias Nissler (ping if slow)
2012/07/31 13:02:15
Done.
| |
| 124 | |
| 125 // Returns the currently active device settings. Returns NULL if the device | |
| 126 // settings have not yet been retrieved from session_manager yet. | |
|
pastarmovj
2012/07/30 12:19:19
yet...yet. Please remove one of them.
Mattias Nissler (ping if slow)
2012/07/31 13:02:15
Done.
| |
| 127 const enterprise_management::PolicyData* policy_data() { | |
| 128 return policy_data_.get(); | |
| 129 } | |
| 130 const enterprise_management::ChromeDeviceSettingsProto* | |
| 131 device_settings() const { | |
| 132 return device_settings_.get(); | |
| 133 } | |
| 134 | |
| 135 // Returns the currently used owner key. | |
| 136 scoped_refptr<OwnerKey> GetOwnerKey(); | |
| 137 | |
| 138 // Returns the status generated by the last operation. | |
| 139 Status status() { | |
| 140 return store_status_; | |
| 141 } | |
| 142 | |
| 143 // Triggers an attempt to pull the public half of the owner key from disk and | |
| 144 // load the device settings. | |
| 145 void Load(); | |
| 146 | |
| 147 // Signs |settings| with the private half of the owner key and sends the | |
| 148 // resulting policy blob to session manager for storage. The result of the | |
| 149 // operation is reported through |callback|. If successful, the updated device | |
| 150 // settings are present in policy_data() and device_settings() when the | |
| 151 // callback runs. | |
| 152 void SignAndStore( | |
| 153 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> settings, | |
| 154 const base::Closure& callback); | |
| 155 | |
| 156 // Stores a policy blob to session_manager. The result of the operation is | |
| 157 // reported through |callback|. If successful, the updated device settings are | |
| 158 // present in policy_data() and device_settings() when the callback runs. | |
| 159 void Store(const std::string& policy_blob, const base::Closure& callback); | |
| 160 | |
| 161 // This method can be run either on FILE or UI threads. If |blocking| flag | |
| 162 // is specified then it is guaranteed to return either OWNERSHIP_NONE or | |
| 163 // OWNERSHIP_TAKEN (and not OWNERSHIP_UNKNOWN), however in this case it may | |
| 164 // occasionally block doing I/O. | |
| 165 // | |
| 166 // TODO(mnissler, pastarmovj): Remove the blocking flag and only allow this on | |
| 167 // the UI thread. | |
|
pastarmovj
2012/07/30 12:19:19
FYI: I have an idea how we can get rid of the call
Mattias Nissler (ping if slow)
2012/07/31 13:02:15
I've fixed DeviceSettingsProvider in the second pa
| |
| 168 OwnershipStatus GetOwnershipStatus(bool blocking); | |
| 169 | |
| 170 // Determines the ownership status and reports the result to |callback|. This | |
| 171 // is guaranteed to never return OWNERSHIP_UNKNOWN. | |
| 172 void GetOwnershipStatusAsync(const OwnershipStatusCallback& callback); | |
| 173 | |
| 174 // Checks whether we have the private owner key. | |
| 175 bool HasPrivateOwnerKey(); | |
| 176 | |
| 177 // Sets the identity of the user that's interacting with the service. This is | |
| 178 // relevant only for writing settings through SignAndStore(). | |
| 179 void SetUsername(const std::string& username); | |
| 180 const std::string& GetUsername() const; | |
| 181 | |
| 182 // Adds an observer. | |
| 183 void AddObserver(Observer* observer); | |
| 184 // Removes and observer. | |
| 185 void RemoveObserver(Observer* observer); | |
| 186 | |
| 187 // SessionManagerClient::Observer: | |
| 188 virtual void OwnerKeySet(bool success) OVERRIDE; | |
| 189 virtual void PropertyChangeComplete(bool success) OVERRIDE; | |
| 190 | |
| 191 private: | |
| 192 // Enqueues a new operation. Takes ownership of |operation| and starts it | |
| 193 // right away if there is no active operation currently. | |
| 194 void Enqueue(SessionManagerOperation* operation); | |
| 195 | |
| 196 // Enqueues a load operation. | |
| 197 void EnqueueLoad(bool force_key_load); | |
| 198 | |
| 199 // Makes sure there's a reload operation so changes to the settings (and key, | |
| 200 // in case force_key_load is set) are getting picked up. | |
| 201 void EnsureReload(bool force_key_load); | |
| 202 | |
| 203 // Runs the next pending operation. | |
| 204 void StartNextOperation(); | |
| 205 | |
| 206 // Updates status, policy data and owner key from a finished operation. | |
| 207 // Starts the next pending operation if available. | |
| 208 void HandleCompletedOperation(const base::Closure& callback, | |
| 209 SessionManagerOperation* operation, | |
| 210 Status status); | |
| 211 | |
| 212 // Sets ownership status. May be called on either thread. | |
| 213 void SetOwnershipStatus(OwnershipStatus new_status); | |
| 214 | |
| 215 SessionManagerClient* session_manager_client_; | |
| 216 scoped_refptr<OwnerKeyUtil> owner_key_util_; | |
| 217 | |
| 218 base::WeakPtrFactory<DeviceSettingsService> weak_factory_; | |
| 219 | |
| 220 Status store_status_; | |
| 221 | |
| 222 OwnershipStatus ownership_status_; | |
| 223 base::Lock ownership_status_lock_; | |
| 224 std::vector<OwnershipStatusCallback> pending_ownership_status_callbacks_; | |
| 225 | |
| 226 std::string username_; | |
| 227 scoped_refptr<OwnerKey> owner_key_; | |
| 228 | |
| 229 scoped_ptr<enterprise_management::PolicyData> policy_data_; | |
| 230 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> device_settings_; | |
| 231 | |
| 232 // The queue of pending operations. The first operation on the queue is | |
| 233 // currently active; it gets removed and destroyed once it completes. | |
| 234 std::deque<SessionManagerOperation*> pending_operations_; | |
| 235 | |
| 236 ObserverList<Observer, true> observers_; | |
| 237 | |
| 238 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsService); | |
| 239 }; | |
| 240 | |
| 241 } // namespace chromeos | |
| 242 | |
| 243 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_DEVICE_SETTINGS_SERVICE_H_ | |
| OLD | NEW |