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

Side by Side Diff: google_apis/gaia/oauth2_token_service.h

Issue 23382008: Making OAuth2TokenService multi-login aware, updating callers, minor fixes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing a unit test, addressing comments from courage@ Created 7 years, 3 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_H_ 5 #ifndef GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_H_ 6 #define GOOGLE_APIS_GAIA_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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 // completed. 65 // completed.
66 virtual void OnGetTokenSuccess(const Request* request, 66 virtual void OnGetTokenSuccess(const Request* request,
67 const std::string& access_token, 67 const std::string& access_token,
68 const base::Time& expiration_time) = 0; 68 const base::Time& expiration_time) = 0;
69 virtual void OnGetTokenFailure(const Request* request, 69 virtual void OnGetTokenFailure(const Request* request,
70 const GoogleServiceAuthError& error) = 0; 70 const GoogleServiceAuthError& error) = 0;
71 }; 71 };
72 72
73 // Classes that want to listen for token availability should implement this 73 // Classes that want to listen for token availability should implement this
74 // interface and register with the AddObserver() call. 74 // interface and register with the AddObserver() call.
75 // TODO(rogerta): may get rid of |error| argument for OnRefreshTokenRevoked()
76 // once we stop supporting ClientLogin. Need to evaluate if its still useful.
77 class Observer { 75 class Observer {
78 public: 76 public:
79 // Called whenever a new login-scoped refresh token is available for 77 // Called whenever a new login-scoped refresh token is available for
80 // account |account_id|. Once available, access tokens can be retrieved for 78 // account |account_id|. Once available, access tokens can be retrieved for
81 // this account. This is called during initial startup for each token 79 // this account. This is called during initial startup for each token
82 // loaded. 80 // loaded.
83 virtual void OnRefreshTokenAvailable(const std::string& account_id) {} 81 virtual void OnRefreshTokenAvailable(const std::string& account_id) {}
84 // Called whenever the login-scoped refresh token becomes unavailable for 82 // Called whenever the login-scoped refresh token becomes unavailable for
85 // account |account_id|. 83 // account |account_id|.
86 virtual void OnRefreshTokenRevoked(const std::string& account_id) {} 84 virtual void OnRefreshTokenRevoked(const std::string& account_id) {}
87 // Called after all refresh tokens are loaded during OAuth2TokenService 85 // Called after all refresh tokens are loaded during OAuth2TokenService
88 // startup. 86 // startup.
89 virtual void OnRefreshTokensLoaded() {} 87 virtual void OnRefreshTokensLoaded() {}
90 // Called after all refresh tokens are removed from OAuth2TokenService.
91 virtual void OnRefreshTokensCleared() {}
92 protected: 88 protected:
93 virtual ~Observer() {} 89 virtual ~Observer() {}
94 }; 90 };
95 91
96 // A set of scopes in OAuth2 authentication. 92 // A set of scopes in OAuth2 authentication.
97 typedef std::set<std::string> ScopeSet; 93 typedef std::set<std::string> ScopeSet;
98 94
99 OAuth2TokenService(); 95 OAuth2TokenService();
100 virtual ~OAuth2TokenService(); 96 virtual ~OAuth2TokenService();
101 97
102 // Add or remove observers of this token service. 98 // Add or remove observers of this token service.
103 void AddObserver(Observer* observer); 99 void AddObserver(Observer* observer);
104 void RemoveObserver(Observer* observer); 100 void RemoveObserver(Observer* observer);
105 101
106 // Checks in the cache for a valid access token, and if not found starts 102 // Checks in the cache for a valid access token, and if not found starts
107 // a request for an OAuth2 access token using the OAuth2 refresh token 103 // a request for an OAuth2 access token using the OAuth2 refresh token
108 // maintained by this instance. The caller owns the returned Request. 104 // maintained by this instance. The caller owns the returned Request.
109 // |scopes| is the set of scopes to get an access token for, |consumer| is 105 // |scopes| is the set of scopes to get an access token for, |consumer| is
110 // the object that will be called back with results if the returned request 106 // the object that will be called back with results if the returned request
111 // is not deleted. 107 // is not deleted.
112 // TODO(atwilson): Make this non-virtual when we change 108 // TODO(atwilson): Make this non-virtual when we change
113 // ProfileOAuth2TokenServiceRequestTest to use FakeProfileOAuth2TokenService. 109 // ProfileOAuth2TokenServiceRequestTest to use FakeProfileOAuth2TokenService.
114 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, 110 virtual scoped_ptr<Request> StartRequest(const std::string& account_id,
Mattias Nissler (ping if slow) 2013/09/06 09:34:48 It'd be helpful to explain how account_id affects
fgorski 2013/09/12 23:46:24 Done. I've updated the documentation in the class
111 const ScopeSet& scopes,
115 Consumer* consumer); 112 Consumer* consumer);
116 113
117 // This method does the same as |StartRequest| except it uses |client_id| and 114 // This method does the same as |StartRequest| except it uses |client_id| and
118 // |client_secret| to identify OAuth client app instead of using 115 // |client_secret| to identify OAuth client app instead of using
119 // Chrome's default values. 116 // Chrome's default values.
120 scoped_ptr<Request> StartRequestForClient( 117 scoped_ptr<Request> StartRequestForClient(
118 const std::string& account_id,
121 const std::string& client_id, 119 const std::string& client_id,
122 const std::string& client_secret, 120 const std::string& client_secret,
123 const ScopeSet& scopes, 121 const ScopeSet& scopes,
124 Consumer* consumer); 122 Consumer* consumer);
125 123
126 // This method does the same as |StartRequest| except it uses the request 124 // This method does the same as |StartRequest| except it uses the request
127 // context given by |getter| instead of using the one returned by 125 // context given by |getter| instead of using the one returned by
128 // |GetRequestContext| implemented by derived classes. 126 // |GetRequestContext| implemented by derived classes.
129 scoped_ptr<Request> StartRequestWithContext( 127 scoped_ptr<Request> StartRequestWithContext(
128 const std::string& account_id,
130 net::URLRequestContextGetter* getter, 129 net::URLRequestContextGetter* getter,
131 const ScopeSet& scopes, 130 const ScopeSet& scopes,
132 Consumer* consumer); 131 Consumer* consumer);
133 132
133 // Lists account IDs of all accounts with a refresh token.
134 virtual std::vector<std::string> GetAccounts();
135
134 // Returns true if a refresh token exists. If false, calls to 136 // Returns true if a refresh token exists. If false, calls to
135 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. 137 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback.
136 virtual bool RefreshTokenIsAvailable(); 138 virtual bool RefreshTokenIsAvailable(const std::string& account_id);
137 139
138 // Mark an OAuth2 access token as invalid. This should be done if the token 140 // Mark an OAuth2 access token as invalid. This should be done if the token
139 // was received from this class, but was not accepted by the server (e.g., 141 // was received from this class, but was not accepted by the server (e.g.,
140 // the server returned 401 Unauthorized). The token will be removed from the 142 // the server returned 401 Unauthorized). The token will be removed from the
141 // cache for the given scopes. 143 // cache for the given scopes.
142 virtual void InvalidateToken(const ScopeSet& scopes, 144 virtual void InvalidateToken(const ScopeSet& scopes,
143 const std::string& invalid_token); 145 const std::string& invalid_token);
144 146
145 // Return the current number of entries in the cache. 147 // Return the current number of entries in the cache.
146 int cache_size_for_testing() const; 148 int cache_size_for_testing() const;
(...skipping 16 matching lines...) Expand all
163 const std::string& access_token, 165 const std::string& access_token,
164 const base::Time& expiration_date); 166 const base::Time& expiration_date);
165 167
166 private: 168 private:
167 // |consumer_| to call back when this request completes. 169 // |consumer_| to call back when this request completes.
168 Consumer* const consumer_; 170 Consumer* const consumer_;
169 }; 171 };
170 172
171 // Subclasses should return the refresh token maintained. 173 // Subclasses should return the refresh token maintained.
172 // If no token is available, return an empty string. 174 // If no token is available, return an empty string.
173 virtual std::string GetRefreshToken() = 0; 175 virtual std::string GetRefreshToken(const std::string& account_id) = 0;
174 176
175 // Subclasses can override if they want to report errors to the user. 177 // Subclasses can override if they want to report errors to the user.
176 virtual void UpdateAuthError(const GoogleServiceAuthError& error); 178 virtual void UpdateAuthError(
179 const std::string& account_id,
180 const GoogleServiceAuthError& error);
177 181
178 // Add a new entry to the cache. 182 // Add a new entry to the cache.
179 // Subclasses can override if there are implementation-specific reasons 183 // Subclasses can override if there are implementation-specific reasons
180 // that an access token should ever not be cached. 184 // that an access token should ever not be cached.
181 virtual void RegisterCacheEntry(const std::string& refresh_token, 185 virtual void RegisterCacheEntry(const std::string& refresh_token,
182 const ScopeSet& scopes, 186 const ScopeSet& scopes,
183 const std::string& access_token, 187 const std::string& access_token,
184 const base::Time& expiration_date); 188 const base::Time& expiration_date);
185 189
186 // 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
(...skipping 12 matching lines...) Expand all
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();
210 213
211 // Derived classes must provide a request context used for fetching access 214 // Derived classes must provide a request context used for fetching access
212 // tokens with the |StartRequest| method. 215 // tokens with the |StartRequest| method.
213 virtual net::URLRequestContextGetter* GetRequestContext() = 0; 216 virtual net::URLRequestContextGetter* GetRequestContext() = 0;
214 217
215 // Fetches an OAuth token for the specified client/scopes. Virtual so it can 218 // Fetches an OAuth token for the specified client/scopes. Virtual so it can
216 // be overridden for tests and for platform-specific behavior on Android. 219 // be overridden for tests and for platform-specific behavior on Android.
217 virtual void FetchOAuth2Token(RequestImpl* request, 220 virtual void FetchOAuth2Token(RequestImpl* request,
221 const std::string& account_id,
218 net::URLRequestContextGetter* getter, 222 net::URLRequestContextGetter* getter,
219 const std::string& client_id, 223 const std::string& client_id,
220 const std::string& client_secret, 224 const std::string& client_secret,
221 const ScopeSet& scopes); 225 const ScopeSet& scopes);
222 226
223 private: 227 private:
224 // Class that fetches an OAuth2 access token for a given set of scopes and 228 // Class that fetches an OAuth2 access token for a given set of scopes and
225 // OAuth2 refresh token. 229 // OAuth2 refresh token.
226 class Fetcher; 230 class Fetcher;
227 friend class Fetcher; 231 friend class Fetcher;
228 232
229 // Struct that contains the information of an OAuth2 access token. 233 // Struct that contains the information of an OAuth2 access token.
230 struct CacheEntry { 234 struct CacheEntry {
231 std::string access_token; 235 std::string access_token;
232 base::Time expiration_date; 236 base::Time expiration_date;
233 }; 237 };
234 238
235 // This method does the same as |StartRequestWithContext| except it 239 // This method does the same as |StartRequestWithContext| except it
236 // uses |client_id| and |client_secret| to identify OAuth 240 // uses |client_id| and |client_secret| to identify OAuth
237 // client app instead of using Chrome's default values. 241 // client app instead of using Chrome's default values.
238 scoped_ptr<Request> StartRequestForClientWithContext( 242 scoped_ptr<Request> StartRequestForClientWithContext(
243 const std::string& account_id,
239 net::URLRequestContextGetter* getter, 244 net::URLRequestContextGetter* getter,
240 const std::string& client_id, 245 const std::string& client_id,
241 const std::string& client_secret, 246 const std::string& client_secret,
242 const ScopeSet& scopes, 247 const ScopeSet& scopes,
243 Consumer* consumer); 248 Consumer* consumer);
244 249
245 // 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,
246 // 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
247 // 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
248 // returned entry is done. 253 // returned entry is done.
(...skipping 27 matching lines...) Expand all
276 // Makes sure list is empty on destruction. 281 // Makes sure list is empty on destruction.
277 ObserverList<Observer, true> observer_list_; 282 ObserverList<Observer, true> observer_list_;
278 283
279 // Maximum number of retries in fetching an OAuth2 access token. 284 // Maximum number of retries in fetching an OAuth2 access token.
280 static int max_fetch_retry_num_; 285 static int max_fetch_retry_num_;
281 286
282 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); 287 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService);
283 }; 288 };
284 289
285 #endif // GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_H_ 290 #endif // GOOGLE_APIS_GAIA_OAUTH2_TOKEN_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698