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 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/stl_util.h" |
| 10 #include "base/time.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/signin/signin_manager.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "chrome/browser/signin/token_service.h" |
| 15 #include "chrome/browser/signin/token_service_factory.h" |
| 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_details.h" |
| 19 #include "content/public/browser/notification_source.h" |
| 20 #include "google_apis/gaia/gaia_constants.h" |
| 21 #include "google_apis/gaia/google_service_auth_error.h" |
| 22 #include "net/url_request/url_request_context_getter.h" |
| 23 |
| 24 |
| 25 ProfileOAuth2TokenService::ProfileOAuth2TokenService( |
| 26 net::URLRequestContextGetter* getter) |
| 27 : OAuth2TokenService(getter), |
| 28 profile_(NULL), |
| 29 last_auth_error_(GoogleServiceAuthError::NONE) { |
| 30 } |
| 31 |
| 32 ProfileOAuth2TokenService::~ProfileOAuth2TokenService() { |
| 33 } |
| 34 |
| 35 void ProfileOAuth2TokenService::Initialize(Profile* profile) { |
| 36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 37 |
| 38 DCHECK(profile); |
| 39 DCHECK(!profile_); |
| 40 profile_ = profile; |
| 41 |
| 42 content::Source<TokenService> token_service_source( |
| 43 TokenServiceFactory::GetForProfile(profile)); |
| 44 registrar_.Add(this, |
| 45 chrome::NOTIFICATION_TOKENS_CLEARED, |
| 46 token_service_source); |
| 47 registrar_.Add(this, |
| 48 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 49 token_service_source); |
| 50 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()-> |
| 51 AddProvider(this); |
| 52 } |
| 53 |
| 54 void ProfileOAuth2TokenService::Shutdown() { |
| 55 if (profile_) { |
| 56 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()-> |
| 57 RemoveProvider(this); |
| 58 } |
| 59 } |
| 60 |
| 61 std::string ProfileOAuth2TokenService::GetRefreshToken() { |
| 62 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
| 63 if (!token_service || !token_service->HasOAuthLoginToken()) { |
| 64 return std::string(); |
| 65 } |
| 66 return token_service->GetOAuth2LoginRefreshToken(); |
| 67 } |
| 68 |
| 69 void ProfileOAuth2TokenService::UpdateAuthError( |
| 70 const GoogleServiceAuthError& error) { |
| 71 // Do not report connection errors as these are not actually auth errors. |
| 72 // We also want to avoid masking a "real" auth error just because we |
| 73 // subsequently get a transient network error. |
| 74 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) |
| 75 return; |
| 76 |
| 77 if (error.state() != last_auth_error_.state()) { |
| 78 last_auth_error_ = error; |
| 79 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()-> |
| 80 AuthStatusChanged(); |
| 81 } |
| 82 } |
| 83 |
| 84 void ProfileOAuth2TokenService::Observe( |
| 85 int type, |
| 86 const content::NotificationSource& source, |
| 87 const content::NotificationDetails& details) { |
| 88 DCHECK(type == chrome::NOTIFICATION_TOKENS_CLEARED || |
| 89 type == chrome::NOTIFICATION_TOKEN_AVAILABLE); |
| 90 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { |
| 91 TokenService::TokenAvailableDetails* tok_details = |
| 92 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); |
| 93 if (tok_details->service() != GaiaConstants::kGaiaOAuth2LoginRefreshToken) |
| 94 return; |
| 95 } |
| 96 // The GaiaConstants::kGaiaOAuth2LoginRefreshToken token is used to create |
| 97 // OAuth2 access tokens. If this token either changes or is cleared, any |
| 98 // available tokens must be invalidated. |
| 99 ClearCache(); |
| 100 UpdateAuthError(GoogleServiceAuthError::AuthErrorNone()); |
| 101 } |
| 102 |
| 103 GoogleServiceAuthError ProfileOAuth2TokenService::GetAuthStatus() const { |
| 104 return last_auth_error_; |
| 105 } |
| 106 |
| 107 void ProfileOAuth2TokenService::RegisterCacheEntry( |
| 108 const std::string& refresh_token, |
| 109 const ScopeSet& scopes, |
| 110 const std::string& access_token, |
| 111 const base::Time& expiration_date) { |
| 112 if (ShouldCacheForRefreshToken(TokenServiceFactory::GetForProfile(profile_), |
| 113 refresh_token)) { |
| 114 OAuth2TokenService::RegisterCacheEntry(refresh_token, |
| 115 scopes, |
| 116 access_token, |
| 117 expiration_date); |
| 118 } |
| 119 } |
| 120 |
| 121 bool ProfileOAuth2TokenService::ShouldCacheForRefreshToken( |
| 122 TokenService *token_service, |
| 123 const std::string& refresh_token) { |
| 124 if (!token_service || |
| 125 !token_service->HasOAuthLoginToken() || |
| 126 token_service->GetOAuth2LoginRefreshToken().compare(refresh_token) != 0) { |
| 127 DLOG(INFO) << |
| 128 "Received a token with a refresh token not maintained by TokenService."; |
| 129 return false; |
| 130 } |
| 131 return true; |
| 132 } |
OLD | NEW |