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 845 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 856 if (profile.IsPresentButInvalid(ADDRESS_HOME_STATE)) | 856 if (profile.IsPresentButInvalid(ADDRESS_HOME_STATE)) |
| 857 return false; | 857 return false; |
| 858 | 858 |
| 859 // Reject profiles with invalid US zip information. | 859 // Reject profiles with invalid US zip information. |
| 860 if (profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)) | 860 if (profile.IsPresentButInvalid(ADDRESS_HOME_ZIP)) |
| 861 return false; | 861 return false; |
| 862 | 862 |
| 863 return true; | 863 return true; |
| 864 } | 864 } |
| 865 | 865 |
| 866 // static | 866 // TODO(crbug.com/618448): Refactor MergeProfile to not depend on class |
| 867 // variables. | |
| 867 std::string PersonalDataManager::MergeProfile( | 868 std::string PersonalDataManager::MergeProfile( |
| 868 const AutofillProfile& new_profile, | 869 const AutofillProfile& new_profile, |
| 869 std::vector<AutofillProfile*> existing_profiles, | 870 std::vector<AutofillProfile*> existing_profiles, |
| 870 const std::string& app_locale, | 871 const std::string& app_locale, |
| 871 std::vector<AutofillProfile>* merged_profiles) { | 872 std::vector<AutofillProfile>* merged_profiles) { |
| 872 merged_profiles->clear(); | 873 merged_profiles->clear(); |
| 873 | 874 |
| 874 // Sort the existing profiles in decreasing order of frecency, so the "best" | 875 // Sort the existing profiles in decreasing order of frecency, so the "best" |
| 875 // profiles are checked first. | 876 // profiles are checked first. |
| 876 base::Time comparison_time = base::Time::Now(); | 877 base::Time comparison_time = base::Time::Now(); |
| 877 std::sort(existing_profiles.begin(), existing_profiles.end(), | 878 std::sort(existing_profiles.begin(), existing_profiles.end(), |
| 878 [comparison_time](const AutofillDataModel* a, | 879 [comparison_time](const AutofillDataModel* a, |
| 879 const AutofillDataModel* b) { | 880 const AutofillDataModel* b) { |
| 880 return a->CompareFrecency(b, comparison_time); | 881 return a->CompareFrecency(b, comparison_time); |
| 881 }); | 882 }); |
| 882 | 883 |
| 883 // Set to true if |existing_profiles| already contains an equivalent profile. | 884 // Set to true if |existing_profiles| already contains an equivalent profile. |
| 884 bool matching_profile_found = false; | 885 bool matching_profile_found = false; |
| 885 std::string guid = new_profile.guid(); | 886 std::string guid = new_profile.guid(); |
| 886 | 887 |
| 887 // If we have already saved this address, merge in any missing values. | 888 // If we have already saved this address, merge in any missing values. |
| 888 // Only merge with the first match. | 889 // Only merge with the first match. |
| 889 for (AutofillProfile* existing_profile : existing_profiles) { | 890 for (AutofillProfile* existing_profile : existing_profiles) { |
| 890 if (!matching_profile_found && !new_profile.PrimaryValue().empty() && | 891 if (!matching_profile_found && |
| 892 !new_profile.PrimaryValue(app_locale_).empty() && | |
| 891 existing_profile->SaveAdditionalInfo(new_profile, app_locale)) { | 893 existing_profile->SaveAdditionalInfo(new_profile, app_locale)) { |
| 892 // Unverified profiles should always be updated with the newer data, | 894 // Unverified profiles should always be updated with the newer data, |
| 893 // whereas verified profiles should only ever be overwritten by verified | 895 // whereas verified profiles should only ever be overwritten by verified |
| 894 // data. If an automatically aggregated profile would overwrite a | 896 // data. If an automatically aggregated profile would overwrite a |
| 895 // verified profile, just drop it. | 897 // verified profile, just drop it. |
| 896 matching_profile_found = true; | 898 matching_profile_found = true; |
| 897 guid = existing_profile->guid(); | 899 guid = existing_profile->guid(); |
| 898 | 900 |
| 901 // Look for duplicates of |existing_profile| to merge into. | |
| 902 FindMergeAndDeleteDuplicateProfiles(existing_profiles, existing_profile); | |
| 903 | |
| 899 // We set the modification date so that immediate requests for profiles | 904 // We set the modification date so that immediate requests for profiles |
| 900 // will properly reflect the fact that this profile has been modified | 905 // will properly reflect the fact that this profile has been modified |
| 901 // recently. After writing to the database and refreshing the local copies | 906 // recently. After writing to the database and refreshing the local copies |
| 902 // the profile will have a very slightly newer time reflecting what's | 907 // the profile will have a very slightly newer time reflecting what's |
| 903 // actually stored in the database. | 908 // actually stored in the database. |
| 904 existing_profile->set_modification_date(base::Time::Now()); | 909 existing_profile->set_modification_date(base::Time::Now()); |
| 905 } | 910 } |
| 906 merged_profiles->push_back(*existing_profile); | 911 merged_profiles->push_back(*existing_profile); |
| 907 } | 912 } |
| 908 | 913 |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1505 if (IsFeatureSubstringMatchEnabled()) { | 1510 if (IsFeatureSubstringMatchEnabled()) { |
| 1506 std::stable_sort(suggestions.begin(), suggestions.end(), | 1511 std::stable_sort(suggestions.begin(), suggestions.end(), |
| 1507 [](const Suggestion& a, const Suggestion& b) { | 1512 [](const Suggestion& a, const Suggestion& b) { |
| 1508 return a.match < b.match; | 1513 return a.match < b.match; |
| 1509 }); | 1514 }); |
| 1510 } | 1515 } |
| 1511 | 1516 |
| 1512 return suggestions; | 1517 return suggestions; |
| 1513 } | 1518 } |
| 1514 | 1519 |
| 1520 void PersonalDataManager::FindMergeAndDeleteDuplicateProfiles( | |
| 1521 const std::vector<AutofillProfile*>& existing_profiles, | |
| 1522 AutofillProfile* profile_to_merge) { | |
| 1523 std::vector<std::string> profile_guids_to_delete; | |
| 1524 | |
| 1525 FindAndMergeDuplicateProfiles(existing_profiles, profile_to_merge, | |
| 1526 &profile_guids_to_delete); | |
| 1527 | |
| 1528 // Delete the duplicate profiles. | |
| 1529 for (std::string profile_guid_to_delete : profile_guids_to_delete) { | |
|
Roger McFarlane (Chromium)
2016/06/13 15:12:52
const ref
sebsg
2016/06/13 17:57:59
Done.
| |
| 1530 RemoveByGUID(profile_guid_to_delete); | |
| 1531 } | |
| 1532 } | |
| 1533 | |
| 1534 void PersonalDataManager::FindAndMergeDuplicateProfiles( | |
| 1535 const std::vector<AutofillProfile*>& existing_profiles, | |
| 1536 AutofillProfile* profile_to_merge, | |
| 1537 std::vector<std::string>* profile_guids_to_delete) { | |
| 1538 for (AutofillProfile* existing_profile : existing_profiles) { | |
| 1539 // Don't try to merge a profile with itself or with any profile with a | |
| 1540 // different PrimaryValue. | |
| 1541 if (existing_profile->guid() != profile_to_merge->guid() && | |
| 1542 AutofillProfile::CanonicalizeProfileString( | |
| 1543 existing_profile->PrimaryValue(app_locale_)) == | |
| 1544 AutofillProfile::CanonicalizeProfileString( | |
| 1545 profile_to_merge->PrimaryValue(app_locale_))) { | |
| 1546 if (existing_profile->SaveAdditionalInfo(*profile_to_merge, | |
| 1547 app_locale_)) { | |
| 1548 // Since |profile_to_merge| was a duplicate of |existing_profile| and | |
| 1549 // was merged sucessfully, it can now be deleted. | |
| 1550 profile_guids_to_delete->push_back(profile_to_merge->guid()); | |
| 1551 | |
| 1552 // Now try to merge the new resulting profile with the rest of the | |
| 1553 // existing profiles. | |
| 1554 profile_to_merge = existing_profile; | |
| 1555 } | |
| 1556 } | |
| 1557 } | |
| 1558 } | |
| 1559 | |
| 1515 } // namespace autofill | 1560 } // namespace autofill |
| OLD | NEW |