diff options
author | msarda@chromium.org <msarda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-13 13:28:20 +0000 |
---|---|---|
committer | msarda@chromium.org <msarda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-13 13:28:20 +0000 |
commit | 6ce127516f7e2a118e9d0f4c0e6e656be7502e2a (patch) | |
tree | 675e11e59bed6c0b0c60505ed272d41291fce965 /google_apis | |
parent | 0dc85512d800fd271b1bf7cd8cda9eac8fe48a6b (diff) | |
download | chromium_src-6ce127516f7e2a118e9d0f4c0e6e656be7502e2a.zip chromium_src-6ce127516f7e2a118e9d0f4c0e6e656be7502e2a.tar.gz chromium_src-6ce127516f7e2a118e9d0f4c0e6e656be7502e2a.tar.bz2 |
Add create access token fetcher to OAuthTokenService.
This CL add a factory method to create access token fetchers in the
OAuth2 token service. This allows subclasses to create specific access
token fetchers and re-use the OAuth2 caching logic without duplicating
the code.
TBR=atwilson, blundell, gene
BUG=320625
Review URL: https://codereview.chromium.org/193043002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@256816 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'google_apis')
-rw-r--r-- | google_apis/gaia/fake_oauth2_token_service.cc | 27 | ||||
-rw-r--r-- | google_apis/gaia/fake_oauth2_token_service.h | 19 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_access_token_fetcher.h | 1 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_access_token_fetcher_impl.cc | 11 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_access_token_fetcher_impl.h | 6 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_access_token_fetcher_impl_unittest.cc | 11 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_api_call_flow.cc | 3 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_api_call_flow_unittest.cc | 40 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_token_service.cc | 35 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_token_service.h | 17 | ||||
-rw-r--r-- | google_apis/gaia/oauth2_token_service_unittest.cc | 29 |
11 files changed, 110 insertions, 89 deletions
diff --git a/google_apis/gaia/fake_oauth2_token_service.cc b/google_apis/gaia/fake_oauth2_token_service.cc index b1d9054..f23f349 100644 --- a/google_apis/gaia/fake_oauth2_token_service.cc +++ b/google_apis/gaia/fake_oauth2_token_service.cc @@ -4,17 +4,11 @@ #include "google_apis/gaia/fake_oauth2_token_service.h" -FakeOAuth2TokenService::FakeOAuth2TokenService() { -} +FakeOAuth2TokenService::FakeOAuth2TokenService() : request_context_(NULL) {} FakeOAuth2TokenService::~FakeOAuth2TokenService() { } -std::string FakeOAuth2TokenService::GetRefreshToken( - const std::string& account_id) const { - return std::string(); -} - void FakeOAuth2TokenService::FetchOAuth2Token( RequestImpl* request, const std::string& account_id, @@ -32,5 +26,24 @@ void FakeOAuth2TokenService::InvalidateOAuth2Token( } net::URLRequestContextGetter* FakeOAuth2TokenService::GetRequestContext() { + return request_context_; +} + +bool FakeOAuth2TokenService::RefreshTokenIsAvailable( + const std::string& account_id) const { + return account_ids_.count(account_id) != 0; +}; + +void FakeOAuth2TokenService::AddAccount(const std::string& account_id) { + account_ids_.insert(account_id); +} + +OAuth2AccessTokenFetcher* FakeOAuth2TokenService::CreateAccessTokenFetcher( + const std::string& account_id, + net::URLRequestContextGetter* getter, + OAuth2AccessTokenConsumer* consumer) { + // |FakeOAuth2TokenService| overrides |FetchOAuth2Token| and thus + // |CreateAccessTokenFetcher| should never be called. + NOTREACHED(); return NULL; } diff --git a/google_apis/gaia/fake_oauth2_token_service.h b/google_apis/gaia/fake_oauth2_token_service.h index 2554bd4..8c267e5 100644 --- a/google_apis/gaia/fake_oauth2_token_service.h +++ b/google_apis/gaia/fake_oauth2_token_service.h @@ -5,6 +5,7 @@ #ifndef GOOGLE_APIS_GAIA_FAKE_OAUTH2_TOKEN_SERVICE_H_ #define GOOGLE_APIS_GAIA_FAKE_OAUTH2_TOKEN_SERVICE_H_ +#include <set> #include <string> #include "base/compiler_specific.h" @@ -20,6 +21,12 @@ class FakeOAuth2TokenService : public OAuth2TokenService { FakeOAuth2TokenService(); virtual ~FakeOAuth2TokenService(); + void AddAccount(const std::string& account_id); + + void set_request_context(net::URLRequestContextGetter* request_context) { + request_context_ = request_context; + } + protected: // OAuth2TokenService overrides. virtual void FetchOAuth2Token(RequestImpl* request, @@ -34,13 +41,21 @@ class FakeOAuth2TokenService : public OAuth2TokenService { const ScopeSet& scopes, const std::string& access_token) OVERRIDE; - virtual std::string GetRefreshToken(const std::string& account_id) - const OVERRIDE; + virtual bool RefreshTokenIsAvailable(const std::string& account_id) const + OVERRIDE; private: // OAuth2TokenService overrides. virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; + virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( + const std::string& account_id, + net::URLRequestContextGetter* getter, + OAuth2AccessTokenConsumer* consumer) OVERRIDE; + + std::set<std::string> account_ids_; + net::URLRequestContextGetter* request_context_; // weak + DISALLOW_COPY_AND_ASSIGN(FakeOAuth2TokenService); }; diff --git a/google_apis/gaia/oauth2_access_token_fetcher.h b/google_apis/gaia/oauth2_access_token_fetcher.h index 241fe99..cd3ee5c 100644 --- a/google_apis/gaia/oauth2_access_token_fetcher.h +++ b/google_apis/gaia/oauth2_access_token_fetcher.h @@ -36,7 +36,6 @@ class OAuth2AccessTokenFetcher { // a super-set of the specified scopes. virtual void Start(const std::string& client_id, const std::string& client_secret, - const std::string& refresh_token, const std::vector<std::string>& scopes) = 0; // Cancels the current request and informs the consumer. diff --git a/google_apis/gaia/oauth2_access_token_fetcher_impl.cc b/google_apis/gaia/oauth2_access_token_fetcher_impl.cc index 67b4e6a..f4cd4f0 100644 --- a/google_apis/gaia/oauth2_access_token_fetcher_impl.cc +++ b/google_apis/gaia/oauth2_access_token_fetcher_impl.cc @@ -130,8 +130,13 @@ scoped_ptr<base::DictionaryValue> ParseGetAccessTokenResponse( } // namespace OAuth2AccessTokenFetcherImpl::OAuth2AccessTokenFetcherImpl( - OAuth2AccessTokenConsumer* consumer, URLRequestContextGetter* getter) - : OAuth2AccessTokenFetcher(consumer), getter_(getter), state_(INITIAL) {} + OAuth2AccessTokenConsumer* consumer, + net::URLRequestContextGetter* getter, + const std::string& refresh_token) + : OAuth2AccessTokenFetcher(consumer), + getter_(getter), + refresh_token_(refresh_token), + state_(INITIAL) {} OAuth2AccessTokenFetcherImpl::~OAuth2AccessTokenFetcherImpl() {} @@ -140,11 +145,9 @@ void OAuth2AccessTokenFetcherImpl::CancelRequest() { fetcher_.reset(); } void OAuth2AccessTokenFetcherImpl::Start( const std::string& client_id, const std::string& client_secret, - const std::string& refresh_token, const std::vector<std::string>& scopes) { client_id_ = client_id; client_secret_ = client_secret; - refresh_token_ = refresh_token; scopes_ = scopes; StartGetAccessToken(); } diff --git a/google_apis/gaia/oauth2_access_token_fetcher_impl.h b/google_apis/gaia/oauth2_access_token_fetcher_impl.h index e855236..8839b6c 100644 --- a/google_apis/gaia/oauth2_access_token_fetcher_impl.h +++ b/google_apis/gaia/oauth2_access_token_fetcher_impl.h @@ -49,13 +49,13 @@ class OAuth2AccessTokenFetcherImpl : public OAuth2AccessTokenFetcher, public net::URLFetcherDelegate { public: OAuth2AccessTokenFetcherImpl(OAuth2AccessTokenConsumer* consumer, - net::URLRequestContextGetter* getter); + net::URLRequestContextGetter* getter, + const std::string& refresh_token); virtual ~OAuth2AccessTokenFetcherImpl(); // Implementation of OAuth2AccessTokenFetcher virtual void Start(const std::string& client_id, const std::string& client_secret, - const std::string& refresh_token, const std::vector<std::string>& scopes) OVERRIDE; virtual void CancelRequest() OVERRIDE; @@ -97,13 +97,13 @@ class OAuth2AccessTokenFetcherImpl : public OAuth2AccessTokenFetcher, // State that is set during construction. net::URLRequestContextGetter* const getter_; + std::string refresh_token_; State state_; // While a fetch is in progress. scoped_ptr<net::URLFetcher> fetcher_; std::string client_id_; std::string client_secret_; - std::string refresh_token_; std::vector<std::string> scopes_; friend class OAuth2AccessTokenFetcherImplTest; diff --git a/google_apis/gaia/oauth2_access_token_fetcher_impl_unittest.cc b/google_apis/gaia/oauth2_access_token_fetcher_impl_unittest.cc index 8bf60fe..f263932 100644 --- a/google_apis/gaia/oauth2_access_token_fetcher_impl_unittest.cc +++ b/google_apis/gaia/oauth2_access_token_fetcher_impl_unittest.cc @@ -86,7 +86,7 @@ class OAuth2AccessTokenFetcherImplTest : public testing::Test { OAuth2AccessTokenFetcherImplTest() : request_context_getter_(new net::TestURLRequestContextGetter( base::MessageLoopProxy::current())), - fetcher_(&consumer_, request_context_getter_) { + fetcher_(&consumer_, request_context_getter_, "refresh_token") { base::RunLoop().RunUntilIdle(); } @@ -125,8 +125,7 @@ TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_GetAccessTokenRequestFailure) { TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, std::string()); EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); - fetcher_.Start( - "client_id", "client_secret", "refresh_token", ScopeList()); + fetcher_.Start("client_id", "client_secret", ScopeList()); fetcher_.OnURLFetchComplete(url_fetcher); } @@ -135,8 +134,7 @@ TEST_F(OAuth2AccessTokenFetcherImplTest, TestURLFetcher* url_fetcher = SetupGetAccessToken(true, net::HTTP_FORBIDDEN, std::string()); EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); - fetcher_.Start( - "client_id", "client_secret", "refresh_token", ScopeList()); + fetcher_.Start("client_id", "client_secret", ScopeList()); fetcher_.OnURLFetchComplete(url_fetcher); } @@ -144,8 +142,7 @@ TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_Success) { TestURLFetcher* url_fetcher = SetupGetAccessToken(true, net::HTTP_OK, kValidTokenResponse); EXPECT_CALL(consumer_, OnGetTokenSuccess("at1", _)).Times(1); - fetcher_.Start( - "client_id", "client_secret", "refresh_token", ScopeList()); + fetcher_.Start("client_id", "client_secret", ScopeList()); fetcher_.OnURLFetchComplete(url_fetcher); } diff --git a/google_apis/gaia/oauth2_api_call_flow.cc b/google_apis/gaia/oauth2_api_call_flow.cc index 9688e5a..08312eb 100644 --- a/google_apis/gaia/oauth2_api_call_flow.cc +++ b/google_apis/gaia/oauth2_api_call_flow.cc @@ -109,7 +109,6 @@ void OAuth2ApiCallFlow::BeginMintAccessToken() { oauth2_access_token_fetcher_->Start( GaiaUrls::GetInstance()->oauth2_chrome_client_id(), GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), - refresh_token_, scopes_); } @@ -127,7 +126,7 @@ void OAuth2ApiCallFlow::EndMintAccessToken( } OAuth2AccessTokenFetcher* OAuth2ApiCallFlow::CreateAccessTokenFetcher() { - return new OAuth2AccessTokenFetcherImpl(this, context_); + return new OAuth2AccessTokenFetcherImpl(this, context_, refresh_token_); } void OAuth2ApiCallFlow::OnURLFetchComplete(const net::URLFetcher* source) { diff --git a/google_apis/gaia/oauth2_api_call_flow_unittest.cc b/google_apis/gaia/oauth2_api_call_flow_unittest.cc index 29202e8..754b15e 100644 --- a/google_apis/gaia/oauth2_api_call_flow_unittest.cc +++ b/google_apis/gaia/oauth2_api_call_flow_unittest.cc @@ -73,15 +73,15 @@ class MockUrlFetcherFactory : public ScopedURLFetcherFactory, class MockAccessTokenFetcher : public OAuth2AccessTokenFetcherImpl { public: MockAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, - net::URLRequestContextGetter* getter) - : OAuth2AccessTokenFetcherImpl(consumer, getter) {} + net::URLRequestContextGetter* getter, + const std::string& refresh_token) + : OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token) {} ~MockAccessTokenFetcher() {} - MOCK_METHOD4(Start, - void (const std::string& client_id, - const std::string& client_secret, - const std::string& refresh_token, - const std::vector<std::string>& scopes)); + MOCK_METHOD3(Start, + void(const std::string& client_id, + const std::string& client_secret, + const std::vector<std::string>& scopes)); }; class MockApiCallFlow : public OAuth2ApiCallFlow { @@ -110,13 +110,11 @@ class MockApiCallFlow : public OAuth2ApiCallFlow { class OAuth2ApiCallFlowTest : public testing::Test { protected: - void SetupAccessTokenFetcher( - const std::string& rt, const std::vector<std::string>& scopes) { + void SetupAccessTokenFetcher(const std::vector<std::string>& scopes) { EXPECT_CALL(*access_token_fetcher_, - Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), - GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), - rt, scopes)) - .Times(1); + Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), + GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), + scopes)).Times(1); EXPECT_CALL(*flow_, CreateAccessTokenFetcher()) .WillOnce(Return(access_token_fetcher_.release())); } @@ -146,8 +144,8 @@ class OAuth2ApiCallFlowTest : public testing::Test { message_loop_.message_loop_proxy()); flow_.reset(new MockApiCallFlow( request_context_getter, refresh_token, access_token, scopes)); - access_token_fetcher_.reset( - new MockAccessTokenFetcher(flow_.get(), request_context_getter)); + access_token_fetcher_.reset(new MockAccessTokenFetcher( + flow_.get(), request_context_getter, refresh_token)); } TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { @@ -188,7 +186,7 @@ TEST_F(OAuth2ApiCallFlowTest, SecondApiCallSucceeds) { CreateFlow(rt, at, scopes); TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); flow_->Start(); - SetupAccessTokenFetcher(rt, scopes); + SetupAccessTokenFetcher(scopes); flow_->OnURLFetchComplete(url_fetcher1); TestURLFetcher* url_fetcher2 = SetupApiCall(true, net::HTTP_OK); EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher2)); @@ -206,7 +204,7 @@ TEST_F(OAuth2ApiCallFlowTest, SecondApiCallFails) { CreateFlow(rt, at, scopes); TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); flow_->Start(); - SetupAccessTokenFetcher(rt, scopes); + SetupAccessTokenFetcher(scopes); flow_->OnURLFetchComplete(url_fetcher1); TestURLFetcher* url_fetcher2 = SetupApiCall(false, net::HTTP_UNAUTHORIZED); EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher2)); @@ -224,7 +222,7 @@ TEST_F(OAuth2ApiCallFlowTest, NewTokenGenerationFails) { CreateFlow(rt, at, scopes); TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_UNAUTHORIZED); flow_->Start(); - SetupAccessTokenFetcher(rt, scopes); + SetupAccessTokenFetcher(scopes); flow_->OnURLFetchComplete(url_fetcher); GoogleServiceAuthError error( GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); @@ -238,7 +236,7 @@ TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) { std::vector<std::string> scopes(CreateTestScopes()); CreateFlow(rt, std::string(), scopes); - SetupAccessTokenFetcher(rt, scopes); + SetupAccessTokenFetcher(scopes); TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); flow_->Start(); @@ -254,7 +252,7 @@ TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) { std::vector<std::string> scopes(CreateTestScopes()); CreateFlow(rt, std::string(), scopes); - SetupAccessTokenFetcher(rt, scopes); + SetupAccessTokenFetcher(scopes); TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY); EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher)); flow_->Start(); @@ -270,7 +268,7 @@ TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) { std::vector<std::string> scopes(CreateTestScopes()); CreateFlow(rt, std::string(), scopes); - SetupAccessTokenFetcher(rt, scopes); + SetupAccessTokenFetcher(scopes); GoogleServiceAuthError error( GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); diff --git a/google_apis/gaia/oauth2_token_service.cc b/google_apis/gaia/oauth2_token_service.cc index f466b8a..7a183b6 100644 --- a/google_apis/gaia/oauth2_token_service.cc +++ b/google_apis/gaia/oauth2_token_service.cc @@ -77,10 +77,8 @@ 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. +// Class that fetches an OAuth2 access token for a given account id and set of +// scopes. // // It aims to meet OAuth2TokenService's requirements on token fetching. Retry // mechanism is used to handle failures. @@ -110,14 +108,13 @@ void OAuth2TokenService::RequestImpl::InformConsumer( class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { public: // Creates a Fetcher and starts fetching an OAuth2 access token for - // |refresh_token| and |scopes| in the request context obtained by |getter|. + // |account_id| and |scopes| in the request context obtained by |getter|. // The given |oauth2_token_service| will be informed when fetching is done. static Fetcher* CreateAndStart(OAuth2TokenService* oauth2_token_service, const std::string& account_id, net::URLRequestContextGetter* getter, const std::string& client_id, const std::string& client_secret, - const std::string& refresh_token, const ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request); virtual ~Fetcher(); @@ -135,7 +132,6 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { void Cancel(); const ScopeSet& GetScopeSet() const; - const std::string& GetRefreshToken() const; const std::string& GetClientId() const; const std::string& GetAccountId() const; @@ -155,7 +151,6 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { net::URLRequestContextGetter* getter, const std::string& client_id, const std::string& client_secret, - const std::string& refresh_token, const OAuth2TokenService::ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request); void Start(); @@ -171,7 +166,6 @@ class OAuth2TokenService::Fetcher : public OAuth2AccessTokenConsumer { OAuth2TokenService* const oauth2_token_service_; scoped_refptr<net::URLRequestContextGetter> getter_; const std::string account_id_; - const std::string refresh_token_; const ScopeSet scopes_; std::vector<base::WeakPtr<RequestImpl> > waiting_requests_; @@ -200,7 +194,6 @@ OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart( net::URLRequestContextGetter* getter, const std::string& client_id, const std::string& client_secret, - const std::string& refresh_token, const OAuth2TokenService::ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request) { OAuth2TokenService::Fetcher* fetcher = new Fetcher( @@ -209,7 +202,6 @@ OAuth2TokenService::Fetcher* OAuth2TokenService::Fetcher::CreateAndStart( getter, client_id, client_secret, - refresh_token, scopes, waiting_request); fetcher->Start(); @@ -222,13 +214,11 @@ OAuth2TokenService::Fetcher::Fetcher( net::URLRequestContextGetter* getter, const std::string& client_id, const std::string& client_secret, - const std::string& refresh_token, const OAuth2TokenService::ScopeSet& scopes, base::WeakPtr<RequestImpl> waiting_request) : oauth2_token_service_(oauth2_token_service), getter_(getter), account_id_(account_id), - refresh_token_(refresh_token), scopes_(scopes), retry_number_(0), error_(GoogleServiceAuthError::SERVICE_UNAVAILABLE), @@ -236,7 +226,6 @@ OAuth2TokenService::Fetcher::Fetcher( client_secret_(client_secret) { DCHECK(oauth2_token_service_); DCHECK(getter_.get()); - DCHECK(refresh_token_.length()); waiting_requests_.push_back(waiting_request); } @@ -247,10 +236,11 @@ OAuth2TokenService::Fetcher::~Fetcher() { } void OAuth2TokenService::Fetcher::Start() { - fetcher_.reset(new OAuth2AccessTokenFetcherImpl(this, getter_.get())); + fetcher_.reset(oauth2_token_service_->CreateAccessTokenFetcher( + account_id_, getter_.get(), this)); + DCHECK(fetcher_); fetcher_->Start(client_id_, client_secret_, - refresh_token_, std::vector<std::string>(scopes_.begin(), scopes_.end())); retry_timer_.Stop(); } @@ -356,10 +346,6 @@ const OAuth2TokenService::ScopeSet& OAuth2TokenService::Fetcher::GetScopeSet() return scopes_; } -const std::string& OAuth2TokenService::Fetcher::GetRefreshToken() const { - return refresh_token_; -} - const std::string& OAuth2TokenService::Fetcher::GetClientId() const { return client_id_; } @@ -406,12 +392,6 @@ void OAuth2TokenService::RemoveDiagnosticsObserver( diagnostics_observer_list_.RemoveObserver(observer); } -bool OAuth2TokenService::RefreshTokenIsAvailable( - const std::string& account_id) const { - DCHECK(CalledOnValidThread()); - return !GetRefreshToken(account_id).empty(); -} - std::vector<std::string> OAuth2TokenService::GetAccounts() { return std::vector<std::string>(); } @@ -515,8 +495,6 @@ void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request, const std::string& client_id, const std::string& client_secret, const ScopeSet& scopes) { - std::string refresh_token = GetRefreshToken(account_id); - // If there is already a pending fetcher for |scopes| and |account_id|, // simply register this |request| for those results rather than starting // a new fetcher. @@ -536,7 +514,6 @@ void OAuth2TokenService::FetchOAuth2Token(RequestImpl* request, getter, client_id, client_secret, - refresh_token, scopes, request->AsWeakPtr()); } diff --git a/google_apis/gaia/oauth2_token_service.h b/google_apis/gaia/oauth2_token_service.h index 013e784..685b9db 100644 --- a/google_apis/gaia/oauth2_token_service.h +++ b/google_apis/gaia/oauth2_token_service.h @@ -26,6 +26,7 @@ class URLRequestContextGetter; } class GoogleServiceAuthError; +class OAuth2AccessTokenFetcher; // Abstract base class for a service that fetches and caches OAuth2 access // tokens. Concrete subclasses should implement GetRefreshToken to return @@ -172,7 +173,7 @@ class OAuth2TokenService : public base::NonThreadSafe { // Returns true if a refresh token exists for |account_id|. If false, calls to // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. - virtual bool RefreshTokenIsAvailable(const std::string& account_id) const; + virtual bool RefreshTokenIsAvailable(const std::string& account_id) const = 0; // Mark an OAuth2 |access_token| issued for |account_id| and |scopes| as // invalid. This should be done if the token was received from this class, @@ -228,10 +229,6 @@ class OAuth2TokenService : public base::NonThreadSafe { Consumer* const consumer_; }; - // Subclasses should return the maintained refresh token for |account_id|. - // If no token is available, return an empty string. - virtual std::string GetRefreshToken(const std::string& account_id) const = 0; - // Subclasses can override if they want to report errors to the user. virtual void UpdateAuthError( const std::string& account_id, @@ -274,6 +271,16 @@ class OAuth2TokenService : public base::NonThreadSafe { const std::string& client_secret, const ScopeSet& scopes); + // Creates an access token fetcher for the given account id. + // + // Subclasses should override to create an access token fetcher for the given + // |account_id|. This method is only called if subclasses use the default + // implementation of |FetchOAuth2Token|. + virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( + const std::string& account_id, + net::URLRequestContextGetter* getter, + OAuth2AccessTokenConsumer* consumer) = 0; + // Invalidates the |access_token| issued for |account_id|, |client_id| and // |scopes|. Virtual so it can be overriden for tests and for platform- // specifc behavior. diff --git a/google_apis/gaia/oauth2_token_service_unittest.cc b/google_apis/gaia/oauth2_token_service_unittest.cc index c2ae5f8..b79e361 100644 --- a/google_apis/gaia/oauth2_token_service_unittest.cc +++ b/google_apis/gaia/oauth2_token_service_unittest.cc @@ -9,6 +9,7 @@ #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_impl.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" @@ -55,18 +56,19 @@ class TestOAuth2TokenService : public OAuth2TokenService { // For testing: set the refresh token to be used. void set_refresh_token(const std::string& account_id, const std::string& refresh_token) { - refresh_tokens_[account_id] = refresh_token; + if (refresh_token.empty()) + refresh_tokens_.erase(account_id); + else + refresh_tokens_[account_id] = refresh_token; } - protected: - virtual std::string GetRefreshToken(const std::string& account_id) - const OVERRIDE { + virtual bool RefreshTokenIsAvailable(const std::string& account_id) const + OVERRIDE { std::map<std::string, std::string>::const_iterator it = refresh_tokens_.find(account_id); - if (it != refresh_tokens_.end()) - return it->second; - return std::string(); - } + + return it != refresh_tokens_.end(); + }; private: // OAuth2TokenService implementation. @@ -74,6 +76,17 @@ class TestOAuth2TokenService : public OAuth2TokenService { return request_context_getter_.get(); } + virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( + const std::string& account_id, + net::URLRequestContextGetter* getter, + OAuth2AccessTokenConsumer* consumer) OVERRIDE { + std::map<std::string, std::string>::const_iterator it = + refresh_tokens_.find(account_id); + DCHECK(it != refresh_tokens_.end()); + std::string refresh_token(it->second); + return new OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token); + }; + std::map<std::string, std::string> refresh_tokens_; scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; }; |