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

Side by Side Diff: chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.cc

Issue 2954293002: Chromad: Prevent session from starting without policy (Closed)
Patch Set: Move MockAuthPolicyClient into unittest Created 3 years, 5 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/policy/user_cloud_policy_manager_chromeos.h" 5 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
6 6
7 #include <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 chrome::AttemptUserExit(); 80 chrome::AttemptUserExit();
81 } 81 }
82 } 82 }
83 83
84 } // namespace 84 } // namespace
85 85
86 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS( 86 UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS(
87 std::unique_ptr<CloudPolicyStore> store, 87 std::unique_ptr<CloudPolicyStore> store,
88 std::unique_ptr<CloudExternalDataManager> external_data_manager, 88 std::unique_ptr<CloudExternalDataManager> external_data_manager,
89 const base::FilePath& component_policy_cache_path, 89 const base::FilePath& component_policy_cache_path,
90 bool wait_for_policy_fetch,
91 base::TimeDelta initial_policy_fetch_timeout, 90 base::TimeDelta initial_policy_fetch_timeout,
92 const scoped_refptr<base::SequencedTaskRunner>& task_runner, 91 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
93 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, 92 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
94 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) 93 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
95 : CloudPolicyManager(dm_protocol::kChromeUserPolicyType, 94 : CloudPolicyManager(dm_protocol::kChromeUserPolicyType,
96 std::string(), 95 std::string(),
97 store.get(), 96 store.get(),
98 task_runner, 97 task_runner,
99 file_task_runner, 98 file_task_runner,
100 io_task_runner), 99 io_task_runner),
101 store_(std::move(store)), 100 store_(std::move(store)),
102 external_data_manager_(std::move(external_data_manager)), 101 external_data_manager_(std::move(external_data_manager)),
103 component_policy_cache_path_(component_policy_cache_path), 102 component_policy_cache_path_(component_policy_cache_path),
104 wait_for_policy_fetch_(wait_for_policy_fetch) { 103 waiting_for_initial_policy_fetch_(
104 !initial_policy_fetch_timeout.is_zero()) {
105 time_init_started_ = base::Time::Now(); 105 time_init_started_ = base::Time::Now();
106 106
107 // Caller must pass a non-zero policy_fetch_timeout iff 107 initial_policy_fetch_may_fail_ =
108 // |wait_for_policy_fetch| is true.
109 DCHECK_NE(wait_for_policy_fetch_, initial_policy_fetch_timeout.is_zero());
110 allow_failed_policy_fetches_ =
111 base::CommandLine::ForCurrentProcess()->HasSwitch( 108 base::CommandLine::ForCurrentProcess()->HasSwitch(
112 chromeos::switches::kAllowFailedPolicyFetchForTest) || 109 chromeos::switches::kAllowFailedPolicyFetchForTest) ||
113 !initial_policy_fetch_timeout.is_max(); 110 !initial_policy_fetch_timeout.is_max();
114 // No need to set the timer when the timeout is infinite. 111 // No need to set the timer when the timeout is infinite.
115 if (wait_for_policy_fetch_ && !initial_policy_fetch_timeout.is_max()) { 112 if (waiting_for_initial_policy_fetch_ && initial_policy_fetch_may_fail_) {
116 policy_fetch_timeout_.Start( 113 policy_fetch_timeout_.Start(
117 FROM_HERE, 114 FROM_HERE,
118 initial_policy_fetch_timeout, 115 initial_policy_fetch_timeout,
119 base::Bind(&UserCloudPolicyManagerChromeOS::OnBlockingFetchTimeout, 116 base::Bind(&UserCloudPolicyManagerChromeOS::OnBlockingFetchTimeout,
120 base::Unretained(this))); 117 base::Unretained(this)));
121 } 118 }
122 } 119 }
123 120
124 void UserCloudPolicyManagerChromeOS::ForceTimeoutForTest() { 121 void UserCloudPolicyManagerChromeOS::ForceTimeoutForTest() {
125 DCHECK(policy_fetch_timeout_.IsRunning()); 122 DCHECK(policy_fetch_timeout_.IsRunning());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 external_data_manager_->Connect(system_request_context); 164 external_data_manager_->Connect(system_request_context);
168 165
169 // Determine the next step after the CloudPolicyService initializes. 166 // Determine the next step after the CloudPolicyService initializes.
170 if (service()->IsInitializationComplete()) { 167 if (service()->IsInitializationComplete()) {
171 OnInitializationCompleted(service()); 168 OnInitializationCompleted(service());
172 169
173 // The cloud policy client may be already registered by this point if the 170 // The cloud policy client may be already registered by this point if the
174 // store has already been loaded and contains a valid policy - the 171 // store has already been loaded and contains a valid policy - the
175 // registration setup in this case is performed by the CloudPolicyService 172 // registration setup in this case is performed by the CloudPolicyService
176 // that is instantiated inside the CloudPolicyCore::Connect() method call. 173 // that is instantiated inside the CloudPolicyCore::Connect() method call.
177 // If that's the case and |wait_for_policy_fetch_| is true, then the policy 174 // If that's the case and |waiting_for_initial_policy_fetch_| is true, then
178 // fetch needs to be issued (it happens otherwise after the client 175 // the policy fetch needs to be issued (it happens otherwise after the
179 // registration is finished, in OnRegistrationStateChanged()). 176 // client registration is finished, in OnRegistrationStateChanged()).
180 if (client()->is_registered() && wait_for_policy_fetch_) { 177 if (client()->is_registered() && waiting_for_initial_policy_fetch_) {
181 service()->RefreshPolicy( 178 service()->RefreshPolicy(
182 base::Bind(&UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch, 179 base::Bind(&UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch,
183 base::Unretained(this))); 180 base::Unretained(this)));
184 } 181 }
185 } else { 182 } else {
186 service()->AddObserver(this); 183 service()->AddObserver(this);
187 } 184 }
188 } 185 }
189 186
190 void UserCloudPolicyManagerChromeOS::OnAccessTokenAvailable( 187 void UserCloudPolicyManagerChromeOS::OnAccessTokenAvailable(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 token_fetcher_.reset(); 220 token_fetcher_.reset();
224 external_data_manager_->Disconnect(); 221 external_data_manager_->Disconnect();
225 CloudPolicyManager::Shutdown(); 222 CloudPolicyManager::Shutdown();
226 } 223 }
227 224
228 bool UserCloudPolicyManagerChromeOS::IsInitializationComplete( 225 bool UserCloudPolicyManagerChromeOS::IsInitializationComplete(
229 PolicyDomain domain) const { 226 PolicyDomain domain) const {
230 if (!CloudPolicyManager::IsInitializationComplete(domain)) 227 if (!CloudPolicyManager::IsInitializationComplete(domain))
231 return false; 228 return false;
232 if (domain == POLICY_DOMAIN_CHROME) 229 if (domain == POLICY_DOMAIN_CHROME)
233 return !wait_for_policy_fetch_; 230 return !waiting_for_initial_policy_fetch_;
234 return true; 231 return true;
235 } 232 }
236 233
237 void UserCloudPolicyManagerChromeOS::OnInitializationCompleted( 234 void UserCloudPolicyManagerChromeOS::OnInitializationCompleted(
238 CloudPolicyService* cloud_policy_service) { 235 CloudPolicyService* cloud_policy_service) {
239 DCHECK_EQ(service(), cloud_policy_service); 236 DCHECK_EQ(service(), cloud_policy_service);
240 cloud_policy_service->RemoveObserver(this); 237 cloud_policy_service->RemoveObserver(this);
241 238
242 time_init_completed_ = base::Time::Now(); 239 time_init_completed_ = base::Time::Now();
243 UMA_HISTOGRAM_MEDIUM_TIMES(kUMADelayInitialization, 240 UMA_HISTOGRAM_MEDIUM_TIMES(kUMADelayInitialization,
244 time_init_completed_ - time_init_started_); 241 time_init_completed_ - time_init_started_);
245 242
246 // If the CloudPolicyClient isn't registered at this stage then it needs an 243 // If the CloudPolicyClient isn't registered at this stage then it needs an
247 // OAuth token for the initial registration. 244 // OAuth token for the initial registration.
248 // 245 //
249 // If |wait_for_policy_fetch_| is true then Profile initialization is blocking 246 // If |waiting_for_initial_policy_fetch_| is true then Profile initialization
250 // on the initial policy fetch, so the token must be fetched immediately. 247 // is blocking on the initial policy fetch, so the token must be fetched
251 // In that case, the signin Profile is used to authenticate a Gaia request to 248 // immediately. In that case, the signin Profile is used to authenticate a
252 // fetch a refresh token, and then the policy token is fetched. 249 // Gaia request to fetch a refresh token, and then the policy token is
250 // fetched.
253 // 251 //
254 // If |wait_for_policy_fetch_| is false then the UserCloudPolicyTokenForwarder 252 // If |waiting_for_initial_policy_fetch_| is false then the
255 // service will eventually call OnAccessTokenAvailable() once an access token 253 // UserCloudPolicyTokenForwarder service will eventually call
256 // is available. That call may have already happened while waiting for 254 // OnAccessTokenAvailable() once an access token is available. That call may
257 // initialization of the CloudPolicyService, so in that case check if an 255 // have already happened while waiting for initialization of the
258 // access token is already available. 256 // CloudPolicyService, so in that case check if an access token is already
257 // available.
259 if (!client()->is_registered()) { 258 if (!client()->is_registered()) {
260 if (wait_for_policy_fetch_) { 259 if (waiting_for_initial_policy_fetch_) {
261 FetchPolicyOAuthToken(); 260 FetchPolicyOAuthToken();
262 } else if (!access_token_.empty()) { 261 } else if (!access_token_.empty()) {
263 OnAccessTokenAvailable(access_token_); 262 OnAccessTokenAvailable(access_token_);
264 } 263 }
265 } 264 }
266 265
267 if (!wait_for_policy_fetch_) { 266 if (!waiting_for_initial_policy_fetch_) {
268 // If this isn't blocking on a policy fetch then 267 // If this isn't blocking on a policy fetch then
269 // CloudPolicyManager::OnStoreLoaded() already published the cached policy. 268 // CloudPolicyManager::OnStoreLoaded() already published the cached policy.
270 // Start the refresh scheduler now, which will eventually refresh the 269 // Start the refresh scheduler now, which will eventually refresh the
271 // cached policy or make the first fetch once the OAuth2 token is 270 // cached policy or make the first fetch once the OAuth2 token is
272 // available. 271 // available.
273 StartRefreshSchedulerIfReady(); 272 StartRefreshSchedulerIfReady();
274 } 273 }
275 } 274 }
276 275
277 void UserCloudPolicyManagerChromeOS::OnPolicyFetched( 276 void UserCloudPolicyManagerChromeOS::OnPolicyFetched(
278 CloudPolicyClient* client) { 277 CloudPolicyClient* client) {
279 // No action required. If we're blocked on a policy fetch, we'll learn about 278 // No action required. If we're blocked on a policy fetch, we'll learn about
280 // completion of it through OnInitialPolicyFetchComplete(), or through the 279 // completion of it through OnInitialPolicyFetchComplete(), or through the
281 // CancelWaitForPolicyFetch() callback. 280 // CancelWaitForPolicyFetch() callback.
282 } 281 }
283 282
284 void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged( 283 void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged(
285 CloudPolicyClient* cloud_policy_client) { 284 CloudPolicyClient* cloud_policy_client) {
286 DCHECK_EQ(client(), cloud_policy_client); 285 DCHECK_EQ(client(), cloud_policy_client);
287 286
288 if (wait_for_policy_fetch_) { 287 if (waiting_for_initial_policy_fetch_) {
289 time_client_registered_ = base::Time::Now(); 288 time_client_registered_ = base::Time::Now();
290 if (!time_token_available_.is_null()) { 289 if (!time_token_available_.is_null()) {
291 UMA_HISTOGRAM_MEDIUM_TIMES( 290 UMA_HISTOGRAM_MEDIUM_TIMES(
292 kUMAInitialFetchDelayClientRegister, 291 kUMAInitialFetchDelayClientRegister,
293 time_client_registered_ - time_token_available_); 292 time_client_registered_ - time_token_available_);
294 } 293 }
295 294
296 // If we're blocked on the policy fetch, now is a good time to issue it. 295 // If we're blocked on the policy fetch, now is a good time to issue it.
297 if (client()->is_registered()) { 296 if (client()->is_registered()) {
298 service()->RefreshPolicy( 297 service()->RefreshPolicy(
299 base::Bind( 298 base::Bind(
300 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, 299 &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete,
301 base::Unretained(this))); 300 base::Unretained(this)));
302 } else { 301 } else {
303 // If the client has switched to not registered, we bail out as this 302 // If the client has switched to not registered, we bail out as this
304 // indicates the cloud policy setup flow has been aborted. 303 // indicates the cloud policy setup flow has been aborted.
305 CancelWaitForPolicyFetch(true); 304 CancelWaitForPolicyFetch(true);
306 } 305 }
307 } 306 }
308 } 307 }
309 308
310 void UserCloudPolicyManagerChromeOS::OnClientError( 309 void UserCloudPolicyManagerChromeOS::OnClientError(
311 CloudPolicyClient* cloud_policy_client) { 310 CloudPolicyClient* cloud_policy_client) {
312 DCHECK_EQ(client(), cloud_policy_client); 311 DCHECK_EQ(client(), cloud_policy_client);
313 if (wait_for_policy_fetch_) { 312 if (waiting_for_initial_policy_fetch_) {
314 UMA_HISTOGRAM_SPARSE_SLOWLY(kUMAInitialFetchClientError, 313 UMA_HISTOGRAM_SPARSE_SLOWLY(kUMAInitialFetchClientError,
315 cloud_policy_client->status()); 314 cloud_policy_client->status());
316 } 315 }
317 switch (client()->status()) { 316 switch (client()->status()) {
318 case DM_STATUS_SUCCESS: 317 case DM_STATUS_SUCCESS:
319 case DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED: 318 case DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED:
320 // If management is not supported for this user, then a registration 319 // If management is not supported for this user, then a registration
321 // error is to be expected. 320 // error is to be expected.
322 CancelWaitForPolicyFetch(true); 321 CancelWaitForPolicyFetch(true);
323 break; 322 break;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 signin_context.get(), g_browser_process->system_request_context(), 396 signin_context.get(), g_browser_process->system_request_context(),
398 base::Bind(&UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched, 397 base::Bind(&UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched,
399 base::Unretained(this))); 398 base::Unretained(this)));
400 } 399 }
401 400
402 void UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched( 401 void UserCloudPolicyManagerChromeOS::OnOAuth2PolicyTokenFetched(
403 const std::string& policy_token, 402 const std::string& policy_token,
404 const GoogleServiceAuthError& error) { 403 const GoogleServiceAuthError& error) {
405 DCHECK(!client()->is_registered()); 404 DCHECK(!client()->is_registered());
406 time_token_available_ = base::Time::Now(); 405 time_token_available_ = base::Time::Now();
407 if (wait_for_policy_fetch_) { 406 if (waiting_for_initial_policy_fetch_) {
408 UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayOAuth2Token, 407 UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayOAuth2Token,
409 time_token_available_ - time_init_completed_); 408 time_token_available_ - time_init_completed_);
410 } 409 }
411 410
412 if (error.state() == GoogleServiceAuthError::NONE) { 411 if (error.state() == GoogleServiceAuthError::NONE) {
413 // Start client registration. Either OnRegistrationStateChanged() or 412 // Start client registration. Either OnRegistrationStateChanged() or
414 // OnClientError() will be called back. 413 // OnClientError() will be called back.
415 client()->Register(em::DeviceRegisterRequest::USER, 414 client()->Register(em::DeviceRegisterRequest::USER,
416 em::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION, 415 em::DeviceRegisterRequest::FLAVOR_USER_REGISTRATION,
417 policy_token, std::string(), std::string(), 416 policy_token, std::string(), std::string(),
(...skipping 20 matching lines...) Expand all
438 bool success) { 437 bool success) {
439 const base::Time now = base::Time::Now(); 438 const base::Time now = base::Time::Now();
440 UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayPolicyFetch, 439 UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayPolicyFetch,
441 now - time_client_registered_); 440 now - time_client_registered_);
442 UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayTotal, 441 UMA_HISTOGRAM_MEDIUM_TIMES(kUMAInitialFetchDelayTotal,
443 now - time_init_started_); 442 now - time_init_started_);
444 CancelWaitForPolicyFetch(success); 443 CancelWaitForPolicyFetch(success);
445 } 444 }
446 445
447 void UserCloudPolicyManagerChromeOS::OnBlockingFetchTimeout() { 446 void UserCloudPolicyManagerChromeOS::OnBlockingFetchTimeout() {
448 DCHECK(wait_for_policy_fetch_); 447 DCHECK(waiting_for_initial_policy_fetch_);
449 LOG(WARNING) << "Timed out while waiting for the policy fetch. " 448 LOG(WARNING) << "Timed out while waiting for the policy fetch. "
450 << "The session will start with the cached policy."; 449 << "The session will start with the cached policy.";
451 CancelWaitForPolicyFetch(false); 450 CancelWaitForPolicyFetch(false);
452 } 451 }
453 452
454 void UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch(bool success) { 453 void UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch(bool success) {
455 if (!wait_for_policy_fetch_) 454 if (!waiting_for_initial_policy_fetch_)
456 return; 455 return;
457 456
458 policy_fetch_timeout_.Stop(); 457 policy_fetch_timeout_.Stop();
459 458
460 // If there was an error, and we don't want to allow profile initialization 459 // If there was an error, and we don't want to allow profile initialization
461 // to go forward after a failed policy fetch, then just return (profile 460 // to go forward after a failed policy fetch, then just return (profile
462 // initialization will not complete). 461 // initialization will not complete).
463 // TODO(atwilson): Add code to retry policy fetching. 462 // TODO(atwilson): Add code to retry policy fetching.
464 if (!success && !allow_failed_policy_fetches_) { 463 if (!success && !initial_policy_fetch_may_fail_) {
465 LOG(ERROR) << "Policy fetch failed for the user. " 464 LOG(ERROR) << "Policy fetch failed for the user. "
466 "Aborting profile initialization"; 465 "Aborting profile initialization";
467 // Need to exit the current user, because we've already started this user's 466 // Need to exit the current user, because we've already started this user's
468 // session. 467 // session.
469 chrome::AttemptUserExit(); 468 chrome::AttemptUserExit();
470 return; 469 return;
471 } 470 }
472 471
473 wait_for_policy_fetch_ = false; 472 waiting_for_initial_policy_fetch_ = false;
473
474 CheckAndPublishPolicy(); 474 CheckAndPublishPolicy();
475 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler 475 // Now that |waiting_for_initial_policy_fetch_| is guaranteed to be false, the
476 // can be started. 476 // scheduler can be started.
477 StartRefreshSchedulerIfReady(); 477 StartRefreshSchedulerIfReady();
478 } 478 }
479 479
480 void UserCloudPolicyManagerChromeOS::StartRefreshSchedulerIfReady() { 480 void UserCloudPolicyManagerChromeOS::StartRefreshSchedulerIfReady() {
481 if (core()->refresh_scheduler()) 481 if (core()->refresh_scheduler())
482 return; // Already started. 482 return; // Already started.
483 483
484 if (wait_for_policy_fetch_) 484 if (waiting_for_initial_policy_fetch_)
485 return; // Still waiting for the initial, blocking fetch. 485 return; // Still waiting for the initial, blocking fetch.
486 486
487 if (!service() || !local_state_) 487 if (!service() || !local_state_)
488 return; // Not connected. 488 return; // Not connected.
489 489
490 if (component_policy_service() && 490 if (component_policy_service() &&
491 !component_policy_service()->is_initialized()) { 491 !component_policy_service()->is_initialized()) {
492 // If the client doesn't have the list of components to fetch yet then don't 492 // If the client doesn't have the list of components to fetch yet then don't
493 // start the scheduler. The |component_policy_service_| will call back into 493 // start the scheduler. The |component_policy_service_| will call back into
494 // OnComponentCloudPolicyUpdated() once it's ready. 494 // OnComponentCloudPolicyUpdated() once it's ready.
495 return; 495 return;
496 } 496 }
497 497
498 core()->StartRefreshScheduler(); 498 core()->StartRefreshScheduler();
499 core()->TrackRefreshDelayPref(local_state_, 499 core()->TrackRefreshDelayPref(local_state_,
500 policy_prefs::kUserPolicyRefreshRate); 500 policy_prefs::kUserPolicyRefreshRate);
501 } 501 }
502 502
503 } // namespace policy 503 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698