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_SESSION_MANAGER_OPERATION_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_SESSION_MANAGER_OPERATION_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/chromeos/policy/device_cloud_policy_validator.h"
13 #include "chrome/browser/chromeos/settings/device_settings_service.h"
15 namespace enterprise_management {
16 class ChromeDeviceSettingsProto;
18 class PolicyFetchResponse;
24 class SessionManagerClient;
26 // Handles a single transaction with session manager. This is a virtual base
27 // class that contains common infrastructure for key and policy loading. There
28 // are subclasses for loading, storing and signing policy blobs.
29 class SessionManagerOperation {
31 typedef base::Callback<void(SessionManagerOperation*,
32 DeviceSettingsService::Status)> Callback;
34 // Creates a new load operation.
35 explicit SessionManagerOperation(const Callback& callback);
36 virtual ~SessionManagerOperation();
38 // Starts the operation.
39 void Start(SessionManagerClient* session_manager_client,
40 scoped_refptr<OwnerKeyUtil> owner_key_util,
41 scoped_refptr<OwnerKey> owner_key);
43 // Restarts a load operation (if that part is already in progress).
44 void RestartLoad(bool key_changed);
46 // Accessors for recovering the loaded policy data after completion.
47 scoped_ptr<enterprise_management::PolicyData>& policy_data() {
50 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto>&
52 return device_settings_;
55 // Owner key as configured/loaded from disk.
56 scoped_refptr<OwnerKey> owner_key() {
60 // Whether the load operation is underway.
61 bool is_loading() const {
65 void set_force_key_load(bool force_key_load) {
66 force_key_load_ = force_key_load;
70 // Runs the operation. The result is reported through |callback_|.
71 virtual void Run() = 0;
73 // Ensures the owner key is loaded.
74 void EnsureOwnerKey(const base::Closure& callback);
76 // Starts a load operation.
79 // Reports the result status of the operation. Once this gets called, the
80 // operation should not perform further processing or trigger callbacks.
81 void ReportResult(DeviceSettingsService::Status status);
83 SessionManagerClient* session_manager_client() {
84 return session_manager_client_;
88 // Loads the owner key from disk. Must be run on a thread that can do I/O.
89 static scoped_refptr<OwnerKey> LoadOwnerKey(
90 scoped_refptr<OwnerKeyUtil> util,
91 scoped_refptr<OwnerKey> current_key);
93 // Stores the owner key loaded by LoadOwnerKey and calls |callback|.
94 void StoreOwnerKey(const base::Closure& callback,
95 scoped_refptr<OwnerKey> new_key);
97 // Triggers a device settings load.
98 void RetrieveDeviceSettings();
100 // Validates device settings after retrieval from session_manager.
101 void ValidateDeviceSettings(const std::string& policy_blob);
103 // Extracts status and device settings from the validator and reports them.
104 void ReportValidatorStatus(policy::DeviceCloudPolicyValidator* validator);
106 SessionManagerClient* session_manager_client_;
107 scoped_refptr<OwnerKeyUtil> owner_key_util_;
109 base::WeakPtrFactory<SessionManagerOperation> weak_factory_;
113 scoped_refptr<OwnerKey> owner_key_;
114 bool force_key_load_;
117 scoped_ptr<enterprise_management::PolicyData> policy_data_;
118 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> device_settings_;
120 DISALLOW_COPY_AND_ASSIGN(SessionManagerOperation);
123 // This operation loads the public owner key from disk if appropriate, fetches
124 // the policy blob from session manager, and validates the loaded policy blob.
125 class LoadSettingsOperation : public SessionManagerOperation {
127 // Creates a new load operation.
128 explicit LoadSettingsOperation(const Callback& callback);
129 virtual ~LoadSettingsOperation();
132 // SessionManagerOperation:
133 virtual void Run() OVERRIDE;
136 DISALLOW_COPY_AND_ASSIGN(LoadSettingsOperation);
139 // Stores a pre-generated policy blob and reloads the device settings from
141 class StoreSettingsOperation : public SessionManagerOperation {
143 // Creates a new store operation.
144 StoreSettingsOperation(
145 const Callback& callback,
146 scoped_ptr<enterprise_management::PolicyFetchResponse> policy);
147 virtual ~StoreSettingsOperation();
150 // SessionManagerOperation:
151 virtual void Run() OVERRIDE;
154 // Handles the result of the store operation and triggers the load.
155 void HandleStoreResult(bool success);
157 scoped_ptr<enterprise_management::PolicyFetchResponse> policy_;
159 base::WeakPtrFactory<StoreSettingsOperation> weak_factory_;
161 DISALLOW_COPY_AND_ASSIGN(StoreSettingsOperation);
164 // Signs device settings and stores the resulting blob to session_manager.
165 class SignAndStoreSettingsOperation : public SessionManagerOperation {
167 // Creates a new sign-and-store operation.
168 SignAndStoreSettingsOperation(
169 const Callback& callback,
170 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> new_settings,
171 const std::string& username);
172 virtual ~SignAndStoreSettingsOperation();
174 // SessionManagerOperation:
175 virtual void Run() OVERRIDE;
178 // Given an owner key, starts the signing operation.
181 // Builds the policy blob and signs it using the owner key.
182 static std::string AssembleAndSignPolicy(
183 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> settings,
184 const std::string& username,
185 scoped_refptr<OwnerKey> owner_key);
187 // Stores the signed device settings blob.
188 void StoreDeviceSettingsBlob(std::string device_settings_blob);
190 // Handles the result of the store operation and triggers the load.
191 void HandleStoreResult(bool success);
193 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> new_settings_;
194 std::string username_;
196 base::WeakPtrFactory<SignAndStoreSettingsOperation> weak_factory_;
198 DISALLOW_COPY_AND_ASSIGN(SignAndStoreSettingsOperation);
203 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_SESSION_MANAGER_OPERATION_H_