File: iban_save_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 (446 lines) | stat: -rw-r--r-- 19,069 bytes parent folder | download | duplicates (3)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2022 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/iban_save_manager.h"

#include <algorithm>

#include "base/check_deref.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/data_model/payments/iban.h"
#include "components/autofill/core/browser/foundations/autofill_client.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/metrics/payments/iban_metrics.h"
#include "components/autofill/core/browser/payments/legal_message_line.h"
#include "components/autofill/core/browser/payments/payments_autofill_client.h"
#include "components/autofill/core/browser/payments/payments_network_interface.h"
#include "components/autofill/core/browser/payments/payments_util.h"
#include "components/autofill/core/browser/strike_databases/payments/iban_save_strike_database.h"
#include "components/autofill/core/browser/studies/autofill_experiments.h"
#include "components/autofill/core/common/autofill_regexes.h"
#include "components/sync/service/sync_user_settings.h"

namespace autofill {

using PaymentsRpcResult = payments::PaymentsAutofillClient::PaymentsRpcResult;

IbanSaveManager::IbanSaveManager(AutofillClient* client)
    : client_(CHECK_DEREF(client)) {}

IbanSaveManager::~IbanSaveManager() = default;

// static
std::string IbanSaveManager::GetPartialIbanHashString(
    const std::string& value) {
  std::string iban_hash_value = base::NumberToString(StrToHash64Bit(value));
  return iban_hash_value.substr(0, iban_hash_value.length() / 2);
}

// static
bool IbanSaveManager::IsIbanUploadEnabled(
    const syncer::SyncService* sync_service,
    AutofillMetrics::PaymentsSigninState signin_state_for_metrics) {
  // If Chrome sync is not active, upload IBAN save is not offered, since the
  // user would not be able to see the IBANs until sync is active again.
  if (!sync_service) {
    autofill_metrics::LogIbanUploadEnabledMetric(
        autofill_metrics::IbanUploadEnabledStatus::kSyncServiceNull,
        signin_state_for_metrics);
    return false;
  }

  // The sync service being paused implies a persistent auth error (for example,
  // the user is signed out). Uploading an IBAN should not be offered in this
  // case since the user is not authenticated, and they won't be able to see the
  // IBANs until sync is turned on.
  if (sync_service->GetTransportState() ==
      syncer::SyncService::TransportState::PAUSED) {
    autofill_metrics::LogIbanUploadEnabledMetric(
        autofill_metrics::IbanUploadEnabledStatus::kSyncServicePaused,
        signin_state_for_metrics);
    return false;
  }

  // When sync service is missing `AUTOFILL_WALLET_DATA` active data type,
  // upload IBAN save is not offered, since the user won't be able to see the
  // IBANs in the settings page.
  if (!sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL_WALLET_DATA)) {
    autofill_metrics::LogIbanUploadEnabledMetric(
        autofill_metrics::IbanUploadEnabledStatus::
            kSyncServiceMissingAutofillWalletDataActiveType,
        signin_state_for_metrics);
    return false;
  }

  // Also don't offer upload for users that have an explicit sync passphrase.
  // Users who have enabled a passphrase have chosen to not make their sync
  // information accessible to Google. Since upload makes IBAN data available
  // to other Google systems, disable it for passphrase users.
  if (sync_service->GetUserSettings()->IsUsingExplicitPassphrase()) {
    autofill_metrics::LogIbanUploadEnabledMetric(
        autofill_metrics::IbanUploadEnabledStatus::kUsingExplicitSyncPassphrase,
        signin_state_for_metrics);
    return false;
  }

  // Don't offer upload for users that are only syncing locally, since they
  // won't receive the IBANs back from Google Payments.
  if (sync_service->IsLocalSyncEnabled()) {
    autofill_metrics::LogIbanUploadEnabledMetric(
        autofill_metrics::IbanUploadEnabledStatus::kLocalSyncEnabled,
        signin_state_for_metrics);
    return false;
  }

