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