#include "components/autofill/core/browser/payments_data_manager.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/i18n/timezone.h"
#include "base/ranges/algorithm.h"
#include "base/strings/utf_string_conversions.h"
#include "base/uuid.h"
#include "components/autofill/core/browser/autofill_optimization_guide.h"
#include "components/autofill/core/browser/autofill_shared_storage_handler.h"
#include "components/autofill/core/browser/data_model/bank_account.h"
#include "components/autofill/core/browser/data_model/credit_card_art_image.h"
#include "components/autofill/core/browser/geo/autofill_country.h"
#include "components/autofill/core/browser/metrics/autofill_settings_metrics.h"
#include "components/autofill/core/browser/metrics/payments/card_metadata_metrics.h"
#include "components/autofill/core/browser/metrics/payments/cvc_storage_metrics.h"
#include "components/autofill/core/browser/metrics/payments/iban_metrics.h"
#include "components/autofill/core/browser/metrics/payments/mandatory_reauth_metrics.h"
#include "components/autofill/core/browser/metrics/payments/offers_metrics.h"
#include "components/autofill/core/browser/metrics/payments/wallet_usage_data_metrics.h"
#include "components/autofill/core/browser/payments/constants.h"
#include "components/autofill/core/browser/payments/payments_data_cleaner.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/ui/autofill_image_fetcher_base.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service_observer.h"
#include "components/autofill/core/common/autofill_clock.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/autofill/core/common/credit_card_number_validation.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/data_type.h"
#include "components/sync/service/sync_user_settings.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#endif
namespace autofill {
LogMandatoryReauthOfferOptInDecision;
MandatoryReauthOfferOptInDecision;
namespace {
template <typename ValueType>
void ReceiveLoadedDbValues(WebDataServiceBase::Handle h,
WDTypedResult* result,
WebDataServiceBase::Handle* pending_handle,
std::vector<ValueType>* dest) { … }
template <typename T>
const T& Deref(T* x) { … }
template <typename T, base::RawPtrTraits Traits = base::RawPtrTraits::kEmpty>
const T& Deref(const raw_ptr<T, Traits>& x) { … }
template <typename T>
const T& Deref(const std::unique_ptr<T>& x) { … }
template <typename T>
const T& Deref(const T& x) { … }
template <typename T>
typename std::vector<T>::const_iterator FindElementByGUID(
const std::vector<T>& container,
std::string_view guid) { … }
template <typename C>
bool FindByGUID(const C& container, std::string_view guid) { … }
template <typename C, typename T>
bool FindByContents(const C& container, const T& needle) { … }
}
class PaymentsDatabaseHelper { … };
PaymentsDataManager::PaymentsDataManager(
scoped_refptr<AutofillWebDataService> profile_database,
scoped_refptr<AutofillWebDataService> account_database,
AutofillImageFetcherBase* image_fetcher,
std::unique_ptr<AutofillSharedStorageHandler> shared_storage_handler,
PrefService* pref_service,
syncer::SyncService* sync_service,
signin::IdentityManager* identity_manager,
GeoIpCountryCode variations_country_code,
const std::string& app_locale)
: … { … }
PaymentsDataManager::~PaymentsDataManager() { … }
void PaymentsDataManager::Shutdown() { … }
void PaymentsDataManager::OnAutofillChangedBySync(syncer::DataType data_type) { … }
void PaymentsDataManager::OnWebDataServiceRequestDone(
WebDataServiceBase::Handle h,
std::unique_ptr<WDTypedResult> result) { … }
CoreAccountInfo PaymentsDataManager::GetAccountInfoForPaymentsServer() const { … }
bool PaymentsDataManager::IsSyncFeatureEnabledForPaymentsServerMetrics() const { … }
void PaymentsDataManager::OnStateChanged(syncer::SyncService* sync_service) { … }
void PaymentsDataManager::OnAccountsCookieDeletedByUserAction() { … }
void PaymentsDataManager::Refresh() { … }
const Iban* PaymentsDataManager::GetIbanByGUID(const std::string& guid) const { … }
const Iban* PaymentsDataManager::GetIbanByInstrumentId(
int64_t instrument_id) const { … }
CreditCard* PaymentsDataManager::GetCreditCardByGUID(const std::string& guid) { … }
CreditCard* PaymentsDataManager::GetCreditCardByNumber(
const std::string& number) { … }
CreditCard* PaymentsDataManager::GetCreditCardByInstrumentId(
int64_t instrument_id) { … }
CreditCard* PaymentsDataManager::GetCreditCardByServerId(
const std::string& server_id) { … }
template <typename T>
std::optional<T> PaymentsDataManager::GetCreditCardBenefitByInstrumentId(
CreditCardBenefitBase::LinkedCardInstrumentId instrument_id,
base::FunctionRef<bool(const T&)> filter) const { … }
std::optional<CreditCardFlatRateBenefit>
PaymentsDataManager::GetFlatRateBenefitByInstrumentId(
const CreditCardBenefitBase::LinkedCardInstrumentId instrument_id) const { … }
std::optional<CreditCardCategoryBenefit>
PaymentsDataManager::GetCategoryBenefitByInstrumentIdAndCategory(
const CreditCardBenefitBase::LinkedCardInstrumentId instrument_id,
const CreditCardCategoryBenefit::BenefitCategory benefit_category) const { … }
std::optional<CreditCardMerchantBenefit>
PaymentsDataManager::GetMerchantBenefitByInstrumentIdAndOrigin(
const CreditCardBenefitBase::LinkedCardInstrumentId instrument_id,
const url::Origin& merchant_origin) const { … }
std::u16string
PaymentsDataManager::GetApplicableBenefitDescriptionForCardAndOrigin(
const CreditCard& credit_card,
const url::Origin& origin,
const AutofillOptimizationGuide* optimization_guide) const { … }
std::vector<CreditCard*> PaymentsDataManager::GetLocalCreditCards() const { … }
std::vector<CreditCard*> PaymentsDataManager::GetServerCreditCards() const { … }
std::vector<CreditCard*> PaymentsDataManager::GetCreditCards() const { … }
std::vector<const Iban*> PaymentsDataManager::GetLocalIbans() const { … }
std::vector<const Iban*> PaymentsDataManager::GetServerIbans() const { … }
std::vector<const Iban*> PaymentsDataManager::GetIbans() const { … }
std::vector<Iban> PaymentsDataManager::GetOrderedIbansToSuggest() const { … }
bool PaymentsDataManager::HasMaskedBankAccounts() const { … }
base::span<const BankAccount> PaymentsDataManager::GetMaskedBankAccounts()
const { … }
PaymentsCustomerData* PaymentsDataManager::GetPaymentsCustomerData() const { … }
std::vector<CreditCardCloudTokenData*>
PaymentsDataManager::GetCreditCardCloudTokenData() const { … }
std::vector<AutofillOfferData*> PaymentsDataManager::GetAutofillOffers() const { … }
std::vector<const AutofillOfferData*>
PaymentsDataManager::GetActiveAutofillPromoCodeOffersForOrigin(
GURL origin) const { … }
GURL PaymentsDataManager::GetCardArtURL(const CreditCard& credit_card) const { … }
gfx::Image* PaymentsDataManager::GetCreditCardArtImageForUrl(
const GURL& card_art_url) const { … }
gfx::Image* PaymentsDataManager::GetCachedCardArtImageForUrl(
const GURL& card_art_url) const { … }
void PaymentsDataManager::SetPrefService(PrefService* pref_service) { … }
void PaymentsDataManager::NotifyObservers() { … }
bool PaymentsDataManager::IsCardEligibleForBenefits(
const CreditCard& card) const { … }
bool PaymentsDataManager::IsCardBenefitsFeatureEnabled() { … }
bool PaymentsDataManager::IsCardBenefitsPrefEnabled() const { … }
bool PaymentsDataManager::IsCardBenefitsSyncEnabled() const { … }
bool PaymentsDataManager::IsAutofillPaymentMethodsEnabled() const { … }
bool PaymentsDataManager::IsAutofillHasSeenIbanPrefEnabled() const { … }
void PaymentsDataManager::SetAutofillHasSeenIban() { … }
bool PaymentsDataManager::IsAutofillWalletImportEnabled() const { … }
bool PaymentsDataManager::IsPaymentsWalletSyncTransportEnabled() const { … }
bool PaymentsDataManager::IsPaymentsDownloadActive() const { … }
AutofillMetrics::PaymentsSigninState
PaymentsDataManager::GetPaymentsSigninStateForMetrics() const { … }
bool PaymentsDataManager::IsCardPresentAsBothLocalAndServerCards(
const CreditCard& credit_card) { … }
const CreditCard* PaymentsDataManager::GetServerCardForLocalCard(
const CreditCard* local_card) const { … }
std::string PaymentsDataManager::OnAcceptedLocalCreditCardSave(
const CreditCard& imported_card) { … }
std::string PaymentsDataManager::OnAcceptedLocalIbanSave(Iban imported_iban) { … }
bool PaymentsDataManager::IsKnownCard(const CreditCard& credit_card) const { … }
bool PaymentsDataManager::IsServerCard(const CreditCard* credit_card) const { … }
bool PaymentsDataManager::ShouldShowCardsFromAccountOption() const { … }
void PaymentsDataManager::OnUserAcceptedCardsFromAccountOption() { … }
void PaymentsDataManager::OnUserAcceptedUpstreamOffer() { … }
void PaymentsDataManager::SetPaymentMethodsMandatoryReauthEnabled(
bool enabled) { … }
bool PaymentsDataManager::IsPaymentMethodsMandatoryReauthEnabled() { … }
bool PaymentsDataManager::ShouldShowPaymentMethodsMandatoryReauthPromo() { … }
void PaymentsDataManager::
IncrementPaymentMethodsMandatoryReauthPromoShownCounter() { … }
bool PaymentsDataManager::IsPaymentCvcStorageEnabled() { … }
base::span<const VirtualCardUsageData>
PaymentsDataManager::GetVirtualCardUsageData() const { … }
std::vector<CreditCard*> PaymentsDataManager::GetCreditCardsToSuggest(
bool should_use_legacy_algorithm) const { … }
std::string PaymentsDataManager::AddAsLocalIban(Iban iban) { … }
std::string PaymentsDataManager::UpdateIban(const Iban& iban) { … }
void PaymentsDataManager::AddCreditCard(const CreditCard& credit_card) { … }
void PaymentsDataManager::DeleteLocalCreditCards(
const std::vector<CreditCard>& cards) { … }
void PaymentsDataManager::DeleteAllLocalCreditCards() { … }
void PaymentsDataManager::UpdateCreditCard(const CreditCard& credit_card) { … }
void PaymentsDataManager::UpdateLocalCvc(const std::string& guid,
const std::u16string& cvc) { … }
void PaymentsDataManager::UpdateServerCardsMetadata(
const std::vector<CreditCard>& credit_cards) { … }
void PaymentsDataManager::AddServerCvc(int64_t instrument_id,
const std::u16string& cvc) { … }
void PaymentsDataManager::UpdateServerCvc(int64_t instrument_id,
const std::u16string& cvc) { … }
void PaymentsDataManager::RemoveServerCvc(int64_t instrument_id) { … }
void PaymentsDataManager::ClearServerCvcs() { … }
void PaymentsDataManager::ClearLocalCvcs() { … }
void PaymentsDataManager::ClearAllServerDataForTesting() { … }
void PaymentsDataManager::SetCreditCards(
std::vector<CreditCard>* credit_cards) { … }
bool PaymentsDataManager::SaveCardLocallyIfNew(
const CreditCard& imported_card) { … }
bool PaymentsDataManager::RemoveByGUID(const std::string& guid) { … }
void PaymentsDataManager::RecordUseOfCard(const CreditCard* card) { … }
void PaymentsDataManager::RecordUseOfIban(Iban& iban) { … }
void PaymentsDataManager::DedupeCreditCardToSuggest(
std::list<CreditCard*>* cards_to_suggest) { … }
scoped_refptr<AutofillWebDataService> PaymentsDataManager::GetLocalDatabase() { … }
scoped_refptr<AutofillWebDataService> PaymentsDataManager::GetServerDatabase() { … }
bool PaymentsDataManager::IsUsingAccountStorageForServerDataForTest() { … }
void PaymentsDataManager::CancelPendingServerQueries() { … }
bool PaymentsDataManager::ShouldSuggestServerPaymentMethods() const { … }
void PaymentsDataManager::LoadCreditCards() { … }
void PaymentsDataManager::LoadCreditCardCloudTokenData() { … }
void PaymentsDataManager::LoadIbans() { … }
void PaymentsDataManager::LoadMaskedBankAccounts() { … }
void PaymentsDataManager::LoadAutofillOffers() { … }
void PaymentsDataManager::LoadVirtualCardUsageData() { … }
void PaymentsDataManager::LoadCreditCardBenefits() { … }
void PaymentsDataManager::CancelPendingLocalQuery(
WebDataServiceBase::Handle* handle) { … }
void PaymentsDataManager::CancelPendingServerQuery(
WebDataServiceBase::Handle* handle) { … }
void PaymentsDataManager::LoadPaymentsCustomerData() { … }
void PaymentsDataManager::FetchImagesForURLs(
base::span<const GURL> updated_urls) const { … }
void PaymentsDataManager::LogStoredPaymentsDataMetrics() const { … }
void PaymentsDataManager::LogServerCardLinkClicked() const { … }
void PaymentsDataManager::LogServerIbanLinkClicked() const { … }
const std::string& PaymentsDataManager::GetCountryCodeForExperimentGroup()
const { … }
void PaymentsDataManager::SetSyncServiceForTest(
syncer::SyncService* sync_service) { … }
void PaymentsDataManager::AddMaskedBankAccountForTest(
const BankAccount& bank_account) { … }
void PaymentsDataManager::AddServerCreditCardForTest(
std::unique_ptr<CreditCard> credit_card) { … }
void PaymentsDataManager::AddCreditCardBenefitForTest(
CreditCardBenefit benefit) { … }
bool PaymentsDataManager::IsFacilitatedPaymentsPixUserPrefEnabled() const { … }
bool PaymentsDataManager::HasPendingPaymentQueries() const { … }
bool PaymentsDataManager::AreBankAccountsSupported() const { … }
void PaymentsDataManager::OnAutofillPaymentsCardBenefitsPrefChange() { … }
void PaymentsDataManager::ClearAllCreditCardBenefits() { … }
void PaymentsDataManager::OnCardArtImagesFetched(
const std::vector<std::unique_ptr<CreditCardArtImage>>& art_images) { … }
void PaymentsDataManager::ProcessCardArtUrlChanges() { … }
void PaymentsDataManager::OnServerCreditCardsRefreshed() { … }
size_t PaymentsDataManager::GetServerCardWithArtImageCount() const { … }
std::string PaymentsDataManager::SaveImportedCreditCard(
const CreditCard& imported_card) { … }
void PaymentsDataManager::OnMaskedBankAccountsRefreshed() { … }
}