Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: chrome/browser/signin/oauth2_token_service.h

Issue 12647008: Refactor OAuth2TokenService to have profile- and device-based implementations. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Small tweak; handle double Shutdown calls robustly. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 protected:
103 virtual void Observe(int type, 97 // Subclasses should return the refresh token maintained.
104 const content::NotificationSource& source, 98 // If no token is available, return an empty string.
105 const content::NotificationDetails& details) OVERRIDE; 99 virtual std::string GetRefreshToken() = 0;
106 100
107 // SigninGlobalError::AuthStatusProvider implementation. 101 // Subclasses can override if they want to report errors to the user.
108 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; 102 virtual void UpdateAuthError(const GoogleServiceAuthError& error);
103
104 // Add a new entry to the cache.
105 // Subclasses can override if there are implementation-specific reasons
106 // that an access token should ever not be cached.
107 virtual void RegisterCacheEntry(const std::string& refresh_token,
108 const ScopeSet& scopes,
109 const std::string& access_token,
110 const base::Time& expiration_date);
111
112 // Clears the internal token cache.
113 void ClearCache();
109 114
110 private: 115 private:
111 // Class that fetches an OAuth2 access token for a given set of scopes and 116 // Class that fetches an OAuth2 access token for a given set of scopes and
112 // OAuth2 refresh token. 117 // OAuth2 refresh token.
113 class Fetcher; 118 class Fetcher;
114 friend class Fetcher; 119 friend class Fetcher;
115 // Implementation of Request. 120 // Implementation of Request.
116 class RequestImpl; 121 class RequestImpl;
117 122
118 // Informs the consumer of |request| fetch results. 123 // Informs the consumer of |request| fetch results.
119 static void InformConsumer( 124 static void InformConsumer(
120 base::WeakPtr<OAuth2TokenService::RequestImpl> request, 125 base::WeakPtr<OAuth2TokenService::RequestImpl> request,
121 GoogleServiceAuthError error, 126 GoogleServiceAuthError error,
122 std::string access_token, 127 std::string access_token,
123 base::Time expiration_date); 128 base::Time expiration_date);
124 129
125 // Struct that contains the information of an OAuth2 access token. 130 // Struct that contains the information of an OAuth2 access token.
126 struct CacheEntry { 131 struct CacheEntry {
127 std::string access_token; 132 std::string access_token;
128 base::Time expiration_date; 133 base::Time expiration_date;
129 }; 134 };
130 135
131 // Returns a currently valid OAuth2 access token for the given set of scopes, 136 // 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 137 // 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 138 // ensure no entry with the same |scopes| is added before the usage of the
134 // returned entry is done. 139 // returned entry is done.
135 const CacheEntry* GetCacheEntry(const ScopeSet& scopes); 140 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 141
143 // Called when |fetcher| finishes fetching. 142 // Called when |fetcher| finishes fetching.
144 void OnFetchComplete(Fetcher* fetcher); 143 void OnFetchComplete(Fetcher* fetcher);
145 144
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. 145 // Getter to use for fetchers.
157 scoped_refptr<net::URLRequestContextGetter> getter_; 146 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
158 147
159 // The cache of currently valid tokens. 148 // The cache of currently valid tokens.
160 typedef std::map<ScopeSet, CacheEntry> TokenCache; 149 typedef std::map<ScopeSet, CacheEntry> TokenCache;
161 TokenCache token_cache_; 150 TokenCache token_cache_;
162 151
163 // The parameters (refresh token and scope set) used to fetch an OAuth2 access 152 // The parameters (refresh token and scope set) used to fetch an OAuth2 access
164 // token. 153 // token.
165 typedef std::pair<std::string, ScopeSet> FetchParameters; 154 typedef std::pair<std::string, ScopeSet> FetchParameters;
166 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access 155 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access
167 // token using these parameters. 156 // token using these parameters.
168 std::map<FetchParameters, Fetcher*> pending_fetchers_; 157 std::map<FetchParameters, Fetcher*> pending_fetchers_;
169 158
170 // Registrar for notifications from the TokenService.
171 content::NotificationRegistrar registrar_;
172
173 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); 159 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService);
174 }; 160 };
175 161
176 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ 162 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698