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 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 | 684 |
685 bool PersonalDataManager::HasServerData() const { | 685 bool PersonalDataManager::HasServerData() const { |
686 return !server_credit_cards_.empty() || !server_profiles_.empty(); | 686 return !server_credit_cards_.empty() || !server_profiles_.empty(); |
687 } | 687 } |
688 | 688 |
689 void PersonalDataManager::Refresh() { | 689 void PersonalDataManager::Refresh() { |
690 LoadProfiles(); | 690 LoadProfiles(); |
691 LoadCreditCards(); | 691 LoadCreditCards(); |
692 } | 692 } |
693 | 693 |
694 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest() | 694 std::vector<Suggestion> PersonalDataManager::GetProfileSuggestions( |
695 const { | 695 const AutofillType& type, |
| 696 const base::string16& field_contents, |
| 697 bool field_is_autofilled, |
| 698 const std::vector<ServerFieldType>& other_field_types) { |
| 699 if (IsInAutofillSuggestionsDisabledExperiment()) |
| 700 return std::vector<Suggestion>(); |
| 701 |
| 702 base::string16 field_contents_canon = |
| 703 AutofillProfile::CanonicalizeProfileString(field_contents); |
| 704 |
696 std::vector<AutofillProfile*> profiles = GetProfiles(true); | 705 std::vector<AutofillProfile*> profiles = GetProfiles(true); |
697 | 706 |
698 // Rank the suggestions by frecency (see AutofillDataModel for details). | 707 // Rank the suggestions by frecency (see AutofillDataModel for details). |
699 base::Time comparison_time = base::Time::Now(); | 708 base::Time comparison_time = base::Time::Now(); |
700 std::sort(profiles.begin(), profiles.end(), | 709 std::sort(profiles.begin(), profiles.end(), |
701 [comparison_time](const AutofillDataModel* a, | 710 [comparison_time](const AutofillDataModel* a, |
702 const AutofillDataModel* b) { | 711 const AutofillDataModel* b) { |
703 return a->CompareFrecency(b, comparison_time); | 712 return a->CompareFrecency(b, comparison_time); |
704 }); | 713 }); |
705 | 714 |
706 return profiles; | |
707 } | |
708 | |
709 std::vector<Suggestion> PersonalDataManager::GetProfileSuggestions( | |
710 const AutofillType& type, | |
711 const base::string16& field_contents, | |
712 bool field_is_autofilled, | |
713 const std::vector<ServerFieldType>& other_field_types) { | |
714 if (IsInAutofillSuggestionsDisabledExperiment()) | |
715 return std::vector<Suggestion>(); | |
716 | |
717 base::string16 field_contents_canon = | |
718 AutofillProfile::CanonicalizeProfileString(field_contents); | |
719 | |
720 // Get the profiles to suggest, which are already sorted. | |
721 std::vector<AutofillProfile*> profiles = GetProfilesToSuggest(); | |
722 | |
723 std::vector<Suggestion> suggestions; | 715 std::vector<Suggestion> suggestions; |
724 // Match based on a prefix search. | 716 // Match based on a prefix search. |
725 std::vector<AutofillProfile*> matched_profiles; | 717 std::vector<AutofillProfile*> matched_profiles; |
726 for (AutofillProfile* profile : profiles) { | 718 for (AutofillProfile* profile : profiles) { |
727 base::string16 value = GetInfoInOneLine(profile, type, app_locale_); | 719 base::string16 value = GetInfoInOneLine(profile, type, app_locale_); |
728 if (value.empty()) | 720 if (value.empty()) |
729 continue; | 721 continue; |
730 | 722 |
731 bool prefix_matched_suggestion; | 723 bool prefix_matched_suggestion; |
732 base::string16 suggestion_canon = | 724 base::string16 suggestion_canon = |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 // trial group or SIZE_MAX if no limit is defined. | 788 // trial group or SIZE_MAX if no limit is defined. |
797 std::string limit_str = variations::GetVariationParamValue( | 789 std::string limit_str = variations::GetVariationParamValue( |
798 kFrecencyFieldTrialName, kFrecencyFieldTrialLimitParam); | 790 kFrecencyFieldTrialName, kFrecencyFieldTrialLimitParam); |
799 size_t limit = base::StringToSizeT(limit_str, &limit) ? limit : SIZE_MAX; | 791 size_t limit = base::StringToSizeT(limit_str, &limit) ? limit : SIZE_MAX; |
800 | 792 |
801 unique_suggestions.resize(std::min(unique_suggestions.size(), limit)); | 793 unique_suggestions.resize(std::min(unique_suggestions.size(), limit)); |
802 | 794 |
803 return unique_suggestions; | 795 return unique_suggestions; |
804 } | 796 } |
805 | 797 |
806 // TODO(crbug.com/613187): Investigate if it would be more efficient to dedupe | |
807 // with a vector instead of a list. | |
808 const std::vector<CreditCard*> PersonalDataManager::GetCreditCardsToSuggest() | |
809 const { | |
810 std::vector<CreditCard*> credit_cards = GetCreditCards(); | |
811 | |
812 std::list<CreditCard*> cards_to_dedupe(credit_cards.begin(), | |
813 credit_cards.end()); | |
814 | |
815 DedupeCreditCardToSuggest(&cards_to_dedupe); | |
816 | |
817 std::vector<CreditCard*> cards_to_suggest( | |
818 std::make_move_iterator(std::begin(cards_to_dedupe)), | |
819 std::make_move_iterator(std::end(cards_to_dedupe))); | |
820 | |
821 // Rank the cards by frecency (see AutofillDataModel for details). All expired | |
822 // cards should be suggested last, also by frecency. | |
823 base::Time comparison_time = base::Time::Now(); | |
824 std::stable_sort(cards_to_suggest.begin(), cards_to_suggest.end(), | |
825 [comparison_time](const CreditCard* a, const CreditCard* b) { | |
826 bool a_is_expired = a->IsExpired(comparison_time); | |
827 if (a_is_expired != b->IsExpired(comparison_time)) | |
828 return !a_is_expired; | |
829 | |
830 return a->CompareFrecency(b, comparison_time); | |
831 }); | |
832 | |
833 return cards_to_suggest; | |
834 } | |
835 | |
836 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions( | 798 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions( |
837 const AutofillType& type, | 799 const AutofillType& type, |
838 const base::string16& field_contents) { | 800 const base::string16& field_contents) { |
839 if (IsInAutofillSuggestionsDisabledExperiment()) | 801 if (IsInAutofillSuggestionsDisabledExperiment()) |
840 return std::vector<Suggestion>(); | 802 return std::vector<Suggestion>(); |
841 | 803 |
842 return GetSuggestionsForCards(type, field_contents, | 804 const std::vector<CreditCard*> credit_cards = GetCreditCards(); |
843 GetCreditCardsToSuggest()); | 805 std::list<const CreditCard*> cards_to_suggest(credit_cards.begin(), |
| 806 credit_cards.end()); |
| 807 |
| 808 DedupeCreditCardToSuggest(&cards_to_suggest); |
| 809 |
| 810 // Rank the cards by frecency (see AutofillDataModel for details). All expired |
| 811 // cards should be suggested last, also by frecency. |
| 812 base::Time comparison_time = base::Time::Now(); |
| 813 cards_to_suggest.sort( |
| 814 [comparison_time](const CreditCard* a, const CreditCard* b) { |
| 815 bool a_is_expired = a->IsExpired(comparison_time); |
| 816 if (a_is_expired != b->IsExpired(comparison_time)) |
| 817 return !a_is_expired; |
| 818 |
| 819 return a->CompareFrecency(b, comparison_time); |
| 820 }); |
| 821 |
| 822 return GetSuggestionsForCards(type, field_contents, cards_to_suggest); |
844 } | 823 } |
845 | 824 |
846 bool PersonalDataManager::IsAutofillEnabled() const { | 825 bool PersonalDataManager::IsAutofillEnabled() const { |
847 return ::autofill::IsAutofillEnabled(pref_service_); | 826 return ::autofill::IsAutofillEnabled(pref_service_); |
848 } | 827 } |
849 | 828 |
850 std::string PersonalDataManager::CountryCodeForCurrentTimezone() const { | 829 std::string PersonalDataManager::CountryCodeForCurrentTimezone() const { |
851 return base::CountryCodeForCurrentTimezone(); | 830 return base::CountryCodeForCurrentTimezone(); |
852 } | 831 } |
853 | 832 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
981 | 960 |
982 // Failing that, guess based on locale. | 961 // Failing that, guess based on locale. |
983 if (default_country_code_.empty()) | 962 if (default_country_code_.empty()) |
984 default_country_code_ = AutofillCountry::CountryCodeForLocale(app_locale()); | 963 default_country_code_ = AutofillCountry::CountryCodeForLocale(app_locale()); |
985 | 964 |
986 return default_country_code_; | 965 return default_country_code_; |
987 } | 966 } |
988 | 967 |
989 // static | 968 // static |
990 void PersonalDataManager::DedupeCreditCardToSuggest( | 969 void PersonalDataManager::DedupeCreditCardToSuggest( |
991 std::list<CreditCard*>* cards_to_suggest) { | 970 std::list<const CreditCard*>* cards_to_suggest) { |
992 for (auto outer_it = cards_to_suggest->begin(); | 971 for (auto outer_it = cards_to_suggest->begin(); |
993 outer_it != cards_to_suggest->end(); ++outer_it) { | 972 outer_it != cards_to_suggest->end(); ++outer_it) { |
994 // If considering a full server card, look for local cards that are | 973 // If considering a full server card, look for local cards that are |
995 // duplicates of it and remove them. | 974 // duplicates of it and remove them. |
996 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) { | 975 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) { |
997 for (auto inner_it = cards_to_suggest->begin(); | 976 for (auto inner_it = cards_to_suggest->begin(); |
998 inner_it != cards_to_suggest->end();) { | 977 inner_it != cards_to_suggest->end();) { |
999 auto inner_it_copy = inner_it++; | 978 auto inner_it_copy = inner_it++; |
1000 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it)) | 979 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it)) |
1001 cards_to_suggest->erase(inner_it_copy); | 980 cards_to_suggest->erase(inner_it_copy); |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1465 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { | 1444 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { |
1466 profiles_.insert( | 1445 profiles_.insert( |
1467 profiles_.end(), server_profiles_.begin(), server_profiles_.end()); | 1446 profiles_.end(), server_profiles_.begin(), server_profiles_.end()); |
1468 } | 1447 } |
1469 return profiles_; | 1448 return profiles_; |
1470 } | 1449 } |
1471 | 1450 |
1472 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( | 1451 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( |
1473 const AutofillType& type, | 1452 const AutofillType& type, |
1474 const base::string16& field_contents, | 1453 const base::string16& field_contents, |
1475 const std::vector<CreditCard*>& cards_to_suggest) const { | 1454 const std::list<const CreditCard*>& cards_to_suggest) const { |
1476 std::vector<Suggestion> suggestions; | 1455 std::vector<Suggestion> suggestions; |
| 1456 std::list<const CreditCard*> substring_matched_cards; |
1477 base::string16 field_contents_lower = base::i18n::ToLower(field_contents); | 1457 base::string16 field_contents_lower = base::i18n::ToLower(field_contents); |
1478 for (const CreditCard* credit_card : cards_to_suggest) { | 1458 for (const CreditCard* credit_card : cards_to_suggest) { |
1479 // The value of the stored data for this field type in the |credit_card|. | 1459 // The value of the stored data for this field type in the |credit_card|. |
1480 base::string16 creditcard_field_value = | 1460 base::string16 creditcard_field_value = |
1481 credit_card->GetInfo(type, app_locale_); | 1461 credit_card->GetInfo(type, app_locale_); |
1482 if (creditcard_field_value.empty()) | 1462 if (creditcard_field_value.empty()) |
1483 continue; | 1463 continue; |
1484 base::string16 creditcard_field_lower = | 1464 base::string16 creditcard_field_lower = |
1485 base::i18n::ToLower(creditcard_field_value); | 1465 base::i18n::ToLower(creditcard_field_value); |
1486 | 1466 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1531 std::stable_sort(suggestions.begin(), suggestions.end(), | 1511 std::stable_sort(suggestions.begin(), suggestions.end(), |
1532 [](const Suggestion& a, const Suggestion& b) { | 1512 [](const Suggestion& a, const Suggestion& b) { |
1533 return a.match < b.match; | 1513 return a.match < b.match; |
1534 }); | 1514 }); |
1535 } | 1515 } |
1536 | 1516 |
1537 return suggestions; | 1517 return suggestions; |
1538 } | 1518 } |
1539 | 1519 |
1540 } // namespace autofill | 1520 } // namespace autofill |
OLD | NEW |