Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: components/autofill/content/renderer/form_autofill_util.cc

Issue 140093005: Add supports that allow Autofill to be initiated from textarea field (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Condition wrap correction Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/autofill/content/renderer/form_autofill_util.h" 5 #include "components/autofill/content/renderer/form_autofill_util.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 NOTREACHED(); 500 NOTREACHED();
501 continue; 501 continue;
502 } 502 }
503 503
504 bool is_initiating_element = (*element == initiating_element); 504 bool is_initiating_element = (*element == initiating_element);
505 505
506 // Only autofill empty fields and the field that initiated the filling, 506 // Only autofill empty fields and the field that initiated the filling,
507 // i.e. the field the user is currently editing and interacting with. 507 // i.e. the field the user is currently editing and interacting with.
508 const WebInputElement* input_element = toWebInputElement(element); 508 const WebInputElement* input_element = toWebInputElement(element);
509 if (!force_override && !is_initiating_element && 509 if (!force_override && !is_initiating_element &&
510 ((IsAutofillableInputElement(input_element) && 510 ((IsAutofillableInputElement(input_element) ||
511 !input_element->value().isEmpty()) || 511 IsTextAreaElement(*element)) &&
512 (IsTextAreaElement(*element) && 512 !element->value().isEmpty()))
513 !element->toConst<WebTextAreaElement>().value().isEmpty())))
514 continue; 513 continue;
515 514
516 if (((filters & FILTER_DISABLED_ELEMENTS) && !element->isEnabled()) || 515 if (((filters & FILTER_DISABLED_ELEMENTS) && !element->isEnabled()) ||
517 ((filters & FILTER_READONLY_ELEMENTS) && element->isReadOnly()) || 516 ((filters & FILTER_READONLY_ELEMENTS) && element->isReadOnly()) ||
518 ((filters & FILTER_NON_FOCUSABLE_ELEMENTS) && !element->isFocusable())) 517 ((filters & FILTER_NON_FOCUSABLE_ELEMENTS) && !element->isFocusable()))
519 continue; 518 continue;
520 519
521 callback(data.fields[i], is_initiating_element, element); 520 callback(data.fields[i], is_initiating_element, element);
522 } 521 }
523 } 522 }
524 523
525 // Sets the |field|'s value to the value in |data|. 524 // Sets the |field|'s value to the value in |data|.
526 // Also sets the "autofilled" attribute, causing the background to be yellow. 525 // Also sets the "autofilled" attribute, causing the background to be yellow.
527 void FillFormField(const FormFieldData& data, 526 void FillFormField(const FormFieldData& data,
528 bool is_initiating_node, 527 bool is_initiating_node,
529 blink::WebFormControlElement* field) { 528 blink::WebFormControlElement* field) {
530 // Nothing to fill. 529 // Nothing to fill.
531 if (data.value.empty()) 530 if (data.value.empty())
532 return; 531 return;
533 532
534 field->setAutofilled(true); 533 field->setAutofilled(true);
535 534
536 WebInputElement* input_element = toWebInputElement(field); 535 WebInputElement* input_element = toWebInputElement(field);
537 if (IsTextInput(input_element) || IsMonthInput(input_element)) { 536 if (IsTextInput(input_element) || IsMonthInput(input_element)) {
538 // If the maxlength attribute contains a negative value, maxLength() 537 // If the maxlength attribute contains a negative value, maxLength()
539 // returns the default maxlength value. 538 // returns the default maxlength value.
540 input_element->setValue( 539 input_element->setValue(
541 data.value.substr(0, input_element->maxLength()), true); 540 data.value.substr(0, input_element->maxLength()), true);
542 if (is_initiating_node) { 541 } else if (IsTextAreaElement(*field) || IsSelectElement(*field)) {
543 int length = input_element->value().length(); 542 if (field->value() != data.value) {
544 input_element->setSelectionRange(length, length); 543 field->setValue(data.value);
545 // Clear the current IME composition (the underline), if there is one. 544 field->dispatchFormControlChangeEvent();
546 input_element->document().frame()->unmarkText();
547 }
548 } else if (IsTextAreaElement(*field)) {
549 WebTextAreaElement text_area = field->to<WebTextAreaElement>();
550 if (text_area.value() != data.value) {
551 text_area.setValue(data.value);
552 text_area.dispatchFormControlChangeEvent();
553 }
554 } else if (IsSelectElement(*field)) {
555 WebSelectElement select_element = field->to<WebSelectElement>();
556 if (select_element.value() != data.value) {
557 select_element.setValue(data.value);
558 select_element.dispatchFormControlChangeEvent();
559 } 545 }
560 } else { 546 } else {
561 DCHECK(IsCheckableElement(input_element)); 547 DCHECK(IsCheckableElement(input_element));
562 input_element->setChecked(data.is_checked, true); 548 input_element->setChecked(data.is_checked, true);
563 } 549 }
550
551 if (is_initiating_node &&
552 ((IsTextInput(input_element) || IsMonthInput(input_element)) ||
553 IsTextAreaElement(*field))) {
554 int length = field->value().length();
555 field->setSelectionRange(length, length);
556 // Clear the current IME composition (the underline), if there is one.
557 field->document().frame()->unmarkText();
558 }
564 } 559 }
565 560
566 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|. 561 // Sets the |field|'s "suggested" (non JS visible) value to the value in |data|.
567 // Also sets the "autofilled" attribute, causing the background to be yellow. 562 // Also sets the "autofilled" attribute, causing the background to be yellow.
568 void PreviewFormField(const FormFieldData& data, 563 void PreviewFormField(const FormFieldData& data,
569 bool is_initiating_node, 564 bool is_initiating_node,
570 blink::WebFormControlElement* field) { 565 blink::WebFormControlElement* field) {
571 // Nothing to preview. 566 // Nothing to preview.
572 if (data.value.empty()) 567 if (data.value.empty())
573 return; 568 return;
574 569
575 // Preview input and textarea fields. For input fields, excludes checkboxes 570 // Preview input and textarea fields. For input fields, excludes checkboxes
576 // and radio buttons, as there is no provision for setSuggestedCheckedValue 571 // and radio buttons, as there is no provision for setSuggestedCheckedValue
577 // in WebInputElement. 572 // in WebInputElement.
578 WebInputElement* input_element = toWebInputElement(field); 573 WebInputElement* input_element = toWebInputElement(field);
579 if (IsTextInput(input_element) || IsMonthInput(input_element)) { 574 if (IsTextInput(input_element) || IsMonthInput(input_element)) {
580 // If the maxlength attribute contains a negative value, maxLength() 575 // If the maxlength attribute contains a negative value, maxLength()
581 // returns the default maxlength value. 576 // returns the default maxlength value.
582 input_element->setSuggestedValue( 577 input_element->setSuggestedValue(
583 data.value.substr(0, input_element->maxLength())); 578 data.value.substr(0, input_element->maxLength()));
584 input_element->setAutofilled(true); 579 input_element->setAutofilled(true);
585 if (is_initiating_node) {
586 // Select the part of the text that the user didn't type.
587 input_element->setSelectionRange(
588 input_element->value().length(),
589 input_element->suggestedValue().length());
590 }
591 } else if (IsTextAreaElement(*field)) { 580 } else if (IsTextAreaElement(*field)) {
592 WebTextAreaElement textarea = field->to<WebTextAreaElement>(); 581 field->setSuggestedValue(data.value);
593 textarea.setSuggestedValue(data.value);
594 field->setAutofilled(true); 582 field->setAutofilled(true);
595 } 583 }
584
585 if (is_initiating_node &&
586 (IsTextInput(input_element) || IsTextAreaElement(*field))) {
587 // Select the part of the text that the user didn't type.
588 int start = field->value().length();
589 int end = field->suggestedValue().length();
590 field->setSelectionRange(start, end);
591 }
596 } 592 }
597 593
598 std::string RetrievalMethodToString( 594 std::string RetrievalMethodToString(
599 const WebElementDescriptor::RetrievalMethod& method) { 595 const WebElementDescriptor::RetrievalMethod& method) {
600 switch (method) { 596 switch (method) {
601 case WebElementDescriptor::CSS_SELECTOR: 597 case WebElementDescriptor::CSS_SELECTOR:
602 return "CSS_SELECTOR"; 598 return "CSS_SELECTOR";
603 case WebElementDescriptor::ID: 599 case WebElementDescriptor::ID:
604 return "ID"; 600 return "ID";
605 case WebElementDescriptor::NONE: 601 case WebElementDescriptor::NONE:
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 // Discard overly long attribute values to avoid DOS-ing the browser 761 // Discard overly long attribute values to avoid DOS-ing the browser
766 // process. However, send over a default string to indicate that the 762 // process. However, send over a default string to indicate that the
767 // attribute was present. 763 // attribute was present.
768 field->autocomplete_attribute = "x-max-data-length-exceeded"; 764 field->autocomplete_attribute = "x-max-data-length-exceeded";
769 } 765 }
770 766
771 if (!IsAutofillableElement(element)) 767 if (!IsAutofillableElement(element))
772 return; 768 return;
773 769
774 const WebInputElement* input_element = toWebInputElement(&element); 770 const WebInputElement* input_element = toWebInputElement(&element);
771 if (IsAutofillableInputElement(input_element) ||
772 IsTextAreaElement(element)) {
773 field->is_autofilled = element.isAutofilled();
774 field->is_focusable = element.isFocusable();
775 field->should_autocomplete = element.autoComplete();
776 field->text_direction = element.directionForFormData() ==
777 "rtl" ? base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
778 }
779
775 if (IsAutofillableInputElement(input_element)) { 780 if (IsAutofillableInputElement(input_element)) {
776 if (IsTextInput(input_element)) 781 if (IsTextInput(input_element))
777 field->max_length = input_element->maxLength(); 782 field->max_length = input_element->maxLength();
778 783
779 field->is_autofilled = input_element->isAutofilled();
780 field->is_focusable = input_element->isFocusable();
781 field->is_checkable = IsCheckableElement(input_element); 784 field->is_checkable = IsCheckableElement(input_element);
782 field->is_checked = input_element->isChecked(); 785 field->is_checked = input_element->isChecked();
783 field->should_autocomplete = input_element->autoComplete();
784 field->text_direction = input_element->directionForFormData() == "rtl" ?
785 base::i18n::RIGHT_TO_LEFT : base::i18n::LEFT_TO_RIGHT;
786 } else if (IsTextAreaElement(element)) { 786 } else if (IsTextAreaElement(element)) {
787 // Nothing more to do in this case. 787 // Nothing more to do in this case.
788 } else if (extract_mask & EXTRACT_OPTIONS) { 788 } else if (extract_mask & EXTRACT_OPTIONS) {
789 // Set option strings on the field if available. 789 // Set option strings on the field if available.
790 DCHECK(IsSelectElement(element)); 790 DCHECK(IsSelectElement(element));
791 const WebSelectElement select_element = element.toConst<WebSelectElement>(); 791 const WebSelectElement select_element = element.toConst<WebSelectElement>();
792 GetOptionStringsFromElement(select_element, 792 GetOptionStringsFromElement(select_element,
793 &field->option_values, 793 &field->option_values,
794 &field->option_contents); 794 &field->option_contents);
795 } 795 }
796 796
797 if (!(extract_mask & EXTRACT_VALUE)) 797 if (!(extract_mask & EXTRACT_VALUE))
798 return; 798 return;
799 799
800 base::string16 value; 800 base::string16 value = element.value();
801 if (IsAutofillableInputElement(input_element)) { 801
802 value = input_element->value(); 802 if (IsSelectElement(element)) {
803 } else if (IsTextAreaElement(element)) {
804 value = element.toConst<WebTextAreaElement>().value();
805 } else {
806 DCHECK(IsSelectElement(element));
807 const WebSelectElement select_element = element.toConst<WebSelectElement>(); 803 const WebSelectElement select_element = element.toConst<WebSelectElement>();
808 value = select_element.value();
809
810 // Convert the |select_element| value to text if requested. 804 // Convert the |select_element| value to text if requested.
811 if (extract_mask & EXTRACT_OPTION_TEXT) { 805 if (extract_mask & EXTRACT_OPTION_TEXT) {
812 WebVector<WebElement> list_items = select_element.listItems(); 806 WebVector<WebElement> list_items = select_element.listItems();
813 for (size_t i = 0; i < list_items.size(); ++i) { 807 for (size_t i = 0; i < list_items.size(); ++i) {
814 if (IsOptionElement(list_items[i])) { 808 if (IsOptionElement(list_items[i])) {
815 const WebOptionElement option_element = 809 const WebOptionElement option_element =
816 list_items[i].toConst<WebOptionElement>(); 810 list_items[i].toConst<WebOptionElement>();
817 if (option_element.value() == value) { 811 if (option_element.value() == value) {
818 value = option_element.text(); 812 value = option_element.text();
819 break; 813 break;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 958
965 // Copy the created FormFields into the resulting FormData object. 959 // Copy the created FormFields into the resulting FormData object.
966 for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin(); 960 for (ScopedVector<FormFieldData>::const_iterator iter = form_fields.begin();
967 iter != form_fields.end(); ++iter) { 961 iter != form_fields.end(); ++iter) {
968 form->fields.push_back(**iter); 962 form->fields.push_back(**iter);
969 } 963 }
970 964
971 return true; 965 return true;
972 } 966 }
973 967
974 bool FindFormAndFieldForInputElement(const WebInputElement& element, 968 bool FindFormAndFieldForFormControlElement(const WebFormControlElement& element,
975 FormData* form, 969 FormData* form,
976 FormFieldData* field, 970 FormFieldData* field,
977 RequirementsMask requirements) { 971 RequirementsMask requirements) {
978 if (!IsAutofillableElement(element)) 972 if (!IsAutofillableElement(element))
979 return false; 973 return false;
980 974
981 const WebFormElement form_element = element.form(); 975 const WebFormElement form_element = element.form();
982 if (form_element.isNull()) 976 if (form_element.isNull())
983 return false; 977 return false;
984 978
985 ExtractMask extract_mask = 979 ExtractMask extract_mask =
986 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); 980 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
987 return WebFormElementToFormData(form_element, 981 return WebFormElementToFormData(form_element,
988 element, 982 element,
989 requirements, 983 requirements,
990 extract_mask, 984 extract_mask,
991 form, 985 form,
992 field); 986 field);
993 } 987 }
994 988
995 void FillForm(const FormData& form, const WebInputElement& element) { 989 void FillForm(const FormData& form, const WebFormControlElement& element) {
996 WebFormElement form_element = element.form(); 990 WebFormElement form_element = element.form();
997 if (form_element.isNull()) 991 if (form_element.isNull())
998 return; 992 return;
999 993
1000 ForEachMatchingFormField(form_element, 994 ForEachMatchingFormField(form_element,
1001 element, 995 element,
1002 form, 996 form,
1003 FILTER_ALL_NON_EDITIABLE_ELEMENTS, 997 FILTER_ALL_NON_EDITIABLE_ELEMENTS,
1004 false, /* dont force override */ 998 false, /* dont force override */
1005 &FillFormField); 999 &FillFormField);
(...skipping 20 matching lines...) Expand all
1026 return; 1020 return;
1027 1021
1028 ForEachMatchingFormField(form_element, 1022 ForEachMatchingFormField(form_element,
1029 WebInputElement(), 1023 WebInputElement(),
1030 form_data, 1024 form_data,
1031 FILTER_NONE, 1025 FILTER_NONE,
1032 true, /* force override */ 1026 true, /* force override */
1033 &FillFormField); 1027 &FillFormField);
1034 } 1028 }
1035 1029
1036 void PreviewForm(const FormData& form, const WebInputElement& element) { 1030 void PreviewForm(const FormData& form, const WebFormControlElement& element) {
1037 WebFormElement form_element = element.form(); 1031 WebFormElement form_element = element.form();
1038 if (form_element.isNull()) 1032 if (form_element.isNull())
1039 return; 1033 return;
1040 1034
1041 ForEachMatchingFormField(form_element, 1035 ForEachMatchingFormField(form_element,
1042 element, 1036 element,
1043 form, 1037 form,
1044 FILTER_ALL_NON_EDITIABLE_ELEMENTS, 1038 FILTER_ALL_NON_EDITIABLE_ELEMENTS,
1045 false, /* dont force override */ 1039 false, /* dont force override */
1046 &PreviewFormField); 1040 &PreviewFormField);
1047 } 1041 }
1048 1042
1049 bool ClearPreviewedFormWithElement(const WebInputElement& element, 1043 bool ClearPreviewedFormWithElement(const WebFormControlElement& element,
1050 bool was_autofilled) { 1044 bool was_autofilled) {
1051 WebFormElement form_element = element.form(); 1045 WebFormElement form_element = element.form();
1052 if (form_element.isNull()) 1046 if (form_element.isNull())
1053 return false; 1047 return false;
1054 1048
1055 std::vector<WebFormControlElement> control_elements; 1049 std::vector<WebFormControlElement> control_elements;
1056 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, 1050 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE,
1057 &control_elements); 1051 &control_elements);
1058 for (size_t i = 0; i < control_elements.size(); ++i) { 1052 for (size_t i = 0; i < control_elements.size(); ++i) {
1059 // There might be unrelated elements in this form which have already been 1053 // There might be unrelated elements in this form which have already been
1060 // auto-filled. For example, the user might have already filled the address 1054 // auto-filled. For example, the user might have already filled the address
1061 // part of a form and now be dealing with the credit card section. We only 1055 // part of a form and now be dealing with the credit card section. We only
1062 // want to reset the auto-filled status for fields that were previewed. 1056 // want to reset the auto-filled status for fields that were previewed.
1063 WebFormControlElement control_element = control_elements[i]; 1057 WebFormControlElement control_element = control_elements[i];
1064 1058
1065 // Only text input and textarea elements can be previewed. 1059 // Only text input and textarea elements can be previewed.
1066 WebInputElement* input_element = toWebInputElement(&control_element); 1060 WebInputElement* input_element = toWebInputElement(&control_element);
1067 if (!IsTextInput(input_element) && 1061 if (!IsTextInput(input_element) &&
1068 !IsMonthInput(input_element) && 1062 !IsMonthInput(input_element) &&
1069 !IsTextAreaElement(control_element)) 1063 !IsTextAreaElement(control_element))
1070 continue; 1064 continue;
1071 1065
1072 // If the element is not auto-filled, we did not preview it, 1066 // If the element is not auto-filled, we did not preview it,
1073 // so there is nothing to reset. 1067 // so there is nothing to reset.
1074 if(!control_element.isAutofilled()) 1068 if(!control_element.isAutofilled())
1075 continue; 1069 continue;
1076 1070
1077 if ((IsTextInput(input_element) && 1071 if ((IsTextInput(input_element) ||
1078 input_element->suggestedValue().isEmpty()) || 1072 IsMonthInput(input_element) ||
1079 (IsMonthInput(input_element) && 1073 IsTextAreaElement(control_element)) &&
1080 input_element->suggestedValue().isEmpty()) || 1074 control_element.suggestedValue().isEmpty())
1081 (IsTextAreaElement(control_element) &&
1082 control_element.to<WebTextAreaElement>().suggestedValue().isEmpty()))
1083 continue; 1075 continue;
1084 1076
1085 // Clear the suggested value. For the initiating node, also restore the 1077 // Clear the suggested value. For the initiating node, also restore the
1086 // original value. 1078 // original value.
1087 if (IsTextInput(input_element) || IsMonthInput(input_element)) { 1079 if (IsTextInput(input_element) || IsMonthInput(input_element) ||
1088 input_element->setSuggestedValue(WebString()); 1080 IsTextAreaElement(control_element)) {
1089 bool is_initiating_node = (element == *input_element); 1081 control_element.setSuggestedValue(WebString());
1090 if (is_initiating_node) 1082 bool is_initiating_node = (element == control_element);
1091 input_element->setAutofilled(was_autofilled);
1092 else
1093 input_element->setAutofilled(false);
1094
1095 // Clearing the suggested value in the focused node (above) can cause
1096 // selection to be lost. We force selection range to restore the text
1097 // cursor.
1098 if (is_initiating_node) { 1083 if (is_initiating_node) {
1099 int length = input_element->value().length(); 1084 control_element.setAutofilled(was_autofilled);
1100 input_element->setSelectionRange(length, length); 1085 // Clearing the suggested value in the focused node (above) can cause
1086 // selection to be lost. We force selection range to restore the text
1087 // cursor.
1088 int length = control_element.value().length();
1089 control_element.setSelectionRange(length, length);
1090 } else {
1091 control_element.setAutofilled(false);
1101 } 1092 }
1102 } else if (IsTextAreaElement(control_element)) {
1103 WebTextAreaElement text_area = control_element.to<WebTextAreaElement>();
1104 text_area.setSuggestedValue(WebString());
1105 bool is_initiating_node = (element == text_area);
1106 if (is_initiating_node)
1107 control_element.setAutofilled(was_autofilled);
1108 else
1109 control_element.setAutofilled(false);
1110 } 1093 }
1111 } 1094 }
1112 1095
1113 return true; 1096 return true;
1114 } 1097 }
1115 1098
1116 bool FormWithElementIsAutofilled(const WebInputElement& element) { 1099 bool FormWithElementIsAutofilled(const WebInputElement& element) {
1117 WebFormElement form_element = element.form(); 1100 WebFormElement form_element = element.form();
1118 if (form_element.isNull()) 1101 if (form_element.isNull())
1119 return false; 1102 return false;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1177 tag_is_allowed = true; 1160 tag_is_allowed = true;
1178 break; 1161 break;
1179 } 1162 }
1180 } 1163 }
1181 if (!tag_is_allowed) 1164 if (!tag_is_allowed)
1182 return false; 1165 return false;
1183 } 1166 }
1184 return true; 1167 return true;
1185 } 1168 }
1186 1169
1187 gfx::RectF GetScaledBoundingBox(float scale, WebInputElement* element) { 1170 gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) {
1188 gfx::Rect bounding_box(element->boundsInViewportSpace()); 1171 gfx::Rect bounding_box(element->boundsInViewportSpace());
1189 return gfx::RectF(bounding_box.x() * scale, 1172 return gfx::RectF(bounding_box.x() * scale,
1190 bounding_box.y() * scale, 1173 bounding_box.y() * scale,
1191 bounding_box.width() * scale, 1174 bounding_box.width() * scale,
1192 bounding_box.height() * scale); 1175 bounding_box.height() * scale);
1193 } 1176 }
1194 1177
1195 } // namespace autofill 1178 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698