| 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 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 while (k < form_structure->field_count() && | 186 while (k < form_structure->field_count() && |
| 187 *form_structure->field(k) != form.fields[j]) { | 187 *form_structure->field(k) != form.fields[j]) { |
| 188 k++; | 188 k++; |
| 189 } | 189 } |
| 190 | 190 |
| 191 // If we didn't find a match, continue on to the next |form| field. | 191 // If we didn't find a match, continue on to the next |form| field. |
| 192 if (k >= form_structure->field_count()) | 192 if (k >= form_structure->field_count()) |
| 193 continue; | 193 continue; |
| 194 | 194 |
| 195 AutofillType autofill_type(form_structure->field(k)->type()); | 195 AutofillType autofill_type(form_structure->field(k)->type()); |
| 196 if (form.fields[j].is_autofilled()) | 196 if (form.fields[j].is_autofilled) |
| 197 return true; | 197 return true; |
| 198 | 198 |
| 199 // We found a matching field in the |form_structure| so we | 199 // We found a matching field in the |form_structure| so we |
| 200 // proceed to the next |form| field, and the next |form_structure|. | 200 // proceed to the next |form| field, and the next |form_structure|. |
| 201 ++i; | 201 ++i; |
| 202 } | 202 } |
| 203 | 203 |
| 204 return false; | 204 return false; |
| 205 } | 205 } |
| 206 | 206 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 | 375 |
| 376 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids); | 376 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids); |
| 377 } | 377 } |
| 378 } | 378 } |
| 379 } | 379 } |
| 380 | 380 |
| 381 // Add the results from AutoComplete. They come back asynchronously, so we | 381 // Add the results from AutoComplete. They come back asynchronously, so we |
| 382 // hand off what we generated and they will send the results back to the | 382 // hand off what we generated and they will send the results back to the |
| 383 // renderer. | 383 // renderer. |
| 384 tab_contents()->autocomplete_history_manager()->OnGetAutocompleteSuggestions( | 384 tab_contents()->autocomplete_history_manager()->OnGetAutocompleteSuggestions( |
| 385 query_id, field.name(), field.value(), values, labels, icons, unique_ids); | 385 query_id, field.name, field.value, values, labels, icons, unique_ids); |
| 386 } | 386 } |
| 387 | 387 |
| 388 void AutofillManager::OnFillAutoFillFormData(int query_id, | 388 void AutofillManager::OnFillAutoFillFormData(int query_id, |
| 389 const FormData& form, | 389 const FormData& form, |
| 390 const FormField& field, | 390 const FormField& field, |
| 391 int unique_id) { | 391 int unique_id) { |
| 392 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); | 392 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); |
| 393 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); | 393 const std::vector<CreditCard*>& credit_cards = personal_data_->credit_cards(); |
| 394 RenderViewHost* host = NULL; | 394 RenderViewHost* host = NULL; |
| 395 FormStructure* form_structure = NULL; | 395 FormStructure* form_structure = NULL; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 } | 570 } |
| 571 | 571 |
| 572 return prefs->GetBoolean(prefs::kAutoFillEnabled); | 572 return prefs->GetBoolean(prefs::kAutoFillEnabled); |
| 573 } | 573 } |
| 574 | 574 |
| 575 void AutofillManager::DeterminePossibleFieldTypesForUpload( | 575 void AutofillManager::DeterminePossibleFieldTypesForUpload( |
| 576 FormStructure* submitted_form) { | 576 FormStructure* submitted_form) { |
| 577 for (size_t i = 0; i < submitted_form->field_count(); i++) { | 577 for (size_t i = 0; i < submitted_form->field_count(); i++) { |
| 578 const AutofillField* field = submitted_form->field(i); | 578 const AutofillField* field = submitted_form->field(i); |
| 579 FieldTypeSet field_types; | 579 FieldTypeSet field_types; |
| 580 personal_data_->GetPossibleFieldTypes(field->value(), &field_types); | 580 personal_data_->GetPossibleFieldTypes(field->value, &field_types); |
| 581 | 581 |
| 582 DCHECK(!field_types.empty()); | 582 DCHECK(!field_types.empty()); |
| 583 submitted_form->set_possible_types(i, field_types); | 583 submitted_form->set_possible_types(i, field_types); |
| 584 } | 584 } |
| 585 } | 585 } |
| 586 | 586 |
| 587 void AutofillManager::LogMetricsAboutSubmittedForm( | 587 void AutofillManager::LogMetricsAboutSubmittedForm( |
| 588 const FormData& form, | 588 const FormData& form, |
| 589 const FormStructure* submitted_form) { | 589 const FormStructure* submitted_form) { |
| 590 FormStructure* cached_submitted_form; | 590 FormStructure* cached_submitted_form; |
| 591 if (!FindCachedForm(form, &cached_submitted_form)) { | 591 if (!FindCachedForm(form, &cached_submitted_form)) { |
| 592 NOTREACHED(); | 592 NOTREACHED(); |
| 593 return; | 593 return; |
| 594 } | 594 } |
| 595 | 595 |
| 596 // Map from field signatures to cached fields. | 596 // Map from field signatures to cached fields. |
| 597 std::map<std::string, const AutofillField*> cached_fields; | 597 std::map<std::string, const AutofillField*> cached_fields; |
| 598 for (size_t i = 0; i < cached_submitted_form->field_count(); ++i) { | 598 for (size_t i = 0; i < cached_submitted_form->field_count(); ++i) { |
| 599 const AutofillField* field = cached_submitted_form->field(i); | 599 const AutofillField* field = cached_submitted_form->field(i); |
| 600 cached_fields[field->FieldSignature()] = field; | 600 cached_fields[field->FieldSignature()] = field; |
| 601 } | 601 } |
| 602 | 602 |
| 603 std::string experiment_id = cached_submitted_form->server_experiment_id(); | 603 std::string experiment_id = cached_submitted_form->server_experiment_id(); |
| 604 for (size_t i = 0; i < submitted_form->field_count(); ++i) { | 604 for (size_t i = 0; i < submitted_form->field_count(); ++i) { |
| 605 const AutofillField* field = submitted_form->field(i); | 605 const AutofillField* field = submitted_form->field(i); |
| 606 FieldTypeSet field_types; | 606 FieldTypeSet field_types; |
| 607 personal_data_->GetPossibleFieldTypes(field->value(), &field_types); | 607 personal_data_->GetPossibleFieldTypes(field->value, &field_types); |
| 608 DCHECK(!field_types.empty()); | 608 DCHECK(!field_types.empty()); |
| 609 | 609 |
| 610 if (field->form_control_type() == ASCIIToUTF16("select-one")) { | 610 if (field->form_control_type == ASCIIToUTF16("select-one")) { |
| 611 // TODO(isherman): <select> fields don't support |is_autofilled()|. Since | 611 // TODO(isherman): <select> fields don't support |is_autofilled|. Since |
| 612 // this is heavily relied upon by our metrics, we just don't log anything | 612 // this is heavily relied upon by our metrics, we just don't log anything |
| 613 // for all <select> fields. Better to have less data than misleading data. | 613 // for all <select> fields. Better to have less data than misleading data. |
| 614 continue; | 614 continue; |
| 615 } | 615 } |
| 616 | 616 |
| 617 // Log various quality metrics. | 617 // Log various quality metrics. |
| 618 metric_logger_->Log(AutofillMetrics::FIELD_SUBMITTED, experiment_id); | 618 metric_logger_->Log(AutofillMetrics::FIELD_SUBMITTED, experiment_id); |
| 619 if (field_types.find(EMPTY_TYPE) == field_types.end() && | 619 if (field_types.find(EMPTY_TYPE) == field_types.end() && |
| 620 field_types.find(UNKNOWN_TYPE) == field_types.end()) { | 620 field_types.find(UNKNOWN_TYPE) == field_types.end()) { |
| 621 if (field->is_autofilled()) { | 621 if (field->is_autofilled) { |
| 622 metric_logger_->Log(AutofillMetrics::FIELD_AUTOFILLED, experiment_id); | 622 metric_logger_->Log(AutofillMetrics::FIELD_AUTOFILLED, experiment_id); |
| 623 } else { | 623 } else { |
| 624 metric_logger_->Log(AutofillMetrics::FIELD_AUTOFILL_FAILED, | 624 metric_logger_->Log(AutofillMetrics::FIELD_AUTOFILL_FAILED, |
| 625 experiment_id); | 625 experiment_id); |
| 626 | 626 |
| 627 AutofillFieldType heuristic_type = UNKNOWN_TYPE; | 627 AutofillFieldType heuristic_type = UNKNOWN_TYPE; |
| 628 AutofillFieldType server_type = NO_SERVER_DATA; | 628 AutofillFieldType server_type = NO_SERVER_DATA; |
| 629 std::map<std::string, const AutofillField*>::const_iterator | 629 std::map<std::string, const AutofillField*>::const_iterator |
| 630 cached_field = cached_fields.find(field->FieldSignature()); | 630 cached_field = cached_fields.find(field->FieldSignature()); |
| 631 if (cached_field != cached_fields.end()) { | 631 if (cached_field != cached_fields.end()) { |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); | 810 const std::vector<AutoFillProfile*>& profiles = personal_data_->profiles(); |
| 811 std::vector<AutoFillProfile*> matched_profiles; | 811 std::vector<AutoFillProfile*> matched_profiles; |
| 812 for (std::vector<AutoFillProfile*>::const_iterator iter = profiles.begin(); | 812 for (std::vector<AutoFillProfile*>::const_iterator iter = profiles.begin(); |
| 813 iter != profiles.end(); ++iter) { | 813 iter != profiles.end(); ++iter) { |
| 814 AutoFillProfile* profile = *iter; | 814 AutoFillProfile* profile = *iter; |
| 815 | 815 |
| 816 // The value of the stored data for this field type in the |profile|. | 816 // The value of the stored data for this field type in the |profile|. |
| 817 string16 profile_field_value = profile->GetFieldText(type); | 817 string16 profile_field_value = profile->GetFieldText(type); |
| 818 | 818 |
| 819 if (!profile_field_value.empty() && | 819 if (!profile_field_value.empty() && |
| 820 StartsWith(profile_field_value, field.value(), false)) { | 820 StartsWith(profile_field_value, field.value, false)) { |
| 821 matched_profiles.push_back(profile); | 821 matched_profiles.push_back(profile); |
| 822 values->push_back(profile_field_value); | 822 values->push_back(profile_field_value); |
| 823 unique_ids->push_back(PackGUIDs(std::string(), profile->guid())); | 823 unique_ids->push_back(PackGUIDs(std::string(), profile->guid())); |
| 824 } | 824 } |
| 825 } | 825 } |
| 826 | 826 |
| 827 std::vector<AutofillFieldType> form_fields; | 827 std::vector<AutofillFieldType> form_fields; |
| 828 form_fields.reserve(form->field_count()); | 828 form_fields.reserve(form->field_count()); |
| 829 for (std::vector<AutofillField*>::const_iterator iter = form->begin(); | 829 for (std::vector<AutofillField*>::const_iterator iter = form->begin(); |
| 830 iter != form->end(); ++iter) { | 830 iter != form->end(); ++iter) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 850 std::vector<string16>* icons, | 850 std::vector<string16>* icons, |
| 851 std::vector<int>* unique_ids) { | 851 std::vector<int>* unique_ids) { |
| 852 for (std::vector<CreditCard*>::const_iterator iter = | 852 for (std::vector<CreditCard*>::const_iterator iter = |
| 853 personal_data_->credit_cards().begin(); | 853 personal_data_->credit_cards().begin(); |
| 854 iter != personal_data_->credit_cards().end(); ++iter) { | 854 iter != personal_data_->credit_cards().end(); ++iter) { |
| 855 CreditCard* credit_card = *iter; | 855 CreditCard* credit_card = *iter; |
| 856 | 856 |
| 857 // The value of the stored data for this field type in the |credit_card|. | 857 // The value of the stored data for this field type in the |credit_card|. |
| 858 string16 creditcard_field_value = credit_card->GetFieldText(type); | 858 string16 creditcard_field_value = credit_card->GetFieldText(type); |
| 859 if (!creditcard_field_value.empty() && | 859 if (!creditcard_field_value.empty() && |
| 860 StartsWith(creditcard_field_value, field.value(), false)) { | 860 StartsWith(creditcard_field_value, field.value, false)) { |
| 861 if (type.field_type() == CREDIT_CARD_NUMBER) | 861 if (type.field_type() == CREDIT_CARD_NUMBER) |
| 862 creditcard_field_value = credit_card->ObfuscatedNumber(); | 862 creditcard_field_value = credit_card->ObfuscatedNumber(); |
| 863 | 863 |
| 864 values->push_back(creditcard_field_value); | 864 values->push_back(creditcard_field_value); |
| 865 labels->push_back(kCreditCardPrefix + credit_card->LastFourDigits()); | 865 labels->push_back(kCreditCardPrefix + credit_card->LastFourDigits()); |
| 866 icons->push_back(credit_card->type()); | 866 icons->push_back(credit_card->type()); |
| 867 unique_ids->push_back(PackGUIDs(credit_card->guid(), std::string())); | 867 unique_ids->push_back(PackGUIDs(credit_card->guid(), std::string())); |
| 868 } | 868 } |
| 869 } | 869 } |
| 870 } | 870 } |
| 871 | 871 |
| 872 void AutofillManager::FillCreditCardFormField(const CreditCard* credit_card, | 872 void AutofillManager::FillCreditCardFormField(const CreditCard* credit_card, |
| 873 AutofillType type, | 873 AutofillType type, |
| 874 webkit_glue::FormField* field) { | 874 webkit_glue::FormField* field) { |
| 875 DCHECK(credit_card); | 875 DCHECK(credit_card); |
| 876 DCHECK_EQ(AutofillType::CREDIT_CARD, type.group()); | 876 DCHECK_EQ(AutofillType::CREDIT_CARD, type.group()); |
| 877 DCHECK(field); | 877 DCHECK(field); |
| 878 | 878 |
| 879 if (field->form_control_type() == ASCIIToUTF16("select-one")) { | 879 if (field->form_control_type == ASCIIToUTF16("select-one")) { |
| 880 autofill::FillSelectControl(*credit_card, type, field); | 880 autofill::FillSelectControl(*credit_card, type, field); |
| 881 } else if (field->form_control_type() == ASCIIToUTF16("month")) { | 881 } else if (field->form_control_type == ASCIIToUTF16("month")) { |
| 882 // HTML5 input="month" consists of year-month. | 882 // HTML5 input="month" consists of year-month. |
| 883 string16 year = credit_card->GetFieldText( | 883 string16 year = credit_card->GetFieldText( |
| 884 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 884 AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 885 string16 month = credit_card->GetFieldText( | 885 string16 month = credit_card->GetFieldText( |
| 886 AutofillType(CREDIT_CARD_EXP_MONTH)); | 886 AutofillType(CREDIT_CARD_EXP_MONTH)); |
| 887 if (!year.empty() && !month.empty()) { | 887 if (!year.empty() && !month.empty()) { |
| 888 // Fill the value only if |credit_card| includes both year and month | 888 // Fill the value only if |credit_card| includes both year and month |
| 889 // information. | 889 // information. |
| 890 field->set_value(year + ASCIIToUTF16("-") + month); | 890 field->value = year + ASCIIToUTF16("-") + month; |
| 891 } | 891 } |
| 892 } else { | 892 } else { |
| 893 field->set_value(credit_card->GetFieldText(type)); | 893 field->value = credit_card->GetFieldText(type); |
| 894 } | 894 } |
| 895 } | 895 } |
| 896 | 896 |
| 897 void AutofillManager::FillFormField(const AutoFillProfile* profile, | 897 void AutofillManager::FillFormField(const AutoFillProfile* profile, |
| 898 AutofillType type, | 898 AutofillType type, |
| 899 webkit_glue::FormField* field) { | 899 webkit_glue::FormField* field) { |
| 900 DCHECK(profile); | 900 DCHECK(profile); |
| 901 DCHECK_NE(AutofillType::CREDIT_CARD, type.group()); | 901 DCHECK_NE(AutofillType::CREDIT_CARD, type.group()); |
| 902 DCHECK(field); | 902 DCHECK(field); |
| 903 | 903 |
| 904 if (type.subgroup() == AutofillType::PHONE_NUMBER) { | 904 if (type.subgroup() == AutofillType::PHONE_NUMBER) { |
| 905 FillPhoneNumberField(profile, type, field); | 905 FillPhoneNumberField(profile, type, field); |
| 906 } else { | 906 } else { |
| 907 if (field->form_control_type() == ASCIIToUTF16("select-one")) | 907 if (field->form_control_type == ASCIIToUTF16("select-one")) |
| 908 autofill::FillSelectControl(*profile, type, field); | 908 autofill::FillSelectControl(*profile, type, field); |
| 909 else | 909 else |
| 910 field->set_value(profile->GetFieldText(type)); | 910 field->value = profile->GetFieldText(type); |
| 911 } | 911 } |
| 912 } | 912 } |
| 913 | 913 |
| 914 void AutofillManager::FillPhoneNumberField(const AutoFillProfile* profile, | 914 void AutofillManager::FillPhoneNumberField(const AutoFillProfile* profile, |
| 915 AutofillType type, | 915 AutofillType type, |
| 916 webkit_glue::FormField* field) { | 916 webkit_glue::FormField* field) { |
| 917 // If we are filling a phone number, check to see if the size field | 917 // If we are filling a phone number, check to see if the size field |
| 918 // matches the "prefix" or "suffix" sizes and fill accordingly. | 918 // matches the "prefix" or "suffix" sizes and fill accordingly. |
| 919 string16 number = profile->GetFieldText(AutofillType(type)); | 919 string16 number = profile->GetFieldText(AutofillType(type)); |
| 920 bool has_valid_suffix_and_prefix = (number.length() == | 920 bool has_valid_suffix_and_prefix = (number.length() == |
| 921 static_cast<size_t>(PhoneNumber::kPrefixLength + | 921 static_cast<size_t>(PhoneNumber::kPrefixLength + |
| 922 PhoneNumber::kSuffixLength)); | 922 PhoneNumber::kSuffixLength)); |
| 923 if (has_valid_suffix_and_prefix && | 923 if (has_valid_suffix_and_prefix && |
| 924 field->max_length() == PhoneNumber::kPrefixLength) { | 924 field->max_length == PhoneNumber::kPrefixLength) { |
| 925 number = number.substr(PhoneNumber::kPrefixOffset, | 925 number = number.substr(PhoneNumber::kPrefixOffset, |
| 926 PhoneNumber::kPrefixLength); | 926 PhoneNumber::kPrefixLength); |
| 927 field->set_value(number); | 927 field->value = number; |
| 928 } else if (has_valid_suffix_and_prefix && | 928 } else if (has_valid_suffix_and_prefix && |
| 929 field->max_length() == PhoneNumber::kSuffixLength) { | 929 field->max_length == PhoneNumber::kSuffixLength) { |
| 930 number = number.substr(PhoneNumber::kSuffixOffset, | 930 number = number.substr(PhoneNumber::kSuffixOffset, |
| 931 PhoneNumber::kSuffixLength); | 931 PhoneNumber::kSuffixLength); |
| 932 field->set_value(number); | 932 field->value = number; |
| 933 } else { | 933 } else { |
| 934 field->set_value(number); | 934 field->value = number; |
| 935 } | 935 } |
| 936 } | 936 } |
| 937 | 937 |
| 938 void AutofillManager::ParseForms(const std::vector<FormData>& forms) { | 938 void AutofillManager::ParseForms(const std::vector<FormData>& forms) { |
| 939 std::vector<FormStructure*> non_queryable_forms; | 939 std::vector<FormStructure*> non_queryable_forms; |
| 940 for (std::vector<FormData>::const_iterator iter = forms.begin(); | 940 for (std::vector<FormData>::const_iterator iter = forms.begin(); |
| 941 iter != forms.end(); ++iter) { | 941 iter != forms.end(); ++iter) { |
| 942 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); | 942 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); |
| 943 if (!form_structure->ShouldBeParsed(false)) | 943 if (!form_structure->ShouldBeParsed(false)) |
| 944 continue; | 944 continue; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1011 return std::string(); | 1011 return std::string(); |
| 1012 | 1012 |
| 1013 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); | 1013 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); |
| 1014 if (iter == id_guid_map_.end()) { | 1014 if (iter == id_guid_map_.end()) { |
| 1015 NOTREACHED(); | 1015 NOTREACHED(); |
| 1016 return std::string(); | 1016 return std::string(); |
| 1017 } | 1017 } |
| 1018 | 1018 |
| 1019 return iter->second; | 1019 return iter->second; |
| 1020 } | 1020 } |
| OLD | NEW |