chromium/components/autofill/core/browser/webdata/payments/payments_autofill_table.cc

// Copyright 2013 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/webdata/payments/payments_autofill_table.h"

#include <stdint.h>

#include <algorithm>
#include <initializer_list>
#include <limits>
#include <map>
#include <memory>
#include <set>
#include <string_view>
#include <utility>
#include <vector>

#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/functional/overloaded.h"
#include "base/i18n/case_conversion.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_model/autofill_offer_data.h"
#include "components/autofill/core/browser/data_model/autofill_wallet_usage_data.h"
#include "components/autofill/core/browser/data_model/bank_account.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/data_model/credit_card_cloud_token_data.h"
#include "components/autofill/core/browser/data_model/iban.h"
#include "components/autofill/core/browser/data_model/payment_instrument.h"
#include "components/autofill/core/browser/data_model/payments_metadata.h"
#include "components/autofill/core/browser/field_type_utils.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/payments/payments_customer_data.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/webdata/autofill_change.h"
#include "components/autofill/core/browser/webdata/autofill_table_encryptor.h"
#include "components/autofill/core/browser/webdata/autofill_table_encryptor_factory.h"
#include "components/autofill/core/browser/webdata/autofill_table_utils.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_switches.h"
#include "components/autofill/core/common/autofill_util.h"
#include "components/webdata/common/web_database.h"
#include "sql/statement.h"
#include "sql/transaction.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace autofill {

namespace {

constexpr std::string_view kCreditCardsTable =;
constexpr std::string_view kGuid =;
constexpr std::string_view kNameOnCard =;
constexpr std::string_view kExpirationMonth =;
constexpr std::string_view kExpirationYear =;
constexpr std::string_view kCardNumberEncrypted =;
constexpr std::string_view kUseCount =;
constexpr std::string_view kUseDate =;
constexpr std::string_view kDateModified =;
constexpr std::string_view kOrigin =;
constexpr std::string_view kBillingAddressId =;
constexpr std::string_view kNickname =;

constexpr std::string_view kMaskedCreditCardsTable =;
constexpr std::string_view kId =;
constexpr std::string_view kStatus =;
// kNameOnCard = "name_on_card"
constexpr std::string_view kNetwork =;
constexpr std::string_view kLastFour =;
constexpr std::string_view kExpMonth =;
constexpr std::string_view kExpYear =;
constexpr std::string_view kBankName =;
// kNickname = "nickname"
constexpr std::string_view kCardIssuer =;
constexpr std::string_view kCardIssuerId =;
constexpr std::string_view kInstrumentId =;
constexpr std::string_view kVirtualCardEnrollmentState =;
constexpr std::string_view kVirtualCardEnrollmentType =;
constexpr std::string_view kCardArtUrl =;
constexpr std::string_view kProductDescription =;
constexpr std::string_view kProductTermsUrl =;

constexpr std::string_view kServerCardCloudTokenDataTable =;
// kId = "id"
constexpr std::string_view kSuffix =;
// kExpMonth = "exp_month"
// kExpYear = "exp_year"
// kCardArtUrl = "card_art_url"
constexpr std::string_view kInstrumentToken =;

constexpr std::string_view kServerCardMetadataTable =;
// kId = "id"
// kUseCount = "use_count"
// kUseDate = "use_date"
// kBillingAddressId = "billing_address_id"

// This shouldn't be used in new code, and it only exists for the purposes of
// migration logic. It has renamed to `local_ibans`.
constexpr std::string_view kIbansTable =;
constexpr std::string_view kLocalIbansTable =;
// kGuid = "guid"
// kUseCount = "use_count"
// kUseDate = "use_date"
constexpr std::string_view kValueEncrypted =;
// In an older version of the table, the value used to be unencrypted.
constexpr std::string_view kValue =;
// kNickname = "nickname"

constexpr std::string_view kMaskedIbansTable =;
// kInstrumentId = "instrument_id"
constexpr std::string_view kPrefix =;
// kSuffix = "suffix";
// kNickname = "nickname"

constexpr std::string_view kMaskedIbansMetadataTable =;
// kInstrumentId = "instrument_id"
// kUseCount = "use_count"
// kUseDate = "use_date"

constexpr std::string_view kPaymentsCustomerDataTable =;
constexpr std::string_view kCustomerId =;

constexpr std::string_view kPaymentsUpiVpaTable =;

constexpr std::string_view kOfferDataTable =;
constexpr std::string_view kOfferId =;
constexpr std::string_view kOfferRewardAmount =;
constexpr std::string_view kExpiry =;
constexpr std::string_view kOfferDetailsUrl =;
constexpr std::string_view kPromoCode =;
constexpr std::string_view kValuePropText =;
constexpr std::string_view kSeeDetailsText =;
constexpr std::string_view kUsageInstructionsText =;

constexpr std::string_view kOfferEligibleInstrumentTable =;
// kOfferId = "offer_id"
// kInstrumentId = "instrument_id"

constexpr std::string_view kOfferMerchantDomainTable =;
// kOfferId = "offer_id"
constexpr std::string_view kMerchantDomain =;

constexpr std::string_view kVirtualCardUsageDataTable =;
// kId = "id"
// kInstrumentId = "instrument_id"
// kMerchantDomain = "merchant_domain"
// kLastFour = "last_four"

constexpr std::string_view kLocalStoredCvcTable =;
// kGuid = "guid"
// kValueEncrypted = "value_encrypted"
constexpr std::string_view kLastUpdatedTimestamp =;

constexpr std::string_view kServerStoredCvcTable =;
// kInstrumentId = "instrument_id"
// kValueEncrypted = "value_encrypted"
// kLastUpdatedTimestamp = "last_updated_timestamp"

constexpr std::string_view kMaskedBankAccountsMetadataTable =;
// kInstrumentId = "instrument_id"
// kUseCount = "use_count"
// kUseDate = "use_date"
constexpr std::initializer_list<std::pair<std::string_view, std::string_view>>
    kMaskedBankAccountsMetadataColumnNamesAndTypes =;

constexpr std::string_view kMaskedBankAccountsTable =;
// kInstrumentId = "instrument_id"
// kBankName = "bank_name"
constexpr std::string_view kAccountNumberSuffix =;
constexpr std::string_view kAccountType =;
// kNickname = "nickname"
constexpr std::string_view kDisplayIconUrl =;
constexpr std::initializer_list<std::pair<std::string_view, std::string_view>>
    kMaskedBankAccountsColumnNamesAndTypes =;

constexpr std::string_view kMaskedCreditCardBenefitsTable =;
constexpr std::string_view kBenefitId =;
// kInstrumentId = "instrument_id"
constexpr std::string_view kBenefitType =;
constexpr std::string_view kBenefitCategory =;
constexpr std::string_view kBenefitDescription =;
constexpr std::string_view kStartTime =;
constexpr std::string_view kEndTime =;
constexpr std::initializer_list<std::pair<std::string_view, std::string_view>>
    kMaskedCreditCardBenefitsColumnNamesAndTypes =;

constexpr std::string_view kBenefitMerchantDomainsTable =;
// kBenefitId = "benefit_id"
// kMerchantDomain = "merchant_domain";
constexpr std::initializer_list<std::pair<std::string_view, std::string_view>>
    kBenefitMerchantDomainsColumnNamesAndTypes =;

constexpr std::string_view kGenericPaymentInstrumentsTable =;
// kInstrumentId = "instrument_id"
constexpr std::string_view kSerializedValueEncrypted =;
constexpr std::initializer_list<std::pair<std::string_view, std::string_view>>
    kGenericPaymentInstrumentsColumnNamesAndTypes =;

void BindEncryptedStringToColumn(sql::Statement* s,
                                 int column_index,
                                 const std::string& value,
                                 const AutofillTableEncryptor& encryptor) {}

void BindEncryptedU16StringToColumn(sql::Statement* s,
                                    int column_index,
                                    const std::u16string& value,
                                    const AutofillTableEncryptor& encryptor) {}

void BindCreditCardToStatement(const CreditCard& credit_card,
                               base::Time modification_date,
                               sql::Statement* s,
                               const AutofillTableEncryptor& encryptor) {}

void BindLocalStoredCvcToStatement(const std::string& guid,
                                   const std::u16string& cvc,
                                   base::Time modification_date,
                                   sql::Statement* s,
                                   const AutofillTableEncryptor& encryptor) {}

void BindServerCvcToStatement(const ServerCvc& server_cvc,
                              const AutofillTableEncryptor& encryptor,
                              sql::Statement* s) {}

void BindMaskedBankAccountToStatement(const BankAccount& bank_account,
                                      sql::Statement* s) {}

void BindIbanToStatement(const Iban& iban,
                         sql::Statement* s,
                         const AutofillTableEncryptor& encryptor) {}

void BindVirtualCardUsageDataToStatement(
    const VirtualCardUsageData& virtual_card_usage_data,
    sql::Statement& s) {}

void BindPaymentInstrumentToStatement(
    const sync_pb::PaymentInstrument& payment_instrument,
    sql::Statement* s,
    const AutofillTableEncryptor& encryptor) {}

VirtualCardUsageData GetVirtualCardUsageDataFromStatement(sql::Statement& s) {}

std::string DecryptStringFromColumn(sql::Statement& s,
                                    int column_index,
                                    const AutofillTableEncryptor& encryptor) {}

std::u16string DecryptU16StringFromColumn(
    sql::Statement& s,
    int column_index,
    const AutofillTableEncryptor& encryptor) {}

std::unique_ptr<CreditCard> CreditCardFromStatement(
    sql::Statement& card_statement,
    std::optional<std::reference_wrapper<sql::Statement>> cvc_statement,
    const AutofillTableEncryptor& encryptor) {}

std::unique_ptr<ServerCvc> ServerCvcFromStatement(
    sql::Statement& s,
    const AutofillTableEncryptor& encryptor) {}

std::unique_ptr<Iban> IbanFromStatement(
    sql::Statement& s,
    const AutofillTableEncryptor& encryptor) {}

WebDatabaseTable::TypeKey GetKey() {}

time_t GetEndTime(base::Time end) {}

}  // namespace

PaymentsAutofillTable::PaymentsAutofillTable()
    :{}

PaymentsAutofillTable::~PaymentsAutofillTable() = default;

// static
PaymentsAutofillTable* PaymentsAutofillTable::FromWebDatabase(WebDatabase* db) {}

WebDatabaseTable::TypeKey PaymentsAutofillTable::GetTypeKey() const {}

bool PaymentsAutofillTable::CreateTablesIfNecessary() {}

bool PaymentsAutofillTable::MigrateToVersion(int version,
                                             bool* update_compatible_version) {}

bool PaymentsAutofillTable::SetMaskedBankAccounts(
    const std::vector<BankAccount>& bank_accounts) {}

bool PaymentsAutofillTable::GetMaskedBankAccounts(
    std::vector<BankAccount>& bank_accounts) {}

bool PaymentsAutofillTable::AddLocalIban(const Iban& iban) {}

bool PaymentsAutofillTable::UpdateLocalIban(const Iban& iban) {}

bool PaymentsAutofillTable::RemoveLocalIban(const std::string& guid) {}

std::unique_ptr<Iban> PaymentsAutofillTable::GetLocalIban(const std::string& guid) {}

bool PaymentsAutofillTable::GetLocalIbans(std::vector<std::unique_ptr<Iban>>* ibans) {}

bool PaymentsAutofillTable::AddCreditCard(const CreditCard& credit_card) {}

bool PaymentsAutofillTable::UpdateCreditCard(const CreditCard& credit_card) {}

bool PaymentsAutofillTable::UpdateLocalCvc(const std::string& guid,
                                   const std::u16string& cvc) {}

bool PaymentsAutofillTable::RemoveCreditCard(const std::string& guid) {}

bool PaymentsAutofillTable::AddServerCreditCardForTesting(
    const CreditCard& credit_card) {}

std::unique_ptr<CreditCard> PaymentsAutofillTable::GetCreditCard(
    const std::string& guid) {}

bool PaymentsAutofillTable::GetCreditCards(
    std::vector<std::unique_ptr<CreditCard>>* credit_cards) {}

bool PaymentsAutofillTable::GetServerCreditCards(
    std::vector<std::unique_ptr<CreditCard>>& credit_cards) const {}

void PaymentsAutofillTable::SetServerCreditCards(
    const std::vector<CreditCard>& credit_cards) {}

bool PaymentsAutofillTable::AddServerCvc(const ServerCvc& server_cvc) {}

bool PaymentsAutofillTable::UpdateServerCvc(const ServerCvc& server_cvc) {}

bool PaymentsAutofillTable::RemoveServerCvc(int64_t instrument_id) {}

bool PaymentsAutofillTable::ClearServerCvcs() {}

std::vector<std::unique_ptr<ServerCvc>>
PaymentsAutofillTable::DeleteOrphanedServerCvcs() {}

std::vector<std::unique_ptr<ServerCvc>> PaymentsAutofillTable::GetAllServerCvcs()
    const {}

bool PaymentsAutofillTable::ClearLocalCvcs() {}

bool PaymentsAutofillTable::AddServerCardMetadata(
    const PaymentsMetadata& card_metadata) {}

bool PaymentsAutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) {}

bool PaymentsAutofillTable::UpdateServerCardMetadata(
    const PaymentsMetadata& card_metadata) {}

bool PaymentsAutofillTable::RemoveServerCardMetadata(const std::string& id) {}

bool PaymentsAutofillTable::GetServerCardsMetadata(
    std::vector<PaymentsMetadata>& cards_metadata) const {}

bool PaymentsAutofillTable::AddOrUpdateServerIbanMetadata(
    const PaymentsMetadata& iban_metadata) {}

bool PaymentsAutofillTable::RemoveServerIbanMetadata(const std::string& instrument_id) {}

bool PaymentsAutofillTable::GetServerIbansMetadata(
    std::vector<PaymentsMetadata>& ibans_metadata) const {}

void PaymentsAutofillTable::SetServerCardsData(
    const std::vector<CreditCard>& credit_cards) {}

void PaymentsAutofillTable::SetCreditCardCloudTokenData(
    const std::vector<CreditCardCloudTokenData>& credit_card_cloud_token_data) {}

bool PaymentsAutofillTable::GetCreditCardCloudTokenData(
    std::vector<std::unique_ptr<CreditCardCloudTokenData>>&
        credit_card_cloud_token_data) {}

bool PaymentsAutofillTable::GetServerIbans(std::vector<std::unique_ptr<Iban>>& ibans) {}

bool PaymentsAutofillTable::SetServerIbansData(const std::vector<Iban>& ibans) {}

void PaymentsAutofillTable::SetServerIbansForTesting(const std::vector<Iban>& ibans) {}

void PaymentsAutofillTable::SetPaymentsCustomerData(
    const PaymentsCustomerData* customer_data) {}

bool PaymentsAutofillTable::GetPaymentsCustomerData(
    std::unique_ptr<PaymentsCustomerData>& customer_data) const {}

void PaymentsAutofillTable::SetAutofillOffers(
    const std::vector<AutofillOfferData>& autofill_offer_data) {}

bool PaymentsAutofillTable::GetAutofillOffers(
    std::vector<std::unique_ptr<AutofillOfferData>>* autofill_offer_data) {}

bool PaymentsAutofillTable::AddOrUpdateVirtualCardUsageData(
    const VirtualCardUsageData& virtual_card_usage_data) {}

std::optional<VirtualCardUsageData>
PaymentsAutofillTable::GetVirtualCardUsageData(
    const std::string& usage_data_id) {}

bool PaymentsAutofillTable::RemoveVirtualCardUsageData(
    const std::string& usage_data_id) {}

void PaymentsAutofillTable::SetVirtualCardUsageData(
    const std::vector<VirtualCardUsageData>& virtual_card_usage_data) {}

bool PaymentsAutofillTable::GetAllVirtualCardUsageData(
    std::vector<VirtualCardUsageData>& virtual_card_usage_data) {}

bool PaymentsAutofillTable::RemoveAllVirtualCardUsageData() {}

bool PaymentsAutofillTable::ClearAllServerData() {}

bool PaymentsAutofillTable::RemoveAutofillDataModifiedBetween(
    base::Time delete_begin,
    base::Time delete_end,
    std::vector<std::unique_ptr<CreditCard>>* credit_cards) {}

bool PaymentsAutofillTable::RemoveOriginURLsModifiedBetween(
    base::Time delete_begin,
    base::Time delete_end) {}

bool PaymentsAutofillTable::SetCreditCardBenefits(
    const std::vector<CreditCardBenefit>& credit_card_benefits) {}

bool PaymentsAutofillTable::GetAllCreditCardBenefits(
    std::vector<CreditCardBenefit>& credit_card_benefits) {}

bool PaymentsAutofillTable::GetCreditCardBenefitsForInstrumentId(
    const std::optional<int64_t> instrument_id,
    std::vector<CreditCardBenefit>& credit_card_benefits) {}

bool PaymentsAutofillTable::ClearAllCreditCardBenefits() {}

bool PaymentsAutofillTable::SetPaymentInstruments(
    const std::vector<sync_pb::PaymentInstrument>& payment_instruments) {}

bool PaymentsAutofillTable::GetPaymentInstruments(
    std::vector<sync_pb::PaymentInstrument>& payment_instruments) {}

bool PaymentsAutofillTable::MigrateToVersion83RemoveServerCardTypeColumn() {}

bool PaymentsAutofillTable::MigrateToVersion84AddNicknameColumn() {}

