OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #ifndef CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ | 5 #ifndef CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ |
6 #define CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ | 6 #define CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
| 10 #include <vector> |
10 | 11 |
11 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | 14 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" |
14 #include "content/public/common/url_fetcher.h" | |
15 #include "content/public/common/url_fetcher_delegate.h" | |
16 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" |
17 | 17 |
18 class OAuth2AccessTokenFetcherTest; | 18 class OAuth2AccessTokenFetcherTest; |
19 | 19 |
20 namespace net { | 20 namespace net { |
| 21 class URLFetcher; |
21 class URLRequestContextGetter; | 22 class URLRequestContextGetter; |
22 class URLRequestStatus; | 23 class URLRequestStatus; |
23 } | 24 } |
24 | 25 |
25 // Abstracts the details to get OAuth2 access token token from | 26 // Abstracts the details to get OAuth2 access token token from |
26 // OAuth2 refresh token. | 27 // OAuth2 refresh token. |
27 // See "Using the Refresh Token" section in: | 28 // See "Using the Refresh Token" section in: |
28 // http://code.google.com/apis/accounts/docs/OAuth2WebServer.html | 29 // http://code.google.com/apis/accounts/docs/OAuth2WebServer.html |
29 // | 30 // |
30 // This class should be used on a single thread, but it can be whichever thread | 31 // This class should be used on a single thread, but it can be whichever thread |
31 // that you like. | 32 // that you like. |
32 // Also, do not reuse the same instance. Once Start() is called, the instance | 33 // Also, do not reuse the same instance. Once Start() is called, the instance |
33 // should not be reused. | 34 // should not be reused. |
34 // | 35 // |
35 // Usage: | 36 // Usage: |
36 // * Create an instance with a consumer. | 37 // * Create an instance with a consumer. |
37 // * Call Start() | 38 // * Call Start() |
38 // * The consumer passed in the constructor will be called on the same | 39 // * The consumer passed in the constructor will be called on the same |
39 // thread Start was called with the results. | 40 // thread Start was called with the results. |
40 // | 41 // |
41 // This class can handle one request at a time. To parallelize requests, | 42 // This class can handle one request at a time. To parallelize requests, |
42 // create multiple instances. | 43 // create multiple instances. |
43 class OAuth2AccessTokenFetcher : public content::URLFetcherDelegate { | 44 class OAuth2AccessTokenFetcher : public net::URLFetcherDelegate { |
44 public: | 45 public: |
45 OAuth2AccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, | 46 OAuth2AccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, |
46 net::URLRequestContextGetter* getter); | 47 net::URLRequestContextGetter* getter); |
47 virtual ~OAuth2AccessTokenFetcher(); | 48 virtual ~OAuth2AccessTokenFetcher(); |
48 | 49 |
49 // Starts the flow with the given parameters. | 50 // Starts the flow with the given parameters. |
50 // |scopes| can be empty. If it is empty then the access token will have the | 51 // |scopes| can be empty. If it is empty then the access token will have the |
51 // same scope as the refresh token. If not empty, then access token will have | 52 // same scope as the refresh token. If not empty, then access token will have |
52 // the scopes specified. In this case, the access token will successfully be | 53 // the scopes specified. In this case, the access token will successfully be |
53 // generated only if refresh token has login scope of a list of scopes that is | 54 // generated only if refresh token has login scope of a list of scopes that is |
54 // a super-set of the specified scopes. | 55 // a super-set of the specified scopes. |
55 virtual void Start(const std::string& client_id, | 56 virtual void Start(const std::string& client_id, |
56 const std::string& client_secret, | 57 const std::string& client_secret, |
57 const std::string& refresh_token, | 58 const std::string& refresh_token, |
58 const std::vector<std::string>& scopes); | 59 const std::vector<std::string>& scopes); |
59 | 60 |
60 void CancelRequest(); | 61 void CancelRequest(); |
61 | 62 |
62 // Implementation of content::URLFetcherDelegate | 63 // Implementation of net::URLFetcherDelegate |
63 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 64 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
64 | 65 |
65 private: | 66 private: |
66 enum State { | 67 enum State { |
67 INITIAL, | 68 INITIAL, |
68 GET_ACCESS_TOKEN_STARTED, | 69 GET_ACCESS_TOKEN_STARTED, |
69 GET_ACCESS_TOKEN_DONE, | 70 GET_ACCESS_TOKEN_DONE, |
70 ERROR_STATE, | 71 ERROR_STATE, |
71 }; | 72 }; |
72 | 73 |
(...skipping 14 matching lines...) Expand all Loading... |
87 const std::vector<std::string>& scopes); | 88 const std::vector<std::string>& scopes); |
88 static bool ParseGetAccessTokenResponse(const net::URLFetcher* source, | 89 static bool ParseGetAccessTokenResponse(const net::URLFetcher* source, |
89 std::string* access_token); | 90 std::string* access_token); |
90 | 91 |
91 // State that is set during construction. | 92 // State that is set during construction. |
92 OAuth2AccessTokenConsumer* const consumer_; | 93 OAuth2AccessTokenConsumer* const consumer_; |
93 net::URLRequestContextGetter* const getter_; | 94 net::URLRequestContextGetter* const getter_; |
94 State state_; | 95 State state_; |
95 | 96 |
96 // While a fetch is in progress. | 97 // While a fetch is in progress. |
97 scoped_ptr<content::URLFetcher> fetcher_; | 98 scoped_ptr<net::URLFetcher> fetcher_; |
98 std::string client_id_; | 99 std::string client_id_; |
99 std::string client_secret_; | 100 std::string client_secret_; |
100 std::string refresh_token_; | 101 std::string refresh_token_; |
101 std::vector<std::string> scopes_; | 102 std::vector<std::string> scopes_; |
102 | 103 |
103 friend class OAuth2AccessTokenFetcherTest; | 104 friend class OAuth2AccessTokenFetcherTest; |
104 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest, | 105 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest, |
105 ParseGetAccessTokenResponse); | 106 ParseGetAccessTokenResponse); |
106 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest, | 107 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest, |
107 MakeGetAccessTokenBody); | 108 MakeGetAccessTokenBody); |
108 | 109 |
109 DISALLOW_COPY_AND_ASSIGN(OAuth2AccessTokenFetcher); | 110 DISALLOW_COPY_AND_ASSIGN(OAuth2AccessTokenFetcher); |
110 }; | 111 }; |
111 | 112 |
112 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ | 113 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ |
OLD | NEW |