| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "chrome/browser/chromeos/login/signin/token_handle_fetcher.h" | 5 #include "chrome/browser/chromeos/login/signin/token_handle_fetcher.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" | 8 #include "chrome/browser/chromeos/login/signin/token_handle_util.h" |
| 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 9 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); | 35 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); |
| 36 } | 36 } |
| 37 ~ShutdownNotifierFactory() override {} | 37 ~ShutdownNotifierFactory() override {} |
| 38 | 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); | 39 DISALLOW_COPY_AND_ASSIGN(ShutdownNotifierFactory); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, | 44 TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, |
| 45 const AccountId& account_id) | 45 const user_manager::UserID& user_id) |
| 46 : OAuth2TokenService::Consumer("user_session_manager"), | 46 : OAuth2TokenService::Consumer("user_session_manager"), |
| 47 token_handle_util_(util), | 47 token_handle_util_(util), |
| 48 account_id_(account_id) {} | 48 user_id_(user_id), |
| 49 token_service_(nullptr), |
| 50 waiting_for_refresh_token_(false), |
| 51 profile_(nullptr), |
| 52 tokeninfo_response_start_time_(base::TimeTicks()) { |
| 53 } |
| 49 | 54 |
| 50 TokenHandleFetcher::~TokenHandleFetcher() { | 55 TokenHandleFetcher::~TokenHandleFetcher() { |
| 51 if (waiting_for_refresh_token_) | 56 if (waiting_for_refresh_token_) |
| 52 token_service_->RemoveObserver(this); | 57 token_service_->RemoveObserver(this); |
| 53 } | 58 } |
| 54 | 59 |
| 55 void TokenHandleFetcher::BackfillToken(Profile* profile, | 60 void TokenHandleFetcher::BackfillToken(Profile* profile, |
| 56 const TokenFetchingCallback& callback) { | 61 const TokenFetchingCallback& callback) { |
| 57 profile_ = profile; | 62 profile_ = profile; |
| 58 callback_ = callback; | 63 callback_ = callback; |
| 59 | 64 |
| 60 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 65 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 61 SigninManagerBase* signin_manager = | 66 SigninManagerBase* signin_manager = |
| 62 SigninManagerFactory::GetForProfile(profile); | 67 SigninManagerFactory::GetForProfile(profile); |
| 63 const std::string user_email = signin_manager->GetAuthenticatedAccountId(); | 68 std::string account_id = signin_manager->GetAuthenticatedAccountId(); |
| 64 if (!token_service_->RefreshTokenIsAvailable(user_email)) { | 69 if (!token_service_->RefreshTokenIsAvailable(account_id)) { |
| 65 account_without_token_ = user_email; | 70 account_without_token_ = account_id; |
| 66 profile_shutdown_notification_ = | 71 profile_shutdown_notification_ = |
| 67 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( | 72 ShutdownNotifierFactory::GetInstance()->Get(profile)->Subscribe( |
| 68 base::Bind(&TokenHandleFetcher::OnProfileDestroyed, | 73 base::Bind(&TokenHandleFetcher::OnProfileDestroyed, |
| 69 base::Unretained(this))); | 74 base::Unretained(this))); |
| 70 | 75 |
| 71 token_service_->AddObserver(this); | 76 token_service_->AddObserver(this); |
| 72 waiting_for_refresh_token_ = true; | 77 waiting_for_refresh_token_ = true; |
| 73 return; | 78 return; |
| 74 } | 79 } |
| 75 RequestAccessToken(user_email); | 80 RequestAccessToken(account_id); |
| 76 } | 81 } |
| 77 | 82 |
| 78 void TokenHandleFetcher::OnRefreshTokenAvailable( | 83 void TokenHandleFetcher::OnRefreshTokenAvailable( |
| 79 const std::string& user_email) { | 84 const std::string& account_id) { |
| 80 if (account_without_token_ != user_email) | 85 if (account_without_token_ != account_id) |
| 81 return; | 86 return; |
| 82 waiting_for_refresh_token_ = false; | 87 waiting_for_refresh_token_ = false; |
| 83 token_service_->RemoveObserver(this); | 88 token_service_->RemoveObserver(this); |
| 84 RequestAccessToken(user_email); | 89 RequestAccessToken(account_id); |
| 85 } | 90 } |
| 86 | 91 |
| 87 void TokenHandleFetcher::RequestAccessToken(const std::string& user_email) { | 92 void TokenHandleFetcher::RequestAccessToken(const std::string& account_id) { |
| 88 OAuth2TokenService::ScopeSet scopes; | 93 OAuth2TokenService::ScopeSet scopes; |
| 89 scopes.insert(GaiaConstants::kOAuth1LoginScope); | 94 scopes.insert(GaiaConstants::kOAuth1LoginScope); |
| 90 oauth2_access_token_request_ = | 95 oauth2_access_token_request_ = |
| 91 token_service_->StartRequest(user_email, scopes, this); | 96 token_service_->StartRequest(account_id, scopes, this); |
| 92 } | 97 } |
| 93 | 98 |
| 94 void TokenHandleFetcher::OnGetTokenSuccess( | 99 void TokenHandleFetcher::OnGetTokenSuccess( |
| 95 const OAuth2TokenService::Request* request, | 100 const OAuth2TokenService::Request* request, |
| 96 const std::string& access_token, | 101 const std::string& access_token, |
| 97 const base::Time& expiration_time) { | 102 const base::Time& expiration_time) { |
| 98 oauth2_access_token_request_.reset(); | 103 oauth2_access_token_request_.reset(); |
| 99 FillForAccessToken(access_token); | 104 FillForAccessToken(access_token); |
| 100 } | 105 } |
| 101 | 106 |
| 102 void TokenHandleFetcher::OnGetTokenFailure( | 107 void TokenHandleFetcher::OnGetTokenFailure( |
| 103 const OAuth2TokenService::Request* request, | 108 const OAuth2TokenService::Request* request, |
| 104 const GoogleServiceAuthError& error) { | 109 const GoogleServiceAuthError& error) { |
| 105 oauth2_access_token_request_.reset(); | 110 oauth2_access_token_request_.reset(); |
| 106 LOG(ERROR) << "Could not get access token to backfill token handler" | 111 LOG(ERROR) << "Could not get access token to backfill token handler" |
| 107 << error.ToString(); | 112 << error.ToString(); |
| 108 callback_.Run(account_id_, false); | 113 callback_.Run(user_id_, false); |
| 109 } | 114 } |
| 110 | 115 |
| 111 void TokenHandleFetcher::FillForNewUser(const std::string& access_token, | 116 void TokenHandleFetcher::FillForNewUser(const std::string& access_token, |
| 112 const TokenFetchingCallback& callback) { | 117 const TokenFetchingCallback& callback) { |
| 113 profile_ = chromeos::ProfileHelper::Get()->GetSigninProfile(); | 118 profile_ = chromeos::ProfileHelper::Get()->GetSigninProfile(); |
| 114 callback_ = callback; | 119 callback_ = callback; |
| 115 FillForAccessToken(access_token); | 120 FillForAccessToken(access_token); |
| 116 } | 121 } |
| 117 | 122 |
| 118 void TokenHandleFetcher::FillForAccessToken(const std::string& access_token) { | 123 void TokenHandleFetcher::FillForAccessToken(const std::string& access_token) { |
| 119 if (!gaia_client_.get()) | 124 if (!gaia_client_.get()) |
| 120 gaia_client_.reset( | 125 gaia_client_.reset( |
| 121 new gaia::GaiaOAuthClient(profile_->GetRequestContext())); | 126 new gaia::GaiaOAuthClient(profile_->GetRequestContext())); |
| 122 tokeninfo_response_start_time_ = base::TimeTicks::Now(); | 127 tokeninfo_response_start_time_ = base::TimeTicks::Now(); |
| 123 gaia_client_->GetTokenInfo(access_token, kMaxRetries, this); | 128 gaia_client_->GetTokenInfo(access_token, kMaxRetries, this); |
| 124 } | 129 } |
| 125 | 130 |
| 126 void TokenHandleFetcher::OnOAuthError() { | 131 void TokenHandleFetcher::OnOAuthError() { |
| 127 callback_.Run(account_id_, false); | 132 callback_.Run(user_id_, false); |
| 128 } | 133 } |
| 129 | 134 |
| 130 void TokenHandleFetcher::OnNetworkError(int response_code) { | 135 void TokenHandleFetcher::OnNetworkError(int response_code) { |
| 131 callback_.Run(account_id_, false); | 136 callback_.Run(user_id_, false); |
| 132 } | 137 } |
| 133 | 138 |
| 134 void TokenHandleFetcher::OnGetTokenInfoResponse( | 139 void TokenHandleFetcher::OnGetTokenInfoResponse( |
| 135 scoped_ptr<base::DictionaryValue> token_info) { | 140 scoped_ptr<base::DictionaryValue> token_info) { |
| 136 bool success = false; | 141 bool success = false; |
| 137 if (!token_info->HasKey("error")) { | 142 if (!token_info->HasKey("error")) { |
| 138 std::string handle; | 143 std::string handle; |
| 139 if (token_info->GetString("token_handle", &handle)) { | 144 if (token_info->GetString("token_handle", &handle)) { |
| 140 success = true; | 145 success = true; |
| 141 token_handle_util_->StoreTokenHandle(account_id_, handle); | 146 token_handle_util_->StoreTokenHandle(user_id_, handle); |
| 142 } | 147 } |
| 143 } | 148 } |
| 144 const base::TimeDelta duration = | 149 const base::TimeDelta duration = |
| 145 base::TimeTicks::Now() - tokeninfo_response_start_time_; | 150 base::TimeTicks::Now() - tokeninfo_response_start_time_; |
| 146 UMA_HISTOGRAM_TIMES("Login.TokenObtainResponseTime", duration); | 151 UMA_HISTOGRAM_TIMES("Login.TokenObtainResponseTime", duration); |
| 147 callback_.Run(account_id_, success); | 152 callback_.Run(user_id_, success); |
| 148 } | 153 } |
| 149 | 154 |
| 150 void TokenHandleFetcher::OnProfileDestroyed() { | 155 void TokenHandleFetcher::OnProfileDestroyed() { |
| 151 callback_.Run(account_id_, false); | 156 callback_.Run(user_id_, false); |
| 152 } | 157 } |
| OLD | NEW |