Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / components / autofill / content / renderer / form_autofill_util.h
blob1be05cdac08433fab3baf5dd2e293ea3d2af413c
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 "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"
16 class GURL;
18 namespace blink {
19 class WebDocument;
20 class WebElement;
21 class WebFormElement;
22 class WebFormControlElement;
23 class WebFrame;
24 class WebInputElement;
25 class WebNode;
28 namespace autofill {
30 struct FormData;
31 struct FormFieldData;
32 struct WebElementDescriptor;
34 // A bit field mask to extract data from WebFormControlElement.
35 enum ExtractMask {
36 EXTRACT_NONE = 0,
37 EXTRACT_VALUE = 1 << 0, // Extract value from WebFormControlElement.
38 EXTRACT_OPTION_TEXT = 1 << 1, // Extract option text from
39 // WebFormSelectElement. Only valid when
40 // |EXTRACT_VALUE| is set.
41 // This is used for form submission where
42 // human readable value is captured.
43 EXTRACT_OPTIONS = 1 << 2, // Extract options from
44 // WebFormControlElement.
47 // The maximum number of form fields we are willing to parse, due to
48 // computational costs. Several examples of forms with lots of fields that are
49 // not relevant to Autofill: (1) the Netflix queue; (2) the Amazon wishlist;
50 // (3) router configuration pages; and (4) other configuration pages, e.g. for
51 // Google code project settings.
52 extern const size_t kMaxParseableFields;
54 // Returns true if |element| is a month input element.
55 bool IsMonthInput(const blink::WebInputElement* element);
57 // Returns true if |element| is a text input element.
58 bool IsTextInput(const blink::WebInputElement* element);
60 // Returns true if |element| is a select element.
61 bool IsSelectElement(const blink::WebFormControlElement& element);
63 // Returns true if |element| is a textarea element.
64 bool IsTextAreaElement(const blink::WebFormControlElement& element);
66 // Returns true if |element| is a checkbox or a radio button element.
67 bool IsCheckableElement(const blink::WebInputElement* element);
69 // Returns true if |element| is one of the input element types that can be
70 // autofilled. {Text, Radiobutton, Checkbox}.
71 bool IsAutofillableInputElement(const blink::WebInputElement* element);
73 // Recursively checks whether |node| or any of its children have a non-empty
74 // bounding box.
75 bool IsWebNodeVisible(const blink::WebNode& node);
77 // Returns the form's |name| attribute if non-empty; otherwise the form's |id|
78 // attribute.
79 const base::string16 GetFormIdentifier(const blink::WebFormElement& form);
81 // Returns all the auto-fillable form control elements in |control_elements|.
82 std::vector<blink::WebFormControlElement> ExtractAutofillableElementsFromSet(
83 const blink::WebVector<blink::WebFormControlElement>& control_elements);
85 // Returns all the auto-fillable form control elements in |form_element|.
86 std::vector<blink::WebFormControlElement> ExtractAutofillableElementsInForm(
87 const blink::WebFormElement& form_element);
89 // Fills out a FormField object from a given WebFormControlElement.
90 // |extract_mask|: See the enum ExtractMask above for details.
91 void WebFormControlElementToFormField(
92 const blink::WebFormControlElement& element,
93 ExtractMask extract_mask,
94 FormFieldData* field);
96 // Fills |form| with the FormData object corresponding to the |form_element|.
97 // If |field| is non-NULL, also fills |field| with the FormField object
98 // corresponding to the |form_control_element|. |extract_mask| controls what
99 // data is extracted. Returns true if |form| is filled out. Also returns false
100 // if there are no fields or too many fields in the |form|.
101 bool WebFormElementToFormData(
102 const blink::WebFormElement& form_element,
103 const blink::WebFormControlElement& form_control_element,
104 ExtractMask extract_mask,
105 FormData* form,
106 FormFieldData* field);
108 // Get all form control elements from |elements| that are not part of a form.
109 // If |fieldsets| is not NULL, also append the fieldsets encountered that are
110 // not part of a form.
111 std::vector<blink::WebFormControlElement>
112 GetUnownedAutofillableFormFieldElements(
113 const blink::WebElementCollection& elements,
114 std::vector<blink::WebElement>* fieldsets);
116 // Fills |form| with the form data derived from |fieldsets|, |control_elements|
117 // and |origin|. If |field| is non-NULL, fill it with the FormField
118 // representation for |element|.
119 // |extract_mask| usage and the return value are the same as
120 // WebFormElementToFormData() above.
121 bool UnownedFormElementsAndFieldSetsToFormData(
122 const std::vector<blink::WebElement>& fieldsets,
123 const std::vector<blink::WebFormControlElement>& control_elements,
124 const blink::WebFormControlElement* element,
125 const blink::WebDocument& document,
126 ExtractMask extract_mask,
127 FormData* form,
128 FormFieldData* field);
130 // Finds the form that contains |element| and returns it in |form|. If |field|
131 // is non-NULL, fill it with the FormField representation for |element|.
132 // Returns false if the form is not found or cannot be serialized.
133 bool FindFormAndFieldForFormControlElement(
134 const blink::WebFormControlElement& element,
135 FormData* form,
136 FormFieldData* field);
138 // Fills the form represented by |form|. |element| is the input element that
139 // initiated the auto-fill process.
140 void FillForm(const FormData& form,
141 const blink::WebFormControlElement& element);
143 // Fills focusable and non-focusable form control elements within |form_element|
144 // with field data from |form_data|.
145 void FillFormIncludingNonFocusableElements(
146 const FormData& form_data,
147 const blink::WebFormElement& form_element);
149 // Previews the form represented by |form|. |element| is the input element that
150 // initiated the preview process.
151 void PreviewForm(const FormData& form,
152 const blink::WebFormControlElement& element);
154 // Clears the placeholder values and the auto-filled background for any fields
155 // in the form containing |node| that have been previewed. Resets the
156 // autofilled state of |node| to |was_autofilled|. Returns false if the form is
157 // not found.
158 bool ClearPreviewedFormWithElement(const blink::WebFormControlElement& element,
159 bool was_autofilled);
161 // Checks if the webpage is empty.
162 // This kind of webpage is considered as empty:
163 // <html>
164 // <head>
165 // <head/>
166 // <body>
167 // <body/>
168 // <html/>
169 // Meta, script and title tags don't influence the emptiness of a webpage.
170 bool IsWebpageEmpty(const blink::WebFrame* frame);
172 // This function checks whether the children of |element|
173 // are of the type <script>, <meta>, or <title>.
174 bool IsWebElementEmpty(const blink::WebElement& element);
176 // Return a gfx::RectF that is the bounding box for |element| scaled by |scale|.
177 gfx::RectF GetScaledBoundingBox(float scale, blink::WebElement* element);
179 } // namespace autofill
181 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_