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