File: amount_extraction_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (253 lines) | stat: -rw-r--r-- 9,788 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2025 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/amount_extraction_manager.h"

#include <memory>
#include <string>

#include "base/check_deref.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/foundations/autofill_driver.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#include "components/autofill/core/browser/integrators/optimization_guide/autofill_optimization_guide.h"
#include "components/autofill/core/browser/metrics/payments/amount_extraction_metrics.h"
#include "components/autofill/core/browser/payments/amount_extraction_heuristic_regexes.h"
#include "components/autofill/core/browser/payments/bnpl_manager.h"
#include "components/autofill/core/browser/payments/constants.h"
#include "components/autofill/core/browser/suggestions/suggestions_context.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "third_party/re2/src/re2/re2.h"
#include "url/gurl.h"

namespace autofill::payments {

AmountExtractionManager::AmountExtractionManager(
    BrowserAutofillManager* autofill_manager)
    : autofill_manager_(CHECK_DEREF(autofill_manager)) {}

AmountExtractionManager::~AmountExtractionManager() = default;

// static
std::optional<uint64_t>
AmountExtractionManager::MaybeParseAmountToMonetaryMicroUnits(
    const std::string& amount) {
  const RE2 re(
      R"([^0-9,eE\-]*(0|[0-9]{1,3}(,?[0-9]{3})*)(\.([0-9]{2}))[^0-9eE\-]*)");
  std::string dollar;
  std::string cent;
  // The first regex capture group gives dollar and the fourth gives the cent.
  if (!RE2::FullMatch(amount, re, &dollar, nullptr, nullptr, &cent)) {
    return std::nullopt;
  }
  std::erase(dollar, ',');

  uint64_t dollar_value = 0;
  uint64_t cent_value = 0;
  base::StringToUint64(dollar, &dollar_value);
  base::StringToUint64(cent, &cent_value);

  // Safely multiply to convert amount to micro.
  uint64_t micro_amount = 0;
  base::CheckedNumeric<uint64_t> checked_dollar_value =
      base::CheckedNumeric<uint64_t>(dollar_value) * kMicrosPerDollar;
  base::CheckedNumeric<uint64_t> checked_cent_value =
      base::CheckedNumeric<uint64_t>(cent_value) * (kMicrosPerDollar / 100);
  base::CheckedNumeric<uint64_t> checked_result =
      checked_dollar_value + checked_cent_value;
  if (!checked_result.AssignIfValid(&micro_amount)) {
    return std::nullopt;
  }
  return micro_amount;
}

DenseSet<AmountExtractionManager::EligibleFeature>
AmountExtractionManager::GetEligibleFeatures(const SuggestionsContext& context,
                                             bool should_suppress_suggestions,
                                             bool has_suggestions,
                                             FieldType field_type) const {
  // If there is an ongoing search, do not trigger the search.
  if (search_request_pending_) {
    return {};
  }
  // If autofill is not available, do not trigger the search.
  if (!context.is_autofill_available) {
    return {};
  }

  // If the interacted form field is CVC, do not trigger the search.
  if (kCvcFieldTypes.find(field_type) != kCvcFieldTypes.end()) {
    return {};
  }

  // If there are no suggestions, do not trigger the search as suggestions
  // showing is a requirement for amount extraction.
  if (!has_suggestions) {
    return {};
  }
  // If there are no suggestions, do not trigger the search as suggestions
  // showing is a requirement for amount extraction.
  if (should_suppress_suggestions) {
    return {};
  }
  // Amount extraction is only offered for Credit Card filling scenarios.
  if (context.filling_product != FillingProduct::kCreditCard) {
    return {};
  }

  // None of the projects that use amount extraction are intended to be enabled
  // in off-the-record mode, so do not run amount extraction in off-the-record
  // mode.
  if (autofill_manager_->client().IsOffTheRecord()) {
    return {};
  }

  if constexpr (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) ||
                BUILDFLAG(IS_CHROMEOS)) {
    if (base::FeatureList::IsEnabled(
            ::autofill::features::
                kAutofillEnableAmountExtractionDesktopLogging)) {
      // Insert all amount extraction eligible features for logging.
      return DenseSet<EligibleFeature>::all();
    }
  }

  const DenseSet<EligibleFeature> eligible_features =
      CheckEligiblilityForFeaturesRequiringAmountExtraction();

  // Run after all other feature eligibilities are checked to only check feature
  // flag for eligible users.
  // TODO(crbug.com/414648193): Rename amount extraction feature flag to
  // remove the platform restriction.
  if (!eligible_features.empty() &&
      base::FeatureList::IsEnabled(
          ::autofill::features::kAutofillEnableAmountExtractionDesktop)) {
    return eligible_features;
  }

