| 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" |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 OAuth2TokenService::Consumer::Consumer() { | 308 OAuth2TokenService::Consumer::Consumer() { |
| 309 } | 309 } |
| 310 | 310 |
| 311 OAuth2TokenService::Consumer::~Consumer() { | 311 OAuth2TokenService::Consumer::~Consumer() { |
| 312 } | 312 } |
| 313 | 313 |
| 314 OAuth2TokenService::OAuth2TokenService() { | 314 OAuth2TokenService::OAuth2TokenService() { |
| 315 } | 315 } |
| 316 | 316 |
| 317 OAuth2TokenService::~OAuth2TokenService() { | 317 OAuth2TokenService::~OAuth2TokenService() { |
| 318 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 319 // Release all the pending fetchers. | 318 // Release all the pending fetchers. |
| 320 STLDeleteContainerPairSecondPointers( | 319 STLDeleteContainerPairSecondPointers( |
| 321 pending_fetchers_.begin(), pending_fetchers_.end()); | 320 pending_fetchers_.begin(), pending_fetchers_.end()); |
| 322 } | 321 } |
| 323 | 322 |
| 324 void OAuth2TokenService::AddObserver(Observer* observer) { | 323 void OAuth2TokenService::AddObserver(Observer* observer) { |
| 325 observer_list_.AddObserver(observer); | 324 observer_list_.AddObserver(observer); |
| 326 } | 325 } |
| 327 | 326 |
| 328 void OAuth2TokenService::RemoveObserver(Observer* observer) { | 327 void OAuth2TokenService::RemoveObserver(Observer* observer) { |
| 329 observer_list_.RemoveObserver(observer); | 328 observer_list_.RemoveObserver(observer); |
| 330 } | 329 } |
| 331 | 330 |
| 332 bool OAuth2TokenService::RefreshTokenIsAvailable() { | 331 bool OAuth2TokenService::RefreshTokenIsAvailable() { |
| 333 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 334 return !GetRefreshToken().empty(); | 332 return !GetRefreshToken().empty(); |
| 335 } | 333 } |
| 336 | 334 |
| 337 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( | 335 scoped_ptr<OAuth2TokenService::Request> OAuth2TokenService::StartRequest( |
| 338 const OAuth2TokenService::ScopeSet& scopes, | 336 const OAuth2TokenService::ScopeSet& scopes, |
| 339 OAuth2TokenService::Consumer* consumer) { | 337 OAuth2TokenService::Consumer* consumer) { |
| 340 return StartRequestForClientWithContext( | 338 return StartRequestForClientWithContext( |
| 341 GetRequestContext(), | 339 GetRequestContext(), |
| 342 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | 340 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 343 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | 341 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 OAuth2TokenService::StartRequestForClientWithContext( | 374 OAuth2TokenService::StartRequestForClientWithContext( |
| 377 net::URLRequestContextGetter* getter, | 375 net::URLRequestContextGetter* getter, |
| 378 const std::string& client_id, | 376 const std::string& client_id, |
| 379 const std::string& client_secret, | 377 const std::string& client_secret, |
| 380 const ScopeSet& scopes, | 378 const ScopeSet& scopes, |
| 381 Consumer* consumer) { | 379 Consumer* consumer) { |
| 382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 380 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 383 | 381 |
| 384 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 382 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); |
| 385 | 383 |
| 386 std::string refresh_token = GetRefreshToken(); | 384 if (!RefreshTokenIsAvailable()) { |
| 387 if (refresh_token.empty()) { | |
| 388 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 385 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 389 &RequestImpl::InformConsumer, | 386 &RequestImpl::InformConsumer, |
| 390 request->AsWeakPtr(), | 387 request->AsWeakPtr(), |
| 391 GoogleServiceAuthError( | 388 GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP), |
| 392 GoogleServiceAuthError::USER_NOT_SIGNED_UP), | |
| 393 std::string(), | 389 std::string(), |
| 394 base::Time())); | 390 base::Time())); |
| 395 return request.PassAs<Request>(); | 391 return request.PassAs<Request>(); |
| 396 } | 392 } |
| 397 | 393 |
| 398 if (HasCacheEntry(scopes)) | 394 if (HasCacheEntry(scopes)) { |
| 399 return StartCacheLookupRequest(scopes, consumer); | 395 StartCacheLookupRequest(request.get(), scopes, consumer); |
| 396 } else { |
| 397 FetchOAuth2Token(request.get(), |
| 398 getter, |
| 399 client_id, |
| 400 client_secret, |
| 401 scopes); |
| 402 } |
| 403 return request.PassAs<Request>(); |
| 404 } |
| 405 |
| 406 void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request, |
| 407 net::URLRequestContextGetter* getter, |
| 408 const std::string& client_id, |
| 409 const std::string& client_secret, |
| 410 const ScopeSet& scopes) { |
| 411 std::string refresh_token = GetRefreshToken(); |
| 400 | 412 |
| 401 // If there is already a pending fetcher for |scopes| and |refresh_token|, | 413 // If there is already a pending fetcher for |scopes| and |refresh_token|, |
| 402 // simply register this |request| for those results rather than starting | 414 // simply register this |request| for those results rather than starting |
| 403 // a new fetcher. | 415 // a new fetcher. |
| 404 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); | 416 FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); |
| 405 std::map<FetchParameters, Fetcher*>::iterator iter = | 417 std::map<FetchParameters, Fetcher*>::iterator iter = |
| 406 pending_fetchers_.find(fetch_parameters); | 418 pending_fetchers_.find(fetch_parameters); |
| 407 if (iter != pending_fetchers_.end()) { | 419 if (iter != pending_fetchers_.end()) { |
| 408 iter->second->AddWaitingRequest(request->AsWeakPtr()); | 420 iter->second->AddWaitingRequest(request->AsWeakPtr()); |
| 409 return request.PassAs<Request>(); | 421 return; |
| 410 } | 422 } |
| 411 | 423 |
| 412 pending_fetchers_[fetch_parameters] = | 424 pending_fetchers_[fetch_parameters] = |
| 413 Fetcher::CreateAndStart(this, | 425 Fetcher::CreateAndStart(this, |
| 414 getter, | 426 getter, |
| 415 client_id, | 427 client_id, |
| 416 client_secret, | 428 client_secret, |
| 417 refresh_token, | 429 refresh_token, |
| 418 scopes, | 430 scopes, |
| 419 request->AsWeakPtr()); | 431 request->AsWeakPtr()); |
| 420 return request.PassAs<Request>(); | |
| 421 } | 432 } |
| 422 | 433 |
| 423 scoped_ptr<OAuth2TokenService::Request> | 434 void OAuth2TokenService::StartCacheLookupRequest( |
| 424 OAuth2TokenService::StartCacheLookupRequest( | 435 RequestImpl* request, |
| 425 const OAuth2TokenService::ScopeSet& scopes, | 436 const OAuth2TokenService::ScopeSet& scopes, |
| 426 OAuth2TokenService::Consumer* consumer) { | 437 OAuth2TokenService::Consumer* consumer) { |
| 427 CHECK(HasCacheEntry(scopes)); | 438 CHECK(HasCacheEntry(scopes)); |
| 428 const CacheEntry* cache_entry = GetCacheEntry(scopes); | 439 const CacheEntry* cache_entry = GetCacheEntry(scopes); |
| 429 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | |
| 430 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 440 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 431 &RequestImpl::InformConsumer, | 441 &RequestImpl::InformConsumer, |
| 432 request->AsWeakPtr(), | 442 request->AsWeakPtr(), |
| 433 GoogleServiceAuthError(GoogleServiceAuthError::NONE), | 443 GoogleServiceAuthError(GoogleServiceAuthError::NONE), |
| 434 cache_entry->access_token, | 444 cache_entry->access_token, |
| 435 cache_entry->expiration_date)); | 445 cache_entry->expiration_date)); |
| 436 return request.PassAs<Request>(); | |
| 437 } | 446 } |
| 438 | 447 |
| 439 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, | 448 void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, |
| 440 const std::string& invalid_token) { | 449 const std::string& invalid_token) { |
| 441 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 450 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 442 RemoveCacheEntry(scopes, invalid_token); | 451 RemoveCacheEntry(scopes, invalid_token); |
| 443 } | 452 } |
| 444 | 453 |
| 445 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { | 454 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { |
| 446 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 455 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 | 602 |
| 594 int OAuth2TokenService::cache_size_for_testing() const { | 603 int OAuth2TokenService::cache_size_for_testing() const { |
| 595 return token_cache_.size(); | 604 return token_cache_.size(); |
| 596 } | 605 } |
| 597 | 606 |
| 598 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( | 607 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( |
| 599 int max_retries) { | 608 int max_retries) { |
| 600 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 609 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 601 max_fetch_retry_num_ = max_retries; | 610 max_fetch_retry_num_ = max_retries; |
| 602 } | 611 } |
| OLD | NEW |