| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/oauth2_token_service.h" | 5 #include "google_apis/gaia/oauth2_token_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 } | 59 } |
| 60 | 60 |
| 61 OAuth2TokenService::RequestImpl::RequestImpl( | 61 OAuth2TokenService::RequestImpl::RequestImpl( |
| 62 const std::string& account_id, | 62 const std::string& account_id, |
| 63 OAuth2TokenService::Consumer* consumer) | 63 OAuth2TokenService::Consumer* consumer) |
| 64 : account_id_(account_id), | 64 : account_id_(account_id), |
| 65 consumer_(consumer) { | 65 consumer_(consumer) { |
| 66 } | 66 } |
| 67 | 67 |
| 68 OAuth2TokenService::RequestImpl::~RequestImpl() { | 68 OAuth2TokenService::RequestImpl::~RequestImpl() { |
| 69 DCHECK(CalledOnValidThread()); | 69 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 70 } | 70 } |
| 71 | 71 |
| 72 std::string OAuth2TokenService::RequestImpl::GetAccountId() const { | 72 std::string OAuth2TokenService::RequestImpl::GetAccountId() const { |
| 73 return account_id_; | 73 return account_id_; |
| 74 } | 74 } |
| 75 | 75 |
| 76 std::string OAuth2TokenService::RequestImpl::GetConsumerId() const { | 76 std::string OAuth2TokenService::RequestImpl::GetConsumerId() const { |
| 77 return consumer_->id(); | 77 return consumer_->id(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void OAuth2TokenService::RequestImpl::InformConsumer( | 80 void OAuth2TokenService::RequestImpl::InformConsumer( |
| 81 const GoogleServiceAuthError& error, | 81 const GoogleServiceAuthError& error, |
| 82 const std::string& access_token, | 82 const std::string& access_token, |
| 83 const base::Time& expiration_date) { | 83 const base::Time& expiration_date) { |
| 84 DCHECK(CalledOnValidThread()); | 84 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 85 if (error.state() == GoogleServiceAuthError::NONE) | 85 if (error.state() == GoogleServiceAuthError::NONE) |
| 86 consumer_->OnGetTokenSuccess(this, access_token, expiration_date); | 86 consumer_->OnGetTokenSuccess(this, access_token, expiration_date); |
| 87 else | 87 else |
| 88 consumer_->OnGetTokenFailure(this, error); | 88 consumer_->OnGetTokenFailure(this, error); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Class that fetches an OAuth2 access token for a given account id and set of | 91 // Class that fetches an OAuth2 access token for a given account id and set of |
| 92 // scopes. | 92 // scopes. |
| 93 // | 93 // |
| 94 // It aims to meet OAuth2TokenService's requirements on token fetching. Retry | 94 // It aims to meet OAuth2TokenService's requirements on token fetching. Retry |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 OAuth2TokenService::Consumer::~Consumer() { | 388 OAuth2TokenService::Consumer::~Consumer() { |
| 389 } | 389 } |
| 390 | 390 |
| 391 OAuth2TokenService::OAuth2TokenService( | 391 OAuth2TokenService::OAuth2TokenService( |
| 392 std::unique_ptr<OAuth2TokenServiceDelegate> delegate) | 392 std::unique_ptr<OAuth2TokenServiceDelegate> delegate) |
| 393 : delegate_(std::move(delegate)) { | 393 : delegate_(std::move(delegate)) { |
| 394 DCHECK(delegate_); | 394 DCHECK(delegate_); |
| 395 } | 395 } |
| 396 | 396 |
| 397 OAuth2TokenService::~OAuth2TokenService() { | 397 OAuth2TokenService::~OAuth2TokenService() { |
| 398 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 398 // Release all the pending fetchers. | 399 // Release all the pending fetchers. |
| 399 pending_fetchers_.clear(); | 400 pending_fetchers_.clear(); |
| 400 } | 401 } |
| 401 | 402 |
| 402 OAuth2TokenServiceDelegate* OAuth2TokenService::GetDelegate() { | 403 OAuth2TokenServiceDelegate* OAuth2TokenService::GetDelegate() { |
| 403 return delegate_.get(); | 404 return delegate_.get(); |
| 404 } | 405 } |
| 405 | 406 |
| 406 const OAuth2TokenServiceDelegate* OAuth2TokenService::GetDelegate() const { | 407 const OAuth2TokenServiceDelegate* OAuth2TokenService::GetDelegate() const { |
| 407 return delegate_.get(); | 408 return delegate_.get(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 } | 474 } |
| 474 | 475 |
| 475 std::unique_ptr<OAuth2TokenService::Request> | 476 std::unique_ptr<OAuth2TokenService::Request> |
| 476 OAuth2TokenService::StartRequestForClientWithContext( | 477 OAuth2TokenService::StartRequestForClientWithContext( |
| 477 const std::string& account_id, | 478 const std::string& account_id, |
| 478 net::URLRequestContextGetter* getter, | 479 net::URLRequestContextGetter* getter, |
| 479 const std::string& client_id, | 480 const std::string& client_id, |
| 480 const std::string& client_secret, | 481 const std::string& client_secret, |
| 481 const ScopeSet& scopes, | 482 const ScopeSet& scopes, |
| 482 Consumer* consumer) { | 483 Consumer* consumer) { |
| 483 DCHECK(CalledOnValidThread()); | 484 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 484 | 485 |
| 485 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is | 486 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is |
| 486 // fixed. | 487 // fixed. |
| 487 tracked_objects::ScopedTracker tracking_profile1( | 488 tracked_objects::ScopedTracker tracking_profile1( |
| 488 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 489 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 489 "422460 OAuth2TokenService::StartRequestForClientWithContext 1")); | 490 "422460 OAuth2TokenService::StartRequestForClientWithContext 1")); |
| 490 std::unique_ptr<RequestImpl> request(new RequestImpl(account_id, consumer)); | 491 std::unique_ptr<RequestImpl> request(new RequestImpl(account_id, consumer)); |
| 491 for (auto& observer : diagnostics_observer_list_) | 492 for (auto& observer : diagnostics_observer_list_) |
| 492 observer.OnAccessTokenRequested(account_id, consumer->id(), scopes); | 493 observer.OnAccessTokenRequested(account_id, consumer->id(), scopes); |
| 493 | 494 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 const ScopeSet& scopes, | 625 const ScopeSet& scopes, |
| 625 const std::string& access_token) { | 626 const std::string& access_token) { |
| 626 InvalidateAccessTokenImpl(account_id, client_id, scopes, access_token); | 627 InvalidateAccessTokenImpl(account_id, client_id, scopes, access_token); |
| 627 } | 628 } |
| 628 | 629 |
| 629 void OAuth2TokenService::InvalidateAccessTokenImpl( | 630 void OAuth2TokenService::InvalidateAccessTokenImpl( |
| 630 const std::string& account_id, | 631 const std::string& account_id, |
| 631 const std::string& client_id, | 632 const std::string& client_id, |
| 632 const ScopeSet& scopes, | 633 const ScopeSet& scopes, |
| 633 const std::string& access_token) { | 634 const std::string& access_token) { |
| 634 DCHECK(CalledOnValidThread()); | 635 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 635 RemoveCacheEntry( | 636 RemoveCacheEntry( |
| 636 RequestParameters(client_id, | 637 RequestParameters(client_id, |
| 637 account_id, | 638 account_id, |
| 638 scopes), | 639 scopes), |
| 639 access_token); | 640 access_token); |
| 640 delegate_->InvalidateAccessToken(account_id, client_id, scopes, access_token); | 641 delegate_->InvalidateAccessToken(account_id, client_id, scopes, access_token); |
| 641 } | 642 } |
| 642 | 643 |
| 643 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { | 644 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { |
| 644 DCHECK(CalledOnValidThread()); | 645 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 645 | 646 |
| 646 // Update the auth error state so auth errors are appropriately communicated | 647 // Update the auth error state so auth errors are appropriately communicated |
| 647 // to the user. | 648 // to the user. |
| 648 UpdateAuthError(fetcher->GetAccountId(), fetcher->error()); | 649 UpdateAuthError(fetcher->GetAccountId(), fetcher->error()); |
| 649 | 650 |
| 650 // Note |fetcher| is recorded in |pending_fetcher_| mapped to its refresh | 651 // Note |fetcher| is recorded in |pending_fetcher_| mapped to its refresh |
| 651 // token and scope set. This is guaranteed as follows; here a Fetcher is said | 652 // token and scope set. This is guaranteed as follows; here a Fetcher is said |
| 652 // to be uncompleted if it has not finished calling back | 653 // to be uncompleted if it has not finished calling back |
| 653 // OAuth2TokenService::OnFetchComplete(). | 654 // OAuth2TokenService::OnFetchComplete(). |
| 654 // | 655 // |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 } | 702 } |
| 702 | 703 |
| 703 bool OAuth2TokenService::HasCacheEntry( | 704 bool OAuth2TokenService::HasCacheEntry( |
| 704 const RequestParameters& request_parameters) { | 705 const RequestParameters& request_parameters) { |
| 705 const CacheEntry* cache_entry = GetCacheEntry(request_parameters); | 706 const CacheEntry* cache_entry = GetCacheEntry(request_parameters); |
| 706 return cache_entry && cache_entry->access_token.length(); | 707 return cache_entry && cache_entry->access_token.length(); |
| 707 } | 708 } |
| 708 | 709 |
| 709 const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry( | 710 const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry( |
| 710 const RequestParameters& request_parameters) { | 711 const RequestParameters& request_parameters) { |
| 711 DCHECK(CalledOnValidThread()); | 712 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 712 TokenCache::iterator token_iterator = token_cache_.find(request_parameters); | 713 TokenCache::iterator token_iterator = token_cache_.find(request_parameters); |
| 713 if (token_iterator == token_cache_.end()) | 714 if (token_iterator == token_cache_.end()) |
| 714 return NULL; | 715 return NULL; |
| 715 if (token_iterator->second.expiration_date <= base::Time::Now()) { | 716 if (token_iterator->second.expiration_date <= base::Time::Now()) { |
| 716 token_cache_.erase(token_iterator); | 717 token_cache_.erase(token_iterator); |
| 717 return NULL; | 718 return NULL; |
| 718 } | 719 } |
| 719 return &token_iterator->second; | 720 return &token_iterator->second; |
| 720 } | 721 } |
| 721 | 722 |
| 722 bool OAuth2TokenService::RemoveCacheEntry( | 723 bool OAuth2TokenService::RemoveCacheEntry( |
| 723 const RequestParameters& request_parameters, | 724 const RequestParameters& request_parameters, |
| 724 const std::string& token_to_remove) { | 725 const std::string& token_to_remove) { |
| 725 DCHECK(CalledOnValidThread()); | 726 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 726 TokenCache::iterator token_iterator = token_cache_.find(request_parameters); | 727 TokenCache::iterator token_iterator = token_cache_.find(request_parameters); |
| 727 if (token_iterator != token_cache_.end() && | 728 if (token_iterator != token_cache_.end() && |
| 728 token_iterator->second.access_token == token_to_remove) { | 729 token_iterator->second.access_token == token_to_remove) { |
| 729 for (auto& observer : diagnostics_observer_list_) { | 730 for (auto& observer : diagnostics_observer_list_) { |
| 730 observer.OnTokenRemoved(request_parameters.account_id, | 731 observer.OnTokenRemoved(request_parameters.account_id, |
| 731 request_parameters.scopes); | 732 request_parameters.scopes); |
| 732 } | 733 } |
| 733 token_cache_.erase(token_iterator); | 734 token_cache_.erase(token_iterator); |
| 734 return true; | 735 return true; |
| 735 } | 736 } |
| 736 return false; | 737 return false; |
| 737 } | 738 } |
| 738 void OAuth2TokenService::UpdateAuthError(const std::string& account_id, | 739 void OAuth2TokenService::UpdateAuthError(const std::string& account_id, |
| 739 const GoogleServiceAuthError& error) { | 740 const GoogleServiceAuthError& error) { |
| 740 delegate_->UpdateAuthError(account_id, error); | 741 delegate_->UpdateAuthError(account_id, error); |
| 741 } | 742 } |
| 742 | 743 |
| 743 void OAuth2TokenService::RegisterCacheEntry( | 744 void OAuth2TokenService::RegisterCacheEntry( |
| 744 const std::string& client_id, | 745 const std::string& client_id, |
| 745 const std::string& account_id, | 746 const std::string& account_id, |
| 746 const OAuth2TokenService::ScopeSet& scopes, | 747 const OAuth2TokenService::ScopeSet& scopes, |
| 747 const std::string& access_token, | 748 const std::string& access_token, |
| 748 const base::Time& expiration_date) { | 749 const base::Time& expiration_date) { |
| 749 DCHECK(CalledOnValidThread()); | 750 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 750 | 751 |
| 751 CacheEntry& token = token_cache_[RequestParameters(client_id, | 752 CacheEntry& token = token_cache_[RequestParameters(client_id, |
| 752 account_id, | 753 account_id, |
| 753 scopes)]; | 754 scopes)]; |
| 754 token.access_token = access_token; | 755 token.access_token = access_token; |
| 755 token.expiration_date = expiration_date; | 756 token.expiration_date = expiration_date; |
| 756 } | 757 } |
| 757 | 758 |
| 758 void OAuth2TokenService::ClearCache() { | 759 void OAuth2TokenService::ClearCache() { |
| 759 DCHECK(CalledOnValidThread()); | 760 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 760 for (TokenCache::iterator iter = token_cache_.begin(); | 761 for (TokenCache::iterator iter = token_cache_.begin(); |
| 761 iter != token_cache_.end(); ++iter) { | 762 iter != token_cache_.end(); ++iter) { |
| 762 for (auto& observer : diagnostics_observer_list_) | 763 for (auto& observer : diagnostics_observer_list_) |
| 763 observer.OnTokenRemoved(iter->first.account_id, iter->first.scopes); | 764 observer.OnTokenRemoved(iter->first.account_id, iter->first.scopes); |
| 764 } | 765 } |
| 765 | 766 |
| 766 token_cache_.clear(); | 767 token_cache_.clear(); |
| 767 } | 768 } |
| 768 | 769 |
| 769 void OAuth2TokenService::ClearCacheForAccount(const std::string& account_id) { | 770 void OAuth2TokenService::ClearCacheForAccount(const std::string& account_id) { |
| 770 DCHECK(CalledOnValidThread()); | 771 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 771 for (TokenCache::iterator iter = token_cache_.begin(); | 772 for (TokenCache::iterator iter = token_cache_.begin(); |
| 772 iter != token_cache_.end(); | 773 iter != token_cache_.end(); |
| 773 /* iter incremented in body */) { | 774 /* iter incremented in body */) { |
| 774 if (iter->first.account_id == account_id) { | 775 if (iter->first.account_id == account_id) { |
| 775 for (auto& observer : diagnostics_observer_list_) | 776 for (auto& observer : diagnostics_observer_list_) |
| 776 observer.OnTokenRemoved(account_id, iter->first.scopes); | 777 observer.OnTokenRemoved(account_id, iter->first.scopes); |
| 777 token_cache_.erase(iter++); | 778 token_cache_.erase(iter++); |
| 778 } else { | 779 } else { |
| 779 ++iter; | 780 ++iter; |
| 780 } | 781 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 804 void OAuth2TokenService::CancelFetchers( | 805 void OAuth2TokenService::CancelFetchers( |
| 805 std::vector<Fetcher*> fetchers_to_cancel) { | 806 std::vector<Fetcher*> fetchers_to_cancel) { |
| 806 for (auto iter = fetchers_to_cancel.begin(); iter != fetchers_to_cancel.end(); | 807 for (auto iter = fetchers_to_cancel.begin(); iter != fetchers_to_cancel.end(); |
| 807 ++iter) { | 808 ++iter) { |
| 808 (*iter)->Cancel(); | 809 (*iter)->Cancel(); |
| 809 } | 810 } |
| 810 } | 811 } |
| 811 | 812 |
| 812 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( | 813 void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( |
| 813 int max_retries) { | 814 int max_retries) { |
| 814 DCHECK(CalledOnValidThread()); | 815 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 815 max_fetch_retry_num_ = max_retries; | 816 max_fetch_retry_num_ = max_retries; |
| 816 } | 817 } |
| 817 | 818 |
| 818 size_t OAuth2TokenService::GetNumPendingRequestsForTesting( | 819 size_t OAuth2TokenService::GetNumPendingRequestsForTesting( |
| 819 const std::string& client_id, | 820 const std::string& client_id, |
| 820 const std::string& account_id, | 821 const std::string& account_id, |
| 821 const ScopeSet& scopes) const { | 822 const ScopeSet& scopes) const { |
| 822 auto iter = pending_fetchers_.find( | 823 auto iter = pending_fetchers_.find( |
| 823 OAuth2TokenService::RequestParameters(client_id, account_id, scopes)); | 824 OAuth2TokenService::RequestParameters(client_id, account_id, scopes)); |
| 824 return iter == pending_fetchers_.end() ? | 825 return iter == pending_fetchers_.end() ? |
| 825 0 : iter->second->GetWaitingRequestCount(); | 826 0 : iter->second->GetWaitingRequestCount(); |
| 826 } | 827 } |
| OLD | NEW |