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

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

Issue 962673004: [Autofill/Autocomplete Feature] Substring matching instead of prefix matching. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added comments in autocomplete_history_manager.cc. Created 5 years, 5 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 <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <iterator> 9 #include <iterator>
10 10
(...skipping 12 matching lines...) Expand all
23 #include "components/autofill/core/browser/autofill_experiments.h" 23 #include "components/autofill/core/browser/autofill_experiments.h"
24 #include "components/autofill/core/browser/autofill_field.h" 24 #include "components/autofill/core/browser/autofill_field.h"
25 #include "components/autofill/core/browser/autofill_metrics.h" 25 #include "components/autofill/core/browser/autofill_metrics.h"
26 #include "components/autofill/core/browser/form_structure.h" 26 #include "components/autofill/core/browser/form_structure.h"
27 #include "components/autofill/core/browser/personal_data_manager_observer.h" 27 #include "components/autofill/core/browser/personal_data_manager_observer.h"
28 #include "components/autofill/core/browser/phone_number.h" 28 #include "components/autofill/core/browser/phone_number.h"
29 #include "components/autofill/core/browser/phone_number_i18n.h" 29 #include "components/autofill/core/browser/phone_number_i18n.h"
30 #include "components/autofill/core/browser/validation.h" 30 #include "components/autofill/core/browser/validation.h"
31 #include "components/autofill/core/common/autofill_pref_names.h" 31 #include "components/autofill/core/common/autofill_pref_names.h"
32 #include "components/autofill/core/common/autofill_switches.h" 32 #include "components/autofill/core/common/autofill_switches.h"
33 #include "components/autofill/core/common/autofill_util.h"
33 #include "components/signin/core/browser/account_tracker_service.h" 34 #include "components/signin/core/browser/account_tracker_service.h"
34 #include "components/signin/core/common/signin_pref_names.h" 35 #include "components/signin/core/common/signin_pref_names.h"
35 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" 36 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
36 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo rmatter.h" 37 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo rmatter.h"
37 38
38 namespace autofill { 39 namespace autofill {
39 namespace { 40 namespace {
40 41
41 using ::i18n::addressinput::AddressField; 42 using ::i18n::addressinput::AddressField;
42 using ::i18n::addressinput::GetStreetAddressLinesAsSingleLine; 43 using ::i18n::addressinput::GetStreetAddressLinesAsSingleLine;
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 789
789 std::vector<Suggestion> suggestions; 790 std::vector<Suggestion> suggestions;
790 // Match based on a prefix search. 791 // Match based on a prefix search.
791 std::vector<AutofillProfile*> matched_profiles; 792 std::vector<AutofillProfile*> matched_profiles;
792 for (AutofillProfile* profile : profiles) { 793 for (AutofillProfile* profile : profiles) {
793 base::string16 value = GetInfoInOneLine(profile, type, app_locale_); 794 base::string16 value = GetInfoInOneLine(profile, type, app_locale_);
794 if (value.empty()) 795 if (value.empty())
795 continue; 796 continue;
796 base::string16 value_canon = 797 base::string16 value_canon =
797 AutofillProfile::CanonicalizeProfileString(value); 798 AutofillProfile::CanonicalizeProfileString(value);
798 if (base::StartsWith(value_canon, field_contents_canon, true)) { 799 // CanonicalizeProfileString() returns lower-case string with all
please use gerrit instead 2015/06/29 22:06:29 CanonicalizeProfileString() does not replace "all
Pritam Nikam 2015/06/30 15:05:50 Done. I reffered it from here [1]. [1]. https:/
799 // Prefix match, add suggestion. 800 // separator substituted by whitespaces and trims off trailing whitespace
801 // if any. Passing |value_canon| and |field_contents_canon| to
802 // ContainsTokenThatStartsWith() may lead to unexpected result, hence
803 // preferred passing |values[i]| and |field_contents| instead.
please use gerrit instead 2015/06/29 22:06:29 This part of the comment is outdated. There's no v
Pritam Nikam 2015/06/30 15:05:50 Done.
804 bool substring_matched_suggestion = false;
805 if (base::StartsWith(value_canon, field_contents_canon, true) ||
806 (substring_matched_suggestion =
807 ContainsTokenThatStartsWith(value, field_contents, false))) {
please use gerrit instead 2015/06/29 22:06:29 It's rare in Chromium code to assign a variable in
Pritam Nikam 2015/06/30 15:05:50 Done. Changes to: bool prefix_matched_sugg
please use gerrit instead 2015/06/30 19:06:22 Nice.
800 matched_profiles.push_back(profile); 808 matched_profiles.push_back(profile);
801 suggestions.push_back(Suggestion(value)); 809 suggestions.push_back(Suggestion(value));
802 suggestions.back().backend_id = profile->guid(); 810 suggestions.back().backend_id = profile->guid();
811 suggestions.back().match = substring_matched_suggestion
812 ? Suggestion::SUBSTRING_MATCH
813 : Suggestion::PREFIX_MATCH;
803 } 814 }
804 } 815 }
805 816
817 // Prefix matches should precede other token matches.
818 if (IsFeatureSubstringMatchEnabled()) {
819 std::stable_sort(suggestions.begin(), suggestions.end(),
820 [](const Suggestion& a, const Suggestion& b) {
821 return a.match < b.match;
822 });
please use gerrit instead 2015/06/29 22:06:29 Is this clang formatted?
Pritam Nikam 2015/06/30 15:05:50 Yes.
823 }
824
806 // Don't show two suggestions if one is a subset of the other. 825 // Don't show two suggestions if one is a subset of the other.
807 std::vector<AutofillProfile*> unique_matched_profiles; 826 std::vector<AutofillProfile*> unique_matched_profiles;
808 std::vector<Suggestion> unique_suggestions; 827 std::vector<Suggestion> unique_suggestions;
809 ServerFieldTypeSet types(other_field_types.begin(), other_field_types.end()); 828 ServerFieldTypeSet types(other_field_types.begin(), other_field_types.end());
810 for (size_t i = 0; i < matched_profiles.size(); ++i) { 829 for (size_t i = 0; i < matched_profiles.size(); ++i) {
811 bool include = true; 830 bool include = true;
812 AutofillProfile* profile_a = matched_profiles[i]; 831 AutofillProfile* profile_a = matched_profiles[i];
813 for (size_t j = 0; j < matched_profiles.size(); ++j) { 832 for (size_t j = 0; j < matched_profiles.size(); ++j) {
814 AutofillProfile* profile_b = matched_profiles[j]; 833 AutofillProfile* profile_b = matched_profiles[j];
815 // Check if profile A is a subset of profile B. If not, continue. 834 // Check if profile A is a subset of profile B. If not, continue.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 return unique_suggestions; 866 return unique_suggestions;
848 } 867 }
849 868
850 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions( 869 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions(
851 const AutofillType& type, 870 const AutofillType& type,
852 const base::string16& field_contents) { 871 const base::string16& field_contents) {
853 if (IsInAutofillSuggestionsDisabledExperiment()) 872 if (IsInAutofillSuggestionsDisabledExperiment())
854 return std::vector<Suggestion>(); 873 return std::vector<Suggestion>();
855 874
856 std::list<const CreditCard*> cards_to_suggest; 875 std::list<const CreditCard*> cards_to_suggest;
876 std::list<const CreditCard*> substring_matched_suggestions;
please use gerrit instead 2015/06/29 22:06:29 s/substring_matched_suggestions/substring_matched_
Pritam Nikam 2015/06/30 15:05:50 Done.
857 for (const CreditCard* credit_card : GetCreditCards()) { 877 for (const CreditCard* credit_card : GetCreditCards()) {
858 // The value of the stored data for this field type in the |credit_card|. 878 // The value of the stored data for this field type in the |credit_card|.
859 base::string16 creditcard_field_value = 879 base::string16 creditcard_field_value =
860 credit_card->GetInfo(type, app_locale_); 880 credit_card->GetInfo(type, app_locale_);
861 if (creditcard_field_value.empty()) 881 if (creditcard_field_value.empty())
862 continue; 882 continue;
863 883
864 // For card number fields, suggest the card if: 884 // For card number fields, suggest the card if:
865 // - the number matches any part of the card, or 885 // - the number matches any part of the card, or
866 // - it's a masked card and there are 6 or fewers typed so far. 886 // - it's a masked card and there are 6 or fewers typed so far.
867 // For other fields, require that the field contents match the beginning of 887 // For other fields, require that the field contents match the beginning of
868 // the stored data. 888 // the stored data.
869 if (type.GetStorableType() == CREDIT_CARD_NUMBER) { 889 if (type.GetStorableType() == CREDIT_CARD_NUMBER) {
870 if (creditcard_field_value.find(field_contents) == base::string16::npos && 890 if (creditcard_field_value.find(field_contents) == base::string16::npos &&
871 (credit_card->record_type() != CreditCard::MASKED_SERVER_CARD || 891 (credit_card->record_type() != CreditCard::MASKED_SERVER_CARD ||
872 field_contents.size() >= 6)) { 892 field_contents.size() >= 6)) {
873 continue; 893 continue;
874 } 894 }
875 } else if (!base::StartsWith(creditcard_field_value, field_contents, 895 cards_to_suggest.push_back(credit_card);
876 false)) { 896 } else if (base::StartsWith(creditcard_field_value, field_contents,
877 continue; 897 false)) {
898 cards_to_suggest.push_back(credit_card);
899 } else if (ContainsTokenThatStartsWith(creditcard_field_value,
900 field_contents, false)) {
901 substring_matched_suggestions.push_back(credit_card);
878 } 902 }
903 }
879 904
880 cards_to_suggest.push_back(credit_card); 905 cards_to_suggest.sort(RankByMfu);
906
907 // Prefix matches should precede other token matches.
908 if (IsFeatureSubstringMatchEnabled()) {
909 substring_matched_suggestions.sort(RankByMfu);
910 cards_to_suggest.insert(cards_to_suggest.end(),
911 substring_matched_suggestions.begin(),
912 substring_matched_suggestions.end());
881 } 913 }
882 914
883 // De-dupe card suggestions. Full server cards shadow local cards, and 915 // De-dupe card suggestions. Full server cards shadow local cards, and
884 // local cards shadow masked server cards. 916 // local cards shadow masked server cards.
885 for (auto outer_it = cards_to_suggest.begin(); 917 for (auto outer_it = cards_to_suggest.begin();
886 outer_it != cards_to_suggest.end(); 918 outer_it != cards_to_suggest.end();
887 ++outer_it) { 919 ++outer_it) {
888 920
889 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) { 921 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) {
890 for (auto inner_it = cards_to_suggest.begin(); 922 for (auto inner_it = cards_to_suggest.begin();
891 inner_it != cards_to_suggest.end();) { 923 inner_it != cards_to_suggest.end();) {
892 auto inner_it_copy = inner_it++; 924 auto inner_it_copy = inner_it++;
893 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it)) 925 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it))
894 cards_to_suggest.erase(inner_it_copy); 926 cards_to_suggest.erase(inner_it_copy);
895 } 927 }
896 } else if ((*outer_it)->record_type() == CreditCard::LOCAL_CARD) { 928 } else if ((*outer_it)->record_type() == CreditCard::LOCAL_CARD) {
897 for (auto inner_it = cards_to_suggest.begin(); 929 for (auto inner_it = cards_to_suggest.begin();
898 inner_it != cards_to_suggest.end();) { 930 inner_it != cards_to_suggest.end();) {
899 auto inner_it_copy = inner_it++; 931 auto inner_it_copy = inner_it++;
900 if ((*inner_it_copy)->record_type() == CreditCard::MASKED_SERVER_CARD && 932 if ((*inner_it_copy)->record_type() == CreditCard::MASKED_SERVER_CARD &&
901 (*outer_it)->IsLocalDuplicateOfServerCard(**inner_it_copy)) { 933 (*outer_it)->IsLocalDuplicateOfServerCard(**inner_it_copy)) {
902 cards_to_suggest.erase(inner_it_copy); 934 cards_to_suggest.erase(inner_it_copy);
903 } 935 }
904 } 936 }
905 } 937 }
906 } 938 }
907 939
908 cards_to_suggest.sort(RankByMfu);
909
910 std::vector<Suggestion> suggestions; 940 std::vector<Suggestion> suggestions;
911 for (const CreditCard* credit_card : cards_to_suggest) { 941 for (const CreditCard* credit_card : cards_to_suggest) {
912 // Make a new suggestion. 942 // Make a new suggestion.
913 suggestions.push_back(Suggestion()); 943 suggestions.push_back(Suggestion());
914 Suggestion* suggestion = &suggestions.back(); 944 Suggestion* suggestion = &suggestions.back();
915 945
916 suggestion->value = credit_card->GetInfo(type, app_locale_); 946 suggestion->value = credit_card->GetInfo(type, app_locale_);
917 suggestion->icon = base::UTF8ToUTF16(credit_card->type()); 947 suggestion->icon = base::UTF8ToUTF16(credit_card->type());
918 suggestion->backend_id = credit_card->guid(); 948 suggestion->backend_id = credit_card->guid();
919 949
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
1333 } 1363 }
1334 if (IsExperimentalWalletIntegrationEnabled() && 1364 if (IsExperimentalWalletIntegrationEnabled() &&
1335 pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { 1365 pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) {
1336 profiles_.insert( 1366 profiles_.insert(
1337 profiles_.end(), server_profiles_.begin(), server_profiles_.end()); 1367 profiles_.end(), server_profiles_.begin(), server_profiles_.end());
1338 } 1368 }
1339 return profiles_; 1369 return profiles_;
1340 } 1370 }
1341 1371
1342 } // namespace autofill 1372 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698