File: form_field_data.h

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 (670 lines) | stat: -rw-r--r-- 28,083 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
// 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.

#ifndef COMPONENTS_AUTOFILL_CORE_COMMON_FORM_FIELD_DATA_H_
#define COMPONENTS_AUTOFILL_CORE_COMMON_FORM_FIELD_DATA_H_

#include <stddef.h>

#include <compare>
#include <limits>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>
#include <vector>

#include "base/i18n/rtl.h"
#include "base/types/optional_ref.h"
#include "build/build_config.h"
#include "components/autofill/core/common/autocomplete_parsing_util.h"
#include "components/autofill/core/common/html_field_types.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom-shared.h"
#include "components/autofill/core/common/signatures.h"
#include "components/autofill/core/common/unique_ids.h"
#include "ui/gfx/geometry/rect_f.h"
#include "url/origin.h"

namespace base {
class Pickle;
class PickleIterator;
}  // namespace base

namespace autofill {

class LogBuffer;

// The flags describing form field properties.
enum FieldPropertiesFlags : uint32_t {
  kNoFlags = 0u,
  kUserTyped = 1u << 0,
  // kAutofilled means that at least one character of the field value comes from
  // being autofilled. This is different from
  // WebFormControlElement::IsAutofilled(). It is meant to be used for password
  // fields, to determine whether viewing the value needs user reauthentication.
  kAutofilledOnUserTrigger = 1u << 1,
  // The field received focus at any moment.
  kHadFocus = 1u << 2,
  // Use this flag, if some error occurred in flags processing.
  kErrorOccurred = 1u << 3,
  // On submission, the value of the field was recognised as a value which is
  // already stored.
  kKnownValue = 1u << 4,
  // A value was autofilled on pageload. This means that at least one character
  // of the field value comes from being autofilled.
  kAutofilledOnPageLoad = 1u << 5,
  // Whether a field was filled because the user used the password manual
  // fallbacks feature.
  kAutofilledPasswordFormFilledViaManualFallback = 1u << 6,
  // Whether a change password filled was autofilled as part of change password
  // process. Filling happens on page-load although it's initiated by a user.
  kAutofilledChangePasswordFormOnPageLoad = 1u << 7,
  // A value was autofilled on any of the triggers.
  kAutofilled = kAutofilledOnUserTrigger | kAutofilledOnPageLoad |
                kAutofilledPasswordFormFilledViaManualFallback |
                kAutofilledChangePasswordFormOnPageLoad,
};

// FieldPropertiesMask is used to contain combinations of FieldPropertiesFlags
// values.
using FieldPropertiesMask = std::underlying_type_t<FieldPropertiesFlags>;

// HTML                                      | value  | text
// ------------------------------------------+--------+------
// <option value=Foo label=Bar>Baz</option>  | "Foo"  | "Bar"
// <option value=Foo>Bar</option>            | "Foo"  | "Bar"
// <option label=Bar>Foo</option>            | "Foo"  | "Bar"
// <option>Foo</option>                      | "Foo"  | "Foo"
// <option value=Foo></option>               | "Foo"  | ""
// <option label=Bar></option>               | ""     | "Bar"
struct SelectOption {
  friend bool operator==(const SelectOption& lhs,
                         const SelectOption& rhs) = default;

  // The option's "value" attribute, or, if not present, its text content.
  std::u16string value;
  // The option's "label" attribute, or, if not present, its text content.
  std::u16string text;
};

// Stores information about the section of the field.
class Section {
 public:
  struct Autocomplete {
    friend auto operator<=>(const Autocomplete& lhs,
                            const Autocomplete& rhs) = default;
    friend bool operator==(const Autocomplete& lhs,
                           const Autocomplete& rhs) = default;

    std::string section;
    HtmlFieldMode mode = HtmlFieldMode::kNone;
  };

  using Default = std::monostate;

  struct FieldIdentifier {
    FieldIdentifier() = default;
    FieldIdentifier(std::string field_name,
                    size_t local_frame_id,
                    FieldRendererId field_renderer_id)
        : field_name(std::move(field_name)),
          local_frame_id(local_frame_id),
          field_renderer_id(field_renderer_id) {}

    friend auto operator<=>(const FieldIdentifier& lhs,
                            const FieldIdentifier& rhs) = default;
    friend bool operator==(const FieldIdentifier& lhs,
                           const FieldIdentifier& rhs) = default;

