form_autofill_util.h revision 3551c9c881056c480085172ff9840cab31610854
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
12namespace WebKit {
13class WebDocument;
14class WebFormElement;
15class WebFormControlElement;
16class WebInputElement;
17class WebNode;
18}
19
20namespace autofill {
21
22struct FormData;
23struct FormFieldData;
24struct WebElementDescriptor;
25
26// A bit field mask for form or form element requirements.
27enum RequirementsMask {
28  REQUIRE_NONE         = 0,  // No requirements.
29  REQUIRE_AUTOCOMPLETE = 1,  // Require that autocomplete != off.
30};
31
32// A bit field mask to extract data from WebFormControlElement.
33enum ExtractMask {
34  EXTRACT_NONE        = 0,
35  EXTRACT_VALUE       = 1 << 0,  // Extract value from WebFormControlElement.
36  EXTRACT_OPTION_TEXT = 1 << 1,  // Extract option text from
37                                 // WebFormSelectElement. Only valid when
38                                 // |EXTRACT_VALUE| is set.
39                                 // This is used for form submission where
40                                 // human readable value is captured.
41  EXTRACT_OPTIONS     = 1 << 2,  // Extract options from
42                                 // WebFormControlElement.
43};
44
45// The maximum number of form fields we are willing to parse, due to
46// computational costs.  Several examples of forms with lots of fields that are
47// not relevant to Autofill: (1) the Netflix queue; (2) the Amazon wishlist;
48// (3) router configuration pages; and (4) other configuration pages, e.g. for
49// Google code project settings.
50extern const size_t kMaxParseableFields;
51
52// Returns true if |element| is a text input element.
53bool IsTextInput(const WebKit::WebInputElement* element);
54
55// Returns true if |element| is a select element.
56bool IsSelectElement(const WebKit::WebFormControlElement& element);
57
58// Returns true if |element| is a checkbox or a radio button element.
59bool IsCheckableElement(const WebKit::WebInputElement* element);
60
61// Returns true if |element| is one of the input element types that can be
62// autofilled. {Text, Radiobutton, Checkbox}.
63bool IsAutofillableInputElement(const WebKit::WebInputElement* element);
64
65// Recursively checks whether |node| or any of its children have a non-empty
66// bounding box.
67bool IsWebNodeVisible(const WebKit::WebNode& node);
68
69// Returns the form's |name| attribute if non-empty; otherwise the form's |id|
70// attribute.
71const base::string16 GetFormIdentifier(const WebKit::WebFormElement& form);
72
73// Returns true if the element specified by |click_element| was successfully
74// clicked.
75bool ClickElement(const WebKit::WebDocument& document,
76                  const WebElementDescriptor& element_descriptor);
77
78// Fills |autofillable_elements| with all the auto-fillable form control
79// elements in |form_element|.
80void ExtractAutofillableElements(
81    const WebKit::WebFormElement& form_element,
82    RequirementsMask requirements,
83    std::vector<WebKit::WebFormControlElement>* autofillable_elements);
84
85// Fills out a FormField object from a given WebFormControlElement.
86// |extract_mask|: See the enum ExtractMask above for details.
87void WebFormControlElementToFormField(
88    const WebKit::WebFormControlElement& element,
89    ExtractMask extract_mask,
90    FormFieldData* field);
91
92// Fills |form| with the FormData object corresponding to the |form_element|.
93// If |field| is non-NULL, also fills |field| with the FormField object
94// corresponding to the |form_control_element|.
95// |extract_mask| controls what data is extracted.
96// Returns true if |form| is filled out; it's possible that the |form_element|
97// won't meet the |requirements|.  Also returns false if there are no fields or
98// too many fields in the |form|.
99bool WebFormElementToFormData(
100    const WebKit::WebFormElement& form_element,
101    const WebKit::WebFormControlElement& form_control_element,
102    RequirementsMask requirements,
103    ExtractMask extract_mask,
104    FormData* form,
105    FormFieldData* field);
106
107// Finds the form that contains |element| and returns it in |form|.  Fills
108// |field| with the |FormField| representation for element.
109// Returns false if the form is not found or cannot be serialized.
110bool FindFormAndFieldForInputElement(const WebKit::WebInputElement& element,
111                                     FormData* form,
112                                     FormFieldData* field,
113                                     RequirementsMask requirements);
114
115// Fills the form represented by |form|.  |element| is the input element that
116// initiated the auto-fill process.
117void FillForm(const FormData& form,
118              const WebKit::WebInputElement& element);
119
120// Fills focusable and non-focusable form control elements within |form_element|
121// with field data from |form_data|.
122void FillFormIncludingNonFocusableElements(
123    const FormData& form_data,
124    const WebKit::WebFormElement& form_element);
125
126// Fills all (including disabled, read-only and non-focusable) form control
127// elements within |form_element| with field data from |form_data|.
128void FillFormForAllElements(
129    const FormData& form_data,
130    const WebKit::WebFormElement& form_element);
131
132// Previews the form represented by |form|.  |element| is the input element that
133// initiated the preview process.
134void PreviewForm(const FormData& form,
135                 const WebKit::WebInputElement& element);
136
137// Clears the placeholder values and the auto-filled background for any fields
138// in the form containing |node| that have been previewed.  Resets the
139// autofilled state of |node| to |was_autofilled|.  Returns false if the form is
140// not found.
141bool ClearPreviewedFormWithElement(const WebKit::WebInputElement& element,
142                                   bool was_autofilled);
143
144// Returns true if |form| has any auto-filled fields.
145bool FormWithElementIsAutofilled(const WebKit::WebInputElement& element);
146
147}  // namespace autofill
148
149#endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_FORM_AUTOFILL_UTIL_H_
150