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

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: Fixed crashes under asan. Created 6 years, 1 month 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 96
126 DeviceSettingsService::DeviceSettingsService() 97 DeviceSettingsService::DeviceSettingsService()
127 : session_manager_client_(NULL), 98 : session_manager_client_(NULL),
128 store_status_(STORE_SUCCESS), 99 store_status_(STORE_SUCCESS),
129 load_retries_left_(kMaxLoadRetries), 100 load_retries_left_(kMaxLoadRetries),
130 weak_factory_(this) { 101 weak_factory_(this) {
131 } 102 }
132 103
133 DeviceSettingsService::~DeviceSettingsService() { 104 DeviceSettingsService::~DeviceSettingsService() {
134 DCHECK(pending_operations_.empty()); 105 DCHECK(pending_operations_.empty());
106 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceSettingsServiceShutdown());
135 } 107 }
136 108
137 void DeviceSettingsService::SetSessionManager( 109 void DeviceSettingsService::SetSessionManager(
138 SessionManagerClient* session_manager_client, 110 SessionManagerClient* session_manager_client,
139 scoped_refptr<OwnerKeyUtil> owner_key_util) { 111 scoped_refptr<OwnerKeyUtil> owner_key_util) {
140 DCHECK(session_manager_client); 112 DCHECK(session_manager_client);
141 DCHECK(owner_key_util.get()); 113 DCHECK(owner_key_util.get());
142 DCHECK(!session_manager_client_); 114 DCHECK(!session_manager_client_);
143 DCHECK(!owner_key_util_.get()); 115 DCHECK(!owner_key_util_.get());
144 116
145 session_manager_client_ = session_manager_client; 117 session_manager_client_ = session_manager_client;
146 owner_key_util_ = owner_key_util; 118 owner_key_util_ = owner_key_util;
147 119
148 session_manager_client_->AddObserver(this); 120 session_manager_client_->AddObserver(this);
149 121
150 StartNextOperation(); 122 StartNextOperation();
151 } 123 }
152 124
153 void DeviceSettingsService::UnsetSessionManager() { 125 void DeviceSettingsService::UnsetSessionManager() {
154 STLDeleteContainerPointers(pending_operations_.begin(),
155 pending_operations_.end());
156 pending_operations_.clear(); 126 pending_operations_.clear();
157 127
158 if (session_manager_client_) 128 if (session_manager_client_)
159 session_manager_client_->RemoveObserver(this); 129 session_manager_client_->RemoveObserver(this);
160 session_manager_client_ = NULL; 130 session_manager_client_ = NULL;
161 owner_key_util_ = NULL; 131 owner_key_util_ = NULL;
162 } 132 }
163 133
164 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() { 134 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() {
165 return public_key_; 135 return public_key_;
166 } 136 }
167 137
168 void DeviceSettingsService::Load() { 138 void DeviceSettingsService::Load() {
169 EnqueueLoad(false); 139 EnqueueLoad(false);
170 } 140 }
171 141
172 void DeviceSettingsService::SignAndStore( 142 void DeviceSettingsService::SignAndStore(
173 scoped_ptr<em::ChromeDeviceSettingsProto> new_settings, 143 scoped_ptr<em::ChromeDeviceSettingsProto> new_settings,
174 const base::Closure& callback) { 144 const base::Closure& callback) {
175 if (!owner_settings_service_) {
176 HandleError(STORE_KEY_UNAVAILABLE, callback);
177 return;
178 }
179 scoped_ptr<em::PolicyData> policy = 145 scoped_ptr<em::PolicyData> policy =
180 AssemblePolicy(GetUsername(), policy_data(), new_settings.get()); 146 OwnerSettingsServiceChromeOS::AssemblePolicy(
181 if (!policy) { 147 GetUsername(), policy_data(), new_settings.get());
182 HandleError(STORE_POLICY_ERROR, callback); 148 EnqueueSignAndStore(policy.Pass(), callback);
183 return;
184 }
185
186 owner_settings_service_->SignAndStorePolicyAsync(policy.Pass(), callback);
187 } 149 }
188 150
189 void DeviceSettingsService::SetManagementSettings( 151 void DeviceSettingsService::SetManagementSettings(
190 em::PolicyData::ManagementMode management_mode, 152 em::PolicyData::ManagementMode management_mode,
191 const std::string& request_token, 153 const std::string& request_token,
192 const std::string& device_id, 154 const std::string& device_id,
193 const base::Closure& callback) { 155 const base::Closure& callback) {
194 if (!owner_settings_service_) { 156 if (!owner_settings_service_) {
195 HandleError(STORE_KEY_UNAVAILABLE, callback); 157 HandleError(STORE_KEY_UNAVAILABLE, callback);
196 return; 158 return;
197 } 159 }
198 160
199 em::PolicyData::ManagementMode current_mode = em::PolicyData::NOT_MANAGED; 161 em::PolicyData::ManagementMode current_mode = em::PolicyData::NOT_MANAGED;
200 if (policy_data() && policy_data()->has_management_mode()) 162 if (policy_data() && policy_data()->has_management_mode())
201 current_mode = policy_data()->management_mode(); 163 current_mode = policy_data()->management_mode();
202 164
203 if (!CheckManagementModeTransition(current_mode, management_mode)) { 165 if (!CheckManagementModeTransition(current_mode, management_mode)) {
204 LOG(ERROR) << "Invalid management mode transition: current mode = " 166 LOG(ERROR) << "Invalid management mode transition: current mode = "
205 << current_mode << ", new mode = " << management_mode; 167 << current_mode << ", new mode = " << management_mode;
206 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); 168 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback);
207 return; 169 return;
208 } 170 }
209 171
210 scoped_ptr<em::PolicyData> policy = 172 scoped_ptr<em::PolicyData> policy =
211 AssemblePolicy(GetUsername(), policy_data(), device_settings()); 173 OwnerSettingsServiceChromeOS::AssemblePolicy(
174 GetUsername(), policy_data(), device_settings());
212 if (!policy) { 175 if (!policy) {
213 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); 176 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback);
214 return; 177 return;
215 } 178 }
216 179
217 policy->set_management_mode(management_mode); 180 policy->set_management_mode(management_mode);
218 policy->set_request_token(request_token); 181 policy->set_request_token(request_token);
219 policy->set_device_id(device_id); 182 policy->set_device_id(device_id);
220 183
221 owner_settings_service_->SignAndStorePolicyAsync(policy.Pass(), callback); 184 EnqueueSignAndStore(policy.Pass(), callback);
222 } 185 }
223 186
224 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy, 187 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy,
225 const base::Closure& callback) { 188 const base::Closure& callback) {
226 Enqueue( 189 Enqueue(linked_ptr<SessionManagerOperation>(new StoreSettingsOperation(
227 new StoreSettingsOperation( 190 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
228 base::Bind(&DeviceSettingsService::HandleCompletedOperation, 191 weak_factory_.GetWeakPtr(),
229 weak_factory_.GetWeakPtr(), 192 callback),
230 callback), 193 policy.Pass())));
231 policy.Pass()));
232 } 194 }
233 195
234 DeviceSettingsService::OwnershipStatus 196 DeviceSettingsService::OwnershipStatus
235 DeviceSettingsService::GetOwnershipStatus() { 197 DeviceSettingsService::GetOwnershipStatus() {
236 if (public_key_.get()) 198 if (public_key_.get())
237 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; 199 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE;
238 return OWNERSHIP_UNKNOWN; 200 return OWNERSHIP_UNKNOWN;
239 } 201 }
240 202
241 void DeviceSettingsService::GetOwnershipStatusAsync( 203 void DeviceSettingsService::GetOwnershipStatusAsync(
(...skipping 27 matching lines...) Expand all
269 username_ = username; 231 username_ = username;
270 owner_settings_service_ = owner_settings_service; 232 owner_settings_service_ = owner_settings_service;
271 233
272 EnsureReload(true); 234 EnsureReload(true);
273 } 235 }
274 236
275 const std::string& DeviceSettingsService::GetUsername() const { 237 const std::string& DeviceSettingsService::GetUsername() const {
276 return username_; 238 return username_;
277 } 239 }
278 240
241 ownership::OwnerSettingsService*
242 DeviceSettingsService::GetOwnerSettingsService() const {
243 return owner_settings_service_.get();
244 }
245
279 void DeviceSettingsService::AddObserver(Observer* observer) { 246 void DeviceSettingsService::AddObserver(Observer* observer) {
280 observers_.AddObserver(observer); 247 observers_.AddObserver(observer);
281 } 248 }
282 249
283 void DeviceSettingsService::RemoveObserver(Observer* observer) { 250 void DeviceSettingsService::RemoveObserver(Observer* observer) {
284 observers_.RemoveObserver(observer); 251 observers_.RemoveObserver(observer);
285 } 252 }
286 253
287 void DeviceSettingsService::OwnerKeySet(bool success) { 254 void DeviceSettingsService::OwnerKeySet(bool success) {
288 if (!success) { 255 if (!success) {
289 LOG(ERROR) << "Owner key change failed."; 256 LOG(ERROR) << "Owner key change failed.";
290 return; 257 return;
291 } 258 }
292 259
293 public_key_ = NULL; 260 public_key_ = NULL;
294 EnsureReload(true); 261 EnsureReload(true);
295 } 262 }
296 263
297 void DeviceSettingsService::PropertyChangeComplete(bool success) { 264 void DeviceSettingsService::PropertyChangeComplete(bool success) {
298 if (!success) { 265 if (!success) {
299 LOG(ERROR) << "Policy update failed."; 266 LOG(ERROR) << "Policy update failed.";
300 return; 267 return;
301 } 268 }
302 269
303 EnsureReload(false); 270 EnsureReload(false);
304 } 271 }
305 272
306 void DeviceSettingsService::Enqueue(SessionManagerOperation* operation) { 273 void DeviceSettingsService::Enqueue(
274 const linked_ptr<SessionManagerOperation>& operation) {
307 pending_operations_.push_back(operation); 275 pending_operations_.push_back(operation);
308 if (pending_operations_.front() == operation) 276 if (pending_operations_.front().get() == operation.get())
309 StartNextOperation(); 277 StartNextOperation();
310 } 278 }
311 279
312 void DeviceSettingsService::EnqueueLoad(bool force_key_load) { 280 void DeviceSettingsService::EnqueueLoad(bool force_key_load) {
313 SessionManagerOperation* operation = 281 linked_ptr<SessionManagerOperation> operation(new LoadSettingsOperation(
314 new LoadSettingsOperation( 282 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
283 weak_factory_.GetWeakPtr(),
284 base::Closure())));
285 operation->set_force_key_load(force_key_load);
286 operation->set_username(username_);
287 operation->set_owner_settings_service(owner_settings_service_);
288 Enqueue(operation);
289 }
290
291 void DeviceSettingsService::EnqueueSignAndStore(
292 scoped_ptr<enterprise_management::PolicyData> policy,
293 const base::Closure& callback) {
294 linked_ptr<SessionManagerOperation> operation(
295 new SignAndStoreSettingsOperation(
315 base::Bind(&DeviceSettingsService::HandleCompletedOperation, 296 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
316 weak_factory_.GetWeakPtr(), 297 weak_factory_.GetWeakPtr(),
317 base::Closure())); 298 callback),
318 operation->set_force_key_load(force_key_load); 299 policy.Pass()));
319 operation->set_username(username_);
320 operation->set_owner_settings_service(owner_settings_service_); 300 operation->set_owner_settings_service(owner_settings_service_);
321 Enqueue(operation); 301 Enqueue(operation);
322 } 302 }
323 303
324 void DeviceSettingsService::EnsureReload(bool force_key_load) { 304 void DeviceSettingsService::EnsureReload(bool force_key_load) {
325 if (!pending_operations_.empty()) { 305 if (!pending_operations_.empty()) {
326 pending_operations_.front()->set_username(username_); 306 pending_operations_.front()->set_username(username_);
327 pending_operations_.front()->set_owner_settings_service( 307 pending_operations_.front()->set_owner_settings_service(
328 owner_settings_service_); 308 owner_settings_service_);
329 pending_operations_.front()->RestartLoad(force_key_load); 309 pending_operations_.front()->RestartLoad(force_key_load);
330 } else { 310 } else {
331 EnqueueLoad(force_key_load); 311 EnqueueLoad(force_key_load);
332 } 312 }
333 } 313 }
334 314
335 void DeviceSettingsService::StartNextOperation() { 315 void DeviceSettingsService::StartNextOperation() {
336 if (!pending_operations_.empty() && 316 if (!pending_operations_.empty() && session_manager_client_ &&
337 session_manager_client_ &&
338 owner_key_util_.get()) { 317 owner_key_util_.get()) {
339 pending_operations_.front()->Start( 318 pending_operations_.front()->Start(
340 session_manager_client_, owner_key_util_, public_key_); 319 session_manager_client_, owner_key_util_, public_key_);
341 } 320 }
342 } 321 }
343 322
344 void DeviceSettingsService::HandleCompletedOperation( 323 void DeviceSettingsService::HandleCompletedOperation(
345 const base::Closure& callback, 324 const base::Closure& callback,
346 SessionManagerOperation* operation, 325 SessionManagerOperation* operation,
347 Status status) { 326 Status status) {
348 DCHECK_EQ(operation, pending_operations_.front()); 327 DCHECK_EQ(operation, pending_operations_.front().get());
349 store_status_ = status; 328 store_status_ = status;
350 329
351 OwnershipStatus ownership_status = OWNERSHIP_UNKNOWN; 330 OwnershipStatus ownership_status = OWNERSHIP_UNKNOWN;
352 scoped_refptr<PublicKey> new_key(operation->public_key()); 331 scoped_refptr<PublicKey> new_key(operation->public_key());
353 if (new_key.get()) { 332 if (new_key.get()) {
354 ownership_status = new_key->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; 333 ownership_status = new_key->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE;
355 } else { 334 } else {
356 NOTREACHED() << "Failed to determine key status."; 335 NOTREACHED() << "Failed to determine key status.";
357 } 336 }
358 337
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 } 384 }
406 385
407 // The completion callback happens after the notification so clients can 386 // The completion callback happens after the notification so clients can
408 // filter self-triggered updates. 387 // filter self-triggered updates.
409 if (!callback.is_null()) 388 if (!callback.is_null())
410 callback.Run(); 389 callback.Run();
411 390
412 // Only remove the pending operation here, so new operations triggered by any 391 // Only remove the pending operation here, so new operations triggered by any
413 // of the callbacks above are queued up properly. 392 // of the callbacks above are queued up properly.
414 pending_operations_.pop_front(); 393 pending_operations_.pop_front();
415 delete operation;
416 394
417 StartNextOperation(); 395 StartNextOperation();
418 } 396 }
419 397
420 void DeviceSettingsService::HandleError(Status status, 398 void DeviceSettingsService::HandleError(Status status,
421 const base::Closure& callback) { 399 const base::Closure& callback) {
422 store_status_ = status; 400 store_status_ = status;
423 401
424 LOG(ERROR) << "Session manager operation failed: " << status; 402 LOG(ERROR) << "Session manager operation failed: " << status;
425 403
426 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated()); 404 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated());
427 405
428 // The completion callback happens after the notification so clients can 406 // The completion callback happens after the notification so clients can
429 // filter self-triggered updates. 407 // filter self-triggered updates.
430 if (!callback.is_null()) 408 if (!callback.is_null())
431 callback.Run(); 409 callback.Run();
432 } 410 }
433 411
434 void DeviceSettingsService::OnSignAndStoreOperationCompleted(Status status) {
435 store_status_ = status;
436 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated());
437 }
438
439 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() { 412 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() {
440 DeviceSettingsService::Initialize(); 413 DeviceSettingsService::Initialize();
441 } 414 }
442 415
443 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() { 416 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() {
444 // Clean pending operations. 417 // Clean pending operations.
445 DeviceSettingsService::Get()->UnsetSessionManager(); 418 DeviceSettingsService::Get()->UnsetSessionManager();
446 DeviceSettingsService::Shutdown(); 419 DeviceSettingsService::Shutdown();
447 } 420 }
448 421
449 } // namespace chromeos 422 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698