OLD | NEW |
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/signin/signin_manager.h" | 5 #include "chrome/browser/signin/signin_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 last_result_ = ClientLoginResult(); | 508 last_result_ = ClientLoginResult(); |
509 possibly_invalid_username_.clear(); | 509 possibly_invalid_username_.clear(); |
510 password_.clear(); | 510 password_.clear(); |
511 had_two_factor_error_ = false; | 511 had_two_factor_error_ = false; |
512 type_ = SIGNIN_TYPE_NONE; | 512 type_ = SIGNIN_TYPE_NONE; |
513 temp_oauth_login_tokens_ = ClientOAuthResult(); | 513 temp_oauth_login_tokens_ = ClientOAuthResult(); |
514 } | 514 } |
515 | 515 |
516 void SigninManager::HandleAuthError(const GoogleServiceAuthError& error, | 516 void SigninManager::HandleAuthError(const GoogleServiceAuthError& error, |
517 bool clear_transient_data) { | 517 bool clear_transient_data) { |
| 518 // In some cases, the user should not be signed out. For example, the failure |
| 519 // may be due to a captcha or OTP challenge. In these cases, the transient |
| 520 // data must be kept to properly handle the follow up. This routine clears |
| 521 // the data before sending out the notification so the SigninManager is no |
| 522 // longer in the AuthInProgress state when the notification goes out. |
| 523 if (clear_transient_data) |
| 524 ClearTransientSigninData(); |
| 525 |
518 content::NotificationService::current()->Notify( | 526 content::NotificationService::current()->Notify( |
519 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 527 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
520 content::Source<Profile>(profile_), | 528 content::Source<Profile>(profile_), |
521 content::Details<const GoogleServiceAuthError>(&error)); | 529 content::Details<const GoogleServiceAuthError>(&error)); |
522 | |
523 // In some cases, the user should not be signed out. For example, the failure | |
524 // may be due to a captcha or OTP challenge. In these cases, the transient | |
525 // data must be kept to properly handle the follow up. | |
526 if (clear_transient_data) | |
527 ClearTransientSigninData(); | |
528 } | 530 } |
529 | 531 |
530 void SigninManager::SignOut() { | 532 void SigninManager::SignOut() { |
531 DCHECK(IsInitialized()); | 533 DCHECK(IsInitialized()); |
| 534 |
| 535 if (authenticated_username_.empty()) { |
| 536 if (AuthInProgress()) { |
| 537 // If the user is in the process of signing in, then treat a call to |
| 538 // SignOut as a cancellation request. |
| 539 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 540 HandleAuthError(error, true); |
| 541 } else { |
| 542 // Clean up our transient data and exit if we aren't signed in. |
| 543 // This avoids a perf regression from clearing out the TokenDB if |
| 544 // SignOut() is invoked on startup to clean up any incomplete previous |
| 545 // signin attempts. |
| 546 ClearTransientSigninData(); |
| 547 } |
| 548 return; |
| 549 } |
| 550 |
532 if (prohibit_signout_) { | 551 if (prohibit_signout_) { |
533 DVLOG(1) << "Ignoring attempt to sign out while signout is prohibited"; | 552 DVLOG(1) << "Ignoring attempt to sign out while signout is prohibited"; |
534 return; | 553 return; |
535 } | 554 } |
536 if (authenticated_username_.empty() && !client_login_.get()) { | 555 DCHECK(!authenticated_username_.empty()); |
537 // Clean up our transient data and exit if we aren't signed in (or in the | |
538 // process of signing in). This avoids a perf regression from clearing out | |
539 // the TokenDB if SignOut() is invoked on startup to clean up any | |
540 // incomplete previous signin attempts. | |
541 ClearTransientSigninData(); | |
542 return; | |
543 } | |
544 | |
545 GoogleServiceSignoutDetails details(authenticated_username_); | 556 GoogleServiceSignoutDetails details(authenticated_username_); |
546 | 557 |
547 ClearTransientSigninData(); | 558 ClearTransientSigninData(); |
548 authenticated_username_.clear(); | 559 authenticated_username_.clear(); |
549 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | 560 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
550 | 561 |
551 // Erase (now) stale information from AboutSigninInternals. | 562 // Erase (now) stale information from AboutSigninInternals. |
552 NotifyDiagnosticsObservers(USERNAME, std::string()); | 563 NotifyDiagnosticsObservers(USERNAME, std::string()); |
553 NotifyDiagnosticsObservers(LSID, std::string()); | 564 NotifyDiagnosticsObservers(LSID, std::string()); |
554 NotifyDiagnosticsObservers(signin_internals_util::SID, std::string()); | 565 NotifyDiagnosticsObservers(signin_internals_util::SID, std::string()); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
747 policy::UserPolicySigninService* policy_service = | 758 policy::UserPolicySigninService* policy_service = |
748 policy::UserPolicySigninServiceFactory::GetForProfile(profile_); | 759 policy::UserPolicySigninServiceFactory::GetForProfile(profile_); |
749 policy_service->FetchPolicyForSignedInUser( | 760 policy_service->FetchPolicyForSignedInUser( |
750 policy_client_.Pass(), | 761 policy_client_.Pass(), |
751 base::Bind(&SigninManager::OnPolicyFetchComplete, | 762 base::Bind(&SigninManager::OnPolicyFetchComplete, |
752 weak_pointer_factory_.GetWeakPtr())); | 763 weak_pointer_factory_.GetWeakPtr())); |
753 } | 764 } |
754 | 765 |
755 void SigninManager::OnPolicyFetchComplete(bool success) { | 766 void SigninManager::OnPolicyFetchComplete(bool success) { |
756 // For now, we allow signin to complete even if the policy fetch fails. If | 767 // For now, we allow signin to complete even if the policy fetch fails. If |
757 // we ever want to change this behavior, we could call SignOut() here | 768 // we ever want to change this behavior, we could call HandleAuthError() here |
758 // instead. | 769 // instead. |
759 DLOG_IF(ERROR, !success) << "Error fetching policy for user"; | 770 DLOG_IF(ERROR, !success) << "Error fetching policy for user"; |
760 DVLOG_IF(1, success) << "Policy fetch successful - completing signin"; | 771 DVLOG_IF(1, success) << "Policy fetch successful - completing signin"; |
761 CompleteSigninAfterPolicyLoad(); | 772 CompleteSigninAfterPolicyLoad(); |
762 } | 773 } |
763 | 774 |
764 void SigninManager::TransferCredentialsToNewProfile() { | 775 void SigninManager::TransferCredentialsToNewProfile() { |
765 DCHECK(!possibly_invalid_username_.empty()); | 776 DCHECK(!possibly_invalid_username_.empty()); |
766 DCHECK(policy_client_); | 777 DCHECK(policy_client_); |
767 // Create a new profile and have it call back when done so we can inject our | 778 // Create a new profile and have it call back when done so we can inject our |
768 // signin credentials. | 779 // signin credentials. |
769 ProfileManager::CreateMultiProfileAsync( | 780 ProfileManager::CreateMultiProfileAsync( |
770 UTF8ToUTF16(possibly_invalid_username_), | 781 UTF8ToUTF16(possibly_invalid_username_), |
771 UTF8ToUTF16(ProfileInfoCache::GetDefaultAvatarIconUrl(1)), | 782 UTF8ToUTF16(ProfileInfoCache::GetDefaultAvatarIconUrl(1)), |
772 base::Bind(&SigninManager::CompleteSigninForNewProfile, | 783 base::Bind(&SigninManager::CompleteSigninForNewProfile, |
773 weak_pointer_factory_.GetWeakPtr()), | 784 weak_pointer_factory_.GetWeakPtr()), |
774 chrome::GetActiveDesktop(), | 785 chrome::GetActiveDesktop(), |
775 false); | 786 false); |
776 } | 787 } |
777 | 788 |
778 void SigninManager::CompleteSigninForNewProfile( | 789 void SigninManager::CompleteSigninForNewProfile( |
779 Profile* profile, | 790 Profile* profile, |
780 Profile::CreateStatus status) { | 791 Profile::CreateStatus status) { |
781 DCHECK_NE(profile_, profile); | 792 DCHECK_NE(profile_, profile); |
782 // TODO(atwilson): On error, unregister the client to release the DMToken. | |
783 if (status == Profile::CREATE_STATUS_FAIL) { | 793 if (status == Profile::CREATE_STATUS_FAIL) { |
| 794 // TODO(atwilson): On error, unregister the client to release the DMToken |
| 795 // and surface a better error for the user. |
784 NOTREACHED() << "Error creating new profile"; | 796 NOTREACHED() << "Error creating new profile"; |
785 SignOut(); | 797 GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_UNAVAILABLE); |
| 798 HandleAuthError(error, true); |
786 return; | 799 return; |
787 } | 800 } |
788 | 801 |
789 // Wait until the profile is initialized before we transfer credentials. | 802 // Wait until the profile is initialized before we transfer credentials. |
790 if (status == Profile::CREATE_STATUS_INITIALIZED) { | 803 if (status == Profile::CREATE_STATUS_INITIALIZED) { |
791 DCHECK(!possibly_invalid_username_.empty()); | 804 DCHECK(!possibly_invalid_username_.empty()); |
792 DCHECK(policy_client_); | 805 DCHECK(policy_client_); |
793 // Sign in to the just-created profile and fetch policy for it. | 806 // Sign in to the just-created profile and fetch policy for it. |
794 SigninManager* signin_manager = | 807 SigninManager* signin_manager = |
795 SigninManagerFactory::GetForProfile(profile); | 808 SigninManagerFactory::GetForProfile(profile); |
796 DCHECK(signin_manager); | 809 DCHECK(signin_manager); |
797 signin_manager->possibly_invalid_username_ = possibly_invalid_username_; | 810 signin_manager->possibly_invalid_username_ = possibly_invalid_username_; |
798 signin_manager->last_result_ = last_result_; | 811 signin_manager->last_result_ = last_result_; |
799 signin_manager->temp_oauth_login_tokens_ = temp_oauth_login_tokens_; | 812 signin_manager->temp_oauth_login_tokens_ = temp_oauth_login_tokens_; |
800 signin_manager->policy_client_.reset(policy_client_.release()); | 813 signin_manager->policy_client_.reset(policy_client_.release()); |
801 signin_manager->LoadPolicyWithCachedClient(); | 814 signin_manager->LoadPolicyWithCachedClient(); |
802 // Allow sync to start up if it is not overridden by policy. | 815 // Allow sync to start up if it is not overridden by policy. |
803 browser_sync::SyncPrefs prefs(profile->GetPrefs()); | 816 browser_sync::SyncPrefs prefs(profile->GetPrefs()); |
804 prefs.SetSyncSetupCompleted(); | 817 prefs.SetSyncSetupCompleted(); |
805 | 818 |
806 // We've transferred our credentials to the new profile - sign out. | 819 // We've transferred our credentials to the new profile - notify that |
| 820 // the signin for this profile was cancelled. |
807 SignOut(); | 821 SignOut(); |
808 } | 822 } |
809 } | 823 } |
810 #endif | 824 #endif |
811 | 825 |
812 void SigninManager::CompleteSigninAfterPolicyLoad() { | 826 void SigninManager::CompleteSigninAfterPolicyLoad() { |
813 DCHECK(!possibly_invalid_username_.empty()); | 827 DCHECK(!possibly_invalid_username_.empty()); |
814 SetAuthenticatedUsername(possibly_invalid_username_); | 828 SetAuthenticatedUsername(possibly_invalid_username_); |
815 possibly_invalid_username_.clear(); | 829 possibly_invalid_username_.clear(); |
816 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | 830 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 NotifySigninValueChanged(field, value)); | 967 NotifySigninValueChanged(field, value)); |
954 } | 968 } |
955 | 969 |
956 void SigninManager::NotifyDiagnosticsObservers( | 970 void SigninManager::NotifyDiagnosticsObservers( |
957 const TimedSigninStatusField& field, | 971 const TimedSigninStatusField& field, |
958 const std::string& value) { | 972 const std::string& value) { |
959 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 973 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
960 signin_diagnostics_observers_, | 974 signin_diagnostics_observers_, |
961 NotifySigninValueChanged(field, value)); | 975 NotifySigninValueChanged(field, value)); |
962 } | 976 } |
OLD | NEW |