Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: components/autofill/core/browser/personal_data_manager.cc

Issue 1989173005: [Autofill] Dedupe similar profiles on insertion. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 app_locale);
904
899 // We set the modification date so that immediate requests for profiles 905 // We set the modification date so that immediate requests for profiles
900 // will properly reflect the fact that this profile has been modified 906 // will properly reflect the fact that this profile has been modified
901 // recently. After writing to the database and refreshing the local copies 907 // recently. After writing to the database and refreshing the local copies
902 // the profile will have a very slightly newer time reflecting what's 908 // the profile will have a very slightly newer time reflecting what's
903 // actually stored in the database. 909 // actually stored in the database.
904 existing_profile->set_modification_date(base::Time::Now()); 910 existing_profile->set_modification_date(base::Time::Now());
905 } 911 }
906 merged_profiles->push_back(*existing_profile); 912 merged_profiles->push_back(*existing_profile);
907 } 913 }
908 914
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
1505 if (IsFeatureSubstringMatchEnabled()) { 1511 if (IsFeatureSubstringMatchEnabled()) {
1506 std::stable_sort(suggestions.begin(), suggestions.end(), 1512 std::stable_sort(suggestions.begin(), suggestions.end(),
1507 [](const Suggestion& a, const Suggestion& b) { 1513 [](const Suggestion& a, const Suggestion& b) {
1508 return a.match < b.match; 1514 return a.match < b.match;
1509 }); 1515 });
1510 } 1516 }
1511 1517
1512 return suggestions; 1518 return suggestions;
1513 } 1519 }
1514 1520
1521 void PersonalDataManager::FindMergeAndDeleteDuplicateProfiles(
1522 const std::vector<AutofillProfile*>& existing_profiles,
1523 AutofillProfile* profile_to_merge,
1524 const std::string& app_locale) {
1525 std::vector<std::string> profile_guids_to_delete;
1526
1527 FindAndMergeDuplicateProfiles(existing_profiles, profile_to_merge,
1528 &profile_guids_to_delete, app_locale);
1529
1530 // Delete the duplicate profiles.
1531 for (std::string profile_guid_to_delete : profile_guids_to_delete) {
1532 RemoveByGUID(profile_guid_to_delete);
1533 }
1534 }
1535
1536 void PersonalDataManager::FindAndMergeDuplicateProfiles(
1537 const std::vector<AutofillProfile*>& existing_profiles,
1538 AutofillProfile* profile_to_merge,
1539 std::vector<std::string>* profile_guids_to_delete,
1540 const std::string& app_locale) {
1541 for (AutofillProfile* existing_profile : existing_profiles) {
1542 // Don't try to merge a profile with itself or with any profile with a
1543 // different PrimaryValue.
1544 if (existing_profile->guid() != profile_to_merge->guid() &&
1545 AutofillProfile::CanonicalizeProfileString(
1546 existing_profile->PrimaryValue(app_locale_)) ==
Mathieu 2016/06/09 20:39:28 if you use app_locale_, remove app_locale from arg
sebsg 2016/06/12 21:41:25 Done.
1547 AutofillProfile::CanonicalizeProfileString(
1548 profile_to_merge->PrimaryValue(app_locale_))) {
1549 if (existing_profile->SaveAdditionalInfo(*profile_to_merge, app_locale)) {
Mathieu 2016/06/09 20:39:28 same here, let's be consistent...
sebsg 2016/06/12 21:41:25 Done.
1550 // Since |profile_to_merge| was a duplicate of |existing_profile| and
1551 // was merged sucessfully, it can now be deleted.
1552 profile_guids_to_delete->push_back(profile_to_merge->guid());
1553
1554 // Now try to merge the new resulting profile with the rest of the
1555 // existing profiles.
1556 profile_to_merge = existing_profile;
1557 }
1558 }
1559 }
1560 }
1561
1515 } // namespace autofill 1562 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698