  return {};
}

void AmountExtractionManager::TriggerCheckoutAmountExtraction() {
  if (search_request_pending_) {
    return;
  }
  search_request_pending_ = true;
  const AmountExtractionHeuristicRegexes& heuristics =
      AmountExtractionHeuristicRegexes::GetInstance();
  GetMainFrameDriver()->ExtractLabeledTextNodeValue(
      base::UTF8ToUTF16(heuristics.amount_pattern()),
      base::UTF8ToUTF16(heuristics.keyword_pattern()),
      heuristics.number_of_ancestor_levels_to_search(),
      base::BindOnce(&AmountExtractionManager::OnCheckoutAmountReceived,
                     weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()));

  base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&AmountExtractionManager::OnTimeoutReached,
                     weak_ptr_factory_.GetWeakPtr()),
      kAmountExtractionWaitTime);
}

void AmountExtractionManager::SetSearchRequestPendingForTesting(
    bool search_request_pending) {
  search_request_pending_ = search_request_pending;
}

bool AmountExtractionManager::GetSearchRequestPendingForTesting() {
  return search_request_pending_;
}

void AmountExtractionManager::OnCheckoutAmountReceived(
    base::TimeTicks search_request_start_timestamp,
    const std::string& extracted_amount) {
  base::TimeDelta latency =
      base::TimeTicks::Now() - search_request_start_timestamp;
  autofill_metrics::LogAmountExtractionLatency(latency,
                                               !extracted_amount.empty());
  autofill_metrics::LogAmountExtractionResult(
      extracted_amount.empty()
          ? autofill_metrics::AmountExtractionResult::kAmountNotFound
          : autofill_metrics::AmountExtractionResult::kSuccessful);
  // Set `search_request_pending_` to false once the search is done.
  search_request_pending_ = false;
  // Invalidate the WeakPtr instance to ignore the scheduled delay task when the
  // amount is found.
  weak_ptr_factory_.InvalidateWeakPtrs();

  std::optional<uint64_t> parsed_extracted_amount =
      MaybeParseAmountToMonetaryMicroUnits(extracted_amount);

  if (BnplManager* bnpl_manager = autofill_manager_->GetPaymentsBnplManager()) {
    bnpl_manager->OnAmountExtractionReturned(parsed_extracted_amount);
  }
  if constexpr (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) ||
                BUILDFLAG(IS_CHROMEOS)) {
    if (base::FeatureList::IsEnabled(
            ::autofill::features::
                kAutofillEnableAmountExtractionDesktopLogging)) {
      VLOG(3) << "The result of amount extraction on domain "
              << autofill_manager_->client()
                     .GetLastCommittedPrimaryMainFrameOrigin()
              << " is " << extracted_amount << " with latency of "
              << latency.InMilliseconds() << " milliseconds.";
    }
  }
}

void AmountExtractionManager::OnTimeoutReached() {
  // If the amount is found, ignore this callback.
  if (!search_request_pending_) {
    return;
  }
  search_request_pending_ = false;
  weak_ptr_factory_.InvalidateWeakPtrs();
  autofill_metrics::LogAmountExtractionResult(
      autofill_metrics::AmountExtractionResult::kTimeout);
  // TODO(crbug.com/378517983): Add BNPL flow action logic here.
  if constexpr (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) ||
                BUILDFLAG(IS_CHROMEOS)) {
    if (base::FeatureList::IsEnabled(
            ::autofill::features::
                kAutofillEnableAmountExtractionDesktopLogging)) {
      VLOG(3) << "The amount extraction on domain "
              << autofill_manager_->client()
                     .GetLastCommittedPrimaryMainFrameOrigin()
              << " reached a timeout.";
    }
  }
}

DenseSet<AmountExtractionManager::EligibleFeature>
AmountExtractionManager::CheckEligiblilityForFeaturesRequiringAmountExtraction()
    const {
  DenseSet<EligibleFeature> eligible_features;

  // Check eligibility of BNPL feature.
  // Currently, BNPL is only offered for desktop platforms.
  if constexpr (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) ||
                BUILDFLAG(IS_CHROMEOS)) {
    if (BnplManager* bnpl_manager = autofill_manager_->GetPaymentsBnplManager();
        bnpl_manager && bnpl_manager->IsEligibleForBnpl()) {
      eligible_features.insert(EligibleFeature::kBnpl);
    }
  }

  return eligible_features;
}

AutofillDriver* AmountExtractionManager::GetMainFrameDriver() {
  AutofillDriver* driver = &autofill_manager_->driver();
  while (driver->GetParent()) {
    driver = driver->GetParent();
  }
  return driver;
}

}  // namespace autofill::payments