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/form_autofill_util.h" | 5 #include "chrome/renderer/autofill/form_autofill_util.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/common/form_data.h" |
| 14 #include "chrome/common/form_field.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement
.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebLabelElement.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebLabelElement.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSelectElement.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSelectElement.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
26 #include "webkit/forms/form_data.h" | |
27 #include "webkit/forms/form_field.h" | |
28 | 28 |
| 29 using chrome::FormData; |
| 30 using chrome::FormField; |
29 using WebKit::WebElement; | 31 using WebKit::WebElement; |
30 using WebKit::WebFormControlElement; | 32 using WebKit::WebFormControlElement; |
31 using WebKit::WebFormElement; | 33 using WebKit::WebFormElement; |
32 using WebKit::WebFrame; | 34 using WebKit::WebFrame; |
33 using WebKit::WebInputElement; | 35 using WebKit::WebInputElement; |
34 using WebKit::WebLabelElement; | 36 using WebKit::WebLabelElement; |
35 using WebKit::WebNode; | 37 using WebKit::WebNode; |
36 using WebKit::WebNodeList; | 38 using WebKit::WebNodeList; |
37 using WebKit::WebOptionElement; | 39 using WebKit::WebOptionElement; |
38 using WebKit::WebSelectElement; | 40 using WebKit::WebSelectElement; |
39 using WebKit::WebString; | 41 using WebKit::WebString; |
40 using WebKit::WebVector; | 42 using WebKit::WebVector; |
41 using webkit::forms::FormData; | |
42 using webkit::forms::FormField; | |
43 | 43 |
44 namespace { | 44 namespace { |
45 | 45 |
46 using autofill::ExtractAutofillableElements; | 46 using autofill::ExtractAutofillableElements; |
47 using autofill::IsTextInput; | 47 using autofill::IsTextInput; |
48 using autofill::IsSelectElement; | 48 using autofill::IsSelectElement; |
49 | 49 |
50 // The maximum length allowed for form data. | 50 // The maximum length allowed for form data. |
51 const size_t kMaxDataLength = 1024; | 51 const size_t kMaxDataLength = 1024; |
52 | 52 |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 if (IsOptionElement(list_items[i])) { | 409 if (IsOptionElement(list_items[i])) { |
410 const WebOptionElement option = list_items[i].toConst<WebOptionElement>(); | 410 const WebOptionElement option = list_items[i].toConst<WebOptionElement>(); |
411 option_values->push_back(option.value()); | 411 option_values->push_back(option.value()); |
412 option_contents->push_back(option.text()); | 412 option_contents->push_back(option.text()); |
413 } | 413 } |
414 } | 414 } |
415 } | 415 } |
416 | 416 |
417 // The callback type used by |ForEachMatchingFormField()|. | 417 // The callback type used by |ForEachMatchingFormField()|. |
418 typedef void (*Callback)(WebKit::WebFormControlElement*, | 418 typedef void (*Callback)(WebKit::WebFormControlElement*, |
419 const webkit::forms::FormField*, | 419 const chrome::FormField*, |
420 bool); | 420 bool); |
421 | 421 |
422 // For each autofillable field in |data| that matches a field in the |form|, | 422 // For each autofillable field in |data| that matches a field in the |form|, |
423 // the |callback| is invoked with the corresponding |form| field data. | 423 // the |callback| is invoked with the corresponding |form| field data. |
424 void ForEachMatchingFormField(const WebFormElement& form_element, | 424 void ForEachMatchingFormField(const WebFormElement& form_element, |
425 const WebElement& initiating_element, | 425 const WebElement& initiating_element, |
426 const FormData& data, | 426 const FormData& data, |
427 Callback callback) { | 427 Callback callback) { |
428 std::vector<WebFormControlElement> control_elements; | 428 std::vector<WebFormControlElement> control_elements; |
429 ExtractAutofillableElements(form_element, autofill::REQUIRE_AUTOCOMPLETE, | 429 ExtractAutofillableElements(form_element, autofill::REQUIRE_AUTOCOMPLETE, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 !element->isFocusable()) | 471 !element->isFocusable()) |
472 continue; | 472 continue; |
473 | 473 |
474 callback(element, &data.fields[i], is_initiating_element); | 474 callback(element, &data.fields[i], is_initiating_element); |
475 } | 475 } |
476 } | 476 } |
477 | 477 |
478 // Sets the |field|'s value to the value in |data|. | 478 // Sets the |field|'s value to the value in |data|. |
479 // Also sets the "autofilled" attribute, causing the background to be yellow. | 479 // Also sets the "autofilled" attribute, causing the background to be yellow. |
480 void FillFormField(WebKit::WebFormControlElement* field, | 480 void FillFormField(WebKit::WebFormControlElement* field, |
481 const webkit::forms::FormField* data, | 481 const chrome::FormField* data, |
482 bool is_initiating_node) { | 482 bool is_initiating_node) { |
483 // Nothing to fill. | 483 // Nothing to fill. |
484 if (data->value.empty()) | 484 if (data->value.empty()) |
485 return; | 485 return; |
486 | 486 |
487 WebInputElement* input_element = toWebInputElement(field); | 487 WebInputElement* input_element = toWebInputElement(field); |
488 if (IsTextInput(input_element)) { | 488 if (IsTextInput(input_element)) { |
489 // If the maxlength attribute contains a negative value, maxLength() | 489 // If the maxlength attribute contains a negative value, maxLength() |
490 // returns the default maxlength value. | 490 // returns the default maxlength value. |
491 input_element->setValue( | 491 input_element->setValue( |
492 data->value.substr(0, input_element->maxLength()), true); | 492 data->value.substr(0, input_element->maxLength()), true); |
493 input_element->setAutofilled(true); | 493 input_element->setAutofilled(true); |
494 if (is_initiating_node) { | 494 if (is_initiating_node) { |
495 int length = input_element->value().length(); | 495 int length = input_element->value().length(); |
496 input_element->setSelectionRange(length, length); | 496 input_element->setSelectionRange(length, length); |
497 } | 497 } |
498 } else { | 498 } else { |
499 DCHECK(IsSelectElement(*field)); | 499 DCHECK(IsSelectElement(*field)); |
500 WebSelectElement select_element = field->to<WebSelectElement>(); | 500 WebSelectElement select_element = field->to<WebSelectElement>(); |
501 if (select_element.value() != data->value) { | 501 if (select_element.value() != data->value) { |
502 select_element.setValue(data->value); | 502 select_element.setValue(data->value); |
503 select_element.dispatchFormControlChangeEvent(); | 503 select_element.dispatchFormControlChangeEvent(); |
504 } | 504 } |
505 } | 505 } |
506 } | 506 } |
507 | 507 |
508 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. | 508 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. |
509 // Also sets the "autofilled" attribute, causing the background to be yellow. | 509 // Also sets the "autofilled" attribute, causing the background to be yellow. |
510 void PreviewFormField(WebKit::WebFormControlElement* field, | 510 void PreviewFormField(WebKit::WebFormControlElement* field, |
511 const webkit::forms::FormField* data, | 511 const chrome::FormField* data, |
512 bool is_initiating_node) { | 512 bool is_initiating_node) { |
513 // Nothing to preview. | 513 // Nothing to preview. |
514 if (data->value.empty()) | 514 if (data->value.empty()) |
515 return; | 515 return; |
516 | 516 |
517 // Only preview input fields. | 517 // Only preview input fields. |
518 WebInputElement* input_element = toWebInputElement(field); | 518 WebInputElement* input_element = toWebInputElement(field); |
519 if (!IsTextInput(input_element)) | 519 if (!IsTextInput(input_element)) |
520 return; | 520 return; |
521 | 521 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 value = value.substr(0, kMaxDataLength); | 656 value = value.substr(0, kMaxDataLength); |
657 | 657 |
658 field->value = value; | 658 field->value = value; |
659 } | 659 } |
660 | 660 |
661 bool WebFormElementToFormData( | 661 bool WebFormElementToFormData( |
662 const WebKit::WebFormElement& form_element, | 662 const WebKit::WebFormElement& form_element, |
663 const WebKit::WebFormControlElement& form_control_element, | 663 const WebKit::WebFormControlElement& form_control_element, |
664 RequirementsMask requirements, | 664 RequirementsMask requirements, |
665 ExtractMask extract_mask, | 665 ExtractMask extract_mask, |
666 webkit::forms::FormData* form, | 666 chrome::FormData* form, |
667 webkit::forms::FormField* field) { | 667 chrome::FormField* field) { |
668 const WebFrame* frame = form_element.document().frame(); | 668 const WebFrame* frame = form_element.document().frame(); |
669 if (!frame) | 669 if (!frame) |
670 return false; | 670 return false; |
671 | 671 |
672 if (requirements & REQUIRE_AUTOCOMPLETE && !form_element.autoComplete()) | 672 if (requirements & REQUIRE_AUTOCOMPLETE && !form_element.autoComplete()) |
673 return false; | 673 return false; |
674 | 674 |
675 form->name = GetFormIdentifier(form_element); | 675 form->name = GetFormIdentifier(form_element); |
676 form->method = form_element.method(); | 676 form->method = form_element.method(); |
677 form->origin = frame->document().url(); | 677 form->origin = frame->document().url(); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 for (ScopedVector<FormField>::const_iterator iter = form_fields.begin(); | 785 for (ScopedVector<FormField>::const_iterator iter = form_fields.begin(); |
786 iter != form_fields.end(); ++iter) { | 786 iter != form_fields.end(); ++iter) { |
787 form->fields.push_back(**iter); | 787 form->fields.push_back(**iter); |
788 } | 788 } |
789 | 789 |
790 return true; | 790 return true; |
791 } | 791 } |
792 | 792 |
793 bool FindFormAndFieldForInputElement(const WebInputElement& element, | 793 bool FindFormAndFieldForInputElement(const WebInputElement& element, |
794 FormData* form, | 794 FormData* form, |
795 webkit::forms::FormField* field, | 795 chrome::FormField* field, |
796 RequirementsMask requirements) { | 796 RequirementsMask requirements) { |
797 if (!IsAutofillableElement(element)) | 797 if (!IsAutofillableElement(element)) |
798 return false; | 798 return false; |
799 | 799 |
800 const WebFormElement form_element = element.form(); | 800 const WebFormElement form_element = element.form(); |
801 if (form_element.isNull()) | 801 if (form_element.isNull()) |
802 return false; | 802 return false; |
803 | 803 |
804 ExtractMask extract_mask = | 804 ExtractMask extract_mask = |
805 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); | 805 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
895 continue; | 895 continue; |
896 | 896 |
897 if (input_element->isAutofilled()) | 897 if (input_element->isAutofilled()) |
898 return true; | 898 return true; |
899 } | 899 } |
900 | 900 |
901 return false; | 901 return false; |
902 } | 902 } |
903 | 903 |
904 } // namespace autofill | 904 } // namespace autofill |
OLD | NEW |