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" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 using WebKit::WebString; | 45 using WebKit::WebString; |
46 using webkit::forms::FormData; | 46 using webkit::forms::FormData; |
47 using webkit::forms::FormDataPredictions; | 47 using webkit::forms::FormDataPredictions; |
48 | 48 |
49 namespace { | 49 namespace { |
50 | 50 |
51 // The size above which we stop triggering autofill for an input text field | 51 // The size above which we stop triggering autofill for an input text field |
52 // (so to avoid sending long strings through IPC). | 52 // (so to avoid sending long strings through IPC). |
53 const size_t kMaximumTextSizeForAutofill = 1000; | 53 const size_t kMaximumTextSizeForAutofill = 1000; |
54 | 54 |
| 55 // The maximum number of data list elements to send to the browser process |
| 56 // via IPC (to prevent long IPC messages). |
| 57 const size_t kMaximumDataListSizeForAutofill = 30; |
| 58 |
55 void AppendDataListSuggestions(const WebKit::WebInputElement& element, | 59 void AppendDataListSuggestions(const WebKit::WebInputElement& element, |
56 std::vector<string16>* values, | 60 std::vector<string16>* values, |
57 std::vector<string16>* labels, | 61 std::vector<string16>* labels, |
58 std::vector<string16>* icons, | 62 std::vector<string16>* icons, |
59 std::vector<int>* item_ids) { | 63 std::vector<int>* item_ids) { |
60 WebNodeCollection options = element.dataListOptions(); | 64 WebNodeCollection options = element.dataListOptions(); |
61 if (options.isNull()) | 65 if (options.isNull()) |
62 return; | 66 return; |
63 | 67 |
64 string16 prefix = element.editingValue(); | 68 string16 prefix = element.editingValue(); |
(...skipping 14 matching lines...) Expand all Loading... |
79 values->push_back(option.value()); | 83 values->push_back(option.value()); |
80 if (option.value() != option.label()) | 84 if (option.value() != option.label()) |
81 labels->push_back(option.label()); | 85 labels->push_back(option.label()); |
82 else | 86 else |
83 labels->push_back(string16()); | 87 labels->push_back(string16()); |
84 icons->push_back(string16()); | 88 icons->push_back(string16()); |
85 item_ids->push_back(WebAutofillClient::MenuItemIDDataListEntry); | 89 item_ids->push_back(WebAutofillClient::MenuItemIDDataListEntry); |
86 } | 90 } |
87 } | 91 } |
88 | 92 |
| 93 // Trim the vectors before sending them to the browser process to ensure we |
| 94 // don't send too much data through the IPC. |
| 95 void TrimDataListsForIPC(std::vector<string16>* values, |
| 96 std::vector<string16>* labels, |
| 97 std::vector<string16>* icons, |
| 98 std::vector<int>* unique_ids) { |
| 99 // Limit the size of the vectors. |
| 100 if (values->size() > kMaximumDataListSizeForAutofill) { |
| 101 values->resize(kMaximumDataListSizeForAutofill); |
| 102 labels->resize(kMaximumDataListSizeForAutofill); |
| 103 icons->resize(kMaximumDataListSizeForAutofill); |
| 104 unique_ids->resize(kMaximumDataListSizeForAutofill); |
| 105 } |
| 106 |
| 107 // Limit the size of the strings in the vectors |
| 108 for (size_t i = 0; i < values->size(); ++i) { |
| 109 if ((*values)[i].length() > kMaximumTextSizeForAutofill) |
| 110 (*values)[i].resize(kMaximumTextSizeForAutofill); |
| 111 |
| 112 if ((*labels)[i].length() > kMaximumTextSizeForAutofill) |
| 113 (*labels)[i].resize(kMaximumTextSizeForAutofill); |
| 114 |
| 115 if ((*icons)[i].length() > kMaximumTextSizeForAutofill) |
| 116 (*icons)[i].resize(kMaximumTextSizeForAutofill); |
| 117 } |
| 118 } |
| 119 |
89 } // namespace | 120 } // namespace |
90 | 121 |
91 namespace autofill { | 122 namespace autofill { |
92 | 123 |
93 AutofillAgent::AutofillAgent( | 124 AutofillAgent::AutofillAgent( |
94 content::RenderView* render_view, | 125 content::RenderView* render_view, |
95 PasswordAutofillManager* password_autofill_manager) | 126 PasswordAutofillManager* password_autofill_manager) |
96 : content::RenderViewObserver(render_view), | 127 : content::RenderViewObserver(render_view), |
97 password_autofill_manager_(password_autofill_manager), | 128 password_autofill_manager_(password_autofill_manager), |
98 autofill_query_id_(0), | 129 autofill_query_id_(0), |
(...skipping 20 matching lines...) Expand all Loading... |
119 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionFill, | 150 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionFill, |
120 OnSetAutofillActionFill) | 151 OnSetAutofillActionFill) |
121 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, | 152 IPC_MESSAGE_HANDLER(AutofillMsg_ClearForm, |
122 OnClearForm) | 153 OnClearForm) |
123 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview, | 154 IPC_MESSAGE_HANDLER(AutofillMsg_SetAutofillActionPreview, |
124 OnSetAutofillActionPreview) | 155 OnSetAutofillActionPreview) |
125 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, | 156 IPC_MESSAGE_HANDLER(AutofillMsg_ClearPreviewedForm, |
126 OnClearPreviewedForm) | 157 OnClearPreviewedForm) |
127 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, | 158 IPC_MESSAGE_HANDLER(AutofillMsg_SetNodeText, |
128 OnSetNodeText) | 159 OnSetNodeText) |
| 160 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptDataListSuggestion, |
| 161 OnAcceptDataListSuggestion) |
129 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion, | 162 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion, |
130 OnAcceptPasswordAutofillSuggestion) | 163 OnAcceptPasswordAutofillSuggestion) |
131 IPC_MESSAGE_UNHANDLED(handled = false) | 164 IPC_MESSAGE_UNHANDLED(handled = false) |
132 IPC_END_MESSAGE_MAP() | 165 IPC_END_MESSAGE_MAP() |
133 return handled; | 166 return handled; |
134 } | 167 } |
135 | 168 |
136 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { | 169 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { |
137 // The document has now been fully loaded. Scan for forms to be sent up to | 170 // The document has now been fully loaded. Scan for forms to be sent up to |
138 // the browser. | 171 // the browser. |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 } | 555 } |
523 | 556 |
524 void AutofillAgent::OnClearPreviewedForm() { | 557 void AutofillAgent::OnClearPreviewedForm() { |
525 didClearAutofillSelection(element_); | 558 didClearAutofillSelection(element_); |
526 } | 559 } |
527 | 560 |
528 void AutofillAgent::OnSetNodeText(const string16& value) { | 561 void AutofillAgent::OnSetNodeText(const string16& value) { |
529 SetNodeText(value, &element_); | 562 SetNodeText(value, &element_); |
530 } | 563 } |
531 | 564 |
| 565 void AutofillAgent::OnAcceptDataListSuggestion(const string16& value) { |
| 566 AcceptDataListSuggestion(value); |
| 567 } |
| 568 |
532 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) { | 569 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) { |
533 // We need to make sure this is handled here because the browser process | 570 // We need to make sure this is handled here because the browser process |
534 // skipped it handling because it believed it would be handled here. If it | 571 // skipped it handling because it believed it would be handled here. If it |
535 // isn't handled here then the browser logic needs to be updated. | 572 // isn't handled here then the browser logic needs to be updated. |
536 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion( | 573 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion( |
537 element_, | 574 element_, |
538 value); | 575 value); |
539 DCHECK(handled); | 576 DCHECK(handled); |
540 } | 577 } |
541 | 578 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 webkit::forms::FormField field; | 630 webkit::forms::FormField field; |
594 if (!FindFormAndFieldForInputElement(element, &form, &field, | 631 if (!FindFormAndFieldForInputElement(element, &form, &field, |
595 REQUIRE_AUTOCOMPLETE)) { | 632 REQUIRE_AUTOCOMPLETE)) { |
596 // If we didn't find the cached form, at least let autocomplete have a shot | 633 // If we didn't find the cached form, at least let autocomplete have a shot |
597 // at providing suggestions. | 634 // at providing suggestions. |
598 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); | 635 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); |
599 } | 636 } |
600 | 637 |
601 gfx::Rect bounding_box(element_.boundsInViewportSpace()); | 638 gfx::Rect bounding_box(element_.boundsInViewportSpace()); |
602 | 639 |
| 640 // Find the datalist values and send them to the browser process. |
| 641 std::vector<string16> data_list_values; |
| 642 std::vector<string16> data_list_labels; |
| 643 std::vector<string16> data_list_icons; |
| 644 std::vector<int> data_list_unique_ids; |
| 645 AppendDataListSuggestions(element_, |
| 646 &data_list_values, |
| 647 &data_list_labels, |
| 648 &data_list_icons, |
| 649 &data_list_unique_ids); |
| 650 |
| 651 TrimDataListsForIPC(&data_list_values, |
| 652 &data_list_labels, |
| 653 &data_list_icons, |
| 654 &data_list_unique_ids); |
| 655 |
| 656 Send(new AutofillHostMsg_SetDataList(routing_id(), |
| 657 data_list_values, |
| 658 data_list_labels, |
| 659 data_list_icons, |
| 660 data_list_unique_ids)); |
| 661 |
603 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(), | 662 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(), |
604 autofill_query_id_, | 663 autofill_query_id_, |
605 form, | 664 form, |
606 field, | 665 field, |
607 bounding_box, | 666 bounding_box, |
608 display_warning_if_disabled)); | 667 display_warning_if_disabled)); |
609 } | 668 } |
610 | 669 |
611 void AutofillAgent::FillAutofillFormData(const WebNode& node, | 670 void AutofillAgent::FillAutofillFormData(const WebNode& node, |
612 int unique_id, | 671 int unique_id, |
(...skipping 18 matching lines...) Expand all Loading... |
631 void AutofillAgent::SetNodeText(const string16& value, | 690 void AutofillAgent::SetNodeText(const string16& value, |
632 WebKit::WebInputElement* node) { | 691 WebKit::WebInputElement* node) { |
633 did_set_node_text_ = true; | 692 did_set_node_text_ = true; |
634 string16 substring = value; | 693 string16 substring = value; |
635 substring = substring.substr(0, node->maxLength()); | 694 substring = substring.substr(0, node->maxLength()); |
636 | 695 |
637 node->setEditingValue(substring); | 696 node->setEditingValue(substring); |
638 } | 697 } |
639 | 698 |
640 } // namespace autofill | 699 } // namespace autofill |
OLD | NEW |