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