| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 5 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 // Delay between load retries when there was a validation error. | 30 // Delay between load retries when there was a validation error. |
| 31 // NOTE: This code is here to mitigate clock loss on some devices where policy | 31 // NOTE: This code is here to mitigate clock loss on some devices where policy |
| 32 // loads will fail with a validation error caused by RTC clock being reset when | 32 // loads will fail with a validation error caused by RTC clock being reset when |
| 33 // the battery is drained. | 33 // the battery is drained. |
| 34 int kLoadRetryDelayMs = 1000 * 5; | 34 int kLoadRetryDelayMs = 1000 * 5; |
| 35 // Maximal number of retries before we give up. Calculated to allow for 10 min | 35 // Maximal number of retries before we give up. Calculated to allow for 10 min |
| 36 // of retry time. | 36 // of retry time. |
| 37 int kMaxLoadRetries = (1000 * 60 * 10) / kLoadRetryDelayMs; | 37 int kMaxLoadRetries = (1000 * 60 * 10) / kLoadRetryDelayMs; |
| 38 | 38 |
| 39 // Returns true if it is okay to transfer from the current mode to the new | |
| 40 // mode. This function should be called in SetManagementMode(). | |
| 41 bool CheckManagementModeTransition(em::PolicyData::ManagementMode current_mode, | |
| 42 em::PolicyData::ManagementMode new_mode) { | |
| 43 // Mode is not changed. | |
| 44 if (current_mode == new_mode) | |
| 45 return true; | |
| 46 | |
| 47 switch (current_mode) { | |
| 48 case em::PolicyData::LOCAL_OWNER: | |
| 49 // For consumer management enrollment. | |
| 50 return new_mode == em::PolicyData::CONSUMER_MANAGED; | |
| 51 | |
| 52 case em::PolicyData::ENTERPRISE_MANAGED: | |
| 53 // Management mode cannot be set when it is currently ENTERPRISE_MANAGED. | |
| 54 return false; | |
| 55 | |
| 56 case em::PolicyData::CONSUMER_MANAGED: | |
| 57 // For consumer management unenrollment. | |
| 58 return new_mode == em::PolicyData::LOCAL_OWNER; | |
| 59 } | |
| 60 | |
| 61 NOTREACHED(); | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 } // namespace | 39 } // namespace |
| 66 | 40 |
| 67 namespace chromeos { | 41 namespace chromeos { |
| 68 | 42 |
| 69 DeviceSettingsService::Observer::~Observer() {} | 43 DeviceSettingsService::Observer::~Observer() {} |
| 70 | 44 |
| 71 static DeviceSettingsService* g_device_settings_service = NULL; | 45 static DeviceSettingsService* g_device_settings_service = NULL; |
| 72 | 46 |
| 73 // static | 47 // static |
| 74 void DeviceSettingsService::Initialize() { | 48 void DeviceSettingsService::Initialize() { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 106 } |
| 133 | 107 |
| 134 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() { | 108 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() { |
| 135 return public_key_; | 109 return public_key_; |
| 136 } | 110 } |
| 137 | 111 |
| 138 void DeviceSettingsService::Load() { | 112 void DeviceSettingsService::Load() { |
| 139 EnqueueLoad(false); | 113 EnqueueLoad(false); |
| 140 } | 114 } |
| 141 | 115 |
| 142 void DeviceSettingsService::SetManagementSettings( | |
| 143 em::PolicyData::ManagementMode management_mode, | |
| 144 const std::string& request_token, | |
| 145 const std::string& device_id, | |
| 146 const base::Closure& callback) { | |
| 147 if (!owner_settings_service_) { | |
| 148 HandleError(STORE_KEY_UNAVAILABLE, callback); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 em::PolicyData::ManagementMode current_mode = em::PolicyData::LOCAL_OWNER; | |
| 153 if (policy_data() && policy_data()->has_management_mode()) | |
| 154 current_mode = policy_data()->management_mode(); | |
| 155 | |
| 156 if (!CheckManagementModeTransition(current_mode, management_mode)) { | |
| 157 LOG(ERROR) << "Invalid management mode transition: current mode = " | |
| 158 << current_mode << ", new mode = " << management_mode; | |
| 159 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 scoped_ptr<em::PolicyData> policy = | |
| 164 OwnerSettingsServiceChromeOS::AssemblePolicy( | |
| 165 GetUsername(), policy_data(), device_settings()); | |
| 166 if (!policy) { | |
| 167 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 policy->set_management_mode(management_mode); | |
| 172 policy->set_request_token(request_token); | |
| 173 policy->set_device_id(device_id); | |
| 174 | |
| 175 EnqueueSignAndStore(policy.Pass(), callback); | |
| 176 } | |
| 177 | |
| 178 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy, | 116 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy, |
| 179 const base::Closure& callback) { | 117 const base::Closure& callback) { |
| 180 Enqueue(linked_ptr<SessionManagerOperation>(new StoreSettingsOperation( | 118 Enqueue(linked_ptr<SessionManagerOperation>(new StoreSettingsOperation( |
| 181 base::Bind(&DeviceSettingsService::HandleCompletedOperation, | 119 base::Bind(&DeviceSettingsService::HandleCompletedOperation, |
| 182 weak_factory_.GetWeakPtr(), | 120 weak_factory_.GetWeakPtr(), |
| 183 callback), | 121 callback), |
| 184 policy.Pass()))); | 122 policy.Pass()))); |
| 185 } | 123 } |
| 186 | 124 |
| 187 DeviceSettingsService::OwnershipStatus | 125 DeviceSettingsService::OwnershipStatus |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 if (pending_operations_.front().get() == operation.get()) | 205 if (pending_operations_.front().get() == operation.get()) |
| 268 StartNextOperation(); | 206 StartNextOperation(); |
| 269 } | 207 } |
| 270 | 208 |
| 271 void DeviceSettingsService::EnqueueLoad(bool force_key_load) { | 209 void DeviceSettingsService::EnqueueLoad(bool force_key_load) { |
| 272 linked_ptr<SessionManagerOperation> operation(new LoadSettingsOperation( | 210 linked_ptr<SessionManagerOperation> operation(new LoadSettingsOperation( |
| 273 base::Bind(&DeviceSettingsService::HandleCompletedOperation, | 211 base::Bind(&DeviceSettingsService::HandleCompletedOperation, |
| 274 weak_factory_.GetWeakPtr(), | 212 weak_factory_.GetWeakPtr(), |
| 275 base::Closure()))); | 213 base::Closure()))); |
| 276 operation->set_force_key_load(force_key_load); | 214 operation->set_force_key_load(force_key_load); |
| 277 operation->set_username(username_); | |
| 278 operation->set_owner_settings_service(owner_settings_service_); | |
| 279 Enqueue(operation); | |
| 280 } | |
| 281 | |
| 282 void DeviceSettingsService::EnqueueSignAndStore( | |
| 283 scoped_ptr<enterprise_management::PolicyData> policy, | |
| 284 const base::Closure& callback) { | |
| 285 linked_ptr<SessionManagerOperation> operation( | |
| 286 new SignAndStoreSettingsOperation( | |
| 287 base::Bind(&DeviceSettingsService::HandleCompletedOperation, | |
| 288 weak_factory_.GetWeakPtr(), | |
| 289 callback), | |
| 290 policy.Pass())); | |
| 291 operation->set_owner_settings_service(owner_settings_service_); | |
| 292 Enqueue(operation); | 215 Enqueue(operation); |
| 293 } | 216 } |
| 294 | 217 |
| 295 void DeviceSettingsService::EnsureReload(bool force_key_load) { | 218 void DeviceSettingsService::EnsureReload(bool force_key_load) { |
| 296 if (!pending_operations_.empty()) { | 219 if (!pending_operations_.empty()) |
| 297 pending_operations_.front()->set_username(username_); | |
| 298 pending_operations_.front()->set_owner_settings_service( | |
| 299 owner_settings_service_); | |
| 300 pending_operations_.front()->RestartLoad(force_key_load); | 220 pending_operations_.front()->RestartLoad(force_key_load); |
| 301 } else { | 221 else |
| 302 EnqueueLoad(force_key_load); | 222 EnqueueLoad(force_key_load); |
| 303 } | |
| 304 } | 223 } |
| 305 | 224 |
| 306 void DeviceSettingsService::StartNextOperation() { | 225 void DeviceSettingsService::StartNextOperation() { |
| 307 if (!pending_operations_.empty() && session_manager_client_ && | 226 if (!pending_operations_.empty() && session_manager_client_ && |
| 308 owner_key_util_.get()) { | 227 owner_key_util_.get()) { |
| 309 pending_operations_.front()->Start( | 228 pending_operations_.front()->Start( |
| 310 session_manager_client_, owner_key_util_, public_key_); | 229 session_manager_client_, owner_key_util_, public_key_); |
| 311 } | 230 } |
| 312 } | 231 } |
| 313 | 232 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 DeviceSettingsService::Initialize(); | 323 DeviceSettingsService::Initialize(); |
| 405 } | 324 } |
| 406 | 325 |
| 407 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() { | 326 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() { |
| 408 // Clean pending operations. | 327 // Clean pending operations. |
| 409 DeviceSettingsService::Get()->UnsetSessionManager(); | 328 DeviceSettingsService::Get()->UnsetSessionManager(); |
| 410 DeviceSettingsService::Shutdown(); | 329 DeviceSettingsService::Shutdown(); |
| 411 } | 330 } |
| 412 | 331 |
| 413 } // namespace chromeos | 332 } // namespace chromeos |
| OLD | NEW |