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> |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 // completed. | 70 // completed. |
71 virtual void OnGetTokenSuccess(const Request* request, | 71 virtual void OnGetTokenSuccess(const Request* request, |
72 const std::string& access_token, | 72 const std::string& access_token, |
73 const base::Time& expiration_time) = 0; | 73 const base::Time& expiration_time) = 0; |
74 virtual void OnGetTokenFailure(const Request* request, | 74 virtual void OnGetTokenFailure(const Request* request, |
75 const GoogleServiceAuthError& error) = 0; | 75 const GoogleServiceAuthError& error) = 0; |
76 }; | 76 }; |
77 | 77 |
78 // Classes that want to listen for token availability should implement this | 78 // Classes that want to listen for token availability should implement this |
79 // interface and register with the AddObserver() call. | 79 // interface and register with the AddObserver() call. |
80 // TODO(rogerta): may get rid of |error| argument for OnRefreshTokenRevoked() | |
81 // once we stop supporting ClientLogin. Need to evaluate if its still useful. | |
82 class Observer { | 80 class Observer { |
83 public: | 81 public: |
84 // Called whenever a new login-scoped refresh token is available for | 82 // Called whenever a new login-scoped refresh token is available for |
85 // account |account_id|. Once available, access tokens can be retrieved for | 83 // account |account_id|. Once available, access tokens can be retrieved for |
86 // this account. This is called during initial startup for each token | 84 // this account. This is called during initial startup for each token |
87 // loaded. | 85 // loaded. |
88 virtual void OnRefreshTokenAvailable(const std::string& account_id) {} | 86 virtual void OnRefreshTokenAvailable(const std::string& account_id) {} |
89 // Called whenever the login-scoped refresh token becomes unavailable for | 87 // Called whenever the login-scoped refresh token becomes unavailable for |
90 // account |account_id|. | 88 // account |account_id|. |
91 virtual void OnRefreshTokenRevoked(const std::string& account_id) {} | 89 virtual void OnRefreshTokenRevoked(const std::string& account_id) {} |
92 // Called after all refresh tokens are loaded during OAuth2TokenService | 90 // Called after all refresh tokens are loaded during OAuth2TokenService |
93 // startup. | 91 // startup. |
94 virtual void OnRefreshTokensLoaded() {} | 92 virtual void OnRefreshTokensLoaded() {} |
95 // Called after all refresh tokens are removed from OAuth2TokenService. | |
96 virtual void OnRefreshTokensCleared() {} | |
97 protected: | 93 protected: |
98 virtual ~Observer() {} | 94 virtual ~Observer() {} |
99 }; | 95 }; |
100 | 96 |
101 // A set of scopes in OAuth2 authentication. | 97 // A set of scopes in OAuth2 authentication. |
102 typedef std::set<std::string> ScopeSet; | 98 typedef std::set<std::string> ScopeSet; |
103 | 99 |
104 OAuth2TokenService(); | 100 OAuth2TokenService(); |
105 virtual ~OAuth2TokenService(); | 101 virtual ~OAuth2TokenService(); |
106 | 102 |
107 // Add or remove observers of this token service. | 103 // Add or remove observers of this token service. |
108 void AddObserver(Observer* observer); | 104 void AddObserver(Observer* observer); |
109 void RemoveObserver(Observer* observer); | 105 void RemoveObserver(Observer* observer); |
110 | 106 |
111 // Checks in the cache for a valid access token, and if not found starts | 107 // 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 | 108 // a request for an OAuth2 access token using the OAuth2 refresh token |
113 // maintained by this instance. The caller owns the returned Request. | 109 // 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 | 110 // |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 | 111 // the object that will be called back with results if the returned request |
116 // is not deleted. | 112 // is not deleted. |
117 // TODO(atwilson): Make this non-virtual when we change | 113 // TODO(atwilson): Make this non-virtual when we change |
118 // ProfileOAuth2TokenServiceRequestTest to use FakeProfileOAuth2TokenService. | 114 // ProfileOAuth2TokenServiceRequestTest to use FakeProfileOAuth2TokenService. |
119 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | 115 virtual scoped_ptr<Request> StartRequest(const std::string& account_id, |
116 const ScopeSet& scopes, | |
120 Consumer* consumer); | 117 Consumer* consumer); |
121 | 118 |
122 // This method does the same as |StartRequest| except it uses |client_id| and | 119 // This method does the same as |StartRequest| except it uses |client_id| and |
123 // |client_secret| to identify OAuth client app instead of using | 120 // |client_secret| to identify OAuth client app instead of using |
124 // Chrome's default values. | 121 // Chrome's default values. |
125 scoped_ptr<Request> StartRequestForClient( | 122 virtual scoped_ptr<Request> StartRequestForClient( |
123 const std::string& account_id, | |
126 const std::string& client_id, | 124 const std::string& client_id, |
127 const std::string& client_secret, | 125 const std::string& client_secret, |
128 const ScopeSet& scopes, | 126 const ScopeSet& scopes, |
129 Consumer* consumer); | 127 Consumer* consumer); |
130 | 128 |
131 // This method does the same as |StartRequest| except it uses the request | 129 // This method does the same as |StartRequest| except it uses the request |
132 // context given by |getter| instead of using the one returned by | 130 // context given by |getter| instead of using the one returned by |
133 // |GetRequestContext| implemented by derived classes. | 131 // |GetRequestContext| implemented by derived classes. |
134 scoped_ptr<Request> StartRequestWithContext( | 132 virtual scoped_ptr<Request> StartRequestWithContext( |
133 const std::string& account_id, | |
135 net::URLRequestContextGetter* getter, | 134 net::URLRequestContextGetter* getter, |
136 const ScopeSet& scopes, | 135 const ScopeSet& scopes, |
137 Consumer* consumer); | 136 Consumer* consumer); |
Roger Tawa OOO till Jul 10th
2013/08/26 21:04:53
Do the three StartRequest() methods need to be vir
| |
138 | 137 |
139 // Returns true if a refresh token exists. If false, calls to | 138 // Returns true if a refresh token exists. If false, calls to |
140 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. | 139 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. |
141 virtual bool RefreshTokenIsAvailable(); | 140 virtual bool RefreshTokenIsAvailable(const std::string& account_id); |
142 | 141 |
143 // Mark an OAuth2 access token as invalid. This should be done if the token | 142 // Mark an OAuth2 access token as invalid. This should be done if the token |
144 // was received from this class, but was not accepted by the server (e.g., | 143 // was received from this class, but was not accepted by the server (e.g., |
145 // the server returned 401 Unauthorized). The token will be removed from the | 144 // the server returned 401 Unauthorized). The token will be removed from the |
146 // cache for the given scopes. | 145 // cache for the given scopes. |
147 virtual void InvalidateToken(const ScopeSet& scopes, | 146 virtual void InvalidateToken(const ScopeSet& scopes, |
148 const std::string& invalid_token); | 147 const std::string& invalid_token); |
149 | 148 |
150 // Return the current number of entries in the cache. | 149 // Return the current number of entries in the cache. |
151 int cache_size_for_testing() const; | 150 int cache_size_for_testing() const; |
(...skipping 15 matching lines...) Expand all Loading... | |
167 const std::string& access_token, | 166 const std::string& access_token, |
168 const base::Time& expiration_date); | 167 const base::Time& expiration_date); |
169 | 168 |
170 private: | 169 private: |
171 // |consumer_| to call back when this request completes. | 170 // |consumer_| to call back when this request completes. |
172 Consumer* const consumer_; | 171 Consumer* const consumer_; |
173 }; | 172 }; |
174 | 173 |
175 // Subclasses should return the refresh token maintained. | 174 // Subclasses should return the refresh token maintained. |
176 // If no token is available, return an empty string. | 175 // If no token is available, return an empty string. |
177 virtual std::string GetRefreshToken() = 0; | 176 virtual std::string GetRefreshToken(const std::string& account_id) = 0; |
178 | 177 |
179 // Subclasses can override if they want to report errors to the user. | 178 // Subclasses can override if they want to report errors to the user. |
180 virtual void UpdateAuthError(const GoogleServiceAuthError& error); | 179 virtual void UpdateAuthError( |
180 const std::string& account_id, | |
181 const GoogleServiceAuthError& error); | |
181 | 182 |
182 // Add a new entry to the cache. | 183 // Add a new entry to the cache. |
183 // Subclasses can override if there are implementation-specific reasons | 184 // Subclasses can override if there are implementation-specific reasons |
184 // that an access token should ever not be cached. | 185 // that an access token should ever not be cached. |
185 virtual void RegisterCacheEntry(const std::string& refresh_token, | 186 virtual void RegisterCacheEntry(const std::string& refresh_token, |
186 const ScopeSet& scopes, | 187 const ScopeSet& scopes, |
187 const std::string& access_token, | 188 const std::string& access_token, |
188 const base::Time& expiration_date); | 189 const base::Time& expiration_date); |
189 | 190 |
190 // 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 |
(...skipping 12 matching lines...) Expand all Loading... | |
203 // Cancels all requests that are currently in progress. | 204 // Cancels all requests that are currently in progress. |
204 void CancelAllRequests(); | 205 void CancelAllRequests(); |
205 | 206 |
206 // Cancels all requests related to a given refresh token. | 207 // Cancels all requests related to a given refresh token. |
207 void CancelRequestsForToken(const std::string& refresh_token); | 208 void CancelRequestsForToken(const std::string& refresh_token); |
208 | 209 |
209 // Called by subclasses to notify observers. | 210 // Called by subclasses to notify observers. |
210 void FireRefreshTokenAvailable(const std::string& account_id); | 211 void FireRefreshTokenAvailable(const std::string& account_id); |
211 void FireRefreshTokenRevoked(const std::string& account_id); | 212 void FireRefreshTokenRevoked(const std::string& account_id); |
212 void FireRefreshTokensLoaded(); | 213 void FireRefreshTokensLoaded(); |
213 void FireRefreshTokensCleared(); | |
214 | 214 |
215 // Derived classes must provide a request context used for fetching access | 215 // Derived classes must provide a request context used for fetching access |
216 // tokens with the |StartRequest| method. | 216 // tokens with the |StartRequest| method. |
217 virtual net::URLRequestContextGetter* GetRequestContext() = 0; | 217 virtual net::URLRequestContextGetter* GetRequestContext() = 0; |
218 | 218 |
219 // Fetches an OAuth token for the specified client/scopes. Virtual so it can | 219 // Fetches an OAuth token for the specified client/scopes. Virtual so it can |
220 // be overridden for tests and for platform-specific behavior on Android. | 220 // be overridden for tests and for platform-specific behavior on Android. |
221 virtual void FetchOAuth2Token(RequestImpl* request, | 221 virtual void FetchOAuth2Token(RequestImpl* request, |
222 const std::string& account_id, | |
222 net::URLRequestContextGetter* getter, | 223 net::URLRequestContextGetter* getter, |
223 const std::string& client_id, | 224 const std::string& client_id, |
224 const std::string& client_secret, | 225 const std::string& client_secret, |
225 const ScopeSet& scopes); | 226 const ScopeSet& scopes); |
226 | 227 |
227 private: | 228 private: |
228 // Class that fetches an OAuth2 access token for a given set of scopes and | 229 // Class that fetches an OAuth2 access token for a given set of scopes and |
229 // OAuth2 refresh token. | 230 // OAuth2 refresh token. |
230 class Fetcher; | 231 class Fetcher; |
231 friend class Fetcher; | 232 friend class Fetcher; |
232 | 233 |
233 // Struct that contains the information of an OAuth2 access token. | 234 // Struct that contains the information of an OAuth2 access token. |
234 struct CacheEntry { | 235 struct CacheEntry { |
235 std::string access_token; | 236 std::string access_token; |
236 base::Time expiration_date; | 237 base::Time expiration_date; |
237 }; | 238 }; |
238 | 239 |
239 // This method does the same as |StartRequestWithContext| except it | 240 // This method does the same as |StartRequestWithContext| except it |
240 // uses |client_id| and |client_secret| to identify OAuth | 241 // uses |client_id| and |client_secret| to identify OAuth |
241 // client app instead of using Chrome's default values. | 242 // client app instead of using Chrome's default values. |
242 scoped_ptr<Request> StartRequestForClientWithContext( | 243 scoped_ptr<Request> StartRequestForClientWithContext( |
244 const std::string& account_id, | |
243 net::URLRequestContextGetter* getter, | 245 net::URLRequestContextGetter* getter, |
244 const std::string& client_id, | 246 const std::string& client_id, |
245 const std::string& client_secret, | 247 const std::string& client_secret, |
246 const ScopeSet& scopes, | 248 const ScopeSet& scopes, |
247 Consumer* consumer); | 249 Consumer* consumer); |
248 | 250 |
249 // Returns a currently valid OAuth2 access token for the given set of scopes, | 251 // Returns a currently valid OAuth2 access token for the given set of scopes, |
250 // or NULL if none have been cached. Note the user of this method should | 252 // or NULL if none have been cached. Note the user of this method should |
251 // ensure no entry with the same |scopes| is added before the usage of the | 253 // ensure no entry with the same |scopes| is added before the usage of the |
252 // returned entry is done. | 254 // returned entry is done. |
(...skipping 27 matching lines...) Expand all Loading... | |
280 // Makes sure list is empty on destruction. | 282 // Makes sure list is empty on destruction. |
281 ObserverList<Observer, true> observer_list_; | 283 ObserverList<Observer, true> observer_list_; |
282 | 284 |
283 // Maximum number of retries in fetching an OAuth2 access token. | 285 // Maximum number of retries in fetching an OAuth2 access token. |
284 static int max_fetch_retry_num_; | 286 static int max_fetch_retry_num_; |
285 | 287 |
286 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 288 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
287 }; | 289 }; |
288 | 290 |
289 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 291 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |