1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_
10 #include "base/strings/string16.h"
11 #include "ui/gfx/rect.h"
17 class WebFormControlElement
;
19 class WebInputElement
;
27 struct WebElementDescriptor
;
29 // A bit field mask for form or form element requirements.
30 enum RequirementsMask
{
31 REQUIRE_NONE
= 0, // No requirements.
32 REQUIRE_AUTOCOMPLETE
= 1, // Require that autocomplete != off.
35 // A bit field mask to extract data from WebFormControlElement.
38 EXTRACT_VALUE
= 1 << 0, // Extract value from WebFormControlElement.
39 EXTRACT_OPTION_TEXT
= 1 << 1, // Extract option text from
40 // WebFormSelectElement. Only valid when
41 // |EXTRACT_VALUE| is set.
42 // This is used for form submission where
43 // human readable value is captured.
44 EXTRACT_OPTIONS
= 1 << 2, // Extract options from
45 // WebFormControlElement.
48 // The maximum number of form fields we are willing to parse, due to
49 // computational costs. Several examples of forms with lots of fields that are
50 // not relevant to Autofill: (1) the Netflix queue; (2) the Amazon wishlist;
51 // (3) router configuration pages; and (4) other configuration pages, e.g. for
52 // Google code project settings.
53 extern const size_t kMaxParseableFields
;
55 // Returns true if |element| is a month input element.
56 bool IsMonthInput(const blink::WebInputElement
* element
);
58 // Returns true if |element| is a text input element.
59 bool IsTextInput(const blink::WebInputElement
* element
);
61 // Returns true if |element| is a select element.
62 bool IsSelectElement(const blink::WebFormControlElement
& element
);
64 // Returns true if |element| is a textarea element.
65 bool IsTextAreaElement(const blink::WebFormControlElement
& element
);
67 // Returns true if |element| is a checkbox or a radio button element.
68 bool IsCheckableElement(const blink::WebInputElement
* element
);
70 // Returns true if |element| is one of the input element types that can be
71 // autofilled. {Text, Radiobutton, Checkbox}.
72 bool IsAutofillableInputElement(const blink::WebInputElement
* element
);
74 // Recursively checks whether |node| or any of its children have a non-empty
76 bool IsWebNodeVisible(const blink::WebNode
& node
);
78 // Returns the form's |name| attribute if non-empty; otherwise the form's |id|
80 const base::string16
GetFormIdentifier(const blink::WebFormElement
& form
);
82 // Returns true if the element specified by |click_element| was successfully
84 bool ClickElement(const blink::WebDocument
& document
,
85 const WebElementDescriptor
& element_descriptor
);
87 // Fills |autofillable_elements| with all the auto-fillable form control
88 // elements in |form_element|.
89 void ExtractAutofillableElements(
90 const blink::WebFormElement
& form_element
,
91 RequirementsMask requirements
,
92 std::vector
<blink::WebFormControlElement
>* autofillable_elements
);
94 // Fills out a FormField object from a given WebFormControlElement.
95 // |extract_mask|: See the enum ExtractMask above for details.
96 void WebFormControlElementToFormField(
97 const blink::WebFormControlElement
& element
,
98 ExtractMask extract_mask
,
99 FormFieldData
* field
);
101 // Fills |form| with the FormData object corresponding to the |form_element|.
102 // If |field| is non-NULL, also fills |field| with the FormField object
103 // corresponding to the |form_control_element|.
104 // |extract_mask| controls what data is extracted.
105 // Returns true if |form| is filled out; it's possible that the |form_element|
106 // won't meet the |requirements|. Also returns false if there are no fields or
107 // too many fields in the |form|.
108 bool WebFormElementToFormData(
109 const blink::WebFormElement
& form_element
,
110 const blink::WebFormControlElement
& form_control_element
,
111 RequirementsMask requirements
,
112 ExtractMask extract_mask
,
114 FormFieldData
* field
);
116 // Finds the form that contains |element| and returns it in |form|. Fills
117 // |field| with the |FormField| representation for element.
118 // Returns false if the form is not found or cannot be serialized.
119 bool FindFormAndFieldForFormControlElement(
120 const blink::WebFormControlElement
& element
,
122 FormFieldData
* field
,
123 RequirementsMask requirements
);
125 // Fills the form represented by |form|. |element| is the input element that
126 // initiated the auto-fill process.
127 void FillForm(const FormData
& form
,
128 const blink::WebFormControlElement
& element
);
130 // Fills focusable and non-focusable form control elements within |form_element|
131 // with field data from |form_data|.
132 void FillFormIncludingNonFocusableElements(
133 const FormData
& form_data
,
134 const blink::WebFormElement
& form_element
);
136 // Fills all (including disabled, read-only and non-focusable) form control
137 // elements within |form_element| with field data from |form_data|.
138 void FillFormForAllElements(
139 const FormData
& form_data
,
140 const blink::WebFormElement
& form_element
);
142 // Previews the form represented by |form|. |element| is the input element that
143 // initiated the preview process.
144 void PreviewForm(const FormData
& form
,
145 const blink::WebFormControlElement
& element
);
147 // Clears the placeholder values and the auto-filled background for any fields
148 // in the form containing |node| that have been previewed. Resets the
149 // autofilled state of |node| to |was_autofilled|. Returns false if the form is
151 bool ClearPreviewedFormWithElement(const blink::WebFormControlElement
& element
,
152 bool was_autofilled
);
154 // Returns true if |form| has any auto-filled fields.
155 bool FormWithElementIsAutofilled(const blink::WebInputElement
& element
);
157 // Checks if the webpage is empty.
158 // This kind of webpage is considered as empty:
165 // Meta, script and title tags don't influence the emptiness of a webpage.
166 bool IsWebpageEmpty(const blink::WebFrame
* frame
);
168 // This function checks whether the children of |element|
169 // are of the type <script>, <meta>, or <title>.
170 bool IsWebElementEmpty(const blink::WebElement
& element
);
172 // Return a gfx::RectF that is the bounding box for |element| scaled by |scale|.
173 gfx::RectF
GetScaledBoundingBox(float scale
,
174 blink::WebFormControlElement
* element
);
176 } // namespace autofill
178 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_