| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/signin/core/browser/account_reconcilor.h" | 5 #include "components/signin/core/browser/account_reconcilor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 16 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 17 #include "components/signin/core/browser/signin_client.h" | 17 #include "components/signin/core/browser/signin_client.h" |
| 18 #include "components/signin/core/browser/signin_metrics.h" | 18 #include "components/signin/core/browser/signin_metrics.h" |
| 19 #include "components/signin/core/common/profile_management_switches.h" | 19 #include "components/signin/core/common/profile_management_switches.h" |
| 20 #include "google_apis/gaia/gaia_auth_util.h" | 20 #include "google_apis/gaia/gaia_auth_util.h" |
| 21 #include "google_apis/gaia/gaia_oauth_client.h" | 21 #include "google_apis/gaia/gaia_oauth_client.h" |
| 22 #include "google_apis/gaia/gaia_urls.h" | 22 #include "google_apis/gaia/gaia_urls.h" |
| 23 | 23 |
| 24 | |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 class AccountEqualToFunc : public std::equal_to<gaia::ListedAccount> { | 26 class AccountEqualToFunc : public std::equal_to<gaia::ListedAccount> { |
| 28 public: | 27 public: |
| 29 bool operator()(const gaia::ListedAccount& p1, | 28 bool operator()(const gaia::ListedAccount& p1, |
| 30 const gaia::ListedAccount& p2) const; | 29 const gaia::ListedAccount& p2) const; |
| 31 }; | 30 }; |
| 32 | 31 |
| 33 bool AccountEqualToFunc::operator()( | 32 bool AccountEqualToFunc::operator()( |
| 34 const gaia::ListedAccount& p1, | 33 const gaia::ListedAccount& p1, |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 250 |
| 252 void AccountReconcilor::StartReconcile() { | 251 void AccountReconcilor::StartReconcile() { |
| 253 if (!IsProfileConnected() || !client_->AreSigninCookiesAllowed()) { | 252 if (!IsProfileConnected() || !client_->AreSigninCookiesAllowed()) { |
| 254 VLOG(1) << "AccountReconcilor::StartReconcile: !connected or no cookies"; | 253 VLOG(1) << "AccountReconcilor::StartReconcile: !connected or no cookies"; |
| 255 return; | 254 return; |
| 256 } | 255 } |
| 257 | 256 |
| 258 if (is_reconcile_started_) | 257 if (is_reconcile_started_) |
| 259 return; | 258 return; |
| 260 | 259 |
| 261 is_reconcile_started_ = true; | |
| 262 error_during_last_reconcile_ = false; | |
| 263 | |
| 264 // Reset state for validating gaia cookie. | 260 // Reset state for validating gaia cookie. |
| 265 gaia_accounts_.clear(); | 261 gaia_accounts_.clear(); |
| 266 | 262 |
| 267 // Reset state for validating oauth2 tokens. | 263 // Reset state for validating oauth2 tokens. |
| 268 primary_account_.clear(); | 264 primary_account_.clear(); |
| 269 chrome_accounts_.clear(); | 265 chrome_accounts_.clear(); |
| 270 add_to_cookie_.clear(); | 266 add_to_cookie_.clear(); |
| 271 ValidateAccountsFromTokenService(); | 267 ValidateAccountsFromTokenService(); |
| 272 | 268 |
| 269 if (primary_account_.empty()) { |
| 270 VLOG(1) << "AccountReconcilor::StartReconcile: primary has error"; |
| 271 return; |
| 272 } |
| 273 |
| 274 is_reconcile_started_ = true; |
| 275 error_during_last_reconcile_ = false; |
| 276 |
| 273 // Rely on the GCMS to manage calls to and responses from ListAccounts. | 277 // Rely on the GCMS to manage calls to and responses from ListAccounts. |
| 274 if (cookie_manager_service_->ListAccounts(&gaia_accounts_)) { | 278 if (cookie_manager_service_->ListAccounts(&gaia_accounts_)) { |
| 275 OnGaiaAccountsInCookieUpdated( | 279 OnGaiaAccountsInCookieUpdated( |
| 276 gaia_accounts_, GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 280 gaia_accounts_, GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
| 277 } | 281 } |
| 278 } | 282 } |
| 279 | 283 |
| 280 void AccountReconcilor::OnGaiaAccountsInCookieUpdated( | 284 void AccountReconcilor::OnGaiaAccountsInCookieUpdated( |
| 281 const std::vector<gaia::ListedAccount>& accounts, | 285 const std::vector<gaia::ListedAccount>& accounts, |
| 282 const GoogleServiceAuthError& error) { | 286 const GoogleServiceAuthError& error) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 298 AbortReconcile(); | 302 AbortReconcile(); |
| 299 } | 303 } |
| 300 } | 304 } |
| 301 | 305 |
| 302 void AccountReconcilor::ValidateAccountsFromTokenService() { | 306 void AccountReconcilor::ValidateAccountsFromTokenService() { |
| 303 primary_account_ = signin_manager_->GetAuthenticatedAccountId(); | 307 primary_account_ = signin_manager_->GetAuthenticatedAccountId(); |
| 304 DCHECK(!primary_account_.empty()); | 308 DCHECK(!primary_account_.empty()); |
| 305 | 309 |
| 306 chrome_accounts_ = token_service_->GetAccounts(); | 310 chrome_accounts_ = token_service_->GetAccounts(); |
| 307 | 311 |
| 312 // Remove any accounts that have an error. There is no point in trying to |
| 313 // reconcile them, since it won't work anyway. If the list ends up being |
| 314 // empty, or if the primary account is in error, then don't reconcile any |
| 315 // accounts. |
| 316 for (auto i = chrome_accounts_.begin(); i != chrome_accounts_.end(); ++i) { |
| 317 if (token_service_->GetDelegate()->RefreshTokenHasError(*i)) { |
| 318 if (primary_account_ == *i) { |
| 319 primary_account_.clear(); |
| 320 chrome_accounts_.clear(); |
| 321 break; |
| 322 } else { |
| 323 VLOG(1) << "AccountReconcilor::ValidateAccountsFromTokenService: " |
| 324 << *i << " has error, won't reconcile"; |
| 325 i->clear(); |
| 326 } |
| 327 } |
| 328 } |
| 329 chrome_accounts_.erase(std::remove(chrome_accounts_.begin(), |
| 330 chrome_accounts_.end(), |
| 331 std::string()), |
| 332 chrome_accounts_.end()); |
| 333 |
| 308 VLOG(1) << "AccountReconcilor::ValidateAccountsFromTokenService: " | 334 VLOG(1) << "AccountReconcilor::ValidateAccountsFromTokenService: " |
| 309 << "Chrome " << chrome_accounts_.size() << " accounts, " | 335 << "Chrome " << chrome_accounts_.size() << " accounts, " |
| 310 << "Primary is '" << primary_account_ << "'"; | 336 << "Primary is '" << primary_account_ << "'"; |
| 311 } | 337 } |
| 312 | 338 |
| 313 void AccountReconcilor::OnNewProfileManagementFlagChanged( | 339 void AccountReconcilor::OnNewProfileManagementFlagChanged( |
| 314 bool new_flag_status) { | 340 bool new_flag_status) { |
| 315 if (new_flag_status) { | 341 if (new_flag_status) { |
| 316 // The reconciler may have been newly created just before this call, or may | 342 // The reconciler may have been newly created just before this call, or may |
| 317 // have already existed and in mid-reconcile. To err on the safe side, force | 343 // have already existed and in mid-reconcile. To err on the safe side, force |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 << "Account added: " << account_id << ", " | 475 << "Account added: " << account_id << ", " |
| 450 << "Error was " << error.ToString(); | 476 << "Error was " << error.ToString(); |
| 451 // Always listens to GaiaCookieManagerService. Only proceed if reconciling. | 477 // Always listens to GaiaCookieManagerService. Only proceed if reconciling. |
| 452 if (is_reconcile_started_ && MarkAccountAsAddedToCookie(account_id)) { | 478 if (is_reconcile_started_ && MarkAccountAsAddedToCookie(account_id)) { |
| 453 if (error.state() != GoogleServiceAuthError::State::NONE) | 479 if (error.state() != GoogleServiceAuthError::State::NONE) |
| 454 error_during_last_reconcile_ = true; | 480 error_during_last_reconcile_ = true; |
| 455 CalculateIfReconcileIsDone(); | 481 CalculateIfReconcileIsDone(); |
| 456 ScheduleStartReconcileIfChromeAccountsChanged(); | 482 ScheduleStartReconcileIfChromeAccountsChanged(); |
| 457 } | 483 } |
| 458 } | 484 } |
| OLD | NEW |