Chromium Code Reviews| 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_tracker.h" | 5 #include "chrome/browser/signin/signin_tracker.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/signin/profile_oauth2_token_service.h" | |
| 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 9 #include "chrome/browser/signin/signin_manager.h" | 11 #include "chrome/browser/signin/signin_manager.h" |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 11 #include "chrome/browser/signin/token_service.h" | |
| 12 #include "chrome/browser/signin/token_service_factory.h" | |
| 13 #include "content/public/browser/notification_details.h" | 13 #include "content/public/browser/notification_details.h" |
| 14 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
| 15 #include "google_apis/gaia/gaia_constants.h" | 15 #include "google_apis/gaia/gaia_constants.h" |
| 16 | 16 |
| 17 static const char* kSignedInServices[] = { | 17 // Returns true if the tokens are loaded for all signed-in services. |
| 18 GaiaConstants::kSyncService, | 18 static bool AreServiceTokensLoaded(Profile* profile) { |
| 19 GaiaConstants::kGaiaOAuth2LoginRefreshToken | 19 // SigninTracker only tracks the primary account, so only need to check |
| 20 }; | 20 // that one. |
| 21 static const int kNumSignedInServices = | 21 ProfileOAuth2TokenService* token_service = |
| 22 arraysize(kSignedInServices); | 22 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 23 | 23 |
| 24 // Helper to check if the given token service is relevant for sync. | 24 return token_service->RefreshTokenIsAvailable() && |
| 25 token_service->GetAuthStatus().state() == GoogleServiceAuthError::NONE; | |
| 26 } | |
| 27 | |
| 28 | |
| 25 SigninTracker::SigninTracker(Profile* profile, Observer* observer) | 29 SigninTracker::SigninTracker(Profile* profile, Observer* observer) |
| 26 : state_(WAITING_FOR_GAIA_VALIDATION), | 30 : profile_(profile), observer_(observer) { |
| 27 profile_(profile), | 31 DCHECK(profile_); |
| 28 observer_(observer), | |
| 29 credentials_valid_(false) { | |
| 30 Initialize(); | 32 Initialize(); |
| 31 } | 33 } |
| 32 | 34 |
| 33 SigninTracker::~SigninTracker() { | 35 SigninTracker::~SigninTracker() { |
| 36 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)-> | |
| 37 RemoveObserver(this); | |
| 34 } | 38 } |
| 35 | 39 |
| 36 void SigninTracker::Initialize() { | 40 void SigninTracker::Initialize() { |
| 37 DCHECK(observer_); | 41 DCHECK(observer_); |
| 42 | |
| 38 // Register for notifications from the SigninManager. | 43 // Register for notifications from the SigninManager. |
| 39 registrar_.Add(this, | 44 registrar_.Add(this, |
| 40 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | |
| 41 content::Source<Profile>(profile_)); | |
| 42 registrar_.Add(this, | |
| 43 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 45 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 44 content::Source<Profile>(profile_)); | 46 content::Source<Profile>(profile_)); |
| 45 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | |
| 46 registrar_.Add(this, | |
| 47 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
| 48 content::Source<TokenService>(token_service)); | |
| 49 registrar_.Add(this, | |
| 50 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | |
| 51 content::Source<TokenService>(token_service)); | |
| 52 | 47 |
| 53 if (state_ == SERVICES_INITIALIZING) | 48 OAuth2TokenService* token_service = |
| 54 HandleServiceStateChange(); | 49 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 50 token_service->AddObserver(this); | |
| 55 } | 51 } |
| 56 | 52 |
| 57 void SigninTracker::Observe(int type, | 53 void SigninTracker::Observe(int type, |
| 58 const content::NotificationSource& source, | 54 const content::NotificationSource& source, |
| 59 const content::NotificationDetails& details) { | 55 const content::NotificationDetails& details) { |
| 56 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, type); | |
| 57 | |
| 60 // We should not get more than one of these notifications. | 58 // We should not get more than one of these notifications. |
| 61 switch (type) { | 59 const GoogleServiceAuthError& error = |
| 62 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: | 60 *(content::Details<const GoogleServiceAuthError>(details).ptr()); |
| 63 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); | 61 observer_->SigninFailed(error); |
| 64 state_ = SERVICES_INITIALIZING; | |
| 65 // If our services are already signed in, see if it's possible to | |
| 66 // transition to the SIGNIN_COMPLETE state. | |
| 67 HandleServiceStateChange(); | |
| 68 break; | |
| 69 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: { | |
| 70 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); | |
| 71 const GoogleServiceAuthError& error = | |
| 72 *(content::Details<const GoogleServiceAuthError>(details).ptr()); | |
| 73 observer_->SigninFailed(error); | |
| 74 break; | |
| 75 } | |
| 76 case chrome::NOTIFICATION_TOKEN_AVAILABLE: | |
| 77 // A new token is available - check to see if we're all signed in now. | |
| 78 HandleServiceStateChange(); | |
| 79 break; | |
| 80 case chrome::NOTIFICATION_TOKEN_REQUEST_FAILED: | |
| 81 if (state_ == SERVICES_INITIALIZING) { | |
| 82 const TokenService::TokenRequestFailedDetails& token_details = | |
| 83 *(content::Details<const TokenService::TokenRequestFailedDetails>( | |
| 84 details).ptr()); | |
| 85 for (int i = 0; i < kNumSignedInServices; ++i) { | |
| 86 if (token_details.service() == kSignedInServices[i]) { | |
| 87 // We got an error loading one of our tokens, so notify our | |
| 88 // observer. | |
| 89 state_ = WAITING_FOR_GAIA_VALIDATION; | |
| 90 observer_->SigninFailed(token_details.error()); | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 break; | |
| 95 default: | |
| 96 NOTREACHED(); | |
| 97 } | |
| 98 } | 62 } |
| 99 | 63 |
| 100 void SigninTracker::HandleServiceStateChange() { | 64 void SigninTracker::OnRefreshTokenAvailable(const std::string& account_id) { |
| 101 if (state_ != SERVICES_INITIALIZING) { | 65 // TODO: when OAuth2TokenService handles multi-login, this should check |
| 102 // Ignore service updates until after our GAIA credentials are validated. | 66 // that |account_id| is the primary account before signalling success. |
| 103 return; | 67 observer_->SigninSuccess(); |
| 104 } | |
| 105 | |
| 106 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); | |
| 107 state_ = GetSigninState(profile_, &error); | |
| 108 switch (state_) { | |
| 109 case WAITING_FOR_GAIA_VALIDATION: | |
| 110 observer_->SigninFailed(error); | |
| 111 break; | |
| 112 case SIGNIN_COMPLETE: | |
| 113 observer_->SigninSuccess(); | |
| 114 break; | |
| 115 default: | |
| 116 // State has not changed, nothing to do. | |
| 117 DCHECK_EQ(state_, SERVICES_INITIALIZING); | |
| 118 break; | |
| 119 } | |
| 120 } | 68 } |
| 121 | 69 |
| 122 // static | 70 void SigninTracker::OnRefreshTokenRevoked(const std::string& account_id) { |
| 123 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { | 71 // TODO: when OAuth2TokenService handles multi-login, this should check |
| 124 // See if we have all of the tokens required. | 72 // that |account_id| is the primary account before signalling failure. |
| 125 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); | 73 observer_->SigninFailed( |
| 126 for (int i = 0; i < kNumSignedInServices; ++i) { | 74 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED)); |
|
Andrew T Wilson (Slow)
2013/09/09 12:08:27
Can we document under what circumstance OnRefreshT
Roger Tawa OOO till Jul 10th
2013/09/10 14:29:23
Good catch. While writing the unit tests I realiz
| |
| 127 if (!token_service->HasTokenForService(kSignedInServices[i])) { | |
| 128 // Don't have a token for one of our signed-in services. | |
| 129 return false; | |
| 130 } | |
| 131 } | |
| 132 return true; | |
| 133 } | 75 } |
| 134 | |
| 135 // static | |
| 136 SigninTracker::LoginState SigninTracker::GetSigninState( | |
| 137 Profile* profile, | |
| 138 GoogleServiceAuthError* error) { | |
| 139 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | |
| 140 if (signin->GetAuthenticatedUsername().empty()) { | |
| 141 // User is signed out, trigger a signin failure. | |
| 142 if (error) | |
| 143 *error = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | |
| 144 return WAITING_FOR_GAIA_VALIDATION; | |
| 145 } | |
| 146 | |
| 147 // If we haven't loaded all our service tokens yet, just exit (we'll be called | |
| 148 // again when another token is loaded, or will transition to SigninFailed if | |
| 149 // the loading fails). | |
| 150 if (!AreServiceTokensLoaded(profile)) | |
| 151 return SERVICES_INITIALIZING; | |
| 152 | |
| 153 return SIGNIN_COMPLETE; | |
| 154 } | |
| OLD | NEW |