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