File: prediction_quality_metrics.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 (959 lines) | stat: -rw-r--r-- 36,355 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
// Copyright 2024 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/prediction_quality_metrics.h"

#include <string>
#include <string_view>
#include <vector>

#include "base/check_op.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/data_quality/validation.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_parsing/autofill_parsing_utils.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/metrics/form_interactions_ukm_logger.h"
#include "components/autofill/core/common/dense_set.h"
#include "components/autofill/core/common/label_source_util.h"

namespace autofill::autofill_metrics {

namespace {

constexpr std::string_view kFieldPredictionMetricPrefix =
    "Autofill.FieldPrediction.";
constexpr std::string_view kAggregateFieldPredictionMetricPrefix =
    "Autofill.FieldPredictionQuality.Aggregate.";
constexpr std::string_view kByFieldTypeFieldPredictionMetricPrefix =
    "Autofill.FieldPredictionQuality.ByFieldType.";
constexpr std::string_view kFieldPredictionOverlapPrefix =
    "Autofill.FieldPredictionOverlap.";

// Don't change the enum values because they are recorded in metrics.
enum FieldTypeGroupForMetrics {
  GROUP_AMBIGUOUS = 0,
  GROUP_NAME = 1,
  GROUP_COMPANY = 2,
  GROUP_ADDRESS_LINE_1 = 3,
  GROUP_ADDRESS_LINE_2 = 4,
  GROUP_ADDRESS_CITY = 5,
  GROUP_ADDRESS_STATE = 6,
  GROUP_ADDRESS_ZIP = 7,
  GROUP_ADDRESS_COUNTRY = 8,
  GROUP_ADDRESS_HOME_STREET_NAME = 9,
  GROUP_ADDRESS_HOME_HOUSE_NUMBER = 10,
  GROUP_ADDRESS_HOME_SUBPREMISE = 11,
  GROUP_PHONE = 12,
  GROUP_FAX = 13,  // Deprecated.
  GROUP_EMAIL = 14,
  GROUP_CREDIT_CARD_NAME = 15,
  GROUP_CREDIT_CARD_NUMBER = 16,
  GROUP_CREDIT_CARD_DATE = 17,
  GROUP_CREDIT_CARD_TYPE = 18,
  GROUP_PASSWORD = 19,
  GROUP_ADDRESS_LINE_3 = 20,
  GROUP_USERNAME = 21,
  GROUP_STREET_ADDRESS = 22,
  GROUP_CREDIT_CARD_VERIFICATION = 23,
  GROUP_UNFILLABLE = 24,
  GROUP_ADDRESS_HOME_APT_NUM = 25,
  GROUP_ADDRESS_HOME_SORTING_CODE = 26,
  GROUP_ADDRESS_HOME_DEPENDENT_LOCALITY = 27,
  GROUP_ADDRESS_HOME_OTHER_SUBUNIT = 28,
  GROUP_ADDRESS_HOME_ADDRESS = 29,
  GROUP_ADDRESS_HOME_ADDRESS_WITH_NAME = 30,
  GROUP_ADDRESS_HOME_FLOOR = 31,
  GROUP_UNKNOWN_TYPE = 32,
  GROUP_BIRTHDATE = 33,  // Deprecated
  GROUP_IBAN = 34,
  GROUP_ADDRESS_HOME_LANDMARK = 35,
  GROUP_ADDRESS_HOME_BETWEEN_STREETS = 36,
  GROUP_ADDRESS_HOME_ADMIN_LEVEL2 = 37,
  GROUP_ADDRESS_HOME_STREET_LOCATION = 38,
  GROUP_ADDRESS_HOME_OVERFLOW = 39,
  GROUP_DELIVERY_INSTRUCTIONS = 40,
  GROUP_ADDRESS_HOME_OVERFLOW_AND_LANDMARK = 41,
  GROUP_ADDRESS_HOME_BETWEEN_STREETS_OR_LANDMARK = 42,
  GROUP_ADDRESS_HOME_STREET_LOCATION_AND_LOCALITY = 43,
  GROUP_ADDRESS_HOME_STREET_LOCATION_AND_LANDMARK = 44,
  GROUP_ADDRESS_HOME_DEPENDENT_LOCALITY_AND_LANDMARK = 45,
  GROUP_ADDRESS_HOME_HOUSE_NUMBER_AND_APT = 46,
  GROUP_STANDALONE_CREDIT_CARD_VERIFICATION = 47,
  GROUP_AUTOFILL_AI = 48,
  GROUP_LOYALTY_CARD = 49,
  // Note: if adding an enum value here, run
  // tools/metrics/histograms/update_autofill_enums.py
  NUM_FIELD_TYPE_GROUPS_FOR_METRICS
};

// Given a set of `possible_types` for a field, select the best type to use as
// the "actual" field type when calculating metrics. If the `predicted_type` is
// among the `possible_types` then use that as the best type (i.e., the
// prediction is deemed to have been correct).
FieldType GetActualFieldType(const FieldTypeSet& possible_types,
                             FieldType predicted_type) {
  DCHECK_NE(possible_types.size(), 0u);

  if (possible_types.count(EMPTY_TYPE)) {
    DCHECK_EQ(possible_types.size(), 1u);
    return EMPTY_TYPE;
  }

  if (possible_types.count(UNKNOWN_TYPE)) {
    DCHECK_EQ(possible_types.size(), 1u);
    return UNKNOWN_TYPE;
  }

  if (possible_types.count(predicted_type)) {
    return predicted_type;
  }

  // Collapse field types that Chrome treats as identical, e.g. home and
  // billing address fields.
  FieldTypeSet collapsed_field_types;
  for (FieldType type : possible_types) {
    DCHECK_NE(type, EMPTY_TYPE);
    DCHECK_NE(type, UNKNOWN_TYPE);

    // A phone number that's only missing its country code is (for metrics
    // purposes) the same as the whole phone number.
    if (type == PHONE_HOME_CITY_AND_NUMBER) {
      collapsed_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
    } else {
      collapsed_field_types.insert(type);
    }
  }

  // Capture the field's type, if it is unambiguous.
  FieldType actual_type = AMBIGUOUS_TYPE;
  if (collapsed_field_types.size() == 1) {
    actual_type = *collapsed_field_types.begin();
  }

  return actual_type;
}

bool FilledTypeAgreesWithActual(const FieldTypeSet& possible_types,
                                FieldType predicted_type) {
  return predicted_type == GetActualFieldType(possible_types, predicted_type);
}

FieldPredictionOverlapSourcesSuperset GetFieldPredictionOverlapSample(
    bool server_agrees,
    bool heuristics_agree,
    bool autocomplete_agrees) {
  if (server_agrees && heuristics_agree && autocomplete_agrees) {
    return FieldPredictionOverlapSourcesSuperset::
        kServerHeuristicsAutocompleteCorrect;
  }
  if (server_agrees && heuristics_agree) {
    return FieldPredictionOverlapSourcesSuperset::kServerHeuristicsCorrect;
  }
  if (heuristics_agree && autocomplete_agrees) {
    return FieldPredictionOverlapSourcesSuperset::
        kHeuristicsAutocompleteCorrect;
  }
  if (server_agrees && autocomplete_agrees) {
    return FieldPredictionOverlapSourcesSuperset::kServerAutocompleteCorrect;
  }
  if (heuristics_agree) {
    return FieldPredictionOverlapSourcesSuperset::kHeuristicsCorrect;
  }
  if (server_agrees) {
    return FieldPredictionOverlapSourcesSuperset::kServerCorrect;
  }
  if (autocomplete_agrees) {
    return FieldPredictionOverlapSourcesSuperset::kAutocompleteCorrect;
  }
  return FieldPredictionOverlapSourcesSuperset::kNoneCorrect;
}

}  // namespace

// First, translates |field_type| to the corresponding logical |group| from
// |FieldTypeGroupForMetrics|.  Then, interpolates this with the given |metric|,
// which should be in the range [0, |num_possible_metrics|).
// Returns the interpolated index.
//
// The interpolation maps the pair (|group|, |metric|) to a single index, so
// that all the indices for a given group are adjacent.  In particular, with
// the groups {AMBIGUOUS, NAME, ...} combining with the metrics
// {UNKNOWN, MATCH, MISMATCH}, we create this set of mapped indices:
// {
//   AMBIGUOUS+UNKNOWN,
//   AMBIGUOUS+MATCH,
//   AMBIGUOUS+MISMATCH,
//   NAME+UNKNOWN,
//   NAME+MATCH,
//   NAME+MISMATCH,
//   ...
// }.
//
// Clients must ensure that |field_type| is one of the types Chrome supports
// natively, e.g. |field_type| must not be a billing address.
// NOTE: This is defined outside of the anonymous namespace so that it can be
// accessed from the unit test file. It is not exposed in the header file,
// however, because it is not intended for consumption outside of the metrics
// implementation.
int GetFieldTypeGroupPredictionQualityMetric(FieldType field_type,
                                             FieldTypeQualityMetric metric) {
  DCHECK_LT(metric, NUM_FIELD_TYPE_QUALITY_METRICS);

  FieldTypeGroupForMetrics group = GROUP_AMBIGUOUS;
  switch (GroupTypeOfFieldType(field_type)) {
    case FieldTypeGroup::kNoGroup:
      group = GROUP_AMBIGUOUS;
      break;

    case FieldTypeGroup::kName:
      group = GROUP_NAME;
      break;

    case FieldTypeGroup::kCompany:
      group = GROUP_COMPANY;
      break;

    case FieldTypeGroup::kIban:
      group = GROUP_IBAN;
      break;

    case FieldTypeGroup::kAutofillAi:
      group = GROUP_AUTOFILL_AI;
      break;

    case FieldTypeGroup::kLoyaltyCard:
      group = GROUP_LOYALTY_CARD;
      break;

    case FieldTypeGroup::kAddress:
      switch (field_type) {
        case ADDRESS_HOME_LINE1:
          group = GROUP_ADDRESS_LINE_1;
          break;
        case ADDRESS_HOME_LINE2:
          group = GROUP_ADDRESS_LINE_2;
          break;
        case ADDRESS_HOME_LINE3:
          group = GROUP_ADDRESS_LINE_3;
          break;
        case ADDRESS_HOME_APT:
        case ADDRESS_HOME_APT_NUM:
        case ADDRESS_HOME_APT_TYPE:
          group = GROUP_ADDRESS_HOME_APT_NUM;
          break;
        case ADDRESS_HOME_HOUSE_NUMBER_AND_APT:
          group = GROUP_ADDRESS_HOME_HOUSE_NUMBER_AND_APT;
          break;
        case ADDRESS_HOME_STREET_ADDRESS:
          group = GROUP_STREET_ADDRESS;
          break;
        case ADDRESS_HOME_CITY:
          group = GROUP_ADDRESS_CITY;
          break;
        case ADDRESS_HOME_STATE:
          group = GROUP_ADDRESS_STATE;
          break;
        case ADDRESS_HOME_ZIP:
          group = GROUP_ADDRESS_ZIP;
          break;
        case ADDRESS_HOME_COUNTRY:
          group = GROUP_ADDRESS_COUNTRY;
          break;
        case ADDRESS_HOME_STREET_NAME:
          group = GROUP_ADDRESS_HOME_STREET_NAME;
          break;
        case ADDRESS_HOME_SORTING_CODE:
          group = GROUP_ADDRESS_HOME_SORTING_CODE;
          break;
        case ADDRESS_HOME_DEPENDENT_LOCALITY:
          group = GROUP_ADDRESS_HOME_DEPENDENT_LOCALITY;
          break;
        case ADDRESS_HOME_HOUSE_NUMBER:
          group = GROUP_ADDRESS_HOME_HOUSE_NUMBER;
          break;
        case ADDRESS_HOME_SUBPREMISE:
          group = GROUP_ADDRESS_HOME_SUBPREMISE;
          break;
        case ADDRESS_HOME_OTHER_SUBUNIT:
          group = GROUP_ADDRESS_HOME_OTHER_SUBUNIT;
          break;
        case ADDRESS_HOME_ADDRESS:
          group = GROUP_ADDRESS_HOME_ADDRESS;
          break;
        case ADDRESS_HOME_ADDRESS_WITH_NAME:
          group = GROUP_ADDRESS_HOME_ADDRESS_WITH_NAME;
          break;
        case ADDRESS_HOME_FLOOR:
          group = GROUP_ADDRESS_HOME_FLOOR;
          break;
        case ADDRESS_HOME_LANDMARK:
          group = GROUP_ADDRESS_HOME_LANDMARK;
          break;
        case ADDRESS_HOME_BETWEEN_STREETS:
        case ADDRESS_HOME_BETWEEN_STREETS_1:
        case ADDRESS_HOME_BETWEEN_STREETS_2:
          group = GROUP_ADDRESS_HOME_BETWEEN_STREETS;
          break;
        case ADDRESS_HOME_ADMIN_LEVEL2:
          group = GROUP_ADDRESS_HOME_ADMIN_LEVEL2;
          break;
        case ADDRESS_HOME_OVERFLOW:
          group = GROUP_ADDRESS_HOME_OVERFLOW;
          break;
        case ADDRESS_HOME_OVERFLOW_AND_LANDMARK:
          group = GROUP_ADDRESS_HOME_OVERFLOW_AND_LANDMARK;
          break;
        case ADDRESS_HOME_BETWEEN_STREETS_OR_LANDMARK:
          group = GROUP_ADDRESS_HOME_BETWEEN_STREETS_OR_LANDMARK;
          break;
        case ADDRESS_HOME_STREET_LOCATION:
          group = GROUP_ADDRESS_HOME_STREET_LOCATION;
          break;
        case ADDRESS_HOME_STREET_LOCATION_AND_LOCALITY:
          group = GROUP_ADDRESS_HOME_STREET_LOCATION_AND_LOCALITY;
          break;
        case ADDRESS_HOME_STREET_LOCATION_AND_LANDMARK:
          group = GROUP_ADDRESS_HOME_STREET_LOCATION_AND_LANDMARK;
          break;
        case ADDRESS_HOME_DEPENDENT_LOCALITY_AND_LANDMARK:
          group = GROUP_ADDRESS_HOME_DEPENDENT_LOCALITY_AND_LANDMARK;
          break;
        case DELIVERY_INSTRUCTIONS:
          group = GROUP_DELIVERY_INSTRUCTIONS;
          break;
        case UNKNOWN_TYPE:
          group = GROUP_UNKNOWN_TYPE;
          break;
        case NO_SERVER_DATA:
        case EMPTY_TYPE:
        case NAME_FIRST:
        case NAME_MIDDLE:
        case NAME_LAST:
        case NAME_MIDDLE_INITIAL:
        case NAME_FULL:
        case NAME_SUFFIX:
        case ALTERNATIVE_FULL_NAME:
        case ALTERNATIVE_GIVEN_NAME:
        case ALTERNATIVE_FAMILY_NAME:
        case EMAIL_ADDRESS:
        case PHONE_HOME_NUMBER:
        case PHONE_HOME_NUMBER_PREFIX:
        case PHONE_HOME_NUMBER_SUFFIX:
        case PHONE_HOME_CITY_CODE:
        case PHONE_HOME_CITY_CODE_WITH_TRUNK_PREFIX:
        case PHONE_HOME_COUNTRY_CODE:
        case PHONE_HOME_CITY_AND_NUMBER:
        case PHONE_HOME_CITY_AND_NUMBER_WITHOUT_TRUNK_PREFIX:
        case PHONE_HOME_WHOLE_NUMBER:
        case CREDIT_CARD_NAME_FULL:
        case CREDIT_CARD_NUMBER:
        case CREDIT_CARD_EXP_MONTH:
        case CREDIT_CARD_EXP_2_DIGIT_YEAR:
        case CREDIT_CARD_EXP_4_DIGIT_YEAR:
        case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR:
        case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR:
        case CREDIT_CARD_TYPE:
        case CREDIT_CARD_VERIFICATION_CODE:
        case COMPANY_NAME:
        case FIELD_WITH_DEFAULT_VALUE:
        case MERCHANT_EMAIL_SIGNUP:
        case MERCHANT_PROMO_CODE:
        case PASSWORD:
        case ACCOUNT_CREATION_PASSWORD:
        case NOT_ACCOUNT_CREATION_PASSWORD:
        case USERNAME:
        case USERNAME_AND_EMAIL_ADDRESS:
        case NEW_PASSWORD:
        case PROBABLY_NEW_PASSWORD:
        case NOT_NEW_PASSWORD:
        case CREDIT_CARD_NAME_FIRST:
        case CREDIT_CARD_NAME_LAST:
        case PHONE_HOME_EXTENSION:
        case CONFIRMATION_PASSWORD:
        case AMBIGUOUS_TYPE:
        case SEARCH_TERM:
        case PRICE:
        case NUMERIC_QUANTITY:
        case NOT_PASSWORD:
        case SINGLE_USERNAME:
        case NOT_USERNAME:
        case ONE_TIME_CODE:
        case NAME_LAST_PREFIX:
        case NAME_LAST_CORE:
        case NAME_LAST_FIRST:
        case NAME_LAST_CONJUNCTION:
        case NAME_LAST_SECOND:
        case NAME_HONORIFIC_PREFIX:
        case IBAN_VALUE:
        case MAX_VALID_FIELD_TYPE:
        case CREDIT_CARD_STANDALONE_VERIFICATION_CODE:
        case SINGLE_USERNAME_FORGOT_PASSWORD:
        case SINGLE_USERNAME_WITH_INTERMEDIATE_VALUES:
        case IMPROVED_PREDICTION:
        case PASSPORT_NAME_TAG:
        case PASSPORT_NUMBER:
        case PASSPORT_ISSUING_COUNTRY:
        case PASSPORT_EXPIRATION_DATE:
        case PASSPORT_ISSUE_DATE:
        case LOYALTY_MEMBERSHIP_PROGRAM:
        case LOYALTY_MEMBERSHIP_PROVIDER:
        case LOYALTY_MEMBERSHIP_ID:
        case VEHICLE_OWNER_TAG:
        case VEHICLE_LICENSE_PLATE:
        case VEHICLE_VIN:
        case VEHICLE_MAKE:
        case VEHICLE_MODEL:
        case VEHICLE_YEAR:
        case VEHICLE_PLATE_STATE:
        case DRIVERS_LICENSE_NAME_TAG:
        case DRIVERS_LICENSE_REGION:
        case DRIVERS_LICENSE_NUMBER:
        case DRIVERS_LICENSE_EXPIRATION_DATE:
        case DRIVERS_LICENSE_ISSUE_DATE:
        case EMAIL_OR_LOYALTY_MEMBERSHIP_ID:
          NOTREACHED() << field_type << " type is not in that group.";
      }
      break;

    case FieldTypeGroup::kEmail:
      group = GROUP_EMAIL;
      break;

    case FieldTypeGroup::kPhone:
      group = GROUP_PHONE;
      break;

    case FieldTypeGroup::kCreditCard:
      switch (field_type) {
        case CREDIT_CARD_NAME_FULL:
        case CREDIT_CARD_NAME_FIRST:
        case CREDIT_CARD_NAME_LAST:
          group = GROUP_CREDIT_CARD_NAME;
          break;
        case CREDIT_CARD_NUMBER:
          group = GROUP_CREDIT_CARD_NUMBER;
          break;
        case CREDIT_CARD_TYPE:
          group = GROUP_CREDIT_CARD_TYPE;
          break;
        case CREDIT_CARD_EXP_MONTH:
        case CREDIT_CARD_EXP_2_DIGIT_YEAR:
        case CREDIT_CARD_EXP_4_DIGIT_YEAR:
        case CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR:
        case CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR:
          group = GROUP_CREDIT_CARD_DATE;
          break;
        case CREDIT_CARD_VERIFICATION_CODE:
          group = GROUP_CREDIT_CARD_VERIFICATION;
          break;
        default:
          NOTREACHED() << field_type << " has no group assigned (ambiguous)";
      }
      break;

    case FieldTypeGroup::kStandaloneCvcField:
      group = GROUP_STANDALONE_CREDIT_CARD_VERIFICATION;
      break;

    case FieldTypeGroup::kPasswordField:
      group = GROUP_PASSWORD;
      break;

    case FieldTypeGroup::kUsernameField:
      group = GROUP_USERNAME;
      break;

    case FieldTypeGroup::kUnfillable:
      group = GROUP_UNFILLABLE;
      break;

    case FieldTypeGroup::kTransaction:
      NOTREACHED();
  }

  // Use bits 8-15 for the group and bits 0-7 for the metric.
  static_assert(NUM_FIELD_TYPE_QUALITY_METRICS <= UINT8_MAX,
                "maximum field type quality metric must fit into 8 bits");
  static_assert(NUM_FIELD_TYPE_GROUPS_FOR_METRICS <= UINT8_MAX,
                "number of field type groups must fit into 8 bits");
  return (group << 8) | metric;
}

namespace {

const char* GetQualityMetricPredictionSource(
    QualityMetricPredictionSource source) {
  switch (source) {
    case PREDICTION_SOURCE_UNKNOWN:
      NOTREACHED();
    case PREDICTION_SOURCE_HEURISTIC:
      return "Heuristic";
    case PREDICTION_SOURCE_SERVER:
      return "Server";
    case PREDICTION_SOURCE_OVERALL:
      return "Overall";
    case PREDICTION_SOURCE_ML_PREDICTIONS:
      return "ML";
  }
}

const char* GetQualityMetricTypeSuffix(QualityMetricType metric_type) {
  switch (metric_type) {
    default:
      NOTREACHED();
    case TYPE_SUBMISSION:
      return "";
    case TYPE_NO_SUBMISSION:
      return ".NoSubmission";
    case TYPE_AUTOCOMPLETE_BASED:
      return ".BasedOnAutocomplete";
  }
}

// Check if the value of |field| is same as one of the other autofilled
// values. This indicates a bad rationalization if |field| has
// only_fill_when_focused set to true.
bool DuplicatedFilling(const FormStructure& form, const AutofillField& field) {
  auto is_autofilled_with_same_value =
      [&field](const std::unique_ptr<AutofillField>& form_field) {
        if (field.global_id() == form_field->global_id()) {
          // When looking for fields in the form that have been filled with
          // the same value as `field`, skip `field`: `field` would
          // always have a matching value but does not indicate that a
          // *duplicate* filling happened.
          return false;
        }
        return field.value_for_import() == form_field->value_for_import() &&
               form_field->is_autofilled();
      };
  return std::ranges::any_of(form, is_autofilled_with_same_value);
}

void LogOnlyFillWhenFocusedRationalizationQuality(
    const std::string& aggregate_histogram,
    const std::string& type_specific_histogram,
    FieldType predicted_type,
    FieldType actual_type,
    bool is_empty,
    bool is_ambiguous,
    bool log_rationalization_metrics,
    const FormStructure& form,
    const AutofillField& field) {
  static constexpr char kRationalizationQualityHistogram[] =
      "Autofill.Rationalization.OnlyFillWhenFocused.Quality";
  // If it is filled with values unknown, it is a true negative.
  if (actual_type == UNKNOWN_TYPE) {
    // Only log aggregate true negative; do not log type specific metrics
    // for UNKNOWN/EMPTY.
    base::UmaHistogramSparse(
        aggregate_histogram,
        (is_empty ? TRUE_NEGATIVE_EMPTY
                  : (is_ambiguous ? TRUE_NEGATIVE_AMBIGUOUS
                                  : TRUE_NEGATIVE_UNKNOWN)));
    if (log_rationalization_metrics) {
      base::UmaHistogramSparse(
          kRationalizationQualityHistogram,
          (is_empty ? RATIONALIZATION_GOOD : RATIONALIZATION_OK));
    }
    return;
  }

  // If it is filled with same type as predicted, it is a true positive. We
  // also log an RATIONALIZATION_BAD by checking if the filled value is filled
  // already in previous fields, this means autofill could have filled it
  // automatically if there has been no rationalization.
  if (predicted_type == actual_type) {
    base::UmaHistogramSparse(aggregate_histogram, TRUE_POSITIVE);
    base::UmaHistogramSparse(
        type_specific_histogram,
        GetFieldTypeGroupPredictionQualityMetric(actual_type, TRUE_POSITIVE));
    if (log_rationalization_metrics) {
      bool duplicated_filling = DuplicatedFilling(form, field);
      base::UmaHistogramSparse(
          kRationalizationQualityHistogram,
          (duplicated_filling ? RATIONALIZATION_BAD : RATIONALIZATION_OK));
    }
    return;
  }

  // Here the prediction is wrong, but user has to provide some value still.
  // This should be a false negative.
  base::UmaHistogramSparse(aggregate_histogram, FALSE_NEGATIVE_MISMATCH);
  // Log FALSE_NEGATIVE_MISMATCH for predicted type if it did predicted
  // something but actual type is different.
  if (predicted_type != UNKNOWN_TYPE) {
    base::UmaHistogramSparse(type_specific_histogram,
                             GetFieldTypeGroupPredictionQualityMetric(
                                 predicted_type, FALSE_NEGATIVE_MISMATCH));
  }
  if (log_rationalization_metrics) {
    // Logging RATIONALIZATION_OK despite of type mismatch here because autofill
    // would have got it wrong with or without rationalization. Rationalization
    // here does not help, neither does it do any harm.
    base::UmaHistogramSparse(kRationalizationQualityHistogram,
                             RATIONALIZATION_OK);
  }
}

void LogPredictionQualityMetricsForCommonFields(
    const std::string& aggregate_histogram,
    const std::string& type_specific_histogram,
    FieldType predicted_type,
    FieldType actual_type,
    bool is_empty,
    bool is_ambiguous) {
  // If the predicted and actual types match then it's either a true positive
  // or a true negative (if they are both unknown). Do not log type specific
  // true negatives (instead log a true positive for the "Ambiguous" type).
  if (predicted_type == actual_type) {
    if (actual_type == UNKNOWN_TYPE) {
      // Only log aggregate true negative; do not log type specific metrics
      // for UNKNOWN/EMPTY.
      base::UmaHistogramSparse(
          aggregate_histogram,
          (is_empty ? TRUE_NEGATIVE_EMPTY
                    : (is_ambiguous ? TRUE_NEGATIVE_AMBIGUOUS
                                    : TRUE_NEGATIVE_UNKNOWN)));
      return;
    }

    // Log both aggregate and type specific true positive if we correctly
    // predict that type with which the field was filled.
    base::UmaHistogramSparse(aggregate_histogram, TRUE_POSITIVE);
    base::UmaHistogramSparse(
        type_specific_histogram,
        GetFieldTypeGroupPredictionQualityMetric(actual_type, TRUE_POSITIVE));
    return;
  }

  // Note: At this point predicted_type != actual type
  // If actual type is UNKNOWN_TYPE then the prediction is a false positive.
  // Further specialize the type of false positive by whether the field was
  // empty or contained an unknown value.
  if (actual_type == UNKNOWN_TYPE) {
    auto metric = (is_empty ? FALSE_POSITIVE_EMPTY
                            : (is_ambiguous ? FALSE_POSITIVE_AMBIGUOUS
                                            : FALSE_POSITIVE_UNKNOWN));
    base::UmaHistogramSparse(aggregate_histogram, metric);
    base::UmaHistogramSparse(
        type_specific_histogram,
        GetFieldTypeGroupPredictionQualityMetric(predicted_type, metric));
    return;
  }

  // Note: At this point predicted_type != actual type, actual_type != UNKNOWN.
  // If predicted type is UNKNOWN_TYPE then the prediction is a false negative
  // unknown.
  if (predicted_type == UNKNOWN_TYPE) {
    base::UmaHistogramSparse(aggregate_histogram, FALSE_NEGATIVE_UNKNOWN);
    base::UmaHistogramSparse(type_specific_histogram,
                             GetFieldTypeGroupPredictionQualityMetric(
                                 actual_type, FALSE_NEGATIVE_UNKNOWN));
    return;
  }

  // Note: At this point predicted_type != actual type, actual_type != UNKNOWN,
  //       predicted_type != UNKNOWN.
  // This is a mismatch. From the reference of the actual type, this is a false
  // negative (it was T, but predicted U). From the reference of the prediction,
  // this is a false positive (predicted it was T, but it was U).
  base::UmaHistogramSparse(aggregate_histogram, FALSE_NEGATIVE_MISMATCH);
  base::UmaHistogramSparse(type_specific_histogram,
                           GetFieldTypeGroupPredictionQualityMetric(
                               actual_type, FALSE_NEGATIVE_MISMATCH));
  base::UmaHistogramSparse(type_specific_histogram,
                           GetFieldTypeGroupPredictionQualityMetric(
                               predicted_type, FALSE_POSITIVE_MISMATCH));
}

// Logs field type prediction quality metrics.  The primary histogram name is
// constructed based on |prediction_source| The field-specific histogram names
// also incorporates the possible and predicted types for |field|. A suffix may
// be appended to the metric name, depending on |metric_type|.
void LogPredictionQualityMetrics(
    QualityMetricPredictionSource prediction_source,
    FieldType predicted_type,
    FormInteractionsUkmLogger& form_interactions_ukm_logger,
    ukm::SourceId source_id,
    const FormStructure& form,
    const AutofillField& field,
    QualityMetricType metric_type,
    bool log_rationalization_metrics) {
  // Generate histogram names.
  const char* source = GetQualityMetricPredictionSource(prediction_source);
  const char* suffix = GetQualityMetricTypeSuffix(metric_type);
  std::string raw_data_histogram =
      base::StrCat({kFieldPredictionMetricPrefix, source, suffix});
  std::string aggregate_histogram =
      base::StrCat({kAggregateFieldPredictionMetricPrefix, source, suffix});
  std::string type_specific_histogram =
      base::StrCat({kByFieldTypeFieldPredictionMetricPrefix, source, suffix});

  const FieldTypeSet& possible_types =
      metric_type == TYPE_AUTOCOMPLETE_BASED
          ? FieldTypeSet{AutofillType(field.html_type()).GetStorableType()}
          : field.possible_types();

  // Get the best type classification we can for the field.
  FieldType actual_type = GetActualFieldType(possible_types, predicted_type);

  DCHECK_LE(predicted_type, UINT16_MAX);
  DCHECK_LE(actual_type, UINT16_MAX);
  base::UmaHistogramSparse(raw_data_histogram,
                           (predicted_type << 16) | actual_type);

  form_interactions_ukm_logger.LogFieldType(
      source_id, form.form_parsed_timestamp(), form.form_signature(),
      field.GetFieldSignature(), prediction_source, metric_type, predicted_type,
      actual_type);

  // NO_SERVER_DATA is the equivalent of predicting UNKNOWN.
  if (predicted_type == NO_SERVER_DATA) {
    predicted_type = UNKNOWN_TYPE;
  }

  // The actual type being EMPTY_TYPE is the same as UNKNOWN_TYPE for comparison
  // purposes, but remember whether or not it was empty for more precise logging
  // later.
  bool is_empty = (actual_type == EMPTY_TYPE);
  bool is_ambiguous = (actual_type == AMBIGUOUS_TYPE);
  if (is_empty || is_ambiguous) {
    actual_type = UNKNOWN_TYPE;
  }

  // Log metrics for a field that is |only_fill_when_focused|==true. Basically
  // autofill might have a field prediction but it also thinks it should not
  // be filled automatically unless user focused on the field. This requires
  // different metrics logging than normal fields.
  if (field.only_fill_when_focused()) {
    LogOnlyFillWhenFocusedRationalizationQuality(
        aggregate_histogram, type_specific_histogram, predicted_type,
        actual_type, is_empty, is_ambiguous, log_rationalization_metrics, form,
        field);
    return;
  }

  LogPredictionQualityMetricsForCommonFields(
      aggregate_histogram, type_specific_histogram, predicted_type, actual_type,
      is_empty, is_ambiguous);
}

}  // namespace

void LogHeuristicPredictionQualityMetrics(
    FormInteractionsUkmLogger& form_interactions_ukm_logger,
    ukm::SourceId source_id,
    const FormStructure& form,
    const AutofillField& field,
    QualityMetricType metric_type) {
  LogPredictionQualityMetrics(
      PREDICTION_SOURCE_HEURISTIC, field.heuristic_type(),
      form_interactions_ukm_logger, source_id, form, field, metric_type,
      /*log_rationalization_metrics=*/false);
  if (metric_type == TYPE_SUBMISSION) {
    LogHeuristicPredictionQualityPerLabelSourceMetric(field);
  }
}

void LogHeuristicPredictionQualityPerLabelSourceMetric(
    const AutofillField& field) {
  FieldType predicted_type = field.heuristic_type();
  // If there are multiple `possible_types()`, `GetActualFieldType()` will:
  // - Return the `predicted_type` if it is contained in the set. A "true"
  //   sample is emitted to the metric.
  // - Return AMBIGUOUS_TYPE otherwise. A "false" sample is emitted to the
  //   metric.
  FieldType actual_type =
      GetActualFieldType(field.possible_types(), predicted_type);
  if (actual_type != UNKNOWN_TYPE && actual_type != EMPTY_TYPE) {
    base::UmaHistogramBoolean(
        base::StrCat(
            {kAggregateFieldPredictionMetricPrefix,
             GetQualityMetricPredictionSource(PREDICTION_SOURCE_HEURISTIC), ".",
             LabelSourceToString(field.label_source())}),
        predicted_type == actual_type);
  }
}

void LogMlPredictionQualityMetrics(
    FormInteractionsUkmLogger& form_interactions_ukm_logger,
    ukm::SourceId source_id,
    const FormStructure& form,
    const AutofillField& field,
    QualityMetricType metric_type) {
  LogPredictionQualityMetrics(
      PREDICTION_SOURCE_ML_PREDICTIONS,
      field.heuristic_type(HeuristicSource::kAutofillMachineLearning),
      form_interactions_ukm_logger, source_id, form, field, metric_type,
      /*log_rationalization_metrics=*/false);
}

// static
void LogServerPredictionQualityMetrics(
    FormInteractionsUkmLogger& form_interactions_ukm_logger,
    ukm::SourceId source_id,
    const FormStructure& form,
    const AutofillField& field,
    QualityMetricType metric_type) {
  LogPredictionQualityMetrics(PREDICTION_SOURCE_SERVER, field.server_type(),
                              form_interactions_ukm_logger, source_id, form,
                              field, metric_type,
                              /*log_rationalization_metrics=*/false);
}

// static
void LogOverallPredictionQualityMetrics(
    FormInteractionsUkmLogger& form_interactions_ukm_logger,
    ukm::SourceId source_id,
    const FormStructure& form,
    const AutofillField& field,
    QualityMetricType metric_type) {
  LogPredictionQualityMetrics(
      PREDICTION_SOURCE_OVERALL, field.Type().GetStorableType(),
      form_interactions_ukm_logger, source_id, form, field, metric_type,
      /*log_rationalization_metrics=*/true);
}

void LogEmailFieldPredictionMetrics(const AutofillField& field) {
  // If the field has no value, there is no need to record any of the metrics.
  const std::u16string& value = field.value_for_import();
  if (value.empty()) {
    return;
  }

  bool is_valid_email = IsValidEmailAddress(value);
  bool is_email_prediction = field.Type().GetStorableType() == EMAIL_ADDRESS;

  if (is_email_prediction) {
    EmailPredictionConfusionMatrix prediction_precision =
        is_valid_email ? EmailPredictionConfusionMatrix::kTruePositive
                       : EmailPredictionConfusionMatrix::kFalsePositive;
    base::UmaHistogramEnumeration(
        "Autofill.EmailPredictionCorrectness.Precision", prediction_precision);
  }

  if (is_valid_email) {
    EmailPredictionConfusionMatrix prediction_recall =
        is_email_prediction ? EmailPredictionConfusionMatrix::kTruePositive
                            : EmailPredictionConfusionMatrix::kFalseNegative;
    base::UmaHistogramEnumeration("Autofill.EmailPredictionCorrectness.Recall",
                                  prediction_recall);
  }
}

void LogLocalHeuristicMatchedAttribute(
    DenseSet<MatchAttribute> match_attributes) {
  // `match_attributes` can be empty if the field was classified as
  // UNKNOWN_TYPE. It can contain more than one entry if label and name were
  // used for the classification. In these cases, the metrics emit kNone and
  // kAmbiguous, respectively.
  enum class MetricsMatchAttribute {
    kNone = 0,
    kAmbiguous = 1,
    kLabel = 2,
    kName = 3,
    kMaxValue = kName
  };
  base::UmaHistogramEnumeration("Autofill.LocalHeuristics.MatchedAttribute",
                                [&] {
                                  if (match_attributes.empty()) {
                                    return MetricsMatchAttribute::kNone;
                                  }
                                  if (match_attributes.size() != 1) {
                                    return MetricsMatchAttribute::kAmbiguous;
                                  }
                                  switch (*match_attributes.begin()) {
                                    case MatchAttribute::kLabel:
                                      return MetricsMatchAttribute::kLabel;
                                    case MatchAttribute::kName:
                                      return MetricsMatchAttribute::kName;
                                  }
                                }());
}

void LogFieldPredictionOverlapMetrics(const AutofillField& field) {
  static constexpr std::string_view kAutocompletePresent =
      "AutocompleteAttributePresent.";
  static constexpr std::string_view kAutocompleteAbsent =
      "AutocompleteAttributeAbsent.";
  static constexpr std::string_view kAutocompleteAggregate =
      "AutocompleteAttributeAggregate.";
  static constexpr std::string_view kSourcesOverall = "Overall.";
  static constexpr std::string_view kAllTypes = "AllTypes";

  // In the majority of cases, there is only one possible type - let's discard
  // situations with multiple so that the metric is easy to interpret.
  if (field.possible_types().size() != 1) {
    return;
  }
  FieldType submitted_field_type = *field.possible_types().begin();
  if (submitted_field_type == UNKNOWN_TYPE ||
      submitted_field_type == EMPTY_TYPE) {
    return;
  }
  bool server_agrees =
      FilledTypeAgreesWithActual(field.possible_types(), field.server_type());
  bool heuristics_agree = FilledTypeAgreesWithActual(field.possible_types(),
                                                     field.heuristic_type());
  // We treat ac=garbage the same as ac unspecified because if it's garbage we
  // can't say what type it was supposed to represent.
  bool autocomplete_present =
      field.html_type() != HtmlFieldType::kUnspecified &&
      field.html_type() != HtmlFieldType::kUnrecognized;
  bool autocomplete_agrees =
      autocomplete_present &&
      FilledTypeAgreesWithActual(
          field.possible_types(),
          HtmlFieldTypeToBestCorrespondingFieldType(field.html_type()));

  FieldPredictionOverlapSourcesSuperset sample =
      GetFieldPredictionOverlapSample(server_agrees, heuristics_agree,
                                      autocomplete_agrees);

  std::string_view field_type_str = FieldTypeToStringView(submitted_field_type);
  std::string prediction_source =
      field.PredictionSource().has_value()
          ? base::StrCat({AutofillPredictionSourceToStringView(
                              *field.PredictionSource()),
                          "Active."})
          : "NoPredictionExists.";

  // Autocomplete-aggreated histograms:
  {
    std::string prefix =
        base::StrCat({kFieldPredictionOverlapPrefix, kAutocompleteAggregate});
    // Sources aggregated:
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, kSourcesOverall, kAllTypes}), sample);
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, kSourcesOverall, field_type_str}), sample);
    // Source-specific:
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, prediction_source, kAllTypes}), sample);
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, prediction_source, field_type_str}), sample);
  }

  // Autocomplete-specific histograms:
  {
    std::string prefix = base::StrCat(
        {kFieldPredictionOverlapPrefix,
         autocomplete_present ? kAutocompletePresent : kAutocompleteAbsent});
    // Sources aggregated:
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, kSourcesOverall, kAllTypes}), sample);
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, kSourcesOverall, field_type_str}), sample);
    // Source-specific:
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, prediction_source, kAllTypes}), sample);
    base::UmaHistogramEnumeration(
        base::StrCat({prefix, prediction_source, field_type_str}), sample);
  }
}

}  // namespace autofill::autofill_metrics