| Index: chrome/browser/chromeos/login/oauth2_login_manager.cc
|
| diff --git a/chrome/browser/chromeos/login/oauth2_login_manager.cc b/chrome/browser/chromeos/login/oauth2_login_manager.cc
|
| index c8f1921b72fa39cc6f8e764ee376397c2f423dab..0509bea7b9a0327eb018ec473a52a57a7478828a 100644
|
| --- a/chrome/browser/chromeos/login/oauth2_login_manager.cc
|
| +++ b/chrome/browser/chromeos/login/oauth2_login_manager.cc
|
| @@ -12,21 +12,23 @@
|
| #include "chrome/browser/profiles/profile.h"
|
| #include "chrome/browser/signin/profile_oauth2_token_service.h"
|
| #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
|
| -#include "chrome/browser/signin/token_service.h"
|
| -#include "chrome/browser/signin/token_service_factory.h"
|
| #include "chrome/common/chrome_switches.h"
|
| #include "google_apis/gaia/gaia_constants.h"
|
| +#include "google_apis/gaia/gaia_urls.h"
|
| #include "net/url_request/url_request_context_getter.h"
|
|
|
| namespace chromeos {
|
|
|
| +static const char kServiceScopeGetUserInfo[] =
|
| + "https://www.googleapis.com/auth/userinfo.email";
|
| +static const int kMaxRetries = 5;
|
| +
|
| OAuth2LoginManager::OAuth2LoginManager(Profile* user_profile)
|
| : user_profile_(user_profile),
|
| restore_strategy_(RESTORE_FROM_COOKIE_JAR),
|
| state_(SESSION_RESTORE_NOT_STARTED),
|
| loading_reported_(false) {
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_)->
|
| - AddObserver(this);
|
| + GetTokenService()->AddObserver(this);
|
| }
|
|
|
| OAuth2LoginManager::~OAuth2LoginManager() {
|
| @@ -67,14 +69,12 @@ void OAuth2LoginManager::ContinueSessionRestore() {
|
| if (restore_strategy_ == RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN) {
|
| DCHECK(!refresh_token_.empty());
|
| restore_strategy_ = RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN;
|
| - GaiaAuthConsumer::ClientOAuthResult oauth2_tokens;
|
| - oauth2_tokens.refresh_token = refresh_token_;
|
| - StoreOAuth2Tokens(oauth2_tokens);
|
| + GetAccountIdOfRefreshToken(refresh_token_);
|
| return;
|
| }
|
|
|
| DCHECK(restore_strategy_ == RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN);
|
| - LoadAndVerifyOAuth2Tokens();
|
| + GetTokenService()->LoadCredentials();
|
| }
|
|
|
| void OAuth2LoginManager::Stop() {
|
| @@ -98,26 +98,52 @@ void OAuth2LoginManager::OnRefreshTokenAvailable(
|
| RestoreSessionCookies();
|
| }
|
|
|
| -TokenService* OAuth2LoginManager::SetupTokenService() {
|
| - TokenService* token_service =
|
| - TokenServiceFactory::GetForProfile(user_profile_);
|
| +ProfileOAuth2TokenService* OAuth2LoginManager::GetTokenService() {
|
| + ProfileOAuth2TokenService* token_service =
|
| + ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_);
|
| return token_service;
|
| }
|
|
|
| -void OAuth2LoginManager::StoreOAuth2Tokens(
|
| - const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) {
|
| - TokenService* token_service = SetupTokenService();
|
| - token_service->UpdateCredentialsWithOAuth2(oauth2_tokens);
|
| +void OAuth2LoginManager::GetAccountIdOfRefreshToken(
|
| + const std::string& refresh_token) {
|
| + gaia::OAuthClientInfo client_info;
|
| + GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
|
| + client_info.client_id = gaia_urls->oauth2_chrome_client_id();
|
| + client_info.client_secret = gaia_urls->oauth2_chrome_client_secret();
|
| +
|
| + account_id_fetcher_.reset(new gaia::GaiaOAuthClient(
|
| + auth_request_context_.get()));
|
| + account_id_fetcher_->RefreshToken(client_info, refresh_token,
|
| + std::vector<std::string>(1, kServiceScopeGetUserInfo), kMaxRetries,
|
| + this);
|
| +}
|
| +
|
| +void OAuth2LoginManager::OnRefreshTokenResponse(
|
| + const std::string& access_token,
|
| + int expires_in_seconds) {
|
| + account_id_fetcher_->GetUserEmail(access_token, kMaxRetries, this);
|
| +}
|
| +
|
| +void OAuth2LoginManager::OnGetUserEmailResponse(
|
| + const std::string& user_email) {
|
| + DCHECK(!refresh_token_.empty());
|
| + account_id_fetcher_.reset();
|
| + GetTokenService()->UpdateCredentials(user_email, refresh_token_);
|
|
|
| FOR_EACH_OBSERVER(Observer, observer_list_,
|
| OnNewRefreshTokenAvaiable(user_profile_));
|
| }
|
|
|
| -void OAuth2LoginManager::LoadAndVerifyOAuth2Tokens() {
|
| - // If we have no cookies, try to load saved OAuth2 token from TokenService.
|
| - TokenService* token_service = SetupTokenService();
|
| - token_service->Initialize(GaiaConstants::kChromeSource, user_profile_);
|
| - token_service->LoadTokensFromDB();
|
| +void OAuth2LoginManager::OnOAuthError() {
|
| + account_id_fetcher_.reset();
|
| + LOG(ERROR) << "Account id fetch failed!";
|
| + SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED);
|
| +}
|
| +
|
| +void OAuth2LoginManager::OnNetworkError(int response_code) {
|
| + account_id_fetcher_.reset();
|
| + LOG(ERROR) << "Account id fetch failed! response_code=" << response_code;
|
| + SetSessionRestoreState(OAuth2LoginManager::SESSION_RESTORE_FAILED);
|
| }
|
|
|
| void OAuth2LoginManager::FetchOAuth2Tokens() {
|
| @@ -143,7 +169,9 @@ void OAuth2LoginManager::FetchOAuth2Tokens() {
|
| void OAuth2LoginManager::OnOAuth2TokensAvailable(
|
| const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) {
|
| LOG(INFO) << "OAuth2 tokens fetched";
|
| - StoreOAuth2Tokens(oauth2_tokens);
|
| + DCHECK(refresh_token_.empty());
|
| + refresh_token_.assign(oauth2_tokens.refresh_token);
|
| + GetAccountIdOfRefreshToken(refresh_token_);
|
| }
|
|
|
| void OAuth2LoginManager::OnOAuth2TokensFetchFailed() {
|
| @@ -165,8 +193,7 @@ void OAuth2LoginManager::RestoreSessionCookies() {
|
| }
|
|
|
| void OAuth2LoginManager::Shutdown() {
|
| - ProfileOAuth2TokenServiceFactory::GetForProfile(user_profile_)->
|
| - RemoveObserver(this);
|
| + GetTokenService()->RemoveObserver(this);
|
| login_verifier_.reset();
|
| oauth2_token_fetcher_.reset();
|
| }
|
| @@ -174,7 +201,9 @@ void OAuth2LoginManager::Shutdown() {
|
| void OAuth2LoginManager::OnOAuthLoginSuccess(
|
| const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) {
|
| LOG(INFO) << "OAuth2 refresh token successfully exchanged for GAIA token.";
|
| - StartTokenService(gaia_credentials);
|
| +
|
| + FOR_EACH_OBSERVER(Observer, observer_list_,
|
| + OnSessionAuthenticated(user_profile_));
|
| }
|
|
|
| void OAuth2LoginManager::OnOAuthLoginFailure(bool connection_error) {
|
| @@ -207,18 +236,6 @@ void OAuth2LoginManager::OnSessionMergeFailure(bool connection_error) {
|
| OAuth2LoginManager::SESSION_RESTORE_FAILED);
|
| }
|
|
|
| -void OAuth2LoginManager::StartTokenService(
|
| - const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) {
|
| - TokenService* token_service = SetupTokenService();
|
| - token_service->UpdateCredentials(gaia_credentials);
|
| -
|
| - FOR_EACH_OBSERVER(Observer, observer_list_,
|
| - OnSessionAuthenticated(user_profile_));
|
| -
|
| - if (token_service->AreCredentialsValid())
|
| - token_service->StartFetchingTokens();
|
| -}
|
| -
|
| void OAuth2LoginManager::SetSessionRestoreState(
|
| OAuth2LoginManager::SessionRestoreState state) {
|
| if (state_ == state)
|
|
|