    std::string field_name;
    size_t local_frame_id;
    FieldRendererId field_renderer_id;
  };

  static Section FromAutocomplete(Autocomplete autocomplete);
  static Section FromFieldIdentifier(
      const FormFieldData& field,
      base::flat_map<LocalFrameToken, size_t>& frame_token_ids);

  Section();
  Section(const Section& section);
  ~Section();

  friend auto operator<=>(const Section& lhs, const Section& rhs) = default;
  friend bool operator==(const Section& lhs, const Section& rhs) = default;
  explicit operator bool() const;

  bool is_from_autocomplete() const;
  bool is_from_fieldidentifier() const;
  bool is_default() const;

  // Reconstructs `this` to a string. The string representation of the section
  // is used in the renderer.
  // TODO(crbug.com/40200532): Remove when fixed.
  std::string ToString() const;

 private:
  // Represents the section's origin:
  //  - `Default` is the empty, initial value before running any sectioning
  //     algorithm,
  //  - `Autocomplete` represents a section derived from the autocomplete
  //     attribute,
  //  - `FieldIdentifier` represents a section generated based on the first
  //     field in the section.
  using SectionValue = std::variant<Default, Autocomplete, FieldIdentifier>;

  friend struct mojo::StructTraits<autofill::mojom::SectionDataView,
                                   autofill::Section>;
  friend struct mojo::UnionTraits<autofill::mojom::SectionValueDataView,
                                  autofill::Section::SectionValue>;

  SectionValue value_;
};

LogBuffer& operator<<(LogBuffer& buffer, const Section& section);
std::ostream& operator<<(std::ostream& os, const Section& section);

using FormControlType = mojom::FormControlType;

LogBuffer& operator<<(LogBuffer& buffer, FormControlType type);

// Stores information about a field in a form. Read more about forms and fields
// at FormData.
class FormFieldData {
 public:
  using CheckStatus = mojom::FormFieldData_CheckStatus;
  using RoleAttribute = mojom::FormFieldData_RoleAttribute;
  using LabelSource = mojom::FormFieldData_LabelSource;

  struct FillData;

  // Returns true if many members of fields |a| and |b| are identical.
  //
  // "Many" is intended to be "all", but currently the following members are not
  // being compared:
  //
  // - FormFieldData::value,
  // - FormFieldData::aria_label,
  // - FormFieldData::aria_description,
  // - FormFieldData::host_frame,
  // - FormFieldData::host_form_id,
  // - FormFieldData::host_form_signature,
  // - FormFieldData::origin,
  // - FormFieldData::force_override,
  // - FormFieldData::form_control_ax_id,
  // - FormFieldData::section,
  // - FormFieldData::is_autofilled,
  // - FormFieldData::is_user_edited,
  // - FormFieldData::properties_mask,
  // - FormFieldData::is_enabled,
  // - FormFieldData::is_readonly,
  // - FormFieldData::user_input,
  // - FormFieldData::options,
  // - FormFieldData::label_source,
  // - FormFieldData::bounds,
  // - FormFieldData::datalist_options.
  static bool DeepEqual(const FormFieldData& a, const FormFieldData& b);

  FormFieldData();
  FormFieldData(const FormFieldData&);
  FormFieldData& operator=(const FormFieldData&);
  FormFieldData(FormFieldData&&);
  FormFieldData& operator=(FormFieldData&&);
  ~FormFieldData();

  // Uniquely identifies the DOM element that this field represents.
  //
  // It does *not* uniquely identify this FormFieldData object (there is no such
  // kind of identifier because FormFieldData is a value type). In particular,
  // they're not guaranteed to be unique FormData::fields; see FormData::fields
  // for details.
  //
  // Must not be leaked to renderer process. See FieldGlobalId for details.
  FieldGlobalId global_id() const { return {host_frame(), renderer_id()}; }

  // An identifier of the renderer form that contained this field.
  // This may be different from the browser form that contains this field in the
  // case of a frame-transcending form. See AutofillDriverRouter and
  // internal::FormForest for details on the distinction between renderer and
  // browser forms.
  FormGlobalId renderer_form_id() const {
    return {host_frame(), host_form_id()};
  }

  // TODO(crbug.com/40183094): This function is deprecated. Use
  // FormFieldData::DeepEqual() instead.
  bool SameFieldAs(const FormFieldData& field) const;

  // Returns true for all of textfield-looking types: text, password,
  // search, email, url, and number. It must work the same way as Blink function
  // WebInputElement::IsTextField(), and it returns false if |*this| represents
  // a textarea.
  bool IsTextInputElement() const;

