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

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

Issue 12647008: Refactor OAuth2TokenService to have profile- and device-based implementations. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: rebase again Created 7 years, 8 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
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>
11 11
12 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
15 #include "base/time.h" 16 #include "base/time.h"
16 #include "chrome/browser/profiles/profile_keyed_service.h" 17
17 #include "chrome/browser/signin/signin_global_error.h" 18 namespace base {
18 #include "content/public/browser/notification_observer.h" 19 class Time;
19 #include "content/public/browser/notification_registrar.h" 20 }
20 #include "net/url_request/url_request_context_getter.h" 21
22 namespace net {
23 class URLRequestContextGetter;
24 }
21 25
22 class GoogleServiceAuthError; 26 class GoogleServiceAuthError;
23 class OAuth2AccessTokenConsumer;
24 class Profile;
25 27
26 // OAuth2TokenService is a ProfileKeyedService that retrieves OAuth2 access 28 // Abstract base class for a service that fetches and caches OAuth2 access
27 // tokens for a given set of scopes using the OAuth2 refresh token maintained by 29 // tokens. Concrete subclasses should implement GetRefreshToken to return
28 // TokenService. All calls are expected from the UI thread. 30 // the appropriate refresh token.
31 //
32 // All calls are expected from the UI thread.
29 // 33 //
30 // 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
31 // 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
32 // 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
33 // asynchronously with the fetch results. 37 // asynchronously with the fetch results.
34 // 38 //
35 // - 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
36 // never be called back. 40 // never be called back.
37 // Note in this case, the actual network requests are not canceled and the 41 // Note in this case, the actual network requests are not canceled and the
38 // cache will be populated with the fetched results; it is just the consumer 42 // cache will be populated with the fetched results; it is just the consumer
39 // callback that is aborted. 43 // callback that is aborted.
40 // 44 //
41 // - Otherwise the consumer will be called back with the request and the fetch 45 // - Otherwise the consumer will be called back with the request and the fetch
42 // results. 46 // results.
43 // 47 //
44 // The caller of StartRequest() owns the returned request and is responsible to 48 // The caller of StartRequest() owns the returned request and is responsible to
45 // delete the request even once the callback has been invoked. 49 // delete the request even once the callback has been invoked.
46 // 50 class OAuth2TokenService {
47 // Note the request should be started from the UI thread. To start a request
48 // from other thread, please use OAuth2TokenServiceRequest.
49 class OAuth2TokenService : public content::NotificationObserver,
50 public SigninGlobalError::AuthStatusProvider,
51 public ProfileKeyedService {
52 public: 51 public:
53 // Class representing a request that fetches an OAuth2 access token. 52 // Class representing a request that fetches an OAuth2 access token.
54 class Request { 53 class Request {
55 public: 54 public:
56 virtual ~Request(); 55 virtual ~Request();
57 protected: 56 protected:
58 Request(); 57 Request();
59 }; 58 };
60 59
61 // Class representing the consumer of a Request passed to |StartRequest|, 60 // Class representing the consumer of a Request passed to |StartRequest|,
62 // which will be called back when the request completes. 61 // which will be called back when the request completes.
63 class Consumer { 62 class Consumer {
64 public: 63 public:
65 Consumer(); 64 Consumer();
66 virtual ~Consumer(); 65 virtual ~Consumer();
67 // |request| is a Request that is started by this consumer and has 66 // |request| is a Request that is started by this consumer and has
68 // completed. 67 // completed.
69 virtual void OnGetTokenSuccess(const Request* request, 68 virtual void OnGetTokenSuccess(const Request* request,
70 const std::string& access_token, 69 const std::string& access_token,
71 const base::Time& expiration_time) = 0; 70 const base::Time& expiration_time) = 0;
72 virtual void OnGetTokenFailure(const Request* request, 71 virtual void OnGetTokenFailure(const Request* request,
73 const GoogleServiceAuthError& error) = 0; 72 const GoogleServiceAuthError& error) = 0;
74 }; 73 };
75 74
76 // A set of scopes in OAuth2 authentication. 75 // A set of scopes in OAuth2 authentication.
77 typedef std::set<std::string> ScopeSet; 76 typedef std::set<std::string> ScopeSet;
78 77
79 OAuth2TokenService(); 78 explicit OAuth2TokenService(net::URLRequestContextGetter* getter);
80 virtual ~OAuth2TokenService(); 79 virtual ~OAuth2TokenService();
81 80
82 // Initializes this token service with the profile. 81 // Checks in the cache for a valid access token, and if not found starts
83 void Initialize(Profile* profile); 82 // a request for an OAuth2 access token using the OAuth2 refresh token
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
85 // the object that will be called back with results if the returned request
86 // is not deleted.
87 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes,
88 Consumer* consumer);
84 89
85 // ProfileKeyedService implementation. 90 // Returns true if a refresh token exists. If false, calls to
86 virtual void Shutdown() OVERRIDE; 91 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback.
87 92 bool RefreshTokenIsAvailable();
88 // Starts a request for an OAuth2 access token using the OAuth2 refresh token
89 // maintained by TokenService. The caller owns the returned Request. |scopes|
90 // is the set of scopes to get an access token for, |consumer| is the object
91 // that will be called back with results if the returned request is not
92 // deleted.
93 // Note the refresh token has been collected from TokenService when this
94 // method returns, and the request can continue even if TokenService clears
95 // its tokens after this method returns. This means that outstanding
96 // StartRequest actions will still complete even if the user signs out in the
97 // meantime.
98 virtual scoped_ptr<Request> StartRequest(
99 const ScopeSet& scopes,
100 OAuth2TokenService::Consumer* consumer);
101 93
102 // Mark an OAuth2 access token as invalid. This should be done if the token 94 // Mark an OAuth2 access token as invalid. This should be done if the token
103 // was received from this class, but was not accepted by the server (e.g., 95 // was received from this class, but was not accepted by the server (e.g.,
104 // the server returned 401 Unauthorized). The token will be removed from the 96 // the server returned 401 Unauthorized). The token will be removed from the
105 // cache for the given scopes. 97 // cache for the given scopes.
106 void InvalidateToken(const ScopeSet& scopes, 98 virtual void InvalidateToken(const ScopeSet& scopes,
107 const std::string& invalid_token); 99 const std::string& invalid_token);
108 100
109 // content::NotificationObserver 101 // Return the current number of entries in the cache.
110 virtual void Observe(int type, 102 int cache_size_for_testing() const;
111 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE;
113 103
114 // SigninGlobalError::AuthStatusProvider implementation. 104 protected:
115 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; 105 // Subclasses should return the refresh token maintained.
106 // If no token is available, return an empty string.
107 virtual std::string GetRefreshToken() = 0;
108
109 // Subclasses can override if they want to report errors to the user.
110 virtual void UpdateAuthError(const GoogleServiceAuthError& error);
111
112 // Add a new entry to the cache.
113 // Subclasses can override if there are implementation-specific reasons
114 // that an access token should ever not be cached.
115 virtual void RegisterCacheEntry(const std::string& refresh_token,
116 const ScopeSet& scopes,
117 const std::string& access_token,
118 const base::Time& expiration_date);
119
120 // Returns true if GetCacheEntry would return a valid cache entry for the
121 // given scopes.
122 bool HasCacheEntry(const ScopeSet& scopes);
123
124 // Posts a task to fire the Consumer callback with the cached token. Must
125 // only be called if HasCacheEntry() returns true.
126 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes,
127 Consumer* consumer);
128
129 // Clears the internal token cache.
130 void ClearCache();
131
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 // Informs the consumer of |request| fetch results.
152 static void InformConsumer(base::WeakPtr<RequestImpl> request,
153 const GoogleServiceAuthError& error,
154 const std::string& access_token,
155 const base::Time& expiration_date);
116 156
117 private: 157 private:
118 // Class that fetches an OAuth2 access token for a given set of scopes and 158 // Class that fetches an OAuth2 access token for a given set of scopes and
119 // OAuth2 refresh token. 159 // OAuth2 refresh token.
120 class Fetcher; 160 class Fetcher;
121 friend class Fetcher; 161 friend class Fetcher;
122 // Implementation of Request.
123 class RequestImpl;
124
125 // Informs the consumer of |request| fetch results.
126 static void InformConsumer(
127 base::WeakPtr<OAuth2TokenService::RequestImpl> request,
128 const GoogleServiceAuthError& error,
129 const std::string& access_token,
130 const base::Time& expiration_date);
131 162
132 // Struct that contains the information of an OAuth2 access token. 163 // Struct that contains the information of an OAuth2 access token.
133 struct CacheEntry { 164 struct CacheEntry {
134 std::string access_token; 165 std::string access_token;
135 base::Time expiration_date; 166 base::Time expiration_date;
136 }; 167 };
137 168
138 // Returns a currently valid OAuth2 access token for the given set of scopes, 169 // Returns a currently valid OAuth2 access token for the given set of scopes,
139 // or NULL if none have been cached. Note the user of this method should 170 // or NULL if none have been cached. Note the user of this method should
140 // ensure no entry with the same |scopes| is added before the usage of the 171 // ensure no entry with the same |scopes| is added before the usage of the
141 // returned entry is done. 172 // returned entry is done.
142 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); 173 const CacheEntry* GetCacheEntry(const ScopeSet& scopes);
143 174
144 // Registers a new access token in the cache if |refresh_token| is the one
145 // currently held by TokenService.
146 void RegisterCacheEntry(const std::string& refresh_token,
147 const ScopeSet& scopes,
148 const std::string& access_token,
149 const base::Time& expiration_date);
150 175
151 // Removes an access token for the given set of scopes from the cache. 176 // Removes an access token for the given set of scopes from the cache.
152 // Returns true if the entry was removed, otherwise false. 177 // Returns true if the entry was removed, otherwise false.
153 bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes, 178 bool RemoveCacheEntry(const OAuth2TokenService::ScopeSet& scopes,
154 const std::string& token_to_remove); 179 const std::string& token_to_remove);
155 180
156 181
157 // Called when |fetcher| finishes fetching. 182 // Called when |fetcher| finishes fetching.
158 void OnFetchComplete(Fetcher* fetcher); 183 void OnFetchComplete(Fetcher* fetcher);
159 184
160 // Updates the internal cache of the result from the most-recently-completed
161 // auth request (used for reporting errors to the user).
162 void UpdateAuthError(const GoogleServiceAuthError& error);
163
164 // The profile with which this instance was initialized, or NULL.
165 Profile* profile_;
166
167 // The auth status from the most-recently-completed request.
168 GoogleServiceAuthError last_auth_error_;
169
170 // Getter to use for fetchers. 185 // Getter to use for fetchers.
171 scoped_refptr<net::URLRequestContextGetter> getter_; 186 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
172 187
173 // The cache of currently valid tokens. 188 // The cache of currently valid tokens.
174 typedef std::map<ScopeSet, CacheEntry> TokenCache; 189 typedef std::map<ScopeSet, CacheEntry> TokenCache;
175 TokenCache token_cache_; 190 TokenCache token_cache_;
176 191
177 // The parameters (refresh token and scope set) used to fetch an OAuth2 access 192 // The parameters (refresh token and scope set) used to fetch an OAuth2 access
178 // token. 193 // token.
179 typedef std::pair<std::string, ScopeSet> FetchParameters; 194 typedef std::pair<std::string, ScopeSet> FetchParameters;
180 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access 195 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access
181 // token using these parameters. 196 // token using these parameters.
182 std::map<FetchParameters, Fetcher*> pending_fetchers_; 197 std::map<FetchParameters, Fetcher*> pending_fetchers_;
183 198
184 // Registrar for notifications from the TokenService.
185 content::NotificationRegistrar registrar_;
186
187 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); 199 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService);
188 }; 200 };
189 201
190 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ 202 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/browser/signin/android_profile_oauth2_token_service.cc ('k') | chrome/browser/signin/oauth2_token_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698