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" |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 for (size_t i = 0; i < list_items.size(); ++i) { | 406 for (size_t i = 0; i < list_items.size(); ++i) { |
407 if (IsOptionElement(list_items[i])) { | 407 if (IsOptionElement(list_items[i])) { |
408 const WebOptionElement option = list_items[i].toConst<WebOptionElement>(); | 408 const WebOptionElement option = list_items[i].toConst<WebOptionElement>(); |
409 option_values->push_back(option.value()); | 409 option_values->push_back(option.value()); |
410 option_contents->push_back(option.text()); | 410 option_contents->push_back(option.text()); |
411 } | 411 } |
412 } | 412 } |
413 } | 413 } |
414 | 414 |
415 // The callback type used by |ForEachMatchingFormField()|. | 415 // The callback type used by |ForEachMatchingFormField()|. |
416 typedef void (*Callback)(WebKit::WebFormControlElement*, | 416 typedef void (*Callback)(const FormFieldData&, |
417 const FormFieldData*, | 417 bool, |
418 bool); | 418 WebKit::WebFormControlElement*); |
419 | 419 |
420 // For each autofillable field in |data| that matches a field in the |form|, | 420 // For each autofillable field in |data| that matches a field in the |form|, |
421 // the |callback| is invoked with the corresponding |form| field data. | 421 // the |callback| is invoked with the corresponding |form| field data. |
422 void ForEachMatchingFormField(const WebFormElement& form_element, | 422 void ForEachMatchingFormField(const WebFormElement& form_element, |
423 const WebElement& initiating_element, | 423 const WebElement& initiating_element, |
424 const FormData& data, | 424 const FormData& data, |
425 Callback callback) { | 425 Callback callback) { |
426 std::vector<WebFormControlElement> control_elements; | 426 std::vector<WebFormControlElement> control_elements; |
427 ExtractAutofillableElements(form_element, autofill::REQUIRE_AUTOCOMPLETE, | 427 ExtractAutofillableElements(form_element, autofill::REQUIRE_AUTOCOMPLETE, |
428 &control_elements); | 428 &control_elements); |
(...skipping 30 matching lines...) Expand all Loading... | |
459 | 459 |
460 const WebInputElement* input_element = toWebInputElement(element); | 460 const WebInputElement* input_element = toWebInputElement(element); |
461 if (IsTextInput(input_element)) { | 461 if (IsTextInput(input_element)) { |
462 // Only autofill empty fields and the field that initiated the filling, | 462 // Only autofill empty fields and the field that initiated the filling, |
463 // i.e. the field the user is currently editing and interacting with. | 463 // i.e. the field the user is currently editing and interacting with. |
464 if (!is_initiating_element && !input_element->value().isEmpty()) | 464 if (!is_initiating_element && !input_element->value().isEmpty()) |
465 continue; | 465 continue; |
466 } | 466 } |
467 | 467 |
468 if (!element->isEnabled() || element->isReadOnly() || | 468 if (!element->isEnabled() || element->isReadOnly() || |
469 !element->isFocusable()) | 469 !element->isFocusable()) |
Ilya Sherman
2012/11/30 01:26:54
We'll need to add a parameter to this method to ig
Dan Beam
2012/11/30 03:01:16
Done.
| |
470 continue; | 470 continue; |
471 | 471 |
472 callback(element, &data.fields[i], is_initiating_element); | 472 callback(data.fields[i], is_initiating_element, element); |
473 } | 473 } |
474 } | 474 } |
475 | 475 |
476 // Sets the |field|'s value to the value in |data|. | 476 // Sets the |field|'s value to the value in |data|. |
477 // Also sets the "autofilled" attribute, causing the background to be yellow. | 477 // Also sets the "autofilled" attribute, causing the background to be yellow. |
478 void FillFormField(WebKit::WebFormControlElement* field, | 478 void FillFormField(const FormFieldData& data, |
479 const FormFieldData* data, | 479 bool is_initiating_node, |
480 bool is_initiating_node) { | 480 WebKit::WebFormControlElement* field) { |
481 // Nothing to fill. | 481 // Nothing to fill. |
482 if (data->value.empty()) | 482 if (data.value.empty()) |
483 return; | 483 return; |
484 | 484 |
485 WebInputElement* input_element = toWebInputElement(field); | 485 WebInputElement* input_element = toWebInputElement(field); |
486 if (IsTextInput(input_element)) { | 486 if (IsTextInput(input_element)) { |
487 // If the maxlength attribute contains a negative value, maxLength() | 487 // If the maxlength attribute contains a negative value, maxLength() |
488 // returns the default maxlength value. | 488 // returns the default maxlength value. |
489 input_element->setValue( | 489 input_element->setValue( |
490 data->value.substr(0, input_element->maxLength()), true); | 490 data.value.substr(0, input_element->maxLength()), true); |
491 input_element->setAutofilled(true); | 491 input_element->setAutofilled(true); |
492 if (is_initiating_node) { | 492 if (is_initiating_node) { |
493 int length = input_element->value().length(); | 493 int length = input_element->value().length(); |
494 input_element->setSelectionRange(length, length); | 494 input_element->setSelectionRange(length, length); |
495 // Clear the current IME composition (the underline), if there is one. | 495 // Clear the current IME composition (the underline), if there is one. |
496 input_element->document().frame()->unmarkText(); | 496 input_element->document().frame()->unmarkText(); |
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(const FormFieldData& data, |
511 const FormFieldData* data, | 511 bool is_initiating_node, |
512 bool is_initiating_node) { | 512 WebKit::WebFormControlElement* field) { |
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 |
522 // If the maxlength attribute contains a negative value, maxLength() | 522 // If the maxlength attribute contains a negative value, maxLength() |
523 // returns the default maxlength value. | 523 // returns the default maxlength value. |
524 input_element->setSuggestedValue( | 524 input_element->setSuggestedValue( |
525 data->value.substr(0, input_element->maxLength())); | 525 data.value.substr(0, input_element->maxLength())); |
526 input_element->setAutofilled(true); | 526 input_element->setAutofilled(true); |
527 if (is_initiating_node) { | 527 if (is_initiating_node) { |
528 // Select the part of the text that the user didn't type. | 528 // Select the part of the text that the user didn't type. |
529 input_element->setSelectionRange(input_element->value().length(), | 529 input_element->setSelectionRange(input_element->value().length(), |
530 input_element->suggestedValue().length()); | 530 input_element->suggestedValue().length()); |
531 } | 531 } |
532 } | 532 } |
533 | 533 |
534 } // namespace | 534 } // namespace |
535 | 535 |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
806 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); | 806 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); |
807 return WebFormElementToFormData(form_element, | 807 return WebFormElementToFormData(form_element, |
808 element, | 808 element, |
809 requirements, | 809 requirements, |
810 extract_mask, | 810 extract_mask, |
811 form, | 811 form, |
812 field); | 812 field); |
813 } | 813 } |
814 | 814 |
815 void FillForm(const FormData& form, const WebInputElement& element) { | 815 void FillForm(const FormData& form, const WebInputElement& element) { |
816 WebFormElement form_element = element.form(); | 816 FillForm(form, element.form()); |
817 } | |
818 | |
819 void FillForm(const FormData& form_data, const WebFormElement& form_element) { | |
817 if (form_element.isNull()) | 820 if (form_element.isNull()) |
818 return; | 821 return; |
819 | 822 |
820 ForEachMatchingFormField(form_element, | 823 ForEachMatchingFormField(form_element, |
821 element, | 824 WebInputElement(), |
822 form, | 825 form_data, |
823 &FillFormField); | 826 &FillFormField); |
824 } | 827 } |
825 | 828 |
826 void PreviewForm(const FormData& form, const WebInputElement& element) { | 829 void PreviewForm(const FormData& form, const WebInputElement& element) { |
827 WebFormElement form_element = element.form(); | 830 WebFormElement form_element = element.form(); |
828 if (form_element.isNull()) | 831 if (form_element.isNull()) |
829 return; | 832 return; |
830 | 833 |
831 ForEachMatchingFormField(form_element, | 834 ForEachMatchingFormField(form_element, |
832 element, | 835 element, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
896 continue; | 899 continue; |
897 | 900 |
898 if (input_element->isAutofilled()) | 901 if (input_element->isAutofilled()) |
899 return true; | 902 return true; |
900 } | 903 } |
901 | 904 |
902 return false; | 905 return false; |
903 } | 906 } |
904 | 907 |
905 } // namespace autofill | 908 } // namespace autofill |
OLD | NEW |