  bool IsPasswordInputElement() const;

  // <select> gets special handling when it comes to unfocusable fields. The
  // motivation for this exception is that synthetic select fields often come
  // with an unfocusable <select> element.
  //
  // A synthetic select field is a combination of JavaScript-controlled DOM
  // elements that provide a list of options. They're frequently associated with
  // hidden (i.e., unfocusable) <select> element. JavaScript keeps the selected
  // option in sync with the visible DOM elements of the select field. To
  // support synthetic select fields, Autofill intentionally fills unfocusable
  // <select> elements.
  bool IsSelectElement() const;

  // Returns true if the field is focusable to the user.
  // This is an approximation of visibility with false positives.
  bool IsFocusable() const {
    return is_focusable() && role() != RoleAttribute::kPresentation;
  }

  // NOTE: Update `SameFieldAs()` and `FormFieldDataAndroid::SimilarFieldAs()`
  // if needed when adding new a member.

  // The name by which autofill knows this field. This is generally either the
  // name attribute or the id_attribute value, which-ever is non-empty with
  // priority given to the name_attribute. This value is used when computing
  // form signatures.
  // TODO(crbug.com/40598703): remove this and use attributes/unique_id instead.
  const std::u16string& name() const { return name_; }
  void set_name(std::u16string name) { name_ = std::move(name); }

  const std::u16string& id_attribute() const { return id_attribute_; }
  void set_id_attribute(std::u16string id_attribute) {
    id_attribute_ = std::move(id_attribute);
  }
  const std::u16string& name_attribute() const { return name_attribute_; }
  void set_name_attribute(std::u16string name_attribute) {
    name_attribute_ = std::move(name_attribute);
  }
  const std::u16string& label() const { return label_; }
  void set_label(std::u16string label) { label_ = std::move(label); }

  // The form control element's value (i.e., the value of their IDL attribute
  // "value") or the contenteditable's text content, depending on the
  // FormFieldData::form_control_type().
  //
  // To get a field's initial value or the value for submission, see
  // AutofillField::initial_value() and AutofillField::value_for_import().
  //
  // A note on FormFieldData objects of type FormControlType::kSelect*, i.e.,
  // <select> elements:
  //
  //   <select> elements have an associated list <option> elements, each of
  //   which has a value and a text. The idea is that the value serves technical
  //   purposes, while the text is visible to the user.
  //
  //   FormFieldData::value() is the value of the selected <option>, if any, or
  //   the empty string. See SelectOption for details on how the value of an
  //   <option> is determined.
  //
  //   FormFieldData::value() may not be the ideal human-readable representation
  //   of a <select> element. The selected option's text is usually the better
  //   string to display to the user (e.g., during form import). For further
  //   details, see SelectOption and FormFieldData::selected_option().
  //
  // Truncated at `kMaxStringLength`.
  // TODO(crbug.com/40941640): Extract the value of contenteditables on iOS.
  const std::u16string& value() const { return value_; }
  void set_value(std::u16string value) { value_ = std::move(value); }

  // Returns the (first) selected option. Returns std::nullopt if none is found.
  // The only field types that come with options are FormControlType::kSelect*
  // and FormControlType::kInput* with a datalist. But even their `value()` may
  // mismatch all `options()`, e.g., when JavaScript set the value to a
  // different value or when the number or string length of the options exceeded
  // limits during extraction.
  base::optional_ref<const SelectOption> selected_option() const LIFETIME_BOUND;

  // The selected text, or the empty string if no text is selected.
  // Truncated at `50 * kMaxStringLength`.
  // This is not necessarily a substring of `value` because both strings are
  // truncated, and because for rich-text contenteditables the selection and
  // text content differ in whitespace.
  // TODO(crbug.com/40941640): Extract on iOS.
  const std::u16string& selected_text() const { return selected_text_; }
  void set_selected_text(std::u16string selected_text) {
    selected_text_ = std::move(selected_text);
  }

  FormControlType form_control_type() const { return form_control_type_; }
  void set_form_control_type(FormControlType form_control_type) {
    form_control_type_ = form_control_type;
  }
  const std::string& autocomplete_attribute() const {
    return autocomplete_attribute_;
  }
  void set_autocomplete_attribute(std::string autocomplete_attribute) {
    autocomplete_attribute_ = std::move(autocomplete_attribute);
  }
  const std::optional<AutocompleteParsingResult>& parsed_autocomplete() const {
    return parsed_autocomplete_;
  }
  void set_parsed_autocomplete(
      std::optional<AutocompleteParsingResult> parsed_autocomplete) {
    parsed_autocomplete_ = std::move(parsed_autocomplete);
  }

