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