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[] = { | |
| 18 GaiaConstants::kSyncService, | |
| 19 GaiaConstants::kGaiaOAuth2LoginRefreshToken | |
| 20 }; | |
| 21 static const int kNumSignedInServices = | |
| 22 arraysize(kSignedInServices); | |
| 23 | |
| 24 // Helper to check if the given token service is relevant for sync. | |
| 25 SigninTracker::SigninTracker(Profile* profile, Observer* observer) | 17 SigninTracker::SigninTracker(Profile* profile, Observer* observer) |
| 26 : state_(WAITING_FOR_GAIA_VALIDATION), | 18 : state_(WAITING_FOR_GAIA_VALIDATION), |
| 27 profile_(profile), | 19 profile_(profile), |
| 28 observer_(observer), | 20 observer_(observer), |
| 29 credentials_valid_(false) { | 21 credentials_valid_(false) { |
| 22 DCHECK(profile_); | |
| 30 Initialize(); | 23 Initialize(); |
|
Andrew T Wilson (Slow)
2013/08/19 15:23:06
I'm guessing it's an error to call this if the OAu
| |
| 31 } | 24 } |
| 32 | 25 |
| 33 SigninTracker::~SigninTracker() { | 26 SigninTracker::~SigninTracker() { |
| 27 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)-> | |
| 28 RemoveObserver(this); | |
| 34 } | 29 } |
| 35 | 30 |
| 36 void SigninTracker::Initialize() { | 31 void SigninTracker::Initialize() { |
| 37 DCHECK(observer_); | 32 DCHECK(observer_); |
| 38 // Register for notifications from the SigninManager. | 33 // Register for notifications from the SigninManager. |
| 39 registrar_.Add(this, | 34 registrar_.Add(this, |
| 40 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 35 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 41 content::Source<Profile>(profile_)); | 36 content::Source<Profile>(profile_)); |
| 42 registrar_.Add(this, | 37 registrar_.Add(this, |
| 43 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 38 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 44 content::Source<Profile>(profile_)); | 39 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 | 40 |
| 53 if (state_ == SERVICES_INITIALIZING) | 41 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->AddObserver(this); |
| 54 HandleServiceStateChange(); | |
|
Andrew T Wilson (Slow)
2013/08/19 15:23:06
I guess we no longer support initializing in an in
Roger Tawa OOO till Jul 10th
2013/08/29 19:28:41
I noticed this was true while making this change.
| |
| 55 } | 42 } |
| 56 | 43 |
| 57 void SigninTracker::Observe(int type, | 44 void SigninTracker::Observe(int type, |
| 58 const content::NotificationSource& source, | 45 const content::NotificationSource& source, |
| 59 const content::NotificationDetails& details) { | 46 const content::NotificationDetails& details) { |
| 60 // We should not get more than one of these notifications. | 47 // We should not get more than one of these notifications. |
| 61 switch (type) { | 48 switch (type) { |
| 62 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: | 49 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: |
| 63 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); | 50 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); |
| 64 state_ = SERVICES_INITIALIZING; | 51 state_ = SERVICES_INITIALIZING; |
| 65 // If our services are already signed in, see if it's possible to | 52 // If our services are already signed in, see if it's possible to |
| 66 // transition to the SIGNIN_COMPLETE state. | 53 // transition to the SIGNIN_COMPLETE state. |
| 67 HandleServiceStateChange(); | 54 HandleServiceStateChange(); |
| 68 break; | 55 break; |
| 69 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: { | 56 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: { |
| 70 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); | 57 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); |
| 71 const GoogleServiceAuthError& error = | 58 const GoogleServiceAuthError& error = |
| 72 *(content::Details<const GoogleServiceAuthError>(details).ptr()); | 59 *(content::Details<const GoogleServiceAuthError>(details).ptr()); |
| 73 observer_->SigninFailed(error); | 60 observer_->SigninFailed(error); |
| 74 break; | 61 break; |
| 75 } | 62 } |
| 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: | 63 default: |
| 96 NOTREACHED(); | 64 NOTREACHED() << "Invalid type=" << type; |
| 97 } | 65 } |
| 98 } | 66 } |
| 99 | 67 |
| 68 void SigninTracker::OnRefreshTokenAvailable(const std::string& account_id) { | |
| 69 HandleServiceStateChange(); | |
| 70 } | |
| 71 | |
| 72 void SigninTracker::OnRefreshTokenRevoked(const std::string& account_id, | |
| 73 const GoogleServiceAuthError& error) { | |
| 74 if (state_ == SERVICES_INITIALIZING) { | |
| 75 // We got an error loading one of our tokens, so notify our observer. | |
| 76 state_ = WAITING_FOR_GAIA_VALIDATION; | |
| 77 observer_->SigninFailed(error); | |
| 78 } | |
| 79 } | |
| 80 | |
| 100 void SigninTracker::HandleServiceStateChange() { | 81 void SigninTracker::HandleServiceStateChange() { |
| 101 if (state_ != SERVICES_INITIALIZING) { | 82 if (state_ != SERVICES_INITIALIZING) { |
| 102 // Ignore service updates until after our GAIA credentials are validated. | 83 // Ignore service updates until after our GAIA credentials are validated. |
| 103 return; | 84 return; |
| 104 } | 85 } |
| 105 | 86 |
| 106 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); | 87 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); |
| 107 state_ = GetSigninState(profile_, &error); | 88 state_ = GetSigninState(profile_, &error); |
| 108 switch (state_) { | 89 switch (state_) { |
| 109 case WAITING_FOR_GAIA_VALIDATION: | 90 case WAITING_FOR_GAIA_VALIDATION: |
| 110 observer_->SigninFailed(error); | 91 observer_->SigninFailed(error); |
| 111 break; | 92 break; |
| 112 case SIGNIN_COMPLETE: | 93 case SIGNIN_COMPLETE: |
| 113 observer_->SigninSuccess(); | 94 observer_->SigninSuccess(); |
| 114 break; | 95 break; |
| 115 default: | 96 default: |
| 116 // State has not changed, nothing to do. | 97 // State has not changed, nothing to do. |
| 117 DCHECK_EQ(state_, SERVICES_INITIALIZING); | 98 DCHECK_EQ(state_, SERVICES_INITIALIZING); |
| 118 break; | 99 break; |
| 119 } | 100 } |
| 120 } | 101 } |
| 121 | 102 |
| 122 // static | 103 // static |
| 123 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { | 104 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { |
| 124 // See if we have all of the tokens required. | 105 return ProfileOAuth2TokenServiceFactory::GetForProfile(profile)-> |
| 125 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); | 106 RefreshTokenIsAvailable(); |
| 126 for (int i = 0; i < kNumSignedInServices; ++i) { | |
| 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 } | 107 } |
| 134 | 108 |
| 135 // static | 109 // static |
| 136 SigninTracker::LoginState SigninTracker::GetSigninState( | 110 SigninTracker::LoginState SigninTracker::GetSigninState( |
| 137 Profile* profile, | 111 Profile* profile, |
| 138 GoogleServiceAuthError* error) { | 112 GoogleServiceAuthError* error) { |
| 139 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | 113 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); |
| 140 if (signin->GetAuthenticatedUsername().empty()) { | 114 if (signin->GetAuthenticatedUsername().empty()) { |
| 141 // User is signed out, trigger a signin failure. | 115 // User is signed out, trigger a signin failure. |
| 142 if (error) | 116 if (error) |
| 143 *error = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 117 *error = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 144 return WAITING_FOR_GAIA_VALIDATION; | 118 return WAITING_FOR_GAIA_VALIDATION; |
| 145 } | 119 } |
| 146 | 120 |
| 147 // If we haven't loaded all our service tokens yet, just exit (we'll be called | 121 // 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 | 122 // again when another token is loaded, or will transition to SigninFailed if |
| 149 // the loading fails). | 123 // the loading fails). |
| 150 if (!AreServiceTokensLoaded(profile)) | 124 if (!AreServiceTokensLoaded(profile)) |
| 151 return SERVICES_INITIALIZING; | 125 return SERVICES_INITIALIZING; |
| 152 | 126 |
| 153 return SIGNIN_COMPLETE; | 127 return SIGNIN_COMPLETE; |
| 154 } | 128 } |
| OLD | NEW |