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 // content::NotificationObserver listening for TokenService updates. |
| 45 virtual void Observe(int type, |
| 46 const content::NotificationSource& source, |
| 47 const content::NotificationDetails& details) OVERRIDE; |
| 48 |
| 49 // Initializes this token service with the profile. |
| 50 virtual void Initialize(Profile* profile); |
| 51 |
| 52 // ProfileKeyedService implementation. |
| 53 virtual void Shutdown() OVERRIDE; |
| 54 |
| 55 // SigninGlobalError::AuthStatusProvider implementation. |
| 56 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; |
| 57 |
| 58 // Takes injected TokenService for testing. |
| 59 bool ShouldCacheForRefreshToken(TokenService *token_service, |
| 60 const std::string& refresh_token); |
| 61 |
| 62 protected: |
| 63 friend class ProfileOAuth2TokenServiceFactory; |
| 64 explicit ProfileOAuth2TokenService(net::URLRequestContextGetter* getter); |
| 65 virtual ~ProfileOAuth2TokenService(); |
| 66 |
| 67 virtual std::string GetRefreshToken() OVERRIDE; |
| 68 |
| 69 // Updates the internal cache of the result from the most-recently-completed |
| 70 // auth request (used for reporting errors to the user). |
| 71 virtual void UpdateAuthError(const GoogleServiceAuthError& error) OVERRIDE; |
| 72 |
| 73 // Overridden to not cache tokens if the TokenService refresh token |
| 74 // changes while a token fetch is in-flight. If the user logs out and |
| 75 // logs back in with a different account, then any in-flight token |
| 76 // fetches will be for the old account's refresh token. Therefore |
| 77 // when they come back, they shouldn't be cached. |
| 78 virtual void RegisterCacheEntry(const std::string& refresh_token, |
| 79 const ScopeSet& scopes, |
| 80 const std::string& access_token, |
| 81 const base::Time& expiration_date) OVERRIDE; |
| 82 |
| 83 private: |
| 84 FRIEND_TEST_ALL_PREFIXES(ProfileOAuth2TokenServiceTest, |
| 85 StaleRefreshTokensNotCached); |
| 86 FRIEND_TEST_ALL_PREFIXES(ProfileOAuth2TokenServiceTest, |
| 87 TokenServiceUpdateClearsCache); |
| 88 |
| 89 // The profile with which this instance was initialized, or NULL. |
| 90 Profile* profile_; |
| 91 |
| 92 // The auth status from the most-recently-completed request. |
| 93 GoogleServiceAuthError last_auth_error_; |
| 94 |
| 95 // Registrar for notifications from the TokenService. |
| 96 content::NotificationRegistrar registrar_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(ProfileOAuth2TokenService); |
| 99 }; |
| 100 |
| 101 #endif // CHROME_BROWSER_SIGNIN_PROFILE_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |