File: payments_profile_comparator.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 (317 lines) | stat: -rw-r--r-- 12,256 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 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/payments/core/payments_profile_comparator.h"

#include <algorithm>
#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/data_quality/autofill_data_util.h"
#include "components/autofill/core/browser/data_quality/validation.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/geo/address_i18n.h"
#include "components/autofill/core/browser/geo/autofill_country.h"
#include "components/payments/core/payment_options_provider.h"
#include "components/payments/core/payment_request_data_util.h"
#include "components/strings/grit/components_strings.h"
#include "third_party/libaddressinput/chromium/addressinput_util.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "ui/base/l10n/l10n_util.h"

namespace payments {

PaymentsProfileComparator::PaymentsProfileComparator(
    const std::string& app_locale,
    const PaymentOptionsProvider& options)
    : autofill::AutofillProfileComparator(app_locale), options_(options) {}

PaymentsProfileComparator::~PaymentsProfileComparator() = default;

PaymentsProfileComparator::ProfileFields
PaymentsProfileComparator::GetMissingProfileFields(
    const autofill::AutofillProfile* profile) const {
  if (!profile)
    return kName | kPhone | kEmail | kAddress;

  if (!cache_.count(profile->guid())) {
    cache_[profile->guid()] = ComputeMissingFields(*profile);
  } else {
    // Cache hit. In debug mode, recompute and check that invalidation has
    // occurred where necessary.
    DCHECK_EQ(cache_[profile->guid()], ComputeMissingFields(*profile))
        << "Profiles must be invalidated when their contents change.";
  }

  return cache_[profile->guid()];
}

std::vector<raw_ptr<autofill::AutofillProfile, VectorExperimental>>
PaymentsProfileComparator::FilterProfilesForContact(
    const std::vector<raw_ptr<autofill::AutofillProfile, VectorExperimental>>&
        profiles) const {
  // We will be removing profiles, so we operate on a copy.
  std::vector<raw_ptr<autofill::AutofillProfile, VectorExperimental>>
      processed = profiles;

  // Stable sort, since profiles are expected to be passed in frecency order.
  std::stable_sort(
      processed.begin(), processed.end(),
      [this](autofill::AutofillProfile* p1, autofill::AutofillProfile* p2) {
        return GetContactCompletenessScore(p1) >
               GetContactCompletenessScore(p2);
      });

  auto it = processed.begin();
  while (it != processed.end()) {
    if (GetContactCompletenessScore(*it) == 0) {
      // Since profiles are sorted by completeness, this and any further
      // profiles can be discarded.
      processed.erase(it, processed.end());
      break;
    }

    // Attempt to find a matching element in the vector before the current.
    // This is quadratic, but the number of elements is generally small
    // (< 10), so a more complicated algorithm would be overkill.
    if (std::find_if(processed.begin(), it,
                     [&](autofill::AutofillProfile* prior) {
                       return IsContactEqualOrSuperset(*prior, **it);
                     }) != it) {
      // Remove the subset profile. |it| will point to the next element after
      // erasure.
      it = processed.erase(it);
    } else {
      it++;
    }
  }

  return processed;
}

bool PaymentsProfileComparator::IsContactEqualOrSuperset(
    const autofill::AutofillProfile& super,
    const autofill::AutofillProfile& sub) const {
  if (options_->request_payer_name()) {
    if (sub.HasInfo(autofill::NAME_FULL) &&
        !super.HasInfo(autofill::NAME_FULL)) {
      return false;
    }
    if (!HaveMergeableNames(super, sub))
      return false;
  }
  if (options_->request_payer_phone()) {
    if (sub.HasInfo(autofill::PHONE_HOME_WHOLE_NUMBER) &&
        !super.HasInfo(autofill::PHONE_HOME_WHOLE_NUMBER)) {
      return false;
    }
    if (!HaveMergeablePhoneNumbers(super, sub))
      return false;
  }
  if (options_->request_payer_email()) {
    if (sub.HasInfo(autofill::EMAIL_ADDRESS) &&
        !super.HasInfo(autofill::EMAIL_ADDRESS)) {
      return false;
    }
    if (!HaveMergeableEmailAddresses(super, sub))
      return false;
  }
  return true;
}

int PaymentsProfileComparator::GetContactCompletenessScore(
    const autofill::AutofillProfile* profile) const {
  // Create a bitmask of the fields that are both present and required.
  ProfileFields present =
      ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForContact();

  // Count how many are set.
  return !!(present & kName) + !!(present & kPhone) + !!(present & kEmail);
}

bool PaymentsProfileComparator::IsContactInfoComplete(
    const autofill::AutofillProfile* profile) const {
  // Mask the fields that are missing with those that are requried. If any bits
  // are set (i.e., the result is nonzero), then contact info is incomplete.
  return !(GetMissingProfileFields(profile) &
           GetRequiredProfileFieldsForContact());
}

std::vector<raw_ptr<autofill::AutofillProfile, VectorExperimental>>
PaymentsProfileComparator::FilterProfilesForShipping(
    const std::vector<raw_ptr<autofill::AutofillProfile, VectorExperimental>>&
        profiles) const {
  // Since we'll be changing the order/contents of the const input vector,
  // we make a copy.
  std::vector<raw_ptr<autofill::AutofillProfile, VectorExperimental>>
      processed = profiles;

  std::stable_sort(
      processed.begin(), processed.end(),
      [this](autofill::AutofillProfile* p1, autofill::AutofillProfile* p2) {
        return GetShippingCompletenessScore(p1) >
               GetShippingCompletenessScore(p2);
      });

  // TODO(crbug.com/40520855): Remove profiles with no relevant information, or
  // which are subsets of more-complete profiles.

  return processed;
}

int PaymentsProfileComparator::GetShippingCompletenessScore(
    const autofill::AutofillProfile* profile) const {
  // Create a bitmask of the fields that are both present and required.
  ProfileFields present =
      ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForShipping();

  // Count how many are set. The completeness of the address is weighted so as
  // to dominate the other fields.
  return !!(present & kName) + !!(present & kPhone) +
         (10 * !!(present & kAddress));
}

bool PaymentsProfileComparator::IsShippingComplete(
    const autofill::AutofillProfile* profile) const {
  // Mask the fields that are missing with those that are requried. If any bits
  // are set (i.e., the result is nonzero), then shipping is incomplete.
  return !(GetMissingProfileFields(profile) &
           GetRequiredProfileFieldsForShipping());
}

std::u16string PaymentsProfileComparator::GetStringForMissingContactFields(
    const autofill::AutofillProfile& profile) const {
  return GetStringForMissingFields(GetMissingProfileFields(&profile) &
                                   GetRequiredProfileFieldsForContact());
}

std::u16string PaymentsProfileComparator::GetTitleForMissingContactFields(
    const autofill::AutofillProfile& profile) const {
  return GetTitleForMissingFields(GetMissingProfileFields(&profile) &
                                  GetRequiredProfileFieldsForContact());
}

std::u16string PaymentsProfileComparator::GetStringForMissingShippingFields(
    const autofill::AutofillProfile& profile) const {
  return GetStringForMissingFields(GetMissingProfileFields(&profile) &
                                   GetRequiredProfileFieldsForShipping());
}

std::u16string PaymentsProfileComparator::GetTitleForMissingShippingFields(
    const autofill::AutofillProfile& profile) const {
  return GetTitleForMissingFields(GetMissingProfileFields(&profile) &
                                  GetRequiredProfileFieldsForShipping());
}

void PaymentsProfileComparator::Invalidate(
    const autofill::AutofillProfile& profile) {
  cache_.erase(profile.guid());
}

PaymentsProfileComparator::ProfileFields
PaymentsProfileComparator::ComputeMissingFields(
    const autofill::AutofillProfile& profile) const {
  ProfileFields missing = kNone;

  if (!profile.HasInfo(autofill::NAME_FULL))
    missing |= kName;

  // Determine the country code to use when validating the phone number. Use
  // the profile's country if it has one, or the code for the app locale
  // otherwise. Note that international format numbers will always work--this
  // is just the region that will be used to check if the number is
  // potentially in a local format.
  const std::string country =
      autofill::data_util::GetCountryCodeWithFallback(profile, app_locale());

  std::u16string phone =
      profile.GetInfo(autofill::PHONE_HOME_WHOLE_NUMBER, app_locale());
  std::u16string intl_phone = base::UTF8ToUTF16("+" + base::UTF16ToUTF8(phone));
  if (!(autofill::IsPossiblePhoneNumber(phone, country) ||
        autofill::IsPossiblePhoneNumber(intl_phone, country)))
    missing |= kPhone;

  std::u16string email = profile.GetInfo(autofill::EMAIL_ADDRESS, app_locale());
  if (!autofill::IsValidEmailAddress(email))
    missing |= kEmail;

  if (!AreRequiredAddressFieldsPresent(profile))
    missing |= kAddress;

  return missing;
}

PaymentsProfileComparator::ProfileFields
PaymentsProfileComparator::GetRequiredProfileFieldsForContact() const {
  ProfileFields required = kNone;
  if (options_->request_payer_name())
    required |= kName;
  if (options_->request_payer_phone())
    required |= kPhone;
  if (options_->request_payer_email())
    required |= kEmail;
  return required;
}

PaymentsProfileComparator::ProfileFields
PaymentsProfileComparator::GetRequiredProfileFieldsForShipping() const {
  return options_->request_shipping() ? (kAddress | kName | kPhone) : kNone;
}

std::u16string PaymentsProfileComparator::GetStringForMissingFields(
    PaymentsProfileComparator::ProfileFields fields) const {
  switch (fields) {
    case kNone:
      // No bits are set, so no fields are missing.
      return std::u16string();
    case kName:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_NAME_REQUIRED);
    case kPhone:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_PHONE_NUMBER_REQUIRED);
    case kEmail:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_EMAIL_REQUIRED);
    case kAddress:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_INVALID_ADDRESS);
    default:
      // Either multiple bits are set (likely) or one bit that doesn't
      // correspond to a named constant is set (shouldn't happen). Return a
      // generic "More information" message.
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_MORE_INFORMATION_REQUIRED);
  }
}

std::u16string PaymentsProfileComparator::GetTitleForMissingFields(
    PaymentsProfileComparator::ProfileFields fields) const {
  switch (fields) {
    case 0:
      NOTREACHED() << "Title should not be requested if no fields are missing";
    case kName:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_NAME);
    case kPhone:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_PHONE_NUMBER);
    case kEmail:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_EMAIL);
    case kAddress:
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_VALID_ADDRESS);
    default:
      // Either multiple bits are set (likely) or one bit that doesn't
      // correspond to a named constant is set (shouldn't happen). Return a
      // generic "More information" message.
      return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_MORE_INFORMATION);
  }
}

bool PaymentsProfileComparator::AreRequiredAddressFieldsPresent(
    const autofill::AutofillProfile& profile) const {
  std::unique_ptr<::i18n::addressinput::AddressData> data =
      autofill::i18n::CreateAddressDataFromAutofillProfile(profile,
                                                           app_locale());

  return autofill::addressinput::HasAllRequiredFields(*data);
}

}  // namespace payments