OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/arc/arc_auth_service.h" | 5 #include "chrome/browser/chromeos/arc/arc_auth_service.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 | 123 |
124 const std::string auth_code = GetAndResetAuthCode(); | 124 const std::string auth_code = GetAndResetAuthCode(); |
125 const bool verification_disabled = IsOptInVerificationDisabled(); | 125 const bool verification_disabled = IsOptInVerificationDisabled(); |
126 if (!auth_code.empty() || verification_disabled) { | 126 if (!auth_code.empty() || verification_disabled) { |
127 callback.Run(mojo::String(auth_code), !verification_disabled); | 127 callback.Run(mojo::String(auth_code), !verification_disabled); |
128 return; | 128 return; |
129 } | 129 } |
130 | 130 |
131 initial_opt_in_ = false; | 131 initial_opt_in_ = false; |
132 auth_callback_ = callback; | 132 auth_callback_ = callback; |
133 SetState(State::FETCHING_CODE); | 133 StartUI(); |
134 FetchAuthCode(); | |
135 } | 134 } |
136 | 135 |
137 void ArcAuthService::OnSignInComplete() { | 136 void ArcAuthService::OnSignInComplete() { |
138 DCHECK(thread_checker_.CalledOnValidThread()); | 137 DCHECK(thread_checker_.CalledOnValidThread()); |
139 DCHECK_EQ(state_, State::ACTIVE); | 138 DCHECK_EQ(state_, State::ACTIVE); |
140 | 139 |
141 profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); | 140 profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); |
142 CloseUI(); | 141 CloseUI(); |
143 } | 142 } |
144 | 143 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 286 |
288 DCHECK(!initial_opt_in_); | 287 DCHECK(!initial_opt_in_); |
289 context_prepared_ = true; | 288 context_prepared_ = true; |
290 ShowUI(UIPage::LSO_PROGRESS, base::string16()); | 289 ShowUI(UIPage::LSO_PROGRESS, base::string16()); |
291 } | 290 } |
292 | 291 |
293 void ArcAuthService::OnMergeSessionFailure( | 292 void ArcAuthService::OnMergeSessionFailure( |
294 const GoogleServiceAuthError& error) { | 293 const GoogleServiceAuthError& error) { |
295 DCHECK(thread_checker_.CalledOnValidThread()); | 294 DCHECK(thread_checker_.CalledOnValidThread()); |
296 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; | 295 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; |
297 OnAuthCodeFailed(); | 296 OnPrepareContextFailed(); |
298 } | 297 } |
299 | 298 |
300 void ArcAuthService::OnUbertokenSuccess(const std::string& token) { | 299 void ArcAuthService::OnUbertokenSuccess(const std::string& token) { |
301 DCHECK(thread_checker_.CalledOnValidThread()); | 300 DCHECK(thread_checker_.CalledOnValidThread()); |
302 merger_fetcher_.reset( | 301 merger_fetcher_.reset( |
303 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, | 302 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, |
304 storage_partition_->GetURLRequestContext())); | 303 storage_partition_->GetURLRequestContext())); |
305 merger_fetcher_->StartMergeSession(token, std::string()); | 304 merger_fetcher_->StartMergeSession(token, std::string()); |
306 } | 305 } |
307 | 306 |
308 void ArcAuthService::OnUbertokenFailure(const GoogleServiceAuthError& error) { | 307 void ArcAuthService::OnUbertokenFailure(const GoogleServiceAuthError& error) { |
309 DCHECK(thread_checker_.CalledOnValidThread()); | 308 DCHECK(thread_checker_.CalledOnValidThread()); |
310 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; | 309 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; |
311 OnAuthCodeFailed(); | 310 OnPrepareContextFailed(); |
312 } | 311 } |
313 | 312 |
314 void ArcAuthService::OnSyncedPrefChanged(const std::string& path, | 313 void ArcAuthService::OnSyncedPrefChanged(const std::string& path, |
315 bool from_sync) { | 314 bool from_sync) { |
316 DCHECK(thread_checker_.CalledOnValidThread()); | 315 DCHECK(thread_checker_.CalledOnValidThread()); |
317 | 316 |
318 // Update UMA only for local changes | 317 // Update UMA only for local changes |
319 if (!from_sync) { | 318 if (!from_sync) { |
320 UpdateOptInActionUMA(profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled) | 319 UpdateOptInActionUMA(profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled) |
321 ? OptInActionType::OPTED_IN | 320 ? OptInActionType::OPTED_IN |
322 : OptInActionType::OPTED_OUT); | 321 : OptInActionType::OPTED_OUT); |
323 } | 322 } |
324 | 323 |
325 OnOptInPreferenceChanged(); | 324 OnOptInPreferenceChanged(); |
326 } | 325 } |
327 | 326 |
328 void ArcAuthService::OnOptInPreferenceChanged() { | 327 void ArcAuthService::OnOptInPreferenceChanged() { |
329 DCHECK(thread_checker_.CalledOnValidThread()); | 328 DCHECK(thread_checker_.CalledOnValidThread()); |
330 DCHECK(profile_); | 329 DCHECK(profile_); |
331 | 330 |
332 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { | 331 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { |
333 if (state_ != State::ACTIVE) { | 332 if (state_ != State::ACTIVE) { |
334 CloseUI(); | 333 CloseUI(); |
335 auth_code_.clear(); | 334 auth_code_.clear(); |
336 | 335 |
337 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) { | 336 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) { |
338 // Need pre-fetch auth code and show OptIn UI if needed. | 337 // Need pre-fetch auth code and show OptIn UI if needed. |
339 initial_opt_in_ = true; | 338 initial_opt_in_ = true; |
340 SetState(State::FETCHING_CODE); | 339 StartUI(); |
341 FetchAuthCode(); | |
342 } else { | 340 } else { |
343 // Ready to start Arc. | 341 // Ready to start Arc. |
344 StartArc(); | 342 StartArc(); |
345 } | 343 } |
346 | 344 |
347 UpdateEnabledStateUMA(true); | 345 UpdateEnabledStateUMA(true); |
348 } | 346 } |
349 } else { | 347 } else { |
350 if (state_ != State::STOPPED) | 348 if (state_ != State::STOPPED) |
351 UpdateEnabledStateUMA(false); | 349 UpdateEnabledStateUMA(false); |
352 ShutdownBridgeAndCloseUI(); | 350 ShutdownBridgeAndCloseUI(); |
353 } | 351 } |
354 } | 352 } |
355 | 353 |
356 void ArcAuthService::ShutdownBridge() { | 354 void ArcAuthService::ShutdownBridge() { |
357 auth_callback_.reset(); | 355 auth_callback_.reset(); |
358 auth_fetcher_.reset(); | |
359 ubertoken_fethcher_.reset(); | 356 ubertoken_fethcher_.reset(); |
360 merger_fetcher_.reset(); | 357 merger_fetcher_.reset(); |
361 ArcBridgeService::Get()->Shutdown(); | 358 ArcBridgeService::Get()->Shutdown(); |
362 SetState(State::STOPPED); | 359 SetState(State::STOPPED); |
363 } | 360 } |
364 | 361 |
365 void ArcAuthService::ShutdownBridgeAndCloseUI() { | 362 void ArcAuthService::ShutdownBridgeAndCloseUI() { |
366 ShutdownBridge(); | 363 ShutdownBridge(); |
367 CloseUI(); | 364 CloseUI(); |
368 } | 365 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 ShutdownBridgeAndCloseUI(); | 414 ShutdownBridgeAndCloseUI(); |
418 return; | 415 return; |
419 } | 416 } |
420 | 417 |
421 SetUIPage(UIPage::START_PROGRESS, base::string16()); | 418 SetUIPage(UIPage::START_PROGRESS, base::string16()); |
422 ShutdownBridge(); | 419 ShutdownBridge(); |
423 auth_code_ = auth_code; | 420 auth_code_ = auth_code; |
424 StartArc(); | 421 StartArc(); |
425 } | 422 } |
426 | 423 |
427 void ArcAuthService::CheckAuthCode() { | 424 void ArcAuthService::StartLso() { |
428 DCHECK(thread_checker_.CalledOnValidThread()); | 425 DCHECK(thread_checker_.CalledOnValidThread()); |
429 | 426 |
430 // Update UMA only if error is currently shown. | 427 // Update UMA only if error is currently shown. |
431 if (ui_page_ == UIPage::ERROR) | 428 if (ui_page_ == UIPage::ERROR) |
432 UpdateOptInActionUMA(OptInActionType::RETRY); | 429 UpdateOptInActionUMA(OptInActionType::RETRY); |
433 | 430 |
434 initial_opt_in_ = false; | 431 initial_opt_in_ = false; |
435 SetState(State::FETCHING_CODE); | 432 StartUI(); |
436 FetchAuthCode(); | |
437 } | |
438 | |
439 void ArcAuthService::FetchAuthCode() { | |
440 DCHECK(thread_checker_.CalledOnValidThread()); | |
441 | |
442 if (state_ != State::FETCHING_CODE) | |
443 return; | |
444 | |
445 auth_fetcher_.reset( | |
446 new ArcAuthFetcher(storage_partition_->GetURLRequestContext(), this)); | |
447 } | 433 } |
448 | 434 |
449 void ArcAuthService::CancelAuthCode() { | 435 void ArcAuthService::CancelAuthCode() { |
450 DCHECK(thread_checker_.CalledOnValidThread()); | 436 DCHECK(thread_checker_.CalledOnValidThread()); |
451 | 437 |
452 if (state_ != State::FETCHING_CODE) | 438 if (state_ != State::FETCHING_CODE) |
453 return; | 439 return; |
454 | 440 |
455 // Update UMA with user cancel only if error is not currently shown. | 441 // Update UMA with user cancel only if error is not currently shown. |
456 if (ui_page_ != UIPage::ERROR && ui_page_ != UIPage::NO_PAGE) | 442 if (ui_page_ != UIPage::ERROR && ui_page_ != UIPage::NO_PAGE) |
457 UpdateOptInCancelUMA(OptInCancelReason::USER_CANCEL); | 443 UpdateOptInCancelUMA(OptInCancelReason::USER_CANCEL); |
458 | 444 |
459 DisableArc(); | 445 DisableArc(); |
460 } | 446 } |
461 | 447 |
462 void ArcAuthService::EnableArc() { | 448 void ArcAuthService::EnableArc() { |
463 DCHECK(thread_checker_.CalledOnValidThread()); | 449 DCHECK(thread_checker_.CalledOnValidThread()); |
464 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); | 450 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
465 } | 451 } |
466 | 452 |
467 void ArcAuthService::DisableArc() { | 453 void ArcAuthService::DisableArc() { |
468 DCHECK(thread_checker_.CalledOnValidThread()); | 454 DCHECK(thread_checker_.CalledOnValidThread()); |
469 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, false); | 455 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, false); |
470 } | 456 } |
471 | 457 |
472 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { | |
473 DCHECK_EQ(state_, State::FETCHING_CODE); | |
474 SetAuthCodeAndStartArc(auth_code); | |
475 } | |
476 | |
477 void ArcAuthService::PrepareContext() { | 458 void ArcAuthService::PrepareContext() { |
478 DCHECK(thread_checker_.CalledOnValidThread()); | 459 DCHECK(thread_checker_.CalledOnValidThread()); |
479 | 460 |
480 // Get auth token to continue. | 461 // Get auth token to continue. |
481 ProfileOAuth2TokenService* token_service = | 462 ProfileOAuth2TokenService* token_service = |
482 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 463 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
483 SigninManagerBase* signin_manager = | 464 SigninManagerBase* signin_manager = |
484 SigninManagerFactory::GetForProfile(profile_); | 465 SigninManagerFactory::GetForProfile(profile_); |
485 CHECK(token_service && signin_manager); | 466 CHECK(token_service && signin_manager); |
486 const std::string& account_id = signin_manager->GetAuthenticatedAccountId(); | 467 const std::string& account_id = signin_manager->GetAuthenticatedAccountId(); |
487 ubertoken_fethcher_.reset( | 468 ubertoken_fethcher_.reset( |
488 new UbertokenFetcher(token_service, this, GaiaConstants::kChromeOSSource, | 469 new UbertokenFetcher(token_service, this, GaiaConstants::kChromeOSSource, |
489 storage_partition_->GetURLRequestContext())); | 470 storage_partition_->GetURLRequestContext())); |
490 ubertoken_fethcher_->StartFetchingToken(account_id); | 471 ubertoken_fethcher_->StartFetchingToken(account_id); |
491 } | 472 } |
492 | 473 |
493 void ArcAuthService::OnAuthCodeNeedUI() { | 474 void ArcAuthService::StartUI() { |
494 DCHECK(thread_checker_.CalledOnValidThread()); | 475 DCHECK(thread_checker_.CalledOnValidThread()); |
495 | 476 |
| 477 SetState(State::FETCHING_CODE); |
| 478 |
496 if (initial_opt_in_) { | 479 if (initial_opt_in_) { |
497 initial_opt_in_ = false; | 480 initial_opt_in_ = false; |
498 ShowUI(UIPage::START, base::string16()); | 481 ShowUI(UIPage::START, base::string16()); |
499 } else if (context_prepared_) { | 482 } else if (context_prepared_) { |
500 ShowUI(UIPage::LSO_PROGRESS, base::string16()); | 483 ShowUI(UIPage::LSO_PROGRESS, base::string16()); |
501 } else { | 484 } else { |
502 PrepareContext(); | 485 PrepareContext(); |
503 } | 486 } |
504 } | 487 } |
505 | 488 |
506 void ArcAuthService::OnAuthCodeFailed() { | 489 void ArcAuthService::OnPrepareContextFailed() { |
507 DCHECK_EQ(state_, State::FETCHING_CODE); | 490 DCHECK_EQ(state_, State::FETCHING_CODE); |
508 | 491 |
509 if (initial_opt_in_) { | 492 ShutdownBridgeAndShowUI( |
510 // Don't show error as first page. | 493 UIPage::ERROR, |
511 initial_opt_in_ = false; | 494 l10n_util::GetStringUTF16(IDS_ARC_SERVER_COMMUNICATION_ERROR)); |
512 ShutdownBridgeAndShowUI(UIPage::START, base::string16()); | 495 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR); |
513 } else { | |
514 ShutdownBridgeAndShowUI( | |
515 UIPage::ERROR, | |
516 l10n_util::GetStringUTF16(IDS_ARC_SERVER_COMMUNICATION_ERROR)); | |
517 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR); | |
518 } | |
519 } | 496 } |
520 | 497 |
521 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { | 498 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { |
522 switch (state) { | 499 switch (state) { |
523 case ArcAuthService::State::STOPPED: | 500 case ArcAuthService::State::STOPPED: |
524 return os << kStateStopped; | 501 return os << kStateStopped; |
525 case ArcAuthService::State::FETCHING_CODE: | 502 case ArcAuthService::State::FETCHING_CODE: |
526 return os << kStateFetchingCode; | 503 return os << kStateFetchingCode; |
527 case ArcAuthService::State::ACTIVE: | 504 case ArcAuthService::State::ACTIVE: |
528 return os << kStateActive; | 505 return os << kStateActive; |
529 default: | 506 default: |
530 NOTREACHED(); | 507 NOTREACHED(); |
531 return os; | 508 return os; |
532 } | 509 } |
533 } | 510 } |
534 | 511 |
535 } // namespace arc | 512 } // namespace arc |
OLD | NEW |