chromium/components/autofill/core/common/form_field_data.h

// 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 <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 {};

// Autofill supports assigning <label for=x> tags to inputs if x is id/name,
// or the id/name of a shadow host element containing the input.
// This enum is used to track how often each case occurs in practice.
enum class AssignedLabelSource {};

// FieldPropertiesMask is used to contain combinations of FieldPropertiesFlags
// values.
FieldPropertiesMask;

// 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 {};

// Stores information about the section of the field.
class Section {};

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

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 {};

// 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 {};

std::string_view FormControlTypeToString(FormControlType type);

// Consider using the FormControlType enum instead.
//
// The fallback value is returned if `type_string` has no corresponding enum
// value in `FormControlType`. Regular use-cases should not need to pass a
// fallback value because `FormControlType` reflects all autofillable form
// control types.
//
// An exception where a fallback is needed is deserialization code. For legacy
// reasons, form control types are serialized as strings. The fallback value
// handles cases where the serialized data is corrupted or perhaps refers to an
// old form control type that has been removed from the HTML spec or from
// Autofill since.
FormControlType StringToFormControlTypeDiscouraged(
    std::string_view type_string,
    std::optional<FormControlType> fallback = std::nullopt);

// 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);

// Prefer to use this macro in place of |EXPECT_EQ()| for comparing
// |FormFieldData|s in test code.
// TODO(crbug.com/40765988): Replace this with FormData::DeepEqual().
#define EXPECT_FORM_FIELD_DATA_EQUALS(expected, actual)

// 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_