[Android] Allow multiple --install in bb_device_steps.py.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / session_manager_operation.h
blob356025e9514612c2d9204a1a266c1e21b8078041
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"
14 #include "components/ownership/owner_settings_service.h"
15 #include "net/cert/x509_util_nss.h"
17 namespace enterprise_management {
18 class ChromeDeviceSettingsProto;
19 class PolicyData;
20 class PolicyFetchResponse;
23 namespace ownership {
24 class OwnerKeyUtil;
25 class PublicKey;
28 namespace chromeos {
30 class SessionManagerClient;
32 // Handles a single transaction with session manager. This is a virtual base
33 // class that contains common infrastructure for key and policy loading. There
34 // are subclasses for loading, storing and signing policy blobs.
35 class SessionManagerOperation {
36 public:
37 typedef base::Callback<void(SessionManagerOperation*,
38 DeviceSettingsService::Status)> Callback;
40 // Creates a new load operation.
41 explicit SessionManagerOperation(const Callback& callback);
42 virtual ~SessionManagerOperation();
44 // Starts the operation.
45 void Start(SessionManagerClient* session_manager_client,
46 scoped_refptr<ownership::OwnerKeyUtil> owner_key_util,
47 scoped_refptr<ownership::PublicKey> public_key);
49 // Restarts a load operation (if that part is already in progress).
50 void RestartLoad(bool key_changed);
52 // Accessors for recovering the loaded policy data after completion.
53 scoped_ptr<enterprise_management::PolicyData>& policy_data() {
54 return policy_data_;
56 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto>&
57 device_settings() {
58 return device_settings_;
61 // Public part of the owner key as configured/loaded from disk.
62 scoped_refptr<ownership::PublicKey> public_key() { return public_key_; }
64 // Whether the load operation is underway.
65 bool is_loading() const { return is_loading_; }
67 void set_force_key_load(bool force_key_load) {
68 force_key_load_ = force_key_load;
71 void set_username(const std::string& username) { username_ = username; }
73 void set_owner_settings_service(const base::WeakPtr<
74 ownership::OwnerSettingsService>& owner_settings_service) {
75 owner_settings_service_ = owner_settings_service;
78 protected:
79 // Runs the operation. The result is reported through |callback_|.
80 virtual void Run() = 0;
82 // Ensures the public key is loaded.
83 void EnsurePublicKey(const base::Closure& callback);
85 // Starts a load operation.
86 void StartLoading();
88 // Reports the result status of the operation. Once this gets called, the
89 // operation should not perform further processing or trigger callbacks.
90 void ReportResult(DeviceSettingsService::Status status);
92 SessionManagerClient* session_manager_client() {
93 return session_manager_client_;
96 base::WeakPtr<ownership::OwnerSettingsService> owner_settings_service_;
98 private:
99 // Loads the owner key from disk. Must be run on a thread that can do I/O.
100 static scoped_refptr<ownership::PublicKey> LoadPublicKey(
101 scoped_refptr<ownership::OwnerKeyUtil> util,
102 scoped_refptr<ownership::PublicKey> current_key);
104 // Stores the owner key loaded by LoadOwnerKey and calls |callback|.
105 void StorePublicKey(const base::Closure& callback,
106 scoped_refptr<ownership::PublicKey> new_key);
108 // Triggers a device settings load.
109 void RetrieveDeviceSettings();
111 // Validates device settings after retrieval from session_manager.
112 void ValidateDeviceSettings(const std::string& policy_blob);
114 // Extracts status and device settings from the validator and reports them.
115 void ReportValidatorStatus(policy::DeviceCloudPolicyValidator* validator);
117 SessionManagerClient* session_manager_client_;
118 scoped_refptr<ownership::OwnerKeyUtil> owner_key_util_;
120 Callback callback_;
122 scoped_refptr<ownership::PublicKey> public_key_;
123 bool force_key_load_;
124 std::string username_;
126 bool is_loading_;
127 scoped_ptr<enterprise_management::PolicyData> policy_data_;
128 scoped_ptr<enterprise_management::ChromeDeviceSettingsProto> device_settings_;
130 base::WeakPtrFactory<SessionManagerOperation> weak_factory_;
132 DISALLOW_COPY_AND_ASSIGN(SessionManagerOperation);
135 // This operation loads the public owner key from disk if appropriate, fetches
136 // the policy blob from session manager, and validates the loaded policy blob.
137 class LoadSettingsOperation : public SessionManagerOperation {
138 public:
139 // Creates a new load operation.
140 explicit LoadSettingsOperation(const Callback& callback);
141 virtual ~LoadSettingsOperation();
143 protected:
144 // SessionManagerOperation:
145 virtual void Run() override;
147 private:
148 DISALLOW_COPY_AND_ASSIGN(LoadSettingsOperation);
151 // Stores a pre-generated policy blob and reloads the device settings from
152 // session_manager.
153 class StoreSettingsOperation : public SessionManagerOperation {
154 public:
155 // Creates a new store operation.
156 StoreSettingsOperation(
157 const Callback& callback,
158 scoped_ptr<enterprise_management::PolicyFetchResponse> policy);
159 virtual ~StoreSettingsOperation();
161 protected:
162 // SessionManagerOperation:
163 virtual void Run() override;
165 private:
166 // Handles the result of the store operation and triggers the load.
167 void HandleStoreResult(bool success);
169 scoped_ptr<enterprise_management::PolicyFetchResponse> policy_;
171 base::WeakPtrFactory<StoreSettingsOperation> weak_factory_;
173 DISALLOW_COPY_AND_ASSIGN(StoreSettingsOperation);
176 // Signs device settings and stores the resulting blob to session_manager.
177 class SignAndStoreSettingsOperation : public SessionManagerOperation {
178 public:
179 // Creates a new sign-and-store operation.
180 SignAndStoreSettingsOperation(
181 const Callback& callback,
182 scoped_ptr<enterprise_management::PolicyData> new_policy);
183 virtual ~SignAndStoreSettingsOperation();
185 // SessionManagerOperation:
186 virtual void Run() override;
188 private:
189 void StartSigning(bool has_private_key);
191 // Stores the signed device settings blob.
192 void StoreDeviceSettings(
193 scoped_ptr<enterprise_management::PolicyFetchResponse> policy_response);
195 // Handles the result of the store operation and triggers the load.
196 void HandleStoreResult(bool success);
198 scoped_ptr<enterprise_management::PolicyData> new_policy_;
200 base::WeakPtrFactory<SignAndStoreSettingsOperation> weak_factory_;
202 DISALLOW_COPY_AND_ASSIGN(SignAndStoreSettingsOperation);
205 } // namespace chromeos
207 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_SESSION_MANAGER_OPERATION_H_