blob: 245dd975c2029d6e4cda9a14f50610fc1a51c26b [file] [log] [blame]
[email protected]663bd9e2011-03-21 01:07:011// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]3a60d232010-02-05 01:30:492// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]78192082011-01-29 05:43:445#ifndef CHROME_RENDERER_AUTOFILL_FORM_MANAGER_H_
6#define CHROME_RENDERER_AUTOFILL_FORM_MANAGER_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]3a60d232010-02-05 01:30:498
9#include <map>
10#include <vector>
11
[email protected]94d98bf2010-05-28 04:47:3812#include "base/callback.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_vector.h"
[email protected]3a60d232010-02-05 01:30:4914#include "base/string16.h"
[email protected]8bd0fe62011-01-17 06:44:3715#include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h"
[email protected]14c22052010-04-01 01:33:1916
17namespace webkit_glue {
18struct FormData;
[email protected]e2aaa812011-03-08 18:46:0419struct FormField;
[email protected]14c22052010-04-01 01:33:1920} // namespace webkit_glue
[email protected]3a60d232010-02-05 01:30:4921
22namespace WebKit {
[email protected]14c22052010-04-01 01:33:1923class WebFormControlElement;
[email protected]3a60d232010-02-05 01:30:4924class WebFrame;
[email protected]14c22052010-04-01 01:33:1925} // namespace WebKit
[email protected]3a60d232010-02-05 01:30:4926
[email protected]78192082011-01-29 05:43:4427namespace autofill {
28
[email protected]3a60d232010-02-05 01:30:4929// Manages the forms in a RenderView.
30class FormManager {
31 public:
[email protected]14c22052010-04-01 01:33:1932 // A bit field mask for form requirements.
33 enum RequirementsMask {
[email protected]5af80432010-11-01 01:25:2234 REQUIRE_NONE = 0, // No requirements.
35 REQUIRE_AUTOCOMPLETE = 1 << 0, // Require that autocomplete != off.
36 REQUIRE_ENABLED = 1 << 1, // Require that disabled attribute is off.
37 REQUIRE_EMPTY = 1 << 2, // Require that the fields are empty.
38 };
39
40 // A bit field mask to extract data from WebFormControlElement.
41 enum ExtractMask {
[email protected]18ecd002010-11-16 19:29:5542 EXTRACT_NONE = 0,
43 EXTRACT_VALUE = 1 << 0, // Extract value from WebFormControlElement.
44 EXTRACT_OPTION_TEXT = 1 << 1, // Extract option text from
45 // WebFormSelectElement. Only valid when
46 // |EXTRACT_VALUE| is set.
47 // This is used for form submission where
48 // human readable value is captured.
49 EXTRACT_OPTIONS = 1 << 2, // Extract options from
50 // WebFormControlElement.
[email protected]14c22052010-04-01 01:33:1951 };
[email protected]3a60d232010-02-05 01:30:4952
53 FormManager();
54 virtual ~FormManager();
55
[email protected]b715f7f2010-04-05 22:01:0456 // Fills out a FormField object from a given WebFormControlElement.
[email protected]5af80432010-11-01 01:25:2257 // |extract_mask|: See the enum ExtractMask above for details.
[email protected]14c22052010-04-01 01:33:1958 static void WebFormControlElementToFormField(
59 const WebKit::WebFormControlElement& element,
[email protected]5af80432010-11-01 01:25:2260 ExtractMask extract_mask,
[email protected]14c22052010-04-01 01:33:1961 webkit_glue::FormField* field);
62
[email protected]72eb2da2010-04-14 00:31:0863 // Returns the corresponding label for |element|. WARNING: This method can
64 // potentially be very slow. Do not use during any code paths where the page
65 // is loading.
66 static string16 LabelForElement(const WebKit::WebFormControlElement& element);
67
[email protected]1fa7b482010-10-01 18:28:5868 // Fills out a FormData object from a given WebFormElement. If |get_values|
69 // is true, the fields in |form| will have the values filled out. If
70 // |get_options| is true, the fields in |form will have select options filled
71 // out. Returns true if |form| is filled out; it's possible that |element|
72 // won't meet the requirements in |requirements|. This also returns false if
73 // there are no fields in |form|.
[email protected]b715f7f2010-04-05 22:01:0474 // TODO(jhawkins): Remove the user of this in RenderView and move this to
75 // private.
76 static bool WebFormElementToFormData(const WebKit::WebFormElement& element,
77 RequirementsMask requirements,
[email protected]5af80432010-11-01 01:25:2278 ExtractMask extract_mask,
[email protected]b715f7f2010-04-05 22:01:0479 webkit_glue::FormData* form);
80
[email protected]3a60d232010-02-05 01:30:4981 // Scans the DOM in |frame| extracting and storing forms.
[email protected]14c22052010-04-01 01:33:1982 void ExtractForms(const WebKit::WebFrame* frame);
[email protected]3a60d232010-02-05 01:30:4983
[email protected]b1438212010-04-03 00:30:5984 // Returns a vector of forms in |frame| that match |requirements|.
85 void GetFormsInFrame(const WebKit::WebFrame* frame,
86 RequirementsMask requirements,
87 std::vector<webkit_glue::FormData>* forms);
88
[email protected]8ea20252010-03-27 02:19:1589 // Finds the form that contains |element| and returns it in |form|. Returns
90 // false if the form is not found.
[email protected]b1438212010-04-03 00:30:5991 bool FindFormWithFormControlElement(
92 const WebKit::WebFormControlElement& element,
93 RequirementsMask requirements,
94 webkit_glue::FormData* form);
[email protected]3a60d232010-02-05 01:30:4995
[email protected]22258c852011-02-01 23:20:0496 // Fills the form represented by |form|. |node| is the input element that
97 // initiated the auto-fill process.
[email protected]2ad69c892010-06-10 22:00:1798 bool FillForm(const webkit_glue::FormData& form, const WebKit::WebNode& node);
[email protected]3a60d232010-02-05 01:30:4999
[email protected]22258c852011-02-01 23:20:04100 // Previews the form represented by |form|. |node| is the input element that
101 // initiated the preview process.
[email protected]e7d8b512010-11-01 16:09:05102 bool PreviewForm(const webkit_glue::FormData& form,
103 const WebKit::WebNode &node);
[email protected]18ca9a6b2010-06-02 19:05:18104
[email protected]631ec052010-06-13 22:26:30105 // Clears the values of all input elements in the form that contains |node|.
106 // Returns false if the form is not found.
107 bool ClearFormWithNode(const WebKit::WebNode& node);
108
[email protected]18ca9a6b2010-06-02 19:05:18109 // Clears the placeholder values and the auto-filled background for any fields
[email protected]d77ddc8062010-11-24 01:14:06110 // in the form containing |node| that have been previewed. Resets the
111 // autofilled state of |node| to |was_autofilled|. Returns false if the form
112 // is not found.
113 bool ClearPreviewedFormWithNode(const WebKit::WebNode& node,
114 bool was_autofilled);
[email protected]18ca9a6b2010-06-02 19:05:18115
[email protected]3a60d232010-02-05 01:30:49116 // Resets the stored set of forms.
117 void Reset();
118
[email protected]174e60e2010-05-06 00:21:16119 // Resets the forms for the specified |frame|.
120 void ResetFrame(const WebKit::WebFrame* frame);
121
[email protected]631ec052010-06-13 22:26:30122 // Returns true if |form| has any auto-filled fields.
[email protected]663bd9e2011-03-21 01:07:01123 bool FormWithNodeIsAutofilled(const WebKit::WebNode& node);
[email protected]631ec052010-06-13 22:26:30124
[email protected]3a60d232010-02-05 01:30:49125 private:
[email protected]e250e492010-04-06 21:35:36126 // Stores the WebFormElement and the form control elements for a form.
[email protected]aa1182d02010-10-11 04:18:19127 // Original form values are stored so when we clear a form we can reset
[email protected]22258c852011-02-01 23:20:04128 // <select> elements to their original value.
[email protected]38e08982010-10-22 17:28:43129 struct FormElement;
[email protected]3a60d232010-02-05 01:30:49130
[email protected]9503ca352010-09-29 22:18:48131 // Type for cache of FormElement objects.
[email protected]22258c852011-02-01 23:20:04132 typedef ScopedVector<FormElement> FormElementList;
[email protected]3a60d232010-02-05 01:30:49133
[email protected]94d98bf2010-05-28 04:47:38134 // The callback type used by ForEachMatchingFormField().
[email protected]e7d8b512010-11-01 16:09:05135 typedef Callback3<WebKit::WebFormControlElement*,
136 const webkit_glue::FormField*,
137 bool>::Type Callback;
[email protected]94d98bf2010-05-28 04:47:38138
[email protected]631ec052010-06-13 22:26:30139 // Finds the cached FormElement that contains |node|.
140 bool FindCachedFormElementWithNode(const WebKit::WebNode& node,
141 FormElement** form_element);
142
[email protected]94d98bf2010-05-28 04:47:38143 // Uses the data in |form| to find the cached FormElement.
144 bool FindCachedFormElement(const webkit_glue::FormData& form,
145 FormElement** form_element);
146
[email protected]18ca9a6b2010-06-02 19:05:18147 // For each field in |data| that matches the corresponding field in |form|
148 // and meets the |requirements|, |callback| is called with the actual
[email protected]2ad69c892010-06-10 22:00:17149 // WebFormControlElement and the FormField data from |form|. The field that
150 // matches |node| is not required to be empty if |requirements| includes
151 // REQUIRE_EMPTY. This method owns |callback|.
[email protected]94d98bf2010-05-28 04:47:38152 void ForEachMatchingFormField(FormElement* form,
[email protected]2ad69c892010-06-10 22:00:17153 const WebKit::WebNode& node,
[email protected]18ca9a6b2010-06-02 19:05:18154 RequirementsMask requirements,
[email protected]94d98bf2010-05-28 04:47:38155 const webkit_glue::FormData& data,
156 Callback* callback);
157
158 // A ForEachMatchingFormField() callback that sets |field|'s value using the
[email protected]18ca9a6b2010-06-02 19:05:18159 // value in |data|. This method also sets the autofill attribute, causing the
160 // background to be yellow.
[email protected]94d98bf2010-05-28 04:47:38161 void FillFormField(WebKit::WebFormControlElement* field,
[email protected]e7d8b512010-11-01 16:09:05162 const webkit_glue::FormField* data,
163 bool is_initiating_node);
[email protected]94d98bf2010-05-28 04:47:38164
[email protected]18ca9a6b2010-06-02 19:05:18165 // A ForEachMatchingFormField() callback that sets |field|'s placeholder value
166 // using the value in |data|, causing the test to be greyed-out. This method
167 // also sets the autofill attribute, causing the background to be yellow.
168 void PreviewFormField(WebKit::WebFormControlElement* field,
[email protected]e7d8b512010-11-01 16:09:05169 const webkit_glue::FormField* data,
170 bool is_initiating_node);
[email protected]18ca9a6b2010-06-02 19:05:18171
[email protected]9503ca352010-09-29 22:18:48172 // The cached FormElement objects.
173 FormElementList form_elements_;
[email protected]3a60d232010-02-05 01:30:49174
175 DISALLOW_COPY_AND_ASSIGN(FormManager);
176};
177
[email protected]78192082011-01-29 05:43:44178} // namespace autofill
179
180#endif // CHROME_RENDERER_AUTOFILL_FORM_MANAGER_H_