| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // Add or remove observers of this token service. | 108 // Add or remove observers of this token service. |
| 108 void AddObserver(Observer* observer); | 109 void AddObserver(Observer* observer); |
| 109 void RemoveObserver(Observer* observer); | 110 void RemoveObserver(Observer* observer); |
| 110 | 111 |
| 111 // Checks in the cache for a valid access token, and if not found starts | 112 // Checks in the cache for a valid access token, and if not found starts |
| 112 // a request for an OAuth2 access token using the OAuth2 refresh token | 113 // a request for an OAuth2 access token using the OAuth2 refresh token |
| 113 // maintained by this instance. The caller owns the returned Request. | 114 // maintained by this instance. The caller owns the returned Request. |
| 114 // |scopes| is the set of scopes to get an access token for, |consumer| is | 115 // |scopes| is the set of scopes to get an access token for, |consumer| is |
| 115 // the object that will be called back with results if the returned request | 116 // the object that will be called back with results if the returned request |
| 116 // is not deleted. | 117 // is not deleted. |
| 118 // TODO(atwilson): Make this non-virtual when we change |
| 119 // ProfileOAuth2TokenServiceRequestTest to use FakeProfileOAuth2TokenService. |
| 117 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | 120 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, |
| 118 Consumer* consumer); | 121 Consumer* consumer); |
| 119 | 122 |
| 120 // This method does the same as |StartRequest| except it uses |client_id| and | 123 // This method does the same as |StartRequest| except it uses |client_id| and |
| 121 // |client_secret| to identify OAuth client app instead of using | 124 // |client_secret| to identify OAuth client app instead of using |
| 122 // Chrome's default values. | 125 // Chrome's default values. |
| 123 virtual scoped_ptr<Request> StartRequestForClient( | 126 scoped_ptr<Request> StartRequestForClient( |
| 124 const std::string& client_id, | 127 const std::string& client_id, |
| 125 const std::string& client_secret, | 128 const std::string& client_secret, |
| 126 const ScopeSet& scopes, | 129 const ScopeSet& scopes, |
| 127 Consumer* consumer); | 130 Consumer* consumer); |
| 128 | 131 |
| 129 // This method does the same as |StartRequest| except it uses the request | 132 // This method does the same as |StartRequest| except it uses the request |
| 130 // context given by |getter| instead of using the one returned by | 133 // context given by |getter| instead of using the one returned by |
| 131 // |GetRequestContext| implemented by derived classes. | 134 // |GetRequestContext| implemented by derived classes. |
| 132 virtual scoped_ptr<Request> StartRequestWithContext( | 135 scoped_ptr<Request> StartRequestWithContext( |
| 133 net::URLRequestContextGetter* getter, | 136 net::URLRequestContextGetter* getter, |
| 134 const ScopeSet& scopes, | 137 const ScopeSet& scopes, |
| 135 Consumer* consumer); | 138 Consumer* consumer); |
| 136 | 139 |
| 137 // Returns true if a refresh token exists. If false, calls to | 140 // Returns true if a refresh token exists. If false, calls to |
| 138 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. | 141 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. |
| 139 virtual bool RefreshTokenIsAvailable(); | 142 virtual bool RefreshTokenIsAvailable(); |
| 140 | 143 |
| 141 // Mark an OAuth2 access token as invalid. This should be done if the token | 144 // Mark an OAuth2 access token as invalid. This should be done if the token |
| 142 // was received from this class, but was not accepted by the server (e.g., | 145 // 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... |
| 184 const ScopeSet& scopes, | 187 const ScopeSet& scopes, |
| 185 const std::string& access_token, | 188 const std::string& access_token, |
| 186 const base::Time& expiration_date); | 189 const base::Time& expiration_date); |
| 187 | 190 |
| 188 // Returns true if GetCacheEntry would return a valid cache entry for the | 191 // Returns true if GetCacheEntry would return a valid cache entry for the |
| 189 // given scopes. | 192 // given scopes. |
| 190 bool HasCacheEntry(const ScopeSet& scopes); | 193 bool HasCacheEntry(const ScopeSet& scopes); |
| 191 | 194 |
| 192 // Posts a task to fire the Consumer callback with the cached token. Must | 195 // Posts a task to fire the Consumer callback with the cached token. Must |
| 193 // Must only be called if HasCacheEntry() returns true. | 196 // Must only be called if HasCacheEntry() returns true. |
| 194 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, | 197 void StartCacheLookupRequest(RequestImpl* request, |
| 195 Consumer* consumer); | 198 const ScopeSet& scopes, |
| 199 Consumer* consumer); |
| 196 | 200 |
| 197 // Clears the internal token cache. | 201 // Clears the internal token cache. |
| 198 void ClearCache(); | 202 void ClearCache(); |
| 199 | 203 |
| 200 // Cancels all requests that are currently in progress. | 204 // Cancels all requests that are currently in progress. |
| 201 void CancelAllRequests(); | 205 void CancelAllRequests(); |
| 202 | 206 |
| 203 // Cancels all requests related to a given refresh token. | 207 // Cancels all requests related to a given refresh token. |
| 204 void CancelRequestsForToken(const std::string& refresh_token); | 208 void CancelRequestsForToken(const std::string& refresh_token); |
| 205 | 209 |
| 206 // Called by subclasses to notify observers. | 210 // Called by subclasses to notify observers. |
| 207 void FireRefreshTokenAvailable(const std::string& account_id); | 211 void FireRefreshTokenAvailable(const std::string& account_id); |
| 208 void FireRefreshTokenRevoked(const std::string& account_id, | 212 void FireRefreshTokenRevoked(const std::string& account_id, |
| 209 const GoogleServiceAuthError& error); | 213 const GoogleServiceAuthError& error); |
| 210 void FireRefreshTokensLoaded(); | 214 void FireRefreshTokensLoaded(); |
| 211 void FireRefreshTokensCleared(); | 215 void FireRefreshTokensCleared(); |
| 212 | 216 |
| 213 private: | |
| 214 // Derived classes must provide a request context used for fetching access | 217 // Derived classes must provide a request context used for fetching access |
| 215 // tokens with the |StartRequest| method. | 218 // tokens with the |StartRequest| method. |
| 216 virtual net::URLRequestContextGetter* GetRequestContext() = 0; | 219 virtual net::URLRequestContextGetter* GetRequestContext() = 0; |
| 217 | 220 |
| 221 // Fetches an OAuth token for the specified client/scopes. Virtual so it can |
| 222 // be overridden for tests and for platform-specific behavior on Android. |
| 223 virtual void FetchOAuth2Token(RequestImpl* request, |
| 224 net::URLRequestContextGetter* getter, |
| 225 const std::string& client_id, |
| 226 const std::string& client_secret, |
| 227 const ScopeSet& scopes); |
| 228 private: |
| 229 // This method does the same as |StartRequestWithContext| except it |
| 230 // uses |client_id| and |client_secret| to identify OAuth |
| 231 // client app instead of using Chrome's default values. |
| 232 scoped_ptr<Request> StartRequestForClientWithContext( |
| 233 net::URLRequestContextGetter* getter, |
| 234 const std::string& client_id, |
| 235 const std::string& client_secret, |
| 236 const ScopeSet& scopes, |
| 237 Consumer* consumer); |
| 238 |
| 218 // Class that fetches an OAuth2 access token for a given set of scopes and | 239 // Class that fetches an OAuth2 access token for a given set of scopes and |
| 219 // OAuth2 refresh token. | 240 // OAuth2 refresh token. |
| 220 class Fetcher; | 241 class Fetcher; |
| 221 friend class Fetcher; | 242 friend class Fetcher; |
| 222 | 243 |
| 223 // Struct that contains the information of an OAuth2 access token. | 244 // Struct that contains the information of an OAuth2 access token. |
| 224 struct CacheEntry { | 245 struct CacheEntry { |
| 225 std::string access_token; | 246 std::string access_token; |
| 226 base::Time expiration_date; | 247 base::Time expiration_date; |
| 227 }; | 248 }; |
| 228 | 249 |
| 229 // This method does the same as |StartRequestWithContext| except it | |
| 230 // uses |client_id| and |client_secret| to identify OAuth | |
| 231 // client app instead of using Chrome's default values. | |
| 232 scoped_ptr<Request> StartRequestForClientWithContext( | |
| 233 net::URLRequestContextGetter* getter, | |
| 234 const std::string& client_id, | |
| 235 const std::string& client_secret, | |
| 236 const ScopeSet& scopes, | |
| 237 Consumer* consumer); | |
| 238 | |
| 239 // Returns a currently valid OAuth2 access token for the given set of scopes, | 250 // Returns a currently valid OAuth2 access token for the given set of scopes, |
| 240 // or NULL if none have been cached. Note the user of this method should | 251 // or NULL if none have been cached. Note the user of this method should |
| 241 // ensure no entry with the same |scopes| is added before the usage of the | 252 // ensure no entry with the same |scopes| is added before the usage of the |
| 242 // returned entry is done. | 253 // returned entry is done. |
| 243 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); | 254 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); |
| 244 | 255 |
| 245 | 256 |
| 246 // Removes an access token for the given set of scopes from the cache. | 257 // Removes an access token for the given set of scopes from the cache. |
| 247 // Returns true if the entry was removed, otherwise false. | 258 // Returns true if the entry was removed, otherwise false. |
| 248 bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes, | 259 bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 270 // Makes sure list is empty on destruction. | 281 // Makes sure list is empty on destruction. |
| 271 ObserverList<Observer, true> observer_list_; | 282 ObserverList<Observer, true> observer_list_; |
| 272 | 283 |
| 273 // Maximum number of retries in fetching an OAuth2 access token. | 284 // Maximum number of retries in fetching an OAuth2 access token. |
| 274 static int max_fetch_retry_num_; | 285 static int max_fetch_retry_num_; |
| 275 | 286 |
| 276 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 287 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
| 277 }; | 288 }; |
| 278 | 289 |
| 279 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 290 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| OLD | NEW |