Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/signin/account_service_flag_fetcher.h" | |
| 6 | |
| 7 #include "base/strings/string_split.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 11 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 12 #include "components/signin/core/browser/signin_manager.h" | |
| 13 #include "google_apis/gaia/gaia_constants.h" | |
| 14 | |
| 15 AccountServiceFlagFetcher::AccountServiceFlagFetcher(Profile* profile) | |
| 16 : OAuth2TokenService::Consumer("account_service_flag_fetcher"), | |
| 17 profile_(profile), | |
| 18 gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, | |
| 19 profile->GetRequestContext()) {} | |
| 20 | |
| 21 AccountServiceFlagFetcher::~AccountServiceFlagFetcher() { | |
| 22 // Ensures PO2TS observation is cleared when AccountServiceFlagFetcher is | |
| 23 // destructed before refresh token is available. | |
| 24 ProfileOAuth2TokenService* service = | |
| 25 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 26 service->RemoveObserver(this); | |
| 27 } | |
| 28 | |
| 29 void AccountServiceFlagFetcher::Start(const Callback& callback) { | |
| 30 DCHECK(!IsPending()); | |
| 31 | |
| 32 callback_ = callback; | |
| 33 | |
| 34 ProfileOAuth2TokenService* token_service = | |
| 35 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 36 SigninManagerBase* signin_manager = | |
| 37 SigninManagerFactory::GetForProfile(profile_); | |
| 38 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | |
| 39 if (token_service->RefreshTokenIsAvailable(account_id)) { | |
| 40 StartFetchingOAuth2AccessToken(account_id); | |
| 41 } else { | |
| 42 // Wait until we get a refresh token. | |
| 43 token_service->AddObserver(this); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 void AccountServiceFlagFetcher::Cancel() { | |
| 48 oauth2_access_token_request_.reset(); | |
| 49 ProfileOAuth2TokenService* service = | |
| 50 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 51 service->RemoveObserver(this); | |
|
Andrew T Wilson (Slow)
2014/05/16 08:44:44
Is it OK to call RemoveObserver() if you've never
Marc Treib
2014/05/20 10:46:40
Yes.
| |
| 52 | |
| 53 gaia_auth_fetcher_.CancelRequest(); | |
| 54 | |
| 55 callback_.Reset(); | |
| 56 } | |
| 57 | |
| 58 bool AccountServiceFlagFetcher::IsPending() const { | |
| 59 return !callback_.is_null(); | |
| 60 } | |
| 61 | |
| 62 void AccountServiceFlagFetcher::OnRefreshTokenAvailable( | |
| 63 const std::string& account_id) { | |
| 64 // Wait until we get a refresh token for the primary (authenticated) account. | |
| 65 SigninManagerBase* signin_manager = | |
| 66 SigninManagerFactory::GetForProfile(profile_); | |
| 67 if (account_id != signin_manager->GetAuthenticatedAccountId()) | |
|
Andrew T Wilson (Slow)
2014/05/16 08:44:44
Is it possible that you'll never get a token loade
Marc Treib
2014/05/20 10:46:40
I suppose that can happen - so you're saying I sho
Andrew T Wilson (Slow)
2014/05/23 14:51:59
Technically, yes.
Marc Treib
2014/05/26 12:31:40
Done.
However, if we call this after the tokens ha
| |
| 68 return; | |
| 69 | |
| 70 ProfileOAuth2TokenService* token_service = | |
| 71 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 72 token_service->RemoveObserver(this); | |
| 73 | |
| 74 StartFetchingOAuth2AccessToken(account_id); | |
| 75 } | |
| 76 | |
| 77 void AccountServiceFlagFetcher::StartFetchingOAuth2AccessToken( | |
| 78 const std::string& account_id) { | |
| 79 OAuth2TokenService::ScopeSet scopes; | |
| 80 scopes.insert(GaiaConstants::kOAuth1LoginScope); | |
| 81 ProfileOAuth2TokenService* token_service = | |
| 82 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 83 oauth2_access_token_request_ = token_service->StartRequest( | |
| 84 account_id, scopes, this); | |
| 85 } | |
| 86 | |
| 87 void AccountServiceFlagFetcher::OnGetTokenSuccess( | |
| 88 const OAuth2TokenService::Request* request, | |
| 89 const std::string& access_token, | |
| 90 const base::Time& expiration_time) { | |
| 91 DCHECK_EQ(oauth2_access_token_request_.get(), request); | |
| 92 oauth2_access_token_request_.reset(); | |
| 93 ProfileOAuth2TokenService* service = | |
| 94 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 95 service->RemoveObserver(this); | |
| 96 | |
| 97 gaia_auth_fetcher_.StartOAuthLogin(access_token, GaiaConstants::kGaiaService); | |
| 98 } | |
| 99 | |
| 100 void AccountServiceFlagFetcher::OnGetTokenFailure( | |
| 101 const OAuth2TokenService::Request* request, | |
| 102 const GoogleServiceAuthError& error) { | |
| 103 DCHECK_EQ(oauth2_access_token_request_.get(), request); | |
| 104 oauth2_access_token_request_.reset(); | |
| 105 ProfileOAuth2TokenService* service = | |
| 106 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 107 service->RemoveObserver(this); | |
| 108 | |
| 109 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetTokenFailure: " | |
| 110 << error.ToString(); | |
| 111 callback_.Reset(); | |
|
Andrew T Wilson (Slow)
2014/05/16 08:44:44
Hmmm. So, the paradigm is that you call Start(), a
Marc Treib
2014/05/20 10:46:40
I have added a status code to the callback to repo
| |
| 112 } | |
| 113 | |
| 114 void AccountServiceFlagFetcher::OnClientLoginSuccess( | |
| 115 const ClientLoginResult& result) { | |
| 116 gaia_auth_fetcher_.StartGetUserInfo(result.lsid); | |
| 117 } | |
| 118 | |
| 119 void AccountServiceFlagFetcher::OnClientLoginFailure( | |
| 120 const GoogleServiceAuthError& error) { | |
| 121 DLOG(WARNING) << "AccountServiceFlagFetcher::OnClientLoginFailure: " | |
| 122 << error.ToString(); | |
| 123 callback_.Reset(); | |
| 124 } | |
| 125 | |
| 126 void AccountServiceFlagFetcher::OnGetUserInfoSuccess(const UserInfoMap& data) { | |
| 127 std::vector<std::string> services; | |
| 128 UserInfoMap::const_iterator services_iter = data.find("allServices"); | |
| 129 if (services_iter != data.end()) { | |
| 130 base::SplitString(services_iter->second, ',', &services); | |
| 131 } else { | |
| 132 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoSuccess: " | |
| 133 << "GetUserInfo response didn't include allServices field."; | |
| 134 } | |
| 135 callback_.Run(services); | |
| 136 callback_.Reset(); | |
| 137 } | |
| 138 | |
| 139 void AccountServiceFlagFetcher::OnGetUserInfoFailure( | |
| 140 const GoogleServiceAuthError& error) { | |
| 141 DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoFailure: " | |
| 142 << error.ToString(); | |
| 143 callback_.Reset(); | |
| 144 } | |
| OLD | NEW |