  autofill_metrics::LogIbanUploadEnabledMetric(
      autofill_metrics::IbanUploadEnabledStatus::kEnabled,
      signin_state_for_metrics);
  return true;
}

bool IbanSaveManager::AttemptToOfferSave(Iban& import_candidate) {
#if !BUILDFLAG(IS_IOS)
  UpdateRecordType(import_candidate);
  switch (DetermineHowToSaveIban(import_candidate)) {
    case TypeOfOfferToSave::kDoNotOfferToSave:
      return false;
    case TypeOfOfferToSave::kOfferServerSave:
      return AttemptToOfferUploadSave(import_candidate);
    case TypeOfOfferToSave::kOfferLocalSave:
      return AttemptToOfferLocalSave(import_candidate);
  }
#else
  // IBAN save prompts do not currently exist on iOS.
  return false;
#endif
}

void IbanSaveManager::UpdateRecordType(Iban& import_candidate) {
  if (MatchesExistingServerIban(import_candidate)) {
    import_candidate.set_record_type(Iban::RecordType::kServerIban);
    return;
  }
  if (MatchesExistingLocalIban(import_candidate)) {
    import_candidate.set_record_type(Iban::RecordType::kLocalIban);
    return;
  }
  import_candidate.set_record_type(Iban::RecordType::kUnknown);
}

IbanSaveManager::TypeOfOfferToSave IbanSaveManager::DetermineHowToSaveIban(
    const Iban& import_candidate) const {
  // Server IBANs are ideal and should not offer to resave to the server or
  // locally.
  if (import_candidate.record_type() == Iban::kServerIban) {
    return TypeOfOfferToSave::kDoNotOfferToSave;
  }

  // Trigger server save if available, otherwise local save as long as the IBAN
  // isn't already saved locally.
  if (IsIbanUploadEnabled(
          client_->GetSyncService(),
          payments_data_manager().GetPaymentsSigninStateForMetrics()) &&
      payments_data_manager().GetServerIbans().size() <= kMaxNumServerIbans) {
    autofill_metrics::LogIbanSaveOfferedCountry(
        import_candidate.GetCountryCode());
    return TypeOfOfferToSave::kOfferServerSave;
  }
  if (import_candidate.record_type() != Iban::kLocalIban) {
    autofill_metrics::LogIbanSaveOfferedCountry(
        import_candidate.GetCountryCode());
    return TypeOfOfferToSave::kOfferLocalSave;
  }
  return TypeOfOfferToSave::kDoNotOfferToSave;
}

bool IbanSaveManager::MatchesExistingLocalIban(
    const Iban& import_candidate) const {
  return std::ranges::any_of(payments_data_manager().GetLocalIbans(),
                             [&](const Iban* iban) {
                               return iban->value() == import_candidate.value();
                             });
}

bool IbanSaveManager::MatchesExistingServerIban(
    const Iban& import_candidate) const {
  return std::ranges::any_of(
      payments_data_manager().GetServerIbans(),
      [&import_candidate](const auto& iban) {
        return iban->MatchesPrefixAndSuffix(import_candidate);
      });
}

bool IbanSaveManager::AttemptToOfferLocalSave(Iban& import_candidate) {
  if (observer_for_testing_) {
    observer_for_testing_->OnOfferLocalSave();
  }

  bool show_save_prompt = !GetIbanSaveStrikeDatabase()->ShouldBlockFeature(
      GetPartialIbanHashString(base::UTF16ToUTF8(import_candidate.value())));
  if (!show_save_prompt) {
    autofill_metrics::LogIbanSaveNotOfferedDueToMaxStrikesMetric(
        AutofillMetrics::SaveTypeMetric::LOCAL);
  }

  client_->GetPaymentsAutofillClient()->ConfirmSaveIbanLocally(
      import_candidate, show_save_prompt,
      base::BindOnce(&IbanSaveManager::OnUserDidDecideOnLocalSave,
                     weak_ptr_factory_.GetWeakPtr(), import_candidate));

  return show_save_prompt;
}

