File: autofill_metrics_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (260 lines) | stat: -rw-r--r-- 9,960 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
// 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/metrics/autofill_metrics_utils.h"

#include "base/check.h"
#include "base/strings/string_number_conversions.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/field_type_utils.h"
#include "components/autofill/core/browser/form_structure.h"

namespace autofill::autofill_metrics {

namespace internal {

constexpr DenseSet<FormType> kAddressFormTypes = {FormType::kAddressForm};
constexpr DenseSet<FormType> kCreditCardFormTypes = {
    FormType::kCreditCardForm, FormType::kStandaloneCvcForm};
constexpr DenseSet<FormType> kLoyaltyCardFormTypes = {
    FormType::kLoyaltyCardForm};
constexpr DenseSet<FieldType> kFieldTypesOfATypicalStoreLocatorForm = {
    ADDRESS_HOME_CITY, ADDRESS_HOME_STATE, ADDRESS_HOME_ZIP};

bool IsCvcOnlyForm(const FormStructure& form) {
  if (form.fields().size() != 1) {
    return false;
  }
  // Theoretically we don't need to check
  // CREDIT_CARD_STANDALONE_VERIFICATION_CODE type here since if it's
  // CREDIT_CARD_STANDALONE_VERIFICATION_CODE, the form type would be treated as
  // kStandaloneCvcForm already. Add CREDIT_CARD_STANDALONE_VERIFICATION_CODE
  // here just for completion.
  static constexpr FieldTypeSet kCvcTypes = {
      CREDIT_CARD_VERIFICATION_CODE, CREDIT_CARD_STANDALONE_VERIFICATION_CODE};
  return kCvcTypes.contains(form.fields()[0]->Type().GetStorableType());
}

bool IsEmailOnlyForm(const FormStructure& form) {
  bool has_email_field = false;
  for (const auto& field : form.fields()) {
    FieldType field_type = field->Type().GetStorableType();
    if (field_type == EMAIL_ADDRESS) {
      has_email_field = true;
    }
    if (field_type != EMAIL_ADDRESS && field_type != UNKNOWN_TYPE &&
        FieldTypeGroupToFormType(field->Type().group()) !=
            FormType::kPasswordForm) {
      return false;
    }
  }
  return has_email_field;
}

bool IsPostalAddressForm(const FormStructure& form) {
  DenseSet<FieldType> postal_address_field_types;
  for (const auto& field : form.fields()) {
    if (field->Type().group() == FieldTypeGroup::kAddress &&
        field->Type().GetStorableType() != ADDRESS_HOME_COUNTRY) {
      postal_address_field_types.insert(field->Type().GetStorableType());
    }
  }
  return postal_address_field_types.size() >= 3 &&
         postal_address_field_types != kFieldTypesOfATypicalStoreLocatorForm;
}

// Returns `form`'s types for logging. If `filter_by` is not empty, only those
// `FormTypeNameForLogging` entries are returned that correspond to the form
// types in `filter_by`.
DenseSet<FormTypeNameForLogging> GetFormTypesForLogging(
    const FormStructure& form,
    std::optional<DenseSet<FormType>> filter_by = std::nullopt) {
  DenseSet<FormTypeNameForLogging> form_types;
  for (FormType form_type : form.GetFormTypes()) {
    if (filter_by && !(*filter_by).contains(form_type)) {
      continue;
    }
    switch (form_type) {
      case FormType::kAddressForm:
        form_types.insert(FormTypeNameForLogging::kAddressForm);
        if (IsEmailOnlyForm(form)) {
          form_types.insert(FormTypeNameForLogging::kEmailOnlyForm);
        } else if (IsPostalAddressForm(form)) {
          form_types.insert(FormTypeNameForLogging::kPostalAddressForm);
        }
        break;
      case FormType::kCreditCardForm:
        form_types.insert(IsCvcOnlyForm(form)
                              ? FormTypeNameForLogging::kStandaloneCvcForm
                              : FormTypeNameForLogging::kCreditCardForm);
        break;
      case FormType::kStandaloneCvcForm:
        form_types.insert(FormTypeNameForLogging::kStandaloneCvcForm);
        break;
      case FormType::kLoyaltyCardForm:
        form_types.insert(FormTypeNameForLogging::kLoyaltyCardForm);
        break;
      case FormType::kPasswordForm:
      case FormType::kUnknownFormType:
        break;
    }
  }
  return form_types;
}

}  // namespace internal

AutofillProfileRecordTypeCategory GetCategoryOfProfile(
    const AutofillProfile& profile) {
  switch (profile.record_type()) {
    case AutofillProfile::RecordType::kLocalOrSyncable:
      return AutofillProfileRecordTypeCategory::kLocalOrSyncable;
    case AutofillProfile::RecordType::kAccount:
      return profile.initial_creator_id() ==
                     AutofillProfile::kInitialCreatorOrModifierChrome
                 ? AutofillProfileRecordTypeCategory::kAccountChrome
                 : AutofillProfileRecordTypeCategory::kAccountNonChrome;
    case AutofillProfile::RecordType::kAccountHome:
      return AutofillProfileRecordTypeCategory::kAccountHome;
    case AutofillProfile::RecordType::kAccountWork:
      return AutofillProfileRecordTypeCategory::kAccountWork;
  }
}

const char* GetProfileCategorySuffix(
    AutofillProfileRecordTypeCategory category) {
  switch (category) {
    case AutofillProfileRecordTypeCategory::kLocalOrSyncable:
      return "Legacy";
    case AutofillProfileRecordTypeCategory::kAccountChrome:
      return "AccountChrome";
    case AutofillProfileRecordTypeCategory::kAccountNonChrome:
      return "AccountNonChrome";
    case AutofillProfileRecordTypeCategory::kAccountHome:
      return "AccountHome";
    case AutofillProfileRecordTypeCategory::kAccountWork:
      return "AccountWork";
  }
}

SettingsVisibleFieldTypeForMetrics ConvertSettingsVisibleFieldTypeForMetrics(
    FieldType field_type) {
  switch (field_type) {
    case NAME_FULL:
      return SettingsVisibleFieldTypeForMetrics::kName;

    case EMAIL_ADDRESS:
      return SettingsVisibleFieldTypeForMetrics::kEmailAddress;

    case PHONE_HOME_WHOLE_NUMBER:
      return SettingsVisibleFieldTypeForMetrics::kPhoneNumber;

    case ADDRESS_HOME_CITY:
      return SettingsVisibleFieldTypeForMetrics::kCity;

    case ADDRESS_HOME_COUNTRY:
      return SettingsVisibleFieldTypeForMetrics::kCountry;

    case ADDRESS_HOME_ZIP:
      return SettingsVisibleFieldTypeForMetrics::kZip;

    case ADDRESS_HOME_STATE:
      return SettingsVisibleFieldTypeForMetrics::kState;

    case ADDRESS_HOME_STREET_ADDRESS:
      return SettingsVisibleFieldTypeForMetrics::kStreetAddress;

    case ADDRESS_HOME_DEPENDENT_LOCALITY:
      return SettingsVisibleFieldTypeForMetrics::kDependentLocality;

    case COMPANY_NAME:
      return SettingsVisibleFieldTypeForMetrics::kCompany;

    case ADDRESS_HOME_ADMIN_LEVEL2:
      return SettingsVisibleFieldTypeForMetrics::kAdminLevel2;

    case ALTERNATIVE_FULL_NAME:
      return SettingsVisibleFieldTypeForMetrics::kAlternativeName;

    default:
      return SettingsVisibleFieldTypeForMetrics::kUndefined;
  }
}

DenseSet<FormTypeNameForLogging> GetFormTypesForLogging(
    const FormStructure& form) {
  return internal::GetFormTypesForLogging(form);
}

DenseSet<FormTypeNameForLogging> GetAddressFormTypesForLogging(
    const FormStructure& form) {
  return internal::GetFormTypesForLogging(form, internal::kAddressFormTypes);
}

DenseSet<FormTypeNameForLogging> GetLoyaltyFormTypesForLogging(
    const FormStructure& form) {
  return internal::GetFormTypesForLogging(form,
                                          internal::kLoyaltyCardFormTypes);
}

DenseSet<FormTypeNameForLogging> GetCreditCardFormTypesForLogging(
    const FormStructure& form) {
  return internal::GetFormTypesForLogging(form, internal::kCreditCardFormTypes);
}

bool ShouldLogAutofillSuggestionShown(
    AutofillSuggestionTriggerSource trigger_source) {
  switch (trigger_source) {
    case AutofillSuggestionTriggerSource::kUnspecified:
    case AutofillSuggestionTriggerSource::kFormControlElementClicked:
    case AutofillSuggestionTriggerSource::kTextareaFocusedWithoutClick:
    case AutofillSuggestionTriggerSource::kContentEditableClicked:
    case AutofillSuggestionTriggerSource::kTextFieldDidReceiveKeyDown:
    case AutofillSuggestionTriggerSource::kOpenTextDataListChooser:
    case AutofillSuggestionTriggerSource::kComposeDialogLostFocus:
    case AutofillSuggestionTriggerSource::kPasswordManager:
    case AutofillSuggestionTriggerSource::kiOS:
    case AutofillSuggestionTriggerSource::
        kShowPromptAfterDialogClosedNonManualFallback:
    case AutofillSuggestionTriggerSource::kPasswordManagerProcessedFocusedField:
    case AutofillSuggestionTriggerSource::kManualFallbackPasswords:
    case AutofillSuggestionTriggerSource::kManualFallbackPlusAddresses:
      return true;
    case AutofillSuggestionTriggerSource::kTextFieldValueChanged:
    case AutofillSuggestionTriggerSource::kComposeDelayedProactiveNudge:
    case AutofillSuggestionTriggerSource::kAutofillAi:
    case AutofillSuggestionTriggerSource::kPlusAddressUpdatedInBrowserProcess:
      return false;
  }
}

int GetBucketForAcceptanceMetricsGroupedByFieldType(FieldType field_type,
                                                    bool suggestion_accepted) {
  static_assert(FieldType::MAX_VALID_FIELD_TYPE <= (UINT16_MAX >> 4),
                "Autofill::FieldType value needs more than 12 bits.");

  return (field_type << 2) | suggestion_accepted;
}

int GetDuplicationRank(
    base::span<const DifferingProfileWithTypeSet> min_incompatible_sets) {
  // All elements of `min_incompatible_sets` have the same size.
  return min_incompatible_sets.empty()
             ? std::numeric_limits<int>::max()
             : min_incompatible_sets.back().field_type_set.size();
}

uint64_t FormGlobalIdToHash64Bit(const FormGlobalId& form_global_id) {
  return StrToHash64Bit(
      base::NumberToString(form_global_id.renderer_id.value()) +
      form_global_id.frame_token.ToString());
}

uint64_t FieldGlobalIdToHash64Bit(const FieldGlobalId& field_global_id) {
  return StrToHash64Bit(
      base::NumberToString(field_global_id.renderer_id.value()) +
      field_global_id.frame_token.ToString());
}

}  // namespace autofill::autofill_metrics