OLD | NEW |
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 using blink::WebNodeList; | 47 using blink::WebNodeList; |
48 using blink::WebOptionElement; | 48 using blink::WebOptionElement; |
49 using blink::WebSelectElement; | 49 using blink::WebSelectElement; |
50 using blink::WebTextAreaElement; | 50 using blink::WebTextAreaElement; |
51 using blink::WebString; | 51 using blink::WebString; |
52 using blink::WebVector; | 52 using blink::WebVector; |
53 | 53 |
54 namespace autofill { | 54 namespace autofill { |
55 namespace { | 55 namespace { |
56 | 56 |
57 // A bit field mask for FillForm functions to not fill some fields. | |
58 enum FieldFilterMask { | |
59 FILTER_NONE = 0, | |
60 FILTER_DISABLED_ELEMENTS = 1 << 0, | |
61 FILTER_READONLY_ELEMENTS = 1 << 1, | |
62 FILTER_NON_FOCUSABLE_ELEMENTS = 1 << 2, | |
63 FILTER_ALL_NON_EDITIABLE_ELEMENTS = FILTER_DISABLED_ELEMENTS | | |
64 FILTER_READONLY_ELEMENTS | | |
65 FILTER_NON_FOCUSABLE_ELEMENTS, | |
66 }; | |
67 | |
68 bool IsOptionElement(const WebElement& element) { | 57 bool IsOptionElement(const WebElement& element) { |
69 CR_DEFINE_STATIC_LOCAL(WebString, kOption, ("option")); | 58 CR_DEFINE_STATIC_LOCAL(WebString, kOption, ("option")); |
70 return element.hasHTMLTagName(kOption); | 59 return element.hasHTMLTagName(kOption); |
71 } | 60 } |
72 | 61 |
73 bool IsScriptElement(const WebElement& element) { | 62 bool IsScriptElement(const WebElement& element) { |
74 CR_DEFINE_STATIC_LOCAL(WebString, kScript, ("script")); | 63 CR_DEFINE_STATIC_LOCAL(WebString, kScript, ("script")); |
75 return element.hasHTMLTagName(kScript); | 64 return element.hasHTMLTagName(kScript); |
76 } | 65 } |
77 | 66 |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 // The callback type used by |ForEachMatchingFormField()|. | 458 // The callback type used by |ForEachMatchingFormField()|. |
470 typedef void (*Callback)(const FormFieldData&, | 459 typedef void (*Callback)(const FormFieldData&, |
471 bool, /* is_initiating_element */ | 460 bool, /* is_initiating_element */ |
472 blink::WebFormControlElement*); | 461 blink::WebFormControlElement*); |
473 | 462 |
474 // For each autofillable field in |data| that matches a field in the |form|, | 463 // For each autofillable field in |data| that matches a field in the |form|, |
475 // the |callback| is invoked with the corresponding |form| field data. | 464 // the |callback| is invoked with the corresponding |form| field data. |
476 void ForEachMatchingFormField(const WebFormElement& form_element, | 465 void ForEachMatchingFormField(const WebFormElement& form_element, |
477 const WebElement& initiating_element, | 466 const WebElement& initiating_element, |
478 const FormData& data, | 467 const FormData& data, |
479 FieldFilterMask filters, | 468 bool only_focusable_elements, |
480 bool force_override, | 469 bool force_override, |
481 Callback callback) { | 470 Callback callback) { |
482 std::vector<WebFormControlElement> control_elements; | 471 std::vector<WebFormControlElement> control_elements; |
483 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, | 472 ExtractAutofillableElements(form_element, REQUIRE_AUTOCOMPLETE, |
484 &control_elements); | 473 &control_elements); |
485 | 474 |
486 if (control_elements.size() != data.fields.size()) { | 475 if (control_elements.size() != data.fields.size()) { |
487 // This case should be reachable only for pathological websites and tests, | 476 // This case should be reachable only for pathological websites and tests, |
488 // which add or remove form fields while the user is interacting with the | 477 // which add or remove form fields while the user is interacting with the |
489 // Autofill popup. | 478 // Autofill popup. |
(...skipping 22 matching lines...) Expand all Loading... |
512 | 501 |
513 // Only autofill empty fields and the field that initiated the filling, | 502 // Only autofill empty fields and the field that initiated the filling, |
514 // i.e. the field the user is currently editing and interacting with. | 503 // i.e. the field the user is currently editing and interacting with. |
515 const WebInputElement* input_element = toWebInputElement(element); | 504 const WebInputElement* input_element = toWebInputElement(element); |
516 if (!force_override && !is_initiating_element && | 505 if (!force_override && !is_initiating_element && |
517 ((IsAutofillableInputElement(input_element) || | 506 ((IsAutofillableInputElement(input_element) || |
518 IsTextAreaElement(*element)) && | 507 IsTextAreaElement(*element)) && |
519 !element->value().isEmpty())) | 508 !element->value().isEmpty())) |
520 continue; | 509 continue; |
521 | 510 |
522 if (((filters & FILTER_DISABLED_ELEMENTS) && !element->isEnabled()) || | 511 if (!element->isEnabled() || element->isReadOnly() || |
523 ((filters & FILTER_READONLY_ELEMENTS) && element->isReadOnly()) || | 512 (only_focusable_elements && !element->isFocusable())) |
524 ((filters & FILTER_NON_FOCUSABLE_ELEMENTS) && !element->isFocusable())) | |
525 continue; | 513 continue; |
526 | 514 |
527 callback(data.fields[i], is_initiating_element, element); | 515 callback(data.fields[i], is_initiating_element, element); |
528 } | 516 } |
529 } | 517 } |
530 | 518 |
531 // Sets the |field|'s value to the value in |data|. | 519 // Sets the |field|'s value to the value in |data|. |
532 // Also sets the "autofilled" attribute, causing the background to be yellow. | 520 // Also sets the "autofilled" attribute, causing the background to be yellow. |
533 void FillFormField(const FormFieldData& data, | 521 void FillFormField(const FormFieldData& data, |
534 bool is_initiating_node, | 522 bool is_initiating_node, |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 } | 982 } |
995 | 983 |
996 void FillForm(const FormData& form, const WebFormControlElement& element) { | 984 void FillForm(const FormData& form, const WebFormControlElement& element) { |
997 WebFormElement form_element = element.form(); | 985 WebFormElement form_element = element.form(); |
998 if (form_element.isNull()) | 986 if (form_element.isNull()) |
999 return; | 987 return; |
1000 | 988 |
1001 ForEachMatchingFormField(form_element, | 989 ForEachMatchingFormField(form_element, |
1002 element, | 990 element, |
1003 form, | 991 form, |
1004 FILTER_ALL_NON_EDITIABLE_ELEMENTS, | 992 true, /* only_focusable_elements */ |
1005 false, /* dont force override */ | 993 false, /* don't force override */ |
1006 &FillFormField); | 994 &FillFormField); |
1007 } | 995 } |
1008 | 996 |
1009 void FillFormIncludingNonFocusableElements(const FormData& form_data, | 997 void FillFormIncludingNonFocusableElements(const FormData& form_data, |
1010 const WebFormElement& form_element) { | 998 const WebFormElement& form_element) { |
1011 if (form_element.isNull()) | 999 if (form_element.isNull()) |
1012 return; | 1000 return; |
1013 | 1001 |
1014 FieldFilterMask filter_mask = static_cast<FieldFilterMask>( | |
1015 FILTER_DISABLED_ELEMENTS | FILTER_READONLY_ELEMENTS); | |
1016 ForEachMatchingFormField(form_element, | |
1017 WebInputElement(), | |
1018 form_data, | |
1019 filter_mask, | |
1020 true, /* force override */ | |
1021 &FillFormField); | |
1022 } | |
1023 | |
1024 void FillFormForAllElements(const FormData& form_data, | |
1025 const WebFormElement& form_element) { | |
1026 if (form_element.isNull()) | |
1027 return; | |
1028 | |
1029 ForEachMatchingFormField(form_element, | 1002 ForEachMatchingFormField(form_element, |
1030 WebInputElement(), | 1003 WebInputElement(), |
1031 form_data, | 1004 form_data, |
1032 FILTER_NONE, | 1005 false, /* only_focusable_elements */ |
1033 true, /* force override */ | 1006 true, /* force override */ |
1034 &FillFormField); | 1007 &FillFormField); |
1035 } | 1008 } |
1036 | 1009 |
1037 void PreviewForm(const FormData& form, const WebFormControlElement& element) { | 1010 void PreviewForm(const FormData& form, const WebFormControlElement& element) { |
1038 WebFormElement form_element = element.form(); | 1011 WebFormElement form_element = element.form(); |
1039 if (form_element.isNull()) | 1012 if (form_element.isNull()) |
1040 return; | 1013 return; |
1041 | 1014 |
1042 ForEachMatchingFormField(form_element, | 1015 ForEachMatchingFormField(form_element, |
1043 element, | 1016 element, |
1044 form, | 1017 form, |
1045 FILTER_ALL_NON_EDITIABLE_ELEMENTS, | 1018 true, /* only_focusable_elements */ |
1046 false, /* dont force override */ | 1019 false, /* dont force override */ |
1047 &PreviewFormField); | 1020 &PreviewFormField); |
1048 } | 1021 } |
1049 | 1022 |
1050 bool ClearPreviewedFormWithElement(const WebFormControlElement& element, | 1023 bool ClearPreviewedFormWithElement(const WebFormControlElement& element, |
1051 bool was_autofilled) { | 1024 bool was_autofilled) { |
1052 WebFormElement form_element = element.form(); | 1025 WebFormElement form_element = element.form(); |
1053 if (form_element.isNull()) | 1026 if (form_element.isNull()) |
1054 return false; | 1027 return false; |
1055 | 1028 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1181 | 1154 |
1182 gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) { | 1155 gfx::RectF GetScaledBoundingBox(float scale, WebFormControlElement* element) { |
1183 gfx::Rect bounding_box(element->boundsInViewportSpace()); | 1156 gfx::Rect bounding_box(element->boundsInViewportSpace()); |
1184 return gfx::RectF(bounding_box.x() * scale, | 1157 return gfx::RectF(bounding_box.x() * scale, |
1185 bounding_box.y() * scale, | 1158 bounding_box.y() * scale, |
1186 bounding_box.width() * scale, | 1159 bounding_box.width() * scale, |
1187 bounding_box.height() * scale); | 1160 bounding_box.height() * scale); |
1188 } | 1161 } |
1189 | 1162 |
1190 } // namespace autofill | 1163 } // namespace autofill |
OLD | NEW |