ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / content / renderer / form_autofill_util.h
blobb3a299df1aeed4b62bf5d667346539e6ea07b507
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_
8 #include <vector>
10 #include "base/strings/string16.h"
11 #include "third_party/WebKit/public/platform/WebVector.h"
12 #include "third_party/WebKit/public/web/WebElementCollection.h"
13 #include "ui/gfx/geometry/rect.h"
15 class GURL;
17 namespace blink {
18 class WebDocument;
19 class WebElement;
20 class WebFormElement;
21 class WebFormControlElement;
22 class WebFrame;
23 class WebInputElement;
24 class WebNode;
27 namespace autofill {
29 struct FormData;
30 struct FormFieldData;
31 struct WebElementDescriptor;
33 // A bit field mask for form or form element requirements.
34 enum RequirementsMask {
35 REQUIRE_NONE = 0, // No requirements.
36 REQUIRE_AUTOCOMPLETE = 1, // Require that autocomplete != off.
39 // A bit field mask to extract data from WebFormControlElement.
40 enum ExtractMask {
41 EXTRACT_NONE = 0,
42 EXTRACT_VALUE = 1 << 0, // Extract value from WebFormControlElement.
43 EXTRACT_OPTION_TEXT = 1 << 1, // Extract option text from
44 // WebFormSelectElement. Only valid when
45 // |EXTRACT_VALUE| is set.
46 // This is used for form submission where
47 // human readable value is captured.
48 EXTRACT_OPTIONS = 1 << 2, // Extract options from
49 // WebFormControlElement.
52 // The maximum number of form fields we are willing to parse, due to
53 // computational costs. Several examples of forms with lots of fields that are
54 // not relevant to Autofill: (1) the Netflix queue; (2) the Amazon wishlist;
55 // (3) router configuration pages; and (4) other configuration pages, e.g. for
56 // Google code project settings.
57 extern const size_t kMaxParseableFields;
59 // Returns true if |element| is a month input element.
60 bool IsMonthInput(const blink::WebInputElement* element);
62 // Returns true if |element| is a text input element.
63 bool IsTextInput(const blink::WebInputElement* element);
65 // Returns true if |element| is a select element.
66 bool IsSelectElement(const blink::WebFormControlElement& element);
68 // Returns true if |element| is a textarea element.
69 bool IsTextAreaElement(const blink::WebFormControlElement& element);
71 // Returns true if |element| is a checkbox or a radio button element.
72 bool IsCheckableElement(const blink::WebInputElement* element);
74 // Returns true if |element| is one of the input element types that can be
75 // autofilled. {Text, Radiobutton, Checkbox}.
76 bool IsAutofillableInputElement(const blink::WebInputElement* element);
78 // Recursively checks whether |node| or any of its children have a non-empty
79 // bounding box.
80 bool IsWebNodeVisible(const blink::WebNode& node);
82 // Returns the form's |name| attribute if non-empty; otherwise the form's |id|
83 // attribute.
84 const base::string16 GetFormIdentifier(const blink::WebFormElement& form);
86 // Returns all the auto-fillable form control elements in |control_elements|.
87 std::vector<blink::WebFormControlElement> ExtractAutofillableElementsFromSet(
88 const blink::WebVector<blink::WebFormControlElement>& control_elements,
89 RequirementsMask requirements);
91 // Returns all the auto-fillable form control elements in |form_element|.
92 std::vector<blink::WebFormControlElement> ExtractAutofillableElementsInForm(
93 const blink::WebFormElement& form_element,
94 RequirementsMask requirements);
96 // Fills out a FormField object from a given WebFormControlElement.
97 // |extract_mask|: See the enum ExtractMask above for details.
98 void WebFormControlElementToFormField(
99 const blink::WebFormControlElement& element,
100 ExtractMask extract_mask,
101 FormFieldData* field);
103 // Fills |form| with the FormData object corresponding to the |form_element|.
104 // If |field| is non-NULL, also fills |field| with the FormField object
105 // corresponding to the |form_control_element|.
106 // |extract_mask| controls what data is extracted.
107 // Returns true if |form| is filled out; it's possible that the |form_element|
108 // won't meet the |requirements|. Also returns false if there are no fields or
109 // too many fields in the |form|.
110 bool WebFormElementToFormData(
111 const blink::WebFormElement& form_element,
112 const blink::WebFormControlElement& form_control_element,
113 RequirementsMask requirements,
114 ExtractMask extract_mask,
115 FormData* form,
116 FormFieldData* field);
118 // Get all form control elements from |elements| that are not part of a form.
119 // If |fieldsets| is not NULL, also append the fieldsets encountered that are
120 // not part of a form.
121 std::vector<blink::WebFormControlElement>
122 GetUnownedAutofillableFormFieldElements(
123 const blink::WebElementCollection& elements,
124 std::vector<blink::WebElement>* fieldsets);
126 // Fills |form| with the form data derived from |fieldsets|, |control_elements|
127 // and |origin|. If |field| is non-NULL, fill it with the FormField
128 // representation for |element|.
129 // |extract_mask| usage and the return value are the same as
130 // WebFormElementToFormData() above.
131 bool UnownedFormElementsAndFieldSetsToFormData(
132 const std::vector<blink::WebElement>& fieldsets,
133 const std::vector<blink::WebFormControlElement>& control_elements,
134 const blink::WebFormControlElement* element,
135 const GURL& origin,
136 RequirementsMask requirements,
137 ExtractMask extract_mask,
138 FormData* form,
139 FormFieldData* field);
141 // Finds the form that contains |element| and returns it in |form|. If |field|
142 // is non-NULL, fill it with the FormField representation for |element|.
143 // Returns false if the form is not found or cannot be serialized.
144 bool FindFormAndFieldForFormControlElement(
145 const blink::WebFormControlElement& element,
146 FormData* form,
147 FormFieldData* field,
148 RequirementsMask requirements);
150 // Fills the form represented by |form|. |element| is the input element that
151 // initiated the auto-fill process.
152 void FillForm(const FormData& form,
153 const blink::WebFormControlElement& element);
155 // Fills focusable and non-focusable form control elements within |form_element|
156 // with field data from |form_data|.
157 void FillFormIncludingNonFocusableElements(
158 const FormData& form_data,
159 const blink::WebFormElement& form_element);
161 // Previews the form represented by |form|. |element| is the input element that
162 // initiated the preview process.
163 void PreviewForm(const FormData& form,
164 const blink::WebFormControlElement& element);
166 // Clears the placeholder values and the auto-filled background for any fields
167 // in the form containing |node| that have been previewed. Resets the
168 // autofilled state of |node| to |was_autofilled|. Returns false if the form is
169 // not found.
170 bool ClearPreviewedFormWithElement(const blink::WebFormControlElement& element,
171 bool was_autofilled);
173 // Checks if the webpage is empty.
174 // This kind of webpage is considered as empty:
175 // <html>
176 // <head>
177 // <head/>
178 // <body>
179 // <body/>
180 // <html/>
181 // Meta, script and title tags don't influence the emptiness of a webpage.
182 bool IsWebpageEmpty(const blink::WebFrame* frame);
184 // This function checks whether the children of |element|
185 // are of the type <script>, <meta>, or <title>.
186 bool IsWebElementEmpty(const blink::WebElement& element);
188 // Return a gfx::RectF that is the bounding box for |element| scaled by |scale|.
189 gfx::RectF GetScaledBoundingBox(float scale, blink::WebElement* element);
191 } // namespace autofill
193 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_