| 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" |
| 16 #include "chrome/common/form_data_predictions.h" |
| 17 #include "chrome/common/form_field.h" |
| 15 #include "chrome/renderer/autofill/form_autofill_util.h" | 18 #include "chrome/renderer/autofill/form_autofill_util.h" |
| 16 #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" |
| 17 #include "content/public/renderer/render_view.h" | 21 #include "content/public/renderer/render_view.h" |
| 18 #include "grit/chromium_strings.h" | 22 #include "grit/chromium_strings.h" |
| 19 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h" |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" |
| 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 29 #include "ui/base/keycodes/keyboard_codes.h" | 33 #include "ui/base/keycodes/keyboard_codes.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "webkit/forms/form_data.h" | |
| 32 #include "webkit/forms/form_data_predictions.h" | |
| 33 #include "webkit/forms/form_field.h" | |
| 34 #include "webkit/forms/password_form.h" | |
| 35 | 35 |
| 36 using WebKit::WebAutofillClient; | 36 using WebKit::WebAutofillClient; |
| 37 using WebKit::WebFormControlElement; | 37 using WebKit::WebFormControlElement; |
| 38 using WebKit::WebFormElement; | 38 using WebKit::WebFormElement; |
| 39 using WebKit::WebFrame; | 39 using WebKit::WebFrame; |
| 40 using WebKit::WebInputElement; | 40 using WebKit::WebInputElement; |
| 41 using WebKit::WebKeyboardEvent; | 41 using WebKit::WebKeyboardEvent; |
| 42 using WebKit::WebNode; | 42 using WebKit::WebNode; |
| 43 using WebKit::WebNodeCollection; | 43 using WebKit::WebNodeCollection; |
| 44 using WebKit::WebOptionElement; | 44 using WebKit::WebOptionElement; |
| 45 using WebKit::WebString; | 45 using WebKit::WebString; |
| 46 using webkit::forms::FormData; | 46 using chrome::FormData; |
| 47 using webkit::forms::FormDataPredictions; | 47 using chrome::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 | 55 // The maximum number of data list elements to send to the browser process |
| 56 // via IPC (to prevent long IPC messages). | 56 // via IPC (to prevent long IPC messages). |
| 57 const size_t kMaximumDataListSizeForAutofill = 30; | 57 const size_t kMaximumDataListSizeForAutofill = 30; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion, | 162 IPC_MESSAGE_HANDLER(AutofillMsg_AcceptPasswordAutofillSuggestion, |
| 163 OnAcceptPasswordAutofillSuggestion) | 163 OnAcceptPasswordAutofillSuggestion) |
| 164 IPC_MESSAGE_UNHANDLED(handled = false) | 164 IPC_MESSAGE_UNHANDLED(handled = false) |
| 165 IPC_END_MESSAGE_MAP() | 165 IPC_END_MESSAGE_MAP() |
| 166 return handled; | 166 return handled; |
| 167 } | 167 } |
| 168 | 168 |
| 169 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { | 169 void AutofillAgent::DidFinishDocumentLoad(WebFrame* frame) { |
| 170 // 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 |
| 171 // the browser. | 171 // the browser. |
| 172 std::vector<webkit::forms::FormData> forms; | 172 std::vector<chrome::FormData> forms; |
| 173 form_cache_.ExtractForms(*frame, &forms); | 173 form_cache_.ExtractForms(*frame, &forms); |
| 174 | 174 |
| 175 if (!forms.empty()) { | 175 if (!forms.empty()) { |
| 176 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms, | 176 Send(new AutofillHostMsg_FormsSeen(routing_id(), forms, |
| 177 base::TimeTicks::Now())); | 177 base::TimeTicks::Now())); |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 void AutofillAgent::FrameDetached(WebFrame* frame) { | 181 void AutofillAgent::FrameDetached(WebFrame* frame) { |
| 182 form_cache_.ResetFrame(*frame); | 182 form_cache_.ResetFrame(*frame); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 } | 327 } |
| 328 | 328 |
| 329 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) { | 329 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) { |
| 330 if (password_autofill_manager_->TextDidChangeInTextField(element)) { | 330 if (password_autofill_manager_->TextDidChangeInTextField(element)) { |
| 331 element_ = element; | 331 element_ = element; |
| 332 return; | 332 return; |
| 333 } | 333 } |
| 334 | 334 |
| 335 ShowSuggestions(element, false, true, false); | 335 ShowSuggestions(element, false, true, false); |
| 336 | 336 |
| 337 webkit::forms::FormData form; | 337 chrome::FormData form; |
| 338 webkit::forms::FormField field; | 338 chrome::FormField field; |
| 339 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) { | 339 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) { |
| 340 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field, | 340 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field, |
| 341 base::TimeTicks::Now())); | 341 base::TimeTicks::Now())); |
| 342 } | 342 } |
| 343 } | 343 } |
| 344 | 344 |
| 345 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element, | 345 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element, |
| 346 const WebKeyboardEvent& event) { | 346 const WebKeyboardEvent& event) { |
| 347 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) { | 347 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) { |
| 348 element_ = element; | 348 element_ = element; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 } | 500 } |
| 501 last_part.append(suggested_value); | 501 last_part.append(suggested_value); |
| 502 parts[parts.size() - 1] = last_part; | 502 parts[parts.size() - 1] = last_part; |
| 503 | 503 |
| 504 new_value = JoinString(parts, ','); | 504 new_value = JoinString(parts, ','); |
| 505 } | 505 } |
| 506 SetNodeText(new_value, &element_); | 506 SetNodeText(new_value, &element_); |
| 507 } | 507 } |
| 508 | 508 |
| 509 void AutofillAgent::OnFormDataFilled(int query_id, | 509 void AutofillAgent::OnFormDataFilled(int query_id, |
| 510 const webkit::forms::FormData& form) { | 510 const chrome::FormData& form) { |
| 511 if (!render_view()->GetWebView() || query_id != autofill_query_id_) | 511 if (!render_view()->GetWebView() || query_id != autofill_query_id_) |
| 512 return; | 512 return; |
| 513 | 513 |
| 514 was_query_node_autofilled_ = element_.isAutofilled(); | 514 was_query_node_autofilled_ = element_.isAutofilled(); |
| 515 | 515 |
| 516 switch (autofill_action_) { | 516 switch (autofill_action_) { |
| 517 case AUTOFILL_FILL: | 517 case AUTOFILL_FILL: |
| 518 FillForm(form, element_); | 518 FillForm(form, element_); |
| 519 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), | 519 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), |
| 520 base::TimeTicks::Now())); | 520 base::TimeTicks::Now())); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 | 619 |
| 620 QueryAutofillSuggestions(element, display_warning_if_disabled); | 620 QueryAutofillSuggestions(element, display_warning_if_disabled); |
| 621 } | 621 } |
| 622 | 622 |
| 623 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element, | 623 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element, |
| 624 bool display_warning_if_disabled) { | 624 bool display_warning_if_disabled) { |
| 625 static int query_counter = 0; | 625 static int query_counter = 0; |
| 626 autofill_query_id_ = query_counter++; | 626 autofill_query_id_ = query_counter++; |
| 627 display_warning_if_disabled_ = display_warning_if_disabled; | 627 display_warning_if_disabled_ = display_warning_if_disabled; |
| 628 | 628 |
| 629 webkit::forms::FormData form; | 629 chrome::FormData form; |
| 630 webkit::forms::FormField field; | 630 chrome::FormField field; |
| 631 if (!FindFormAndFieldForInputElement(element, &form, &field, | 631 if (!FindFormAndFieldForInputElement(element, &form, &field, |
| 632 REQUIRE_AUTOCOMPLETE)) { | 632 REQUIRE_AUTOCOMPLETE)) { |
| 633 // 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 |
| 634 // at providing suggestions. | 634 // at providing suggestions. |
| 635 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); | 635 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); |
| 636 } | 636 } |
| 637 | 637 |
| 638 gfx::Rect bounding_box(element_.boundsInViewportSpace()); | 638 gfx::Rect bounding_box(element_.boundsInViewportSpace()); |
| 639 | 639 |
| 640 // Find the datalist values and send them to the browser process. | 640 // Find the datalist values and send them to the browser process. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 668 } | 668 } |
| 669 | 669 |
| 670 void AutofillAgent::FillAutofillFormData(const WebNode& node, | 670 void AutofillAgent::FillAutofillFormData(const WebNode& node, |
| 671 int unique_id, | 671 int unique_id, |
| 672 AutofillAction action) { | 672 AutofillAction action) { |
| 673 DCHECK_GT(unique_id, 0); | 673 DCHECK_GT(unique_id, 0); |
| 674 | 674 |
| 675 static int query_counter = 0; | 675 static int query_counter = 0; |
| 676 autofill_query_id_ = query_counter++; | 676 autofill_query_id_ = query_counter++; |
| 677 | 677 |
| 678 webkit::forms::FormData form; | 678 chrome::FormData form; |
| 679 webkit::forms::FormField field; | 679 chrome::FormField field; |
| 680 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, | 680 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, |
| 681 &field, REQUIRE_AUTOCOMPLETE)) { | 681 &field, REQUIRE_AUTOCOMPLETE)) { |
| 682 return; | 682 return; |
| 683 } | 683 } |
| 684 | 684 |
| 685 autofill_action_ = action; | 685 autofill_action_ = action; |
| 686 Send(new AutofillHostMsg_FillAutofillFormData( | 686 Send(new AutofillHostMsg_FillAutofillFormData( |
| 687 routing_id(), autofill_query_id_, form, field, unique_id)); | 687 routing_id(), autofill_query_id_, form, field, unique_id)); |
| 688 } | 688 } |
| 689 | 689 |
| 690 void AutofillAgent::SetNodeText(const string16& value, | 690 void AutofillAgent::SetNodeText(const string16& value, |
| 691 WebKit::WebInputElement* node) { | 691 WebKit::WebInputElement* node) { |
| 692 did_set_node_text_ = true; | 692 did_set_node_text_ = true; |
| 693 string16 substring = value; | 693 string16 substring = value; |
| 694 substring = substring.substr(0, node->maxLength()); | 694 substring = substring.substr(0, node->maxLength()); |
| 695 | 695 |
| 696 node->setEditingValue(substring); | 696 node->setEditingValue(substring); |
| 697 } | 697 } |
| 698 | 698 |
| 699 } // namespace autofill | 699 } // namespace autofill |
| OLD | NEW |