  // The value of the form control element's "pattern" attribute. The string
  // comes from the renderer without any further validation. There are no
  // guarantees about the format of the string.
  const std::u16string& pattern() const { return pattern_; }
  void set_pattern(std::u16string pattern) { pattern_ = std::move(pattern); }

  const std::u16string& placeholder() const { return placeholder_; }
  void set_placeholder(std::u16string placeholder) {
    placeholder_ = std::move(placeholder);
  }
  const std::u16string& css_classes() const { return css_classes_; }
  void set_css_classes(std::u16string css_classes) {
    css_classes_ = std::move(css_classes);
  }
  const std::u16string& aria_label() const { return aria_label_; }
  void set_aria_label(std::u16string aria_label) {
    aria_label_ = std::move(aria_label);
  }
  const std::u16string& aria_description() const { return aria_description_; }
  void set_aria_description(std::u16string aria_description) {
    aria_description_ = std::move(aria_description);
  }

  // A unique identifier of the containing frame. This value is not serialized
  // because LocalFrameTokens must not be leaked to other renderer processes.
  // It is not persistent between page loads and therefore not used in
  // comparison in SameFieldAs().
  const LocalFrameToken& host_frame() const { return host_frame_; }
  void set_host_frame(LocalFrameToken host_frame) {
    host_frame_ = std::move(host_frame);
  }

  // Uniquely identifies the DOM element that this field represents among the
  // field DOM elements in the same frame.
  // In the browser process, use global_id() instead.
  // See global_id() for details on the properties and pitfalls.
  FieldRendererId renderer_id() const { return renderer_id_; }
  void set_renderer_id(FieldRendererId renderer_id) {
    renderer_id_ = renderer_id;
  }

  // Renderer ID of the owning form in the same frame.
  FormRendererId host_form_id() const { return host_form_id_; }
  void set_host_form_id(FormRendererId host_form_id) {
    host_form_id_ = host_form_id;
  }

  // The signature of the field's renderer form, that is, the signature of the
  // FormData that contained this field when it was received by the
  // AutofillDriver (see AutofillDriverRouter and internal::FormForest
  // for details on the distinction between renderer and browser forms).
  // Currently, the value is only set in ContentAutofillDriver; it's null on iOS
  // and in the Password Manager.
  // This value is written and read only in the browser for voting of
  // cross-frame forms purposes. It is therefore not sent via mojo.
  FormSignature host_form_signature() const { return host_form_signature_; }
  void set_host_form_signature(FormSignature host_form_signature) {
    host_form_signature_ = host_form_signature;
  }

  // The origin of the frame that hosts the field.
  const url::Origin& origin() const { return origin_; }
  void set_origin(url::Origin origin) { origin_ = std::move(origin); }

  // The ax node id of the form control in the accessibility tree.
  int32_t form_control_ax_id() const { return form_control_ax_id_; }
  void set_form_control_ax_id(int32_t form_control_ax_id) {
    form_control_ax_id_ = form_control_ax_id;
  }

  // The unique identifier of the section (e.g. billing vs. shipping address)
  // of this field.
  const Section& section() const { return section_; }
  void set_section(Section section) { section_ = std::move(section); }

  // The default value for text fields that have no maxlength attribute
  // specified. We choose the maximum 32 bit, rather than 64 bit, number because
  // so we don't need to worry about integer overflows when doing arithmetic
  // with FormFieldData::max_length.
  static constexpr size_t kDefaultMaxLength =
      std::numeric_limits<uint32_t>::max();

  // The maximum length of the FormFieldData::value as specified in the DOM. For
  // fields that do not support free text input (e.g., <select> and <input
  // type=month>), this is 0. For other fields (e.g., <input type=text>), this
  // is `kDefaultMaxLength`, which means we don't need to worry about integer
  // overflows when doing arithmetic with FormFieldData::max_length.
  //
  // Changes to the default value also must be reflected in
  // form_autofill_util.cc's GetMaxLength().
  //
  // We use uint64_t instead of size_t because this struct is sent over IPC
  // which could span 32 & 64 bit processes. We chose uint64_t instead of
  // uint32_t to maintain compatibility with old code which used size_t
  // (base::Pickle used to serialize that as 64 bit).
  uint64_t max_length() const { return max_length_; }
  void set_max_length(uint64_t max_length) { max_length_ = max_length; }

