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> |
11 | 11 |
12 #include "base/basictypes.h" | |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/time.h" | 16 |
16 #include "chrome/browser/profiles/profile_keyed_service.h" | 17 namespace base { |
17 #include "chrome/browser/signin/signin_global_error.h" | 18 class Time; |
18 #include "content/public/browser/notification_observer.h" | 19 } |
19 #include "content/public/browser/notification_registrar.h" | 20 |
20 #include "net/url_request/url_request_context_getter.h" | 21 namespace net { |
22 class URLRequestContextGetter; | |
23 } | |
21 | 24 |
22 class GoogleServiceAuthError; | 25 class GoogleServiceAuthError; |
23 class OAuth2AccessTokenConsumer; | |
24 class Profile; | |
25 | 26 |
26 // OAuth2TokenService is a ProfileKeyedService that retrieves OAuth2 access | 27 // Abstract base class for a service that fetches and caches OAuth2 access |
27 // tokens for a given set of scopes using the OAuth2 refresh token maintained by | 28 // tokens. Concrete subclasses should implement GetRefreshToken to return |
Mattias Nissler (ping if slow)
2013/04/03 11:33:16
nitty nit: This file seems to use only one space b
David Roche
2013/04/03 16:34:09
Done.
| |
28 // TokenService. All calls are expected from the UI thread. | 29 // the appropriate refresh token. |
30 // | |
31 // All calls are expected from the UI thread. | |
29 // | 32 // |
30 // To use this service, call StartRequest() with a given set of scopes and a | 33 // To use this service, call StartRequest() with a given set of scopes and a |
31 // consumer of the request results. The consumer is required to outlive the | 34 // consumer of the request results. The consumer is required to outlive the |
32 // request. The request can be deleted. The consumer may be called back | 35 // request. The request can be deleted. The consumer may be called back |
33 // asynchronously with the fetch results. | 36 // asynchronously with the fetch results. |
34 // | 37 // |
35 // - If the consumer is not called back before the request is deleted, it will | 38 // - If the consumer is not called back before the request is deleted, it will |
36 // never be called back. | 39 // never be called back. |
37 // Note in this case, the actual network requests are not canceled and the | 40 // Note in this case, the actual network requests are not canceled and the |
38 // cache will be populated with the fetched results; it is just the consumer | 41 // cache will be populated with the fetched results; it is just the consumer |
39 // callback that is aborted. | 42 // callback that is aborted. |
40 // | 43 // |
41 // - Otherwise the consumer will be called back with the request and the fetch | 44 // - Otherwise the consumer will be called back with the request and the fetch |
42 // results. | 45 // results. |
43 // | 46 // |
44 // The caller of StartRequest() owns the returned request and is responsible to | 47 // The caller of StartRequest() owns the returned request and is responsible to |
45 // delete the request even once the callback has been invoked. | 48 // delete the request even once the callback has been invoked. |
46 // | 49 class OAuth2TokenService { |
47 // Note the request should be started from the UI thread. To start a request | |
48 // from other thread, please use OAuth2TokenServiceRequest. | |
49 class OAuth2TokenService : public content::NotificationObserver, | |
50 public SigninGlobalError::AuthStatusProvider, | |
51 public ProfileKeyedService { | |
52 public: | 50 public: |
53 // Class representing a request that fetches an OAuth2 access token. | 51 // Class representing a request that fetches an OAuth2 access token. |
54 class Request { | 52 class Request { |
55 public: | 53 public: |
56 virtual ~Request(); | 54 virtual ~Request(); |
57 protected: | 55 protected: |
58 Request(); | 56 Request(); |
59 }; | 57 }; |
60 | 58 |
61 // Class representing the consumer of a Request passed to |StartRequest|, | 59 // Class representing the consumer of a Request passed to |StartRequest|, |
62 // which will be called back when the request completes. | 60 // which will be called back when the request completes. |
63 class Consumer { | 61 class Consumer { |
64 public: | 62 public: |
65 Consumer(); | 63 Consumer(); |
66 virtual ~Consumer(); | 64 virtual ~Consumer(); |
67 // |request| is a Request that is started by this consumer and has | 65 // |request| is a Request that is started by this consumer and has |
68 // completed. | 66 // completed. |
69 virtual void OnGetTokenSuccess(const Request* request, | 67 virtual void OnGetTokenSuccess(const Request* request, |
70 const std::string& access_token, | 68 const std::string& access_token, |
71 const base::Time& expiration_time) = 0; | 69 const base::Time& expiration_time) = 0; |
72 virtual void OnGetTokenFailure(const Request* request, | 70 virtual void OnGetTokenFailure(const Request* request, |
73 const GoogleServiceAuthError& error) = 0; | 71 const GoogleServiceAuthError& error) = 0; |
74 }; | 72 }; |
75 | 73 |
76 // A set of scopes in OAuth2 authentication. | 74 // A set of scopes in OAuth2 authentication. |
77 typedef std::set<std::string> ScopeSet; | 75 typedef std::set<std::string> ScopeSet; |
78 | 76 |
79 OAuth2TokenService(); | 77 explicit OAuth2TokenService(net::URLRequestContextGetter* getter); |
80 virtual ~OAuth2TokenService(); | 78 virtual ~OAuth2TokenService(); |
81 | 79 |
82 // Initializes this token service with the profile. | 80 // Checks in the cache for a valid access token, and if not found starts |
83 void Initialize(Profile* profile); | 81 // a request for an OAuth2 access token using the OAuth2 refresh token |
82 // maintained by this instance. The caller owns the returned Request. | |
83 // |scopes| is the set of scopes to get an access token for, |consumer| is | |
84 // the object that will be called back with results if the returned request | |
85 // is not deleted. | |
86 virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes, | |
87 Consumer* consumer); | |
84 | 88 |
85 // ProfileKeyedService implementation. | 89 // Returns true if a refresh token exists. If false, calls to |
86 virtual void Shutdown() OVERRIDE; | 90 // |StartRequest| will result in a Consumer::OnGetTokenFailure callback. |
91 bool RefreshTokenIsAvailable(); | |
87 | 92 |
88 // Starts a request for an OAuth2 access token using the OAuth2 refresh token | 93 // Return the current number of entries in the cache. |
89 // maintained by TokenService. The caller owns the returned Request. |scopes| | 94 int cache_size_for_testing() const; |
90 // is the set of scopes to get an access token for, |consumer| is the object | |
91 // that will be called back with results if the returned request is not | |
92 // deleted. | |
93 // Note the refresh token has been collected from TokenService when this | |
94 // method returns, and the request can continue even if TokenService clears | |
95 // its tokens after this method returns. This means that outstanding | |
96 // StartRequest actions will still complete even if the user signs out in the | |
97 // meantime. | |
98 virtual scoped_ptr<Request> StartRequest( | |
99 const ScopeSet& scopes, | |
100 OAuth2TokenService::Consumer* consumer); | |
101 | 95 |
102 // content::NotificationObserver | 96 // Terminate all in-flight requests, and cause all future requests to fail. |
103 virtual void Observe(int type, | 97 void Shutdown(); |
Mattias Nissler (ping if slow)
2013/04/03 11:33:16
Can't this just be done in the dtor?
David Roche
2013/04/03 16:34:09
I was concerned about clients that were holding th
Mattias Nissler (ping if slow)
2013/04/04 13:11:11
Well, lifetime management isn't simple in C++ :)
| |
104 const content::NotificationSource& source, | |
105 const content::NotificationDetails& details) OVERRIDE; | |
106 | 98 |
107 // SigninGlobalError::AuthStatusProvider implementation. | 99 protected: |
108 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; | 100 // Subclasses should return the refresh token maintained. |
101 // If no token is available, return an empty string. | |
102 virtual std::string GetRefreshToken() = 0; | |
103 | |
104 // Subclasses can override if they want to report errors to the user. | |
105 virtual void UpdateAuthError(const GoogleServiceAuthError& error); | |
106 | |
107 // Add a new entry to the cache. | |
108 // Subclasses can override if there are implementation-specific reasons | |
109 // that an access token should ever not be cached. | |
110 virtual void RegisterCacheEntry(const std::string& refresh_token, | |
111 const ScopeSet& scopes, | |
112 const std::string& access_token, | |
113 const base::Time& expiration_date); | |
114 | |
115 // Clears the internal token cache. | |
116 void ClearCache(); | |
109 | 117 |
110 private: | 118 private: |
111 // Class that fetches an OAuth2 access token for a given set of scopes and | 119 // Class that fetches an OAuth2 access token for a given set of scopes and |
112 // OAuth2 refresh token. | 120 // OAuth2 refresh token. |
113 class Fetcher; | 121 class Fetcher; |
114 friend class Fetcher; | 122 friend class Fetcher; |
115 // Implementation of Request. | 123 // Implementation of Request. |
116 class RequestImpl; | 124 class RequestImpl; |
117 | 125 |
118 // Informs the consumer of |request| fetch results. | 126 // Informs the consumer of |request| fetch results. |
119 static void InformConsumer( | 127 static void InformConsumer( |
120 base::WeakPtr<OAuth2TokenService::RequestImpl> request, | 128 base::WeakPtr<OAuth2TokenService::RequestImpl> request, |
121 GoogleServiceAuthError error, | 129 GoogleServiceAuthError error, |
122 std::string access_token, | 130 std::string access_token, |
123 base::Time expiration_date); | 131 base::Time expiration_date); |
124 | 132 |
125 // Struct that contains the information of an OAuth2 access token. | 133 // Struct that contains the information of an OAuth2 access token. |
126 struct CacheEntry { | 134 struct CacheEntry { |
127 std::string access_token; | 135 std::string access_token; |
128 base::Time expiration_date; | 136 base::Time expiration_date; |
129 }; | 137 }; |
130 | 138 |
131 // Returns a currently valid OAuth2 access token for the given set of scopes, | 139 // Returns a currently valid OAuth2 access token for the given set of scopes, |
132 // or NULL if none have been cached. Note the user of this method should | 140 // or NULL if none have been cached. Note the user of this method should |
133 // ensure no entry with the same |scopes| is added before the usage of the | 141 // ensure no entry with the same |scopes| is added before the usage of the |
134 // returned entry is done. | 142 // returned entry is done. |
135 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); | 143 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); |
136 // Registers a new access token in the cache if |refresh_token| is the one | |
137 // currently held by TokenService. | |
138 void RegisterCacheEntry(const std::string& refresh_token, | |
139 const ScopeSet& scopes, | |
140 const std::string& access_token, | |
141 const base::Time& expiration_date); | |
142 | 144 |
143 // Called when |fetcher| finishes fetching. | 145 // Called when |fetcher| finishes fetching. |
144 void OnFetchComplete(Fetcher* fetcher); | 146 void OnFetchComplete(Fetcher* fetcher); |
145 | 147 |
146 // Updates the internal cache of the result from the most-recently-completed | |
147 // auth request (used for reporting errors to the user). | |
148 void UpdateAuthError(const GoogleServiceAuthError& error); | |
149 | |
150 // The profile with which this instance was initialized, or NULL. | |
151 Profile* profile_; | |
152 | |
153 // The auth status from the most-recently-completed request. | |
154 GoogleServiceAuthError last_auth_error_; | |
155 | |
156 // Getter to use for fetchers. | 148 // Getter to use for fetchers. |
157 scoped_refptr<net::URLRequestContextGetter> getter_; | 149 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
158 | 150 |
159 // The cache of currently valid tokens. | 151 // The cache of currently valid tokens. |
160 typedef std::map<ScopeSet, CacheEntry> TokenCache; | 152 typedef std::map<ScopeSet, CacheEntry> TokenCache; |
161 TokenCache token_cache_; | 153 TokenCache token_cache_; |
162 | 154 |
163 // The parameters (refresh token and scope set) used to fetch an OAuth2 access | 155 // The parameters (refresh token and scope set) used to fetch an OAuth2 access |
164 // token. | 156 // token. |
165 typedef std::pair<std::string, ScopeSet> FetchParameters; | 157 typedef std::pair<std::string, ScopeSet> FetchParameters; |
166 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access | 158 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access |
167 // token using these parameters. | 159 // token using these parameters. |
168 std::map<FetchParameters, Fetcher*> pending_fetchers_; | 160 std::map<FetchParameters, Fetcher*> pending_fetchers_; |
169 | 161 |
170 // Registrar for notifications from the TokenService. | 162 bool shutdown_; |
171 content::NotificationRegistrar registrar_; | |
172 | 163 |
173 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 164 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
174 }; | 165 }; |
175 | 166 |
176 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 167 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |