Skip to content

Commit

Permalink
[Blink] Move FormControlType enum to //third_party/blink/public/common
Browse files Browse the repository at this point in the history
Before this CL, HTMLFormControlElement::FormControlType() returned
an AtomicString and WebFormControlElement::FormControlType() mapped
that string an enum defined in web_form_control_element.h.

This CL moves the enum to //third_party/blink/public/common. The
main goal is to make the enum visible in ContextMenuData and
other context-menu-related classes.

This CL also eliminates the large if-statement in
WebFormControlElement::FormControlType() and instead returns
an enum in HTMLFormControlElement::FormControlType().

Bug: 1491569, 1482526, 1484810
Change-Id: I6075445059cfcdd8b60286ecd6507a9a6d0ed6e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4927529
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Philip Jägenstedt <foolip@chromium.org>
Commit-Queue: Christoph Schwering <schwering@google.com>
Reviewed-by: Mason Freed <masonf@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1209345}
  • Loading branch information
schwering authored and Chromium LUCI CQ committed Oct 13, 2023
1 parent 3c7d2f8 commit d7fd9bf
Show file tree
Hide file tree
Showing 75 changed files with 393 additions and 459 deletions.
18 changes: 9 additions & 9 deletions chrome/renderer/autofill/form_autofill_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ using blink::WebLocalFrame;
using blink::WebSelectElement;
using blink::WebString;
using blink::WebVector;
using Type = WebFormControlElement::Type;

namespace autofill::form_util {

Expand Down Expand Up @@ -493,12 +492,13 @@ class FormAutofillTest : public ChromeRenderViewTest {
WebString value;
WebFormControlElement element = GetFormControlElementById(
WebString::FromASCII(field_case.id_attribute));
if ((element.FormControlType() == Type::kSelectOne) ||
(element.FormControlType() == Type::kTextArea)) {
if ((element.FormControlType() == blink::FormControlType::kSelectOne) ||
(element.FormControlType() == blink::FormControlType::kTextArea)) {
value = get_value_function(element);
} else {
ASSERT_TRUE(element.FormControlType() == Type::kInputText ||
element.FormControlType() == Type::kInputMonth);
ASSERT_TRUE(
element.FormControlType() == blink::FormControlType::kInputText ||
element.FormControlType() == blink::FormControlType::kInputMonth);
value = get_value_function(element);
}

Expand Down Expand Up @@ -2294,23 +2294,23 @@ class FormAutofillTest : public ChromeRenderViewTest {
}

static WebString GetValueWrapper(WebFormControlElement element) {
if (element.FormControlType() == Type::kTextArea) {
if (element.FormControlType() == blink::FormControlType::kTextArea) {
return element.To<WebFormControlElement>().Value();
}

if (element.FormControlType() == Type::kSelectOne) {
if (element.FormControlType() == blink::FormControlType::kSelectOne) {
return element.To<WebSelectElement>().Value();
}

return element.To<WebInputElement>().Value();
}

static WebString GetSuggestedValueWrapper(WebFormControlElement element) {
if (element.FormControlType() == Type::kTextArea) {
if (element.FormControlType() == blink::FormControlType::kTextArea) {
return element.To<WebFormControlElement>().SuggestedValue();
}

if (element.FormControlType() == Type::kSelectOne) {
if (element.FormControlType() == blink::FormControlType::kSelectOne) {
return element.To<WebSelectElement>().SuggestedValue();
}

Expand Down
2 changes: 1 addition & 1 deletion components/autofill/content/renderer/autofill_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ FocusedFieldType AutofillAgent::FocusStateNotifier::GetFieldType(
}

if (input_element.FormControlTypeForAutofill() ==
WebFormControlElement::Type::kInputSearch) {
blink::FormControlType::kInputSearch) {
return FocusedFieldType::kFillableSearchField;
}
if (input_element.IsPasswordFieldForAutofill()) {
Expand Down
32 changes: 16 additions & 16 deletions components/autofill/content/renderer/form_autofill_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ using blink::mojom::GenericIssueErrorType;
// https://chromium-review.googlesource.com/c/chromium/src/+/4543002.
namespace autofill::form_util {

using Type = WebFormControlElement::Type;
using Type = blink::FormControlType;
using ::autofill::mojom::ButtonTitleType;

struct ShadowFieldData {
Expand Down Expand Up @@ -1979,35 +1979,35 @@ bool IsAutofillableElement(const WebFormControlElement& element) {
base::FeatureList::IsEnabled(features::kAutofillEnableSelectList));
}

FormControlType ToAutofillFormControlType(WebFormControlElement::Type type) {
FormControlType ToAutofillFormControlType(blink::FormControlType type) {
switch (type) {
case WebFormControlElement::Type::kInputCheckbox:
case blink::FormControlType::kInputCheckbox:
return FormControlType::kInputCheckbox;
case WebFormControlElement::Type::kInputEmail:
case blink::FormControlType::kInputEmail:
return FormControlType::kInputEmail;
case WebFormControlElement::Type::kInputMonth:
case blink::FormControlType::kInputMonth:
return FormControlType::kInputMonth;
case WebFormControlElement::Type::kInputNumber:
case blink::FormControlType::kInputNumber:
return FormControlType::kInputNumber;
case WebFormControlElement::Type::kInputPassword:
case blink::FormControlType::kInputPassword:
return FormControlType::kInputPassword;
case WebFormControlElement::Type::kInputRadio:
case blink::FormControlType::kInputRadio:
return FormControlType::kInputRadio;
case WebFormControlElement::Type::kInputSearch:
case blink::FormControlType::kInputSearch:
return FormControlType::kInputSearch;
case WebFormControlElement::Type::kInputTelephone:
case blink::FormControlType::kInputTelephone:
return FormControlType::kInputTelephone;
case WebFormControlElement::Type::kInputText:
case blink::FormControlType::kInputText:
return FormControlType::kInputText;
case WebFormControlElement::Type::kInputUrl:
case blink::FormControlType::kInputUrl:
return FormControlType::kInputUrl;
case WebFormControlElement::Type::kSelectOne:
case blink::FormControlType::kSelectOne:
return FormControlType::kSelectOne;
case WebFormControlElement::Type::kSelectMultiple:
case blink::FormControlType::kSelectMultiple:
return FormControlType::kSelectMultiple;
case WebFormControlElement::Type::kSelectList:
case blink::FormControlType::kSelectList:
return FormControlType::kSelectList;
case WebFormControlElement::Type::kTextArea:
case blink::FormControlType::kTextArea:
return FormControlType::kTextArea;
default:
NOTREACHED_NORETURN();
Expand Down
3 changes: 1 addition & 2 deletions components/autofill/content/renderer/form_autofill_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ bool IsAutofillableInputElement(const blink::WebInputElement& element);
// {Text, Radiobutton, Checkbox, Select, TextArea}.
bool IsAutofillableElement(const blink::WebFormControlElement& element);

FormControlType ToAutofillFormControlType(
blink::WebFormControlElement::Type type);
FormControlType ToAutofillFormControlType(blink::FormControlType type);

// Returns true iff `element` has a "webauthn" autocomplete attribute.
bool IsWebauthnTaggedElement(const blink::WebFormControlElement& element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ void AnnotateFieldsWithSignatures(
// fields, both of which are cached in the Document.
bool HasPasswordField(const WebLocalFrame& frame) {
auto ContainsPasswordField = [&](const auto& fields) {
return base::Contains(fields,
blink::WebFormControlElement::Type::kInputPassword,
return base::Contains(fields, blink::FormControlType::kInputPassword,
&WebFormControlElement::FormControlTypeForAutofill);
};

Expand Down Expand Up @@ -2211,7 +2210,7 @@ bool PasswordAutofillAgent::IsPasswordFieldFilledByUser(
const WebFormControlElement& element) const {
FieldRendererId element_id = form_util::GetFieldRendererId(element);
return element.FormControlTypeForAutofill() ==
blink::WebFormControlElement::Type::kInputPassword &&
blink::FormControlType::kInputPassword &&
(field_data_manager_->DidUserType(element_id) ||
field_data_manager_->WasAutofilledOnUserTrigger(element_id));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ bool IsGaiaReauthenticationForm(const blink::WebFormElement& form) {
// We're only interested in the presence
// of <input type="hidden" /> elements.
const WebInputElement input = element.DynamicTo<WebInputElement>();
if (input.IsNull() ||
input.FormControlTypeForAutofill() !=
blink::WebFormControlElement::Type::kInputHidden) {
if (input.IsNull() || input.FormControlTypeForAutofill() !=
blink::FormControlType::kInputHidden) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions third_party/blink/public/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ source_set("headers") {
"fetch/fetch_api_request_headers_map.h",
"fetch/fetch_api_request_headers_mojom_traits.h",
"forcedark/forcedark_switches.h",
"forms/form_control_type.h",
"frame/delegated_capability_request_token.h",
"frame/fenced_frame_permissions_policies.h",
"frame/fenced_frame_sandbox_flags.h",
Expand Down
57 changes: 57 additions & 0 deletions third_party/blink/public/common/forms/form_control_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_PUBLIC_COMMON_FORMS_FORM_CONTROL_TYPE_H_
#define THIRD_PARTY_BLINK_PUBLIC_COMMON_FORMS_FORM_CONTROL_TYPE_H_

#include <cstdint>

namespace blink {

// An enum representation of the values of the `type` attribute of form control
// elements. This list is exhaustive.
enum class FormControlType : uint8_t {
// https://html.spec.whatwg.org/multipage/form-elements.html#attr-button-type
kButtonButton,
kButtonSubmit,
kButtonReset,
kButtonSelectList,
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-fieldset-type
kFieldset,
// https://html.spec.whatwg.org/multipage/input.html#attr-input-type
kInputButton,
kInputCheckbox,
kInputColor,
kInputDate,
kInputDatetimeLocal,
kInputEmail,
kInputFile,
kInputHidden,
kInputImage,
kInputMonth,
kInputNumber,
kInputPassword,
kInputRadio,
kInputRange,
kInputReset,
kInputSearch,
kInputSubmit,
kInputTelephone,
kInputText,
kInputTime,
kInputUrl,
kInputWeek,
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-type
kOutput,
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-select-type
kSelectOne,
kSelectMultiple,
kSelectList,
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-type
kTextArea,
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_PUBLIC_COMMON_FORMS_FORM_CONTROL_TYPE_H_
39 changes: 3 additions & 36 deletions third_party/blink/public/web/web_form_control_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_FORM_CONTROL_ELEMENT_H_
#define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_FORM_CONTROL_ELEMENT_H_

#include "third_party/blink/public/common/forms/form_control_type.h"
#include "third_party/blink/public/common/metrics/form_element_pii_type.h"
#include "third_party/blink/public/platform/web_common.h"
#include "third_party/blink/public/platform/web_string.h"
Expand Down Expand Up @@ -59,42 +60,8 @@ class BLINK_EXPORT WebFormControlElement : public WebElement {
bool IsReadOnly() const;
WebString FormControlName() const;

enum class Type {
kButtonButton,
kButtonSubmit,
kButtonReset,
kButtonSelectList,
kFieldset,
kInputButton,
kInputCheckbox,
kInputColor,
kInputDate,
kInputDatetimeLocal,
kInputEmail,
kInputFile,
kInputHidden,
kInputImage,
kInputMonth,
kInputNumber,
kInputPassword,
kInputRadio,
kInputRange,
kInputReset,
kInputSearch,
kInputSubmit,
kInputTelephone,
kInputText,
kInputTime,
kInputUrl,
kInputWeek,
kOutput,
kSelectOne,
kSelectMultiple,
kSelectList,
kTextArea,
};
Type FormControlType() const;
Type FormControlTypeForAutofill() const;
enum FormControlType FormControlType() const;
enum FormControlType FormControlTypeForAutofill() const;

enum WebAutofillState GetAutofillState() const;
bool IsAutofilled() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,91 +64,14 @@ WebString WebFormControlElement::FormControlName() const {
return ConstUnwrap<HTMLFormControlElement>()->GetName();
}

WebFormControlElement::Type WebFormControlElement::FormControlType() const {
const HTMLFormControlElement* form_control =
ConstUnwrap<HTMLFormControlElement>();
const WTF::AtomicString& type = form_control->type();
if (IsA<HTMLButtonElement>(form_control)) {
if (type == "button") {
return Type::kButtonButton;
} else if (type == "submit") {
return Type::kButtonSubmit;
} else if (type == "reset") {
return Type::kButtonReset;
} else if (type == "selectlist") {
return Type::kButtonSelectList;
}
} else if (IsA<HTMLFieldSetElement>(form_control)) {
CHECK_EQ(type, "fieldset");
return Type::kFieldset;
} else if (IsA<HTMLInputElement>(form_control)) {
if (type == input_type_names::kButton) {
return Type::kInputButton;
} else if (type == input_type_names::kCheckbox) {
return Type::kInputCheckbox;
} else if (type == input_type_names::kColor) {
return Type::kInputColor;
} else if (type == input_type_names::kDate) {
return Type::kInputDate;
} else if (type == input_type_names::kDatetimeLocal) {
return Type::kInputDatetimeLocal;
} else if (type == input_type_names::kEmail) {
return Type::kInputEmail;
} else if (type == input_type_names::kFile) {
return Type::kInputFile;
} else if (type == input_type_names::kHidden) {
return Type::kInputHidden;
} else if (type == input_type_names::kImage) {
return Type::kInputImage;
} else if (type == input_type_names::kMonth) {
return Type::kInputMonth;
} else if (type == input_type_names::kNumber) {
return Type::kInputNumber;
} else if (type == input_type_names::kPassword) {
return Type::kInputPassword;
} else if (type == input_type_names::kRadio) {
return Type::kInputRadio;
} else if (type == input_type_names::kRange) {
return Type::kInputRange;
} else if (type == input_type_names::kReset) {
return Type::kInputReset;
} else if (type == input_type_names::kSearch) {
return Type::kInputSearch;
} else if (type == input_type_names::kSubmit) {
return Type::kInputSubmit;
} else if (type == input_type_names::kTel) {
return Type::kInputTelephone;
} else if (type == input_type_names::kText) {
return Type::kInputText;
} else if (type == input_type_names::kTime) {
return Type::kInputTime;
} else if (type == input_type_names::kUrl) {
return Type::kInputUrl;
} else if (type == input_type_names::kWeek) {
return Type::kInputWeek;
}
} else if (IsA<HTMLOutputElement>(form_control)) {
CHECK_EQ(type, "output");
return Type::kOutput;
} else if (IsA<HTMLSelectElement>(form_control)) {
if (type == "select-one") {
return Type::kSelectOne;
} else if (type == "select-multiple") {
return Type::kSelectMultiple;
}
} else if (IsA<HTMLSelectListElement>(form_control)) {
return Type::kSelectList;
} else if (IsA<HTMLTextAreaElement>(form_control)) {
return Type::kTextArea;
}
NOTREACHED_NORETURN();
FormControlType WebFormControlElement::FormControlType() const {
return ConstUnwrap<HTMLFormControlElement>()->FormControlType();
}

WebFormControlElement::Type WebFormControlElement::FormControlTypeForAutofill()
const {
FormControlType WebFormControlElement::FormControlTypeForAutofill() const {
if (auto* input = ::blink::DynamicTo<HTMLInputElement>(*private_)) {
if (input->IsTextField() && input->HasBeenPasswordField()) {
return Type::kInputPassword;
return FormControlType::kInputPassword;
}
}
return FormControlType();
Expand Down

0 comments on commit d7fd9bf

Please sign in to comment.