Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/autofill/core/browser/personal_data_manager.h" | 5 #include "components/autofill/core/browser/personal_data_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 #include "components/autofill/core/browser/phone_number.h" | 32 #include "components/autofill/core/browser/phone_number.h" |
| 33 #include "components/autofill/core/browser/phone_number_i18n.h" | 33 #include "components/autofill/core/browser/phone_number_i18n.h" |
| 34 #include "components/autofill/core/browser/validation.h" | 34 #include "components/autofill/core/browser/validation.h" |
| 35 #include "components/autofill/core/common/autofill_pref_names.h" | 35 #include "components/autofill/core/common/autofill_pref_names.h" |
| 36 #include "components/autofill/core/common/autofill_switches.h" | 36 #include "components/autofill/core/common/autofill_switches.h" |
| 37 #include "components/autofill/core/common/autofill_util.h" | 37 #include "components/autofill/core/common/autofill_util.h" |
| 38 #include "components/prefs/pref_service.h" | 38 #include "components/prefs/pref_service.h" |
| 39 #include "components/signin/core/browser/account_tracker_service.h" | 39 #include "components/signin/core/browser/account_tracker_service.h" |
| 40 #include "components/signin/core/browser/signin_manager.h" | 40 #include "components/signin/core/browser/signin_manager.h" |
| 41 #include "components/signin/core/common/signin_pref_names.h" | 41 #include "components/signin/core/common/signin_pref_names.h" |
| 42 #include "components/sync_driver/sync_service.h" | |
| 42 #include "components/variations/variations_associated_data.h" | 43 #include "components/variations/variations_associated_data.h" |
| 43 #include "components/version_info/version_info.h" | 44 #include "components/version_info/version_info.h" |
| 44 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" | 45 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" |
| 45 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo rmatter.h" | 46 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo rmatter.h" |
| 46 | 47 |
| 47 namespace autofill { | 48 namespace autofill { |
| 48 namespace { | 49 namespace { |
| 49 | 50 |
| 50 using ::i18n::addressinput::AddressField; | 51 using ::i18n::addressinput::AddressField; |
| 51 using ::i18n::addressinput::GetStreetAddressLinesAsSingleLine; | 52 using ::i18n::addressinput::GetStreetAddressLinesAsSingleLine; |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 AutofillMetrics::LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); | 281 AutofillMetrics::LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); |
| 281 | 282 |
| 282 // WebDataService may not be available in tests. | 283 // WebDataService may not be available in tests. |
| 283 if (!database_.get()) | 284 if (!database_.get()) |
| 284 return; | 285 return; |
| 285 | 286 |
| 286 LoadProfiles(); | 287 LoadProfiles(); |
| 287 LoadCreditCards(); | 288 LoadCreditCards(); |
| 288 | 289 |
| 289 database_->AddObserver(this); | 290 database_->AddObserver(this); |
| 291 is_autofill_profile_dedupe_pending_ = IsAutofillProfileCleanupEnabled(); | |
| 290 } | 292 } |
| 291 | 293 |
| 292 PersonalDataManager::~PersonalDataManager() { | 294 PersonalDataManager::~PersonalDataManager() { |
| 293 CancelPendingQuery(&pending_profiles_query_); | 295 CancelPendingQuery(&pending_profiles_query_); |
| 294 CancelPendingQuery(&pending_server_profiles_query_); | 296 CancelPendingQuery(&pending_server_profiles_query_); |
| 295 CancelPendingQuery(&pending_creditcards_query_); | 297 CancelPendingQuery(&pending_creditcards_query_); |
| 296 CancelPendingQuery(&pending_server_creditcards_query_); | 298 CancelPendingQuery(&pending_server_creditcards_query_); |
| 297 | 299 |
| 298 if (database_.get()) | 300 if (database_.get()) |
| 299 database_->RemoveObserver(this); | 301 database_->RemoveObserver(this); |
| 300 } | 302 } |
| 301 | 303 |
| 304 void PersonalDataManager::OnSyncServiceConfigured( | |
| 305 sync_driver::SyncService* sync_service) { | |
| 306 // We want to know if we when, if at all, we need to run autofill profile | |
|
sebsg
2016/07/12 18:59:53
nit: We want to know if we when, ... -> We want to
Roger McFarlane (Chromium)
2016/07/12 19:52:48
Done.
| |
| 307 // de-duplication: now or after waiting until sync has started. | |
| 308 if (!is_autofill_profile_dedupe_pending_) { | |
| 309 // De-duplication isn't enabled. | |
| 310 return; | |
| 311 } | |
| 312 | |
| 313 // If the sync service is configured to start and to sync autofill profiles, | |
| 314 // then we can just let the notification that sync has started trigger the | |
| 315 // de-duplication. | |
| 316 if (sync_service && sync_service->CanSyncStart() && | |
| 317 sync_service->GetPreferredDataTypes().Has(syncer::AUTOFILL_PROFILE)) { | |
| 318 return; | |
| 319 } | |
| 320 | |
| 321 // Otherwise, Refresh() will attempt the de-duplication now. | |
| 322 Refresh(); | |
| 323 } | |
| 324 | |
| 302 void PersonalDataManager::OnWebDataServiceRequestDone( | 325 void PersonalDataManager::OnWebDataServiceRequestDone( |
| 303 WebDataServiceBase::Handle h, | 326 WebDataServiceBase::Handle h, |
| 304 const WDTypedResult* result) { | 327 const WDTypedResult* result) { |
| 305 DCHECK(pending_profiles_query_ || pending_server_profiles_query_ || | 328 DCHECK(pending_profiles_query_ || pending_server_profiles_query_ || |
| 306 pending_creditcards_query_ || pending_server_creditcards_query_); | 329 pending_creditcards_query_ || pending_server_creditcards_query_); |
| 307 | 330 |
| 308 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is | 331 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is |
| 309 // fixed. | 332 // fixed. |
| 310 tracked_objects::ScopedTracker tracking_profile( | 333 tracked_objects::ScopedTracker tracking_profile( |
| 311 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 334 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 312 "422460 PersonalDataManager::OnWebDataServiceRequestDone")); | 335 "422460 PersonalDataManager::OnWebDataServiceRequestDone")); |
| 313 | 336 |
| 314 if (!result) { | 337 if (!result) { |
| 315 // Error from the web database. | 338 // Error from the web database. |
| 316 if (h == pending_creditcards_query_) | 339 if (h == pending_creditcards_query_) |
| 317 pending_creditcards_query_ = 0; | 340 pending_creditcards_query_ = 0; |
| 318 else if (h == pending_profiles_query_) | 341 else if (h == pending_profiles_query_) |
| 319 pending_profiles_query_ = 0; | 342 pending_profiles_query_ = 0; |
| 320 return; | 343 return; |
| 321 } | 344 } |
| 322 | 345 |
| 323 switch (result->GetType()) { | 346 switch (result->GetType()) { |
| 324 case AUTOFILL_PROFILES_RESULT: | 347 case AUTOFILL_PROFILES_RESULT: |
| 325 if (h == pending_profiles_query_) { | 348 if (h == pending_profiles_query_) { |
| 326 ReceiveLoadedDbValues(h, result, &pending_profiles_query_, | 349 ReceiveLoadedDbValues(h, result, &pending_profiles_query_, |
| 327 &web_profiles_); | 350 &web_profiles_); |
| 328 LogProfileCount(); // This only logs local profiles. | 351 LogProfileCount(); // This only logs local profiles. |
| 329 // Since these two routines both re-launch the database queries, don't | 352 ApplyProfileUseDatesFix(); |
| 330 // run them on the same query response. | |
| 331 if (!ApplyDedupingRoutine()) | |
| 332 ApplyProfileUseDatesFix(); | |
| 333 } else { | 353 } else { |
| 334 ReceiveLoadedDbValues(h, result, &pending_server_profiles_query_, | 354 ReceiveLoadedDbValues(h, result, &pending_server_profiles_query_, |
| 335 &server_profiles_); | 355 &server_profiles_); |
| 336 | 356 |
| 337 if (!server_profiles_.empty()) { | 357 if (!server_profiles_.empty()) { |
| 338 std::string account_id = signin_manager_->GetAuthenticatedAccountId(); | 358 std::string account_id = signin_manager_->GetAuthenticatedAccountId(); |
| 339 base::string16 email = | 359 base::string16 email = |
| 340 base::UTF8ToUTF16( | 360 base::UTF8ToUTF16( |
| 341 account_tracker_->GetAccountInfo(account_id).email); | 361 account_tracker_->GetAccountInfo(account_id).email); |
| 342 | 362 |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 717 credit_cards_.insert(credit_cards_.end(), server_credit_cards_.begin(), | 737 credit_cards_.insert(credit_cards_.end(), server_credit_cards_.begin(), |
| 718 server_credit_cards_.end()); | 738 server_credit_cards_.end()); |
| 719 } | 739 } |
| 720 return credit_cards_; | 740 return credit_cards_; |
| 721 } | 741 } |
| 722 | 742 |
| 723 bool PersonalDataManager::HasServerData() const { | 743 bool PersonalDataManager::HasServerData() const { |
| 724 return !server_credit_cards_.empty() || !server_profiles_.empty(); | 744 return !server_credit_cards_.empty() || !server_profiles_.empty(); |
| 725 } | 745 } |
| 726 | 746 |
| 727 void PersonalDataManager::Refresh() { | 747 void PersonalDataManager::Refresh() { |
|
Mathieu
2016/07/12 18:10:12
Refresh is called all over the place in this file.
Roger McFarlane (Chromium)
2016/07/12 18:42:19
Done.
| |
| 748 if (is_autofill_profile_dedupe_pending_) | |
| 749 ApplyDedupingRoutine(); | |
| 750 | |
| 728 LoadProfiles(); | 751 LoadProfiles(); |
| 729 LoadCreditCards(); | 752 LoadCreditCards(); |
| 730 } | 753 } |
| 731 | 754 |
| 732 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest() | 755 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest() |
| 733 const { | 756 const { |
| 734 std::vector<AutofillProfile*> profiles = GetProfiles(true); | 757 std::vector<AutofillProfile*> profiles = GetProfiles(true); |
| 735 | 758 |
| 736 // Rank the suggestions by frecency (see AutofillDataModel for details). | 759 // Rank the suggestions by frecency (see AutofillDataModel for details). |
| 737 base::Time comparison_time = base::Time::Now(); | 760 base::Time comparison_time = base::Time::Now(); |
| (...skipping 861 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1599 } | 1622 } |
| 1600 | 1623 |
| 1601 // Set the pref so that this fix is never run again. | 1624 // Set the pref so that this fix is never run again. |
| 1602 pref_service_->SetBoolean(prefs::kAutofillProfileUseDatesFixed, true); | 1625 pref_service_->SetBoolean(prefs::kAutofillProfileUseDatesFixed, true); |
| 1603 | 1626 |
| 1604 if (has_changed_data) | 1627 if (has_changed_data) |
| 1605 SetProfiles(&profiles); | 1628 SetProfiles(&profiles); |
| 1606 } | 1629 } |
| 1607 | 1630 |
| 1608 bool PersonalDataManager::ApplyDedupingRoutine() { | 1631 bool PersonalDataManager::ApplyDedupingRoutine() { |
| 1609 if (!IsAutofillProfileCleanupEnabled()) | 1632 if (!is_autofill_profile_dedupe_pending_) { |
| 1610 return false; | 1633 return false; |
| 1634 } | |
| 1611 | 1635 |
| 1612 int current_major_version = atoi(version_info::GetVersionNumber().c_str()); | 1636 DCHECK(IsAutofillProfileCleanupEnabled()); |
| 1637 is_autofill_profile_dedupe_pending_ = false; | |
| 1613 | 1638 |
| 1614 // Check if the deduping routine has already been run on this major version. | 1639 // Check if the deduping routine has already been run on this major version. |
| 1640 int current_major_version = atoi(version_info::GetVersionNumber().c_str()); | |
| 1615 if (pref_service_->GetInteger(prefs::kAutofillLastVersionDeduped) >= | 1641 if (pref_service_->GetInteger(prefs::kAutofillLastVersionDeduped) >= |
| 1616 current_major_version) | 1642 current_major_version) { |
| 1643 VLOG(1) | |
| 1644 << "Autofill profile de-duplication already performed for this version"; | |
| 1617 return false; | 1645 return false; |
| 1646 } | |
| 1618 | 1647 |
| 1648 VLOG(1) << "Starting autofill profile de-duplication."; | |
| 1619 std::vector<AutofillProfile*> existing_profiles = web_profiles_.get(); | 1649 std::vector<AutofillProfile*> existing_profiles = web_profiles_.get(); |
| 1620 std::unordered_set<AutofillProfile*> profiles_to_delete; | 1650 std::unordered_set<AutofillProfile*> profiles_to_delete; |
| 1621 profiles_to_delete.reserve(existing_profiles.size()); | 1651 profiles_to_delete.reserve(existing_profiles.size()); |
| 1622 | 1652 |
| 1623 DedupeProfiles(&existing_profiles, &profiles_to_delete); | 1653 DedupeProfiles(&existing_profiles, &profiles_to_delete); |
| 1624 | 1654 |
| 1625 // Apply the changes to the database. | 1655 // Apply the changes to the database. |
| 1626 for (AutofillProfile* profile : existing_profiles) { | 1656 for (AutofillProfile* profile : existing_profiles) { |
| 1627 // If the profile was set to be deleted, remove it from the database. | 1657 // If the profile was set to be deleted, remove it from the database. |
| 1628 if (profiles_to_delete.count(profile)) { | 1658 if (profiles_to_delete.count(profile)) { |
| 1629 database_->RemoveAutofillProfile(profile->guid()); | 1659 database_->RemoveAutofillProfile(profile->guid()); |
| 1630 } else { | 1660 } else { |
| 1631 // Otherwise, update the profile in the database. | 1661 // Otherwise, update the profile in the database. |
| 1632 database_->UpdateAutofillProfile(*profile); | 1662 database_->UpdateAutofillProfile(*profile); |
| 1633 } | 1663 } |
| 1634 } | 1664 } |
| 1635 | 1665 |
| 1636 // Set the pref to the current major version. | 1666 // Set the pref to the current major version. |
| 1637 pref_service_->SetInteger(prefs::kAutofillLastVersionDeduped, | 1667 pref_service_->SetInteger(prefs::kAutofillLastVersionDeduped, |
| 1638 current_major_version); | 1668 current_major_version); |
| 1639 | 1669 |
| 1640 // Refresh the local cache and send notifications to observers. | |
| 1641 Refresh(); | |
| 1642 | |
| 1643 return true; | 1670 return true; |
| 1644 } | 1671 } |
| 1645 | 1672 |
| 1646 void PersonalDataManager::DedupeProfiles( | 1673 void PersonalDataManager::DedupeProfiles( |
| 1647 std::vector<AutofillProfile*>* existing_profiles, | 1674 std::vector<AutofillProfile*>* existing_profiles, |
| 1648 std::unordered_set<AutofillProfile*>* profiles_to_delete) { | 1675 std::unordered_set<AutofillProfile*>* profiles_to_delete) { |
| 1649 AutofillMetrics::LogNumberOfProfilesConsideredForDedupe( | 1676 AutofillMetrics::LogNumberOfProfilesConsideredForDedupe( |
| 1650 existing_profiles->size()); | 1677 existing_profiles->size()); |
| 1651 | 1678 |
| 1652 // Sort the profiles by frecency with all the verified profiles at the end. | 1679 // Sort the profiles by frecency with all the verified profiles at the end. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1712 if (profile_to_merge->IsVerified()) | 1739 if (profile_to_merge->IsVerified()) |
| 1713 break; | 1740 break; |
| 1714 } | 1741 } |
| 1715 } | 1742 } |
| 1716 } | 1743 } |
| 1717 AutofillMetrics::LogNumberOfProfilesRemovedDuringDedupe( | 1744 AutofillMetrics::LogNumberOfProfilesRemovedDuringDedupe( |
| 1718 profiles_to_delete->size()); | 1745 profiles_to_delete->size()); |
| 1719 } | 1746 } |
| 1720 | 1747 |
| 1721 } // namespace autofill | 1748 } // namespace autofill |
| OLD | NEW |