session_manager_operation.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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_SESSION_MANAGER_OPERATION_H_
6#define CHROME_BROWSER_CHROMEOS_SETTINGS_SESSION_MANAGER_OPERATION_H_
7
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/settings/device_settings_service.h"
13
14namespace enterprise_management {
15class ChromeDeviceSettingsProto;
16class PolicyData;
17class PolicyFetchResponse;
18}
19
20namespace chromeos {
21
22class OwnerKeyUtil;
23class SessionManagerClient;
24
25// Handles a single transaction with session manager. This is a virtual base
26// class that contains common infrastructure for key and policy loading. There
27// are subclasses for loading, storing and signing policy blobs.
28class SessionManagerOperation {
29 public:
30  typedef base::Callback<void(SessionManagerOperation*,
31                              DeviceSettingsService::Status)> Callback;
32
33  // Creates a new load operation.
34  explicit SessionManagerOperation(const Callback& callback);
35  virtual ~SessionManagerOperation();
36
37  // Starts the operation.
38  void Start(SessionManagerClient* session_manager_client,
39             scoped_refptr<OwnerKeyUtil> owner_key_util,
40             scoped_refptr<OwnerKey> owner_key);
41
42  // Restarts a load operation (if that part is already in progress).
43  void RestartLoad(bool key_changed);
44
45  // Accessors for recovering the loaded policy data after completion.
46  scoped_ptr<enterprise_management::PolicyData>& policy_data() {
47    return policy_data_;
48  }
49  scoped_ptr<enterprise_management::ChromeDeviceSettingsProto>&
50      device_settings() {
51    return device_settings_;
52  }
53
54  // Owner key as configured/loaded from disk.
55  scoped_refptr<OwnerKey> owner_key() {
56    return owner_key_;
57  }
58
59  // Whether the load operation is underway.
60  bool is_loading() const {
61    return is_loading_;
62  }
63
64  void set_force_key_load(bool force_key_load) {
65    force_key_load_ = force_key_load;
66  }
67
68 protected:
69  // Runs the operation. The result is reported through |callback_|.
70  virtual void Run() = 0;
71
72  // Ensures the owner key is loaded.
73  void EnsureOwnerKey(const base::Closure& callback);
74
75  // Starts a load operation.
76  void StartLoading();
77
78  // Reports the result status of the operation. Once this gets called, the
79  // operation should not perform further processing or trigger callbacks.
80  void ReportResult(DeviceSettingsService::Status status);
81
82  SessionManagerClient* session_manager_client() {
83    return session_manager_client_;
84  }
85
86 private:
87  // Loads the owner key from disk. Must be run on a thread that can do I/O.
88  static scoped_refptr<OwnerKey> LoadOwnerKey(
89      scoped_refptr<OwnerKeyUtil> util,
90      scoped_refptr<OwnerKey> current_key);
91
92  // Stores the owner key loaded by LoadOwnerKey and calls |callback|.
93  void StoreOwnerKey(const base::Closure& callback,
94                     scoped_refptr<OwnerKey> new_key);
95
96  // Triggers a device settings load.
97  void RetrieveDeviceSettings();
98
99  // Validates device settings after retrieval from session_manager.
100  void ValidateDeviceSettings(const std::string& policy_blob);
101
102  // Extracts status and device settings from the validator and reports them.
103  void ReportValidatorStatus(policy::DeviceCloudPolicyValidator* validator);
104
105  SessionManagerClient* session_manager_client_;
106  scoped_refptr<OwnerKeyUtil> owner_key_util_;
107
108  base::WeakPtrFactory<SessionManagerOperation> weak_factory_;
109
110  Callback callback_;
111
112  scoped_refptr<OwnerKey> owner_key_;
113  bool force_key_load_;
114
115  bool is_loading_;
116  scoped_ptr<enterprise_management::PolicyData> policy_data_;
117  scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> device_settings_;
118
119  DISALLOW_COPY_AND_ASSIGN(SessionManagerOperation);
120};
121
122// This operation loads the public owner key from disk if appropriate, fetches
123// the policy blob from session manager, and validates the loaded policy blob.
124class LoadSettingsOperation : public SessionManagerOperation {
125 public:
126  // Creates a new load operation.
127  explicit LoadSettingsOperation(const Callback& callback);
128  virtual ~LoadSettingsOperation();
129
130 protected:
131  // SessionManagerOperation:
132  virtual void Run() OVERRIDE;
133
134 private:
135  DISALLOW_COPY_AND_ASSIGN(LoadSettingsOperation);
136};
137
138// Stores a pre-generated policy blob and reloads the device settings from
139// session_manager.
140class StoreSettingsOperation : public SessionManagerOperation {
141 public:
142  // Creates a new store operation.
143  StoreSettingsOperation(
144      const Callback& callback,
145      scoped_ptr<enterprise_management::PolicyFetchResponse> policy);
146  virtual ~StoreSettingsOperation();
147
148 protected:
149  // SessionManagerOperation:
150  virtual void Run() OVERRIDE;
151
152 private:
153  // Handles the result of the store operation and triggers the load.
154  void HandleStoreResult(bool success);
155
156  scoped_ptr<enterprise_management::PolicyFetchResponse> policy_;
157
158  base::WeakPtrFactory<StoreSettingsOperation> weak_factory_;
159
160  DISALLOW_COPY_AND_ASSIGN(StoreSettingsOperation);
161};
162
163// Signs device settings and stores the resulting blob to session_manager.
164class SignAndStoreSettingsOperation : public SessionManagerOperation {
165 public:
166  // Creates a new sign-and-store operation.
167  SignAndStoreSettingsOperation(
168      const Callback& callback,
169      scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> new_settings,
170      const std::string& username);
171  virtual ~SignAndStoreSettingsOperation();
172
173  // SessionManagerOperation:
174  virtual void Run() OVERRIDE;
175
176 private:
177  // Given an owner key, starts the signing operation.
178  void StartSigning();
179
180  // Builds the policy blob and signs it using the owner key.
181  static std::string AssembleAndSignPolicy(
182      scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> settings,
183      const std::string& username,
184      scoped_refptr<OwnerKey> owner_key);
185
186  // Stores the signed device settings blob.
187  void StoreDeviceSettingsBlob(std::string device_settings_blob);
188
189  // Handles the result of the store operation and triggers the load.
190  void HandleStoreResult(bool success);
191
192  scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> new_settings_;
193  std::string username_;
194
195  base::WeakPtrFactory<SignAndStoreSettingsOperation> weak_factory_;
196
197  DISALLOW_COPY_AND_ASSIGN(SignAndStoreSettingsOperation);
198};
199
200}  // namespace
201
202#endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_SESSION_MANAGER_OPERATION_H_
203