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 <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <iterator> | 9 #include <iterator> |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
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 Loading... |
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 bool prefix_matched_suggestion = |
799 // Prefix match, add suggestion. | 800 base::StartsWith(value_canon, field_contents_canon, true); |
| 801 if (prefix_matched_suggestion || |
| 802 ContainsTokenThatStartsWith(value, field_contents, false)) { |
800 matched_profiles.push_back(profile); | 803 matched_profiles.push_back(profile); |
801 suggestions.push_back(Suggestion(value)); | 804 suggestions.push_back(Suggestion(value)); |
802 suggestions.back().backend_id = profile->guid(); | 805 suggestions.back().backend_id = profile->guid(); |
| 806 suggestions.back().match = prefix_matched_suggestion |
| 807 ? Suggestion::PREFIX_MATCH |
| 808 : Suggestion::SUBSTRING_MATCH; |
803 } | 809 } |
804 } | 810 } |
805 | 811 |
| 812 // Prefix matches should precede other token matches. |
| 813 if (IsFeatureSubstringMatchEnabled()) { |
| 814 std::stable_sort(suggestions.begin(), suggestions.end(), |
| 815 [](const Suggestion& a, const Suggestion& b) { |
| 816 return a.match < b.match; |
| 817 }); |
| 818 } |
| 819 |
806 // Don't show two suggestions if one is a subset of the other. | 820 // Don't show two suggestions if one is a subset of the other. |
807 std::vector<AutofillProfile*> unique_matched_profiles; | 821 std::vector<AutofillProfile*> unique_matched_profiles; |
808 std::vector<Suggestion> unique_suggestions; | 822 std::vector<Suggestion> unique_suggestions; |
809 ServerFieldTypeSet types(other_field_types.begin(), other_field_types.end()); | 823 ServerFieldTypeSet types(other_field_types.begin(), other_field_types.end()); |
810 for (size_t i = 0; i < matched_profiles.size(); ++i) { | 824 for (size_t i = 0; i < matched_profiles.size(); ++i) { |
811 bool include = true; | 825 bool include = true; |
812 AutofillProfile* profile_a = matched_profiles[i]; | 826 AutofillProfile* profile_a = matched_profiles[i]; |
813 for (size_t j = 0; j < matched_profiles.size(); ++j) { | 827 for (size_t j = 0; j < matched_profiles.size(); ++j) { |
814 AutofillProfile* profile_b = matched_profiles[j]; | 828 AutofillProfile* profile_b = matched_profiles[j]; |
815 // Check if profile A is a subset of profile B. If not, continue. | 829 // 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 Loading... |
847 return unique_suggestions; | 861 return unique_suggestions; |
848 } | 862 } |
849 | 863 |
850 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions( | 864 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions( |
851 const AutofillType& type, | 865 const AutofillType& type, |
852 const base::string16& field_contents) { | 866 const base::string16& field_contents) { |
853 if (IsInAutofillSuggestionsDisabledExperiment()) | 867 if (IsInAutofillSuggestionsDisabledExperiment()) |
854 return std::vector<Suggestion>(); | 868 return std::vector<Suggestion>(); |
855 | 869 |
856 std::list<const CreditCard*> cards_to_suggest; | 870 std::list<const CreditCard*> cards_to_suggest; |
| 871 std::list<const CreditCard*> substring_matched_cards; |
857 for (const CreditCard* credit_card : GetCreditCards()) { | 872 for (const CreditCard* credit_card : GetCreditCards()) { |
858 // The value of the stored data for this field type in the |credit_card|. | 873 // The value of the stored data for this field type in the |credit_card|. |
859 base::string16 creditcard_field_value = | 874 base::string16 creditcard_field_value = |
860 credit_card->GetInfo(type, app_locale_); | 875 credit_card->GetInfo(type, app_locale_); |
861 if (creditcard_field_value.empty()) | 876 if (creditcard_field_value.empty()) |
862 continue; | 877 continue; |
863 | 878 |
864 // For card number fields, suggest the card if: | 879 // For card number fields, suggest the card if: |
865 // - the number matches any part of the card, or | 880 // - the number matches any part of the card, or |
866 // - it's a masked card and there are 6 or fewers typed so far. | 881 // - 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 | 882 // For other fields, require that the field contents match the beginning of |
868 // the stored data. | 883 // the stored data. |
869 if (type.GetStorableType() == CREDIT_CARD_NUMBER) { | 884 if (type.GetStorableType() == CREDIT_CARD_NUMBER) { |
870 if (creditcard_field_value.find(field_contents) == base::string16::npos && | 885 if (creditcard_field_value.find(field_contents) == base::string16::npos && |
871 (credit_card->record_type() != CreditCard::MASKED_SERVER_CARD || | 886 (credit_card->record_type() != CreditCard::MASKED_SERVER_CARD || |
872 field_contents.size() >= 6)) { | 887 field_contents.size() >= 6)) { |
873 continue; | 888 continue; |
874 } | 889 } |
875 } else if (!base::StartsWith(creditcard_field_value, field_contents, | 890 cards_to_suggest.push_back(credit_card); |
876 false)) { | 891 } else if (base::StartsWith(creditcard_field_value, field_contents, |
877 continue; | 892 false)) { |
| 893 cards_to_suggest.push_back(credit_card); |
| 894 } else if (ContainsTokenThatStartsWith(creditcard_field_value, |
| 895 field_contents, false)) { |
| 896 substring_matched_cards.push_back(credit_card); |
878 } | 897 } |
| 898 } |
879 | 899 |
880 cards_to_suggest.push_back(credit_card); | 900 cards_to_suggest.sort(RankByMfu); |
| 901 |
| 902 // Prefix matches should precede other token matches. |
| 903 if (IsFeatureSubstringMatchEnabled()) { |
| 904 substring_matched_cards.sort(RankByMfu); |
| 905 cards_to_suggest.insert(cards_to_suggest.end(), |
| 906 substring_matched_cards.begin(), |
| 907 substring_matched_cards.end()); |
881 } | 908 } |
882 | 909 |
883 // De-dupe card suggestions. Full server cards shadow local cards, and | 910 // De-dupe card suggestions. Full server cards shadow local cards, and |
884 // local cards shadow masked server cards. | 911 // local cards shadow masked server cards. |
885 for (auto outer_it = cards_to_suggest.begin(); | 912 for (auto outer_it = cards_to_suggest.begin(); |
886 outer_it != cards_to_suggest.end(); | 913 outer_it != cards_to_suggest.end(); |
887 ++outer_it) { | 914 ++outer_it) { |
888 | 915 |
889 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) { | 916 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) { |
890 for (auto inner_it = cards_to_suggest.begin(); | 917 for (auto inner_it = cards_to_suggest.begin(); |
891 inner_it != cards_to_suggest.end();) { | 918 inner_it != cards_to_suggest.end();) { |
892 auto inner_it_copy = inner_it++; | 919 auto inner_it_copy = inner_it++; |
893 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it)) | 920 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it)) |
894 cards_to_suggest.erase(inner_it_copy); | 921 cards_to_suggest.erase(inner_it_copy); |
895 } | 922 } |
896 } else if ((*outer_it)->record_type() == CreditCard::LOCAL_CARD) { | 923 } else if ((*outer_it)->record_type() == CreditCard::LOCAL_CARD) { |
897 for (auto inner_it = cards_to_suggest.begin(); | 924 for (auto inner_it = cards_to_suggest.begin(); |
898 inner_it != cards_to_suggest.end();) { | 925 inner_it != cards_to_suggest.end();) { |
899 auto inner_it_copy = inner_it++; | 926 auto inner_it_copy = inner_it++; |
900 if ((*inner_it_copy)->record_type() == CreditCard::MASKED_SERVER_CARD && | 927 if ((*inner_it_copy)->record_type() == CreditCard::MASKED_SERVER_CARD && |
901 (*outer_it)->IsLocalDuplicateOfServerCard(**inner_it_copy)) { | 928 (*outer_it)->IsLocalDuplicateOfServerCard(**inner_it_copy)) { |
902 cards_to_suggest.erase(inner_it_copy); | 929 cards_to_suggest.erase(inner_it_copy); |
903 } | 930 } |
904 } | 931 } |
905 } | 932 } |
906 } | 933 } |
907 | 934 |
908 cards_to_suggest.sort(RankByMfu); | |
909 | |
910 std::vector<Suggestion> suggestions; | 935 std::vector<Suggestion> suggestions; |
911 for (const CreditCard* credit_card : cards_to_suggest) { | 936 for (const CreditCard* credit_card : cards_to_suggest) { |
912 // Make a new suggestion. | 937 // Make a new suggestion. |
913 suggestions.push_back(Suggestion()); | 938 suggestions.push_back(Suggestion()); |
914 Suggestion* suggestion = &suggestions.back(); | 939 Suggestion* suggestion = &suggestions.back(); |
915 | 940 |
916 suggestion->value = credit_card->GetInfo(type, app_locale_); | 941 suggestion->value = credit_card->GetInfo(type, app_locale_); |
917 suggestion->icon = base::UTF8ToUTF16(credit_card->type()); | 942 suggestion->icon = base::UTF8ToUTF16(credit_card->type()); |
918 suggestion->backend_id = credit_card->guid(); | 943 suggestion->backend_id = credit_card->guid(); |
919 | 944 |
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1333 } | 1358 } |
1334 if (IsExperimentalWalletIntegrationEnabled() && | 1359 if (IsExperimentalWalletIntegrationEnabled() && |
1335 pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { | 1360 pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { |
1336 profiles_.insert( | 1361 profiles_.insert( |
1337 profiles_.end(), server_profiles_.begin(), server_profiles_.end()); | 1362 profiles_.end(), server_profiles_.begin(), server_profiles_.end()); |
1338 } | 1363 } |
1339 return profiles_; | 1364 return profiles_; |
1340 } | 1365 } |
1341 | 1366 |
1342 } // namespace autofill | 1367 } // namespace autofill |
OLD | NEW |