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