Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "chrome/browser/signin/oauth2_token_service.h" | 5 #include "chrome/browser/signin/oauth2_token_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "google_apis/gaia/gaia_urls.h" | 17 #include "google_apis/gaia/gaia_urls.h" |
| 18 #include "google_apis/gaia/google_service_auth_error.h" | 18 #include "google_apis/gaia/google_service_auth_error.h" |
| 19 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 19 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 20 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 20 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 21 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 22 | 22 |
| 23 namespace { | 23 int OAuth2TokenService::max_fetch_retry_num_ = 5; |
| 24 | |
| 25 // Maximum number of retries in fetching an OAuth2 access token. | |
| 26 const int kMaxFetchRetryNum = 5; | |
| 27 | |
| 28 // Returns an exponential backoff in milliseconds including randomness less than | |
| 29 // 1000 ms when retrying fetching an OAuth2 access token. | |
| 30 int64 ComputeExponentialBackOffMilliseconds(int retry_num) { | |
| 31 DCHECK(retry_num < kMaxFetchRetryNum); | |
| 32 int64 exponential_backoff_in_seconds = 1 << retry_num; | |
| 33 // Returns a backoff with randomness < 1000ms | |
| 34 return (exponential_backoff_in_seconds + base::RandDouble()) * 1000; | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | 24 |
| 39 OAuth2TokenService::RequestImpl::RequestImpl( | 25 OAuth2TokenService::RequestImpl::RequestImpl( |
| 40 OAuth2TokenService::Consumer* consumer) | 26 OAuth2TokenService::Consumer* consumer) |
| 41 : consumer_(consumer) { | 27 : consumer_(consumer) { |
| 42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 28 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 43 } | 29 } |
| 44 | 30 |
| 45 OAuth2TokenService::RequestImpl::~RequestImpl() { | 31 OAuth2TokenService::RequestImpl::~RequestImpl() { |
| 46 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 32 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 47 } | 33 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 98 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 113 | 99 |
| 114 private: | 100 private: |
| 115 Fetcher(OAuth2TokenService* oauth2_token_service, | 101 Fetcher(OAuth2TokenService* oauth2_token_service, |
| 116 net::URLRequestContextGetter* getter, | 102 net::URLRequestContextGetter* getter, |
| 117 const std::string& refresh_token, | 103 const std::string& refresh_token, |
| 118 const OAuth2TokenService::ScopeSet& scopes, | 104 const OAuth2TokenService::ScopeSet& scopes, |
| 119 base::WeakPtr<RequestImpl> waiting_request); | 105 base::WeakPtr<RequestImpl> waiting_request); |
| 120 void Start(); | 106 void Start(); |
| 121 void InformWaitingRequests(); | 107 void InformWaitingRequests(); |
| 108 void InformWaitingRequestsAndQuit(); | |
| 122 static bool ShouldRetry(const GoogleServiceAuthError& error); | 109 static bool ShouldRetry(const GoogleServiceAuthError& error); |
| 110 int64 ComputeExponentialBackOffMilliseconds(int retry_num); | |
| 123 | 111 |
| 124 // |oauth2_token_service_| remains valid for the life of this Fetcher, since | 112 // |oauth2_token_service_| remains valid for the life of this Fetcher, since |
| 125 // this Fetcher is destructed in the dtor of the OAuth2TokenService or is | 113 // this Fetcher is destructed in the dtor of the OAuth2TokenService or is |
| 126 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess | 114 // scheduled for deletion at the end of OnGetTokenFailure/OnGetTokenSuccess |
| 127 // (whichever comes first). | 115 // (whichever comes first). |
| 128 OAuth2TokenService* const oauth2_token_service_; | 116 OAuth2TokenService* const oauth2_token_service_; |
| 129 scoped_refptr<net::URLRequestContextGetter> getter_; | 117 scoped_refptr<net::URLRequestContextGetter> getter_; |
| 130 const std::string refresh_token_; | 118 const std::string refresh_token_; |
| 131 const OAuth2TokenService::ScopeSet scopes_; | 119 const OAuth2TokenService::ScopeSet scopes_; |
| 132 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; | 120 std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 expiration_date_ = expiration_date; | 190 expiration_date_ = expiration_date; |
| 203 | 191 |
| 204 // Subclasses may override this method to skip caching in some cases, but | 192 // Subclasses may override this method to skip caching in some cases, but |
| 205 // we still inform all waiting Consumers of a successful token fetch below. | 193 // we still inform all waiting Consumers of a successful token fetch below. |
| 206 // This is intentional -- some consumers may need the token for cleanup | 194 // This is intentional -- some consumers may need the token for cleanup |
| 207 // tasks. https://chromiumcodereview.appspot.com/11312124/ | 195 // tasks. https://chromiumcodereview.appspot.com/11312124/ |
| 208 oauth2_token_service_->RegisterCacheEntry(refresh_token_, | 196 oauth2_token_service_->RegisterCacheEntry(refresh_token_, |
| 209 scopes_, | 197 scopes_, |
| 210 access_token_, | 198 access_token_, |
| 211 expiration_date_); | 199 expiration_date_); |
| 212 // Deregisters itself from the service to prevent more waiting requests to | 200 InformWaitingRequestsAndQuit(); |
|
Andrew T Wilson (Slow)
2013/06/20 17:47:28
"AndQuit" -> "AndDelete" since "Quit" doesn't mean
David Roche
2013/06/20 18:17:18
Done.
| |
| 213 // be added when it calls back the waiting requests. | |
| 214 oauth2_token_service_->OnFetchComplete(this); | |
| 215 InformWaitingRequests(); | |
| 216 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 217 } | 201 } |
| 218 | 202 |
| 219 void OAuth2TokenService::Fetcher::OnGetTokenFailure( | 203 void OAuth2TokenService::Fetcher::OnGetTokenFailure( |
| 220 const GoogleServiceAuthError& error) { | 204 const GoogleServiceAuthError& error) { |
| 221 fetcher_.reset(); | 205 fetcher_.reset(); |
| 222 | 206 |
| 223 if (ShouldRetry(error) && retry_number_ < kMaxFetchRetryNum) { | 207 if (ShouldRetry(error) && retry_number_ < max_fetch_retry_num_) { |
| 224 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_); | 208 int64 backoff = ComputeExponentialBackOffMilliseconds(retry_number_); |
| 225 ++retry_number_; | 209 ++retry_number_; |
| 226 retry_timer_.Stop(); | 210 retry_timer_.Stop(); |
| 227 retry_timer_.Start(FROM_HERE, | 211 retry_timer_.Start(FROM_HERE, |
| 228 base::TimeDelta::FromMilliseconds(backoff), | 212 base::TimeDelta::FromMilliseconds(backoff), |
| 229 this, | 213 this, |
| 230 &OAuth2TokenService::Fetcher::Start); | 214 &OAuth2TokenService::Fetcher::Start); |
| 231 return; | 215 return; |
| 232 } | 216 } |
| 233 | 217 |
| 234 // Fetch completes. | |
| 235 error_ = error; | 218 error_ = error; |
| 219 InformWaitingRequestsAndQuit(); | |
| 220 } | |
| 236 | 221 |
| 237 // Deregisters itself from the service to prevent more waiting requests to be | 222 // Returns an exponential backoff in milliseconds including randomness less than |
| 238 // added when it calls back the waiting requests. | 223 // 1000 ms when retrying fetching an OAuth2 access token. |
| 239 oauth2_token_service_->OnFetchComplete(this); | 224 int64 OAuth2TokenService::Fetcher::ComputeExponentialBackOffMilliseconds( |
| 240 InformWaitingRequests(); | 225 int retry_num) { |
| 241 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 226 DCHECK(retry_num < max_fetch_retry_num_); |
| 227 int64 exponential_backoff_in_seconds = 1 << retry_num; | |
| 228 // Returns a backoff with randomness < 1000ms | |
| 229 return (exponential_backoff_in_seconds + base::RandDouble()) * 1000; | |
| 242 } | 230 } |
| 243 | 231 |
| 244 // static | 232 // static |
| 245 bool OAuth2TokenService::Fetcher::ShouldRetry( | 233 bool OAuth2TokenService::Fetcher::ShouldRetry( |
| 246 const GoogleServiceAuthError& error) { | 234 const GoogleServiceAuthError& error) { |
| 247 GoogleServiceAuthError::State error_state = error.state(); | 235 GoogleServiceAuthError::State error_state = error.state(); |
| 248 return error_state == GoogleServiceAuthError::CONNECTION_FAILED || | 236 return error_state == GoogleServiceAuthError::CONNECTION_FAILED || |
| 249 error_state == GoogleServiceAuthError::REQUEST_CANCELED || | 237 error_state == GoogleServiceAuthError::REQUEST_CANCELED || |
| 250 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE; | 238 error_state == GoogleServiceAuthError::SERVICE_UNAVAILABLE; |
| 251 } | 239 } |
| 252 | 240 |
| 253 void OAuth2TokenService::Fetcher::InformWaitingRequests() { | 241 void OAuth2TokenService::Fetcher::InformWaitingRequests() { |
| 254 std::vector<base::WeakPtr<RequestImpl> >::const_iterator iter = | 242 std::vector<base::WeakPtr<RequestImpl> >::const_iterator iter = |
| 255 waiting_requests_.begin(); | 243 waiting_requests_.begin(); |
| 256 for (; iter != waiting_requests_.end(); ++iter) { | 244 for (; iter != waiting_requests_.end(); ++iter) { |
| 257 base::WeakPtr<RequestImpl> waiting_request = *iter; | 245 base::WeakPtr<RequestImpl> waiting_request = *iter; |
| 258 if (waiting_request.get()) | 246 if (waiting_request.get()) |
| 259 waiting_request->InformConsumer(error_, access_token_, expiration_date_); | 247 waiting_request->InformConsumer(error_, access_token_, expiration_date_); |
| 260 } | 248 } |
| 261 waiting_requests_.clear(); | 249 waiting_requests_.clear(); |
| 262 } | 250 } |
| 263 | 251 |
| 252 void OAuth2TokenService::Fetcher::InformWaitingRequestsAndQuit() { | |
| 253 // Deregisters itself from the service to prevent more waiting requests to | |
| 254 // be added when it calls back the waiting requests. | |
| 255 oauth2_token_service_->OnFetchComplete(this); | |
| 256 InformWaitingRequests(); | |
| 257 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 258 } | |
| 259 | |
| 264 void OAuth2TokenService::Fetcher::AddWaitingRequest( | 260 void OAuth2TokenService::Fetcher::AddWaitingRequest( |
| 265 base::WeakPtr<OAuth2TokenService::RequestImpl> waiting_request) { | 261 base::WeakPtr<OAuth2TokenService::RequestImpl> waiting_request) { |
| 266 waiting_requests_.push_back(waiting_request); | 262 waiting_requests_.push_back(waiting_request); |
| 267 } | 263 } |
| 268 | 264 |
| 269 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() | 265 const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() |
| 270 const { | 266 const { |
| 271 return scopes_; | 267 return scopes_; |
| 272 } | 268 } |
| 273 | 269 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 291 : request_context_getter_(getter) { | 287 : request_context_getter_(getter) { |
| 292 } | 288 } |
| 293 | 289 |
| 294 OAuth2TokenService::~OAuth2TokenService() { | 290 OAuth2TokenService::~OAuth2TokenService() { |
| 295 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 291 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 296 // Release all the pending fetchers. | 292 // Release all the pending fetchers. |
| 297 STLDeleteContainerPairSecondPointers( | 293 STLDeleteContainerPairSecondPointers( |
| 298 pending_fetchers_.begin(), pending_fetchers_.end()); | 294 pending_fetchers_.begin(), pending_fetchers_.end()); |
| 299 } | 295 } |
| 300 | 296 |
| 301 bool OAuth2TokenService::RefreshTokenIsAvailable() { | |
| 302 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 303 return !GetRefreshToken().empty(); | |
| 304 } | |
| 305 | |
| 306 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( | 297 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( |
| 307 const OAuth2TokenService::ScopeSet& scopes, | 298 const OAuth2TokenService::ScopeSet& scopes, |
| 308 OAuth2TokenService::Consumer* consumer) { | 299 OAuth2TokenService::Consumer* consumer) { |
| 309 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 310 | 301 |
| 311 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 302 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); |
| 312 | 303 |
| 313 std::string refresh_token = GetRefreshToken(); | 304 std::string refresh_token = GetRefreshToken(); |
| 314 if (refresh_token.empty()) { | 305 if (refresh_token.empty()) { |
| 315 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 306 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 316 &RequestImpl::InformConsumer, | 307 &RequestImpl::InformConsumer, |
| 317 request->AsWeakPtr(), | 308 request->AsWeakPtr(), |
| 318 GoogleServiceAuthError( | 309 GoogleServiceAuthError( |
| 319 GoogleServiceAuthError::USER_NOT_SIGNED_UP), | 310 GoogleServiceAuthError::USER_NOT_SIGNED_UP), |
| 320 std::string(), | 311 std::string(), |
| 321 base::Time())); | 312 base::Time())); |
| 322 return request.PassAs<Request>(); | 313 return request.PassAs<Request>(); |
| 323 } | 314 } |
| 324 | 315 |
| 325 if (HasCacheEntry(scopes)) | 316 if (HasCacheEntry(scopes)) |
| 326 return StartCacheLookupRequest(scopes, consumer); | 317 return StartCacheLookupRequest(scopes, request.Pass()); |
| 327 | 318 |
| 328 // Makes sure there is a pending fetcher for |scopes| and |refresh_token|. | 319 // If there is already a pending fetcher for |scopes| and |refresh_token|, |
| 329 // Adds |request| to the waiting request list of this fetcher so |request| | 320 // simply register this |request| for those results rather than starting |
| 330 // will be called back when this fetcher finishes fetching. | 321 // a new fetcher. |
| 331 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); | 322 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); |
| 332 std::map<FetchParameters, Fetcher*>::iterator iter = | 323 std::map<FetchParameters, Fetcher*>::iterator iter = |
| 333 pending_fetchers_.find(fetch_parameters); | 324 pending_fetchers_.find(fetch_parameters); |
| 334 if (iter != pending_fetchers_.end()) { | 325 if (iter != pending_fetchers_.end()) { |
| 335 iter->second->AddWaitingRequest(request->AsWeakPtr()); | 326 iter->second->AddWaitingRequest(request->AsWeakPtr()); |
| 336 return request.PassAs<Request>(); | 327 return request.PassAs<Request>(); |
| 337 } | 328 } |
| 329 | |
| 338 pending_fetchers_[fetch_parameters] = | 330 pending_fetchers_[fetch_parameters] = |
| 339 Fetcher::CreateAndStart(this, | 331 Fetcher::CreateAndStart(this, |
| 340 request_context_getter_.get(), | 332 request_context_getter_.get(), |
| 341 refresh_token, | 333 refresh_token, |
| 342 scopes, | 334 scopes, |
| 343 request->AsWeakPtr()); | 335 request->AsWeakPtr()); |
| 344 return request.PassAs<Request>(); | 336 return request.PassAs<Request>(); |
| 345 } | 337 } |
| 346 | 338 |
| 347 scoped_ptr<OAuth2TokenService::Request> | 339 scoped_ptr<OAuth2TokenService::Request> |
| 348 OAuth2TokenService::StartCacheLookupRequest( | 340 OAuth2TokenService::StartCacheLookupRequest( |
| 349 const OAuth2TokenService::ScopeSet& scopes, | 341 const OAuth2TokenService::ScopeSet& scopes, |
| 350 OAuth2TokenService::Consumer* consumer) { | 342 scoped_ptr<RequestImpl> request) { |
| 351 CHECK(HasCacheEntry(scopes)); | 343 CHECK(HasCacheEntry(scopes)); |
| 352 const CacheEntry* cache_entry = GetCacheEntry(scopes); | 344 const CacheEntry* cache_entry = GetCacheEntry(scopes); |
| 353 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | |
| 354 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 345 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 355 &RequestImpl::InformConsumer, | 346 &RequestImpl::InformConsumer, |
| 356 request->AsWeakPtr(), | 347 request->AsWeakPtr(), |
| 357 GoogleServiceAuthError(GoogleServiceAuthError::NONE), | 348 GoogleServiceAuthError(GoogleServiceAuthError::NONE), |
| 358 cache_entry->access_token, | 349 cache_entry->access_token, |
| 359 cache_entry->expiration_date)); | 350 cache_entry->expiration_date)); |
| 360 return request.PassAs<Request>(); | 351 return request.PassAs<Request>(); |
| 361 } | 352 } |
| 362 | 353 |
| 363 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, | 354 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 } | 447 } |
| 457 | 448 |
| 458 void OAuth2TokenService::ClearCache() { | 449 void OAuth2TokenService::ClearCache() { |
| 459 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 450 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 460 token_cache_.clear(); | 451 token_cache_.clear(); |
| 461 } | 452 } |
| 462 | 453 |
| 463 int OAuth2TokenService::cache_size_for_testing() const { | 454 int OAuth2TokenService::cache_size_for_testing() const { |
| 464 return token_cache_.size(); | 455 return token_cache_.size(); |
| 465 } | 456 } |
| 457 | |
| 458 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( | |
| 459 int max_retries) { | |
| 460 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
Andrew T Wilson (Slow)
2013/06/20 17:47:28
See my previous comment - would rather see this in
| |
| 461 max_fetch_retry_num_ = max_retries; | |
| 462 } | |
| OLD | NEW |