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

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

Issue 1982623002: [Autofill] Sort profiles and credit cards by frecency in PaymentRequest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments and refactored Created 4 years, 7 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>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/i18n/case_conversion.h" 14 #include "base/i18n/case_conversion.h"
15 #include "base/i18n/timezone.h" 15 #include "base/i18n/timezone.h"
16 #include "base/profiler/scoped_tracker.h" 16 #include "base/profiler/scoped_tracker.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "build/build_config.h" 20 #include "build/build_config.h"
21 #include "components/autofill/core/browser/address_i18n.h" 21 #include "components/autofill/core/browser/address_i18n.h"
22 #include "components/autofill/core/browser/autofill-inl.h" 22 #include "components/autofill/core/browser/autofill-inl.h"
23 #include "components/autofill/core/browser/autofill_country.h" 23 #include "components/autofill/core/browser/autofill_country.h"
24 #include "components/autofill/core/browser/autofill_data_util.h"
Mathieu 2016/05/20 13:57:09 needed?
sebsg 2016/05/24 17:27:32 Done.
24 #include "components/autofill/core/browser/autofill_experiments.h" 25 #include "components/autofill/core/browser/autofill_experiments.h"
25 #include "components/autofill/core/browser/autofill_field.h" 26 #include "components/autofill/core/browser/autofill_field.h"
26 #include "components/autofill/core/browser/autofill_metrics.h" 27 #include "components/autofill/core/browser/autofill_metrics.h"
27 #include "components/autofill/core/browser/country_data.h" 28 #include "components/autofill/core/browser/country_data.h"
28 #include "components/autofill/core/browser/country_names.h" 29 #include "components/autofill/core/browser/country_names.h"
29 #include "components/autofill/core/browser/form_structure.h" 30 #include "components/autofill/core/browser/form_structure.h"
30 #include "components/autofill/core/browser/personal_data_manager_observer.h" 31 #include "components/autofill/core/browser/personal_data_manager_observer.h"
31 #include "components/autofill/core/browser/phone_number.h" 32 #include "components/autofill/core/browser/phone_number.h"
32 #include "components/autofill/core/browser/phone_number_i18n.h" 33 #include "components/autofill/core/browser/phone_number_i18n.h"
33 #include "components/autofill/core/browser/validation.h" 34 #include "components/autofill/core/browser/validation.h"
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 680
680 bool PersonalDataManager::HasServerData() const { 681 bool PersonalDataManager::HasServerData() const {
681 return !server_credit_cards_.empty() || !server_profiles_.empty(); 682 return !server_credit_cards_.empty() || !server_profiles_.empty();
682 } 683 }
683 684
684 void PersonalDataManager::Refresh() { 685 void PersonalDataManager::Refresh() {
685 LoadProfiles(); 686 LoadProfiles();
686 LoadCreditCards(); 687 LoadCreditCards();
687 } 688 }
688 689
690 const std::vector<AutofillProfile*> PersonalDataManager::GetProfilesToSuggest()
691 const {
692 std::vector<AutofillProfile*> profiles = GetProfiles(true);
693
694 // Rank the suggestions by frecency (see AutofillDataModel for details).
695 base::Time comparison_time = base::Time::Now();
696 std::sort(profiles.begin(), profiles.end(),
697 [comparison_time](const AutofillDataModel* a,
698 const AutofillDataModel* b) {
699 return a->CompareFrecency(b, comparison_time);
700 });
701
702 return profiles;
703 }
704
689 std::vector<Suggestion> PersonalDataManager::GetProfileSuggestions( 705 std::vector<Suggestion> PersonalDataManager::GetProfileSuggestions(
690 const AutofillType& type, 706 const AutofillType& type,
691 const base::string16& field_contents, 707 const base::string16& field_contents,
692 bool field_is_autofilled, 708 bool field_is_autofilled,
693 const std::vector<ServerFieldType>& other_field_types) { 709 const std::vector<ServerFieldType>& other_field_types) {
694 if (IsInAutofillSuggestionsDisabledExperiment()) 710 if (IsInAutofillSuggestionsDisabledExperiment())
695 return std::vector<Suggestion>(); 711 return std::vector<Suggestion>();
696 712
697 base::string16 field_contents_canon = 713 base::string16 field_contents_canon =
698 AutofillProfile::CanonicalizeProfileString(field_contents); 714 AutofillProfile::CanonicalizeProfileString(field_contents);
699 715
700 std::vector<AutofillProfile*> profiles = GetProfiles(true); 716 // Get the profiles to suggest, which are already sorted.
701 717 std::vector<AutofillProfile*> profiles = GetProfilesToSuggest();
702 // Rank the suggestions by frecency (see AutofillDataModel for details).
703 base::Time comparison_time = base::Time::Now();
704 std::sort(profiles.begin(), profiles.end(),
705 [comparison_time](const AutofillDataModel* a,
706 const AutofillDataModel* b) {
707 return a->CompareFrecency(b, comparison_time);
708 });
709 718
710 std::vector<Suggestion> suggestions; 719 std::vector<Suggestion> suggestions;
711 // Match based on a prefix search. 720 // Match based on a prefix search.
712 std::vector<AutofillProfile*> matched_profiles; 721 std::vector<AutofillProfile*> matched_profiles;
713 for (AutofillProfile* profile : profiles) { 722 for (AutofillProfile* profile : profiles) {
714 base::string16 value = GetInfoInOneLine(profile, type, app_locale_); 723 base::string16 value = GetInfoInOneLine(profile, type, app_locale_);
715 if (value.empty()) 724 if (value.empty())
716 continue; 725 continue;
717 726
718 bool prefix_matched_suggestion; 727 bool prefix_matched_suggestion;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 // trial group or SIZE_MAX if no limit is defined. 792 // trial group or SIZE_MAX if no limit is defined.
784 std::string limit_str = variations::GetVariationParamValue( 793 std::string limit_str = variations::GetVariationParamValue(
785 kFrecencyFieldTrialName, kFrecencyFieldTrialLimitParam); 794 kFrecencyFieldTrialName, kFrecencyFieldTrialLimitParam);
786 size_t limit = base::StringToSizeT(limit_str, &limit) ? limit : SIZE_MAX; 795 size_t limit = base::StringToSizeT(limit_str, &limit) ? limit : SIZE_MAX;
787 796
788 unique_suggestions.resize(std::min(unique_suggestions.size(), limit)); 797 unique_suggestions.resize(std::min(unique_suggestions.size(), limit));
789 798
790 return unique_suggestions; 799 return unique_suggestions;
791 } 800 }
792 801
802 // TODO(crbug.com/613187): Investigate if it would be more efficient to dedupe
803 // with a vector instead of a list.
804 const std::vector<CreditCard*> PersonalDataManager::GetCreditCardsToSuggest()
805 const {
806 std::vector<CreditCard*> credit_cards = GetCreditCards();
807
808 std::list<CreditCard*> cards_to_dedupe(credit_cards.begin(),
809 credit_cards.end());
810
811 DedupeCreditCardToSuggest(&cards_to_dedupe);
812
813 std::vector<CreditCard*> cards_to_suggest(
814 std::make_move_iterator(std::begin(cards_to_dedupe)),
815 std::make_move_iterator(std::end(cards_to_dedupe)));
816
817 // Rank the cards by frecency (see AutofillDataModel for details). All expired
818 // cards should be suggested last, also by frecency.
819 base::Time comparison_time = base::Time::Now();
820 std::stable_sort(cards_to_suggest.begin(), cards_to_suggest.end(),
821 [comparison_time](const CreditCard* a, const CreditCard* b) {
822 bool a_is_expired = a->IsExpired(comparison_time);
823 if (a_is_expired != b->IsExpired(comparison_time))
824 return !a_is_expired;
825
826 return a->CompareFrecency(b, comparison_time);
827 });
828
829 return cards_to_suggest;
830 }
831
793 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions( 832 std::vector<Suggestion> PersonalDataManager::GetCreditCardSuggestions(
794 const AutofillType& type, 833 const AutofillType& type,
795 const base::string16& field_contents) { 834 const base::string16& field_contents) {
796 if (IsInAutofillSuggestionsDisabledExperiment()) 835 if (IsInAutofillSuggestionsDisabledExperiment())
797 return std::vector<Suggestion>(); 836 return std::vector<Suggestion>();
798 837
799 const std::vector<CreditCard*> credit_cards = GetCreditCards(); 838 return GetSuggestionsForCards(type, field_contents,
800 std::list<const CreditCard*> cards_to_suggest(credit_cards.begin(), 839 GetCreditCardsToSuggest());
801 credit_cards.end());
802
803 DedupeCreditCardToSuggest(&cards_to_suggest);
804
805 // Rank the cards by frecency (see AutofillDataModel for details). All expired
806 // cards should be suggested last, also by frecency.
807 base::Time comparison_time = base::Time::Now();
808 cards_to_suggest.sort(
809 [comparison_time](const CreditCard* a, const CreditCard* b) {
810 bool a_is_expired = a->IsExpired(comparison_time);
811 if (a_is_expired != b->IsExpired(comparison_time))
812 return !a_is_expired;
813
814 return a->CompareFrecency(b, comparison_time);
815 });
816
817 return GetSuggestionsForCards(type, field_contents, cards_to_suggest);
818 } 840 }
819 841
820 bool PersonalDataManager::IsAutofillEnabled() const { 842 bool PersonalDataManager::IsAutofillEnabled() const {
821 return ::autofill::IsAutofillEnabled(pref_service_); 843 return ::autofill::IsAutofillEnabled(pref_service_);
822 } 844 }
823 845
824 std::string PersonalDataManager::CountryCodeForCurrentTimezone() const { 846 std::string PersonalDataManager::CountryCodeForCurrentTimezone() const {
825 return base::CountryCodeForCurrentTimezone(); 847 return base::CountryCodeForCurrentTimezone();
826 } 848 }
827 849
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 979
958 // Failing that, guess based on locale. 980 // Failing that, guess based on locale.
959 if (default_country_code_.empty()) 981 if (default_country_code_.empty())
960 default_country_code_ = AutofillCountry::CountryCodeForLocale(app_locale()); 982 default_country_code_ = AutofillCountry::CountryCodeForLocale(app_locale());
961 983
962 return default_country_code_; 984 return default_country_code_;
963 } 985 }
964 986
965 // static 987 // static
966 void PersonalDataManager::DedupeCreditCardToSuggest( 988 void PersonalDataManager::DedupeCreditCardToSuggest(
967 std::list<const CreditCard*>* cards_to_suggest) { 989 std::list<CreditCard*>* cards_to_suggest) {
968 for (auto outer_it = cards_to_suggest->begin(); 990 for (auto outer_it = cards_to_suggest->begin();
969 outer_it != cards_to_suggest->end(); ++outer_it) { 991 outer_it != cards_to_suggest->end(); ++outer_it) {
970 // If considering a full server card, look for local cards that are 992 // If considering a full server card, look for local cards that are
971 // duplicates of it and remove them. 993 // duplicates of it and remove them.
972 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) { 994 if ((*outer_it)->record_type() == CreditCard::FULL_SERVER_CARD) {
973 for (auto inner_it = cards_to_suggest->begin(); 995 for (auto inner_it = cards_to_suggest->begin();
974 inner_it != cards_to_suggest->end();) { 996 inner_it != cards_to_suggest->end();) {
975 auto inner_it_copy = inner_it++; 997 auto inner_it_copy = inner_it++;
976 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it)) 998 if ((*inner_it_copy)->IsLocalDuplicateOfServerCard(**outer_it))
977 cards_to_suggest->erase(inner_it_copy); 999 cards_to_suggest->erase(inner_it_copy);
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
1441 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) { 1463 if (pref_service_->GetBoolean(prefs::kAutofillWalletImportEnabled)) {
1442 profiles_.insert( 1464 profiles_.insert(
1443 profiles_.end(), server_profiles_.begin(), server_profiles_.end()); 1465 profiles_.end(), server_profiles_.begin(), server_profiles_.end());
1444 } 1466 }
1445 return profiles_; 1467 return profiles_;
1446 } 1468 }
1447 1469
1448 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards( 1470 std::vector<Suggestion> PersonalDataManager::GetSuggestionsForCards(
1449 const AutofillType& type, 1471 const AutofillType& type,
1450 const base::string16& field_contents, 1472 const base::string16& field_contents,
1451 const std::list<const CreditCard*>& cards_to_suggest) const { 1473 const std::vector<CreditCard*>& cards_to_suggest) const {
1452 std::vector<Suggestion> suggestions; 1474 std::vector<Suggestion> suggestions;
1453 std::list<const CreditCard*> substring_matched_cards;
1454 base::string16 field_contents_lower = base::i18n::ToLower(field_contents); 1475 base::string16 field_contents_lower = base::i18n::ToLower(field_contents);
1455 for (const CreditCard* credit_card : cards_to_suggest) { 1476 for (const CreditCard* credit_card : cards_to_suggest) {
1456 // The value of the stored data for this field type in the |credit_card|. 1477 // The value of the stored data for this field type in the |credit_card|.
1457 base::string16 creditcard_field_value = 1478 base::string16 creditcard_field_value =
1458 credit_card->GetInfo(type, app_locale_); 1479 credit_card->GetInfo(type, app_locale_);
1459 if (creditcard_field_value.empty()) 1480 if (creditcard_field_value.empty())
1460 continue; 1481 continue;
1461 base::string16 creditcard_field_lower = 1482 base::string16 creditcard_field_lower =
1462 base::i18n::ToLower(creditcard_field_value); 1483 base::i18n::ToLower(creditcard_field_value);
1463 1484
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1508 std::stable_sort(suggestions.begin(), suggestions.end(), 1529 std::stable_sort(suggestions.begin(), suggestions.end(),
1509 [](const Suggestion& a, const Suggestion& b) { 1530 [](const Suggestion& a, const Suggestion& b) {
1510 return a.match < b.match; 1531 return a.match < b.match;
1511 }); 1532 });
1512 } 1533 }
1513 1534
1514 return suggestions; 1535 return suggestions;
1515 } 1536 }
1516 1537
1517 } // namespace autofill 1538 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698