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 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 class EmailEqualToFunc : public std::equal_to<gaia::ListedAccount> { | 27 class AccountEqualToFunc : public std::equal_to<gaia::ListedAccount> { |
28 public: | 28 public: |
29 bool operator()(const gaia::ListedAccount& p1, | 29 bool operator()(const gaia::ListedAccount& p1, |
30 const gaia::ListedAccount& p2) const; | 30 const gaia::ListedAccount& p2) const; |
31 }; | 31 }; |
32 | 32 |
33 bool EmailEqualToFunc::operator()( | 33 bool AccountEqualToFunc::operator()( |
34 const gaia::ListedAccount& p1, | 34 const gaia::ListedAccount& p1, |
35 const gaia::ListedAccount& p2) const { | 35 const gaia::ListedAccount& p2) const { |
36 return p1.valid == p2.valid && gaia::AreEmailsSame(p1.email, p2.email); | 36 return p1.valid == p2.valid && p1.id == p2.id; |
37 } | 37 } |
38 | 38 |
39 class AreEmailsSameFunc : public std::equal_to<std::string> { | 39 gaia::ListedAccount AccountForId(const std::string& account_id) { |
40 public: | |
41 bool operator()(const std::string& p1, | |
42 const std::string& p2) const; | |
43 }; | |
44 | |
45 bool AreEmailsSameFunc::operator()( | |
46 const std::string& p1, | |
47 const std::string& p2) const { | |
48 return gaia::AreEmailsSame(p1, p2); | |
49 } | |
50 | |
51 gaia::ListedAccount AccountFromEmail(const std::string& email) { | |
52 gaia::ListedAccount account; | 40 gaia::ListedAccount account; |
53 account.email = email; | 41 account.id = account_id; |
54 account.gaia_id = std::string(); | 42 account.gaia_id = std::string(); |
| 43 account.email = std::string(); |
55 account.valid = true; | 44 account.valid = true; |
56 return account; | 45 return account; |
57 } | 46 } |
58 | 47 |
59 } // namespace | 48 } // namespace |
60 | 49 |
61 | 50 |
62 AccountReconcilor::AccountReconcilor( | 51 AccountReconcilor::AccountReconcilor( |
63 ProfileOAuth2TokenService* token_service, | 52 ProfileOAuth2TokenService* token_service, |
64 SigninManagerBase* signin_manager, | 53 SigninManagerBase* signin_manager, |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 } else { | 321 } else { |
333 Shutdown(); | 322 Shutdown(); |
334 } | 323 } |
335 } | 324 } |
336 | 325 |
337 void AccountReconcilor::FinishReconcile() { | 326 void AccountReconcilor::FinishReconcile() { |
338 VLOG(1) << "AccountReconcilor::FinishReconcile"; | 327 VLOG(1) << "AccountReconcilor::FinishReconcile"; |
339 DCHECK(add_to_cookie_.empty()); | 328 DCHECK(add_to_cookie_.empty()); |
340 int number_gaia_accounts = gaia_accounts_.size(); | 329 int number_gaia_accounts = gaia_accounts_.size(); |
341 bool are_primaries_equal = number_gaia_accounts > 0 && | 330 bool are_primaries_equal = number_gaia_accounts > 0 && |
342 gaia::AreEmailsSame(primary_account_, gaia_accounts_[0].email); | 331 primary_account_ == gaia_accounts_[0].id; |
343 | 332 |
344 // If there are any accounts in the gaia cookie but not in chrome, then | 333 // If there are any accounts in the gaia cookie but not in chrome, then |
345 // those accounts need to be removed from the cookie. This means we need | 334 // those accounts need to be removed from the cookie. This means we need |
346 // to blow the cookie away. | 335 // to blow the cookie away. |
347 int removed_from_cookie = 0; | 336 int removed_from_cookie = 0; |
348 for (size_t i = 0; i < gaia_accounts_.size(); ++i) { | 337 for (size_t i = 0; i < gaia_accounts_.size(); ++i) { |
349 const std::string& gaia_account = gaia_accounts_[i].email; | |
350 if (gaia_accounts_[i].valid && | 338 if (gaia_accounts_[i].valid && |
351 chrome_accounts_.end() == | 339 chrome_accounts_.end() == std::find(chrome_accounts_.begin(), |
352 std::find_if(chrome_accounts_.begin(), | 340 chrome_accounts_.end(), |
353 chrome_accounts_.end(), | 341 gaia_accounts_[i].id)) { |
354 std::bind1st(AreEmailsSameFunc(), gaia_account))) { | |
355 ++removed_from_cookie; | 342 ++removed_from_cookie; |
356 } | 343 } |
357 } | 344 } |
358 | 345 |
359 bool rebuild_cookie = !are_primaries_equal || removed_from_cookie > 0; | 346 bool rebuild_cookie = !are_primaries_equal || removed_from_cookie > 0; |
360 std::vector<gaia::ListedAccount> original_gaia_accounts = | 347 std::vector<gaia::ListedAccount> original_gaia_accounts = |
361 gaia_accounts_; | 348 gaia_accounts_; |
362 if (rebuild_cookie) { | 349 if (rebuild_cookie) { |
363 VLOG(1) << "AccountReconcilor::FinishReconcile: rebuild cookie"; | 350 VLOG(1) << "AccountReconcilor::FinishReconcile: rebuild cookie"; |
364 // Really messed up state. Blow away the gaia cookie completely and | 351 // Really messed up state. Blow away the gaia cookie completely and |
(...skipping 15 matching lines...) Expand all Loading... |
380 // For each account known to chrome, PerformMergeAction() if the account is | 367 // For each account known to chrome, PerformMergeAction() if the account is |
381 // not already in the cookie jar or its state is invalid, or signal merge | 368 // not already in the cookie jar or its state is invalid, or signal merge |
382 // completed otherwise. Make a copy of |add_to_cookie_| since calls to | 369 // completed otherwise. Make a copy of |add_to_cookie_| since calls to |
383 // SignalComplete() will change the array. | 370 // SignalComplete() will change the array. |
384 std::vector<std::string> add_to_cookie_copy = add_to_cookie_; | 371 std::vector<std::string> add_to_cookie_copy = add_to_cookie_; |
385 int added_to_cookie = 0; | 372 int added_to_cookie = 0; |
386 for (size_t i = 0; i < add_to_cookie_copy.size(); ++i) { | 373 for (size_t i = 0; i < add_to_cookie_copy.size(); ++i) { |
387 if (gaia_accounts_.end() != | 374 if (gaia_accounts_.end() != |
388 std::find_if(gaia_accounts_.begin(), | 375 std::find_if(gaia_accounts_.begin(), |
389 gaia_accounts_.end(), | 376 gaia_accounts_.end(), |
390 std::bind1st(EmailEqualToFunc(), | 377 std::bind1st(AccountEqualToFunc(), |
391 AccountFromEmail(add_to_cookie_copy[i] | 378 AccountForId(add_to_cookie_copy[i])))) { |
392 )))) { | |
393 cookie_manager_service_->SignalComplete( | 379 cookie_manager_service_->SignalComplete( |
394 add_to_cookie_copy[i], | 380 add_to_cookie_copy[i], |
395 GoogleServiceAuthError::AuthErrorNone()); | 381 GoogleServiceAuthError::AuthErrorNone()); |
396 } else { | 382 } else { |
397 PerformMergeAction(add_to_cookie_copy[i]); | 383 PerformMergeAction(add_to_cookie_copy[i]); |
398 if (original_gaia_accounts.end() == | 384 if (original_gaia_accounts.end() == |
399 std::find_if(original_gaia_accounts.begin(), | 385 std::find_if(original_gaia_accounts.begin(), |
400 original_gaia_accounts.end(), | 386 original_gaia_accounts.end(), |
401 std::bind1st(EmailEqualToFunc(), | 387 std::bind1st(AccountEqualToFunc(), |
402 AccountFromEmail(add_to_cookie_copy[i] | 388 AccountForId(add_to_cookie_copy[i])))) { |
403 )))) { | |
404 added_to_cookie++; | 389 added_to_cookie++; |
405 } | 390 } |
406 } | 391 } |
407 } | 392 } |
408 | 393 |
409 signin_metrics::LogSigninAccountReconciliation(chrome_accounts_.size(), | 394 signin_metrics::LogSigninAccountReconciliation(chrome_accounts_.size(), |
410 added_to_cookie, | 395 added_to_cookie, |
411 removed_from_cookie, | 396 removed_from_cookie, |
412 are_primaries_equal, | 397 are_primaries_equal, |
413 first_execution_, | 398 first_execution_, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 << "Account added: " << account_id << ", " | 449 << "Account added: " << account_id << ", " |
465 << "Error was " << error.ToString(); | 450 << "Error was " << error.ToString(); |
466 // Always listens to GaiaCookieManagerService. Only proceed if reconciling. | 451 // Always listens to GaiaCookieManagerService. Only proceed if reconciling. |
467 if (is_reconcile_started_ && MarkAccountAsAddedToCookie(account_id)) { | 452 if (is_reconcile_started_ && MarkAccountAsAddedToCookie(account_id)) { |
468 if (error.state() != GoogleServiceAuthError::State::NONE) | 453 if (error.state() != GoogleServiceAuthError::State::NONE) |
469 error_during_last_reconcile_ = true; | 454 error_during_last_reconcile_ = true; |
470 CalculateIfReconcileIsDone(); | 455 CalculateIfReconcileIsDone(); |
471 ScheduleStartReconcileIfChromeAccountsChanged(); | 456 ScheduleStartReconcileIfChromeAccountsChanged(); |
472 } | 457 } |
473 } | 458 } |
OLD | NEW |