Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/chromeos/settings/device_settings_service.cc

Issue 654263003: Implemented OwnerSettingsService::Set() method. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
13 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" 14 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
14 #include "chrome/browser/chromeos/settings/session_manager_operation.h" 15 #include "chrome/browser/chromeos/settings/session_manager_operation.h"
15 #include "components/ownership/owner_key_util.h" 16 #include "components/ownership/owner_key_util.h"
16 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 17 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_service.h" 19 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h" 20 #include "content/public/browser/notification_source.h"
20 #include "crypto/rsa_private_key.h" 21 #include "crypto/rsa_private_key.h"
21 22
22 namespace em = enterprise_management; 23 namespace em = enterprise_management;
23 24
24 using ownership::OwnerKeyUtil; 25 using ownership::OwnerKeyUtil;
25 using ownership::PublicKey; 26 using ownership::PublicKey;
26 27
27 namespace { 28 namespace {
28 29
29 // Delay between load retries when there was a validation error. 30 // Delay between load retries when there was a validation error.
30 // 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
31 // 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
32 // the battery is drained. 33 // the battery is drained.
33 int kLoadRetryDelayMs = 1000 * 5; 34 int kLoadRetryDelayMs = 1000 * 5;
34 // 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
35 // of retry time. 36 // of retry time.
36 int kMaxLoadRetries = (1000 * 60 * 10) / kLoadRetryDelayMs; 37 int kMaxLoadRetries = (1000 * 60 * 10) / kLoadRetryDelayMs;
37 38
38 // Assembles PolicyData based on |settings|, |policy_data| and
39 // |user_id|.
40 scoped_ptr<em::PolicyData> AssemblePolicy(
41 const std::string& user_id,
42 const em::PolicyData* policy_data,
43 const em::ChromeDeviceSettingsProto* settings) {
44 scoped_ptr<em::PolicyData> policy(new em::PolicyData());
45 if (policy_data) {
46 // Preserve management settings.
47 if (policy_data->has_management_mode())
48 policy->set_management_mode(policy_data->management_mode());
49 if (policy_data->has_request_token())
50 policy->set_request_token(policy_data->request_token());
51 if (policy_data->has_device_id())
52 policy->set_device_id(policy_data->device_id());
53 } else {
54 // If there's no previous policy data, this is the first time the device
55 // setting is set. We set the management mode to NOT_MANAGED initially.
56 policy->set_management_mode(em::PolicyData::NOT_MANAGED);
57 }
58 policy->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType);
59 policy->set_timestamp(
60 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds());
61 policy->set_username(user_id);
62 if (!settings->SerializeToString(policy->mutable_policy_value()))
63 return scoped_ptr<em::PolicyData>();
64
65 return policy.Pass();
66 }
67
68 // Returns true if it is okay to transfer from the current mode to the new 39 // Returns true if it is okay to transfer from the current mode to the new
69 // mode. This function should be called in SetManagementMode(). 40 // mode. This function should be called in SetManagementMode().
70 bool CheckManagementModeTransition(em::PolicyData::ManagementMode current_mode, 41 bool CheckManagementModeTransition(em::PolicyData::ManagementMode current_mode,
71 em::PolicyData::ManagementMode new_mode) { 42 em::PolicyData::ManagementMode new_mode) {
72 // Mode is not changed. 43 // Mode is not changed.
73 if (current_mode == new_mode) 44 if (current_mode == new_mode)
74 return true; 45 return true;
75 46
76 switch (current_mode) { 47 switch (current_mode) {
77 case em::PolicyData::NOT_MANAGED: 48 case em::PolicyData::NOT_MANAGED:
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 115
145 session_manager_client_ = session_manager_client; 116 session_manager_client_ = session_manager_client;
146 owner_key_util_ = owner_key_util; 117 owner_key_util_ = owner_key_util;
147 118
148 session_manager_client_->AddObserver(this); 119 session_manager_client_->AddObserver(this);
149 120
150 StartNextOperation(); 121 StartNextOperation();
151 } 122 }
152 123
153 void DeviceSettingsService::UnsetSessionManager() { 124 void DeviceSettingsService::UnsetSessionManager() {
154 STLDeleteContainerPointers(pending_operations_.begin(),
155 pending_operations_.end());
156 pending_operations_.clear(); 125 pending_operations_.clear();
157 126
158 if (session_manager_client_) 127 if (session_manager_client_)
159 session_manager_client_->RemoveObserver(this); 128 session_manager_client_->RemoveObserver(this);
160 session_manager_client_ = NULL; 129 session_manager_client_ = NULL;
161 owner_key_util_ = NULL; 130 owner_key_util_ = NULL;
162 } 131 }
163 132
164 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() { 133 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() {
165 return public_key_; 134 return public_key_;
166 } 135 }
167 136
168 void DeviceSettingsService::Load() { 137 void DeviceSettingsService::Load() {
169 EnqueueLoad(false); 138 EnqueueLoad(false);
170 } 139 }
171 140
172 void DeviceSettingsService::SignAndStore( 141 void DeviceSettingsService::SignAndStore(
173 scoped_ptr<em::ChromeDeviceSettingsProto> new_settings, 142 scoped_ptr<em::ChromeDeviceSettingsProto> new_settings,
174 const base::Closure& callback) { 143 const base::Closure& callback) {
175 if (!owner_settings_service_) {
176 HandleError(STORE_KEY_UNAVAILABLE, callback);
177 return;
178 }
179 scoped_ptr<em::PolicyData> policy = 144 scoped_ptr<em::PolicyData> policy =
180 AssemblePolicy(GetUsername(), policy_data(), new_settings.get()); 145 OwnerSettingsServiceChromeOS::AssemblePolicy(
181 if (!policy) { 146 GetUsername(), policy_data(), new_settings.get());
182 HandleError(STORE_POLICY_ERROR, callback); 147 EnqueueSignAndStore(policy.Pass(), callback);
183 return;
184 }
185
186 owner_settings_service_->SignAndStorePolicyAsync(policy.Pass(), callback);
187 } 148 }
188 149
189 void DeviceSettingsService::SetManagementSettings( 150 void DeviceSettingsService::SetManagementSettings(
Mattias Nissler (ping if slow) 2014/10/24 08:29:56 This should probably be re-routed through OwnerSet
ygorshenin1 2014/10/24 10:33:01 I've added a comment to the DeviceSettingsService:
190 em::PolicyData::ManagementMode management_mode, 151 em::PolicyData::ManagementMode management_mode,
191 const std::string& request_token, 152 const std::string& request_token,
192 const std::string& device_id, 153 const std::string& device_id,
193 const base::Closure& callback) { 154 const base::Closure& callback) {
194 if (!owner_settings_service_) { 155 if (!owner_settings_service_) {
195 HandleError(STORE_KEY_UNAVAILABLE, callback); 156 HandleError(STORE_KEY_UNAVAILABLE, callback);
196 return; 157 return;
197 } 158 }
198 159
199 em::PolicyData::ManagementMode current_mode = em::PolicyData::NOT_MANAGED; 160 em::PolicyData::ManagementMode current_mode = em::PolicyData::NOT_MANAGED;
200 if (policy_data() && policy_data()->has_management_mode()) 161 if (policy_data() && policy_data()->has_management_mode())
201 current_mode = policy_data()->management_mode(); 162 current_mode = policy_data()->management_mode();
202 163
203 if (!CheckManagementModeTransition(current_mode, management_mode)) { 164 if (!CheckManagementModeTransition(current_mode, management_mode)) {
204 LOG(ERROR) << "Invalid management mode transition: current mode = " 165 LOG(ERROR) << "Invalid management mode transition: current mode = "
205 << current_mode << ", new mode = " << management_mode; 166 << current_mode << ", new mode = " << management_mode;
206 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); 167 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback);
207 return; 168 return;
208 } 169 }
209 170
210 scoped_ptr<em::PolicyData> policy = 171 scoped_ptr<em::PolicyData> policy =
211 AssemblePolicy(GetUsername(), policy_data(), device_settings()); 172 OwnerSettingsServiceChromeOS::AssemblePolicy(
173 GetUsername(), policy_data(), device_settings());
212 if (!policy) { 174 if (!policy) {
213 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); 175 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback);
214 return; 176 return;
215 } 177 }
216 178
217 policy->set_management_mode(management_mode); 179 policy->set_management_mode(management_mode);
218 policy->set_request_token(request_token); 180 policy->set_request_token(request_token);
219 policy->set_device_id(device_id); 181 policy->set_device_id(device_id);
220 182
221 owner_settings_service_->SignAndStorePolicyAsync(policy.Pass(), callback); 183 EnqueueSignAndStore(policy.Pass(), callback);
222 } 184 }
223 185
224 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy, 186 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy,
225 const base::Closure& callback) { 187 const base::Closure& callback) {
226 Enqueue( 188 Enqueue(linked_ptr<SessionManagerOperation>(new StoreSettingsOperation(
227 new StoreSettingsOperation( 189 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
228 base::Bind(&DeviceSettingsService::HandleCompletedOperation, 190 weak_factory_.GetWeakPtr(),
229 weak_factory_.GetWeakPtr(), 191 callback),
230 callback), 192 policy.Pass())));
231 policy.Pass()));
232 } 193 }
233 194
234 DeviceSettingsService::OwnershipStatus 195 DeviceSettingsService::OwnershipStatus
235 DeviceSettingsService::GetOwnershipStatus() { 196 DeviceSettingsService::GetOwnershipStatus() {
236 if (public_key_.get()) 197 if (public_key_.get())
237 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; 198 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE;
238 return OWNERSHIP_UNKNOWN; 199 return OWNERSHIP_UNKNOWN;
239 } 200 }
240 201
241 void DeviceSettingsService::GetOwnershipStatusAsync( 202 void DeviceSettingsService::GetOwnershipStatusAsync(
(...skipping 27 matching lines...) Expand all
269 username_ = username; 230 username_ = username;
270 owner_settings_service_ = owner_settings_service; 231 owner_settings_service_ = owner_settings_service;
271 232
272 EnsureReload(true); 233 EnsureReload(true);
273 } 234 }
274 235
275 const std::string& DeviceSettingsService::GetUsername() const { 236 const std::string& DeviceSettingsService::GetUsername() const {
276 return username_; 237 return username_;
277 } 238 }
278 239
240 ownership::OwnerSettingsService*
241 DeviceSettingsService::GetOwnerSettingsService() const {
242 return owner_settings_service_.get();
243 }
244
279 void DeviceSettingsService::AddObserver(Observer* observer) { 245 void DeviceSettingsService::AddObserver(Observer* observer) {
280 observers_.AddObserver(observer); 246 observers_.AddObserver(observer);
281 } 247 }
282 248
283 void DeviceSettingsService::RemoveObserver(Observer* observer) { 249 void DeviceSettingsService::RemoveObserver(Observer* observer) {
284 observers_.RemoveObserver(observer); 250 observers_.RemoveObserver(observer);
285 } 251 }
286 252
287 void DeviceSettingsService::OwnerKeySet(bool success) { 253 void DeviceSettingsService::OwnerKeySet(bool success) {
288 if (!success) { 254 if (!success) {
289 LOG(ERROR) << "Owner key change failed."; 255 LOG(ERROR) << "Owner key change failed.";
290 return; 256 return;
291 } 257 }
292 258
293 public_key_ = NULL; 259 public_key_ = NULL;
294 EnsureReload(true); 260 EnsureReload(true);
295 } 261 }
296 262
297 void DeviceSettingsService::PropertyChangeComplete(bool success) { 263 void DeviceSettingsService::PropertyChangeComplete(bool success) {
298 if (!success) { 264 if (!success) {
299 LOG(ERROR) << "Policy update failed."; 265 LOG(ERROR) << "Policy update failed.";
300 return; 266 return;
301 } 267 }
302 268
303 EnsureReload(false); 269 EnsureReload(false);
304 } 270 }
305 271
306 void DeviceSettingsService::Enqueue(SessionManagerOperation* operation) { 272 void DeviceSettingsService::Enqueue(
273 const linked_ptr<SessionManagerOperation>& operation) {
307 pending_operations_.push_back(operation); 274 pending_operations_.push_back(operation);
308 if (pending_operations_.front() == operation) 275 if (pending_operations_.front().get() == operation.get())
309 StartNextOperation(); 276 StartNextOperation();
310 } 277 }
311 278
312 void DeviceSettingsService::EnqueueLoad(bool force_key_load) { 279 void DeviceSettingsService::EnqueueLoad(bool force_key_load) {
313 SessionManagerOperation* operation = 280 linked_ptr<SessionManagerOperation> operation(new LoadSettingsOperation(
314 new LoadSettingsOperation( 281 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
282 weak_factory_.GetWeakPtr(),
283 base::Closure())));
284 operation->set_force_key_load(force_key_load);
285 operation->set_username(username_);
286 operation->set_owner_settings_service(owner_settings_service_);
287 Enqueue(operation);
288 }
289
290 void DeviceSettingsService::EnqueueSignAndStore(
291 scoped_ptr<enterprise_management::PolicyData> policy,
292 const base::Closure& callback) {
293 linked_ptr<SessionManagerOperation> operation(
294 new SignAndStoreSettingsOperation(
315 base::Bind(&DeviceSettingsService::HandleCompletedOperation, 295 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
316 weak_factory_.GetWeakPtr(), 296 weak_factory_.GetWeakPtr(),
317 base::Closure())); 297 callback),
318 operation->set_force_key_load(force_key_load); 298 policy.Pass()));
319 operation->set_username(username_);
320 operation->set_owner_settings_service(owner_settings_service_); 299 operation->set_owner_settings_service(owner_settings_service_);
321 Enqueue(operation); 300 Enqueue(operation);
322 } 301 }
323 302
324 void DeviceSettingsService::EnsureReload(bool force_key_load) { 303 void DeviceSettingsService::EnsureReload(bool force_key_load) {
325 if (!pending_operations_.empty()) { 304 if (!pending_operations_.empty()) {
326 pending_operations_.front()->set_username(username_); 305 pending_operations_.front()->set_username(username_);
327 pending_operations_.front()->set_owner_settings_service( 306 pending_operations_.front()->set_owner_settings_service(
328 owner_settings_service_); 307 owner_settings_service_);
329 pending_operations_.front()->RestartLoad(force_key_load); 308 pending_operations_.front()->RestartLoad(force_key_load);
330 } else { 309 } else {
331 EnqueueLoad(force_key_load); 310 EnqueueLoad(force_key_load);
332 } 311 }
333 } 312 }
334 313
335 void DeviceSettingsService::StartNextOperation() { 314 void DeviceSettingsService::StartNextOperation() {
336 if (!pending_operations_.empty() && 315 if (!pending_operations_.empty() && session_manager_client_ &&
337 session_manager_client_ &&
338 owner_key_util_.get()) { 316 owner_key_util_.get()) {
339 pending_operations_.front()->Start( 317 pending_operations_.front()->Start(
340 session_manager_client_, owner_key_util_, public_key_); 318 session_manager_client_, owner_key_util_, public_key_);
341 } 319 }
342 } 320 }
343 321
344 void DeviceSettingsService::HandleCompletedOperation( 322 void DeviceSettingsService::HandleCompletedOperation(
345 const base::Closure& callback, 323 const base::Closure& callback,
346 SessionManagerOperation* operation, 324 SessionManagerOperation* operation,
347 Status status) { 325 Status status) {
348 DCHECK_EQ(operation, pending_operations_.front()); 326 DCHECK_EQ(operation, pending_operations_.front().get());
349 store_status_ = status; 327 store_status_ = status;
350 328
351 OwnershipStatus ownership_status = OWNERSHIP_UNKNOWN; 329 OwnershipStatus ownership_status = OWNERSHIP_UNKNOWN;
352 scoped_refptr<PublicKey> new_key(operation->public_key()); 330 scoped_refptr<PublicKey> new_key(operation->public_key());
353 if (new_key.get()) { 331 if (new_key.get()) {
354 ownership_status = new_key->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; 332 ownership_status = new_key->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE;
355 } else { 333 } else {
356 NOTREACHED() << "Failed to determine key status."; 334 NOTREACHED() << "Failed to determine key status.";
357 } 335 }
358 336
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 } 383 }
406 384
407 // The completion callback happens after the notification so clients can 385 // The completion callback happens after the notification so clients can
408 // filter self-triggered updates. 386 // filter self-triggered updates.
409 if (!callback.is_null()) 387 if (!callback.is_null())
410 callback.Run(); 388 callback.Run();
411 389
412 // Only remove the pending operation here, so new operations triggered by any 390 // Only remove the pending operation here, so new operations triggered by any
413 // of the callbacks above are queued up properly. 391 // of the callbacks above are queued up properly.
414 pending_operations_.pop_front(); 392 pending_operations_.pop_front();
415 delete operation;
416 393
417 StartNextOperation(); 394 StartNextOperation();
418 } 395 }
419 396
420 void DeviceSettingsService::HandleError(Status status, 397 void DeviceSettingsService::HandleError(Status status,
421 const base::Closure& callback) { 398 const base::Closure& callback) {
422 store_status_ = status; 399 store_status_ = status;
423 400
424 LOG(ERROR) << "Session manager operation failed: " << status; 401 LOG(ERROR) << "Session manager operation failed: " << status;
425 402
426 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated()); 403 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated());
427 404
428 // The completion callback happens after the notification so clients can 405 // The completion callback happens after the notification so clients can
429 // filter self-triggered updates. 406 // filter self-triggered updates.
430 if (!callback.is_null()) 407 if (!callback.is_null())
431 callback.Run(); 408 callback.Run();
432 } 409 }
433 410
434 void DeviceSettingsService::OnSignAndStoreOperationCompleted(Status status) {
435 store_status_ = status;
436 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated());
437 }
438
439 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() { 411 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() {
440 DeviceSettingsService::Initialize(); 412 DeviceSettingsService::Initialize();
441 } 413 }
442 414
443 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() { 415 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() {
444 // Clean pending operations. 416 // Clean pending operations.
445 DeviceSettingsService::Get()->UnsetSessionManager(); 417 DeviceSettingsService::Get()->UnsetSessionManager();
446 DeviceSettingsService::Shutdown(); 418 DeviceSettingsService::Shutdown();
447 } 419 }
448 420
449 } // namespace chromeos 421 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698