OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "google_apis/gaia/ubertoken_fetcher.h" | 5 #include "google_apis/gaia/ubertoken_fetcher.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/rand_util.h" |
| 11 #include "base/time/time.h" |
10 #include "google_apis/gaia/gaia_auth_fetcher.h" | 12 #include "google_apis/gaia/gaia_auth_fetcher.h" |
11 #include "google_apis/gaia/gaia_constants.h" | 13 #include "google_apis/gaia/gaia_constants.h" |
12 #include "google_apis/gaia/google_service_auth_error.h" | 14 #include "google_apis/gaia/google_service_auth_error.h" |
13 #include "google_apis/gaia/oauth2_token_service.h" | 15 #include "google_apis/gaia/oauth2_token_service.h" |
14 | 16 |
| 17 const int UbertokenFetcher::kMaxRetries = 3; |
| 18 |
15 UbertokenFetcher::UbertokenFetcher( | 19 UbertokenFetcher::UbertokenFetcher( |
16 OAuth2TokenService* token_service, | 20 OAuth2TokenService* token_service, |
17 UbertokenConsumer* consumer, | 21 UbertokenConsumer* consumer, |
18 net::URLRequestContextGetter* request_context) | 22 net::URLRequestContextGetter* request_context) |
19 : OAuth2TokenService::Consumer("uber_token_fetcher"), | 23 : OAuth2TokenService::Consumer("uber_token_fetcher"), |
20 token_service_(token_service), | 24 token_service_(token_service), |
21 consumer_(consumer), | 25 consumer_(consumer), |
22 request_context_(request_context) { | 26 request_context_(request_context), |
| 27 retry_number_(0), |
| 28 second_access_token_request_(false) { |
23 DCHECK(token_service); | 29 DCHECK(token_service); |
24 DCHECK(consumer); | 30 DCHECK(consumer); |
25 DCHECK(request_context); | 31 DCHECK(request_context); |
26 } | 32 } |
27 | 33 |
28 UbertokenFetcher::~UbertokenFetcher() { | 34 UbertokenFetcher::~UbertokenFetcher() { |
29 } | 35 } |
30 | 36 |
31 void UbertokenFetcher::StartFetchingToken(const std::string& account_id) { | 37 void UbertokenFetcher::StartFetchingToken(const std::string& account_id) { |
32 OAuth2TokenService::ScopeSet scopes; | 38 DCHECK(!account_id.empty()); |
33 scopes.insert(GaiaConstants::kOAuth1LoginScope); | 39 account_id_ = account_id; |
34 access_token_request_ = | 40 second_access_token_request_ = false; |
35 token_service_->StartRequest(account_id, scopes, this); | 41 RequestAccessToken(); |
36 } | 42 } |
37 | 43 |
38 void UbertokenFetcher::OnUberAuthTokenSuccess(const std::string& token) { | 44 void UbertokenFetcher::OnUberAuthTokenSuccess(const std::string& token) { |
39 consumer_->OnUbertokenSuccess(token); | 45 consumer_->OnUbertokenSuccess(token); |
40 } | 46 } |
41 | 47 |
42 void UbertokenFetcher::OnUberAuthTokenFailure( | 48 void UbertokenFetcher::OnUberAuthTokenFailure( |
43 const GoogleServiceAuthError& error) { | 49 const GoogleServiceAuthError& error) { |
| 50 // Retry only transient errors. |
| 51 bool should_retry = |
| 52 error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
| 53 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE; |
| 54 if (should_retry) { |
| 55 if (retry_number_ < kMaxRetries) { |
| 56 // Calculate an exponential backoff with randomness of less than 1 sec. |
| 57 double backoff = base::RandDouble() + (1 << retry_number_); |
| 58 ++retry_number_; |
| 59 retry_timer_.Stop(); |
| 60 retry_timer_.Start(FROM_HERE, |
| 61 base::TimeDelta::FromSecondsD(backoff), |
| 62 this, |
| 63 &UbertokenFetcher::ExchangeTokens); |
| 64 return; |
| 65 } |
| 66 } else { |
| 67 // The access token is invalid. Tell the token service. |
| 68 OAuth2TokenService::ScopeSet scopes; |
| 69 scopes.insert(GaiaConstants::kOAuth1LoginScope); |
| 70 token_service_->InvalidateToken(account_id_, scopes, access_token_); |
| 71 |
| 72 // In case the access was just stale, try one more time. |
| 73 if (!second_access_token_request_) { |
| 74 second_access_token_request_ = true; |
| 75 RequestAccessToken(); |
| 76 return; |
| 77 } |
| 78 } |
| 79 |
44 consumer_->OnUbertokenFailure(error); | 80 consumer_->OnUbertokenFailure(error); |
45 } | 81 } |
46 | 82 |
47 void UbertokenFetcher::OnGetTokenSuccess( | 83 void UbertokenFetcher::OnGetTokenSuccess( |
48 const OAuth2TokenService::Request* request, | 84 const OAuth2TokenService::Request* request, |
49 const std::string& access_token, | 85 const std::string& access_token, |
50 const base::Time& expiration_time) { | 86 const base::Time& expiration_time) { |
| 87 DCHECK(!access_token.empty()); |
| 88 access_token_ = access_token; |
51 access_token_request_.reset(); | 89 access_token_request_.reset(); |
52 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, | 90 ExchangeTokens(); |
53 GaiaConstants::kChromeSource, | |
54 request_context_)); | |
55 gaia_auth_fetcher_->StartTokenFetchForUberAuthExchange(access_token); | |
56 } | 91 } |
57 | 92 |
58 void UbertokenFetcher::OnGetTokenFailure( | 93 void UbertokenFetcher::OnGetTokenFailure( |
59 const OAuth2TokenService::Request* request, | 94 const OAuth2TokenService::Request* request, |
60 const GoogleServiceAuthError& error) { | 95 const GoogleServiceAuthError& error) { |
61 access_token_request_.reset(); | 96 access_token_request_.reset(); |
62 consumer_->OnUbertokenFailure(error); | 97 consumer_->OnUbertokenFailure(error); |
63 } | 98 } |
| 99 |
| 100 void UbertokenFetcher::RequestAccessToken() { |
| 101 retry_number_ = 0; |
| 102 gaia_auth_fetcher_.reset(); |
| 103 retry_timer_.Stop(); |
| 104 |
| 105 OAuth2TokenService::ScopeSet scopes; |
| 106 scopes.insert(GaiaConstants::kOAuth1LoginScope); |
| 107 access_token_request_ = |
| 108 token_service_->StartRequest(account_id_, scopes, this); |
| 109 } |
| 110 |
| 111 void UbertokenFetcher::ExchangeTokens() { |
| 112 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, |
| 113 GaiaConstants::kChromeSource, |
| 114 request_context_)); |
| 115 gaia_auth_fetcher_->StartTokenFetchForUberAuthExchange(access_token_); |
| 116 } |
OLD | NEW |