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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 93 |
94 // 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 |
95 // 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., |
96 // 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 |
97 // cache for the given scopes. | 97 // cache for the given scopes. |
98 virtual void InvalidateToken(const ScopeSet& scopes, | 98 virtual void InvalidateToken(const ScopeSet& scopes, |
99 const std::string& invalid_token); | 99 const std::string& invalid_token); |
100 | 100 |
101 // Return the current number of entries in the cache. | 101 // Return the current number of entries in the cache. |
102 int cache_size_for_testing() const; | 102 int cache_size_for_testing() const; |
| 103 void set_max_authorization_token_fetch_retries_for_testing(int max_retries); |
103 | 104 |
104 protected: | 105 protected: |
| 106 // Implements a cancelable |OAuth2TokenService::Request|, which should be |
| 107 // operated on the UI thread. |
| 108 // TODO(davidroche): move this out of header file. |
| 109 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>, |
| 110 public Request { |
| 111 public: |
| 112 // |consumer| is required to outlive this. |
| 113 explicit RequestImpl(Consumer* consumer); |
| 114 virtual ~RequestImpl(); |
| 115 |
| 116 // Informs |consumer_| that this request is completed. |
| 117 void InformConsumer(const GoogleServiceAuthError& error, |
| 118 const std::string& access_token, |
| 119 const base::Time& expiration_date); |
| 120 |
| 121 private: |
| 122 // |consumer_| to call back when this request completes. |
| 123 Consumer* const consumer_; |
| 124 }; |
| 125 |
105 // Subclasses should return the refresh token maintained. | 126 // Subclasses should return the refresh token maintained. |
106 // If no token is available, return an empty string. | 127 // If no token is available, return an empty string. |
107 virtual std::string GetRefreshToken() = 0; | 128 virtual std::string GetRefreshToken() = 0; |
108 | 129 |
109 // Subclasses can override if they want to report errors to the user. | 130 // Subclasses can override if they want to report errors to the user. |
110 virtual void UpdateAuthError(const GoogleServiceAuthError& error); | 131 virtual void UpdateAuthError(const GoogleServiceAuthError& error); |
111 | 132 |
112 // Add a new entry to the cache. | 133 // Add a new entry to the cache. |
113 // Subclasses can override if there are implementation-specific reasons | 134 // Subclasses can override if there are implementation-specific reasons |
114 // that an access token should ever not be cached. | 135 // that an access token should ever not be cached. |
115 virtual void RegisterCacheEntry(const std::string& refresh_token, | 136 virtual void RegisterCacheEntry(const std::string& refresh_token, |
116 const ScopeSet& scopes, | 137 const ScopeSet& scopes, |
117 const std::string& access_token, | 138 const std::string& access_token, |
118 const base::Time& expiration_date); | 139 const base::Time& expiration_date); |
119 | 140 |
120 // Returns true if GetCacheEntry would return a valid cache entry for the | 141 // Returns true if GetCacheEntry would return a valid cache entry for the |
121 // given scopes. | 142 // given scopes. |
122 bool HasCacheEntry(const ScopeSet& scopes); | 143 bool HasCacheEntry(const ScopeSet& scopes); |
123 | 144 |
124 // Posts a task to fire the Consumer callback with the cached token. Must | 145 // Posts a task to fire the Consumer callback with the cached token. Must |
125 // only be called if HasCacheEntry() returns true. | 146 // Must only be called if HasCacheEntry() returns true. |
126 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, | 147 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, |
127 Consumer* consumer); | 148 Consumer* consumer); |
128 | 149 |
129 // Clears the internal token cache. | 150 // Clears the internal token cache. |
130 void ClearCache(); | 151 void ClearCache(); |
131 | 152 |
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 private: | 153 private: |
152 // Class that fetches an OAuth2 access token for a given set of scopes and | 154 // Class that fetches an OAuth2 access token for a given set of scopes and |
153 // OAuth2 refresh token. | 155 // OAuth2 refresh token. |
154 class Fetcher; | 156 class Fetcher; |
155 friend class Fetcher; | 157 friend class Fetcher; |
156 | 158 |
157 // Struct that contains the information of an OAuth2 access token. | 159 // Struct that contains the information of an OAuth2 access token. |
158 struct CacheEntry { | 160 struct CacheEntry { |
159 std::string access_token; | 161 std::string access_token; |
160 base::Time expiration_date; | 162 base::Time expiration_date; |
(...skipping 21 matching lines...) Expand all Loading... |
182 // The cache of currently valid tokens. | 184 // The cache of currently valid tokens. |
183 typedef std::map<ScopeSet, CacheEntry> TokenCache; | 185 typedef std::map<ScopeSet, CacheEntry> TokenCache; |
184 TokenCache token_cache_; | 186 TokenCache token_cache_; |
185 | 187 |
186 // The parameters (refresh token and scope set) used to fetch an OAuth2 access | 188 // The parameters (refresh token and scope set) used to fetch an OAuth2 access |
187 // token. | 189 // token. |
188 typedef std::pair<std::string, ScopeSet> FetchParameters; | 190 typedef std::pair<std::string, ScopeSet> FetchParameters; |
189 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access | 191 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access |
190 // token using these parameters. | 192 // token using these parameters. |
191 std::map<FetchParameters, Fetcher*> pending_fetchers_; | 193 std::map<FetchParameters, Fetcher*> pending_fetchers_; |
| 194 // Maximum number of retries in fetching an OAuth2 access token. |
| 195 static int max_fetch_retry_num_; |
192 | 196 |
193 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 197 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
194 }; | 198 }; |
195 | 199 |
196 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 200 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |