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_helper.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/chrome_constants.h" | 9 #include "chrome/common/chrome_constants.h" |
9 #include "chrome/common/render_messages.h" | |
10 #include "chrome/renderer/form_manager.h" | 10 #include "chrome/renderer/form_manager.h" |
11 #include "chrome/renderer/password_autocomplete_manager.h" | 11 #include "chrome/renderer/password_autocomplete_manager.h" |
12 #include "chrome/renderer/render_view.h" | 12 #include "chrome/renderer/render_view.h" |
13 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
19 #include "ui/base/keycodes/keyboard_codes.h" | 19 #include "ui/base/keycodes/keyboard_codes.h" |
(...skipping 28 matching lines...) Expand all Loading... |
48 display_warning_if_disabled_(false), | 48 display_warning_if_disabled_(false), |
49 was_query_node_autofilled_(false), | 49 was_query_node_autofilled_(false), |
50 suggestions_clear_index_(-1), | 50 suggestions_clear_index_(-1), |
51 suggestions_options_index_(-1), | 51 suggestions_options_index_(-1), |
52 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 52 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
53 } | 53 } |
54 | 54 |
55 bool AutoFillHelper::OnMessageReceived(const IPC::Message& message) { | 55 bool AutoFillHelper::OnMessageReceived(const IPC::Message& message) { |
56 bool handled = true; | 56 bool handled = true; |
57 IPC_BEGIN_MESSAGE_MAP(AutoFillHelper, message) | 57 IPC_BEGIN_MESSAGE_MAP(AutoFillHelper, message) |
58 IPC_MESSAGE_HANDLER(ViewMsg_AutoFillSuggestionsReturned, | 58 IPC_MESSAGE_HANDLER(AutoFillMsg_SuggestionsReturned, OnSuggestionsReturned) |
59 OnSuggestionsReturned) | 59 IPC_MESSAGE_HANDLER(AutoFillMsg_FormDataFilled, OnFormDataFilled) |
60 IPC_MESSAGE_HANDLER(ViewMsg_AutoFillFormDataFilled, OnFormDataFilled) | |
61 IPC_MESSAGE_UNHANDLED(handled = false) | 60 IPC_MESSAGE_UNHANDLED(handled = false) |
62 IPC_END_MESSAGE_MAP() | 61 IPC_END_MESSAGE_MAP() |
63 return handled; | 62 return handled; |
64 } | 63 } |
65 | 64 |
66 void AutoFillHelper::DidFinishDocumentLoad(WebKit::WebFrame* frame) { | 65 void AutoFillHelper::DidFinishDocumentLoad(WebKit::WebFrame* frame) { |
67 // The document has now been fully loaded. Scan for forms to be sent up to | 66 // The document has now been fully loaded. Scan for forms to be sent up to |
68 // the browser. | 67 // the browser. |
69 form_manager_.ExtractForms(frame); | 68 form_manager_.ExtractForms(frame); |
70 SendForms(frame); | 69 SendForms(frame); |
(...skipping 21 matching lines...) Expand all Loading... |
92 } | 91 } |
93 | 92 |
94 void AutoFillHelper::didAcceptAutoFillSuggestion(const WebKit::WebNode& node, | 93 void AutoFillHelper::didAcceptAutoFillSuggestion(const WebKit::WebNode& node, |
95 const WebKit::WebString& value, | 94 const WebKit::WebString& value, |
96 const WebKit::WebString& label, | 95 const WebKit::WebString& label, |
97 int unique_id, | 96 int unique_id, |
98 unsigned index) { | 97 unsigned index) { |
99 if (suggestions_options_index_ != -1 && | 98 if (suggestions_options_index_ != -1 && |
100 index == static_cast<unsigned>(suggestions_options_index_)) { | 99 index == static_cast<unsigned>(suggestions_options_index_)) { |
101 // User selected 'AutoFill Options'. | 100 // User selected 'AutoFill Options'. |
102 Send(new ViewHostMsg_ShowAutoFillDialog(routing_id())); | 101 Send(new AutoFillHostMsg_ShowAutoFillDialog(routing_id())); |
103 } else if (suggestions_clear_index_ != -1 && | 102 } else if (suggestions_clear_index_ != -1 && |
104 index == static_cast<unsigned>(suggestions_clear_index_)) { | 103 index == static_cast<unsigned>(suggestions_clear_index_)) { |
105 // User selected 'Clear form'. | 104 // User selected 'Clear form'. |
106 form_manager_.ClearFormWithNode(node); | 105 form_manager_.ClearFormWithNode(node); |
107 } else if (!unique_id) { | 106 } else if (!unique_id) { |
108 // User selected an Autocomplete entry, so we fill directly. | 107 // User selected an Autocomplete entry, so we fill directly. |
109 WebInputElement element = node.toConst<WebInputElement>(); | 108 WebInputElement element = node.toConst<WebInputElement>(); |
110 | 109 |
111 string16 substring = value; | 110 string16 substring = value; |
112 substring = substring.substr(0, element.maxLength()); | 111 substring = substring.substr(0, element.maxLength()); |
(...skipping 29 matching lines...) Expand all Loading... |
142 | 141 |
143 void AutoFillHelper::removeAutocompleteSuggestion( | 142 void AutoFillHelper::removeAutocompleteSuggestion( |
144 const WebKit::WebString& name, | 143 const WebKit::WebString& name, |
145 const WebKit::WebString& value) { | 144 const WebKit::WebString& value) { |
146 // The index of clear & options will have shifted down. | 145 // The index of clear & options will have shifted down. |
147 if (suggestions_clear_index_ != -1) | 146 if (suggestions_clear_index_ != -1) |
148 suggestions_clear_index_--; | 147 suggestions_clear_index_--; |
149 if (suggestions_options_index_ != -1) | 148 if (suggestions_options_index_ != -1) |
150 suggestions_options_index_--; | 149 suggestions_options_index_--; |
151 | 150 |
152 Send(new ViewHostMsg_RemoveAutocompleteEntry(routing_id(), name, value)); | 151 Send(new AutoFillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value)); |
153 } | 152 } |
154 | 153 |
155 void AutoFillHelper::textFieldDidEndEditing( | 154 void AutoFillHelper::textFieldDidEndEditing( |
156 const WebKit::WebInputElement& element) { | 155 const WebKit::WebInputElement& element) { |
157 password_autocomplete_manager_->TextFieldDidEndEditing(element); | 156 password_autocomplete_manager_->TextFieldDidEndEditing(element); |
158 } | 157 } |
159 | 158 |
160 void AutoFillHelper::textFieldDidChange( | 159 void AutoFillHelper::textFieldDidChange( |
161 const WebKit::WebInputElement& element) { | 160 const WebKit::WebInputElement& element) { |
162 // We post a task for doing the AutoFill as the caret position is not set | 161 // We post a task for doing the AutoFill as the caret position is not set |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 suggestions_options_index_ = v.size() - 1; | 254 suggestions_options_index_ = v.size() - 1; |
256 separator_index = values.size(); | 255 separator_index = values.size(); |
257 } | 256 } |
258 | 257 |
259 // Send to WebKit for display. | 258 // Send to WebKit for display. |
260 if (!v.empty() && autofill_query_node_.hasNonEmptyBoundingBox()) { | 259 if (!v.empty() && autofill_query_node_.hasNonEmptyBoundingBox()) { |
261 web_view->applyAutoFillSuggestions( | 260 web_view->applyAutoFillSuggestions( |
262 autofill_query_node_, v, l, i, ids, separator_index); | 261 autofill_query_node_, v, l, i, ids, separator_index); |
263 } | 262 } |
264 | 263 |
265 Send(new ViewHostMsg_DidShowAutoFillSuggestions(routing_id())); | 264 Send(new AutoFillHostMsg_DidShowAutoFillSuggestions(routing_id())); |
266 } | 265 } |
267 | 266 |
268 void AutoFillHelper::OnFormDataFilled( | 267 void AutoFillHelper::OnFormDataFilled( |
269 int query_id, const webkit_glue::FormData& form) { | 268 int query_id, const webkit_glue::FormData& form) { |
270 if (!render_view()->webview() || query_id != autofill_query_id_) | 269 if (!render_view()->webview() || query_id != autofill_query_id_) |
271 return; | 270 return; |
272 | 271 |
273 switch (autofill_action_) { | 272 switch (autofill_action_) { |
274 case AUTOFILL_FILL: | 273 case AUTOFILL_FILL: |
275 form_manager_.FillForm(form, autofill_query_node_); | 274 form_manager_.FillForm(form, autofill_query_node_); |
276 break; | 275 break; |
277 case AUTOFILL_PREVIEW: | 276 case AUTOFILL_PREVIEW: |
278 form_manager_.PreviewForm(form, autofill_query_node_); | 277 form_manager_.PreviewForm(form, autofill_query_node_); |
279 break; | 278 break; |
280 default: | 279 default: |
281 NOTREACHED(); | 280 NOTREACHED(); |
282 } | 281 } |
283 autofill_action_ = AUTOFILL_NONE; | 282 autofill_action_ = AUTOFILL_NONE; |
284 Send(new ViewHostMsg_DidFillAutoFillFormData(routing_id())); | 283 Send(new AutoFillHostMsg_DidFillAutoFillFormData(routing_id())); |
285 } | 284 } |
286 | 285 |
287 void AutoFillHelper::ShowSuggestions(const WebInputElement& element, | 286 void AutoFillHelper::ShowSuggestions(const WebInputElement& element, |
288 bool autofill_on_empty_values, | 287 bool autofill_on_empty_values, |
289 bool requires_caret_at_end, | 288 bool requires_caret_at_end, |
290 bool display_warning_if_disabled) { | 289 bool display_warning_if_disabled) { |
291 if (!element.isEnabledFormControl() || !element.isTextField() || | 290 if (!element.isEnabledFormControl() || !element.isTextField() || |
292 element.isPasswordField() || element.isReadOnly() || | 291 element.isPasswordField() || element.isReadOnly() || |
293 !element.autoComplete()) | 292 !element.autoComplete()) |
294 return; | 293 return; |
(...skipping 28 matching lines...) Expand all Loading... |
323 webkit_glue::FormData form; | 322 webkit_glue::FormData form; |
324 webkit_glue::FormField field; | 323 webkit_glue::FormField field; |
325 if (!FindFormAndFieldForNode(node, &form, &field)) { | 324 if (!FindFormAndFieldForNode(node, &form, &field)) { |
326 // If we didn't find the cached form, at least let autocomplete have a shot | 325 // If we didn't find the cached form, at least let autocomplete have a shot |
327 // at providing suggestions. | 326 // at providing suggestions. |
328 FormManager::WebFormControlElementToFormField( | 327 FormManager::WebFormControlElementToFormField( |
329 node.toConst<WebFormControlElement>(), FormManager::EXTRACT_VALUE, | 328 node.toConst<WebFormControlElement>(), FormManager::EXTRACT_VALUE, |
330 &field); | 329 &field); |
331 } | 330 } |
332 | 331 |
333 Send(new ViewHostMsg_QueryFormFieldAutoFill( | 332 Send(new AutoFillHostMsg_QueryFormFieldAutoFill( |
334 routing_id(), autofill_query_id_, form, field)); | 333 routing_id(), autofill_query_id_, form, field)); |
335 } | 334 } |
336 | 335 |
337 void AutoFillHelper::FillAutoFillFormData(const WebNode& node, | 336 void AutoFillHelper::FillAutoFillFormData(const WebNode& node, |
338 int unique_id, | 337 int unique_id, |
339 AutoFillAction action) { | 338 AutoFillAction action) { |
340 static int query_counter = 0; | 339 static int query_counter = 0; |
341 autofill_query_id_ = query_counter++; | 340 autofill_query_id_ = query_counter++; |
342 | 341 |
343 webkit_glue::FormData form; | 342 webkit_glue::FormData form; |
344 webkit_glue::FormField field; | 343 webkit_glue::FormField field; |
345 if (!FindFormAndFieldForNode(node, &form, &field)) | 344 if (!FindFormAndFieldForNode(node, &form, &field)) |
346 return; | 345 return; |
347 | 346 |
348 autofill_action_ = action; | 347 autofill_action_ = action; |
349 was_query_node_autofilled_ = field.is_autofilled(); | 348 was_query_node_autofilled_ = field.is_autofilled(); |
350 Send(new ViewHostMsg_FillAutoFillFormData( | 349 Send(new AutoFillHostMsg_FillAutoFillFormData( |
351 routing_id(), autofill_query_id_, form, field, unique_id)); | 350 routing_id(), autofill_query_id_, form, field, unique_id)); |
352 } | 351 } |
353 | 352 |
354 void AutoFillHelper::SendForms(WebFrame* frame) { | 353 void AutoFillHelper::SendForms(WebFrame* frame) { |
355 // TODO(jhawkins): Use FormManager once we have strict ordering of form | 354 // TODO(jhawkins): Use FormManager once we have strict ordering of form |
356 // control elements in the cache. | 355 // control elements in the cache. |
357 WebKit::WebVector<WebFormElement> web_forms; | 356 WebKit::WebVector<WebFormElement> web_forms; |
358 frame->forms(web_forms); | 357 frame->forms(web_forms); |
359 | 358 |
360 std::vector<webkit_glue::FormData> forms; | 359 std::vector<webkit_glue::FormData> forms; |
361 for (size_t i = 0; i < web_forms.size(); ++i) { | 360 for (size_t i = 0; i < web_forms.size(); ++i) { |
362 const WebFormElement& web_form = web_forms[i]; | 361 const WebFormElement& web_form = web_forms[i]; |
363 | 362 |
364 webkit_glue::FormData form; | 363 webkit_glue::FormData form; |
365 if (FormManager::WebFormElementToFormData( | 364 if (FormManager::WebFormElementToFormData( |
366 web_form, FormManager::REQUIRE_NONE, | 365 web_form, FormManager::REQUIRE_NONE, |
367 FormManager::EXTRACT_NONE, &form)) { | 366 FormManager::EXTRACT_NONE, &form)) { |
368 forms.push_back(form); | 367 forms.push_back(form); |
369 } | 368 } |
370 } | 369 } |
371 | 370 |
372 if (!forms.empty()) | 371 if (!forms.empty()) |
373 Send(new ViewHostMsg_FormsSeen(routing_id(), forms)); | 372 Send(new AutoFillHostMsg_FormsSeen(routing_id(), forms)); |
374 } | 373 } |
375 | 374 |
376 bool AutoFillHelper::FindFormAndFieldForNode(const WebNode& node, | 375 bool AutoFillHelper::FindFormAndFieldForNode(const WebNode& node, |
377 webkit_glue::FormData* form, | 376 webkit_glue::FormData* form, |
378 webkit_glue::FormField* field) { | 377 webkit_glue::FormField* field) { |
379 const WebInputElement& element = node.toConst<WebInputElement>(); | 378 const WebInputElement& element = node.toConst<WebInputElement>(); |
380 if (!form_manager_.FindFormWithFormControlElement(element, | 379 if (!form_manager_.FindFormWithFormControlElement(element, |
381 FormManager::REQUIRE_NONE, | 380 FormManager::REQUIRE_NONE, |
382 form)) | 381 form)) |
383 return false; | 382 return false; |
384 | 383 |
385 FormManager::WebFormControlElementToFormField(element, | 384 FormManager::WebFormControlElementToFormField(element, |
386 FormManager::EXTRACT_VALUE, | 385 FormManager::EXTRACT_VALUE, |
387 field); | 386 field); |
388 | 387 |
389 // WebFormControlElementToFormField does not scrape the DOM for the field | 388 // WebFormControlElementToFormField does not scrape the DOM for the field |
390 // label, so find the label here. | 389 // label, so find the label here. |
391 // TODO(jhawkins): Add form and field identities so we can use the cached form | 390 // TODO(jhawkins): Add form and field identities so we can use the cached form |
392 // data in FormManager. | 391 // data in FormManager. |
393 field->set_label(FormManager::LabelForElement(element)); | 392 field->set_label(FormManager::LabelForElement(element)); |
394 | 393 |
395 return true; | 394 return true; |
396 } | 395 } |
OLD | NEW |