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_
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"
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
{
53 // Indicates ownership status of the device.
54 enum OwnershipStatus
{
55 // Listed in upgrade order.
56 OWNERSHIP_UNKNOWN
= 0,
61 typedef base::Callback
<void(OwnershipStatus
)> OwnershipStatusCallback
;
63 // Status codes for Store().
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.
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 virtual ~DeviceSettingsService();
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.
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.
134 // Sets the management related settings in PolicyData.
136 // TODO (ygorshenin@, crbug.com/230018): move this to the
137 // OwnerSettingsService.
138 void SetManagementSettings(
139 enterprise_management::PolicyData::ManagementMode management_mode
,
140 const std::string
& request_token
,
141 const std::string
& device_id
,
142 const base::Closure
& callback
);
144 // Stores a policy blob to session_manager. The result of the operation is
145 // reported through |callback|. If successful, the updated device settings are
146 // present in policy_data() and device_settings() when the callback runs.
147 void Store(scoped_ptr
<enterprise_management::PolicyFetchResponse
> policy
,
148 const base::Closure
& callback
);
150 // Returns the ownership status. May return OWNERSHIP_UNKNOWN if the disk
151 // hasn't been checked yet.
152 OwnershipStatus
GetOwnershipStatus();
154 // Determines the ownership status and reports the result to |callback|. This
155 // is guaranteed to never return OWNERSHIP_UNKNOWN.
156 void GetOwnershipStatusAsync(const OwnershipStatusCallback
& callback
);
158 // Checks whether we have the private owner key.
159 bool HasPrivateOwnerKey();
161 // Sets the identity of the user that's interacting with the service. This is
162 // relevant only for writing settings through SignAndStore().
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;
172 void AddObserver(Observer
* observer
);
173 // Removes an observer.
174 void RemoveObserver(Observer
* observer
);
176 // SessionManagerClient::Observer:
177 virtual void OwnerKeySet(bool success
) override
;
178 virtual void PropertyChangeComplete(bool success
) override
;
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 // Enqueues a sign and store operation.
192 // TODO (ygorshenin@, crbug.com/433840): extract SetManagementSettings() out
193 // of DeviceSettingsService and get rid of the method.
194 void EnqueueSignAndStore(scoped_ptr
<enterprise_management::PolicyData
> policy
,
195 const base::Closure
& callback
);
197 // Makes sure there's a reload operation so changes to the settings (and key,
198 // in case force_key_load is set) are getting picked up.
199 void EnsureReload(bool force_key_load
);
201 // Runs the next pending operation.
202 void StartNextOperation();
204 // Updates status, policy data and owner key from a finished operation.
205 // Starts the next pending operation if available.
206 void HandleCompletedOperation(const base::Closure
& callback
,
207 SessionManagerOperation
* operation
,
210 // Updates status and invokes the callback immediately.
211 void HandleError(Status status
, const base::Closure
& callback
);
213 SessionManagerClient
* session_manager_client_
;
214 scoped_refptr
<ownership::OwnerKeyUtil
> owner_key_util_
;
216 Status store_status_
;
218 std::vector
<OwnershipStatusCallback
> pending_ownership_status_callbacks_
;
220 std::string username_
;
221 scoped_refptr
<ownership::PublicKey
> public_key_
;
222 base::WeakPtr
<ownership::OwnerSettingsService
> owner_settings_service_
;
224 scoped_ptr
<enterprise_management::PolicyData
> policy_data_
;
225 scoped_ptr
<enterprise_management::ChromeDeviceSettingsProto
> device_settings_
;
227 // The queue of pending operations. The first operation on the queue is
228 // currently active; it gets removed and destroyed once it completes.
229 std::deque
<linked_ptr
<SessionManagerOperation
>> pending_operations_
;
231 ObserverList
<Observer
> observers_
;
233 // For recoverable load errors how many retries are left before we give up.
234 int load_retries_left_
;
236 base::WeakPtrFactory
<DeviceSettingsService
> weak_factory_
;
238 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsService
);
241 // Helper class for tests. Initializes the DeviceSettingsService singleton on
242 // construction and tears it down again on destruction.
243 class ScopedTestDeviceSettingsService
{
245 ScopedTestDeviceSettingsService();
246 ~ScopedTestDeviceSettingsService();
249 DISALLOW_COPY_AND_ASSIGN(ScopedTestDeviceSettingsService
);
252 } // namespace chromeos
254 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_SERVICE_H_