bool PaymentsAutofillTable::
    MigrateToVersion85AddCardIssuerColumnToMaskedCreditCard() {}

bool PaymentsAutofillTable::MigrateToVersion87AddCreditCardNicknameColumn() {}

bool PaymentsAutofillTable::
    MigrateToVersion89AddInstrumentIdColumnToMaskedCreditCard() {}

bool PaymentsAutofillTable::MigrateToVersion94AddPromoCodeColumnsToOfferData() {}

bool PaymentsAutofillTable::MigrateToVersion95AddVirtualCardMetadata() {}

bool PaymentsAutofillTable::
    MigrateToVersion98RemoveStatusColumnMaskedCreditCards() {}

bool PaymentsAutofillTable::MigrateToVersion101RemoveCreditCardArtImageTable() {}

bool PaymentsAutofillTable::MigrateToVersion104AddProductDescriptionColumn() {}

bool PaymentsAutofillTable::MigrateToVersion105AddAutofillIbanTable() {}

bool PaymentsAutofillTable::MigrateToVersion106RecreateAutofillIbanTable() {}

bool PaymentsAutofillTable::MigrateToVersion108AddCardIssuerIdColumn() {}

bool PaymentsAutofillTable::MigrateToVersion109AddVirtualCardUsageDataTable() {}

bool PaymentsAutofillTable::
    MigrateToVersion111AddVirtualCardEnrollmentTypeColumn() {}

bool PaymentsAutofillTable::MigrateToVersion115EncryptIbanValue() {}

bool PaymentsAutofillTable::MigrateToVersion116AddStoredCvcTable() {}

bool PaymentsAutofillTable::MigrateToVersion118RemovePaymentsUpiVpaTable() {}

bool PaymentsAutofillTable::
    MigrateToVersion119AddMaskedIbanTablesAndRenameLocalIbanTable() {}

bool PaymentsAutofillTable::
    MigrateToVersion123AddProductTermsUrlColumnAndAddCardBenefitsTables() {}

bool PaymentsAutofillTable::
    MigrateToVersion124AndDeletePaymentInstrumentRelatedTablesAndAddMaskedBankAccountTable() {}

bool PaymentsAutofillTable::MigrateToVersion125DeleteFullServerCardsTable() {}

bool PaymentsAutofillTable::
    MigrateToVersion129AddGenericPaymentInstrumentsTable() {}

bool PaymentsAutofillTable::
    MigrateToVersion131RemoveGenericPaymentInstrumentTypeColumn() {}

bool PaymentsAutofillTable::
    MigrateToVersion133RemoveLengthColumnFromMaskedIbansTable() {}

void PaymentsAutofillTable::AddMaskedCreditCards(
    const std::vector<CreditCard>& credit_cards) {}

bool PaymentsAutofillTable::DeleteFromMaskedCreditCards(const std::string& id) {}

base::flat_set<url::Origin>
PaymentsAutofillTable::GetMerchantDomainsForBenefitId(
    const CreditCardBenefitBase::BenefitId& benefit_id) {}

bool PaymentsAutofillTable::InitCreditCardsTable() {}

bool PaymentsAutofillTable::InitLocalIbansTable() {}

bool PaymentsAutofillTable::InitMaskedCreditCardsTable() {}

bool PaymentsAutofillTable::InitMaskedIbansTable() {}

bool PaymentsAutofillTable::InitMaskedIbansMetadataTable() {}

bool PaymentsAutofillTable::InitServerCardMetadataTable() {}

bool PaymentsAutofillTable::InitPaymentsCustomerDataTable() {}

bool PaymentsAutofillTable::InitServerCreditCardCloudTokenDataTable() {}

bool PaymentsAutofillTable::InitStoredCvcTable() {}

bool PaymentsAutofillTable::InitOfferDataTable() {}

bool PaymentsAutofillTable::InitOfferEligibleInstrumentTable() {}

bool PaymentsAutofillTable::InitOfferMerchantDomainTable() {}

bool PaymentsAutofillTable::InitVirtualCardUsageDataTable() {}

bool PaymentsAutofillTable::InitMaskedBankAccountsTable() {}

bool PaymentsAutofillTable::InitMaskedBankAccountsMetadataTable() {}

bool PaymentsAutofillTable::InitMaskedCreditCardBenefitsTable() {}

bool PaymentsAutofillTable::InitBenefitMerchantDomainsTable() {}

bool PaymentsAutofillTable::InitGenericPaymentInstrumentsTable() {}

}  // namespace autofill