- add sources.
[platform/framework/web/crosswalk.git] / src / components / autofill / content / renderer / form_autofill_util.h
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.
4
5 #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_
7
8 #include <vector>
9
10 #include "base/strings/string16.h"
11
12 namespace WebKit {
13 class WebDocument;
14 class WebElement;
15 class WebFormElement;
16 class WebFormControlElement;
17 class WebFrame;
18 class WebInputElement;
19 class WebNode;
20 }
21
22 namespace autofill {
23
24 struct FormData;
25 struct FormFieldData;
26 struct WebElementDescriptor;
27
28 // A bit field mask for form or form element requirements.
29 enum RequirementsMask {
30   REQUIRE_NONE         = 0,  // No requirements.
31   REQUIRE_AUTOCOMPLETE = 1,  // Require that autocomplete != off.
32 };
33
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.
45 };
46
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;
53
54 // Returns true if |element| is a month input element.
55 bool IsMonthInput(const WebKit::WebInputElement* element);
56
57 // Returns true if |element| is a text input element.
58 bool IsTextInput(const WebKit::WebInputElement* element);
59
60 // Returns true if |element| is a select element.
61 bool IsSelectElement(const WebKit::WebFormControlElement& element);
62
63 // Returns true if |element| is a textarea element.
64 bool IsTextAreaElement(const WebKit::WebFormControlElement& element);
65
66 // Returns true if |element| is a checkbox or a radio button element.
67 bool IsCheckableElement(const WebKit::WebInputElement* element);
68
69 // Returns true if |element| is one of the input element types that can be
70 // autofilled. {Text, Radiobutton, Checkbox}.
71 bool IsAutofillableInputElement(const WebKit::WebInputElement* element);
72
73 // Recursively checks whether |node| or any of its children have a non-empty
74 // bounding box.
75 bool IsWebNodeVisible(const WebKit::WebNode& node);
76
77 // Returns the form's |name| attribute if non-empty; otherwise the form's |id|
78 // attribute.
79 const base::string16 GetFormIdentifier(const WebKit::WebFormElement& form);
80
81 // Returns true if the element specified by |click_element| was successfully
82 // clicked.
83 bool ClickElement(const WebKit::WebDocument& document,
84                   const WebElementDescriptor& element_descriptor);
85
86 // Fills |autofillable_elements| with all the auto-fillable form control
87 // elements in |form_element|.
88 void ExtractAutofillableElements(
89     const WebKit::WebFormElement& form_element,
90     RequirementsMask requirements,
91     std::vector<WebKit::WebFormControlElement>* autofillable_elements);
92
93 // Fills out a FormField object from a given WebFormControlElement.
94 // |extract_mask|: See the enum ExtractMask above for details.
95 void WebFormControlElementToFormField(
96     const WebKit::WebFormControlElement& element,
97     ExtractMask extract_mask,
98     FormFieldData* field);
99
100 // Fills |form| with the FormData object corresponding to the |form_element|.
101 // If |field| is non-NULL, also fills |field| with the FormField object
102 // corresponding to the |form_control_element|.
103 // |extract_mask| controls what data is extracted.
104 // Returns true if |form| is filled out; it's possible that the |form_element|
105 // won't meet the |requirements|.  Also returns false if there are no fields or
106 // too many fields in the |form|.
107 bool WebFormElementToFormData(
108     const WebKit::WebFormElement& form_element,
109     const WebKit::WebFormControlElement& form_control_element,
110     RequirementsMask requirements,
111     ExtractMask extract_mask,
112     FormData* form,
113     FormFieldData* field);
114
115 // Finds the form that contains |element| and returns it in |form|.  Fills
116 // |field| with the |FormField| representation for element.
117 // Returns false if the form is not found or cannot be serialized.
118 bool FindFormAndFieldForInputElement(const WebKit::WebInputElement& element,
119                                      FormData* form,
120                                      FormFieldData* field,
121                                      RequirementsMask requirements);
122
123 // Fills the form represented by |form|.  |element| is the input element that
124 // initiated the auto-fill process.
125 void FillForm(const FormData& form,
126               const WebKit::WebInputElement& element);
127
128 // Fills focusable and non-focusable form control elements within |form_element|
129 // with field data from |form_data|.
130 void FillFormIncludingNonFocusableElements(
131     const FormData& form_data,
132     const WebKit::WebFormElement& form_element);
133
134 // Fills all (including disabled, read-only and non-focusable) form control
135 // elements within |form_element| with field data from |form_data|.
136 void FillFormForAllElements(
137     const FormData& form_data,
138     const WebKit::WebFormElement& form_element);
139
140 // Previews the form represented by |form|.  |element| is the input element that
141 // initiated the preview process.
142 void PreviewForm(const FormData& form,
143                  const WebKit::WebInputElement& element);
144
145 // Clears the placeholder values and the auto-filled background for any fields
146 // in the form containing |node| that have been previewed.  Resets the
147 // autofilled state of |node| to |was_autofilled|.  Returns false if the form is
148 // not found.
149 bool ClearPreviewedFormWithElement(const WebKit::WebInputElement& element,
150                                    bool was_autofilled);
151
152 // Returns true if |form| has any auto-filled fields.
153 bool FormWithElementIsAutofilled(const WebKit::WebInputElement& element);
154
155 // Checks if the webpage is empty.
156 // This kind of webpage is considered as empty:
157 // <html>
158 //    <head>
159 //    <head/>
160 //    <body>
161 //    <body/>
162 // <html/>
163 // Meta, script and title tags don't influence the emptiness of a webpage.
164 bool IsWebpageEmpty(const WebKit::WebFrame* frame);
165
166 // This function checks whether the children of |element|
167 // are of the type <script>, <meta>, or <title>.
168 bool IsWebElementEmpty(const WebKit::WebElement& element);
169
170 }  // namespace autofill
171
172 #endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_