Chromium Code Reviews| Index: components/autofill/core/browser/personal_data_manager.cc |
| diff --git a/components/autofill/core/browser/personal_data_manager.cc b/components/autofill/core/browser/personal_data_manager.cc |
| index e2b09f90ad65be4e5dab01af33d45e8352c233c5..f93a2f36c99ed13501565302dde2074786ab95ae 100644 |
| --- a/components/autofill/core/browser/personal_data_manager.cc |
| +++ b/components/autofill/core/browser/personal_data_manager.cc |
| @@ -863,7 +863,8 @@ bool PersonalDataManager::IsValidLearnableProfile( |
| return true; |
| } |
| -// static |
| +// TODO(crbug.com/618448): Refactor MergeProfile to not depend on class |
| +// variables. |
| std::string PersonalDataManager::MergeProfile( |
| const AutofillProfile& new_profile, |
| std::vector<AutofillProfile*> existing_profiles, |
| @@ -887,7 +888,8 @@ std::string PersonalDataManager::MergeProfile( |
| // If we have already saved this address, merge in any missing values. |
| // Only merge with the first match. |
| for (AutofillProfile* existing_profile : existing_profiles) { |
| - if (!matching_profile_found && !new_profile.PrimaryValue().empty() && |
| + if (!matching_profile_found && |
| + !new_profile.PrimaryValue(app_locale_).empty() && |
| existing_profile->SaveAdditionalInfo(new_profile, app_locale)) { |
| // Unverified profiles should always be updated with the newer data, |
| // whereas verified profiles should only ever be overwritten by verified |
| @@ -896,6 +898,10 @@ std::string PersonalDataManager::MergeProfile( |
| matching_profile_found = true; |
| guid = existing_profile->guid(); |
| + // Look for duplicates of |existing_profile| to merge into. |
| + FindMergeAndDeleteDuplicateProfiles(existing_profiles, existing_profile, |
| + app_locale); |
| + |
| // We set the modification date so that immediate requests for profiles |
| // will properly reflect the fact that this profile has been modified |
| // recently. After writing to the database and refreshing the local copies |
| @@ -1512,4 +1518,45 @@ std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( |
| return suggestions; |
| } |
| +void PersonalDataManager::FindMergeAndDeleteDuplicateProfiles( |
| + const std::vector<AutofillProfile*>& existing_profiles, |
| + AutofillProfile* profile_to_merge, |
| + const std::string& app_locale) { |
| + std::vector<std::string> profile_guids_to_delete; |
| + |
| + FindAndMergeDuplicateProfiles(existing_profiles, profile_to_merge, |
| + &profile_guids_to_delete, app_locale); |
| + |
| + // Delete the duplicate profiles. |
| + for (std::string profile_guid_to_delete : profile_guids_to_delete) { |
| + RemoveByGUID(profile_guid_to_delete); |
| + } |
| +} |
| + |
| +void PersonalDataManager::FindAndMergeDuplicateProfiles( |
| + const std::vector<AutofillProfile*>& existing_profiles, |
| + AutofillProfile* profile_to_merge, |
| + std::vector<std::string>* profile_guids_to_delete, |
| + const std::string& app_locale) { |
| + for (AutofillProfile* existing_profile : existing_profiles) { |
| + // Don't try to merge a profile with itself or with any profile with a |
| + // different PrimaryValue. |
| + if (existing_profile->guid() != profile_to_merge->guid() && |
| + AutofillProfile::CanonicalizeProfileString( |
| + 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.
|
| + AutofillProfile::CanonicalizeProfileString( |
| + profile_to_merge->PrimaryValue(app_locale_))) { |
| + 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.
|
| + // Since |profile_to_merge| was a duplicate of |existing_profile| and |
| + // was merged sucessfully, it can now be deleted. |
| + profile_guids_to_delete->push_back(profile_to_merge->guid()); |
| + |
| + // Now try to merge the new resulting profile with the rest of the |
| + // existing profiles. |
| + profile_to_merge = existing_profile; |
| + } |
| + } |
| + } |
| +} |
| + |
| } // namespace autofill |