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