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 "net/cert/x509_util_nss.h"
16 namespace enterprise_management
{
17 class ChromeDeviceSettingsProto
;
19 class PolicyFetchResponse
;
29 class SessionManagerClient
;
31 // Handles a single transaction with session manager. This is a virtual base
32 // class that contains common infrastructure for key and policy loading. There
33 // are subclasses for loading, storing and signing policy blobs.
34 class SessionManagerOperation
{
36 typedef base::Callback
<void(SessionManagerOperation
*,
37 DeviceSettingsService::Status
)> Callback
;
39 // Creates a new load operation.
40 explicit SessionManagerOperation(const Callback
& callback
);
41 virtual ~SessionManagerOperation();
43 // Starts the operation.
44 void Start(SessionManagerClient
* session_manager_client
,
45 scoped_refptr
<ownership::OwnerKeyUtil
> owner_key_util
,
46 scoped_refptr
<ownership::PublicKey
> public_key
);
48 // Restarts a load operation (if that part is already in progress).
49 void RestartLoad(bool key_changed
);
51 // Accessors for recovering the loaded policy data after completion.
52 scoped_ptr
<enterprise_management::PolicyData
>& policy_data() {
55 scoped_ptr
<enterprise_management::ChromeDeviceSettingsProto
>&
57 return device_settings_
;
60 // Public part of the owner key as configured/loaded from disk.
61 scoped_refptr
<ownership::PublicKey
> public_key() { return public_key_
; }
63 // Whether the load operation is underway.
64 bool is_loading() const { return is_loading_
; }
66 void set_force_key_load(bool force_key_load
) {
67 force_key_load_
= force_key_load
;
71 // Runs the operation. The result is reported through |callback_|.
72 virtual void Run() = 0;
74 // Ensures the public key is loaded.
75 void EnsurePublicKey(const base::Closure
& callback
);
77 // Starts a load operation.
80 // Reports the result status of the operation. Once this gets called, the
81 // operation should not perform further processing or trigger callbacks.
82 void ReportResult(DeviceSettingsService::Status status
);
84 SessionManagerClient
* session_manager_client() {
85 return session_manager_client_
;
89 // Loads the owner key from disk. Must be run on a thread that can do I/O.
90 static scoped_refptr
<ownership::PublicKey
> LoadPublicKey(
91 scoped_refptr
<ownership::OwnerKeyUtil
> util
,
92 scoped_refptr
<ownership::PublicKey
> current_key
);
94 // Stores the owner key loaded by LoadOwnerKey and calls |callback|.
95 void StorePublicKey(const base::Closure
& callback
,
96 scoped_refptr
<ownership::PublicKey
> new_key
);
98 // Triggers a device settings load.
99 void RetrieveDeviceSettings();
101 // Validates device settings after retrieval from session_manager.
102 void ValidateDeviceSettings(const std::string
& policy_blob
);
104 // Extracts status and device settings from the validator and reports them.
105 void ReportValidatorStatus(policy::DeviceCloudPolicyValidator
* validator
);
107 SessionManagerClient
* session_manager_client_
;
108 scoped_refptr
<ownership::OwnerKeyUtil
> owner_key_util_
;
112 scoped_refptr
<ownership::PublicKey
> public_key_
;
113 bool force_key_load_
;
116 scoped_ptr
<enterprise_management::PolicyData
> policy_data_
;
117 scoped_ptr
<enterprise_management::ChromeDeviceSettingsProto
> device_settings_
;
119 base::WeakPtrFactory
<SessionManagerOperation
> weak_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(SessionManagerOperation
);
124 // This operation loads the public owner key from disk if appropriate, fetches
125 // the policy blob from session manager, and validates the loaded policy blob.
126 class LoadSettingsOperation
: public SessionManagerOperation
{
128 // Creates a new load operation.
129 explicit LoadSettingsOperation(const Callback
& callback
);
130 ~LoadSettingsOperation() override
;
133 // SessionManagerOperation:
137 DISALLOW_COPY_AND_ASSIGN(LoadSettingsOperation
);
140 // Stores a pre-generated policy blob and reloads the device settings from
142 class StoreSettingsOperation
: public SessionManagerOperation
{
144 // Creates a new store operation.
145 StoreSettingsOperation(
146 const Callback
& callback
,
147 scoped_ptr
<enterprise_management::PolicyFetchResponse
> policy
);
148 ~StoreSettingsOperation() override
;
151 // SessionManagerOperation:
155 // Handles the result of the store operation and triggers the load.
156 void HandleStoreResult(bool success
);
158 scoped_ptr
<enterprise_management::PolicyFetchResponse
> policy_
;
160 base::WeakPtrFactory
<StoreSettingsOperation
> weak_factory_
;
162 DISALLOW_COPY_AND_ASSIGN(StoreSettingsOperation
);
165 } // namespace chromeos
167 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_SESSION_MANAGER_OPERATION_H_