| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/autofill_helper.h" | 5 #include "chrome/renderer/autofill/autofill_agent.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/common/autofill_messages.h" | 8 #include "chrome/common/autofill_messages.h" |
| 9 #include "chrome/common/chrome_constants.h" | 9 #include "chrome/common/chrome_constants.h" |
| 10 #include "chrome/renderer/form_manager.h" | 10 #include "chrome/renderer/autofill/password_autofill_manager.h" |
| 11 #include "chrome/renderer/password_autocomplete_manager.h" | |
| 12 #include "chrome/renderer/render_view.h" | 11 #include "chrome/renderer/render_view.h" |
| 13 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 19 #include "ui/base/keycodes/keyboard_codes.h" | 18 #include "ui/base/keycodes/keyboard_codes.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "webkit/glue/form_data.h" | 20 #include "webkit/glue/form_data.h" |
| 22 #include "webkit/glue/form_field.h" | 21 #include "webkit/glue/form_field.h" |
| 23 #include "webkit/glue/password_form.h" | 22 #include "webkit/glue/password_form.h" |
| 24 | 23 |
| 25 using WebKit::WebFormControlElement; | 24 using WebKit::WebFormControlElement; |
| 26 using WebKit::WebFormElement; | 25 using WebKit::WebFormElement; |
| 27 using WebKit::WebFrame; | 26 using WebKit::WebFrame; |
| 28 using WebKit::WebInputElement; | 27 using WebKit::WebInputElement; |
| 29 using WebKit::WebKeyboardEvent; | 28 using WebKit::WebKeyboardEvent; |
| 30 using WebKit::WebNode; | 29 using WebKit::WebNode; |
| 31 using WebKit::WebString; | 30 using WebKit::WebString; |
| 32 | 31 |
| 33 namespace { | 32 namespace { |
| 34 | 33 |
| 35 // The size above which we stop triggering autofill for an input text field | 34 // The size above which we stop triggering autofill for an input text field |
| 36 // (so to avoid sending long strings through IPC). | 35 // (so to avoid sending long strings through IPC). |
| 37 const size_t kMaximumTextSizeForAutoFill = 1000; | 36 const size_t kMaximumTextSizeForAutoFill = 1000; |
| 38 | 37 |
| 39 } // namespace | 38 } // namespace |
| 40 | 39 |
| 41 AutoFillHelper::AutoFillHelper( | 40 namespace autofill { |
| 41 |
| 42 AutoFillAgent::AutoFillAgent( |
| 42 RenderView* render_view, | 43 RenderView* render_view, |
| 43 PasswordAutocompleteManager* password_autocomplete_manager) | 44 PasswordAutoFillManager* password_autofill_manager) |
| 44 : RenderViewObserver(render_view), | 45 : RenderViewObserver(render_view), |
| 45 password_autocomplete_manager_(password_autocomplete_manager), | 46 password_autofill_manager_(password_autofill_manager), |
| 46 autofill_query_id_(0), | 47 autofill_query_id_(0), |
| 47 autofill_action_(AUTOFILL_NONE), | 48 autofill_action_(AUTOFILL_NONE), |
| 48 display_warning_if_disabled_(false), | 49 display_warning_if_disabled_(false), |
| 49 was_query_node_autofilled_(false), | 50 was_query_node_autofilled_(false), |
| 50 suggestions_clear_index_(-1), | 51 suggestions_clear_index_(-1), |
| 51 suggestions_options_index_(-1), | 52 suggestions_options_index_(-1), |
| 52 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 53 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 53 } | 54 } |
| 54 | 55 |
| 55 AutoFillHelper::~AutoFillHelper() {} | 56 AutoFillAgent::~AutoFillAgent() {} |
| 56 | 57 |
| 57 bool AutoFillHelper::OnMessageReceived(const IPC::Message& message) { | 58 bool AutoFillAgent::OnMessageReceived(const IPC::Message& message) { |
| 58 bool handled = true; | 59 bool handled = true; |
| 59 IPC_BEGIN_MESSAGE_MAP(AutoFillHelper, message) | 60 IPC_BEGIN_MESSAGE_MAP(AutoFillAgent, message) |
| 60 IPC_MESSAGE_HANDLER(AutoFillMsg_SuggestionsReturned, OnSuggestionsReturned) | 61 IPC_MESSAGE_HANDLER(AutoFillMsg_SuggestionsReturned, OnSuggestionsReturned) |
| 61 IPC_MESSAGE_HANDLER(AutoFillMsg_FormDataFilled, OnFormDataFilled) | 62 IPC_MESSAGE_HANDLER(AutoFillMsg_FormDataFilled, OnFormDataFilled) |
| 62 IPC_MESSAGE_UNHANDLED(handled = false) | 63 IPC_MESSAGE_UNHANDLED(handled = false) |
| 63 IPC_END_MESSAGE_MAP() | 64 IPC_END_MESSAGE_MAP() |
| 64 return handled; | 65 return handled; |
| 65 } | 66 } |
| 66 | 67 |
| 67 void AutoFillHelper::DidFinishDocumentLoad(WebKit::WebFrame* frame) { | 68 void AutoFillAgent::DidFinishDocumentLoad(WebKit::WebFrame* frame) { |
| 68 // The document has now been fully loaded. Scan for forms to be sent up to | 69 // The document has now been fully loaded. Scan for forms to be sent up to |
| 69 // the browser. | 70 // the browser. |
| 70 form_manager_.ExtractForms(frame); | 71 form_manager_.ExtractForms(frame); |
| 71 SendForms(frame); | 72 SendForms(frame); |
| 72 } | 73 } |
| 73 | 74 |
| 74 void AutoFillHelper::FrameDetached(WebKit::WebFrame* frame) { | 75 void AutoFillAgent::FrameDetached(WebKit::WebFrame* frame) { |
| 75 form_manager_.ResetFrame(frame); | 76 form_manager_.ResetFrame(frame); |
| 76 } | 77 } |
| 77 | 78 |
| 78 void AutoFillHelper::FrameWillClose(WebKit::WebFrame* frame) { | 79 void AutoFillAgent::FrameWillClose(WebKit::WebFrame* frame) { |
| 79 form_manager_.ResetFrame(frame); | 80 form_manager_.ResetFrame(frame); |
| 80 } | 81 } |
| 81 | 82 |
| 82 void AutoFillHelper::FrameTranslated(WebKit::WebFrame* frame) { | 83 void AutoFillAgent::FrameTranslated(WebKit::WebFrame* frame) { |
| 83 // The page is translated, so try to extract the form data again. | 84 // The page is translated, so try to extract the form data again. |
| 84 DidFinishDocumentLoad(frame); | 85 DidFinishDocumentLoad(frame); |
| 85 } | 86 } |
| 86 | 87 |
| 87 bool AutoFillHelper::InputElementClicked(const WebInputElement& element, | 88 bool AutoFillAgent::InputElementClicked(const WebInputElement& element, |
| 88 bool was_focused, | 89 bool was_focused, |
| 89 bool is_focused) { | 90 bool is_focused) { |
| 90 if (was_focused) | 91 if (was_focused) |
| 91 ShowSuggestions(element, true, false, true); | 92 ShowSuggestions(element, true, false, true); |
| 92 return false; | 93 return false; |
| 93 } | 94 } |
| 94 | 95 |
| 95 void AutoFillHelper::didAcceptAutoFillSuggestion(const WebKit::WebNode& node, | 96 void AutoFillAgent::didAcceptAutoFillSuggestion(const WebKit::WebNode& node, |
| 96 const WebKit::WebString& value, | 97 const WebKit::WebString& value, |
| 97 const WebKit::WebString& label, | 98 const WebKit::WebString& label, |
| 98 int unique_id, | 99 int unique_id, |
| 99 unsigned index) { | 100 unsigned index) { |
| 100 if (suggestions_options_index_ != -1 && | 101 if (suggestions_options_index_ != -1 && |
| 101 index == static_cast<unsigned>(suggestions_options_index_)) { | 102 index == static_cast<unsigned>(suggestions_options_index_)) { |
| 102 // User selected 'AutoFill Options'. | 103 // User selected 'AutoFill Options'. |
| 103 Send(new AutoFillHostMsg_ShowAutoFillDialog(routing_id())); | 104 Send(new AutoFillHostMsg_ShowAutoFillDialog(routing_id())); |
| 104 } else if (suggestions_clear_index_ != -1 && | 105 } else if (suggestions_clear_index_ != -1 && |
| 105 index == static_cast<unsigned>(suggestions_clear_index_)) { | 106 index == static_cast<unsigned>(suggestions_clear_index_)) { |
| 106 // User selected 'Clear form'. | 107 // User selected 'Clear form'. |
| 107 form_manager_.ClearFormWithNode(node); | 108 form_manager_.ClearFormWithNode(node); |
| 108 } else if (!unique_id) { | 109 } else if (!unique_id) { |
| 109 // User selected an Autocomplete entry, so we fill directly. | 110 // User selected an Autocomplete entry, so we fill directly. |
| 110 WebInputElement element = node.toConst<WebInputElement>(); | 111 WebInputElement element = node.toConst<WebInputElement>(); |
| 111 | 112 |
| 112 string16 substring = value; | 113 string16 substring = value; |
| 113 substring = substring.substr(0, element.maxLength()); | 114 substring = substring.substr(0, element.maxLength()); |
| 114 element.setValue(substring); | 115 element.setValue(substring); |
| 115 | 116 |
| 116 WebFrame* webframe = node.document().frame(); | 117 WebFrame* webframe = node.document().frame(); |
| 117 if (webframe) | 118 if (webframe) |
| 118 webframe->notifiyPasswordListenerOfAutocomplete(element); | 119 webframe->notifiyPasswordListenerOfAutocomplete(element); |
| 119 } else { | 120 } else { |
| 120 // Fill the values for the whole form. | 121 // Fill the values for the whole form. |
| 121 FillAutoFillFormData(node, unique_id, AUTOFILL_FILL); | 122 FillAutoFillFormData(node, unique_id, AUTOFILL_FILL); |
| 122 } | 123 } |
| 123 | 124 |
| 124 suggestions_clear_index_ = -1; | 125 suggestions_clear_index_ = -1; |
| 125 suggestions_options_index_ = -1; | 126 suggestions_options_index_ = -1; |
| 126 } | 127 } |
| 127 | 128 |
| 128 void AutoFillHelper::didSelectAutoFillSuggestion(const WebKit::WebNode& node, | 129 void AutoFillAgent::didSelectAutoFillSuggestion(const WebKit::WebNode& node, |
| 129 const WebKit::WebString& value, | 130 const WebKit::WebString& value, |
| 130 const WebKit::WebString& label, | 131 const WebKit::WebString& label, |
| 131 int unique_id) { | 132 int unique_id) { |
| 132 DCHECK_GE(unique_id, 0); | 133 DCHECK_GE(unique_id, 0); |
| 133 if (password_autocomplete_manager_->DidSelectAutoFillSuggestion(node, value)) | 134 if (password_autofill_manager_->DidSelectAutoFillSuggestion(node, value)) |
| 134 return; | 135 return; |
| 135 | 136 |
| 136 didClearAutoFillSelection(node); | 137 didClearAutoFillSelection(node); |
| 137 FillAutoFillFormData(node, unique_id, AUTOFILL_PREVIEW); | 138 FillAutoFillFormData(node, unique_id, AUTOFILL_PREVIEW); |
| 138 } | 139 } |
| 139 | 140 |
| 140 void AutoFillHelper::didClearAutoFillSelection(const WebKit::WebNode& node) { | 141 void AutoFillAgent::didClearAutoFillSelection(const WebKit::WebNode& node) { |
| 141 form_manager_.ClearPreviewedFormWithNode(node, was_query_node_autofilled_); | 142 form_manager_.ClearPreviewedFormWithNode(node, was_query_node_autofilled_); |
| 142 } | 143 } |
| 143 | 144 |
| 144 void AutoFillHelper::removeAutocompleteSuggestion( | 145 void AutoFillAgent::removeAutocompleteSuggestion( |
| 145 const WebKit::WebString& name, | 146 const WebKit::WebString& name, |
| 146 const WebKit::WebString& value) { | 147 const WebKit::WebString& value) { |
| 147 // The index of clear & options will have shifted down. | 148 // The index of clear & options will have shifted down. |
| 148 if (suggestions_clear_index_ != -1) | 149 if (suggestions_clear_index_ != -1) |
| 149 suggestions_clear_index_--; | 150 suggestions_clear_index_--; |
| 150 if (suggestions_options_index_ != -1) | 151 if (suggestions_options_index_ != -1) |
| 151 suggestions_options_index_--; | 152 suggestions_options_index_--; |
| 152 | 153 |
| 153 Send(new AutoFillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value)); | 154 Send(new AutoFillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value)); |
| 154 } | 155 } |
| 155 | 156 |
| 156 void AutoFillHelper::textFieldDidEndEditing( | 157 void AutoFillAgent::textFieldDidEndEditing( |
| 157 const WebKit::WebInputElement& element) { | 158 const WebKit::WebInputElement& element) { |
| 158 password_autocomplete_manager_->TextFieldDidEndEditing(element); | 159 password_autofill_manager_->TextFieldDidEndEditing(element); |
| 159 } | 160 } |
| 160 | 161 |
| 161 void AutoFillHelper::textFieldDidChange( | 162 void AutoFillAgent::textFieldDidChange(const WebKit::WebInputElement& element) { |
| 162 const WebKit::WebInputElement& element) { | |
| 163 // We post a task for doing the AutoFill as the caret position is not set | 163 // We post a task for doing the AutoFill as the caret position is not set |
| 164 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and | 164 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and |
| 165 // it is needed to trigger autofill. | 165 // it is needed to trigger autofill. |
| 166 method_factory_.RevokeAll(); | 166 method_factory_.RevokeAll(); |
| 167 MessageLoop::current()->PostTask( | 167 MessageLoop::current()->PostTask( |
| 168 FROM_HERE, | 168 FROM_HERE, |
| 169 method_factory_.NewRunnableMethod( | 169 method_factory_.NewRunnableMethod( |
| 170 &AutoFillHelper::TextFieldDidChangeImpl, element)); | 170 &AutoFillAgent::TextFieldDidChangeImpl, element)); |
| 171 } | 171 } |
| 172 | 172 |
| 173 void AutoFillHelper::TextFieldDidChangeImpl( | 173 void AutoFillAgent::TextFieldDidChangeImpl( |
| 174 const WebKit::WebInputElement& element) { | 174 const WebKit::WebInputElement& element) { |
| 175 if (password_autocomplete_manager_->TextDidChangeInTextField(element)) | 175 if (password_autofill_manager_->TextDidChangeInTextField(element)) |
| 176 return; | 176 return; |
| 177 | 177 |
| 178 ShowSuggestions(element, false, true, false); | 178 ShowSuggestions(element, false, true, false); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void AutoFillHelper::textFieldDidReceiveKeyDown( | 181 void AutoFillAgent::textFieldDidReceiveKeyDown( |
| 182 const WebKit::WebInputElement& element, | 182 const WebKit::WebInputElement& element, |
| 183 const WebKit::WebKeyboardEvent& event) { | 183 const WebKit::WebKeyboardEvent& event) { |
| 184 if (password_autocomplete_manager_->TextFieldHandlingKeyDown(element, event)) | 184 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) |
| 185 return; | 185 return; |
| 186 | 186 |
| 187 if (event.windowsKeyCode == ui::VKEY_DOWN || | 187 if (event.windowsKeyCode == ui::VKEY_DOWN || |
| 188 event.windowsKeyCode == ui::VKEY_UP) | 188 event.windowsKeyCode == ui::VKEY_UP) |
| 189 ShowSuggestions(element, true, true, true); | 189 ShowSuggestions(element, true, true, true); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void AutoFillHelper::OnSuggestionsReturned( | 192 void AutoFillAgent::OnSuggestionsReturned(int query_id, |
| 193 int query_id, | 193 const std::vector<string16>& values, |
| 194 const std::vector<string16>& values, | 194 const std::vector<string16>& labels, |
| 195 const std::vector<string16>& labels, | 195 const std::vector<string16>& icons, |
| 196 const std::vector<string16>& icons, | 196 const std::vector<int>& unique_ids) { |
| 197 const std::vector<int>& unique_ids) { | |
| 198 WebKit::WebView* web_view = render_view()->webview(); | 197 WebKit::WebView* web_view = render_view()->webview(); |
| 199 if (!web_view || query_id != autofill_query_id_) | 198 if (!web_view || query_id != autofill_query_id_) |
| 200 return; | 199 return; |
| 201 | 200 |
| 202 if (values.empty()) { | 201 if (values.empty()) { |
| 203 // No suggestions, any popup currently showing is obsolete. | 202 // No suggestions, any popup currently showing is obsolete. |
| 204 web_view->hidePopups(); | 203 web_view->hidePopups(); |
| 205 return; | 204 return; |
| 206 } | 205 } |
| 207 | 206 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 258 |
| 260 // Send to WebKit for display. | 259 // Send to WebKit for display. |
| 261 if (!v.empty() && autofill_query_node_.hasNonEmptyBoundingBox()) { | 260 if (!v.empty() && autofill_query_node_.hasNonEmptyBoundingBox()) { |
| 262 web_view->applyAutoFillSuggestions( | 261 web_view->applyAutoFillSuggestions( |
| 263 autofill_query_node_, v, l, i, ids, separator_index); | 262 autofill_query_node_, v, l, i, ids, separator_index); |
| 264 } | 263 } |
| 265 | 264 |
| 266 Send(new AutoFillHostMsg_DidShowAutoFillSuggestions(routing_id())); | 265 Send(new AutoFillHostMsg_DidShowAutoFillSuggestions(routing_id())); |
| 267 } | 266 } |
| 268 | 267 |
| 269 void AutoFillHelper::OnFormDataFilled( | 268 void AutoFillAgent::OnFormDataFilled(int query_id, |
| 270 int query_id, const webkit_glue::FormData& form) { | 269 const webkit_glue::FormData& form) { |
| 271 if (!render_view()->webview() || query_id != autofill_query_id_) | 270 if (!render_view()->webview() || query_id != autofill_query_id_) |
| 272 return; | 271 return; |
| 273 | 272 |
| 274 switch (autofill_action_) { | 273 switch (autofill_action_) { |
| 275 case AUTOFILL_FILL: | 274 case AUTOFILL_FILL: |
| 276 form_manager_.FillForm(form, autofill_query_node_); | 275 form_manager_.FillForm(form, autofill_query_node_); |
| 277 break; | 276 break; |
| 278 case AUTOFILL_PREVIEW: | 277 case AUTOFILL_PREVIEW: |
| 279 form_manager_.PreviewForm(form, autofill_query_node_); | 278 form_manager_.PreviewForm(form, autofill_query_node_); |
| 280 break; | 279 break; |
| 281 default: | 280 default: |
| 282 NOTREACHED(); | 281 NOTREACHED(); |
| 283 } | 282 } |
| 284 autofill_action_ = AUTOFILL_NONE; | 283 autofill_action_ = AUTOFILL_NONE; |
| 285 Send(new AutoFillHostMsg_DidFillAutoFillFormData(routing_id())); | 284 Send(new AutoFillHostMsg_DidFillAutoFillFormData(routing_id())); |
| 286 } | 285 } |
| 287 | 286 |
| 288 void AutoFillHelper::ShowSuggestions(const WebInputElement& element, | 287 void AutoFillAgent::ShowSuggestions(const WebInputElement& element, |
| 289 bool autofill_on_empty_values, | 288 bool autofill_on_empty_values, |
| 290 bool requires_caret_at_end, | 289 bool requires_caret_at_end, |
| 291 bool display_warning_if_disabled) { | 290 bool display_warning_if_disabled) { |
| 292 if (!element.isEnabledFormControl() || !element.isTextField() || | 291 if (!element.isEnabledFormControl() || !element.isTextField() || |
| 293 element.isPasswordField() || element.isReadOnly() || | 292 element.isPasswordField() || element.isReadOnly() || |
| 294 !element.autoComplete()) | 293 !element.autoComplete()) |
| 295 return; | 294 return; |
| 296 | 295 |
| 297 // If the field has no name, then we won't have values. | 296 // If the field has no name, then we won't have values. |
| 298 if (element.nameForAutofill().isEmpty()) | 297 if (element.nameForAutofill().isEmpty()) |
| 299 return; | 298 return; |
| 300 | 299 |
| 301 // Don't attempt to autofill with values that are too large. | 300 // Don't attempt to autofill with values that are too large. |
| 302 WebString value = element.value(); | 301 WebString value = element.value(); |
| 303 if (value.length() > kMaximumTextSizeForAutoFill) | 302 if (value.length() > kMaximumTextSizeForAutoFill) |
| 304 return; | 303 return; |
| 305 | 304 |
| 306 if (!autofill_on_empty_values && value.isEmpty()) | 305 if (!autofill_on_empty_values && value.isEmpty()) |
| 307 return; | 306 return; |
| 308 | 307 |
| 309 if (requires_caret_at_end && | 308 if (requires_caret_at_end && |
| 310 (element.selectionStart() != element.selectionEnd() || | 309 (element.selectionStart() != element.selectionEnd() || |
| 311 element.selectionEnd() != static_cast<int>(value.length()))) | 310 element.selectionEnd() != static_cast<int>(value.length()))) |
| 312 return; | 311 return; |
| 313 | 312 |
| 314 QueryAutoFillSuggestions(element, display_warning_if_disabled); | 313 QueryAutoFillSuggestions(element, display_warning_if_disabled); |
| 315 } | 314 } |
| 316 | 315 |
| 317 void AutoFillHelper::QueryAutoFillSuggestions( | 316 void AutoFillAgent::QueryAutoFillSuggestions(const WebNode& node, |
| 318 const WebNode& node, bool display_warning_if_disabled) { | 317 bool display_warning_if_disabled) { |
| 319 static int query_counter = 0; | 318 static int query_counter = 0; |
| 320 autofill_query_id_ = query_counter++; | 319 autofill_query_id_ = query_counter++; |
| 321 autofill_query_node_ = node; | 320 autofill_query_node_ = node; |
| 322 display_warning_if_disabled_ = display_warning_if_disabled; | 321 display_warning_if_disabled_ = display_warning_if_disabled; |
| 323 | 322 |
| 324 webkit_glue::FormData form; | 323 webkit_glue::FormData form; |
| 325 webkit_glue::FormField field; | 324 webkit_glue::FormField field; |
| 326 if (!FindFormAndFieldForNode(node, &form, &field)) { | 325 if (!FindFormAndFieldForNode(node, &form, &field)) { |
| 327 // If we didn't find the cached form, at least let autocomplete have a shot | 326 // If we didn't find the cached form, at least let autocomplete have a shot |
| 328 // at providing suggestions. | 327 // at providing suggestions. |
| 329 FormManager::WebFormControlElementToFormField( | 328 FormManager::WebFormControlElementToFormField( |
| 330 node.toConst<WebFormControlElement>(), FormManager::EXTRACT_VALUE, | 329 node.toConst<WebFormControlElement>(), FormManager::EXTRACT_VALUE, |
| 331 &field); | 330 &field); |
| 332 } | 331 } |
| 333 | 332 |
| 334 Send(new AutoFillHostMsg_QueryFormFieldAutoFill( | 333 Send(new AutoFillHostMsg_QueryFormFieldAutoFill( |
| 335 routing_id(), autofill_query_id_, form, field)); | 334 routing_id(), autofill_query_id_, form, field)); |
| 336 } | 335 } |
| 337 | 336 |
| 338 void AutoFillHelper::FillAutoFillFormData(const WebNode& node, | 337 void AutoFillAgent::FillAutoFillFormData(const WebNode& node, |
| 339 int unique_id, | 338 int unique_id, |
| 340 AutoFillAction action) { | 339 AutoFillAction action) { |
| 341 static int query_counter = 0; | 340 static int query_counter = 0; |
| 342 autofill_query_id_ = query_counter++; | 341 autofill_query_id_ = query_counter++; |
| 343 | 342 |
| 344 webkit_glue::FormData form; | 343 webkit_glue::FormData form; |
| 345 webkit_glue::FormField field; | 344 webkit_glue::FormField field; |
| 346 if (!FindFormAndFieldForNode(node, &form, &field)) | 345 if (!FindFormAndFieldForNode(node, &form, &field)) |
| 347 return; | 346 return; |
| 348 | 347 |
| 349 autofill_action_ = action; | 348 autofill_action_ = action; |
| 350 was_query_node_autofilled_ = field.is_autofilled(); | 349 was_query_node_autofilled_ = field.is_autofilled(); |
| 351 Send(new AutoFillHostMsg_FillAutoFillFormData( | 350 Send(new AutoFillHostMsg_FillAutoFillFormData( |
| 352 routing_id(), autofill_query_id_, form, field, unique_id)); | 351 routing_id(), autofill_query_id_, form, field, unique_id)); |
| 353 } | 352 } |
| 354 | 353 |
| 355 void AutoFillHelper::SendForms(WebFrame* frame) { | 354 void AutoFillAgent::SendForms(WebFrame* frame) { |
| 356 // TODO(jhawkins): Use FormManager once we have strict ordering of form | 355 // TODO(jhawkins): Use FormManager once we have strict ordering of form |
| 357 // control elements in the cache. | 356 // control elements in the cache. |
| 358 WebKit::WebVector<WebFormElement> web_forms; | 357 WebKit::WebVector<WebFormElement> web_forms; |
| 359 frame->forms(web_forms); | 358 frame->forms(web_forms); |
| 360 | 359 |
| 361 std::vector<webkit_glue::FormData> forms; | 360 std::vector<webkit_glue::FormData> forms; |
| 362 for (size_t i = 0; i < web_forms.size(); ++i) { | 361 for (size_t i = 0; i < web_forms.size(); ++i) { |
| 363 const WebFormElement& web_form = web_forms[i]; | 362 const WebFormElement& web_form = web_forms[i]; |
| 364 | 363 |
| 365 webkit_glue::FormData form; | 364 webkit_glue::FormData form; |
| 366 if (FormManager::WebFormElementToFormData( | 365 if (FormManager::WebFormElementToFormData( |
| 367 web_form, FormManager::REQUIRE_NONE, | 366 web_form, FormManager::REQUIRE_NONE, |
| 368 FormManager::EXTRACT_NONE, &form)) { | 367 FormManager::EXTRACT_NONE, &form)) { |
| 369 forms.push_back(form); | 368 forms.push_back(form); |
| 370 } | 369 } |
| 371 } | 370 } |
| 372 | 371 |
| 373 if (!forms.empty()) | 372 if (!forms.empty()) |
| 374 Send(new AutoFillHostMsg_FormsSeen(routing_id(), forms)); | 373 Send(new AutoFillHostMsg_FormsSeen(routing_id(), forms)); |
| 375 } | 374 } |
| 376 | 375 |
| 377 bool AutoFillHelper::FindFormAndFieldForNode(const WebNode& node, | 376 bool AutoFillAgent::FindFormAndFieldForNode(const WebNode& node, |
| 378 webkit_glue::FormData* form, | 377 webkit_glue::FormData* form, |
| 379 webkit_glue::FormField* field) { | 378 webkit_glue::FormField* field) { |
| 380 const WebInputElement& element = node.toConst<WebInputElement>(); | 379 const WebInputElement& element = node.toConst<WebInputElement>(); |
| 381 if (!form_manager_.FindFormWithFormControlElement(element, | 380 if (!form_manager_.FindFormWithFormControlElement(element, |
| 382 FormManager::REQUIRE_NONE, | 381 FormManager::REQUIRE_NONE, |
| 383 form)) | 382 form)) |
| 384 return false; | 383 return false; |
| 385 | 384 |
| 386 FormManager::WebFormControlElementToFormField(element, | 385 FormManager::WebFormControlElementToFormField(element, |
| 387 FormManager::EXTRACT_VALUE, | 386 FormManager::EXTRACT_VALUE, |
| 388 field); | 387 field); |
| 389 | 388 |
| 390 // WebFormControlElementToFormField does not scrape the DOM for the field | 389 // WebFormControlElementToFormField does not scrape the DOM for the field |
| 391 // label, so find the label here. | 390 // label, so find the label here. |
| 392 // TODO(jhawkins): Add form and field identities so we can use the cached form | 391 // TODO(jhawkins): Add form and field identities so we can use the cached form |
| 393 // data in FormManager. | 392 // data in FormManager. |
| 394 field->set_label(FormManager::LabelForElement(element)); | 393 field->set_label(FormManager::LabelForElement(element)); |
| 395 | 394 |
| 396 return true; | 395 return true; |
| 397 } | 396 } |
| 397 |
| 398 } // namespace autofill |
| OLD | NEW |