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/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "chrome/browser/profiles/profile_keyed_service.h" | 16 #include "chrome/browser/profiles/profile_keyed_service.h" |
17 #include "chrome/browser/signin/signin_global_error.h" | |
18 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
20 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
21 | 20 |
22 class GoogleServiceAuthError; | 21 class GoogleServiceAuthError; |
23 class OAuth2AccessTokenConsumer; | 22 class OAuth2AccessTokenConsumer; |
24 class Profile; | 23 class Profile; |
25 | 24 |
26 // OAuth2TokenService is a ProfileKeyedService that retrieves OAuth2 access | 25 // OAuth2TokenService is a ProfileKeyedService that retrieves OAuth2 access |
27 // tokens for a given set of scopes using the OAuth2 refresh token maintained by | 26 // tokens for a given set of scopes using the OAuth2 refresh token maintained by |
(...skipping 12 matching lines...) Expand all Loading... |
40 // | 39 // |
41 // - Otherwise the consumer will be called back with the request and the fetch | 40 // - Otherwise the consumer will be called back with the request and the fetch |
42 // results. | 41 // results. |
43 // | 42 // |
44 // The caller of StartRequest() owns the returned request and is responsible to | 43 // The caller of StartRequest() owns the returned request and is responsible to |
45 // delete the request even once the callback has been invoked. | 44 // delete the request even once the callback has been invoked. |
46 // | 45 // |
47 // Note the request should be started from the UI thread. To start a request | 46 // Note the request should be started from the UI thread. To start a request |
48 // from other thread, please use OAuth2TokenServiceRequest. | 47 // from other thread, please use OAuth2TokenServiceRequest. |
49 class OAuth2TokenService : public content::NotificationObserver, | 48 class OAuth2TokenService : public content::NotificationObserver, |
50 public SigninGlobalError::AuthStatusProvider, | |
51 public ProfileKeyedService { | 49 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 |
(...skipping 14 matching lines...) Expand all Loading... |
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 OAuth2TokenService(); |
80 virtual ~OAuth2TokenService(); | 78 virtual ~OAuth2TokenService(); |
81 | 79 |
82 // Initializes this token service with the profile. | 80 // Initializes this token service with the profile. |
83 void Initialize(Profile* profile); | 81 void Initialize(Profile* profile); |
84 | 82 |
85 // ProfileKeyedService implementation. | |
86 virtual void Shutdown() OVERRIDE; | |
87 | |
88 // Starts a request for an OAuth2 access token using the OAuth2 refresh token | 83 // Starts a request for an OAuth2 access token using the OAuth2 refresh token |
89 // maintained by TokenService. The caller owns the returned Request. |scopes| | 84 // maintained by TokenService. The caller owns the returned Request. |scopes| |
90 // is the set of scopes to get an access token for, |consumer| is the object | 85 // 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 | 86 // that will be called back with results if the returned request is not |
92 // deleted. | 87 // deleted. |
93 // Note the refresh token has been collected from TokenService when this | 88 // Note the refresh token has been collected from TokenService when this |
94 // method returns, and the request can continue even if TokenService clears | 89 // method returns, and the request can continue even if TokenService clears |
95 // its tokens after this method returns. This means that outstanding | 90 // its tokens after this method returns. This means that outstanding |
96 // StartRequest actions will still complete even if the user signs out in the | 91 // StartRequest actions will still complete even if the user signs out in the |
97 // meantime. | 92 // meantime. |
98 virtual scoped_ptr<Request> StartRequest( | 93 virtual scoped_ptr<Request> StartRequest( |
99 const ScopeSet& scopes, | 94 const ScopeSet& scopes, |
100 OAuth2TokenService::Consumer* consumer); | 95 OAuth2TokenService::Consumer* consumer); |
101 | 96 |
102 // content::NotificationObserver | 97 // content::NotificationObserver |
103 virtual void Observe(int type, | 98 virtual void Observe(int type, |
104 const content::NotificationSource& source, | 99 const content::NotificationSource& source, |
105 const content::NotificationDetails& details) OVERRIDE; | 100 const content::NotificationDetails& details) OVERRIDE; |
106 | 101 |
107 // SigninGlobalError::AuthStatusProvider implementation. | |
108 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; | |
109 | |
110 private: | 102 private: |
111 // Class that fetches an OAuth2 access token for a given set of scopes and | 103 // Class that fetches an OAuth2 access token for a given set of scopes and |
112 // OAuth2 refresh token. | 104 // OAuth2 refresh token. |
113 class Fetcher; | 105 class Fetcher; |
114 friend class Fetcher; | 106 friend class Fetcher; |
115 // Implementation of Request. | 107 // Implementation of Request. |
116 class RequestImpl; | 108 class RequestImpl; |
117 | 109 |
118 // Informs the consumer of |request| fetch results. | 110 // Informs the consumer of |request| fetch results. |
119 static void InformConsumer( | 111 static void InformConsumer( |
(...skipping 16 matching lines...) Expand all Loading... |
136 // Registers a new access token in the cache if |refresh_token| is the one | 128 // Registers a new access token in the cache if |refresh_token| is the one |
137 // currently held by TokenService. | 129 // currently held by TokenService. |
138 void RegisterCacheEntry(const std::string& refresh_token, | 130 void RegisterCacheEntry(const std::string& refresh_token, |
139 const ScopeSet& scopes, | 131 const ScopeSet& scopes, |
140 const std::string& access_token, | 132 const std::string& access_token, |
141 const base::Time& expiration_date); | 133 const base::Time& expiration_date); |
142 | 134 |
143 // Called when |fetcher| finishes fetching. | 135 // Called when |fetcher| finishes fetching. |
144 void OnFetchComplete(Fetcher* fetcher); | 136 void OnFetchComplete(Fetcher* fetcher); |
145 | 137 |
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. | 138 // The profile with which this instance was initialized, or NULL. |
151 Profile* profile_; | 139 Profile* profile_; |
152 | 140 |
153 // The auth status from the most-recently-completed request. | |
154 GoogleServiceAuthError last_auth_error_; | |
155 | |
156 // Getter to use for fetchers. | 141 // Getter to use for fetchers. |
157 scoped_refptr<net::URLRequestContextGetter> getter_; | 142 scoped_refptr<net::URLRequestContextGetter> getter_; |
158 | 143 |
159 // The cache of currently valid tokens. | 144 // The cache of currently valid tokens. |
160 typedef std::map<ScopeSet, CacheEntry> TokenCache; | 145 typedef std::map<ScopeSet, CacheEntry> TokenCache; |
161 TokenCache token_cache_; | 146 TokenCache token_cache_; |
162 | 147 |
163 // The parameters (refresh token and scope set) used to fetch an OAuth2 access | 148 // The parameters (refresh token and scope set) used to fetch an OAuth2 access |
164 // token. | 149 // token. |
165 typedef std::pair<std::string, ScopeSet> FetchParameters; | 150 typedef std::pair<std::string, ScopeSet> FetchParameters; |
166 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access | 151 // A map from fetch parameters to a fetcher that is fetching an OAuth2 access |
167 // token using these parameters. | 152 // token using these parameters. |
168 std::map<FetchParameters, Fetcher*> pending_fetchers_; | 153 std::map<FetchParameters, Fetcher*> pending_fetchers_; |
169 | 154 |
170 // Registrar for notifications from the TokenService. | 155 // Registrar for notifications from the TokenService. |
171 content::NotificationRegistrar registrar_; | 156 content::NotificationRegistrar registrar_; |
172 | 157 |
173 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); | 158 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
174 }; | 159 }; |
175 | 160 |
176 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ | 161 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |