diff options
author | eustas@chromium.org <eustas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-05 05:53:13 +0000 |
---|---|---|
committer | eustas@chromium.org <eustas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-09-05 05:53:13 +0000 |
commit | b87bfcd97f0b8027a8bc0e265d974f1ffcdac0f9 (patch) | |
tree | a24ad75332da1547c52bddbe738b2a2f23165347 /google_apis | |
parent | e992b2a79efcf62b88db748097a32a0cf80567e9 (diff) | |
download | chromium_src-b87bfcd97f0b8027a8bc0e265d974f1ffcdac0f9.zip chromium_src-b87bfcd97f0b8027a8bc0e265d974f1ffcdac0f9.tar.gz chromium_src-b87bfcd97f0b8027a8bc0e265d974f1ffcdac0f9.tar.bz2 |
Revert "Handling of multiple concurrent requests from different clients in OAuth2TokenService"
This reverts commit 2a0ff28378173300b81b2874736c30a6c48d0029.
Reason: BuildBot Vista fails TokenServiceUpdateClearsCache test.
BUG=
TBR=zelidrag@chromium.org
Review URL: https://codereview.chromium.org/23767004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221350 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'google_apis')
-rw-r--r-- | google_apis/gaia/oauth2_access_token_fetcher.cc | 13 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_access_token_fetcher.h | 12 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_token_service.cc | 182 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_token_service.h | 75 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_token_service_unittest.cc | 155 |
5 files changed, 89 insertions, 348 deletions
diff --git a/google_apis/gaia/oauth2_access_token_fetcher.cc b/google_apis/gaia/oauth2_access_token_fetcher.cc index 9fd46d7..456251f 100644 --- a/google_apis/gaia/oauth2_access_token_fetcher.cc +++ b/google_apis/gaia/oauth2_access_token_fetcher.cc @@ -56,14 +56,13 @@ static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { } } -static URLFetcher* CreateFetcher(int id, - URLRequestContextGetter* getter, +static URLFetcher* CreateFetcher(URLRequestContextGetter* getter, const GURL& url, const std::string& body, URLFetcherDelegate* delegate) { bool empty_body = body.empty(); URLFetcher* result = net::URLFetcher::Create( - id, url, + 0, url, empty_body ? URLFetcher::GET : URLFetcher::POST, delegate); @@ -83,8 +82,6 @@ static URLFetcher* CreateFetcher(int id, } } // namespace -int OAuth2AccessTokenFetcher::last_fetcher_id_ = 0; - OAuth2AccessTokenFetcher::OAuth2AccessTokenFetcher( OAuth2AccessTokenConsumer* consumer, URLRequestContextGetter* getter) @@ -113,7 +110,6 @@ void OAuth2AccessTokenFetcher::StartGetAccessToken() { CHECK_EQ(INITIAL, state_); state_ = GET_ACCESS_TOKEN_STARTED; fetcher_.reset(CreateFetcher( - last_fetcher_id_++, getter_, MakeGetAccessTokenUrl(), MakeGetAccessTokenBody( @@ -235,8 +231,3 @@ bool OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( return dict->GetString(kAccessTokenKey, access_token) && dict->GetInteger(kExpiresInKey, expires_in); } - -// static -void OAuth2AccessTokenFetcher::ResetLastFetcherIdForTest() { - last_fetcher_id_ = 0; -} diff --git a/google_apis/gaia/oauth2_access_token_fetcher.h b/google_apis/gaia/oauth2_access_token_fetcher.h index e9e1520..11ac6ea 100644 --- a/google_apis/gaia/oauth2_access_token_fetcher.h +++ b/google_apis/gaia/oauth2_access_token_fetcher.h @@ -26,10 +26,6 @@ class URLRequestContextGetter; class URLRequestStatus; } -namespace policy { - class UserPolicySigninServiceTest; -} - // Abstracts the details to get OAuth2 access token token from // OAuth2 refresh token. // See "Using the Refresh Token" section in: @@ -98,9 +94,6 @@ class OAuth2AccessTokenFetcher : public net::URLFetcherDelegate { std::string* access_token, int* expires_in); - // Resets |last_fetcher_id_| to 0. - static void ResetLastFetcherIdForTest(); - // State that is set during construction. OAuth2AccessTokenConsumer* const consumer_; net::URLRequestContextGetter* const getter_; @@ -113,12 +106,7 @@ class OAuth2AccessTokenFetcher : public net::URLFetcherDelegate { std::string refresh_token_; std::vector<std::string> scopes_; - // The last fetcher id. - static int last_fetcher_id_; - friend class OAuth2AccessTokenFetcherTest; - friend class OAuth2TokenServiceTest; - friend class policy::UserPolicySigninServiceTest; FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest, ParseGetAccessTokenResponse); FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest, diff --git a/google_apis/gaia/oauth2_token_service.cc b/google_apis/gaia/oauth2_token_service.cc index 828f4cc6..0834c60 100644 --- a/google_apis/gaia/oauth2_token_service.cc +++ b/google_apis/gaia/oauth2_token_service.cc @@ -15,57 +15,12 @@ #include "base/timer/timer.h" #include "google_apis/gaia/gaia_urls.h" #include "google_apis/gaia/google_service_auth_error.h" +#include "google_apis/gaia/oauth2_access_token_consumer.h" +#include "google_apis/gaia/oauth2_access_token_fetcher.h" #include "net/url_request/url_request_context_getter.h" int OAuth2TokenService::max_fetch_retry_num_ = 5; -OAuth2TokenService::ClientScopeSet::ClientScopeSet( - const std::string& client_id, - const ScopeSet& scopes) - : client_id(client_id), - scopes(scopes) { -} - -OAuth2TokenService::ClientScopeSet::~ClientScopeSet() { -} - -bool OAuth2TokenService::ClientScopeSet::operator<( - const ClientScopeSet& s) const { - if (client_id < s.client_id) - return true; - else if (s.client_id < client_id) - return false; - - return scopes < s.scopes; -} - -OAuth2TokenService::FetchParameters::FetchParameters( - const std::string& client_id, - const std::string& refresh_token, - const ScopeSet& scopes) - : client_id(client_id), - refresh_token(refresh_token), - scopes(scopes) { -} - -OAuth2TokenService::FetchParameters::~FetchParameters() { -} - -bool OAuth2TokenService::FetchParameters::operator<( - const FetchParameters& p) const { - if (client_id < p.client_id) - return true; - else if (p.client_id < client_id) - return false; - - if (refresh_token < p.refresh_token) - return true; - else if (p.refresh_token < refresh_token) - return false; - - return scopes < p.scopes; -} - OAuth2TokenService::RequestImpl::RequestImpl( OAuth2TokenService::Consumer* consumer) : consumer_(consumer) { @@ -86,9 +41,6 @@ void OAuth2TokenService::RequestImpl::InformConsumer( consumer_->OnGetTokenFailure(this, error); } -// Class that fetches an OAuth2 access token for a given set of scopes and -// OAuth2 refresh token. - // Class that fetches OAuth2 access tokens for given scopes and refresh token. // // It aims to meet OAuth2TokenService's requirements on token fetching. Retry @@ -105,17 +57,17 @@ void OAuth2TokenService::RequestImpl::InformConsumer( // // Requests that are waiting for the fetching results of this Fetcher can be // added to the Fetcher by calling -// OAuth2TokenService::Fetcher::AddWaitingRequest() before the Fetcher -// completes fetching. +// OAuth2TokenService::Fetcher::AddWaitingRequest() before the Fetcher completes +// fetching. // -// The waiting requests are taken as weak pointers and they can be deleted. -// The waiting requests will be called back with fetching results if they are -// not deleted +// The waiting requests are taken as weak pointers and they can be deleted. The +// waiting requests will be called back with fetching results if they are not +// deleted // - when the Fetcher completes fetching, if the Fetcher is not destructed // before it completes fetching, or // - when the Fetcher is destructed if the Fetcher is destructed before it -// completes fetching (in this case, the waiting requests will be called -// back with error). +// completes fetching (in this case, the waiting requests will be called back +// with error). class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { public: // Creates a Fetcher and starts fetching an OAuth2 access token for @@ -123,24 +75,20 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { // The given |oauth2_token_service| will be informed when fetching is done. static Fetcher* CreateAndStart(OAuth2TokenService* oauth2_token_service, net::URLRequestContextGetter* getter, - const std::string& client_id, - const std::string& client_secret, + const std::string& chrome_client_id, + const std::string& chrome_client_secret, const std::string& refresh_token, - const ScopeSet& scopes, + const OAuth2TokenService::ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request); virtual ~Fetcher(); // Add a request that is waiting for the result of this Fetcher. void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request); - // Returns count of waiting requests. - size_t GetWaitingRequestCount() const; - void Cancel(); - const ScopeSet& GetScopeSet() const; + const OAuth2TokenService::ScopeSet& GetScopeSet() const; const std::string& GetRefreshToken() const; - const std::string& GetClientId() const; // The error result from this fetcher. const GoogleServiceAuthError& error() const { return error_; } @@ -149,14 +97,13 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { // OAuth2AccessTokenConsumer virtual void OnGetTokenSuccess(const std::string& access_token, const base::Time& expiration_date) OVERRIDE; - virtual void OnGetTokenFailure( - const GoogleServiceAuthError& error) OVERRIDE; + virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; private: Fetcher(OAuth2TokenService* oauth2_token_service, net::URLRequestContextGetter* getter, - const std::string& client_id, - const std::string& client_secret, + const std::string& chrome_client_id, + const std::string& chrome_client_secret, const std::string& refresh_token, const OAuth2TokenService::ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request); @@ -173,11 +120,11 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { OAuth2TokenService* const oauth2_token_service_; scoped_refptr<net::URLRequestContextGetter> getter_; const std::string refresh_token_; - const ScopeSet scopes_; + const OAuth2TokenService::ScopeSet scopes_; std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; int retry_number_; - base::OneShotTimer<Fetcher> retry_timer_; + base::OneShotTimer<OAuth2TokenService::Fetcher> retry_timer_; scoped_ptr<OAuth2AccessTokenFetcher> fetcher_; // Variables that store fetch results. @@ -186,10 +133,9 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { GoogleServiceAuthError error_; std::string access_token_; base::Time expiration_date_; - // OAuth2 client id and secret. - std::string client_id_; - std::string client_secret_; + std::string chrome_client_id_; + std::string chrome_client_secret_; DISALLOW_COPY_AND_ASSIGN(Fetcher); }; @@ -198,16 +144,16 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart( OAuth2TokenService* oauth2_token_service, net::URLRequestContextGetter* getter, - const std::string& client_id, - const std::string& client_secret, + const std::string& chrome_client_id, + const std::string& chrome_client_secret, const std::string& refresh_token, const OAuth2TokenService::ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request) { OAuth2TokenService::Fetcher* fetcher = new Fetcher( oauth2_token_service, getter, - client_id, - client_secret, + chrome_client_id, + chrome_client_secret, refresh_token, scopes, waiting_request); @@ -218,8 +164,8 @@ OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart( OAuth2TokenService::Fetcher::Fetcher( OAuth2TokenService* oauth2_token_service, net::URLRequestContextGetter* getter, - const std::string& client_id, - const std::string& client_secret, + const std::string& chrome_client_id, + const std::string& chrome_client_secret, const std::string& refresh_token, const OAuth2TokenService::ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request) @@ -229,8 +175,8 @@ OAuth2TokenService::Fetcher::Fetcher( scopes_(scopes), retry_number_(0), error_(GoogleServiceAuthError::SERVICE_UNAVAILABLE), - client_id_(client_id), - client_secret_(client_secret) { + chrome_client_id_(chrome_client_id), + chrome_client_secret_(chrome_client_secret) { DCHECK(oauth2_token_service_); DCHECK(getter_.get()); DCHECK(refresh_token_.length()); @@ -245,8 +191,8 @@ OAuth2TokenService::Fetcher::~Fetcher() { void OAuth2TokenService::Fetcher::Start() { fetcher_.reset(new OAuth2AccessTokenFetcher(this, getter_.get())); - fetcher_->Start(client_id_, - client_secret_, + fetcher_->Start(chrome_client_id_, + chrome_client_secret_, refresh_token_, std::vector<std::string>(scopes_.begin(), scopes_.end())); retry_timer_.Stop(); @@ -266,8 +212,7 @@ void OAuth2TokenService::Fetcher::OnGetTokenSuccess( // we still inform all waiting Consumers of a successful token fetch below. // This is intentional -- some consumers may need the token for cleanup // tasks. https://chromiumcodereview.appspot.com/11312124/ - oauth2_token_service_->RegisterCacheEntry(client_id_, - refresh_token_, + oauth2_token_service_->RegisterCacheEntry(refresh_token_, scopes_, access_token_, expiration_date_); @@ -336,10 +281,6 @@ void OAuth2TokenService::Fetcher::AddWaitingRequest( waiting_requests_.push_back(waiting_request); } -size_t OAuth2TokenService::Fetcher::GetWaitingRequestCount() const { - return waiting_requests_.size(); -} - void OAuth2TokenService::Fetcher::Cancel() { fetcher_.reset(); retry_timer_.Stop(); @@ -356,10 +297,6 @@ const std::string& OAuth2TokenService::Fetcher::GetRefreshToken() const { return refresh_token_; } -const std::string& OAuth2TokenService::Fetcher::GetClientId() const { - return client_id_; -} - OAuth2TokenService::Request::Request() { } @@ -452,9 +389,8 @@ OAuth2TokenService::StartRequestForClientWithContext( return request.PassAs<Request>(); } - ClientScopeSet client_scopes(client_id, scopes); - if (HasCacheEntry(client_scopes)) { - StartCacheLookupRequest(request.get(), client_scopes, consumer); + if (HasCacheEntry(scopes)) { + StartCacheLookupRequest(request.get(), scopes, consumer); } else { FetchOAuth2Token(request.get(), getter, @@ -475,9 +411,7 @@ void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request, // If there is already a pending fetcher for |scopes| and |refresh_token|, // simply register this |request| for those results rather than starting // a new fetcher. - FetchParameters fetch_parameters = FetchParameters(client_id, - refresh_token, - scopes); + FetchParameters fetch_parameters = std::make_pair(refresh_token, scopes); std::map<FetchParameters, Fetcher*>::iterator iter = pending_fetchers_.find(fetch_parameters); if (iter != pending_fetchers_.end()) { @@ -497,10 +431,10 @@ void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request, void OAuth2TokenService::StartCacheLookupRequest( RequestImpl* request, - const OAuth2TokenService::ClientScopeSet& client_scopes, + const OAuth2TokenService::ScopeSet& scopes, OAuth2TokenService::Consumer* consumer) { - CHECK(HasCacheEntry(client_scopes)); - const CacheEntry* cache_entry = GetCacheEntry(client_scopes); + CHECK(HasCacheEntry(scopes)); + const CacheEntry* cache_entry = GetCacheEntry(scopes); base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( &RequestImpl::InformConsumer, request->AsWeakPtr(), @@ -512,10 +446,7 @@ void OAuth2TokenService::StartCacheLookupRequest( void OAuth2TokenService::InvalidateToken(const ScopeSet& scopes, const std::string& invalid_token) { DCHECK(CalledOnValidThread()); - RemoveCacheEntry( - ClientScopeSet(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), - scopes), - invalid_token); + RemoveCacheEntry(scopes, invalid_token); } void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { @@ -552,25 +483,23 @@ void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { // Then by (2), |fetcher| is recorded in |pending_fetchers_|. // Then by (3), |fetcher_| is mapped to its refresh token and ScopeSet. std::map<FetchParameters, Fetcher*>::iterator iter = - pending_fetchers_.find(FetchParameters( - fetcher->GetClientId(), - fetcher->GetRefreshToken(), - fetcher->GetScopeSet())); + pending_fetchers_.find(std::make_pair( + fetcher->GetRefreshToken(), fetcher->GetScopeSet())); DCHECK(iter != pending_fetchers_.end()); DCHECK_EQ(fetcher, iter->second); pending_fetchers_.erase(iter); } bool OAuth2TokenService::HasCacheEntry( - const ClientScopeSet& client_scopes) { - const CacheEntry* cache_entry = GetCacheEntry(client_scopes); + const OAuth2TokenService::ScopeSet& scopes) { + const CacheEntry* cache_entry = GetCacheEntry(scopes); return cache_entry && cache_entry->access_token.length(); } const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry( - const ClientScopeSet& client_scopes) { + const OAuth2TokenService::ScopeSet& scopes) { DCHECK(CalledOnValidThread()); - TokenCache::iterator token_iterator = token_cache_.find(client_scopes); + TokenCache::iterator token_iterator = token_cache_.find(scopes); if (token_iterator == token_cache_.end()) return NULL; if (token_iterator->second.expiration_date <= base::Time::Now()) { @@ -581,10 +510,10 @@ const OAuth2TokenService::CacheEntry* OAuth2TokenService::GetCacheEntry( } bool OAuth2TokenService::RemoveCacheEntry( - const ClientScopeSet& client_scopes, + const OAuth2TokenService::ScopeSet& scopes, const std::string& token_to_remove) { DCHECK(CalledOnValidThread()); - TokenCache::iterator token_iterator = token_cache_.find(client_scopes); + TokenCache::iterator token_iterator = token_cache_.find(scopes); if (token_iterator != token_cache_.end() && token_iterator->second.access_token == token_to_remove) { token_cache_.erase(token_iterator); @@ -594,15 +523,13 @@ bool OAuth2TokenService::RemoveCacheEntry( } void OAuth2TokenService::RegisterCacheEntry( - const std::string& client_id, const std::string& refresh_token, const OAuth2TokenService::ScopeSet& scopes, const std::string& access_token, const base::Time& expiration_date) { DCHECK(CalledOnValidThread()); - CacheEntry& token = token_cache_[ClientScopeSet(client_id, - scopes)]; + CacheEntry& token = token_cache_[scopes]; token.access_token = access_token; token.expiration_date = expiration_date; } @@ -634,7 +561,7 @@ void OAuth2TokenService::CancelRequestsForToken( pending_fetchers_.begin(); iter != pending_fetchers_.end(); ++iter) { - if (iter->first.refresh_token == refresh_token) + if (iter->first.first == refresh_token) fetchers_to_cancel.push_back(iter->second); } CancelFetchers(fetchers_to_cancel); @@ -679,16 +606,3 @@ void OAuth2TokenService::set_max_authorization_token_fetch_retries_for_testing( DCHECK(CalledOnValidThread()); max_fetch_retry_num_ = max_retries; } - -size_t OAuth2TokenService::GetNumPendingRequestsForTesting( - const std::string& client_id, - const std::string& refresh_token, - const ScopeSet& scopes) const { - PendingFetcherMap::const_iterator iter = pending_fetchers_.find( - OAuth2TokenService::FetchParameters( - client_id, - refresh_token, - scopes)); - return iter == pending_fetchers_.end() ? - 0 : iter->second->GetWaitingRequestCount(); -} diff --git a/google_apis/gaia/oauth2_token_service.h b/google_apis/gaia/oauth2_token_service.h index b58c563..967dc93 100644 --- a/google_apis/gaia/oauth2_token_service.h +++ b/google_apis/gaia/oauth2_token_service.h @@ -10,16 +10,12 @@ #include <string> #include "base/basictypes.h" -#include "base/gtest_prod_util.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "base/observer_list.h" #include "base/threading/non_thread_safe.h" #include "base/time/time.h" -#include "base/timer/timer.h" #include "google_apis/gaia/google_service_auth_error.h" -#include "google_apis/gaia/oauth2_access_token_consumer.h" -#include "google_apis/gaia/oauth2_access_token_fetcher.h" namespace net { class URLRequestContextGetter; @@ -149,23 +145,8 @@ class OAuth2TokenService : public base::NonThreadSafe { // Return the current number of entries in the cache. int cache_size_for_testing() const; void set_max_authorization_token_fetch_retries_for_testing(int max_retries); - // Returns the current number of pending fetchers matching given params. - size_t GetNumPendingRequestsForTesting( - const std::string& client_id, - const std::string& refresh_token, - const ScopeSet& scopes) const; protected: - struct ClientScopeSet { - ClientScopeSet(const std::string& client_id, - const ScopeSet& scopes); - ~ClientScopeSet(); - bool operator<(const ClientScopeSet& set) const; - - std::string client_id; - ScopeSet scopes; - }; - // Implements a cancelable |OAuth2TokenService::Request|, which should be // operated on the UI thread. // TODO(davidroche): move this out of header file. @@ -197,20 +178,19 @@ class OAuth2TokenService : public base::NonThreadSafe { // Add a new entry to the cache. // Subclasses can override if there are implementation-specific reasons // that an access token should ever not be cached. - virtual void RegisterCacheEntry(const std::string& client_id, - const std::string& refresh_token, + virtual void RegisterCacheEntry(const std::string& refresh_token, const ScopeSet& scopes, const std::string& access_token, const base::Time& expiration_date); // Returns true if GetCacheEntry would return a valid cache entry for the // given scopes. - bool HasCacheEntry(const ClientScopeSet& client_scopes); + bool HasCacheEntry(const ScopeSet& scopes); // Posts a task to fire the Consumer callback with the cached token. Must // Must only be called if HasCacheEntry() returns true. void StartCacheLookupRequest(RequestImpl* request, - const ClientScopeSet& client_scopes, + const ScopeSet& scopes, Consumer* consumer); // Clears the internal token cache. @@ -228,6 +208,10 @@ class OAuth2TokenService : public base::NonThreadSafe { void FireRefreshTokensLoaded(); void FireRefreshTokensCleared(); + // Derived classes must provide a request context used for fetching access + // tokens with the |StartRequest| method. + virtual net::URLRequestContextGetter* GetRequestContext() = 0; + // Fetches an OAuth token for the specified client/scopes. Virtual so it can // be overridden for tests and for platform-specific behavior on Android. virtual void FetchOAuth2Token(RequestImpl* request, @@ -235,32 +219,13 @@ class OAuth2TokenService : public base::NonThreadSafe { const std::string& client_id, const std::string& client_secret, const ScopeSet& scopes); + private: + // Class that fetches an OAuth2 access token for a given set of scopes and + // OAuth2 refresh token. class Fetcher; friend class Fetcher; - // The parameters used to fetch an OAuth2 access token. - struct FetchParameters { - FetchParameters(const std::string& client_id, - const std::string& refresh_token, - const ScopeSet& scopes); - ~FetchParameters(); - bool operator<(const FetchParameters& params) const; - - // OAuth2 client id. - std::string client_id; - // Refresh token used for minting access tokens within this request. - std::string refresh_token; - // URL scopes for the requested access token. - ScopeSet scopes; - }; - - typedef std::map<FetchParameters, Fetcher*> PendingFetcherMap; - - // Derived classes must provide a request context used for fetching access - // tokens with the |StartRequest| method. - virtual net::URLRequestContextGetter* GetRequestContext() = 0; - // Struct that contains the information of an OAuth2 access token. struct CacheEntry { std::string access_token; @@ -279,14 +244,14 @@ class OAuth2TokenService : public base::NonThreadSafe { // Returns a currently valid OAuth2 access token for the given set of scopes, // or NULL if none have been cached. Note the user of this method should - // ensure no entry with the same |client_scopes| is added before the usage of - // the returned entry is done. - const CacheEntry* GetCacheEntry(const ClientScopeSet& client_scopes); + // ensure no entry with the same |scopes| is added before the usage of the + // returned entry is done. + const CacheEntry* GetCacheEntry(const ScopeSet& scopes); // Removes an access token for the given set of scopes from the cache. // Returns true if the entry was removed, otherwise false. - bool RemoveCacheEntry(const ClientScopeSet& client_scopes, + bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes, const std::string& token_to_remove); @@ -297,12 +262,15 @@ class OAuth2TokenService : public base::NonThreadSafe { void CancelFetchers(std::vector<Fetcher*> fetchers_to_cancel); // The cache of currently valid tokens. - typedef std::map<ClientScopeSet, CacheEntry> TokenCache; + typedef std::map<ScopeSet, CacheEntry> TokenCache; TokenCache token_cache_; + // The parameters (refresh token and scope set) used to fetch an OAuth2 access + // token. + typedef std::pair<std::string, ScopeSet> FetchParameters; // A map from fetch parameters to a fetcher that is fetching an OAuth2 access // token using these parameters. - PendingFetcherMap pending_fetchers_; + std::map<FetchParameters, Fetcher*> pending_fetchers_; // List of observers to notify when token availability changes. // Makes sure list is empty on destruction. @@ -311,11 +279,6 @@ class OAuth2TokenService : public base::NonThreadSafe { // Maximum number of retries in fetching an OAuth2 access token. static int max_fetch_retry_num_; - FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest, ClientScopeSetOrderTest); - FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest, FetchParametersOrderTest); - FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest, - SameScopesRequestedForDifferentClients); - DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); }; diff --git a/google_apis/gaia/oauth2_token_service_unittest.cc b/google_apis/gaia/oauth2_token_service_unittest.cc index 8f4e922..9937268 100644 --- a/google_apis/gaia/oauth2_token_service_unittest.cc +++ b/google_apis/gaia/oauth2_token_service_unittest.cc @@ -9,7 +9,6 @@ #include "google_apis/gaia/gaia_constants.h" #include "google_apis/gaia/google_service_auth_error.h" #include "google_apis/gaia/oauth2_access_token_consumer.h" -#include "google_apis/gaia/oauth2_access_token_fetcher.h" #include "google_apis/gaia/oauth2_token_service.h" #include "google_apis/gaia/oauth2_token_service_test_util.h" #include "net/http/http_status_code.h" @@ -79,7 +78,6 @@ class OAuth2TokenServiceTest : public testing::Test { virtual void TearDown() OVERRIDE { // Makes sure that all the clean up tasks are run. base::RunLoop().RunUntilIdle(); - OAuth2AccessTokenFetcher::ResetLastFetcherIdForTest(); } protected: @@ -107,7 +105,7 @@ TEST_F(OAuth2TokenServiceTest, FailureShouldNotRetry) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_UNAUTHORIZED); fetcher->SetResponseString(std::string()); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -125,7 +123,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithoutCaching) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -154,7 +152,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -169,7 +167,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) { base::RunLoop().RunUntilIdle(); // No new network fetcher. - EXPECT_EQ(1U, factory_.GetFetcherCount()); + EXPECT_EQ(fetcher, factory_.GetFetcherByID(0)); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); EXPECT_EQ("token", consumer_.last_token_); @@ -180,9 +178,8 @@ TEST_F(OAuth2TokenServiceTest, SuccessWithCaching) { base::RunLoop().RunUntilIdle(); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); - ASSERT_EQ(2U, factory_.GetFetcherCount()); - fetcher = factory_.GetFetcherByID(1); - ASSERT_TRUE(fetcher); + fetcher = factory_.GetFetcherByID(0); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token2", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -201,7 +198,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 0)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -217,9 +214,8 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndFailure) { EXPECT_EQ(0, consumer_.number_of_errors_); // Network failure. - ASSERT_EQ(2U, factory_.GetFetcherCount()); - fetcher = factory_.GetFetcherByID(1); - ASSERT_TRUE(fetcher); + fetcher = factory_.GetFetcherByID(0); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_UNAUTHORIZED); fetcher->SetResponseString(std::string()); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -237,7 +233,7 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 0)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -252,9 +248,8 @@ TEST_F(OAuth2TokenServiceTest, SuccessAndExpirationAndSuccess) { EXPECT_EQ(1, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); - ASSERT_EQ(2U, factory_.GetFetcherCount()); - fetcher = factory_.GetFetcherByID(1); - ASSERT_TRUE(fetcher); + fetcher = factory_.GetFetcherByID(0); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("another token", 0)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -272,7 +267,7 @@ TEST_F(OAuth2TokenServiceTest, RequestDeletedBeforeCompletion) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); + EXPECT_TRUE(fetcher); request.reset(); @@ -290,7 +285,6 @@ TEST_F(OAuth2TokenServiceTest, RequestDeletedAfterCompletion) { std::set<std::string>(), &consumer_)); base::RunLoop().RunUntilIdle(); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -318,7 +312,6 @@ TEST_F(OAuth2TokenServiceTest, MultipleRequestsForTheSameScopesWithOneDeleted) { request.reset(); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -333,7 +326,6 @@ TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) { std::set<std::string>(), &consumer_)); base::RunLoop().RunUntilIdle(); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -345,7 +337,7 @@ TEST_F(OAuth2TokenServiceTest, ClearedRefreshTokenFailsSubsequentRequests) { oauth2_service_->set_refresh_token(""); request = oauth2_service_->StartRequest(std::set<std::string>(), &consumer_); base::RunLoop().RunUntilIdle(); - EXPECT_EQ(1U, factory_.GetFetcherCount()); + EXPECT_EQ(fetcher, factory_.GetFetcherByID(0)); EXPECT_EQ(1, consumer_.number_of_successful_tokens_); EXPECT_EQ(1, consumer_.number_of_errors_); } @@ -360,9 +352,7 @@ TEST_F(OAuth2TokenServiceTest, scoped_ptr<OAuth2TokenService::Request> request(oauth2_service_->StartRequest( scopes, &consumer_)); base::RunLoop().RunUntilIdle(); - ASSERT_EQ(1U, factory_.GetFetcherCount()); net::TestURLFetcher* fetcher1 = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher1); // Note |request| is still pending when the refresh token changes. oauth2_service_->set_refresh_token("second refreshToken"); @@ -374,8 +364,7 @@ TEST_F(OAuth2TokenServiceTest, oauth2_service_->StartRequest(scopes, &consumer2)); base::RunLoop().RunUntilIdle(); - ASSERT_EQ(2U, factory_.GetFetcherCount()); - net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(1); + net::TestURLFetcher* fetcher2 = factory_.GetFetcherByID(0); fetcher2->set_response_code(net::HTTP_OK); fetcher2->SetResponseString(GetValidTokenResponse("second token", 3600)); fetcher2->delegate()->OnURLFetchComplete(fetcher2); @@ -423,8 +412,7 @@ TEST_F(OAuth2TokenServiceTest, RetryingConsumer) { EXPECT_EQ(0, consumer.number_of_successful_tokens_); EXPECT_EQ(1, consumer.number_of_errors_); - ASSERT_EQ(2U, factory_.GetFetcherCount()); - fetcher = factory_.GetFetcherByID(1); + fetcher = factory_.GetFetcherByID(0); ASSERT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_UNAUTHORIZED); fetcher->SetResponseString(std::string()); @@ -445,7 +433,7 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0); - ASSERT_TRUE(fetcher); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -460,7 +448,7 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) { base::RunLoop().RunUntilIdle(); // No new network fetcher. - ASSERT_EQ(1U, factory_.GetFetcherCount()); + EXPECT_EQ(fetcher, factory_.GetFetcherByID(0)); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); EXPECT_EQ("token", consumer_.last_token_); @@ -472,9 +460,8 @@ TEST_F(OAuth2TokenServiceTest, InvalidateToken) { base::RunLoop().RunUntilIdle(); EXPECT_EQ(2, consumer_.number_of_successful_tokens_); EXPECT_EQ(0, consumer_.number_of_errors_); - ASSERT_EQ(2U, factory_.GetFetcherCount()); - fetcher = factory_.GetFetcherByID(1); - ASSERT_TRUE(fetcher); + fetcher = factory_.GetFetcherByID(0); + EXPECT_TRUE(fetcher); fetcher->set_response_code(net::HTTP_OK); fetcher->SetResponseString(GetValidTokenResponse("token2", 3600)); fetcher->delegate()->OnURLFetchComplete(fetcher); @@ -533,105 +520,3 @@ TEST_F(OAuth2TokenServiceTest, CancelRequestsForToken) { EXPECT_EQ(0, consumer_.number_of_successful_tokens_); EXPECT_EQ(3, consumer_.number_of_errors_); } - -TEST_F(OAuth2TokenServiceTest, SameScopesRequestedForDifferentClients) { - std::string client_id_1("client1"); - std::string client_secret_1("secret1"); - std::string client_id_2("client2"); - std::string client_secret_2("secret2"); - std::set<std::string> scope_set; - scope_set.insert("scope1"); - scope_set.insert("scope2"); - - std::string refresh_token("refreshToken"); - oauth2_service_->set_refresh_token(refresh_token); - - scoped_ptr<OAuth2TokenService::Request> request1( - oauth2_service_->StartRequestForClient(client_id_1, - client_secret_1, - scope_set, - &consumer_)); - scoped_ptr<OAuth2TokenService::Request> request2( - oauth2_service_->StartRequestForClient(client_id_2, - client_secret_2, - scope_set, - &consumer_)); - // Start a request that should be duplicate of |request1|. - scoped_ptr<OAuth2TokenService::Request> request3( - oauth2_service_->StartRequestForClient(client_id_1, - client_secret_1, - scope_set, - &consumer_)); - base::RunLoop().RunUntilIdle(); - - ASSERT_EQ(2U, - oauth2_service_->GetNumPendingRequestsForTesting( - client_id_1, - refresh_token, - scope_set)); - ASSERT_EQ(1U, - oauth2_service_->GetNumPendingRequestsForTesting( - client_id_2, - refresh_token, - scope_set)); -} - -TEST_F(OAuth2TokenServiceTest, ClientScopeSetOrderTest) { - OAuth2TokenService::ScopeSet set_0; - OAuth2TokenService::ScopeSet set_1; - set_1.insert("1"); - - OAuth2TokenService::ClientScopeSet sets[] = { - OAuth2TokenService::ClientScopeSet("0", set_0), - OAuth2TokenService::ClientScopeSet("0", set_1), - OAuth2TokenService::ClientScopeSet("1", set_0), - OAuth2TokenService::ClientScopeSet("1", set_1), - }; - - for (size_t i = 0; i < arraysize(sets); i++) { - for (size_t j = 0; j < arraysize(sets); j++) { - if (i == j) { - EXPECT_FALSE(sets[i] < sets[j]) << " i=" << i << ", j=" << j; - EXPECT_FALSE(sets[j] < sets[i]) << " i=" << i << ", j=" << j; - } else if (i < j) { - EXPECT_TRUE(sets[i] < sets[j]) << " i=" << i << ", j=" << j; - EXPECT_FALSE(sets[j] < sets[i]) << " i=" << i << ", j=" << j; - } else { - EXPECT_TRUE(sets[j] < sets[i]) << " i=" << i << ", j=" << j; - EXPECT_FALSE(sets[i] < sets[j]) << " i=" << i << ", j=" << j; - } - } - } -} - -TEST_F(OAuth2TokenServiceTest, FetchParametersOrderTest) { - OAuth2TokenService::ScopeSet set_0; - OAuth2TokenService::ScopeSet set_1; - set_1.insert("1"); - - OAuth2TokenService::FetchParameters params[] = { - OAuth2TokenService::FetchParameters("0", "0", set_0), - OAuth2TokenService::FetchParameters("0", "0", set_1), - OAuth2TokenService::FetchParameters("0", "1", set_0), - OAuth2TokenService::FetchParameters("0", "1", set_1), - OAuth2TokenService::FetchParameters("1", "0", set_0), - OAuth2TokenService::FetchParameters("1", "0", set_1), - OAuth2TokenService::FetchParameters("1", "1", set_0), - OAuth2TokenService::FetchParameters("1", "1", set_1), - }; - - for (size_t i = 0; i < arraysize(params); i++) { - for (size_t j = 0; j < arraysize(params); j++) { - if (i == j) { - EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j; - EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j; - } else if (i < j) { - EXPECT_TRUE(params[i] < params[j]) << " i=" << i << ", j=" << j; - EXPECT_FALSE(params[j] < params[i]) << " i=" << i << ", j=" << j; - } else { - EXPECT_TRUE(params[j] < params[i]) << " i=" << i << ", j=" << j; - EXPECT_FALSE(params[i] < params[j]) << " i=" << i << ", j=" << j; - } - } - } -} |