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

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

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

Powered by Google App Engine
This is Rietveld 408576698