OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/autofill_agent.h" | 5 #include "chrome/renderer/autofill/autofill_agent.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/common/autofill_messages.h" | 13 #include "chrome/common/autofill_messages.h" |
14 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
15 #include "chrome/common/form_data.h" | 15 #include "chrome/common/form_data.h" |
16 #include "chrome/common/form_data_predictions.h" | 16 #include "chrome/common/form_data_predictions.h" |
17 #include "chrome/common/form_field_data.h" | 17 #include "chrome/common/form_field_data.h" |
18 #include "chrome/renderer/autofill/form_autofill_util.h" | 18 #include "chrome/renderer/autofill/form_autofill_util.h" |
19 #include "chrome/renderer/autofill/password_autofill_manager.h" | 19 #include "chrome/renderer/autofill/password_autofill_manager.h" |
20 #include "content/public/common/password_form.h" | 20 #include "content/public/common/password_form.h" |
21 #include "content/public/renderer/render_view.h" | 21 #include "content/public/renderer/render_view.h" |
22 #include "grit/chromium_strings.h" | 22 #include "grit/chromium_strings.h" |
23 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
33 #include "ui/base/keycodes/keyboard_codes.h" | 34 #include "ui/base/keycodes/keyboard_codes.h" |
34 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
35 | 36 |
36 using WebKit::WebAutofillClient; | 37 using WebKit::WebAutofillClient; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview, | 151 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview, |
151 OnSetAutofillActionPreview) | 152 OnSetAutofillActionPreview) |
152 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, | 153 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, |
153 OnClearPreviewedForm) | 154 OnClearPreviewedForm) |
154 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, | 155 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, |
155 OnSetNodeText) | 156 OnSetNodeText) |
156 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion, | 157 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion, |
157 OnAcceptDataListSuggestion) | 158 OnAcceptDataListSuggestion) |
158 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion, | 159 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion, |
159 OnAcceptPasswordAutofillSuggestion) | 160 OnAcceptPasswordAutofillSuggestion) |
| 161 IPC_MESSAGE_HANDLER(AutofillMsg_RequestAutocompleteFinished, |
| 162 OnRequestAutocompleteFinished) |
160 IPC_MESSAGE_UNHANDLED(handled = false) | 163 IPC_MESSAGE_UNHANDLED(handled = false) |
161 IPC_END_MESSAGE_MAP() | 164 IPC_END_MESSAGE_MAP() |
162 return handled; | 165 return handled; |
163 } | 166 } |
164 | 167 |
165 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { | 168 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { |
166 // The document has now been fully loaded. Scan for forms to be sent up to | 169 // The document has now been fully loaded. Scan for forms to be sent up to |
167 // the browser. | 170 // the browser. |
168 std::vector<FormData> forms; | 171 std::vector<FormData> forms; |
169 form_cache_.ExtractForms(*frame, &forms); | 172 form_cache_.ExtractForms(*frame, &forms); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 Send(new AutofillHostMsg_HideAutofillPopup(routing_id())); | 207 Send(new AutofillHostMsg_HideAutofillPopup(routing_id())); |
205 } | 208 } |
206 | 209 |
207 void AutofillAgent::DidChangeScrollOffset(WebKit::WebFrame*) { | 210 void AutofillAgent::DidChangeScrollOffset(WebKit::WebFrame*) { |
208 // Any time the scroll offset changes, the page's content moves, so Autofill | 211 // Any time the scroll offset changes, the page's content moves, so Autofill |
209 // popups should be hidden. This is only needed for the new Autofill UI | 212 // popups should be hidden. This is only needed for the new Autofill UI |
210 // because WebKit already knows to hide the old UI when this occurs. | 213 // because WebKit already knows to hide the old UI when this occurs. |
211 Send(new AutofillHostMsg_HideAutofillPopup(routing_id())); | 214 Send(new AutofillHostMsg_HideAutofillPopup(routing_id())); |
212 } | 215 } |
213 | 216 |
| 217 void AutofillAgent::didRequestAutocomplete(WebKit::WebFrame* frame, |
| 218 const WebFormElement& form) { |
| 219 FormData form_data; |
| 220 if (!in_flight_request_form_.isNull() || |
| 221 !WebFormElementToFormData(form, |
| 222 WebFormControlElement(), |
| 223 REQUIRE_AUTOCOMPLETE, |
| 224 EXTRACT_NONE, |
| 225 &form_data, |
| 226 NULL)) { |
| 227 WebFormElement(form).finishRequestAutocomplete( |
| 228 WebFormElement::AutocompleteResultError); |
| 229 return; |
| 230 } |
| 231 |
| 232 // Cancel any pending Autofill requests and hide any currently showing popups. |
| 233 ++autofill_query_id_; |
| 234 HidePopups(); |
| 235 |
| 236 in_flight_request_form_ = form; |
| 237 Send(new AutofillHostMsg_RequestAutocomplete(routing_id(), form_data)); |
| 238 } |
| 239 |
214 bool AutofillAgent::InputElementClicked(const WebInputElement& element, | 240 bool AutofillAgent::InputElementClicked(const WebInputElement& element, |
215 bool was_focused, | 241 bool was_focused, |
216 bool is_focused) { | 242 bool is_focused) { |
217 if (was_focused) | 243 if (was_focused) |
218 ShowSuggestions(element, true, false, true); | 244 ShowSuggestions(element, true, false, true); |
219 | 245 |
220 return false; | 246 return false; |
221 } | 247 } |
222 | 248 |
223 bool AutofillAgent::InputElementLostFocus() { | 249 bool AutofillAgent::InputElementLostFocus() { |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) { | 593 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) { |
568 // We need to make sure this is handled here because the browser process | 594 // We need to make sure this is handled here because the browser process |
569 // skipped it handling because it believed it would be handled here. If it | 595 // skipped it handling because it believed it would be handled here. If it |
570 // isn't handled here then the browser logic needs to be updated. | 596 // isn't handled here then the browser logic needs to be updated. |
571 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion( | 597 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion( |
572 element_, | 598 element_, |
573 value); | 599 value); |
574 DCHECK(handled); | 600 DCHECK(handled); |
575 } | 601 } |
576 | 602 |
| 603 void AutofillAgent::OnRequestAutocompleteFinished( |
| 604 WebFormElement::AutocompleteResult result) { |
| 605 DCHECK(!in_flight_request_form_.isNull()); |
| 606 |
| 607 in_flight_request_form_.finishRequestAutocomplete(result); |
| 608 in_flight_request_form_.reset(); |
| 609 } |
| 610 |
577 void AutofillAgent::ShowSuggestions(const WebInputElement& element, | 611 void AutofillAgent::ShowSuggestions(const WebInputElement& element, |
578 bool autofill_on_empty_values, | 612 bool autofill_on_empty_values, |
579 bool requires_caret_at_end, | 613 bool requires_caret_at_end, |
580 bool display_warning_if_disabled) { | 614 bool display_warning_if_disabled) { |
581 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() || | 615 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() || |
582 element.isPasswordField() || !element.suggestedValue().isEmpty()) | 616 element.isPasswordField() || !element.suggestedValue().isEmpty()) |
583 return; | 617 return; |
584 | 618 |
585 // Don't attempt to autofill with values that are too large or if filling | 619 // Don't attempt to autofill with values that are too large or if filling |
586 // criteria are not met. | 620 // criteria are not met. |
587 WebString value = element.editingValue(); | 621 WebString value = element.editingValue(); |
588 if (value.length() > kMaximumTextSizeForAutofill || | 622 if (value.length() > kMaximumTextSizeForAutofill || |
589 (!autofill_on_empty_values && value.isEmpty()) || | 623 (!autofill_on_empty_values && value.isEmpty()) || |
590 (requires_caret_at_end && | 624 (requires_caret_at_end && |
591 (element.selectionStart() != element.selectionEnd() || | 625 (element.selectionStart() != element.selectionEnd() || |
592 element.selectionEnd() != static_cast<int>(value.length())))) { | 626 element.selectionEnd() != static_cast<int>(value.length())))) { |
593 // Any popup currently showing is obsolete. | 627 // Any popup currently showing is obsolete. |
594 WebKit::WebView* web_view = render_view()->GetWebView(); | 628 HidePopups(); |
595 if (web_view) | |
596 web_view->hidePopups(); | |
597 | |
598 return; | 629 return; |
599 } | 630 } |
600 | 631 |
601 element_ = element; | 632 element_ = element; |
602 | 633 |
603 // If autocomplete is disabled at the form level, then we might want to show | 634 // If autocomplete is disabled at the form level, then we might want to show |
604 // a warning in place of suggestions. However, if autocomplete is disabled | 635 // a warning in place of suggestions. However, if autocomplete is disabled |
605 // specifically for this field, we never want to show a warning. Otherwise, | 636 // specifically for this field, we never want to show a warning. Otherwise, |
606 // we might interfere with custom popups (e.g. search suggestions) used by | 637 // we might interfere with custom popups (e.g. search suggestions) used by |
607 // the website. Also, if the field has no name, then we won't have values. | 638 // the website. Also, if the field has no name, then we won't have values. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 | 718 |
688 void AutofillAgent::SetNodeText(const string16& value, | 719 void AutofillAgent::SetNodeText(const string16& value, |
689 WebKit::WebInputElement* node) { | 720 WebKit::WebInputElement* node) { |
690 did_set_node_text_ = true; | 721 did_set_node_text_ = true; |
691 string16 substring = value; | 722 string16 substring = value; |
692 substring = substring.substr(0, node->maxLength()); | 723 substring = substring.substr(0, node->maxLength()); |
693 | 724 |
694 node->setEditingValue(substring); | 725 node->setEditingValue(substring); |
695 } | 726 } |
696 | 727 |
| 728 void AutofillAgent::HidePopups() { |
| 729 WebKit::WebView* web_view = render_view()->GetWebView(); |
| 730 if (web_view) |
| 731 web_view->hidePopups(); |
| 732 } |
| 733 |
697 } // namespace autofill | 734 } // namespace autofill |
OLD | NEW |