  bool is_autofilled() const { return is_autofilled_; }
  void set_is_autofilled(bool is_autofilled) { is_autofilled_ = is_autofilled; }

  // Whether the user has edited this field since page load or resetting the
  // field.
  //
  // Examples that count as edits:
  // - Typing into a text control.
  // - Pasting into a text control.
  // - Clicking and selecting an option of a <select> counts.
  // - Unfocusing a <select> using TAB (because of the keydown event).
  //
  // Examples that do not count as edits:
  // - Autofill.
  // - Typing into a contenteditable.
  // - Setting the field's value directly in JavaScript.
  // - Untrusted events (see JavaScript's Event.isTrusted).
  //
  // The property is sticky: a user-edited field becomes non-user-edited only
  // when the form is reset (JavaScript's HTMLFormElement.reset()).
  // TODO(crbug.com/40941928): On iOS, also non-trusted events reset the
  // property.
  bool is_user_edited() const { return is_user_edited_; }
  void set_is_user_edited(bool is_user_edited) {
    is_user_edited_ = is_user_edited;
  }

  CheckStatus check_status() const { return check_status_; }
  void set_check_status(CheckStatus check_status) {
    check_status_ = check_status;
  }
  bool is_focusable() const { return is_focusable_; }
  void set_is_focusable(bool is_focusable) { is_focusable_ = is_focusable; }
  bool is_visible() const { return is_visible_; }
  void set_is_visible(bool is_visible) { is_visible_ = is_visible; }
  bool should_autocomplete() const { return should_autocomplete_; }
  void set_should_autocomplete(bool should_autocomplete) {
    should_autocomplete_ = should_autocomplete;
  }
  RoleAttribute role() const { return role_; }
  void set_role(RoleAttribute role) { role_ = role; }
  base::i18n::TextDirection text_direction() const { return text_direction_; }
  void set_text_direction(base::i18n::TextDirection text_direction) {
    text_direction_ = text_direction;
  }
  FieldPropertiesMask properties_mask() const { return properties_mask_; }
  void set_properties_mask(FieldPropertiesMask properties_mask) {
    properties_mask_ = properties_mask;
  }

  // Data members from the next block are used for parsing only, they are not
  // serialised for storage.
  bool is_enabled() const { return is_enabled_; }
  void set_is_enabled(bool is_enabled) { is_enabled_ = is_enabled; }
  bool is_readonly() const { return is_readonly_; }
  void set_is_readonly(bool is_readonly) { is_readonly_ = is_readonly; }
  // Contains password, username or credit card number value that was either
  // manually typed or autofilled on user trigger into a text-mode input field.
  const std::u16string& user_input() const { return user_input_; }
  void set_user_input(std::u16string user_input) {
    user_input_ = std::move(user_input);
  }

  // The computed writingsuggestions value. See
  // https://html.spec.whatwg.org/multipage/interaction.html#writing-suggestions
  // for spec.
  // TODO(crbug.com/338590542): Extract on iOS.
  bool allows_writing_suggestions() const {
    return allows_writing_suggestions_;
  }
  void set_allows_writing_suggestions(bool allows_writing_suggestions) {
    allows_writing_suggestions_ = allows_writing_suggestions;
  }

  // The options of a select box.
  const std::vector<SelectOption>& options() const { return options_; }
  void set_options(std::vector<SelectOption> options) {
    options_ = std::move(options);
  }

  // Password Manager doesn't use labels nor client side nor server side, so
  // label_source isn't in serialize methods.
  LabelSource label_source() const { return label_source_; }
  void set_label_source(LabelSource label_source) {
    label_source_ = label_source;
  }

  // The bounds of this field in current frame coordinates at the
  // form-extraction time. It is valid if not empty, will not be synced to the
  // server side or be used for field comparison and isn't in serialize methods.
  const gfx::RectF& bounds() const { return bounds_; }
  void set_bounds(gfx::RectF bounds) { bounds_ = std::move(bounds); }

  // The datalist is associated with this field, if any. Will not be synced to
  // the server side or be used for field comparison and aren't in serialize
  // methods.
  const std::vector<SelectOption>& datalist_options() const {
    return datalist_options_;
  }
  void set_datalist_options(std::vector<SelectOption> datalist_options) {
    datalist_options_ = std::move(datalist_options);
  }

