| 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/signin/mutable_profile_oauth2_token_service_delegate.h" | 5 #include "chrome/browser/signin/mutable_profile_oauth2_token_service_delegate.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/profiler/scoped_tracker.h" | 10 #include "base/profiler/scoped_tracker.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool IsLegacyServiceId(const std::string& account_id) { | 36 bool IsLegacyServiceId(const std::string& account_id) { |
| 37 return account_id.compare(0u, kAccountIdPrefixLength, kAccountIdPrefix) != 0; | 37 return account_id.compare(0u, kAccountIdPrefixLength, kAccountIdPrefix) != 0; |
| 38 } | 38 } |
| 39 | 39 |
| 40 std::string RemoveAccountIdPrefix(const std::string& prefixed_account_id) { | 40 std::string RemoveAccountIdPrefix(const std::string& prefixed_account_id) { |
| 41 return prefixed_account_id.substr(kAccountIdPrefixLength); | 41 return prefixed_account_id.substr(kAccountIdPrefixLength); |
| 42 } | 42 } |
| 43 | 43 |
| 44 OAuth2TokenServiceDelegate::LoadCredentialsState |
| 45 LoadCredentialsStateFromTokenResult(TokenServiceTable::Result token_result) { |
| 46 switch (token_result) { |
| 47 case TokenServiceTable::TOKEN_DB_RESULT_SQL_INVALID_STATEMENT: |
| 48 case TokenServiceTable::TOKEN_DB_RESULT_BAD_ENTRY: |
| 49 return OAuth2TokenServiceDelegate:: |
| 50 LOAD_CREDENTIALS_FINISHED_WITH_DB_ERRORS; |
| 51 case TokenServiceTable::TOKEN_DB_RESULT_DECRYPT_ERROR: |
| 52 return OAuth2TokenServiceDelegate:: |
| 53 LOAD_CREDENTIALS_FINISHED_WITH_DECRYPT_ERRORS; |
| 54 case TokenServiceTable::TOKEN_DB_RESULT_SUCCESS: |
| 55 return OAuth2TokenServiceDelegate::LOAD_CREDENTIALS_FINISHED_WITH_SUCCESS; |
| 56 } |
| 57 NOTREACHED(); |
| 58 return OAuth2TokenServiceDelegate::LOAD_CREDENTIALS_UNKNOWN; |
| 59 } |
| 60 |
| 44 } // namespace | 61 } // namespace |
| 45 | 62 |
| 46 // This class sends a request to GAIA to revoke the given refresh token from | 63 // This class sends a request to GAIA to revoke the given refresh token from |
| 47 // the server. This is a best effort attempt only. This class deletes itself | 64 // the server. This is a best effort attempt only. This class deletes itself |
| 48 // when done successfully or otherwise. | 65 // when done successfully or otherwise. |
| 49 class MutableProfileOAuth2TokenServiceDelegate::RevokeServerRefreshToken | 66 class MutableProfileOAuth2TokenServiceDelegate::RevokeServerRefreshToken |
| 50 : public GaiaAuthConsumer { | 67 : public GaiaAuthConsumer { |
| 51 public: | 68 public: |
| 52 RevokeServerRefreshToken( | 69 RevokeServerRefreshToken( |
| 53 MutableProfileOAuth2TokenServiceDelegate* token_service_delegate, | 70 MutableProfileOAuth2TokenServiceDelegate* token_service_delegate, |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 MutableProfileOAuth2TokenServiceDelegate::AccountStatus::GetAuthStatus() const { | 139 MutableProfileOAuth2TokenServiceDelegate::AccountStatus::GetAuthStatus() const { |
| 123 return last_auth_error_; | 140 return last_auth_error_; |
| 124 } | 141 } |
| 125 | 142 |
| 126 MutableProfileOAuth2TokenServiceDelegate:: | 143 MutableProfileOAuth2TokenServiceDelegate:: |
| 127 MutableProfileOAuth2TokenServiceDelegate( | 144 MutableProfileOAuth2TokenServiceDelegate( |
| 128 SigninClient* client, | 145 SigninClient* client, |
| 129 SigninErrorController* signin_error_controller, | 146 SigninErrorController* signin_error_controller, |
| 130 AccountTrackerService* account_tracker_service) | 147 AccountTrackerService* account_tracker_service) |
| 131 : web_data_service_request_(0), | 148 : web_data_service_request_(0), |
| 149 load_credentials_state_(LOAD_CREDENTIALS_NOT_STARTED), |
| 132 backoff_entry_(&backoff_policy_), | 150 backoff_entry_(&backoff_policy_), |
| 133 backoff_error_(GoogleServiceAuthError::NONE), | 151 backoff_error_(GoogleServiceAuthError::NONE), |
| 134 client_(client), | 152 client_(client), |
| 135 signin_error_controller_(signin_error_controller), | 153 signin_error_controller_(signin_error_controller), |
| 136 account_tracker_service_(account_tracker_service) { | 154 account_tracker_service_(account_tracker_service) { |
| 137 VLOG(1) << "MutablePO2TS::MutablePO2TS"; | 155 VLOG(1) << "MutablePO2TS::MutablePO2TS"; |
| 138 DCHECK(client); | 156 DCHECK(client); |
| 139 DCHECK(signin_error_controller); | 157 DCHECK(signin_error_controller); |
| 140 // It's okay to fill the backoff policy after being used in construction. | 158 // It's okay to fill the backoff policy after being used in construction. |
| 141 backoff_policy_.num_errors_to_ignore = 0; | 159 backoff_policy_.num_errors_to_ignore = 0; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 account_ids.push_back(token.first); | 255 account_ids.push_back(token.first); |
| 238 } | 256 } |
| 239 return account_ids; | 257 return account_ids; |
| 240 } | 258 } |
| 241 | 259 |
| 242 net::URLRequestContextGetter* | 260 net::URLRequestContextGetter* |
| 243 MutableProfileOAuth2TokenServiceDelegate::GetRequestContext() const { | 261 MutableProfileOAuth2TokenServiceDelegate::GetRequestContext() const { |
| 244 return client_->GetURLRequestContext(); | 262 return client_->GetURLRequestContext(); |
| 245 } | 263 } |
| 246 | 264 |
| 265 OAuth2TokenServiceDelegate::LoadCredentialsState |
| 266 MutableProfileOAuth2TokenServiceDelegate::GetLoadCredentialsState() const { |
| 267 return load_credentials_state_; |
| 268 } |
| 269 |
| 247 void MutableProfileOAuth2TokenServiceDelegate::LoadCredentials( | 270 void MutableProfileOAuth2TokenServiceDelegate::LoadCredentials( |
| 248 const std::string& primary_account_id) { | 271 const std::string& primary_account_id) { |
| 272 if (load_credentials_state_ == LOAD_CREDENTIALS_IN_PROGRESS) { |
| 273 VLOG(1) << "Load credentials operation already in progress"; |
| 274 return; |
| 275 } |
| 276 |
| 277 load_credentials_state_ = LOAD_CREDENTIALS_IN_PROGRESS; |
| 249 if (primary_account_id.empty()) { | 278 if (primary_account_id.empty()) { |
| 279 load_credentials_state_ = LOAD_CREDENTIALS_FINISHED_WITH_SUCCESS; |
| 250 FireRefreshTokensLoaded(); | 280 FireRefreshTokensLoaded(); |
| 251 return; | 281 return; |
| 252 } | 282 } |
| 283 |
| 253 ValidateAccountId(primary_account_id); | 284 ValidateAccountId(primary_account_id); |
| 254 DCHECK(loading_primary_account_id_.empty()); | 285 DCHECK(loading_primary_account_id_.empty()); |
| 255 DCHECK_EQ(0, web_data_service_request_); | 286 DCHECK_EQ(0, web_data_service_request_); |
| 256 | 287 |
| 257 refresh_tokens_.clear(); | 288 refresh_tokens_.clear(); |
| 258 | 289 |
| 290 scoped_refptr<TokenWebData> token_web_data = client_->GetDatabase(); |
| 291 if (!token_web_data) { |
| 292 // This case only exists in unit tests that do not care about loading |
| 293 // credentials. |
| 294 load_credentials_state_ = LOAD_CREDENTIALS_FINISHED_WITH_UNKNOWN_ERRORS; |
| 295 FireRefreshTokensLoaded(); |
| 296 return; |
| 297 } |
| 298 |
| 259 // If the account_id is an email address, then canonicalize it. This | 299 // If the account_id is an email address, then canonicalize it. This |
| 260 // is to support legacy account_ids, and will not be needed after | 300 // is to support legacy account_ids, and will not be needed after |
| 261 // switching to gaia-ids. | 301 // switching to gaia-ids. |
| 262 if (primary_account_id.find('@') != std::string::npos) { | 302 if (primary_account_id.find('@') != std::string::npos) { |
| 263 loading_primary_account_id_ = gaia::CanonicalizeEmail(primary_account_id); | 303 loading_primary_account_id_ = gaia::CanonicalizeEmail(primary_account_id); |
| 264 } else { | 304 } else { |
| 265 loading_primary_account_id_ = primary_account_id; | 305 loading_primary_account_id_ = primary_account_id; |
| 266 } | 306 } |
| 267 | 307 |
| 268 scoped_refptr<TokenWebData> token_web_data = client_->GetDatabase(); | 308 web_data_service_request_ = token_web_data->GetAllTokens(this); |
| 269 if (token_web_data.get()) | |
| 270 web_data_service_request_ = token_web_data->GetAllTokens(this); | |
| 271 } | 309 } |
| 272 | 310 |
| 273 void MutableProfileOAuth2TokenServiceDelegate::OnWebDataServiceRequestDone( | 311 void MutableProfileOAuth2TokenServiceDelegate::OnWebDataServiceRequestDone( |
| 274 WebDataServiceBase::Handle handle, | 312 WebDataServiceBase::Handle handle, |
| 275 std::unique_ptr<WDTypedResult> result) { | 313 std::unique_ptr<WDTypedResult> result) { |
| 276 VLOG(1) << "MutablePO2TS::OnWebDataServiceRequestDone. Result type: " | 314 VLOG(1) << "MutablePO2TS::OnWebDataServiceRequestDone. Result type: " |
| 277 << (result.get() == nullptr ? -1 : (int)result->GetType()); | 315 << (result.get() == nullptr ? -1 : (int)result->GetType()); |
| 278 | 316 |
| 279 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is | 317 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is |
| 280 // fixed. | 318 // fixed. |
| 281 tracked_objects::ScopedTracker tracking_profile( | 319 tracked_objects::ScopedTracker tracking_profile( |
| 282 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 320 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 283 "422460 MutableProfileOAuth2Token...::OnWebDataServiceRequestDone")); | 321 "422460 MutableProfileOAuth2Token...::OnWebDataServiceRequestDone")); |
| 284 | 322 |
| 285 DCHECK_EQ(web_data_service_request_, handle); | 323 DCHECK_EQ(web_data_service_request_, handle); |
| 286 web_data_service_request_ = 0; | 324 web_data_service_request_ = 0; |
| 287 | 325 |
| 288 if (result) { | 326 if (result) { |
| 289 DCHECK(result->GetType() == TOKEN_RESULT); | 327 DCHECK(result->GetType() == TOKEN_RESULT); |
| 290 const WDResult<std::map<std::string, std::string>>* token_result = | 328 const WDResult<TokenResult>* token_result = |
| 291 static_cast<const WDResult<std::map<std::string, std::string>>*>( | 329 static_cast<const WDResult<TokenResult>*>(result.get()); |
| 292 result.get()); | 330 LoadAllCredentialsIntoMemory(token_result->GetValue().tokens); |
| 293 LoadAllCredentialsIntoMemory(token_result->GetValue()); | 331 load_credentials_state_ = |
| 332 LoadCredentialsStateFromTokenResult(token_result->GetValue().db_result); |
| 333 } else { |
| 334 load_credentials_state_ = LOAD_CREDENTIALS_FINISHED_WITH_UNKNOWN_ERRORS; |
| 294 } | 335 } |
| 336 FireRefreshTokensLoaded(); |
| 295 | 337 |
| 296 // Make sure that we have an entry for |loading_primary_account_id_| in the | 338 // Make sure that we have an entry for |loading_primary_account_id_| in the |
| 297 // map. The entry could be missing if there is a corruption in the token DB | 339 // map. The entry could be missing if there is a corruption in the token DB |
| 298 // while this profile is connected to an account. | 340 // while this profile is connected to an account. |
| 299 DCHECK(!loading_primary_account_id_.empty()); | 341 DCHECK(!loading_primary_account_id_.empty()); |
| 300 if (refresh_tokens_.count(loading_primary_account_id_) == 0) { | 342 if (refresh_tokens_.count(loading_primary_account_id_) == 0) { |
| 301 refresh_tokens_[loading_primary_account_id_].reset(new AccountStatus( | 343 refresh_tokens_[loading_primary_account_id_].reset(new AccountStatus( |
| 302 signin_error_controller_, loading_primary_account_id_, std::string())); | 344 signin_error_controller_, loading_primary_account_id_, std::string())); |
| 303 } | 345 } |
| 304 | 346 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 } | 435 } |
| 394 } | 436 } |
| 395 } | 437 } |
| 396 | 438 |
| 397 if (!old_login_token.empty()) { | 439 if (!old_login_token.empty()) { |
| 398 DCHECK(!loading_primary_account_id_.empty()); | 440 DCHECK(!loading_primary_account_id_.empty()); |
| 399 if (refresh_tokens_.count(loading_primary_account_id_) == 0) | 441 if (refresh_tokens_.count(loading_primary_account_id_) == 0) |
| 400 UpdateCredentials(loading_primary_account_id_, old_login_token); | 442 UpdateCredentials(loading_primary_account_id_, old_login_token); |
| 401 } | 443 } |
| 402 } | 444 } |
| 403 | |
| 404 FireRefreshTokensLoaded(); | |
| 405 } | 445 } |
| 406 | 446 |
| 407 void MutableProfileOAuth2TokenServiceDelegate::UpdateCredentials( | 447 void MutableProfileOAuth2TokenServiceDelegate::UpdateCredentials( |
| 408 const std::string& account_id, | 448 const std::string& account_id, |
| 409 const std::string& refresh_token) { | 449 const std::string& refresh_token) { |
| 410 DCHECK(thread_checker_.CalledOnValidThread()); | 450 DCHECK(thread_checker_.CalledOnValidThread()); |
| 411 DCHECK(!account_id.empty()); | 451 DCHECK(!account_id.empty()); |
| 412 DCHECK(!refresh_token.empty()); | 452 DCHECK(!refresh_token.empty()); |
| 413 ValidateAccountId(account_id); | 453 ValidateAccountId(account_id); |
| 414 | 454 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 net::NetworkChangeNotifier::ConnectionType type) { | 565 net::NetworkChangeNotifier::ConnectionType type) { |
| 526 // If our network has changed, reset the backoff timer so that errors caused | 566 // If our network has changed, reset the backoff timer so that errors caused |
| 527 // by a previous lack of network connectivity don't prevent new requests. | 567 // by a previous lack of network connectivity don't prevent new requests. |
| 528 backoff_entry_.Reset(); | 568 backoff_entry_.Reset(); |
| 529 } | 569 } |
| 530 | 570 |
| 531 const net::BackoffEntry* | 571 const net::BackoffEntry* |
| 532 MutableProfileOAuth2TokenServiceDelegate::BackoffEntry() const { | 572 MutableProfileOAuth2TokenServiceDelegate::BackoffEntry() const { |
| 533 return &backoff_entry_; | 573 return &backoff_entry_; |
| 534 } | 574 } |
| OLD | NEW |