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/browser/autofill/autofill_manager.h" | 5 #include "chrome/browser/autofill/autofill_manager.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 std::vector<int> unique_ids; | 325 std::vector<int> unique_ids; |
326 | 326 |
327 RenderViewHost* host = NULL; | 327 RenderViewHost* host = NULL; |
328 FormStructure* form_structure = NULL; | 328 FormStructure* form_structure = NULL; |
329 AutofillField* autofill_field = NULL; | 329 AutofillField* autofill_field = NULL; |
330 if (GetHost( | 330 if (GetHost( |
331 personal_data_->profiles(), personal_data_->credit_cards(), &host) && | 331 personal_data_->profiles(), personal_data_->credit_cards(), &host) && |
332 FindCachedFormAndField(form, field, &form_structure, &autofill_field) && | 332 FindCachedFormAndField(form, field, &form_structure, &autofill_field) && |
333 // Don't send suggestions for forms that aren't auto-fillable. | 333 // Don't send suggestions for forms that aren't auto-fillable. |
334 form_structure->IsAutoFillable(false)) { | 334 form_structure->IsAutoFillable(false)) { |
335 AutofillType type(autofill_field->type()); | 335 AutofillFieldType type = autofill_field->type(); |
336 bool is_filling_credit_card = (type.group() == AutofillType::CREDIT_CARD); | 336 bool is_filling_credit_card = |
| 337 (AutofillType(type).group() == AutofillType::CREDIT_CARD); |
337 if (is_filling_credit_card) { | 338 if (is_filling_credit_card) { |
338 GetCreditCardSuggestions( | 339 GetCreditCardSuggestions( |
339 form_structure, field, type, &values, &labels, &icons, &unique_ids); | 340 form_structure, field, type, &values, &labels, &icons, &unique_ids); |
340 } else { | 341 } else { |
341 GetProfileSuggestions( | 342 GetProfileSuggestions( |
342 form_structure, field, type, &values, &labels, &icons, &unique_ids); | 343 form_structure, field, type, &values, &labels, &icons, &unique_ids); |
343 } | 344 } |
344 | 345 |
345 DCHECK_EQ(values.size(), labels.size()); | 346 DCHECK_EQ(values.size(), labels.size()); |
346 DCHECK_EQ(values.size(), icons.size()); | 347 DCHECK_EQ(values.size(), icons.size()); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 §ion_start, §ion_end); | 445 §ion_start, §ion_end); |
445 | 446 |
446 FormData result = form; | 447 FormData result = form; |
447 | 448 |
448 // If the relevant section is auto-filled, we should fill |field| but not the | 449 // If the relevant section is auto-filled, we should fill |field| but not the |
449 // rest of the form. | 450 // rest of the form. |
450 if (SectionIsAutoFilled(form_structure, form, section_start, section_end)) { | 451 if (SectionIsAutoFilled(form_structure, form, section_start, section_end)) { |
451 for (std::vector<FormField>::iterator iter = result.fields.begin(); | 452 for (std::vector<FormField>::iterator iter = result.fields.begin(); |
452 iter != result.fields.end(); ++iter) { | 453 iter != result.fields.end(); ++iter) { |
453 if ((*iter) == field) { | 454 if ((*iter) == field) { |
454 AutofillType autofill_type(autofill_field->type()); | 455 AutofillFieldType field_type = autofill_field->type(); |
455 if (profile) { | 456 if (profile) { |
456 DCHECK_NE(AutofillType::CREDIT_CARD, autofill_type.group()); | 457 DCHECK_NE(AutofillType::CREDIT_CARD, |
457 FillFormField(profile, autofill_type, &(*iter)); | 458 AutofillType(field_type).group()); |
| 459 FillFormField(profile, field_type, &(*iter)); |
458 } else { | 460 } else { |
459 DCHECK_EQ(AutofillType::CREDIT_CARD, autofill_type.group()); | 461 DCHECK_EQ(AutofillType::CREDIT_CARD, |
460 FillCreditCardFormField(credit_card, autofill_type, &(*iter)); | 462 AutofillType(field_type).group()); |
| 463 FillCreditCardFormField(credit_card, field_type, &(*iter)); |
461 } | 464 } |
462 break; | 465 break; |
463 } | 466 } |
464 } | 467 } |
465 | 468 |
466 host->Send(new AutoFillMsg_FormDataFilled(host->routing_id(), query_id, | 469 host->Send(new AutoFillMsg_FormDataFilled(host->routing_id(), query_id, |
467 result)); | 470 result)); |
468 return; | 471 return; |
469 } | 472 } |
470 | 473 |
(...skipping 11 matching lines...) Expand all Loading... |
482 // Search forward in the |form_structure| for a corresponding field. | 485 // Search forward in the |form_structure| for a corresponding field. |
483 while (k < section_end && *form_structure->field(k) != result.fields[j]) { | 486 while (k < section_end && *form_structure->field(k) != result.fields[j]) { |
484 k++; | 487 k++; |
485 } | 488 } |
486 | 489 |
487 // If we've found a match then fill the |result| field with the found | 490 // If we've found a match then fill the |result| field with the found |
488 // field in the |form_structure|. | 491 // field in the |form_structure|. |
489 if (k >= section_end) | 492 if (k >= section_end) |
490 continue; | 493 continue; |
491 | 494 |
492 AutofillType autofill_type(form_structure->field(k)->type()); | 495 AutofillFieldType field_type = form_structure->field(k)->type(); |
493 if (autofill_type.group() != AutofillType::NO_GROUP) { | 496 FieldTypeGroup field_group_type = AutofillType(field_type).group(); |
| 497 if (field_group_type != AutofillType::NO_GROUP) { |
494 if (profile) { | 498 if (profile) { |
495 DCHECK_NE(AutofillType::CREDIT_CARD, autofill_type.group()); | 499 DCHECK_NE(AutofillType::CREDIT_CARD, field_group_type); |
496 FillFormField(profile, autofill_type, &result.fields[j]); | 500 FillFormField(profile, field_type, &result.fields[j]); |
497 } else { | 501 } else { |
498 DCHECK_EQ(AutofillType::CREDIT_CARD, autofill_type.group()); | 502 DCHECK_EQ(AutofillType::CREDIT_CARD, field_group_type); |
499 FillCreditCardFormField(credit_card, autofill_type, &result.fields[j]); | 503 FillCreditCardFormField(credit_card, field_type, &result.fields[j]); |
500 } | 504 } |
501 } | 505 } |
502 | 506 |
503 // We found a matching field in the |form_structure| so we | 507 // We found a matching field in the |form_structure| so we |
504 // proceed to the next |result| field, and the next |form_structure|. | 508 // proceed to the next |result| field, and the next |form_structure|. |
505 ++i; | 509 ++i; |
506 } | 510 } |
507 autofilled_forms_signatures_.push_front(form_structure->FormSignature()); | 511 autofilled_forms_signatures_.push_front(form_structure->FormSignature()); |
508 | 512 |
509 host->Send(new AutoFillMsg_FormDataFilled( | 513 host->Send(new AutoFillMsg_FormDataFilled( |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 } | 722 } |
719 | 723 |
720 if (!(*autofill_field)) | 724 if (!(*autofill_field)) |
721 return false; | 725 return false; |
722 | 726 |
723 return true; | 727 return true; |
724 } | 728 } |
725 | 729 |
726 void AutofillManager::GetProfileSuggestions(FormStructure* form, | 730 void AutofillManager::GetProfileSuggestions(FormStructure* form, |
727 const FormField& field, | 731 const FormField& field, |
728 AutofillType type, | 732 AutofillFieldType type, |
729 std::vector<string16>* values, | 733 std::vector<string16>* values, |
730 std::vector<string16>* labels, | 734 std::vector<string16>* labels, |
731 std::vector<string16>* icons, | 735 std::vector<string16>* icons, |
732 std::vector<int>* unique_ids) { | 736 std::vector<int>* unique_ids) { |
733 const std::vector<AutofillProfile*>& profiles = personal_data_->profiles(); | 737 const std::vector<AutofillProfile*>& profiles = personal_data_->profiles(); |
734 std::vector<AutofillProfile*> matched_profiles; | 738 std::vector<AutofillProfile*> matched_profiles; |
735 for (std::vector<AutofillProfile*>::const_iterator iter = profiles.begin(); | 739 for (std::vector<AutofillProfile*>::const_iterator iter = profiles.begin(); |
736 iter != profiles.end(); ++iter) { | 740 iter != profiles.end(); ++iter) { |
737 AutofillProfile* profile = *iter; | 741 AutofillProfile* profile = *iter; |
738 | 742 |
(...skipping 13 matching lines...) Expand all Loading... |
752 for (std::vector<AutofillField*>::const_iterator iter = form->begin(); | 756 for (std::vector<AutofillField*>::const_iterator iter = form->begin(); |
753 iter != form->end(); ++iter) { | 757 iter != form->end(); ++iter) { |
754 // The field list is terminated with a NULL AutofillField, so don't try to | 758 // The field list is terminated with a NULL AutofillField, so don't try to |
755 // dereference it. | 759 // dereference it. |
756 if (!*iter) | 760 if (!*iter) |
757 break; | 761 break; |
758 form_fields.push_back((*iter)->type()); | 762 form_fields.push_back((*iter)->type()); |
759 } | 763 } |
760 | 764 |
761 AutofillProfile::CreateInferredLabels(&matched_profiles, &form_fields, | 765 AutofillProfile::CreateInferredLabels(&matched_profiles, &form_fields, |
762 type.field_type(), 1, labels); | 766 type, 1, labels); |
763 | 767 |
764 // No icons for profile suggestions. | 768 // No icons for profile suggestions. |
765 icons->resize(values->size()); | 769 icons->resize(values->size()); |
766 } | 770 } |
767 | 771 |
768 void AutofillManager::GetCreditCardSuggestions(FormStructure* form, | 772 void AutofillManager::GetCreditCardSuggestions(FormStructure* form, |
769 const FormField& field, | 773 const FormField& field, |
770 AutofillType type, | 774 AutofillFieldType type, |
771 std::vector<string16>* values, | 775 std::vector<string16>* values, |
772 std::vector<string16>* labels, | 776 std::vector<string16>* labels, |
773 std::vector<string16>* icons, | 777 std::vector<string16>* icons, |
774 std::vector<int>* unique_ids) { | 778 std::vector<int>* unique_ids) { |
775 for (std::vector<CreditCard*>::const_iterator iter = | 779 for (std::vector<CreditCard*>::const_iterator iter = |
776 personal_data_->credit_cards().begin(); | 780 personal_data_->credit_cards().begin(); |
777 iter != personal_data_->credit_cards().end(); ++iter) { | 781 iter != personal_data_->credit_cards().end(); ++iter) { |
778 CreditCard* credit_card = *iter; | 782 CreditCard* credit_card = *iter; |
779 | 783 |
780 // The value of the stored data for this field type in the |credit_card|. | 784 // The value of the stored data for this field type in the |credit_card|. |
781 string16 creditcard_field_value = credit_card->GetFieldText(type); | 785 string16 creditcard_field_value = credit_card->GetFieldText(type); |
782 if (!creditcard_field_value.empty() && | 786 if (!creditcard_field_value.empty() && |
783 StartsWith(creditcard_field_value, field.value, false)) { | 787 StartsWith(creditcard_field_value, field.value, false)) { |
784 if (type.field_type() == CREDIT_CARD_NUMBER) | 788 if (type == CREDIT_CARD_NUMBER) |
785 creditcard_field_value = credit_card->ObfuscatedNumber(); | 789 creditcard_field_value = credit_card->ObfuscatedNumber(); |
786 | 790 |
787 string16 label; | 791 string16 label; |
788 if (credit_card->number().empty()) { | 792 if (credit_card->number().empty()) { |
789 // If there is no CC number, return name to show something. | 793 // If there is no CC number, return name to show something. |
790 label = credit_card->GetFieldText(AutofillType(CREDIT_CARD_NAME)); | 794 label = credit_card->GetFieldText(CREDIT_CARD_NAME); |
791 } else { | 795 } else { |
792 label = kCreditCardPrefix; | 796 label = kCreditCardPrefix; |
793 label.append(credit_card->LastFourDigits()); | 797 label.append(credit_card->LastFourDigits()); |
794 } | 798 } |
795 | 799 |
796 values->push_back(creditcard_field_value); | 800 values->push_back(creditcard_field_value); |
797 labels->push_back(label); | 801 labels->push_back(label); |
798 icons->push_back(credit_card->type()); | 802 icons->push_back(credit_card->type()); |
799 unique_ids->push_back(PackGUIDs(credit_card->guid(), std::string())); | 803 unique_ids->push_back(PackGUIDs(credit_card->guid(), std::string())); |
800 } | 804 } |
801 } | 805 } |
802 } | 806 } |
803 | 807 |
804 void AutofillManager::FillCreditCardFormField(const CreditCard* credit_card, | 808 void AutofillManager::FillCreditCardFormField(const CreditCard* credit_card, |
805 AutofillType type, | 809 AutofillFieldType type, |
806 webkit_glue::FormField* field) { | 810 webkit_glue::FormField* field) { |
807 DCHECK(credit_card); | 811 DCHECK(credit_card); |
808 DCHECK_EQ(AutofillType::CREDIT_CARD, type.group()); | 812 DCHECK_EQ(AutofillType::CREDIT_CARD, AutofillType(type).group()); |
809 DCHECK(field); | 813 DCHECK(field); |
810 | 814 |
811 if (field->form_control_type == ASCIIToUTF16("select-one")) { | 815 if (field->form_control_type == ASCIIToUTF16("select-one")) { |
812 autofill::FillSelectControl(*credit_card, type, field); | 816 autofill::FillSelectControl(*credit_card, type, field); |
813 } else if (field->form_control_type == ASCIIToUTF16("month")) { | 817 } else if (field->form_control_type == ASCIIToUTF16("month")) { |
814 // HTML5 input="month" consists of year-month. | 818 // HTML5 input="month" consists of year-month. |
815 string16 year = credit_card->GetFieldText( | 819 string16 year = credit_card->GetFieldText(CREDIT_CARD_EXP_4_DIGIT_YEAR); |
816 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 820 string16 month = credit_card->GetFieldText(CREDIT_CARD_EXP_MONTH); |
817 string16 month = credit_card->GetFieldText( | |
818 AutofillType(CREDIT_CARD_EXP_MONTH)); | |
819 if (!year.empty() && !month.empty()) { | 821 if (!year.empty() && !month.empty()) { |
820 // Fill the value only if |credit_card| includes both year and month | 822 // Fill the value only if |credit_card| includes both year and month |
821 // information. | 823 // information. |
822 field->value = year + ASCIIToUTF16("-") + month; | 824 field->value = year + ASCIIToUTF16("-") + month; |
823 } | 825 } |
824 } else { | 826 } else { |
825 field->value = credit_card->GetFieldText(type); | 827 field->value = credit_card->GetFieldText(type); |
826 } | 828 } |
827 } | 829 } |
828 | 830 |
829 void AutofillManager::FillFormField(const AutofillProfile* profile, | 831 void AutofillManager::FillFormField(const AutofillProfile* profile, |
830 AutofillType type, | 832 AutofillFieldType type, |
831 webkit_glue::FormField* field) { | 833 webkit_glue::FormField* field) { |
832 DCHECK(profile); | 834 DCHECK(profile); |
833 DCHECK_NE(AutofillType::CREDIT_CARD, type.group()); | 835 DCHECK_NE(AutofillType::CREDIT_CARD, AutofillType(type).group()); |
834 DCHECK(field); | 836 DCHECK(field); |
835 | 837 |
836 if (type.subgroup() == AutofillType::PHONE_NUMBER) { | 838 if (AutofillType(type).subgroup() == AutofillType::PHONE_NUMBER) { |
837 FillPhoneNumberField(profile, type, field); | 839 FillPhoneNumberField(profile, type, field); |
838 } else { | 840 } else { |
839 if (field->form_control_type == ASCIIToUTF16("select-one")) | 841 if (field->form_control_type == ASCIIToUTF16("select-one")) |
840 autofill::FillSelectControl(*profile, type, field); | 842 autofill::FillSelectControl(*profile, type, field); |
841 else | 843 else |
842 field->value = profile->GetFieldText(type); | 844 field->value = profile->GetFieldText(type); |
843 } | 845 } |
844 } | 846 } |
845 | 847 |
846 void AutofillManager::FillPhoneNumberField(const AutofillProfile* profile, | 848 void AutofillManager::FillPhoneNumberField(const AutofillProfile* profile, |
847 AutofillType type, | 849 AutofillFieldType type, |
848 webkit_glue::FormField* field) { | 850 webkit_glue::FormField* field) { |
849 // If we are filling a phone number, check to see if the size field | 851 // If we are filling a phone number, check to see if the size field |
850 // matches the "prefix" or "suffix" sizes and fill accordingly. | 852 // matches the "prefix" or "suffix" sizes and fill accordingly. |
851 string16 number = profile->GetFieldText(AutofillType(type)); | 853 string16 number = profile->GetFieldText(type); |
852 bool has_valid_suffix_and_prefix = (number.length() == | 854 bool has_valid_suffix_and_prefix = (number.length() == |
853 static_cast<size_t>(PhoneNumber::kPrefixLength + | 855 static_cast<size_t>(PhoneNumber::kPrefixLength + |
854 PhoneNumber::kSuffixLength)); | 856 PhoneNumber::kSuffixLength)); |
855 if (has_valid_suffix_and_prefix && | 857 if (has_valid_suffix_and_prefix && |
856 field->max_length == PhoneNumber::kPrefixLength) { | 858 field->max_length == PhoneNumber::kPrefixLength) { |
857 number = number.substr(PhoneNumber::kPrefixOffset, | 859 number = number.substr(PhoneNumber::kPrefixOffset, |
858 PhoneNumber::kPrefixLength); | 860 PhoneNumber::kPrefixLength); |
859 field->value = number; | 861 field->value = number; |
860 } else if (has_valid_suffix_and_prefix && | 862 } else if (has_valid_suffix_and_prefix && |
861 field->max_length == PhoneNumber::kSuffixLength) { | 863 field->max_length == PhoneNumber::kSuffixLength) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 return std::string(); | 947 return std::string(); |
946 | 948 |
947 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); | 949 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); |
948 if (iter == id_guid_map_.end()) { | 950 if (iter == id_guid_map_.end()) { |
949 NOTREACHED(); | 951 NOTREACHED(); |
950 return std::string(); | 952 return std::string(); |
951 } | 953 } |
952 | 954 |
953 return iter->second; | 955 return iter->second; |
954 } | 956 } |
OLD | NEW |