| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 6 #define CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 17 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "google_apis/gaia/google_service_auth_error.h" | 19 #include "google_apis/gaia/google_service_auth_error.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 class Time; | 22 class Time; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 class URLRequestContextGetter; | 26 class URLRequestContextGetter; |
| 26 } | 27 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 42 // never be called back. | 43 // never be called back. |
| 43 // Note in this case, the actual network requests are not canceled and the | 44 // Note in this case, the actual network requests are not canceled and the |
| 44 // cache will be populated with the fetched results; it is just the consumer | 45 // cache will be populated with the fetched results; it is just the consumer |
| 45 // callback that is aborted. | 46 // callback that is aborted. |
| 46 // | 47 // |
| 47 // - Otherwise the consumer will be called back with the request and the fetch | 48 // - Otherwise the consumer will be called back with the request and the fetch |
| 48 // results. | 49 // results. |
| 49 // | 50 // |
| 50 // The caller of StartRequest() owns the returned request and is responsible to | 51 // The caller of StartRequest() owns the returned request and is responsible to |
| 51 // delete the request even once the callback has been invoked. | 52 // delete the request even once the callback has been invoked. |
| 52 class OAuth2TokenService { | 53 class OAuth2TokenService : public base::NonThreadSafe { |
| 53 public: | 54 public: |
| 54 // Class representing a request that fetches an OAuth2 access token. | 55 // Class representing a request that fetches an OAuth2 access token. |
| 55 class Request { | 56 class Request { |
| 56 public: | 57 public: |
| 57 virtual ~Request(); | 58 virtual ~Request(); |
| 58 protected: | 59 protected: |
| 59 Request(); | 60 Request(); |
| 60 }; | 61 }; |
| 61 | 62 |
| 62 // Class representing the consumer of a Request passed to |StartRequest|, | 63 // Class representing the consumer of a Request passed to |StartRequest|, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 // Add or remove observers of this token service. | 107 // Add or remove observers of this token service. |
| 107 void AddObserver(Observer* observer); | 108 void AddObserver(Observer* observer); |
| 108 void RemoveObserver(Observer* observer); | 109 void RemoveObserver(Observer* observer); |
| 109 | 110 |
| 110 // Checks in the cache for a valid access token, and if not found starts | 111 // Checks in the cache for a valid access token, and if not found starts |
| 111 // a request for an OAuth2 access token using the OAuth2 refresh token | 112 // a request for an OAuth2 access token using the OAuth2 refresh token |
| 112 // maintained by this instance. The caller owns the returned Request. | 113 // maintained by this instance. The caller owns the returned Request. |
| 113 // |scopes| is the set of scopes to get an access token for, |consumer| is | 114 // |scopes| is the set of scopes to get an access token for, |consumer| is |
| 114 // the object that will be called back with results if the returned request | 115 // the object that will be called back with results if the returned request |
| 115 // is not deleted. | 116 // is not deleted. |
| 117 // TODO(atwilson): Make this non-virtual when we change |
| 118 // ProfileOAuth2TokenServiceRequestTest to use FakeProfileOAuth2TokenService. |
| 116 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | 119 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, |
| 117 Consumer* consumer); | 120 Consumer* consumer); |
| 118 | 121 |
| 119 // This method does the same as |StartRequest| except it uses |client_id| and | 122 // This method does the same as |StartRequest| except it uses |client_id| and |
| 120 // |client_secret| to identify OAuth client app instead of using | 123 // |client_secret| to identify OAuth client app instead of using |
| 121 // Chrome's default values. | 124 // Chrome's default values. |
| 122 virtual scoped_ptr<Request> StartRequestForClient( | 125 scoped_ptr<Request> StartRequestForClient( |
| 123 const std::string& client_id, | 126 const std::string& client_id, |
| 124 const std::string& client_secret, | 127 const std::string& client_secret, |
| 125 const ScopeSet& scopes, | 128 const ScopeSet& scopes, |
| 126 Consumer* consumer); | 129 Consumer* consumer); |
| 127 | 130 |
| 128 // This method does the same as |StartRequest| except it uses the request | 131 // This method does the same as |StartRequest| except it uses the request |
| 129 // context given by |getter| instead of using the one returned by | 132 // context given by |getter| instead of using the one returned by |
| 130 // |GetRequestContext| implemented by derived classes. | 133 // |GetRequestContext| implemented by derived classes. |
| 131 virtual scoped_ptr<Request> StartRequestWithContext( | 134 scoped_ptr<Request> StartRequestWithContext( |
| 132 net::URLRequestContextGetter* getter, | 135 net::URLRequestContextGetter* getter, |
| 133 const ScopeSet& scopes, | 136 const ScopeSet& scopes, |
| 134 Consumer* consumer); | 137 Consumer* consumer); |
| 135 | 138 |
| 136 // Returns true if a refresh token exists. If false, calls to | 139 // Returns true if a refresh token exists. If false, calls to |
| 137 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. | 140 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. |
| 138 virtual bool RefreshTokenIsAvailable(); | 141 virtual bool RefreshTokenIsAvailable(); |
| 139 | 142 |
| 140 // Mark an OAuth2 access token as invalid. This should be done if the token | 143 // Mark an OAuth2 access token as invalid. This should be done if the token |
| 141 // was received from this class, but was not accepted by the server (e.g., | 144 // was received from this class, but was not accepted by the server (e.g., |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 const ScopeSet& scopes, | 186 const ScopeSet& scopes, |
| 184 const std::string& access_token, | 187 const std::string& access_token, |
| 185 const base::Time& expiration_date); | 188 const base::Time& expiration_date); |
| 186 | 189 |
| 187 // Returns true if GetCacheEntry would return a valid cache entry for the | 190 // Returns true if GetCacheEntry would return a valid cache entry for the |
| 188 // given scopes. | 191 // given scopes. |
| 189 bool HasCacheEntry(const ScopeSet& scopes); | 192 bool HasCacheEntry(const ScopeSet& scopes); |
| 190 | 193 |
| 191 // Posts a task to fire the Consumer callback with the cached token. Must | 194 // Posts a task to fire the Consumer callback with the cached token. Must |
| 192 // Must only be called if HasCacheEntry() returns true. | 195 // Must only be called if HasCacheEntry() returns true. |
| 193 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, | 196 void StartCacheLookupRequest(RequestImpl* request, |
| 194 Consumer* consumer); | 197 const ScopeSet& scopes, |
| 198 Consumer* consumer); |
| 195 | 199 |
| 196 // Clears the internal token cache. | 200 // Clears the internal token cache. |
| 197 void ClearCache(); | 201 void ClearCache(); |
| 198 | 202 |
| 199 // Cancels all requests that are currently in progress. | 203 // Cancels all requests that are currently in progress. |
| 200 void CancelAllRequests(); | 204 void CancelAllRequests(); |
| 201 | 205 |
| 202 // Cancels all requests related to a given refresh token. | 206 // Cancels all requests related to a given refresh token. |
| 203 void CancelRequestsForToken(const std::string& refresh_token); | 207 void CancelRequestsForToken(const std::string& refresh_token); |
| 204 | 208 |
| 205 // Called by subclasses to notify observers. | 209 // Called by subclasses to notify observers. |
| 206 void FireRefreshTokenAvailable(const std::string& account_id); | 210 void FireRefreshTokenAvailable(const std::string& account_id); |
| 207 void FireRefreshTokenRevoked(const std::string& account_id); | 211 void FireRefreshTokenRevoked(const std::string& account_id); |
| 208 void FireRefreshTokensLoaded(); | 212 void FireRefreshTokensLoaded(); |
| 209 void FireRefreshTokensCleared(); | 213 void FireRefreshTokensCleared(); |
| 210 | 214 |
| 211 private: | |
| 212 // Derived classes must provide a request context used for fetching access | 215 // Derived classes must provide a request context used for fetching access |
| 213 // tokens with the |StartRequest| method. | 216 // tokens with the |StartRequest| method. |
| 214 virtual net::URLRequestContextGetter* GetRequestContext() = 0; | 217 virtual net::URLRequestContextGetter* GetRequestContext() = 0; |
| 215 | 218 |
| 219 // Fetches an OAuth token for the specified client/scopes. Virtual so it can |
| 220 // be overridden for tests and for platform-specific behavior on Android. |
| 221 virtual void FetchOAuth2Token(RequestImpl* request, |
| 222 net::URLRequestContextGetter* getter, |
| 223 const std::string& client_id, |
| 224 const std::string& client_secret, |
| 225 const ScopeSet& scopes); |
| 226 private: |
| 216 // Class that fetches an OAuth2 access token for a given set of scopes and | 227 // Class that fetches an OAuth2 access token for a given set of scopes and |
| 217 // OAuth2 refresh token. | 228 // OAuth2 refresh token. |
| 218 class Fetcher; | 229 class Fetcher; |
| 219 friend class Fetcher; | 230 friend class Fetcher; |
| 220 | 231 |
| 221 // Struct that contains the information of an OAuth2 access token. | 232 // Struct that contains the information of an OAuth2 access token. |
| 222 struct CacheEntry { | 233 struct CacheEntry { |
| 223 std::string access_token; | 234 std::string access_token; |
| 224 base::Time expiration_date; | 235 base::Time expiration_date; |
| 225 }; | 236 }; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 // Makes sure list is empty on destruction. | 279 // Makes sure list is empty on destruction. |
| 269 ObserverList<Observer, true> observer_list_; | 280 ObserverList<Observer, true> observer_list_; |
| 270 | 281 |
| 271 // Maximum number of retries in fetching an OAuth2 access token. | 282 // Maximum number of retries in fetching an OAuth2 access token. |
| 272 static int max_fetch_retry_num_; | 283 static int max_fetch_retry_num_; |
| 273 | 284 |
| 274 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 285 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
| 275 }; | 286 }; |
| 276 | 287 |
| 277 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 288 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| OLD | NEW |