File: form_field_data.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (719 lines) | stat: -rw-r--r-- 26,008 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
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
// Copyright 2013 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/common/form_field_data.h"

#include <algorithm>
#include <optional>
#include <tuple>
#include <variant>

#include "base/notreached.h"
#include "base/pickle.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/types/cxx23_to_underlying.h"
#include "base/types/zip.h"
#include "build/build_config.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_util.h"
#include "components/autofill/core/common/logging/log_buffer.h"
#include "components/autofill/core/common/logging/stream_operator_util.h"

// TODO(crbug.com/41422062): Clean up the (de)serialization code.

namespace autofill {

namespace {

// Increment this anytime pickle format is modified as well as provide
// deserialization routine from previous kFormFieldDataPickleVersion format.
const int kFormFieldDataPickleVersion = 9;

void WriteSelectOption(const SelectOption& option, base::Pickle* pickle) {
  pickle->WriteString16(option.value);
  pickle->WriteString16(option.text);
}

bool ReadSelectOption(base::PickleIterator* iter, SelectOption* option) {
  std::u16string value;
  std::u16string content;
  if (!iter->ReadString16(&value) || !iter->ReadString16(&content))
    return false;
  *option = {.value = value, .text = content};
  return true;
}

void WriteSelectOptionVector(const std::vector<SelectOption>& options,
                             base::Pickle* pickle) {
  pickle->WriteInt(static_cast<int>(options.size()));
  for (const SelectOption& option : options)
    WriteSelectOption(option, pickle);
}

bool ReadSelectOptionVector(base::PickleIterator* iter,
                            std::vector<SelectOption>* options) {
  int size;
  if (!iter->ReadInt(&size))
    return false;

  for (int i = 0; i < size; i++) {
    SelectOption pickle_data;
    if (!ReadSelectOption(iter, &pickle_data))
      return false;
    options->push_back(pickle_data);
  }
  return true;
}

bool ReadStringVector(base::PickleIterator* iter,
                      std::vector<std::u16string>* strings) {
  int size;
  if (!iter->ReadInt(&size))
    return false;

  std::u16string pickle_data;
  for (int i = 0; i < size; i++) {
    if (!iter->ReadString16(&pickle_data))
      return false;
    strings->push_back(pickle_data);
  }
  return true;
}

template <typename T>
bool ReadAsInt(base::PickleIterator* iter, T* target_value) {
  int pickle_data;
  if (!iter->ReadInt(&pickle_data))
    return false;

  *target_value = static_cast<T>(pickle_data);
  return true;
}

bool DeserializeSection1(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  std::string form_control_type;
  std::u16string label;
  std::u16string name;
  std::u16string value;
  std::string autocomplete_attribute;
  uint64_t max_length = 0;
  bool is_autofilled = false;
  bool success =
      iter->ReadString16(&label) && iter->ReadString16(&name) &&
      iter->ReadString16(&value) && iter->ReadString(&form_control_type) &&
      iter->ReadString(&autocomplete_attribute) &&
      iter->ReadUInt64(&max_length) && iter->ReadBool(&is_autofilled);
  if (success) {
    field_data->set_label(std::move(label));
    field_data->set_name(std::move(name));
    field_data->set_value(std::move(value));
    field_data->set_autocomplete_attribute(std::move(autocomplete_attribute));
    field_data->set_max_length(max_length);
    field_data->set_is_autofilled(std::move(is_autofilled));
    // Form control types are serialized as strings for legacy reasons.
    // TODO(crbug.com/1353392,crbug.com/1482526): Why does the Password Manager
    // (de)serialize form control types? Remove it or migrate it to the enum
    // values.
    field_data->set_form_control_type(
        StringToFormControlTypeDiscouraged(form_control_type)
            .value_or(FormControlType::kInputText));
  }
  return success;
}

bool DeserializeSection5(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  bool is_checked = false;
  bool is_checkable = false;
  const bool success =
      iter->ReadBool(&is_checked) && iter->ReadBool(&is_checkable);

  if (success)
    SetCheckStatus(field_data, is_checkable, is_checked);

  return success;
}

bool DeserializeSection6(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  FormFieldData::CheckStatus check_status =
      FormFieldData::CheckStatus::kNotCheckable;
  if (!ReadAsInt(iter, &check_status)) {
    return false;
  }
  field_data->set_check_status(check_status);
  return true;
}

bool DeserializeSection7(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  bool is_focusable = false;
  bool should_autocomplete = false;
  if (!iter->ReadBool(&is_focusable) || !iter->ReadBool(&should_autocomplete)) {
    return false;
  }
  field_data->set_is_focusable(std::move(is_focusable));
  field_data->set_should_autocomplete(std::move(should_autocomplete));
  return true;
}

bool DeserializeSection3(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  std::vector<std::u16string> option_values;
  std::vector<std::u16string> option_texts;
  base::i18n::TextDirection text_direction = base::i18n::UNKNOWN_DIRECTION;
  if (!ReadAsInt(iter, &text_direction) ||
      !ReadStringVector(iter, &option_values) ||
      !ReadStringVector(iter, &option_texts) ||
      option_values.size() != option_texts.size()) {
    return false;
  }
  field_data->set_text_direction(text_direction);
  std::vector<SelectOption> options;
  for (auto [option_value, option_text] :
       base::zip(option_values, option_texts)) {
    options.push_back(
        {.value = std::move(option_value), .text = std::move(option_text)});
  }
  field_data->set_options(std::move(options));
  return true;
}

bool DeserializeSection12(base::PickleIterator* iter,
                          FormFieldData* field_data) {
  base::i18n::TextDirection text_direction;
  std::vector<SelectOption> options;
  if (!ReadAsInt(iter, &text_direction) ||
      !ReadSelectOptionVector(iter, &options)) {
    return false;
  }
  field_data->set_text_direction(std::move(text_direction));
  field_data->set_options(std::move(options));
  return true;
}

bool DeserializeSection2(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  FormFieldData::RoleAttribute role = FormFieldData::RoleAttribute::kOther;
  if (!ReadAsInt(iter, &role)) {
    return false;
  }
  field_data->set_role(role);
  return true;
}

bool DeserializeSection4(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  std::u16string placeholder;
  if (!iter->ReadString16(&placeholder)) {
    return false;
  }
  field_data->set_placeholder(std::move(placeholder));
  return true;
}

bool DeserializeSection8(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  std::u16string css_classes;
  if (!iter->ReadString16(&css_classes)) {
    return false;
  }
  field_data->set_css_classes(std::move(css_classes));
  return true;
}

bool DeserializeSection9(base::PickleIterator* iter,
                         FormFieldData* field_data) {
  FieldPropertiesMask properties_mask;
  if (!iter->ReadUInt32(&properties_mask)) {
    return false;
  }
  field_data->set_properties_mask(std::move(properties_mask));
  return true;
}

bool DeserializeSection10(base::PickleIterator* iter,
                          FormFieldData* field_data) {
  std::u16string id_attribute;
  if (!iter->ReadString16(&id_attribute)) {
    return false;
  }
  field_data->set_id_attribute(std::move(id_attribute));
  return true;
}

bool DeserializeSection11(base::PickleIterator* iter,
                          FormFieldData* field_data) {
  std::u16string name_attribute;
  if (!iter->ReadString16(&name_attribute)) {
    return false;
  }
  field_data->set_name_attribute(std::move(name_attribute));
  return true;
}

}  // namespace

Section Section::FromAutocomplete(Section::Autocomplete autocomplete) {
  Section section;
  if (autocomplete.section.empty() && autocomplete.mode == HtmlFieldMode::kNone)
    return section;
  section.value_ = std::move(autocomplete);
  return section;
}

Section Section::FromFieldIdentifier(
    const FormFieldData& field,
    base::flat_map<LocalFrameToken, size_t>& frame_token_ids) {
  Section section;
  // Set the section's value based on the field identifiers: the field's name,
  // mapped frame id, renderer id. We do not use LocalFrameTokens but instead
  // map them to consecutive integers using `frame_token_ids`, which uniquely
  // identify a frame within a given FormStructure. Since we do not intend to
  // compare sections from different FormStructures, this is sufficient.
  //
  // We intentionally do not include the LocalFrameToken in the section
  // because frame tokens should not be sent to a renderer.
  //
  // TODO(crbug.com/40200532): Remove special handling of FrameTokens.
  size_t generated_frame_id =
      frame_token_ids.emplace(field.host_frame(), frame_token_ids.size())
          .first->second;
  section.value_ = FieldIdentifier(base::UTF16ToUTF8(field.name()),
                                   generated_frame_id, field.renderer_id());
  return section;
}

Section::Section() = default;

Section::Section(const Section& section) = default;

Section::~Section() = default;

Section::operator bool() const {
  return !is_default();
}

bool Section::is_from_autocomplete() const {
  return std::holds_alternative<Autocomplete>(value_);
}

bool Section::is_from_fieldidentifier() const {
  return std::holds_alternative<FieldIdentifier>(value_);
}

bool Section::is_default() const {
  return std::holds_alternative<Default>(value_);
}

std::string Section::ToString() const {
  static constexpr char kDefaultSection[] = "-default";

  std::string section_name;
  if (const Autocomplete* autocomplete = std::get_if<Autocomplete>(&value_)) {
    // To prevent potential section name collisions, append `kDefaultSection`
    // suffix to fields without a `HtmlFieldMode`. Without this, 'autocomplete'
    // attribute values "section--shipping street-address" and "shipping
    // street-address" would have the same prefix.
    section_name = autocomplete->section +
                   (autocomplete->mode != HtmlFieldMode::kNone
                        ? "-" + HtmlFieldModeToString(autocomplete->mode)
                        : kDefaultSection);
  } else if (const FieldIdentifier* f = std::get_if<FieldIdentifier>(&value_)) {
    FieldIdentifier field_identifier = *f;
    section_name = base::StrCat(
        {field_identifier.field_name, "_",
         base::NumberToString(field_identifier.local_frame_id), "_",
         base::NumberToString(field_identifier.field_renderer_id.value())});
  }

  return section_name.empty() ? kDefaultSection : section_name;
}

LogBuffer& operator<<(LogBuffer& buffer, const Section& section) {
  return buffer << section.ToString();
}

std::ostream& operator<<(std::ostream& os, const Section& section) {
  return os << section.ToString();
}

LogBuffer& operator<<(LogBuffer& buffer, FormControlType type) {
  return buffer << FormControlTypeToString(type);
}

FormFieldData::FormFieldData() = default;

FormFieldData::FormFieldData(const FormFieldData&) = default;

FormFieldData& FormFieldData::operator=(const FormFieldData&) = default;

FormFieldData::FormFieldData(FormFieldData&&) = default;

FormFieldData& FormFieldData::operator=(FormFieldData&&) = default;

FormFieldData::~FormFieldData() = default;

base::optional_ref<const SelectOption> FormFieldData::selected_option() const {
  for (const SelectOption& option : options()) {
    if (option.value == value()) {
      return option;
    }
  }
  return std::nullopt;
}

bool FormFieldData::SameFieldAs(const FormFieldData& field) const {
  auto equality_tuple = [](const FormFieldData& f) {
    return std::tie(f.label_, f.name_, f.name_attribute_, f.id_attribute_,
                    f.form_control_type_, f.autocomplete_attribute_,
                    f.placeholder_, f.max_length_, f.css_classes_,
                    f.is_focusable_, f.should_autocomplete_, f.role_,
                    f.text_direction_, f.options_);
  };
  return equality_tuple(*this) == equality_tuple(field);
}

bool FormFieldData::IsTextInputElement() const {
  return form_control_type() == FormControlType::kInputText ||
         form_control_type() == FormControlType::kInputPassword ||
         form_control_type() == FormControlType::kInputSearch ||
         form_control_type() == FormControlType::kInputTelephone ||
         form_control_type() == FormControlType::kInputUrl ||
         form_control_type() == FormControlType::kInputEmail ||
         form_control_type() == FormControlType::kInputNumber;
}

bool FormFieldData::IsPasswordInputElement() const {
  return form_control_type() == FormControlType::kInputPassword;
}

bool FormFieldData::IsSelectElement() const {
  return form_control_type() == FormControlType::kSelectOne;
}

// static
bool FormFieldData::DeepEqual(const FormFieldData& a, const FormFieldData& b) {
  return a.global_id() == b.global_id() && a.SameFieldAs(b);
}

FormFieldData::FillData::FillData() = default;

FormFieldData::FillData::~FillData() = default;

FormFieldData::FillData::FillData(const FormFieldData& field)
    : value(field.value()),
      renderer_id(field.renderer_id()),
      host_form_id(field.host_form_id()),
      section(field.section()),
      is_autofilled(field.is_autofilled()),
      force_override(field.force_override()) {}

FormFieldData::FillData::FillData(const FillData&) = default;

FormFieldData::FillData& FormFieldData::FillData::operator=(const FillData&) =
    default;

std::string_view FormControlTypeToString(FormControlType type) {
  switch (type) {
    case FormControlType::kContentEditable:
      return "contenteditable";
    case FormControlType::kInputCheckbox:
      return "checkbox";
    case FormControlType::kInputDate:
      return "date";
    case FormControlType::kInputEmail:
      return "email";
    case FormControlType::kInputMonth:
      return "month";
    case FormControlType::kInputNumber:
      return "number";
    case FormControlType::kInputPassword:
      return "password";
    case FormControlType::kInputRadio:
      return "radio";
    case FormControlType::kInputSearch:
      return "search";
    case FormControlType::kInputTelephone:
      return "tel";
    case FormControlType::kInputText:
      return "text";
    case FormControlType::kInputUrl:
      return "url";
    case FormControlType::kSelectOne:
      return "select-one";
    case FormControlType::kTextArea:
      return "textarea";
  }
  NOTREACHED();
}

std::optional<FormControlType> StringToFormControlTypeDiscouraged(
    std::string_view type_string) {
  for (auto i = base::to_underlying(FormControlType::kMinValue);
       i <= base::to_underlying(FormControlType::kMaxValue); ++i) {
    FormControlType type = static_cast<FormControlType>(i);
    if (mojom::IsKnownEnumValue(type) &&
        type_string == FormControlTypeToString(type)) {
      return type;
    }
  }
  return std::nullopt;
}

void SerializeFormFieldData(const FormFieldData& field_data,
                            base::Pickle* pickle) {
  pickle->WriteInt(kFormFieldDataPickleVersion);
  pickle->WriteString16(field_data.label());
  pickle->WriteString16(field_data.name());
  pickle->WriteString16(field_data.value());
  pickle->WriteString(FormControlTypeToString(field_data.form_control_type()));
  // We don't serialize the `parsed_autocomplete`. See http://crbug.com/1353392.
  pickle->WriteString(field_data.autocomplete_attribute());
  pickle->WriteUInt64(field_data.max_length());
  pickle->WriteBool(field_data.is_autofilled());
  pickle->WriteInt(static_cast<int>(field_data.check_status()));
  pickle->WriteBool(field_data.is_focusable());
  pickle->WriteBool(field_data.should_autocomplete());
  pickle->WriteInt(static_cast<int>(field_data.role()));
  pickle->WriteInt(field_data.text_direction());
  WriteSelectOptionVector(field_data.options(), pickle);
  pickle->WriteString16(field_data.placeholder());
  pickle->WriteString16(field_data.css_classes());
  pickle->WriteUInt32(field_data.properties_mask());
  pickle->WriteString16(field_data.id_attribute());
  pickle->WriteString16(field_data.name_attribute());
}

bool DeserializeFormFieldData(base::PickleIterator* iter,
                              FormFieldData* field_data) {
  int version;
  FormFieldData temp_form_field_data;
  if (!iter->ReadInt(&version)) {
    LOG(ERROR) << "Bad pickle of FormFieldData, no version present";
    return false;
  }

  switch (version) {
    case 1: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection5(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 2: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection5(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 3: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection5(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data) ||
          !DeserializeSection4(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 4: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection6(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data) ||
          !DeserializeSection4(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 5: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection6(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data) ||
          !DeserializeSection4(iter, &temp_form_field_data) ||
          !DeserializeSection8(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 6: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection6(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data) ||
          !DeserializeSection4(iter, &temp_form_field_data) ||
          !DeserializeSection8(iter, &temp_form_field_data) ||
          !DeserializeSection9(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 7: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection6(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data) ||
          !DeserializeSection4(iter, &temp_form_field_data) ||
          !DeserializeSection8(iter, &temp_form_field_data) ||
          !DeserializeSection9(iter, &temp_form_field_data) ||
          !DeserializeSection10(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 8: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection6(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection3(iter, &temp_form_field_data) ||
          !DeserializeSection4(iter, &temp_form_field_data) ||
          !DeserializeSection8(iter, &temp_form_field_data) ||
          !DeserializeSection9(iter, &temp_form_field_data) ||
          !DeserializeSection10(iter, &temp_form_field_data) ||
          !DeserializeSection11(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    case 9: {
      if (!DeserializeSection1(iter, &temp_form_field_data) ||
          !DeserializeSection6(iter, &temp_form_field_data) ||
          !DeserializeSection7(iter, &temp_form_field_data) ||
          !DeserializeSection2(iter, &temp_form_field_data) ||
          !DeserializeSection12(iter, &temp_form_field_data) ||
          !DeserializeSection4(iter, &temp_form_field_data) ||
          !DeserializeSection8(iter, &temp_form_field_data) ||
          !DeserializeSection9(iter, &temp_form_field_data) ||
          !DeserializeSection10(iter, &temp_form_field_data) ||
          !DeserializeSection11(iter, &temp_form_field_data)) {
        LOG(ERROR) << "Could not deserialize FormFieldData from pickle";
        return false;
      }
      break;
    }
    default: {
      LOG(ERROR) << "Unknown FormFieldData pickle version " << version;
      return false;
    }
  }
  *field_data = temp_form_field_data;
  return true;
}

std::ostream& operator<<(std::ostream& os, const FormFieldData& field) {
  return internal::PrintWithIndentation(os, field, /*indentation=*/0);
}

namespace internal {

std::ostream& PrintWithIndentation(std::ostream& os,
                                   const FormFieldData& field,
                                   int indentation,
                                   std::string_view title) {
  std::string space = std::string(indentation, ' ');
  os << space << "{";
  if (!title.empty()) {
    os << " /*" << title << "*/";
  }
  os << '\n';
#define PRINT_PROPERTY(property)                                             \
  os << space << "  " << #property << ": " << PrintWrapper(field.property()) \
     << ",\n"
  PRINT_PROPERTY(global_id);
  PRINT_PROPERTY(label);
  PRINT_PROPERTY(origin);
  PRINT_PROPERTY(name);
  PRINT_PROPERTY(id_attribute);
  PRINT_PROPERTY(name_attribute);
  PRINT_PROPERTY(value);
  PRINT_PROPERTY(form_control_type);
  PRINT_PROPERTY(autocomplete_attribute);
  PRINT_PROPERTY(parsed_autocomplete);
  PRINT_PROPERTY(placeholder);
  PRINT_PROPERTY(max_length);
  PRINT_PROPERTY(css_classes);
  PRINT_PROPERTY(is_autofilled);
  PRINT_PROPERTY(check_status);
  PRINT_PROPERTY(is_focusable);
  PRINT_PROPERTY(should_autocomplete);
  PRINT_PROPERTY(role);
  PRINT_PROPERTY(text_direction);
  PRINT_PROPERTY(is_enabled);
  PRINT_PROPERTY(is_readonly);
  PRINT_PROPERTY(is_focusable);
  PRINT_PROPERTY(is_visible);
  PRINT_PROPERTY(user_input);
  PRINT_PROPERTY(label_source);
  PRINT_PROPERTY(bounds);
#undef PRINT_PROPERTY
  os << space << "}";
  return os;
}

}  // namespace internal

LogBuffer& operator<<(LogBuffer& buffer, const FormFieldData& field) {
  buffer << Tag{"table"};
  buffer << Tr{} << "Name:" << field.name();
  buffer << Tr{} << "Identifiers:"
         << base::StrCat({"renderer id: ",
                          base::NumberToString(field.renderer_id().value()),
                          ", host frame: ",
                          field.renderer_form_id().frame_token.ToString(), " (",
                          field.origin().Serialize(),
                          "), host form renderer id: ",
                          base::NumberToString(field.host_form_id().value())});
  buffer << Tr{} << "Origin:" << field.origin().Serialize();
  buffer << Tr{} << "Name attribute:" << field.name_attribute();
  buffer << Tr{} << "Id attribute:" << field.id_attribute();
  constexpr size_t kMaxLabelSize = 100;
  const std::u16string truncated_label =
      field.label().substr(0, std::min(field.label().length(), kMaxLabelSize));
  buffer << Tr{} << "Label:" << truncated_label;
  buffer << Tr{} << "Form control type:" << field.form_control_type();
  buffer << Tr{} << "Autocomplete attribute:" << field.autocomplete_attribute();
  buffer << Tr{} << "Parsed autocomplete attribute:"
         << (field.parsed_autocomplete()
                 ? field.parsed_autocomplete()->ToString()
                 : "");
  buffer << Tr{} << "Aria label:" << field.aria_label();
  buffer << Tr{} << "Aria description:" << field.aria_description();
  buffer << Tr{} << "Section:" << field.section();
  buffer << Tr{} << "Is focusable:" << field.is_focusable();
  buffer << Tr{} << "Is enabled:" << field.is_enabled();
  buffer << Tr{} << "Is readonly:" << field.is_readonly();
  buffer << Tr{} << "Is empty:" << (field.value().empty() ? "Yes" : "No");
  buffer << Tr{} << "Value:" << field.value() << SetParentTagContainsPII{};
  buffer << CTag{"table"};
  return buffer;
}

}  // namespace autofill