bool IbanSaveManager::AttemptToOfferUploadSave(Iban& import_candidate) {
  autofill_metrics::LogUploadIbanMetric(
      import_candidate.record_type() == Iban::kLocalIban
          ? autofill_metrics::UploadIbanOriginMetric::kLocalIban
          : autofill_metrics::UploadIbanOriginMetric::kNewIban,
      autofill_metrics::UploadIbanActionMetric::kOffered);
  bool show_save_prompt = !GetIbanSaveStrikeDatabase()->ShouldBlockFeature(
      GetPartialIbanHashString(base::UTF16ToUTF8(import_candidate.value())));
  client_->GetPaymentsAutofillClient()
      ->GetPaymentsNetworkInterface()
      ->GetIbanUploadDetails(
          payments_data_manager().app_locale(),
          payments::GetBillingCustomerId(payments_data_manager()),
          import_candidate.GetCountryCode(),
          base::BindOnce(&IbanSaveManager::OnDidGetUploadDetails,
                         weak_ptr_factory_.GetWeakPtr(), show_save_prompt,
                         import_candidate));
  return show_save_prompt;
}

IbanSaveStrikeDatabase* IbanSaveManager::GetIbanSaveStrikeDatabase() {
  if (iban_save_strike_database_.get() == nullptr) {
    iban_save_strike_database_ = std::make_unique<IbanSaveStrikeDatabase>(
        IbanSaveStrikeDatabase(client_->GetStrikeDatabase()));
  }
  return iban_save_strike_database_.get();
}

void IbanSaveManager::OnUserDidDecideOnLocalSave(
    Iban import_candidate,
    payments::PaymentsAutofillClient::SaveIbanOfferUserDecision user_decision,
    std::u16string_view nickname) {
  if (!nickname.empty()) {
    std::u16string trimmed_nickname;
    base::TrimWhitespace(nickname, base::TRIM_ALL, &trimmed_nickname);
    import_candidate.set_nickname(trimmed_nickname);
  }

  const std::string& partial_iban_hash =
      GetPartialIbanHashString(base::UTF16ToUTF8(import_candidate.value()));
  switch (user_decision) {
    case payments::PaymentsAutofillClient::SaveIbanOfferUserDecision::kAccepted:
      autofill_metrics::LogStrikesPresentWhenIbanSaved(
          iban_save_strike_database_->GetStrikes(partial_iban_hash),
          /*is_upload_save=*/false);
      autofill_metrics::LogIbanSaveAcceptedCountry(
          import_candidate.GetCountryCode());
      // Clear all IbanSave strikes for this IBAN, so that if it's later removed
      // the strike count starts over with respect to re-saving it.
      GetIbanSaveStrikeDatabase()->ClearStrikes(partial_iban_hash);
      payments_data_manager().OnAcceptedLocalIbanSave(
          std::move(import_candidate));
      if (observer_for_testing_) {
        observer_for_testing_->OnAcceptSaveIbanComplete();
      }
      break;
    case payments::PaymentsAutofillClient::SaveIbanOfferUserDecision::kIgnored:
    case payments::PaymentsAutofillClient::SaveIbanOfferUserDecision::kDeclined:
      GetIbanSaveStrikeDatabase()->AddStrike(partial_iban_hash);
      if (observer_for_testing_) {
        observer_for_testing_->OnDeclineSaveIbanComplete();
      }
      break;
  }
}

