chromium/components/autofill/core/browser/payments_suggestion_generator.cc

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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"  // nogncheck
#endif

namespace autofill {

namespace {

Suggestion CreateSeparator() {}

Suggestion CreateUndoOrClearFormSuggestion() {}

// Returns the card-linked offers map with credit card guid as the key and the
// pointer to the linked AutofillOfferData as the value.
std::map<std::string, AutofillOfferData*> GetCardLinkedOffers(
    const AutofillClient& autofill_client) {}

int GetObfuscationLength() {}

bool ShouldSplitCardNameAndLastFourDigits() {}

// Adds nested entry to the `suggestion` for filling credit card cardholder name
// if the `credit_card` has the corresponding info is set.
bool AddCreditCardNameChildSuggestion(const CreditCard& credit_card,
                                      const std::string& app_locale,
                                      Suggestion& suggestion) {}

// Adds nested entry to the `suggestion` for filling credit card number if the
// `credit_card` has the corresponding info is set.
bool AddCreditCardNumberChildSuggestion(const CreditCard& credit_card,
                                        const std::string& app_locale,
                                        Suggestion& suggestion) {}

// Adds nested entry to the `suggestion` for filling credit card number expiry
// date. The added entry has 2 nested entries for filling credit card expiry
// year and month.
void AddCreditCardExpiryDateChildSuggestion(const CreditCard& credit_card,
                                            const std::string& app_locale,
                                            Suggestion& suggestion) {}

// Returns whether the `suggestion_canon` is a valid match given
// `field_contents_canon`. To be used for payments suggestions.
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) {}

// Removes expired local credit cards not used since `min_last_used` from
// `cards`. The relative ordering of `cards` is maintained.
void RemoveExpiredLocalCreditCardsNotUsedSinceTimestamp(
    base::Time min_last_used,
    std::vector<CreditCard*>& cards) {}

// Return a nickname for the |card| to display. This is generally the nickname
// stored in |card|, unless |card| exists as a local and a server copy. In
// this case, we prefer the nickname of the local if it is defined. If only
// one copy has a nickname, take that.
std::u16string GetDisplayNicknameForCreditCard(
    const CreditCard& card,
    const PaymentsDataManager& payments_data) {}

// Creates nested/child suggestions for `suggestion` with the `credit_card`
// information. The number of nested suggestions added depends on the
// information present in the `credit_card`.
void AddPaymentsGranularFillingChildSuggestions(const CreditCard& credit_card,
                                                Suggestion& suggestion,
                                                const std::string& app_locale) {}

// Return the texts shown as the first line of the suggestion, based on the
// `credit_card` and the `trigger_field_type`. The first index in the pair
// represents the main text, and the second index represents the minor text.
// The minor text can be empty, in which case the main text should be rendered
// as the entire first line. If the minor text is not empty, they should be
// combined. This splitting is implemented for situations where the first part
// of the first line of the suggestion should be truncated.
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) {}

// Returns the benefit text to display in credit card suggestions if it is
// available.
std::optional<Suggestion::Text> GetCreditCardBenefitSuggestionLabel(
    const CreditCard& credit_card,
    const AutofillClient& client) {}
#endif  // !BUILDFLAG(IS_ANDROID)

// Set the labels to be shown in the suggestion. Note that this does not
// account for virtual cards or card-linked offers.
// `metadata_logging_context` the instrument ids of credit cards for which
// benefits data is available. When displaying card benefits is disabled,
// `metadata_logging_context` will be populated but a benefit label will not
// be shown.
void SetSuggestionLabelsForCard(
    const CreditCard& credit_card,
    const AutofillClient& client,
    FieldType trigger_field_type,
    autofill_metrics::CardMetadataLoggingContext& metadata_logging_context,
    Suggestion& suggestion) {}

// Adjust the content of `suggestion` if it is a virtual card suggestion.
void AdjustVirtualCardSuggestionContent(Suggestion& suggestion,
                                        const CreditCard& credit_card,
                                        const AutofillClient& client,
                                        FieldType trigger_field_type) {}

// Set the URL for the card art image to be shown in the `suggestion`.
void SetCardArtURL(Suggestion& suggestion,
                   const CreditCard& credit_card,
                   const PaymentsDataManager& payments_data,
                   bool virtual_card_option) {}

// Returns non credit card suggestions which are displayed below credit card
// suggestions in the Autofill popup. `should_show_scan_credit_card` is used
// to conditionally add scan credit card suggestion,
// `should_show_cards_from_account` - conditionally add suggestions for
// showing cards from account. `is_autofilled` is used to conditionally add
// suggestion for clearing all autofilled fields. `with_gpay_logo` is used to
// conditionally add GPay logo icon to the manage payment methods suggestion.
std::vector<Suggestion> GetCreditCardFooterSuggestions(
    bool should_show_scan_credit_card,
    bool should_show_cards_from_account,
    bool is_autofilled,
    bool with_gpay_logo) {}

// Returns true if we should show a virtual card option for the server card
// `card`, false otherwise.
bool ShouldShowVirtualCardOptionForServerCard(const CreditCard& card,
                                              const AutofillClient& client) {}

// Helper function to decide whether to show the virtual card option for
// `candidate_card`.
// TODO(crbug.com/326950201): Pass the argument by reference.
bool ShouldShowVirtualCardOption(const CreditCard* candidate_card,
                                 const AutofillClient& client) {}

// Returns the local and server cards ordered by the Autofill ranking.
// If `suppress_disused_cards`, local expired disused cards are removed.
// If `prefix_match`, cards are matched with the contents of `trigger_field`.
// If `include_virtual_cards`, virtual cards will be added when possible.
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) {}

// Creates a suggestion for the given `credit_card`. `virtual_card_option`
// suggests whether the suggestion is a virtual card option.
// `card_linked_offer_available` indicates whether a card-linked offer is
// attached to the `credit_card`. `metadata_logging_context` contains card
// metadata related information used for metrics logging.
// TODO(crbug.com/40232456): Separate logic for desktop, Android dropdown, and
// Keyboard Accessory.
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) {}

}  // namespace

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) {}

// static
Suggestion CreateManageCreditCardsSuggestion(bool with_gpay_logo) {}

// static
Suggestion CreateManageIbansSuggestion() {}

// static
std::vector<Suggestion> GetSuggestionsForIbans(const std::vector<Iban>& ibans) {}

// static
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) {}

}  // namespace autofill