1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "google_apis/gaia/oauth2_access_token_consumer.h"
14 #include "google_apis/gaia/oauth2_access_token_fetcher.h"
15 #include "net/url_request/url_fetcher_delegate.h"
18 class OAuth2AccessTokenFetcherImplTest
;
26 class URLRequestContextGetter
;
27 class URLRequestStatus
;
30 // Abstracts the details to get OAuth2 access token token from
31 // OAuth2 refresh token.
32 // See "Using the Refresh Token" section in:
33 // http://code.google.com/apis/accounts/docs/OAuth2WebServer.html
35 // This class should be used on a single thread, but it can be whichever thread
37 // Also, do not reuse the same instance. Once Start() is called, the instance
38 // should not be reused.
41 // * Create an instance with a consumer.
43 // * The consumer passed in the constructor will be called on the same
44 // thread Start was called with the results.
46 // This class can handle one request at a time. To parallelize requests,
47 // create multiple instances.
48 class OAuth2AccessTokenFetcherImpl
: public OAuth2AccessTokenFetcher
,
49 public net::URLFetcherDelegate
{
51 OAuth2AccessTokenFetcherImpl(OAuth2AccessTokenConsumer
* consumer
,
52 net::URLRequestContextGetter
* getter
,
53 const std::string
& refresh_token
);
54 virtual ~OAuth2AccessTokenFetcherImpl();
56 // Implementation of OAuth2AccessTokenFetcher
57 virtual void Start(const std::string
& client_id
,
58 const std::string
& client_secret
,
59 const std::vector
<std::string
>& scopes
) OVERRIDE
;
61 virtual void CancelRequest() OVERRIDE
;
63 // Implementation of net::URLFetcherDelegate
64 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
69 GET_ACCESS_TOKEN_STARTED
,
70 GET_ACCESS_TOKEN_DONE
,
74 // Helper methods for the flow.
75 void StartGetAccessToken();
76 void EndGetAccessToken(const net::URLFetcher
* source
);
78 // Helper mehtods for reporting back results.
79 void OnGetTokenSuccess(const std::string
& access_token
,
80 const base::Time
& expiration_time
);
81 void OnGetTokenFailure(const GoogleServiceAuthError
& error
);
84 static GURL
MakeGetAccessTokenUrl();
85 static std::string
MakeGetAccessTokenBody(
86 const std::string
& client_id
,
87 const std::string
& client_secret
,
88 const std::string
& refresh_token
,
89 const std::vector
<std::string
>& scopes
);
91 static bool ParseGetAccessTokenSuccessResponse(const net::URLFetcher
* source
,
92 std::string
* access_token
,
95 static bool ParseGetAccessTokenFailureResponse(const net::URLFetcher
* source
,
98 // State that is set during construction.
99 net::URLRequestContextGetter
* const getter_
;
100 std::string refresh_token_
;
103 // While a fetch is in progress.
104 scoped_ptr
<net::URLFetcher
> fetcher_
;
105 std::string client_id_
;
106 std::string client_secret_
;
107 std::vector
<std::string
> scopes_
;
109 friend class OAuth2AccessTokenFetcherImplTest
;
110 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherImplTest
,
111 ParseGetAccessTokenResponse
);
112 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherImplTest
,
113 MakeGetAccessTokenBody
);
115 DISALLOW_COPY_AND_ASSIGN(OAuth2AccessTokenFetcherImpl
);
118 #endif // GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_