void IbanSaveManager::OnUserDidDecideOnUploadSave(
    Iban import_candidate,
    bool show_save_prompt,
    payments::PaymentsAutofillClient::SaveIbanOfferUserDecision user_decision,
    std::u16string_view nickname) {
  CHECK_NE(import_candidate.record_type(), Iban::kServerIban);
  if (!nickname.empty()) {
    std::u16string trimmed_nickname;
    base::TrimWhitespace(nickname, base::TRIM_ALL, &trimmed_nickname);
    if (!trimmed_nickname.empty()) {
      import_candidate.set_nickname(trimmed_nickname);
    }
  }

  autofill_metrics::UploadIbanActionMetric action_metric;
  switch (user_decision) {
    case payments::PaymentsAutofillClient::SaveIbanOfferUserDecision::kAccepted:
      action_metric = autofill_metrics::UploadIbanActionMetric::kAccepted;
      autofill_metrics::LogIbanSaveAcceptedCountry(
          import_candidate.GetCountryCode());
      user_did_accept_upload_prompt_ = true;
      if (!upload_request_details_.risk_data.empty()) {
        // Risk data has already been gathered, so the server request can be
        // sent.
        SendUploadRequest(import_candidate, show_save_prompt);
      }
      break;
    case payments::PaymentsAutofillClient::SaveIbanOfferUserDecision::kIgnored:
      action_metric = autofill_metrics::UploadIbanActionMetric::kIgnored;
      GetIbanSaveStrikeDatabase()->AddStrike(GetPartialIbanHashString(
          base::UTF16ToUTF8(import_candidate.value())));
      break;
    case payments::PaymentsAutofillClient::SaveIbanOfferUserDecision::kDeclined:
      action_metric = autofill_metrics::UploadIbanActionMetric::kDeclined;
      GetIbanSaveStrikeDatabase()->AddStrike(GetPartialIbanHashString(
          base::UTF16ToUTF8(import_candidate.value())));
      if (observer_for_testing_) {
        observer_for_testing_->OnDeclineSaveIbanComplete();
      }
      break;
  }
  autofill_metrics::LogUploadIbanMetric(
      import_candidate.record_type() == Iban::kLocalIban
          ? autofill_metrics::UploadIbanOriginMetric::kLocalIban
          : autofill_metrics::UploadIbanOriginMetric::kNewIban,
      action_metric);
}

void IbanSaveManager::OnDidGetUploadDetails(
    bool show_save_prompt,
    Iban import_candidate,
    PaymentsRpcResult result,
    const std::u16string& validation_regex,
    const std::u16string& context_token,
    std::unique_ptr<base::Value::Dict> legal_message) {
  if (observer_for_testing_) {
    observer_for_testing_->OnReceivedGetUploadDetailsResponse();
  }

  // Upload should only be offered when result is `kSuccess` and the IBAN passes
  // regex validation.
  if (result == PaymentsRpcResult::kSuccess &&
      MatchesRegex(import_candidate.value(), *CompileRegex(validation_regex))) {
    // Upload should only be offered when legal messages are parsed
    // successfully.
    LegalMessageLines parsed_legal_message_lines;
    if (LegalMessageLine::Parse(*legal_message, &parsed_legal_message_lines,
                                /*escape_apostrophes=*/true)) {
      // Reset `risk_data` and `user_did_accept_upload_prompt_` so that the risk
      // data and prompt acceptance state from a previous upload IBAN flow are
      // not re-used, which could potentially result in saves without the user's
      // consent.
      upload_request_details_.risk_data.clear();
      upload_request_details_.app_locale.clear();
      upload_request_details_.context_token.clear();
      upload_request_details_.value.clear();
      upload_request_details_.nickname.clear();
      user_did_accept_upload_prompt_ = false;
      context_token_ = context_token;
      client_->GetPaymentsAutofillClient()->ConfirmUploadIbanToCloud(
          import_candidate, std::move(parsed_legal_message_lines),
          show_save_prompt,
          base::BindOnce(&IbanSaveManager::OnUserDidDecideOnUploadSave,
                         weak_ptr_factory_.GetWeakPtr(), import_candidate,
                         show_save_prompt));
      client_->GetPaymentsAutofillClient()->LoadRiskData(base::BindOnce(
          &IbanSaveManager::OnDidGetUploadRiskData,
          weak_ptr_factory_.GetWeakPtr(), show_save_prompt, import_candidate));
      // If `show_save_prompt`'s value is false, desktop builds will still offer
      // save in the omnibox without popping-up the bubble.
      if (observer_for_testing_) {
        observer_for_testing_->OnOfferUploadSave();
      }
      return;
    }
  }

  // If the upload details request failed, attempt to offer local save.
  if (!MatchesExistingLocalIban(import_candidate)) {
    AttemptToOfferLocalSave(import_candidate);
  }
}