  // When sent from browser to renderer, this bit indicates whether a field
  // should be filled even though it is already considered autofilled OR
  // user modified.
  bool force_override() const { return force_override_; }
  void set_force_override(bool force_override) {
    force_override_ = force_override;
  }

 private:
  std::u16string name_;
  std::u16string id_attribute_;
  std::u16string name_attribute_;
  std::u16string label_;
  std::u16string value_;
  std::u16string selected_text_;
  FormControlType form_control_type_ = FormControlType::kInputText;
  std::string autocomplete_attribute_;
  std::optional<AutocompleteParsingResult> parsed_autocomplete_;
  std::u16string pattern_;
  std::u16string placeholder_;
  std::u16string css_classes_;
  std::u16string aria_label_;
  std::u16string aria_description_;
  LocalFrameToken host_frame_;
  FieldRendererId renderer_id_;
  FormRendererId host_form_id_;
  FormSignature host_form_signature_;
  url::Origin origin_;
  int32_t form_control_ax_id_ = 0;
  uint64_t max_length_ = std::numeric_limits<uint32_t>::max();
  Section section_;
  bool is_autofilled_ = false;
  bool is_user_edited_ = false;
  CheckStatus check_status_ = CheckStatus::kNotCheckable;
  bool is_focusable_ = true;
  bool is_visible_ = true;  // See `features::kAutofillDetectFieldVisibility`.
  bool should_autocomplete_ = true;
  RoleAttribute role_ = RoleAttribute::kOther;
  base::i18n::TextDirection text_direction_ = base::i18n::UNKNOWN_DIRECTION;
  FieldPropertiesMask properties_mask_ = 0;
  bool is_enabled_ = false;
  bool is_readonly_ = false;
  std::u16string user_input_;
  bool allows_writing_suggestions_ = true;
  std::vector<SelectOption> options_;
  LabelSource label_source_ = LabelSource::kUnknown;
  gfx::RectF bounds_;
  std::vector<SelectOption> datalist_options_;
  bool force_override_ = false;
};

// Structure containing necessary information to be sent from the browser to the
// renderer in order to fill a field.
// See documentation of FormFieldData for more info.
struct FormFieldData::FillData {
  FillData();
  explicit FillData(const FormFieldData& field);
  FillData(const FillData&);
  FillData& operator=(const FillData&);

  ~FillData();

  // The field value to be set by the renderer.
  std::u16string value;

  // Uniquely identifies the DOM element that this field represents among the
  // field DOM elements in the same document.
  FieldRendererId renderer_id;

  // Uniquely identifies the DOM element of the form containing this field among
  // elements in the same document (or the collection of unowned fields of the
  // DOM in case this ID is null).
  FormRendererId host_form_id;

  // The unique identifier of the section (e.g. billing vs. shipping address)
  // of this field. This is only used on iOS.
  // TODO(crbug.com/40266549): Remove when Undo Autofill launches on iOS.
  Section section;

  // Whether the renderer should mark the field as autofilled or not. In most
  // filling cases this will be true. However for the case of UndoAutofill we
  // might wanna revert a field state into not autofilled, in which case this
  // would be false.
  bool is_autofilled = false;

  // When sent from browser to renderer, this bit indicates whether a field
  // should be filled even though it is already considered autofilled OR
  // user modified.
  // TODO(crbug.com/40943206): Remove.
  bool force_override = false;
};

std::string_view FormControlTypeToString(FormControlType type);

// Consider using the FormControlType enum instead.
//
// Callers may have to handle `std::nullopt` in case the `type_string` they
// handle may be an invalid type string, e.g., when the function is called in
// deserializiation code.
std::optional<FormControlType> StringToFormControlTypeDiscouraged(
    std::string_view type_string);

// Serialize and deserialize FormFieldData. These are used when FormData objects
// are serialized and deserialized.
void SerializeFormFieldData(const FormFieldData& form_field_data,
                            base::Pickle* serialized);
bool DeserializeFormFieldData(base::PickleIterator* pickle_iterator,
                              FormFieldData* form_field_data);

// So we can compare FormFieldDatas with EXPECT_EQ().
std::ostream& operator<<(std::ostream& os, const FormFieldData& field);

// Produces a <table> element with information about the form.
LogBuffer& operator<<(LogBuffer& buffer, const FormFieldData& form);

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_COMMON_FORM_FIELD_DATA_H_