Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: chrome/browser/signin/oauth2_token_service.h

Issue 22581003: Handling of multiple concurrent requests from different clients in OAuth2TokenService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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>
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 // 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
113 // maintained by this instance. The caller owns the returned Request. 113 // 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 114 // |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 115 // the object that will be called back with results if the returned request
116 // is not deleted. 116 // is not deleted.
117 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, 117 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes,
118 Consumer* consumer); 118 Consumer* consumer);
119 119
120 // This method does the same as |StartRequest| except it uses |client_id| and 120 // This method does the same as |StartRequest| except it uses |client_id| and
121 // |client_secret| to identify OAuth client app instead of using 121 // |client_secret| to identify OAuth client app instead of using
122 // Chrome's default values. 122 // Chrome's default values. |request_origin| is used to differentiate where
123 // request originates from. It's expected to be empty for requests from
124 // the internal chrome services while we will use webapp id for their
125 // requests.
123 virtual scoped_ptr<Request> StartRequestForClient( 126 virtual scoped_ptr<Request> StartRequestForClient(
127 const std::string& request_origin,
124 const std::string& client_id, 128 const std::string& client_id,
125 const std::string& client_secret, 129 const std::string& client_secret,
126 const ScopeSet& scopes, 130 const ScopeSet& scopes,
127 Consumer* consumer); 131 Consumer* consumer);
128 132
129 // This method does the same as |StartRequest| except it uses the request 133 // This method does the same as |StartRequest| except it uses the request
130 // context given by |getter| instead of using the one returned by 134 // context given by |getter| instead of using the one returned by
131 // |GetRequestContext| implemented by derived classes. 135 // |GetRequestContext| implemented by derived classes.
132 virtual scoped_ptr<Request> StartRequestWithContext( 136 virtual scoped_ptr<Request> StartRequestWithContext(
133 net::URLRequestContextGetter* getter, 137 net::URLRequestContextGetter* getter,
134 const ScopeSet& scopes, 138 const ScopeSet& scopes,
135 Consumer* consumer); 139 Consumer* consumer);
136 140
137 // Returns true if a refresh token exists. If false, calls to 141 // Returns true if a refresh token exists. If false, calls to
138 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. 142 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback.
139 virtual bool RefreshTokenIsAvailable(); 143 virtual bool RefreshTokenIsAvailable();
140 144
141 // Mark an OAuth2 access token as invalid. This should be done if the token 145 // 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., 146 // was received from this class, but was not accepted by the server (e.g.,
143 // the server returned 401 Unauthorized). The token will be removed from the 147 // the server returned 401 Unauthorized). The token will be removed from the
144 // cache for the given scopes. 148 // cache for the given scopes.
145 virtual void InvalidateToken(const ScopeSet& scopes, 149 virtual void InvalidateToken(const ScopeSet& scopes,
146 const std::string& invalid_token); 150 const std::string& invalid_token);
147 151
148 // Return the current number of entries in the cache. 152 // Return the current number of entries in the cache.
149 int cache_size_for_testing() const; 153 int cache_size_for_testing() const;
150 void set_max_authorization_token_fetch_retries_for_testing(int max_retries); 154 void set_max_authorization_token_fetch_retries_for_testing(int max_retries);
151 155
152 protected: 156 protected:
157 struct ClientScopeSet {
158 ClientScopeSet(const std::string& request_origin,
159 const std::string& client_id,
160 const ScopeSet& scopes);
161 bool operator<(const ClientScopeSet& set) const;
162
163 std::string request_origin;
164 std::string client_id;
165 ScopeSet scopes;
166 };
167
153 // Implements a cancelable |OAuth2TokenService::Request|, which should be 168 // Implements a cancelable |OAuth2TokenService::Request|, which should be
154 // operated on the UI thread. 169 // operated on the UI thread.
155 // TODO(davidroche): move this out of header file. 170 // TODO(davidroche): move this out of header file.
156 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>, 171 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>,
157 public Request { 172 public Request {
158 public: 173 public:
159 // |consumer| is required to outlive this. 174 // |consumer| is required to outlive this.
160 explicit RequestImpl(Consumer* consumer); 175 explicit RequestImpl(Consumer* consumer);
161 virtual ~RequestImpl(); 176 virtual ~RequestImpl();
162 177
(...skipping 10 matching lines...) Expand all
173 // Subclasses should return the refresh token maintained. 188 // Subclasses should return the refresh token maintained.
174 // If no token is available, return an empty string. 189 // If no token is available, return an empty string.
175 virtual std::string GetRefreshToken() = 0; 190 virtual std::string GetRefreshToken() = 0;
176 191
177 // Subclasses can override if they want to report errors to the user. 192 // Subclasses can override if they want to report errors to the user.
178 virtual void UpdateAuthError(const GoogleServiceAuthError& error); 193 virtual void UpdateAuthError(const GoogleServiceAuthError& error);
179 194
180 // Add a new entry to the cache. 195 // Add a new entry to the cache.
181 // Subclasses can override if there are implementation-specific reasons 196 // Subclasses can override if there are implementation-specific reasons
182 // that an access token should ever not be cached. 197 // that an access token should ever not be cached.
183 virtual void RegisterCacheEntry(const std::string& refresh_token, 198 virtual void RegisterCacheEntry(const std::string& request_origin,
199 const std::string& client_id,
200 const std::string& refresh_token,
184 const ScopeSet& scopes, 201 const ScopeSet& scopes,
185 const std::string& access_token, 202 const std::string& access_token,
186 const base::Time& expiration_date); 203 const base::Time& expiration_date);
187 204
188 // Returns true if GetCacheEntry would return a valid cache entry for the 205 // Returns true if GetCacheEntry would return a valid cache entry for the
189 // given scopes. 206 // given scopes.
190 bool HasCacheEntry(const ScopeSet& scopes); 207 bool HasCacheEntry(const ClientScopeSet& client_scopes);
191 208
192 // Posts a task to fire the Consumer callback with the cached token. Must 209 // Posts a task to fire the Consumer callback with the cached token. Must
193 // Must only be called if HasCacheEntry() returns true. 210 // Must only be called if HasCacheEntry() returns true.
194 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, 211 scoped_ptr<Request> StartCacheLookupRequest(
195 Consumer* consumer); 212 const ClientScopeSet& client_scopes,
213 Consumer* consumer);
196 214
197 // Clears the internal token cache. 215 // Clears the internal token cache.
198 void ClearCache(); 216 void ClearCache();
199 217
200 // Cancels all requests that are currently in progress. 218 // Cancels all requests that are currently in progress.
201 void CancelAllRequests(); 219 void CancelAllRequests();
202 220
203 // Cancels all requests related to a given refresh token. 221 // Cancels all requests related to a given refresh token.
204 void CancelRequestsForToken(const std::string& refresh_token); 222 void CancelRequestsForToken(const std::string& refresh_token);
205 223
206 // Called by subclasses to notify observers. 224 // Called by subclasses to notify observers.
207 void FireRefreshTokenAvailable(const std::string& account_id); 225 void FireRefreshTokenAvailable(const std::string& account_id);
208 void FireRefreshTokenRevoked(const std::string& account_id, 226 void FireRefreshTokenRevoked(const std::string& account_id,
209 const GoogleServiceAuthError& error); 227 const GoogleServiceAuthError& error);
210 void FireRefreshTokensLoaded(); 228 void FireRefreshTokensLoaded();
211 void FireRefreshTokensCleared(); 229 void FireRefreshTokensCleared();
212 230
213 private: 231 private:
232
233 // The parameters used to fetch an OAuth2 access token.
234 struct FetchParameters {
235 FetchParameters(const std::string& request_origin,
236 const std::string& client_id,
237 const std::string& refresh_token,
238 const ScopeSet& scopes);
239 bool operator<(const FetchParameters& params) const;
240
241 // Request origin identifier. It's empty for internal chrome services
242 // requests but the requests originating from webapps should be identified
243 // by their originating extension_id.
244 std::string request_origin;
245 // OAuth2 client id.
246 std::string client_id;
247 // Refresh token used for minting access tokens within this request.
248 std::string refresh_token;
249 // URL scopes for the requested access token.
250 ScopeSet scopes;
251 };
252
214 // Derived classes must provide a request context used for fetching access 253 // Derived classes must provide a request context used for fetching access
215 // tokens with the |StartRequest| method. 254 // tokens with the |StartRequest| method.
216 virtual net::URLRequestContextGetter* GetRequestContext() = 0; 255 virtual net::URLRequestContextGetter* GetRequestContext() = 0;
217 256
218 // Class that fetches an OAuth2 access token for a given set of scopes and 257 // Class that fetches an OAuth2 access token for a given set of scopes and
219 // OAuth2 refresh token. 258 // OAuth2 refresh token.
220 class Fetcher; 259 class Fetcher;
221 friend class Fetcher; 260 friend class Fetcher;
222 261
223 // Struct that contains the information of an OAuth2 access token. 262 // Struct that contains the information of an OAuth2 access token.
224 struct CacheEntry { 263 struct CacheEntry {
225 std::string access_token; 264 std::string access_token;
226 base::Time expiration_date; 265 base::Time expiration_date;
227 }; 266 };
228 267
229 // This method does the same as |StartRequestWithContext| except it 268 // This method does the same as |StartRequestWithContext| except it
230 // uses |client_id| and |client_secret| to identify OAuth 269 // uses |client_id| and |client_secret| to identify OAuth
231 // client app instead of using Chrome's default values. 270 // client app instead of using Chrome's default values.
232 scoped_ptr<Request> StartRequestForClientWithContext( 271 scoped_ptr<Request> StartRequestForClientWithContext(
233 net::URLRequestContextGetter* getter, 272 net::URLRequestContextGetter* getter,
273 const std::string& request_origin,
234 const std::string& client_id, 274 const std::string& client_id,
235 const std::string& client_secret, 275 const std::string& client_secret,
236 const ScopeSet& scopes, 276 const ScopeSet& scopes,
237 Consumer* consumer); 277 Consumer* consumer);
238 278
239 // Returns a currently valid OAuth2 access token for the given set of scopes, 279 // 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 280 // 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 281 // ensure no entry with the same |client_scopes| is added before the usage of
242 // returned entry is done. 282 // the returned entry is done.
243 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); 283 const CacheEntry* GetCacheEntry(const ClientScopeSet& client_scopes);
244 284
245 285
246 // Removes an access token for the given set of scopes from the cache. 286 // Removes an access token for the given set of scopes from the cache.
247 // Returns true if the entry was removed, otherwise false. 287 // Returns true if the entry was removed, otherwise false.
248 bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes, 288 bool RemoveCacheEntry(const ClientScopeSet& client_scopes,
249 const std::string& token_to_remove); 289 const std::string& token_to_remove);
250 290
251 291
252 // Called when |fetcher| finishes fetching. 292 // Called when |fetcher| finishes fetching.
253 void OnFetchComplete(Fetcher* fetcher); 293 void OnFetchComplete(Fetcher* fetcher);
254 294
255 // Called when a number of fetchers need to be canceled. 295 // Called when a number of fetchers need to be canceled.
256 void CancelFetchers(std::vector<Fetcher*> fetchers_to_cancel); 296 void CancelFetchers(std::vector<Fetcher*> fetchers_to_cancel);
257 297
258 // The cache of currently valid tokens. 298 // The cache of currently valid tokens.
259 typedef std::map<ScopeSet, CacheEntry> TokenCache; 299 typedef std::map<ClientScopeSet, CacheEntry> TokenCache;
260 TokenCache token_cache_; 300 TokenCache token_cache_;
261 301
262 // The parameters (refresh token and scope set) used to fetch an OAuth2 access
263 // token.
264 typedef std::pair<std::string, ScopeSet> FetchParameters;
265 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access 302 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access
266 // token using these parameters. 303 // token using these parameters.
267 std::map<FetchParameters, Fetcher*> pending_fetchers_; 304 std::map<FetchParameters, Fetcher*> pending_fetchers_;
268 305
269 // List of observers to notify when token availiability changes. 306 // List of observers to notify when token availiability changes.
270 // Makes sure list is empty on destruction. 307 // Makes sure list is empty on destruction.
271 ObserverList<Observer, true> observer_list_; 308 ObserverList<Observer, true> observer_list_;
272 309
273 // Maximum number of retries in fetching an OAuth2 access token. 310 // Maximum number of retries in fetching an OAuth2 access token.
274 static int max_fetch_retry_num_; 311 static int max_fetch_retry_num_;
275 312
276 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); 313 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService);
277 }; 314 };
278 315
279 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ 316 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698