void IbanSaveManager::OnDidGetUploadRiskData(bool show_save_prompt,
                                             const Iban& import_candidate,
                                             const std::string& risk_data) {
  upload_request_details_.risk_data = risk_data;
  // Populating risk data and offering upload occur asynchronously.
  // If the dialog has already been accepted, send the upload IBAN request.
  // Otherwise, continue to wait for the user to accept the save dialog.
  if (user_did_accept_upload_prompt_) {
    SendUploadRequest(import_candidate, show_save_prompt);
  }
}

void IbanSaveManager::SendUploadRequest(const Iban& import_candidate,
                                        bool show_save_prompt) {
  if (observer_for_testing_) {
    observer_for_testing_->OnSentUploadRequest();
  }
  upload_request_details_.app_locale = payments_data_manager().app_locale();
  upload_request_details_.billing_customer_number =
      payments::GetBillingCustomerId(payments_data_manager());
  upload_request_details_.context_token = context_token_;
  upload_request_details_.value = import_candidate.value();
  upload_request_details_.nickname = import_candidate.nickname();
  client_->GetPaymentsAutofillClient()
      ->GetPaymentsNetworkInterface()
      ->UploadIban(upload_request_details_,
                   base::BindOnce(&IbanSaveManager::OnDidUploadIban,
                                  weak_ptr_factory_.GetWeakPtr(),
                                  import_candidate, show_save_prompt));
}

void IbanSaveManager::OnDidUploadIban(const Iban& import_candidate,
                                      bool show_save_prompt,
                                      PaymentsRpcResult result) {
  const std::string& partial_iban_hash =
      GetPartialIbanHashString(base::UTF16ToUTF8(import_candidate.value()));
  if (result == PaymentsRpcResult::kSuccess) {
    // Clear all IbanSave strikes for this IBAN, so that if it's later removed
    // the strike count starts over with respect to re-saving it.
    autofill_metrics::LogStrikesPresentWhenIbanSaved(
        iban_save_strike_database_->GetStrikes(partial_iban_hash),
        /*is_upload_save=*/true);
    GetIbanSaveStrikeDatabase()->ClearStrikes(partial_iban_hash);
  } else {
    // If upload save failed, check if the IBAN already exists locally. If not,
    // automatically save the IBAN locally so that the IBAN is not left unsaved
    // since the user intended to save it.
    bool should_local_save = !MatchesExistingLocalIban(import_candidate);
    if (should_local_save) {
      payments_data_manager().AddAsLocalIban(import_candidate);
    }
    autofill_metrics::LogIbanUploadSaveFailed(should_local_save);

    // If the upload failed and the bubble was actually shown (NOT just the
    // icon), count that as a strike against offering upload in the future.
    if (show_save_prompt) {
      GetIbanSaveStrikeDatabase()->AddStrike(partial_iban_hash);
    }
  }

  // Display the IBAN upload save confirmation dialog based on the result.
  client_->GetPaymentsAutofillClient()->IbanUploadCompleted(
      result == PaymentsRpcResult::kSuccess,
      GetIbanSaveStrikeDatabase()->ShouldBlockFeature(partial_iban_hash));
  if (observer_for_testing_) {
    if (result == PaymentsRpcResult::kSuccess) {
      observer_for_testing_->OnAcceptUploadSaveIbanComplete();
    } else {
      observer_for_testing_->OnAcceptUploadSaveIbanFailed();
    }
  }
}

PaymentsDataManager& IbanSaveManager::payments_data_manager() {
  return const_cast<PaymentsDataManager&>(
      const_cast<const IbanSaveManager*>(this)->payments_data_manager());
}

const PaymentsDataManager& IbanSaveManager::payments_data_manager() const {
  return client_->GetPersonalDataManager().payments_data_manager();
}

}  // namespace autofill