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_f.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 // True if this node takes up space in the layout, ie. this node or a descendant
76 // has a non-empty bounding bounding client rect.
78 // TODO(esprehn): This isn't really about visibility, it's about the size.
79 // We should remove this function and just call hasNonEmptyLayoutSize()
81 bool IsWebNodeVisible(const blink::WebNode
& node
);
83 // Returns the form's |name| attribute if non-empty; otherwise the form's |id|
85 const base::string16
GetFormIdentifier(const blink::WebFormElement
& form
);
87 // Returns all the auto-fillable form control elements in |control_elements|.
88 std::vector
<blink::WebFormControlElement
> ExtractAutofillableElementsFromSet(
89 const blink::WebVector
<blink::WebFormControlElement
>& control_elements
);
91 // Returns all the auto-fillable form control elements in |form_element|.
92 std::vector
<blink::WebFormControlElement
> ExtractAutofillableElementsInForm(
93 const blink::WebFormElement
& form_element
);
95 // Fills out a FormField object from a given WebFormControlElement.
96 // |extract_mask|: See the enum ExtractMask above for details.
97 void WebFormControlElementToFormField(
98 const blink::WebFormControlElement
& element
,
99 ExtractMask extract_mask
,
100 FormFieldData
* field
);
102 // Fills |form| with the FormData object corresponding to the |form_element|.
103 // If |field| is non-NULL, also fills |field| with the FormField object
104 // corresponding to the |form_control_element|. |extract_mask| controls what
105 // data is extracted. Returns true if |form| is filled out. Also returns false
106 // if there are no fields or too many fields in the |form|.
107 bool WebFormElementToFormData(
108 const blink::WebFormElement
& form_element
,
109 const blink::WebFormControlElement
& form_control_element
,
110 ExtractMask extract_mask
,
112 FormFieldData
* field
);
114 // Get all form control elements from |elements| that are not part of a form.
115 // If |fieldsets| is not NULL, also append the fieldsets encountered that are
116 // not part of a form.
117 std::vector
<blink::WebFormControlElement
>
118 GetUnownedAutofillableFormFieldElements(
119 const blink::WebElementCollection
& elements
,
120 std::vector
<blink::WebElement
>* fieldsets
);
122 // Fills |form| with the form data derived from |fieldsets|, |control_elements|
123 // and |origin|. If |field| is non-NULL, fill it with the FormField
124 // representation for |element|.
125 // |extract_mask| usage and the return value are the same as
126 // WebFormElementToFormData() above.
127 // This function will return false and not perform any extraction if
128 // |document| does not pertain to checkout.
129 bool UnownedCheckoutFormElementsAndFieldSetsToFormData(
130 const std::vector
<blink::WebElement
>& fieldsets
,
131 const std::vector
<blink::WebFormControlElement
>& control_elements
,
132 const blink::WebFormControlElement
* element
,
133 const blink::WebDocument
& document
,
134 ExtractMask extract_mask
,
136 FormFieldData
* field
);
138 // Same as above, but without the requirement that the elements only be
139 // related to checkout.
140 bool UnownedPasswordFormElementsAndFieldSetsToFormData(
141 const std::vector
<blink::WebElement
>& fieldsets
,
142 const std::vector
<blink::WebFormControlElement
>& control_elements
,
143 const blink::WebFormControlElement
* element
,
144 const blink::WebDocument
& document
,
145 ExtractMask extract_mask
,
147 FormFieldData
* field
);
149 // Finds the form that contains |element| and returns it in |form|. If |field|
150 // is non-NULL, fill it with the FormField representation for |element|.
151 // Returns false if the form is not found or cannot be serialized.
152 bool FindFormAndFieldForFormControlElement(
153 const blink::WebFormControlElement
& element
,
155 FormFieldData
* field
);
157 // Fills the form represented by |form|. |element| is the input element that
158 // initiated the auto-fill process.
159 void FillForm(const FormData
& form
,
160 const blink::WebFormControlElement
& element
);
162 // Fills focusable and non-focusable form control elements within |form_element|
163 // with field data from |form_data|.
164 void FillFormIncludingNonFocusableElements(
165 const FormData
& form_data
,
166 const blink::WebFormElement
& form_element
);
168 // Previews the form represented by |form|. |element| is the input element that
169 // initiated the preview process.
170 void PreviewForm(const FormData
& form
,
171 const blink::WebFormControlElement
& element
);
173 // Clears the placeholder values and the auto-filled background for any fields
174 // in the form containing |node| that have been previewed. Resets the
175 // autofilled state of |node| to |was_autofilled|. Returns false if the form is
177 bool ClearPreviewedFormWithElement(const blink::WebFormControlElement
& element
,
178 bool was_autofilled
);
180 // Checks if the webpage is empty.
181 // This kind of webpage is considered as empty:
188 // Meta, script and title tags don't influence the emptiness of a webpage.
189 bool IsWebpageEmpty(const blink::WebFrame
* frame
);
191 // This function checks whether the children of |element|
192 // are of the type <script>, <meta>, or <title>.
193 bool IsWebElementEmpty(const blink::WebElement
& element
);
195 // Return a gfx::RectF that is the bounding box for |element| scaled by |scale|.
196 gfx::RectF
GetScaledBoundingBox(float scale
, blink::WebElement
* element
);
198 // Previews |suggestion| in |input_element| and highlights the suffix of
199 // |suggestion| not included in the |input_element| text. |input_element| must
200 // not be null. |user_input| should be the text typed by the user into
201 // |input_element|. Note that |user_input| cannot be easily derived from
202 // |input_element| by calling value(), because of http://crbug.com/507714.
203 void PreviewSuggestion(const base::string16
& suggestion
,
204 const base::string16
& user_input
,
205 blink::WebFormControlElement
* input_element
);
207 } // namespace autofill
209 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_