Chromium Code Reviews| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 | 80 |
| 81 // Checks in the cache for a valid access token, and if not found starts | 81 // Checks in the cache for a valid access token, and if not found starts |
| 82 // a request for an OAuth2 access token using the OAuth2 refresh token | 82 // a request for an OAuth2 access token using the OAuth2 refresh token |
| 83 // maintained by this instance. The caller owns the returned Request. | 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 | 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 | 85 // the object that will be called back with results if the returned request |
| 86 // is not deleted. | 86 // is not deleted. |
| 87 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | 87 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, |
| 88 Consumer* consumer); | 88 Consumer* consumer); |
| 89 | 89 |
| 90 // Returns true if a refresh token exists. If false, calls to | |
| 91 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. | |
| 92 bool RefreshTokenIsAvailable(); | |
| 93 | |
| 94 // Mark an OAuth2 access token as invalid. This should be done if the token | 90 // 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., | 91 // 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 | 92 // the server returned 401 Unauthorized). The token will be removed from the |
| 97 // cache for the given scopes. | 93 // cache for the given scopes. |
| 98 virtual void InvalidateToken(const ScopeSet& scopes, | 94 virtual void InvalidateToken(const ScopeSet& scopes, |
| 99 const std::string& invalid_token); | 95 const std::string& invalid_token); |
| 100 | 96 |
| 101 // Return the current number of entries in the cache. | 97 // Return the current number of entries in the cache. |
| 102 int cache_size_for_testing() const; | 98 int cache_size_for_testing() const; |
| 99 void set_max_authorization_token_fetch_retries_for_testing(int max_retries); | |
|
Andrew T Wilson (Slow)
2013/06/20 17:47:28
can we inject this in the constructor instead and
David Roche
2013/06/20 18:17:18
Passing this to the constructor would require plum
| |
| 103 | 100 |
| 104 protected: | 101 protected: |
| 102 // Implements a cancelable |OAuth2TokenService::Request|, which should be | |
| 103 // operated on the UI thread. | |
| 104 class RequestImpl : public base::SupportsWeakPtr<RequestImpl>, | |
|
Andrew T Wilson (Slow)
2013/06/20 17:47:28
This is fine, but I wonder now if it's time to mov
David Roche
2013/06/20 18:17:18
Ideally this would just be hidden, since it's an i
| |
| 105 public Request { | |
| 106 public: | |
| 107 // |consumer| is required to outlive this. | |
| 108 explicit RequestImpl(Consumer* consumer); | |
| 109 virtual ~RequestImpl(); | |
| 110 | |
| 111 // Informs |consumer_| that this request is completed. | |
| 112 void InformConsumer(const GoogleServiceAuthError& error, | |
| 113 const std::string& access_token, | |
| 114 const base::Time& expiration_date); | |
| 115 | |
| 116 private: | |
| 117 // |consumer_| to call back when this request completes. | |
| 118 Consumer* const consumer_; | |
| 119 }; | |
| 120 | |
| 105 // Subclasses should return the refresh token maintained. | 121 // Subclasses should return the refresh token maintained. |
| 106 // If no token is available, return an empty string. | 122 // If no token is available, return an empty string. |
| 107 virtual std::string GetRefreshToken() = 0; | 123 virtual std::string GetRefreshToken() = 0; |
| 108 | 124 |
| 109 // Subclasses can override if they want to report errors to the user. | 125 // Subclasses can override if they want to report errors to the user. |
| 110 virtual void UpdateAuthError(const GoogleServiceAuthError& error); | 126 virtual void UpdateAuthError(const GoogleServiceAuthError& error); |
| 111 | 127 |
| 112 // Add a new entry to the cache. | 128 // Add a new entry to the cache. |
| 113 // Subclasses can override if there are implementation-specific reasons | 129 // Subclasses can override if there are implementation-specific reasons |
| 114 // that an access token should ever not be cached. | 130 // that an access token should ever not be cached. |
| 115 virtual void RegisterCacheEntry(const std::string& refresh_token, | 131 virtual void RegisterCacheEntry(const std::string& refresh_token, |
| 116 const ScopeSet& scopes, | 132 const ScopeSet& scopes, |
| 117 const std::string& access_token, | 133 const std::string& access_token, |
| 118 const base::Time& expiration_date); | 134 const base::Time& expiration_date); |
| 119 | 135 |
| 120 // Returns true if GetCacheEntry would return a valid cache entry for the | 136 // Returns true if GetCacheEntry would return a valid cache entry for the |
| 121 // given scopes. | 137 // given scopes. |
| 122 bool HasCacheEntry(const ScopeSet& scopes); | 138 bool HasCacheEntry(const ScopeSet& scopes); |
| 123 | 139 |
| 124 // Posts a task to fire the Consumer callback with the cached token. Must | 140 // Posts a task to fetch the cached token for the given in-flight Request. |
| 125 // only be called if HasCacheEntry() returns true. | 141 // Must only be called if HasCacheEntry() returns true. |
| 126 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, | 142 scoped_ptr<Request> StartCacheLookupRequest(const ScopeSet& scopes, |
| 127 Consumer* consumer); | 143 scoped_ptr<RequestImpl> request); |
| 128 | 144 |
| 129 // Clears the internal token cache. | 145 // Clears the internal token cache. |
| 130 void ClearCache(); | 146 void ClearCache(); |
| 131 | 147 |
| 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: | 148 private: |
| 152 // Class that fetches an OAuth2 access token for a given set of scopes and | 149 // Class that fetches an OAuth2 access token for a given set of scopes and |
| 153 // OAuth2 refresh token. | 150 // OAuth2 refresh token. |
| 154 class Fetcher; | 151 class Fetcher; |
| 155 friend class Fetcher; | 152 friend class Fetcher; |
| 156 | 153 |
| 157 // Struct that contains the information of an OAuth2 access token. | 154 // Struct that contains the information of an OAuth2 access token. |
| 158 struct CacheEntry { | 155 struct CacheEntry { |
| 159 std::string access_token; | 156 std::string access_token; |
| 160 base::Time expiration_date; | 157 base::Time expiration_date; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 182 // The cache of currently valid tokens. | 179 // The cache of currently valid tokens. |
| 183 typedef std::map<ScopeSet, CacheEntry> TokenCache; | 180 typedef std::map<ScopeSet, CacheEntry> TokenCache; |
| 184 TokenCache token_cache_; | 181 TokenCache token_cache_; |
| 185 | 182 |
| 186 // The parameters (refresh token and scope set) used to fetch an OAuth2 access | 183 // The parameters (refresh token and scope set) used to fetch an OAuth2 access |
| 187 // token. | 184 // token. |
| 188 typedef std::pair<std::string, ScopeSet> FetchParameters; | 185 typedef std::pair<std::string, ScopeSet> FetchParameters; |
| 189 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access | 186 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access |
| 190 // token using these parameters. | 187 // token using these parameters. |
| 191 std::map<FetchParameters, Fetcher*> pending_fetchers_; | 188 std::map<FetchParameters, Fetcher*> pending_fetchers_; |
| 189 // Maximum number of retries in fetching an OAuth2 access token. | |
| 190 static int max_fetch_retry_num_; | |
| 192 | 191 |
| 193 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 192 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
| 194 }; | 193 }; |
| 195 | 194 |
| 196 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 195 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| OLD | NEW |