summaryrefslogtreecommitdiffstats
path: root/Source/WebKit/android/WebCoreSupport/autofill/FormManagerAndroid.h
blob: e84498178351cfe17450f3e02c34058a0109f638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (c) 2010 The Chromium Authors. All rights reserved.
 * Copyright 2010, The Android Open Source Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef FormManagerAndroid_h
#define FormManagerAndroid_h

#include "ChromiumIncludes.h"

#include <map>
#include <vector>

// TODO: This file is taken from chromium/chrome/renderer/form_manager.h and
// customised to use WebCore types rather than WebKit API types. It would be
// nice and would ease future merge pain if the two could be combined.

namespace webkit_glue {
struct FormData;
class FormField;
}  // namespace webkit_glue

namespace WebCore {
class Frame;
class HTMLFormControlElement;
class HTMLFormElement;
class Node;
}

using WebCore::Frame;
using WebCore::HTMLFormControlElement;
using WebCore::HTMLFormElement;
using WebCore::Node;

namespace android {

// Manages the forms in a Document.
class FormManager {
public:
    // A bit field mask for form requirements.
    enum RequirementsMask {
        REQUIRE_NONE         = 0,          // No requirements.
        REQUIRE_AUTOCOMPLETE = 1 << 0,     // Require that autocomplete != off.
        REQUIRE_ENABLED      = 1 << 1,     // Require that disabled attribute is off.
        REQUIRE_EMPTY        = 1 << 2,     // Require that the fields are empty.
    };

    // A bit field mask to extract data from HTMLFormControlElement.
    enum ExtractMask {
        EXTRACT_NONE        = 0,
        EXTRACT_VALUE       = 1 << 0, // Extract value from HTMLFormControlElement.
        EXTRACT_OPTION_TEXT = 1 << 1, // Extract option text from HTMLFormSelectElement. Only valid when |EXTRACT_VALUE| is set. This is used for form submission where humand readable value is captured.
        EXTRACT_OPTIONS     = 1 << 2, // Extract options from HTMLFormControlElement.
    };

    FormManager();
    virtual ~FormManager();

    // Fills out a FormField object from a given HTMLFormControlElement.
    // |extract_mask|: See the enum ExtractMask above for details.
    static void HTMLFormControlElementToFormField(HTMLFormControlElement* element, ExtractMask extract_mask, webkit_glue::FormField* field);

    // Returns the corresponding label for |element|.  WARNING: This method can
    // potentially be very slow.  Do not use during any code paths where the page
    // is loading.
    static string16 LabelForElement(const HTMLFormControlElement& element);

    // Fills out a FormData object from a given WebFormElement.  If |get_values|
    // is true, the fields in |form| will have the values filled out.  Returns
    // true if |form| is filled out; it's possible that |element| won't meet the
    // requirements in |requirements|.  This also returns false if there are no
    // fields in |form|.
    // TODO: Remove the user of this in RenderView and move this to
    // private.
    static bool HTMLFormElementToFormData(HTMLFormElement* element, RequirementsMask requirements, ExtractMask extract_mask, webkit_glue::FormData* form);

    // Scans the DOM in |frame| extracting and storing forms.
    void ExtractForms(Frame* frame);

    // Returns a vector of forms in |frame| that match |requirements|.
    void GetFormsInFrame(const Frame* frame, RequirementsMask requirements, std::vector<webkit_glue::FormData>* forms);

    // Finds the form that contains |element| and returns it in |form|. Returns
    // false if the form is not found.
    bool FindFormWithFormControlElement(HTMLFormControlElement* element, RequirementsMask requirements, webkit_glue::FormData* form);

    // Fills the form represented by |form|.  |form| should have the name set to
    // the name of the form to fill out, and the number of elements and values
    // must match the number of stored elements in the form. |node| is the form
    // control element that initiated the auto-fill process.
    // TODO: Is matching on name alone good enough?  It's possible to
    // store multiple forms with the same names from different frames.
    bool FillForm(const webkit_glue::FormData& form, Node* node);

    // Previews the form represented by |form|.  |node| is the form control element
    // that initiated the preview process. Same conditions as FillForm.
    bool PreviewForm(const webkit_glue::FormData& form, Node* node);

    // Clears the values of all input elements in the form that contains |node|.
    // Returns false if the form is not found.
    bool ClearFormWithNode(Node* node);

    // Clears the placeholder values and the auto-filled background for any fields
    // in the form containing |node| that have been previewed. Resets the
    // autofilled state of |node| to |was_autofilled|. Returns false if the form
    // is not found.
    bool ClearPreviewedFormWithNode(Node* node, bool was_autofilled);

    // Resets the stored set of forms.
    void Reset();

    // Resets the forms for the specified |frame|.
    void ResetFrame(const Frame* frame);

    // Returns true if |form| has any auto-filled fields.
    bool FormWithNodeIsAutoFilled(Node* node);

private:
    // Stores the HTMLFormElement and the form control elements for a form.
    // Original form values are stored so when we clear a form we can reset
    // "select-one" values to their original state.
    struct FormElement;

    // Type for cache of FormElement objects.
    typedef std::vector<FormElement*> FormElementList;

    // The callback type used by ForEachMatchingFormField().
    typedef Callback3<HTMLFormControlElement*, const webkit_glue::FormField*, bool>::Type Callback;

    // Infers corresponding label for |element| from surrounding context in the
    // DOM.  Contents of preceeding <p> tag or preceeding text element found in
    // the form.
    static string16 InferLabelForElement(const HTMLFormControlElement& element);

    // Finds the cached FormElement that contains |node|.
    bool FindCachedFormElementWithNode(Node* node, FormElement** form_element);

    // Uses the data in |form| to find the cached FormElement.
    bool FindCachedFormElement(const webkit_glue::FormData& form, FormElement** form_element);

    // For each field in |data| that matches the corresponding field in |form|
    // and meets the |requirements|, |callback| is called with the actual
    // WebFormControlElement and the FormField data from |form|. The field that
    // matches |node| is not required to be empty if |requirements| includes
    // REQUIRE_EMPTY.  This method owns |callback|.
    void ForEachMatchingFormField(FormElement* form, Node* node, RequirementsMask requirements, const webkit_glue::FormData& data, Callback* callback);

    // A ForEachMatchingFormField() callback that sets |field|'s value using the
    // value in |data|.  This method also sets the autofill attribute, causing the
    // background to be yellow.
    void FillFormField(HTMLFormControlElement* field, const webkit_glue::FormField* data, bool is_initiating_node);

    // A ForEachMatchingFormField() callback that sets |field|'s placeholder value
    // using the value in |data|, causing the test to be greyed-out.  This method
    // also sets the autofill attribute, causing the background to be yellow.
    void PreviewFormField(HTMLFormControlElement* field, const webkit_glue::FormField* data, bool is_initiaiting_node);

    // The cached FormElement objects.
    FormElementList form_elements_;

    DISALLOW_COPY_AND_ASSIGN(FormManager);
};

} // namespace android

#endif  // FormManagerAndroid_h