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

Side by Side Diff: chrome/browser/autofill/autofill_manager.cc

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

Powered by Google App Engine
This is Rietveld 408576698