Chromium Code Reviews| 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/time.h" | 16 #include "base/time.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class Time; | 19 class Time; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 class URLRequestContextGetter; | 23 class URLRequestContextGetter; |
| 24 } | 24 } |
| 25 | 25 |
| 26 class GoogleServiceAuthError; | 26 class GoogleServiceAuthError; |
| 27 | 27 |
| 28 // Abstract base class for a service that fetches and caches OAuth2 access | 28 // Abstract base class for a service that fetches and caches OAuth2 access |
| 29 // tokens. Concrete subclasses should implement GetRefreshToken to return | 29 // tokens. Concrete subclasses should implement StartGetRefreshToken to provide |
| 30 // the appropriate refresh token. | 30 // the appropriate refresh token. |
| 31 // | 31 // |
| 32 // All calls are expected from the UI thread. | 32 // All calls are expected from the UI thread. |
| 33 // | 33 // |
| 34 // To use this service, call StartRequest() with a given set of scopes and a | 34 // To use this service, call StartRequest() with a given set of scopes and a |
| 35 // consumer of the request results. The consumer is required to outlive the | 35 // consumer of the request results. The consumer is required to outlive the |
| 36 // request. The request can be deleted. The consumer may be called back | 36 // request. The request can be deleted. The consumer may be called back |
| 37 // asynchronously with the fetch results. | 37 // asynchronously with the fetch results. |
| 38 // | 38 // |
| 39 // - If the consumer is not called back before the request is deleted, it will | 39 // - If the consumer is not called back before the request is deleted, it will |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 | 80 |
| 81 // Checks in the cache for a valid access token, and if not found starts | 81 // Checks in the cache for a valid access token, and if not found starts |
| 82 // a request for an OAuth2 access token using the OAuth2 refresh token | 82 // a request for an OAuth2 access token using the OAuth2 refresh token |
| 83 // maintained by this instance. The caller owns the returned Request. | 83 // maintained by this instance. The caller owns the returned Request. |
| 84 // |scopes| is the set of scopes to get an access token for, |consumer| is | 84 // |scopes| is the set of scopes to get an access token for, |consumer| is |
| 85 // the object that will be called back with results if the returned request | 85 // the object that will be called back with results if the returned request |
| 86 // is not deleted. | 86 // is not deleted. |
| 87 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | 87 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, |
| 88 Consumer* consumer); | 88 Consumer* consumer); |
| 89 | 89 |
| 90 // Returns true if a refresh token exists. If false, calls to | |
| 91 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. | |
| 92 bool RefreshTokenIsAvailable(); | |
| 93 | |
| 94 // Mark an OAuth2 access token as invalid. This should be done if the token | 90 // Mark an OAuth2 access token as invalid. This should be done if the token |
| 95 // was received from this class, but was not accepted by the server (e.g., | 91 // was received from this class, but was not accepted by the server (e.g., |
| 96 // the server returned 401 Unauthorized). The token will be removed from the | 92 // the server returned 401 Unauthorized). The token will be removed from the |
| 97 // cache for the given scopes. | 93 // cache for the given scopes. |
| 98 virtual void InvalidateToken(const ScopeSet& scopes, | 94 virtual void InvalidateToken(const ScopeSet& scopes, |
| 99 const std::string& invalid_token); | 95 const std::string& invalid_token); |
| 100 | 96 |
| 101 // Return the current number of entries in the cache. | 97 // Return the current number of entries in the cache. |
| 102 int cache_size_for_testing() const; | 98 int cache_size_for_testing() const; |
| 99 void set_max_authorization_token_fetch_retries_for_testing(int max_retries); | |
| 103 | 100 |
| 104 protected: | 101 protected: |
| 102 // Interface that StartGetRefreshToken calls back into. | |
| 103 class RefreshTokenValidationConsumer { | |
| 104 public: | |
| 105 virtual void OnRefreshTokenValidationComplete( | |
|
Mattias Nissler (ping if slow)
2013/06/19 17:53:17
this should just be a base::Callback
David Roche
2013/06/20 17:49:29
Removed completely.
| |
| 106 const std::string& refresh_token, | |
| 107 bool is_valid) = 0; | |
| 108 }; | |
| 109 | |
| 110 // Implements a cancelable |OAuth2TokenService::Request|, which should be | |
| 111 // operated on the UI thread. | |
| 112 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>, | |
| 113 public Request { | |
| 114 public: | |
| 115 // |consumer| is required to outlive this. | |
| 116 explicit RequestImpl(Consumer* consumer); | |
| 117 virtual ~RequestImpl(); | |
| 118 | |
| 119 // Informs |consumer_| that this request is completed. | |
| 120 void InformConsumer(const GoogleServiceAuthError& error, | |
| 121 const std::string& access_token, | |
| 122 const base::Time& expiration_date); | |
| 123 | |
| 124 private: | |
| 125 // |consumer_| to call back when this request completes. | |
| 126 Consumer* const consumer_; | |
| 127 }; | |
| 128 | |
| 105 // Subclasses should return the refresh token maintained. | 129 // Subclasses should return the refresh token maintained. |
| 106 // If no token is available, return an empty string. | 130 // If no token is available, return an empty string. |
| 107 virtual std::string GetRefreshToken() = 0; | 131 virtual std::string GetRefreshToken() = 0; |
| 108 | 132 |
| 133 // Subclasses can optionally implement this method to validate the given | |
| 134 // refresh token. Return true if the validation is started and the | |
| 135 // callback should be expected, or false if validation is skipped and the | |
| 136 // token should be used directly. This method may be invoked in parallel; | |
| 137 // subclasses must ensure that all consumers are notified of the results. | |
| 138 virtual bool StartRefreshTokenValidation( | |
| 139 const std::string refresh_token, | |
| 140 RefreshTokenValidationConsumer* consumer); | |
| 141 | |
| 109 // Subclasses can override if they want to report errors to the user. | 142 // Subclasses can override if they want to report errors to the user. |
| 110 virtual void UpdateAuthError(const GoogleServiceAuthError& error); | 143 virtual void UpdateAuthError(const GoogleServiceAuthError& error); |
| 111 | 144 |
| 112 // Add a new entry to the cache. | 145 // Add a new entry to the cache. |
| 113 // Subclasses can override if there are implementation-specific reasons | 146 // Subclasses can override if there are implementation-specific reasons |
| 114 // that an access token should ever not be cached. | 147 // that an access token should ever not be cached. |
| 115 virtual void RegisterCacheEntry(const std::string& refresh_token, | 148 virtual void RegisterCacheEntry(const std::string& refresh_token, |
| 116 const ScopeSet& scopes, | 149 const ScopeSet& scopes, |
| 117 const std::string& access_token, | 150 const std::string& access_token, |
| 118 const base::Time& expiration_date); | 151 const base::Time& expiration_date); |
| 119 | 152 |
| 120 // Returns true if GetCacheEntry would return a valid cache entry for the | 153 // Returns true if GetCacheEntry would return a valid cache entry for the |
| 121 // given scopes. | 154 // given scopes. |
| 122 bool HasCacheEntry(const ScopeSet& scopes); | 155 bool HasCacheEntry(const ScopeSet& scopes); |
| 123 | 156 |
| 124 // Posts a task to fire the Consumer callback with the cached token. Must | 157 // Posts a task to fetch the cached token for the given in-flight Request. |
| 125 // only be called if HasCacheEntry() returns true. | 158 // Must only be called if HasCacheEntry() returns true. |
| 126 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, | 159 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, |
| 127 Consumer* consumer); | 160 scoped_ptr<RequestImpl> request); |
| 128 | 161 |
| 129 // Clears the internal token cache. | 162 // Clears the internal token cache. |
| 130 void ClearCache(); | 163 void ClearCache(); |
| 131 | 164 |
| 132 // Implements a cancelable |OAuth2TokenService::Request|, which should be | |
| 133 // operated on the UI thread. | |
| 134 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>, | |
| 135 public Request { | |
| 136 public: | |
| 137 // |consumer| is required to outlive this. | |
| 138 explicit RequestImpl(Consumer* consumer); | |
| 139 virtual ~RequestImpl(); | |
| 140 | |
| 141 // Informs |consumer_| that this request is completed. | |
| 142 void InformConsumer(const GoogleServiceAuthError& error, | |
| 143 const std::string& access_token, | |
| 144 const base::Time& expiration_date); | |
| 145 | |
| 146 private: | |
| 147 // |consumer_| to call back when this request completes. | |
| 148 Consumer* const consumer_; | |
| 149 }; | |
| 150 | |
| 151 private: | 165 private: |
| 152 // Class that fetches an OAuth2 access token for a given set of scopes and | 166 // Class that fetches an OAuth2 access token for a given set of scopes and |
| 153 // OAuth2 refresh token. | 167 // OAuth2 refresh token. |
| 154 class Fetcher; | 168 class Fetcher; |
| 155 friend class Fetcher; | 169 friend class Fetcher; |
| 156 | 170 |
| 157 // Struct that contains the information of an OAuth2 access token. | 171 // Struct that contains the information of an OAuth2 access token. |
| 158 struct CacheEntry { | 172 struct CacheEntry { |
| 159 std::string access_token; | 173 std::string access_token; |
| 160 base::Time expiration_date; | 174 base::Time expiration_date; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 182 // The cache of currently valid tokens. | 196 // The cache of currently valid tokens. |
| 183 typedef std::map<ScopeSet, CacheEntry> TokenCache; | 197 typedef std::map<ScopeSet, CacheEntry> TokenCache; |
| 184 TokenCache token_cache_; | 198 TokenCache token_cache_; |
| 185 | 199 |
| 186 // The parameters (refresh token and scope set) used to fetch an OAuth2 access | 200 // The parameters (refresh token and scope set) used to fetch an OAuth2 access |
| 187 // token. | 201 // token. |
| 188 typedef std::pair<std::string, ScopeSet> FetchParameters; | 202 typedef std::pair<std::string, ScopeSet> FetchParameters; |
| 189 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access | 203 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access |
| 190 // token using these parameters. | 204 // token using these parameters. |
| 191 std::map<FetchParameters, Fetcher*> pending_fetchers_; | 205 std::map<FetchParameters, Fetcher*> pending_fetchers_; |
| 206 // Maximum number of retries in fetching an OAuth2 access token. | |
| 207 static int max_fetch_retry_num_; | |
| 192 | 208 |
| 193 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 209 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
| 194 }; | 210 }; |
| 195 | 211 |
| 196 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 212 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| OLD | NEW |