OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_SIGNIN_PROFILE_OAUTH2_TOKEN_SERVICE_H_ | |
6 #define CHROME_BROWSER_SIGNIN_PROFILE_OAUTH2_TOKEN_SERVICE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/gtest_prod_util.h" | |
11 #include "chrome/browser/profiles/profile_keyed_service.h" | |
12 #include "chrome/browser/signin/oauth2_token_service.h" | |
13 #include "chrome/browser/signin/signin_global_error.h" | |
14 #include "content/public/browser/notification_observer.h" | |
15 #include "content/public/browser/notification_registrar.h" | |
16 | |
17 namespace net { | |
18 class URLRequestContextGetter; | |
19 } | |
20 | |
21 class GoogleServiceAuthError; | |
22 class Profile; | |
23 class TokenService; | |
24 | |
25 // ProfileOAuth2TokenService is a ProfileKeyedService that retrieves | |
26 // OAuth2 access tokens for a given set of scopes using the OAuth2 login | |
27 // refresh token maintained by TokenService. | |
28 // | |
29 // See |OAuth2TokenService| for usage details. | |
30 // | |
31 // Note: after StartRequest returns, in-flight requests will continue | |
32 // even if the TokenService refresh token that was used to initiate | |
33 // the request changes or is cleared. When the request completes, | |
34 // Consumer::OnGetTokenSuccess will be invoked, but the access token | |
35 // won't be cached. | |
36 // | |
37 // Note: requests should be started from the UI thread. To start a | |
38 // request from other thread, please use ProfileOAuth2TokenServiceRequest. | |
39 class ProfileOAuth2TokenService : public OAuth2TokenService, | |
40 public content::NotificationObserver, | |
41 public SigninGlobalError::AuthStatusProvider, | |
42 public ProfileKeyedService { | |
43 public: | |
44 explicit ProfileOAuth2TokenService(net::URLRequestContextGetter* getter); | |
45 virtual ~ProfileOAuth2TokenService(); | |
46 | |
47 // content::NotificationObserver listening for TokenService updates. | |
48 virtual void Observe(int type, | |
49 const content::NotificationSource& source, | |
50 const content::NotificationDetails& details) OVERRIDE; | |
51 | |
52 // Initializes this token service with the profile. | |
53 virtual void Initialize(Profile* profile); | |
54 | |
55 // ProfileKeyedService implementation. | |
56 virtual void Shutdown() OVERRIDE; | |
57 | |
58 // SigninGlobalError::AuthStatusProvider implementation. | |
59 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; | |
60 | |
61 protected: | |
62 virtual std::string GetRefreshToken() OVERRIDE; | |
63 | |
64 // Updates the internal cache of the result from the most-recently-completed | |
65 // auth request (used for reporting errors to the user). | |
66 virtual void UpdateAuthError(const GoogleServiceAuthError& error) OVERRIDE; | |
67 | |
68 // Overridden to not cache tokens if the TokenService refresh token | |
69 // changes while a token fetch is in-flight. If the user logs out and | |
70 // logs back in with a different account, then any in-flight token | |
71 // fetches will be for the old account's refresh token. Therefore | |
72 // when they come back, they shouldn't be cached. | |
73 virtual void RegisterCacheEntry(const std::string& refresh_token, | |
74 const ScopeSet& scopes, | |
75 const std::string& access_token, | |
76 const base::Time& expiration_date) OVERRIDE; | |
77 | |
78 private: | |
79 FRIEND_TEST_ALL_PREFIXES(ProfileOAuth2TokenServiceTest, | |
80 StaleRefreshTokensNotCached); | |
81 FRIEND_TEST_ALL_PREFIXES(ProfileOAuth2TokenServiceTest, | |
82 TokenServiceUpdateClearsCache); | |
83 | |
84 // The profile with which this instance was initialized, or NULL. | |
85 Profile* profile_; | |
86 | |
87 // The auth status from the most-recently-completed request. | |
88 GoogleServiceAuthError last_auth_error_; | |
89 | |
90 // Registrar for notifications from the TokenService. | |
91 content::NotificationRegistrar registrar_; | |
92 | |
93 // Takes injected TokenService for testing. | |
94 bool ShouldCacheForRefreshToken(TokenService *token_service, | |
Andrew T Wilson (Slow)
2013/03/19 19:58:44
nit: declare member functions before data members.
David Roche
2013/03/21 00:12:02
Done.
| |
95 const std::string& refresh_token); | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ProfileOAuth2TokenService); | |
98 }; | |
99 | |
100 #endif // CHROME_BROWSER_SIGNIN_PROFILE_OAUTH2_TOKEN_SERVICE_H_ | |
OLD | NEW |