#include "components/autofill/core/browser/payments_suggestion_generator.h"
#include <functional>
#include <string>
#include <vector>
#include "base/check_deref.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/i18n/case_conversion.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/autofill_browser_util.h"
#include "components/autofill/core/browser/autofill_client.h"
#include "components/autofill/core/browser/autofill_experiments.h"
#include "components/autofill/core/browser/autofill_optimization_guide.h"
#include "components/autofill/core/browser/data_model/autofill_offer_data.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/data_model/credit_card_benefit.h"
#include "components/autofill/core/browser/data_model/iban.h"
#include "components/autofill/core/browser/field_type_utils.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/metrics/payments/card_metadata_metrics.h"
#include "components/autofill/core/browser/metrics/suggestions_list_metrics.h"
#include "components/autofill/core/browser/payments/autofill_offer_manager.h"
#include "components/autofill/core/browser/payments/constants.h"
#include "components/autofill/core/browser/payments/payments_autofill_client.h"
#include "components/autofill/core/browser/payments_data_manager.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/ui/suggestion.h"
#include "components/autofill/core/browser/ui/suggestion_type.h"
#include "components/autofill/core/common/autofill_clock.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/autofill_util.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/grit/components_scaled_resources.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
#include "ui/native_theme/native_theme.h"
#endif
namespace autofill {
namespace {
Suggestion CreateSeparator() { … }
Suggestion CreateUndoOrClearFormSuggestion() { … }
std::map<std::string, AutofillOfferData*> GetCardLinkedOffers(
const AutofillClient& autofill_client) { … }
int GetObfuscationLength() { … }
bool ShouldSplitCardNameAndLastFourDigits() { … }
bool AddCreditCardNameChildSuggestion(const CreditCard& credit_card,
const std::string& app_locale,
Suggestion& suggestion) { … }
bool AddCreditCardNumberChildSuggestion(const CreditCard& credit_card,
const std::string& app_locale,
Suggestion& suggestion) { … }
void AddCreditCardExpiryDateChildSuggestion(const CreditCard& credit_card,
const std::string& app_locale,
Suggestion& suggestion) { … }
bool IsValidPaymentsSuggestionForFieldContents(
std::u16string suggestion_canon,
std::u16string field_contents_canon,
FieldType trigger_field_type,
bool is_masked_server_card,
bool field_is_autofilled) { … }
bool IsCreditCardExpiryData(FieldType trigger_field_type) { … }
Suggestion CreateManagePaymentMethodsEntry(SuggestionType suggestion_type,
bool with_gpay_logo) { … }
void RemoveExpiredLocalCreditCardsNotUsedSinceTimestamp(
base::Time min_last_used,
std::vector<CreditCard*>& cards) { … }
std::u16string GetDisplayNicknameForCreditCard(
const CreditCard& card,
const PaymentsDataManager& payments_data) { … }
void AddPaymentsGranularFillingChildSuggestions(const CreditCard& credit_card,
Suggestion& suggestion,
const std::string& app_locale) { … }
std::pair<Suggestion::Text, Suggestion::Text>
GetSuggestionMainTextAndMinorTextForCard(const CreditCard& credit_card,
const AutofillClient& client,
FieldType trigger_field_type) { … }
#if !BUILDFLAG(IS_ANDROID)
Suggestion::Text GetBenefitTextWithTermsAppended(
const std::u16string& benefit_text) { … }
std::optional<Suggestion::Text> GetCreditCardBenefitSuggestionLabel(
const CreditCard& credit_card,
const AutofillClient& client) { … }
#endif
void SetSuggestionLabelsForCard(
const CreditCard& credit_card,
const AutofillClient& client,
FieldType trigger_field_type,
autofill_metrics::CardMetadataLoggingContext& metadata_logging_context,
Suggestion& suggestion) { … }
void AdjustVirtualCardSuggestionContent(Suggestion& suggestion,
const CreditCard& credit_card,
const AutofillClient& client,
FieldType trigger_field_type) { … }
void SetCardArtURL(Suggestion& suggestion,
const CreditCard& credit_card,
const PaymentsDataManager& payments_data,
bool virtual_card_option) { … }
std::vector<Suggestion> GetCreditCardFooterSuggestions(
bool should_show_scan_credit_card,
bool should_show_cards_from_account,
bool is_autofilled,
bool with_gpay_logo) { … }
bool ShouldShowVirtualCardOptionForServerCard(const CreditCard& card,
const AutofillClient& client) { … }
bool ShouldShowVirtualCardOption(const CreditCard* candidate_card,
const AutofillClient& client) { … }
std::vector<CreditCard> GetOrderedCardsToSuggest(
const AutofillClient& client,
const FormFieldData& trigger_field,
FieldType trigger_field_type,
bool suppress_disused_cards,
bool prefix_match,
bool require_non_empty_value_on_trigger_field,
bool include_virtual_cards,
bool use_legacy_algorithm = false) { … }
Suggestion CreateCreditCardSuggestion(
const CreditCard& credit_card,
const AutofillClient& client,
FieldType trigger_field_type,
bool virtual_card_option,
bool card_linked_offer_available,
autofill_metrics::CardMetadataLoggingContext& metadata_logging_context) { … }
}
std::vector<Suggestion> GetSuggestionsForCreditCards(
const AutofillClient& client,
const FormFieldData& trigger_field,
const std::vector<std::u16string>& last_four_list_for_suggestion_filtering,
FieldType trigger_field_type,
AutofillSuggestionTriggerSource trigger_source,
bool should_show_scan_credit_card,
bool should_show_cards_from_account,
CreditCardSuggestionSummary& summary) { … }
std::vector<Suggestion> GetSuggestionsForVirtualCardStandaloneCvc(
const AutofillClient& client,
const FormFieldData& trigger_field,
autofill_metrics::CardMetadataLoggingContext& metadata_logging_context,
base::flat_map<std::string, VirtualCardUsageData::VirtualCardLastFour>&
virtual_card_guid_to_last_four_map) { … }
std::vector<CreditCard> GetTouchToFillCardsToSuggest(
const AutofillClient& client,
const FormFieldData& trigger_field,
FieldType trigger_field_type) { … }
std::vector<Suggestion> GetCreditCardSuggestionsForTouchToFill(
base::span<const CreditCard> credit_cards,
const AutofillClient& client) { … }
Suggestion CreateManageCreditCardsSuggestion(bool with_gpay_logo) { … }
Suggestion CreateManageIbansSuggestion() { … }
std::vector<Suggestion> GetSuggestionsForIbans(const std::vector<Iban>& ibans) { … }
std::vector<Suggestion> GetPromoCodeSuggestionsFromPromoCodeOffers(
const std::vector<const AutofillOfferData*>& promo_code_offers) { … }
bool IsCardSuggestionAcceptable(const CreditCard& card,
const AutofillClient& client,
bool is_manual_fallback) { … }
std::vector<CreditCard> GetOrderedCardsToSuggestForTest(
const AutofillClient& client,
const FormFieldData& trigger_field,
FieldType trigger_field_type,
bool suppress_disused_cards,
bool prefix_match,
bool require_non_empty_value_on_trigger_field,
bool include_virtual_cards,
bool use_legacy_algorithm) { … }
Suggestion CreateCreditCardSuggestionForTest(
const CreditCard& credit_card,
const AutofillClient& client,
FieldType trigger_field_type,
bool virtual_card_option,
bool card_linked_offer_available,
base::optional_ref<autofill_metrics::CardMetadataLoggingContext>
metadata_logging_context) { … }
bool ShouldShowVirtualCardOptionForTest(const CreditCard* candidate_card,
const AutofillClient& client) { … }
}