[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7819208 | 2011-01-29 05:43:44 | [diff] [blame] | 5 | #ifndef CHROME_RENDERER_AUTOFILL_FORM_MANAGER_H_ |
| 6 | #define CHROME_RENDERER_AUTOFILL_FORM_MANAGER_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 8 | |
| 9 | #include <map> |
| 10 | #include <vector> |
| 11 | |
[email protected] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 13 | #include "base/memory/scoped_vector.h" |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 14 | #include "base/string16.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 16 | |
| 17 | namespace webkit_glue { |
| 18 | struct FormData; |
[email protected] | e2aaa81 | 2011-03-08 18:46:04 | [diff] [blame] | 19 | struct FormField; |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 20 | } // namespace webkit_glue |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 21 | |
| 22 | namespace WebKit { |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 23 | class WebFormControlElement; |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 24 | class WebFrame; |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 25 | } // namespace WebKit |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 26 | |
[email protected] | 7819208 | 2011-01-29 05:43:44 | [diff] [blame] | 27 | namespace autofill { |
| 28 | |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 29 | // Manages the forms in a RenderView. |
| 30 | class FormManager { |
| 31 | public: |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 32 | // A bit field mask for form requirements. |
| 33 | enum RequirementsMask { |
[email protected] | 5af8043 | 2010-11-01 01:25:22 | [diff] [blame] | 34 | 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] | 18ecd00 | 2010-11-16 19:29:55 | [diff] [blame] | 42 | 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] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 51 | }; |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 52 | |
| 53 | FormManager(); |
| 54 | virtual ~FormManager(); |
| 55 | |
[email protected] | b715f7f | 2010-04-05 22:01:04 | [diff] [blame] | 56 | // Fills out a FormField object from a given WebFormControlElement. |
[email protected] | 5af8043 | 2010-11-01 01:25:22 | [diff] [blame] | 57 | // |extract_mask|: See the enum ExtractMask above for details. |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 58 | static void WebFormControlElementToFormField( |
| 59 | const WebKit::WebFormControlElement& element, |
[email protected] | 5af8043 | 2010-11-01 01:25:22 | [diff] [blame] | 60 | ExtractMask extract_mask, |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 61 | webkit_glue::FormField* field); |
| 62 | |
[email protected] | 72eb2da | 2010-04-14 00:31:08 | [diff] [blame] | 63 | // 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] | 1fa7b48 | 2010-10-01 18:28:58 | [diff] [blame] | 68 | // 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] | b715f7f | 2010-04-05 22:01:04 | [diff] [blame] | 74 | // 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] | 5af8043 | 2010-11-01 01:25:22 | [diff] [blame] | 78 | ExtractMask extract_mask, |
[email protected] | b715f7f | 2010-04-05 22:01:04 | [diff] [blame] | 79 | webkit_glue::FormData* form); |
| 80 | |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 81 | // Scans the DOM in |frame| extracting and storing forms. |
[email protected] | 14c2205 | 2010-04-01 01:33:19 | [diff] [blame] | 82 | void ExtractForms(const WebKit::WebFrame* frame); |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 83 | |
[email protected] | b143821 | 2010-04-03 00:30:59 | [diff] [blame] | 84 | // 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] | 8ea2025 | 2010-03-27 02:19:15 | [diff] [blame] | 89 | // Finds the form that contains |element| and returns it in |form|. Returns |
| 90 | // false if the form is not found. |
[email protected] | b143821 | 2010-04-03 00:30:59 | [diff] [blame] | 91 | bool FindFormWithFormControlElement( |
| 92 | const WebKit::WebFormControlElement& element, |
| 93 | RequirementsMask requirements, |
| 94 | webkit_glue::FormData* form); |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 95 | |
[email protected] | 22258c85 | 2011-02-01 23:20:04 | [diff] [blame] | 96 | // Fills the form represented by |form|. |node| is the input element that |
| 97 | // initiated the auto-fill process. |
[email protected] | 2ad69c89 | 2010-06-10 22:00:17 | [diff] [blame] | 98 | bool FillForm(const webkit_glue::FormData& form, const WebKit::WebNode& node); |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 99 | |
[email protected] | 22258c85 | 2011-02-01 23:20:04 | [diff] [blame] | 100 | // Previews the form represented by |form|. |node| is the input element that |
| 101 | // initiated the preview process. |
[email protected] | e7d8b51 | 2010-11-01 16:09:05 | [diff] [blame] | 102 | bool PreviewForm(const webkit_glue::FormData& form, |
| 103 | const WebKit::WebNode &node); |
[email protected] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 104 | |
[email protected] | 631ec05 | 2010-06-13 22:26:30 | [diff] [blame] | 105 | // 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] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 109 | // Clears the placeholder values and the auto-filled background for any fields |
[email protected] | d77ddc806 | 2010-11-24 01:14:06 | [diff] [blame] | 110 | // 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] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 115 | |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 116 | // Resets the stored set of forms. |
| 117 | void Reset(); |
| 118 | |
[email protected] | 174e60e | 2010-05-06 00:21:16 | [diff] [blame] | 119 | // Resets the forms for the specified |frame|. |
| 120 | void ResetFrame(const WebKit::WebFrame* frame); |
| 121 | |
[email protected] | 631ec05 | 2010-06-13 22:26:30 | [diff] [blame] | 122 | // Returns true if |form| has any auto-filled fields. |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 123 | bool FormWithNodeIsAutofilled(const WebKit::WebNode& node); |
[email protected] | 631ec05 | 2010-06-13 22:26:30 | [diff] [blame] | 124 | |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 125 | private: |
[email protected] | e250e49 | 2010-04-06 21:35:36 | [diff] [blame] | 126 | // Stores the WebFormElement and the form control elements for a form. |
[email protected] | aa1182d0 | 2010-10-11 04:18:19 | [diff] [blame] | 127 | // Original form values are stored so when we clear a form we can reset |
[email protected] | 22258c85 | 2011-02-01 23:20:04 | [diff] [blame] | 128 | // <select> elements to their original value. |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 129 | struct FormElement; |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 130 | |
[email protected] | 9503ca35 | 2010-09-29 22:18:48 | [diff] [blame] | 131 | // Type for cache of FormElement objects. |
[email protected] | 22258c85 | 2011-02-01 23:20:04 | [diff] [blame] | 132 | typedef ScopedVector<FormElement> FormElementList; |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 133 | |
[email protected] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 134 | // The callback type used by ForEachMatchingFormField(). |
[email protected] | e7d8b51 | 2010-11-01 16:09:05 | [diff] [blame] | 135 | typedef Callback3<WebKit::WebFormControlElement*, |
| 136 | const webkit_glue::FormField*, |
| 137 | bool>::Type Callback; |
[email protected] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 138 | |
[email protected] | 631ec05 | 2010-06-13 22:26:30 | [diff] [blame] | 139 | // Finds the cached FormElement that contains |node|. |
| 140 | bool FindCachedFormElementWithNode(const WebKit::WebNode& node, |
| 141 | FormElement** form_element); |
| 142 | |
[email protected] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 143 | // 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] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 147 | // 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] | 2ad69c89 | 2010-06-10 22:00:17 | [diff] [blame] | 149 | // 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] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 152 | void ForEachMatchingFormField(FormElement* form, |
[email protected] | 2ad69c89 | 2010-06-10 22:00:17 | [diff] [blame] | 153 | const WebKit::WebNode& node, |
[email protected] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 154 | RequirementsMask requirements, |
[email protected] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 155 | const webkit_glue::FormData& data, |
| 156 | Callback* callback); |
| 157 | |
| 158 | // A ForEachMatchingFormField() callback that sets |field|'s value using the |
[email protected] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 159 | // value in |data|. This method also sets the autofill attribute, causing the |
| 160 | // background to be yellow. |
[email protected] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 161 | void FillFormField(WebKit::WebFormControlElement* field, |
[email protected] | e7d8b51 | 2010-11-01 16:09:05 | [diff] [blame] | 162 | const webkit_glue::FormField* data, |
| 163 | bool is_initiating_node); |
[email protected] | 94d98bf | 2010-05-28 04:47:38 | [diff] [blame] | 164 | |
[email protected] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 165 | // 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] | e7d8b51 | 2010-11-01 16:09:05 | [diff] [blame] | 169 | const webkit_glue::FormField* data, |
| 170 | bool is_initiating_node); |
[email protected] | 18ca9a6b | 2010-06-02 19:05:18 | [diff] [blame] | 171 | |
[email protected] | 9503ca35 | 2010-09-29 22:18:48 | [diff] [blame] | 172 | // The cached FormElement objects. |
| 173 | FormElementList form_elements_; |
[email protected] | 3a60d23 | 2010-02-05 01:30:49 | [diff] [blame] | 174 | |
| 175 | DISALLOW_COPY_AND_ASSIGN(FormManager); |
| 176 | }; |
| 177 | |
[email protected] | 7819208 | 2011-01-29 05:43:44 | [diff] [blame] | 178 | } // namespace autofill |
| 179 | |
| 180 | #endif // CHROME_RENDERER_AUTOFILL_FORM_MANAGER_H_ |