Chromium Code Reviews| Index: chrome/browser/signin/account_service_flag_fetcher.cc |
| diff --git a/chrome/browser/signin/account_service_flag_fetcher.cc b/chrome/browser/signin/account_service_flag_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dc640015693e78bf3a390dc35fb51954eeedb926 |
| --- /dev/null |
| +++ b/chrome/browser/signin/account_service_flag_fetcher.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/signin/account_service_flag_fetcher.h" |
| + |
| +#include "base/strings/string_split.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| +#include "chrome/browser/signin/signin_manager_factory.h" |
| +#include "components/signin/core/browser/profile_oauth2_token_service.h" |
| +#include "components/signin/core/browser/signin_manager.h" |
| +#include "google_apis/gaia/gaia_constants.h" |
| + |
| +AccountServiceFlagFetcher::AccountServiceFlagFetcher(Profile* profile) |
| + : OAuth2TokenService::Consumer("account_service_flag_fetcher"), |
| + profile_(profile), |
| + gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, |
| + profile->GetRequestContext()) {} |
| + |
| +AccountServiceFlagFetcher::~AccountServiceFlagFetcher() { |
| + // Ensures PO2TS observation is cleared when AccountServiceFlagFetcher is |
| + // destructed before refresh token is available. |
| + ProfileOAuth2TokenService* service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| + service->RemoveObserver(this); |
| +} |
| + |
| +void AccountServiceFlagFetcher::Start(const Callback& callback) { |
| + DCHECK(!IsPending()); |
| + |
| + callback_ = callback; |
| + |
| + ProfileOAuth2TokenService* token_service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| + SigninManagerBase* signin_manager = |
| + SigninManagerFactory::GetForProfile(profile_); |
| + std::string account_id = signin_manager->GetAuthenticatedAccountId(); |
| + if (token_service->RefreshTokenIsAvailable(account_id)) { |
| + StartFetchingOAuth2AccessToken(account_id); |
| + } else { |
| + // Wait until we get a refresh token. |
| + token_service->AddObserver(this); |
| + } |
| +} |
| + |
| +void AccountServiceFlagFetcher::Cancel() { |
| + oauth2_access_token_request_.reset(); |
| + ProfileOAuth2TokenService* service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| + 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.
|
| + |
| + gaia_auth_fetcher_.CancelRequest(); |
| + |
| + callback_.Reset(); |
| +} |
| + |
| +bool AccountServiceFlagFetcher::IsPending() const { |
| + return !callback_.is_null(); |
| +} |
| + |
| +void AccountServiceFlagFetcher::OnRefreshTokenAvailable( |
| + const std::string& account_id) { |
| + // Wait until we get a refresh token for the primary (authenticated) account. |
| + SigninManagerBase* signin_manager = |
| + SigninManagerFactory::GetForProfile(profile_); |
| + 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
|
| + return; |
| + |
| + ProfileOAuth2TokenService* token_service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| + token_service->RemoveObserver(this); |
| + |
| + StartFetchingOAuth2AccessToken(account_id); |
| +} |
| + |
| +void AccountServiceFlagFetcher::StartFetchingOAuth2AccessToken( |
| + const std::string& account_id) { |
| + OAuth2TokenService::ScopeSet scopes; |
| + scopes.insert(GaiaConstants::kOAuth1LoginScope); |
| + ProfileOAuth2TokenService* token_service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| + oauth2_access_token_request_ = token_service->StartRequest( |
| + account_id, scopes, this); |
| +} |
| + |
| +void AccountServiceFlagFetcher::OnGetTokenSuccess( |
| + const OAuth2TokenService::Request* request, |
| + const std::string& access_token, |
| + const base::Time& expiration_time) { |
| + DCHECK_EQ(oauth2_access_token_request_.get(), request); |
| + oauth2_access_token_request_.reset(); |
| + ProfileOAuth2TokenService* service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| + service->RemoveObserver(this); |
| + |
| + gaia_auth_fetcher_.StartOAuthLogin(access_token, GaiaConstants::kGaiaService); |
| +} |
| + |
| +void AccountServiceFlagFetcher::OnGetTokenFailure( |
| + const OAuth2TokenService::Request* request, |
| + const GoogleServiceAuthError& error) { |
| + DCHECK_EQ(oauth2_access_token_request_.get(), request); |
| + oauth2_access_token_request_.reset(); |
| + ProfileOAuth2TokenService* service = |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| + service->RemoveObserver(this); |
| + |
| + DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetTokenFailure: " |
| + << error.ToString(); |
| + 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
|
| +} |
| + |
| +void AccountServiceFlagFetcher::OnClientLoginSuccess( |
| + const ClientLoginResult& result) { |
| + gaia_auth_fetcher_.StartGetUserInfo(result.lsid); |
| +} |
| + |
| +void AccountServiceFlagFetcher::OnClientLoginFailure( |
| + const GoogleServiceAuthError& error) { |
| + DLOG(WARNING) << "AccountServiceFlagFetcher::OnClientLoginFailure: " |
| + << error.ToString(); |
| + callback_.Reset(); |
| +} |
| + |
| +void AccountServiceFlagFetcher::OnGetUserInfoSuccess(const UserInfoMap& data) { |
| + std::vector<std::string> services; |
| + UserInfoMap::const_iterator services_iter = data.find("allServices"); |
| + if (services_iter != data.end()) { |
| + base::SplitString(services_iter->second, ',', &services); |
| + } else { |
| + DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoSuccess: " |
| + << "GetUserInfo response didn't include allServices field."; |
| + } |
| + callback_.Run(services); |
| + callback_.Reset(); |
| +} |
| + |
| +void AccountServiceFlagFetcher::OnGetUserInfoFailure( |
| + const GoogleServiceAuthError& error) { |
| + DLOG(WARNING) << "AccountServiceFlagFetcher::OnGetUserInfoFailure: " |
| + << error.ToString(); |
| + callback_.Reset(); |
| +} |