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 "components/autofill/core/common/autofill_constants.h"
12 #include "third_party/WebKit/public/platform/WebVector.h"
13 #include "third_party/WebKit/public/web/WebElementCollection.h"
14 #include "ui/gfx/geometry/rect.h"
21 class WebFormControlElement
;
24 class WebInputElement
;
32 struct WebElementDescriptor
;
34 // A bit field mask to extract data from WebFormControlElement.
35 // Copied to components/autofill/ios/browser/resources/autofill_controller.js.
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 // Copied to components/autofill/ios/browser/resources/autofill_controller.js.
54 extern const size_t kMaxParseableFields
;
56 // Returns true if |element| is a month input element.
57 bool IsMonthInput(const blink::WebInputElement
* element
);
59 // Returns true if |element| is a text input element.
60 bool IsTextInput(const blink::WebInputElement
* element
);
62 // Returns true if |element| is a select element.
63 bool IsSelectElement(const blink::WebFormControlElement
& element
);
65 // Returns true if |element| is a textarea element.
66 bool IsTextAreaElement(const blink::WebFormControlElement
& element
);
68 // Returns true if |element| is a checkbox or a radio button element.
69 bool IsCheckableElement(const blink::WebInputElement
* element
);
71 // Returns true if |element| is one of the input element types that can be
72 // autofilled. {Text, Radiobutton, Checkbox}.
73 bool IsAutofillableInputElement(const blink::WebInputElement
* element
);
75 // Recursively checks whether |node| or any of its children have a non-empty
77 bool IsWebNodeVisible(const blink::WebNode
& node
);
79 // Returns the form's |name| attribute if non-empty; otherwise the form's |id|
81 const base::string16
GetFormIdentifier(const blink::WebFormElement
& form
);
83 // Returns all the auto-fillable form control elements in |control_elements|.
84 std::vector
<blink::WebFormControlElement
> ExtractAutofillableElementsFromSet(
85 const blink::WebVector
<blink::WebFormControlElement
>& control_elements
);
87 // Returns all the auto-fillable form control elements in |form_element|.
88 std::vector
<blink::WebFormControlElement
> ExtractAutofillableElementsInForm(
89 const blink::WebFormElement
& form_element
);
91 // Fills out a FormField object from a given WebFormControlElement.
92 // |extract_mask|: See the enum ExtractMask above for details.
93 void WebFormControlElementToFormField(
94 const blink::WebFormControlElement
& element
,
95 ExtractMask extract_mask
,
96 FormFieldData
* field
);
98 // Fills |form| with the FormData object corresponding to the |form_element|.
99 // If |field| is non-NULL, also fills |field| with the FormField object
100 // corresponding to the |form_control_element|. |extract_mask| controls what
101 // data is extracted. Returns true if |form| is filled out. Also returns false
102 // if there are no fields or too many fields in the |form|.
103 bool WebFormElementToFormData(
104 const blink::WebFormElement
& form_element
,
105 const blink::WebFormControlElement
& form_control_element
,
106 ExtractMask extract_mask
,
108 FormFieldData
* field
);
110 // Get all form control elements from |elements| that are not part of a form.
111 // If |fieldsets| is not NULL, also append the fieldsets encountered that are
112 // not part of a form.
113 std::vector
<blink::WebFormControlElement
>
114 GetUnownedAutofillableFormFieldElements(
115 const blink::WebElementCollection
& elements
,
116 std::vector
<blink::WebElement
>* fieldsets
);
118 // Fills |form| with the form data derived from |fieldsets|, |control_elements|
119 // and |origin|. If |field| is non-NULL, fill it with the FormField
120 // representation for |element|.
121 // |extract_mask| usage and the return value are the same as
122 // WebFormElementToFormData() above.
123 // This function will return false and not perform any extraction if
124 // |document| does not pertain to checkout.
125 bool UnownedCheckoutFormElementsAndFieldSetsToFormData(
126 const std::vector
<blink::WebElement
>& fieldsets
,
127 const std::vector
<blink::WebFormControlElement
>& control_elements
,
128 const blink::WebFormControlElement
* element
,
129 const blink::WebDocument
& document
,
130 ExtractMask extract_mask
,
132 FormFieldData
* field
);
134 // Same as above, but without the requirement that the elements only be
135 // related to checkout.
136 bool UnownedPasswordFormElementsAndFieldSetsToFormData(
137 const std::vector
<blink::WebElement
>& fieldsets
,
138 const std::vector
<blink::WebFormControlElement
>& control_elements
,
139 const blink::WebFormControlElement
* element
,
140 const blink::WebDocument
& document
,
141 ExtractMask extract_mask
,
143 FormFieldData
* field
);
145 // Finds the form that contains |element| and returns it in |form|. If |field|
146 // is non-NULL, fill it with the FormField representation for |element|.
147 // Returns false if the form is not found or cannot be serialized.
148 bool FindFormAndFieldForFormControlElement(
149 const blink::WebFormControlElement
& element
,
151 FormFieldData
* field
);
153 // Fills the form represented by |form|. |element| is the input element that
154 // initiated the auto-fill process.
155 void FillForm(const FormData
& form
,
156 const blink::WebFormControlElement
& element
);
158 // Fills focusable and non-focusable form control elements within |form_element|
159 // with field data from |form_data|.
160 void FillFormIncludingNonFocusableElements(
161 const FormData
& form_data
,
162 const blink::WebFormElement
& form_element
);
164 // Previews the form represented by |form|. |element| is the input element that
165 // initiated the preview process.
166 void PreviewForm(const FormData
& form
,
167 const blink::WebFormControlElement
& element
);
169 // Clears the placeholder values and the auto-filled background for any fields
170 // in the form containing |node| that have been previewed. Resets the
171 // autofilled state of |node| to |was_autofilled|. Returns false if the form is
173 bool ClearPreviewedFormWithElement(const blink::WebFormControlElement
& element
,
174 bool was_autofilled
);
176 // Checks if the webpage is empty.
177 // This kind of webpage is considered as empty:
184 // Meta, script and title tags don't influence the emptiness of a webpage.
185 bool IsWebpageEmpty(const blink::WebFrame
* frame
);
187 // This function checks whether the children of |element|
188 // are of the type <script>, <meta>, or <title>.
189 bool IsWebElementEmpty(const blink::WebElement
& element
);
191 // Return a gfx::RectF that is the bounding box for |element| scaled by |scale|.
192 gfx::RectF
GetScaledBoundingBox(float scale
, blink::WebElement
* element
);
194 // Previews |suggestion| in |input_element| and highlights the suffix of
195 // |suggestion| not included in the |input_element| text. |input_element| must
196 // not be null. |user_input| should be the text typed by the user into
197 // |input_element|. Note that |user_input| cannot be easily derived from
198 // |input_element| by calling value(), because of http://crbug.com/507714.
199 void PreviewSuggestion(const base::string16
& suggestion
,
200 const base::string16
& user_input
,
201 blink::WebFormControlElement
* input_element
);
203 } // namespace autofill
205 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_