OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "base/stringprintf.h" |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/signin/token_service.h" |
| 9 #include "chrome/browser/signin/ubertoken_fetcher.h" |
| 10 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "chrome/common/net/gaia/gaia_constants.h" |
| 12 #include "chrome/common/net/gaia/gaia_urls.h" |
| 13 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 14 #include "net/base/load_flags.h" |
| 15 |
| 16 UbertokenFetcher::UbertokenFetcher(Profile* profile, |
| 17 UbertokenConsumer* consumer) |
| 18 : profile_(profile), consumer_(consumer) { |
| 19 DCHECK(profile); |
| 20 DCHECK(consumer); |
| 21 } |
| 22 |
| 23 UbertokenFetcher::~UbertokenFetcher() { |
| 24 } |
| 25 |
| 26 void UbertokenFetcher::StartFetchingToken() { |
| 27 TokenService* token_service = profile_->GetTokenService(); |
| 28 if (token_service->HasOAuthLoginToken()) { |
| 29 StartFetchingUbertoken(); |
| 30 } else { |
| 31 registrar_.Add(this, |
| 32 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 33 content::Source<TokenService>(token_service)); |
| 34 registrar_.Add(this, |
| 35 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
| 36 content::Source<TokenService>(token_service)); |
| 37 token_service->StartFetchingTokens(); |
| 38 } |
| 39 } |
| 40 |
| 41 void UbertokenFetcher::StartFetchingUbertoken() { |
| 42 TokenService* token_service = profile_->GetTokenService(); |
| 43 DCHECK(token_service->HasOAuthLoginToken()); |
| 44 gaia::OAuthClientInfo client_info; |
| 45 GaiaUrls* urls = GaiaUrls::GetInstance(); |
| 46 client_info.client_id = urls->oauth2_chrome_client_id(); |
| 47 client_info.client_secret = urls->oauth2_chrome_client_secret(); |
| 48 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( |
| 49 urls->oauth2_token_url(), profile_->GetRequestContext())); |
| 50 gaia_oauth_client_->RefreshToken( |
| 51 client_info, token_service->GetOAuth2LoginRefreshToken(), 1, this); |
| 52 } |
| 53 |
| 54 void UbertokenFetcher::Observe(int type, |
| 55 const content::NotificationSource& source, |
| 56 const content::NotificationDetails& details) { |
| 57 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE || |
| 58 type == chrome::NOTIFICATION_TOKEN_REQUEST_FAILED); |
| 59 |
| 60 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { |
| 61 TokenService::TokenAvailableDetails* token_details = |
| 62 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); |
| 63 if (token_details->service() != |
| 64 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
| 65 return; |
| 66 } |
| 67 registrar_.RemoveAll(); |
| 68 StartFetchingUbertoken(); |
| 69 } else { |
| 70 TokenService::TokenRequestFailedDetails* token_details = |
| 71 content::Details<TokenService::TokenRequestFailedDetails>(details). |
| 72 ptr(); |
| 73 if (token_details->service() == GaiaConstants::kLSOService || |
| 74 token_details->service() == |
| 75 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
| 76 consumer_->OnUbertokenFailure(token_details->error()); |
| 77 } |
| 78 } |
| 79 } |
| 80 |
| 81 void UbertokenFetcher::OnGetTokensResponse(const std::string& refresh_token, |
| 82 const std::string& access_token, |
| 83 int expires_in_seconds) { |
| 84 NOTREACHED(); |
| 85 } |
| 86 |
| 87 void UbertokenFetcher::OnRefreshTokenResponse(const std::string& access_token, |
| 88 int expires_in_seconds) { |
| 89 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, |
| 90 GaiaConstants::kChromeSource, |
| 91 profile_->GetRequestContext())); |
| 92 gaia_auth_fetcher_->StartUberAuthTokenFetch(access_token); |
| 93 } |
| 94 |
| 95 void UbertokenFetcher::OnOAuthError() { |
| 96 GoogleServiceAuthError error( |
| 97 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 98 consumer_->OnUbertokenFailure(error); |
| 99 } |
| 100 |
| 101 void UbertokenFetcher::OnNetworkError(int response_code) { |
| 102 GoogleServiceAuthError error = |
| 103 GoogleServiceAuthError::FromConnectionError(response_code); |
| 104 consumer_->OnUbertokenFailure(error); |
| 105 } |
| 106 |
| 107 void UbertokenFetcher::OnUberAuthTokenSuccess(const std::string& token) { |
| 108 consumer_->OnUbertokenSuccess(token); |
| 109 } |
| 110 |
| 111 void UbertokenFetcher::OnUberAuthTokenFailure( |
| 112 const GoogleServiceAuthError& error) { |
| 113 consumer_->OnUbertokenFailure(error); |
| 114 } |
OLD | NEW |