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

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

Issue 2486813002: Add DeviceADPolicyManager to provide AD policy. (Closed)
Patch Set: Created 4 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 <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 74
75 // static 75 // static
76 DeviceSettingsService* DeviceSettingsService::Get() { 76 DeviceSettingsService* DeviceSettingsService::Get() {
77 CHECK(g_device_settings_service); 77 CHECK(g_device_settings_service);
78 return g_device_settings_service; 78 return g_device_settings_service;
79 } 79 }
80 80
81 DeviceSettingsService::DeviceSettingsService() 81 DeviceSettingsService::DeviceSettingsService()
82 : session_manager_client_(NULL), 82 : session_manager_client_(NULL),
83 store_status_(STORE_SUCCESS), 83 store_status_(STORE_SUCCESS),
84 device_mode_(policy::DEVICE_MODE_PENDING),
emaxx 2016/11/11 15:25:09 nit: Move initializers to the class definition?
Thiemo Nagel 2016/11/16 19:11:01 Done.
84 load_retries_left_(kMaxLoadRetries), 85 load_retries_left_(kMaxLoadRetries),
85 weak_factory_(this) { 86 weak_factory_(this) {}
86 }
87 87
88 DeviceSettingsService::~DeviceSettingsService() { 88 DeviceSettingsService::~DeviceSettingsService() {
89 DCHECK(pending_operations_.empty()); 89 DCHECK(pending_operations_.empty());
90 for (auto& observer : observers_) 90 for (auto& observer : observers_)
91 observer.OnDeviceSettingsServiceShutdown(); 91 observer.OnDeviceSettingsServiceShutdown();
92 } 92 }
93 93
94 void DeviceSettingsService::SetSessionManager( 94 void DeviceSettingsService::SetSessionManager(
95 SessionManagerClient* session_manager_client, 95 SessionManagerClient* session_manager_client,
96 scoped_refptr<OwnerKeyUtil> owner_key_util) { 96 scoped_refptr<OwnerKeyUtil> owner_key_util) {
(...skipping 12 matching lines...) Expand all
109 109
110 void DeviceSettingsService::UnsetSessionManager() { 110 void DeviceSettingsService::UnsetSessionManager() {
111 pending_operations_.clear(); 111 pending_operations_.clear();
112 112
113 if (session_manager_client_) 113 if (session_manager_client_)
114 session_manager_client_->RemoveObserver(this); 114 session_manager_client_->RemoveObserver(this);
115 session_manager_client_ = NULL; 115 session_manager_client_ = NULL;
116 owner_key_util_ = NULL; 116 owner_key_util_ = NULL;
117 } 117 }
118 118
119 void DeviceSettingsService::SetDeviceMode(policy::DeviceMode device_mode) {
120 device_mode_ = device_mode;
emaxx 2016/11/11 15:25:09 Can we add some DCHECK/CHECK regarding allowed dev
Thiemo Nagel 2016/11/16 19:11:01 Sounds like a good idea. Done.
121 RunPendingOwnershipStatusCallbacks();
122 }
123
119 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() { 124 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() {
120 return public_key_; 125 return public_key_;
121 } 126 }
122 127
123 void DeviceSettingsService::Load() { 128 void DeviceSettingsService::Load() {
124 EnqueueLoad(false); 129 EnqueueLoad(false);
125 } 130 }
126 131
127 void DeviceSettingsService::Store( 132 void DeviceSettingsService::Store(
128 std::unique_ptr<em::PolicyFetchResponse> policy, 133 std::unique_ptr<em::PolicyFetchResponse> policy,
129 const base::Closure& callback) { 134 const base::Closure& callback) {
130 Enqueue(linked_ptr<SessionManagerOperation>(new StoreSettingsOperation( 135 Enqueue(linked_ptr<SessionManagerOperation>(new StoreSettingsOperation(
131 base::Bind(&DeviceSettingsService::HandleCompletedOperation, 136 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
132 weak_factory_.GetWeakPtr(), callback), 137 weak_factory_.GetWeakPtr(), callback),
133 std::move(policy)))); 138 std::move(policy))));
134 } 139 }
135 140
136 DeviceSettingsService::OwnershipStatus 141 DeviceSettingsService::OwnershipStatus
137 DeviceSettingsService::GetOwnershipStatus() { 142 DeviceSettingsService::GetOwnershipStatus() {
138 if (public_key_.get()) 143 if (public_key_.get())
139 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; 144 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE;
145 if (device_mode_ == policy::DEVICE_MODE_ENTERPRISE_AD)
146 return OWNERSHIP_TAKEN;
140 return OWNERSHIP_UNKNOWN; 147 return OWNERSHIP_UNKNOWN;
141 } 148 }
142 149
143 void DeviceSettingsService::GetOwnershipStatusAsync( 150 void DeviceSettingsService::GetOwnershipStatusAsync(
144 const OwnershipStatusCallback& callback) { 151 const OwnershipStatusCallback& callback) {
145 if (public_key_.get()) { 152 if (GetOwnershipStatus() != OWNERSHIP_UNKNOWN) {
146 // If there is a key, report status immediately. 153 // Report status immediately.
147 base::ThreadTaskRunnerHandle::Get()->PostTask( 154 base::ThreadTaskRunnerHandle::Get()->PostTask(
148 FROM_HERE, base::Bind(callback, GetOwnershipStatus())); 155 FROM_HERE, base::Bind(callback, GetOwnershipStatus()));
149 } else { 156 } else {
150 // If the key hasn't been loaded yet, enqueue the callback to be fired when 157 // If the key hasn't been loaded yet, enqueue the callback to be fired when
151 // the next SessionManagerOperation completes. If no operation is pending, 158 // the next SessionManagerOperation completes. If no operation is pending,
152 // start a load operation to fetch the key and report the result. 159 // start a load operation to fetch the key and report the result.
153 pending_ownership_status_callbacks_.push_back(callback); 160 pending_ownership_status_callbacks_.push_back(callback);
154 if (pending_operations_.empty()) 161 if (pending_operations_.empty())
155 EnqueueLoad(false); 162 EnqueueLoad(false);
156 } 163 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 } 218 }
212 219
213 void DeviceSettingsService::Enqueue( 220 void DeviceSettingsService::Enqueue(
214 const linked_ptr<SessionManagerOperation>& operation) { 221 const linked_ptr<SessionManagerOperation>& operation) {
215 pending_operations_.push_back(operation); 222 pending_operations_.push_back(operation);
216 if (pending_operations_.front().get() == operation.get()) 223 if (pending_operations_.front().get() == operation.get())
217 StartNextOperation(); 224 StartNextOperation();
218 } 225 }
219 226
220 void DeviceSettingsService::EnqueueLoad(bool force_key_load) { 227 void DeviceSettingsService::EnqueueLoad(bool force_key_load) {
228 bool verify_signature = true;
229 if (device_mode_ == policy::DEVICE_MODE_ENTERPRISE_AD) {
230 force_key_load = false;
emaxx 2016/11/11 15:25:08 That's a bit unexpected that the "force" value is
Thiemo Nagel 2016/11/16 19:11:01 I've renamed to "request_key_load" since there are
231 verify_signature = false;
232 }
221 linked_ptr<SessionManagerOperation> operation(new LoadSettingsOperation( 233 linked_ptr<SessionManagerOperation> operation(new LoadSettingsOperation(
234 force_key_load, verify_signature,
222 base::Bind(&DeviceSettingsService::HandleCompletedOperation, 235 base::Bind(&DeviceSettingsService::HandleCompletedOperation,
223 weak_factory_.GetWeakPtr(), 236 weak_factory_.GetWeakPtr(), base::Closure())));
224 base::Closure())));
225 operation->set_force_key_load(force_key_load);
226 Enqueue(operation); 237 Enqueue(operation);
227 } 238 }
228 239
229 void DeviceSettingsService::EnsureReload(bool force_key_load) { 240 void DeviceSettingsService::EnsureReload(bool force_key_load) {
230 if (!pending_operations_.empty()) 241 if (!pending_operations_.empty())
231 pending_operations_.front()->RestartLoad(force_key_load); 242 pending_operations_.front()->RestartLoad(force_key_load);
232 else 243 else
233 EnqueueLoad(force_key_load); 244 EnqueueLoad(force_key_load);
234 } 245 }
235 246
236 void DeviceSettingsService::StartNextOperation() { 247 void DeviceSettingsService::StartNextOperation() {
237 if (!pending_operations_.empty() && session_manager_client_ && 248 if (!pending_operations_.empty() && session_manager_client_ &&
238 owner_key_util_.get()) { 249 owner_key_util_.get()) {
239 pending_operations_.front()->Start( 250 pending_operations_.front()->Start(
240 session_manager_client_, owner_key_util_, public_key_); 251 session_manager_client_, owner_key_util_, public_key_);
241 } 252 }
242 } 253 }
243 254
244 void DeviceSettingsService::HandleCompletedOperation( 255 void DeviceSettingsService::HandleCompletedOperation(
245 const base::Closure& callback, 256 const base::Closure& callback,
246 SessionManagerOperation* operation, 257 SessionManagerOperation* operation,
247 Status status) { 258 Status status) {
248 DCHECK_EQ(operation, pending_operations_.front().get()); 259 DCHECK_EQ(operation, pending_operations_.front().get());
249 store_status_ = status; 260 store_status_ = status;
250 261
251 OwnershipStatus ownership_status = OWNERSHIP_UNKNOWN;
252 scoped_refptr<PublicKey> new_key(operation->public_key()); 262 scoped_refptr<PublicKey> new_key(operation->public_key());
253 if (new_key.get()) {
emaxx 2016/11/11 15:25:08 Are these checks for new_key.get() and new_key->is
Thiemo Nagel 2016/11/16 19:11:01 RunPendingOwnershipStatusCallbacks() below calls G
254 ownership_status = new_key->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE;
255 } else {
256 NOTREACHED() << "Failed to determine key status.";
257 }
258
259 bool new_owner_key = false; 263 bool new_owner_key = false;
260 if (public_key_.get() != new_key.get()) { 264 if (public_key_.get() != new_key.get()) {
261 public_key_ = new_key; 265 public_key_ = new_key;
262 new_owner_key = true; 266 new_owner_key = true;
263 } 267 }
264 268
265 if (status == STORE_SUCCESS) { 269 if (status == STORE_SUCCESS) {
266 policy_data_ = std::move(operation->policy_data()); 270 policy_data_ = std::move(operation->policy_data());
267 device_settings_ = std::move(operation->device_settings()); 271 device_settings_ = std::move(operation->device_settings());
268 load_retries_left_ = kMaxLoadRetries; 272 load_retries_left_ = kMaxLoadRetries;
(...skipping 23 matching lines...) Expand all
292 observer.OwnershipStatusChanged(); 296 observer.OwnershipStatusChanged();
293 content::NotificationService::current()->Notify( 297 content::NotificationService::current()->Notify(
294 chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED, 298 chrome::NOTIFICATION_OWNERSHIP_STATUS_CHANGED,
295 content::Source<DeviceSettingsService>(this), 299 content::Source<DeviceSettingsService>(this),
296 content::NotificationService::NoDetails()); 300 content::NotificationService::NoDetails());
297 } 301 }
298 302
299 for (auto& observer : observers_) 303 for (auto& observer : observers_)
300 observer.DeviceSettingsUpdated(); 304 observer.DeviceSettingsUpdated();
301 305
302 std::vector<OwnershipStatusCallback> callbacks; 306 RunPendingOwnershipStatusCallbacks();
303 callbacks.swap(pending_ownership_status_callbacks_);
304 for (std::vector<OwnershipStatusCallback>::iterator iter(callbacks.begin());
305 iter != callbacks.end(); ++iter) {
306 iter->Run(ownership_status);
307 }
308 307
309 // The completion callback happens after the notification so clients can 308 // The completion callback happens after the notification so clients can
310 // filter self-triggered updates. 309 // filter self-triggered updates.
311 if (!callback.is_null()) 310 if (!callback.is_null())
312 callback.Run(); 311 callback.Run();
313 312
314 // Only remove the pending operation here, so new operations triggered by any 313 // Only remove the pending operation here, so new operations triggered by any
315 // of the callbacks above are queued up properly. 314 // of the callbacks above are queued up properly.
316 pending_operations_.pop_front(); 315 pending_operations_.pop_front();
317 316
318 StartNextOperation(); 317 StartNextOperation();
319 } 318 }
320 319
321 void DeviceSettingsService::HandleError(Status status, 320 void DeviceSettingsService::HandleError(Status status,
322 const base::Closure& callback) { 321 const base::Closure& callback) {
323 store_status_ = status; 322 store_status_ = status;
324 323
325 LOG(ERROR) << "Session manager operation failed: " << status; 324 LOG(ERROR) << "Session manager operation failed: " << status;
326 325
327 for (auto& observer : observers_) 326 for (auto& observer : observers_)
328 observer.DeviceSettingsUpdated(); 327 observer.DeviceSettingsUpdated();
329 328
330 // The completion callback happens after the notification so clients can 329 // The completion callback happens after the notification so clients can
331 // filter self-triggered updates. 330 // filter self-triggered updates.
332 if (!callback.is_null()) 331 if (!callback.is_null())
333 callback.Run(); 332 callback.Run();
334 } 333 }
335 334
335 void DeviceSettingsService::RunPendingOwnershipStatusCallbacks() {
336 std::vector<OwnershipStatusCallback> callbacks;
337 callbacks.swap(pending_ownership_status_callbacks_);
338 for (auto& callback : callbacks) {
emaxx 2016/11/11 15:25:09 const auto& should be probably enough.
Thiemo Nagel 2016/11/16 19:11:01 Done.
339 callback.Run(GetOwnershipStatus());
340 }
341 }
342
336 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() { 343 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() {
337 DeviceSettingsService::Initialize(); 344 DeviceSettingsService::Initialize();
338 } 345 }
339 346
340 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() { 347 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() {
341 // Clean pending operations. 348 // Clean pending operations.
342 DeviceSettingsService::Get()->UnsetSessionManager(); 349 DeviceSettingsService::Get()->UnsetSessionManager();
343 DeviceSettingsService::Shutdown(); 350 DeviceSettingsService::Shutdown();
344 } 351 }
345 352
346 } // namespace chromeos 353 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698