| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/autofill/browser/autofill_data_model.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "components/autofill/browser/autofill_country.h" | |
| 13 #include "components/autofill/browser/autofill_field.h" | |
| 14 #include "components/autofill/browser/state_names.h" | |
| 15 #include "components/autofill/browser/validation.h" | |
| 16 #include "components/autofill/core/common/form_field_data.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 #include "grit/component_strings.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 20 | |
| 21 namespace autofill { | |
| 22 namespace { | |
| 23 | |
| 24 const char* const kMonthsAbbreviated[] = { | |
| 25 NULL, // Padding so index 1 = month 1 = January. | |
| 26 "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 27 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | |
| 28 }; | |
| 29 | |
| 30 const char* const kMonthsFull[] = { | |
| 31 NULL, // Padding so index 1 = month 1 = January. | |
| 32 "January", "February", "March", "April", "May", "June", | |
| 33 "July", "August", "September", "October", "November", "December", | |
| 34 }; | |
| 35 | |
| 36 const char* const kMonthsNumeric[] = { | |
| 37 NULL, // Padding so index 1 = month 1 = January. | |
| 38 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", | |
| 39 }; | |
| 40 | |
| 41 // Returns true if the value was successfully set, meaning |value| was found in | |
| 42 // the list of select options in |field|. | |
| 43 bool SetSelectControlValue(const base::string16& value, | |
| 44 FormFieldData* field) { | |
| 45 base::string16 value_lowercase = StringToLowerASCII(value); | |
| 46 | |
| 47 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | |
| 48 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
| 49 if (value_lowercase == StringToLowerASCII(field->option_values[i]) || | |
| 50 value_lowercase == StringToLowerASCII(field->option_contents[i])) { | |
| 51 field->value = field->option_values[i]; | |
| 52 return true; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 bool FillStateSelectControl(const base::string16& value, | |
| 60 FormFieldData* field) { | |
| 61 base::string16 full, abbreviation; | |
| 62 state_names::GetNameAndAbbreviation(value, &full, &abbreviation); | |
| 63 | |
| 64 // Try the abbreviation first. | |
| 65 if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field)) | |
| 66 return true; | |
| 67 | |
| 68 return !full.empty() && SetSelectControlValue(full, field); | |
| 69 } | |
| 70 | |
| 71 bool FillExpirationMonthSelectControl(const base::string16& value, | |
| 72 FormFieldData* field) { | |
| 73 int index = 0; | |
| 74 if (!base::StringToInt(value, &index) || | |
| 75 index <= 0 || | |
| 76 static_cast<size_t>(index) >= arraysize(kMonthsFull)) | |
| 77 return false; | |
| 78 | |
| 79 bool filled = | |
| 80 SetSelectControlValue(ASCIIToUTF16(kMonthsAbbreviated[index]), field) || | |
| 81 SetSelectControlValue(ASCIIToUTF16(kMonthsFull[index]), field) || | |
| 82 SetSelectControlValue(ASCIIToUTF16(kMonthsNumeric[index]), field); | |
| 83 return filled; | |
| 84 } | |
| 85 | |
| 86 // Try to fill a credit card type |value| (Visa, MasterCard, etc.) into the | |
| 87 // given |field|. | |
| 88 bool FillCreditCardTypeSelectControl(const base::string16& value, | |
| 89 FormFieldData* field) { | |
| 90 // Try stripping off spaces. | |
| 91 base::string16 value_stripped; | |
| 92 RemoveChars(StringToLowerASCII(value), kWhitespaceUTF16, &value_stripped); | |
| 93 | |
| 94 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
| 95 base::string16 option_value_lowercase; | |
| 96 RemoveChars(StringToLowerASCII(field->option_values[i]), kWhitespaceUTF16, | |
| 97 &option_value_lowercase); | |
| 98 base::string16 option_contents_lowercase; | |
| 99 RemoveChars(StringToLowerASCII(field->option_contents[i]), kWhitespaceUTF16, | |
| 100 &option_contents_lowercase); | |
| 101 | |
| 102 // Perform a case-insensitive comparison; but fill the form with the | |
| 103 // original text, not the lowercased version. | |
| 104 if (value_stripped == option_value_lowercase || | |
| 105 value_stripped == option_contents_lowercase) { | |
| 106 field->value = field->option_values[i]; | |
| 107 return true; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 // For American Express, also try filling as "AmEx". | |
| 112 if (value == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX)) | |
| 113 return FillCreditCardTypeSelectControl(ASCIIToUTF16("AmEx"), field); | |
| 114 | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 | |
| 120 AutofillDataModel::AutofillDataModel(const std::string& guid, | |
| 121 const std::string& origin) | |
| 122 : guid_(guid), | |
| 123 origin_(origin) {} | |
| 124 AutofillDataModel::~AutofillDataModel() {} | |
| 125 | |
| 126 void AutofillDataModel::FillSelectControl(AutofillFieldType type, | |
| 127 const std::string& app_locale, | |
| 128 FormFieldData* field) const { | |
| 129 DCHECK(field); | |
| 130 DCHECK_EQ("select-one", field->form_control_type); | |
| 131 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | |
| 132 | |
| 133 base::string16 field_text = GetInfo(type, app_locale); | |
| 134 base::string16 field_text_lower = StringToLowerASCII(field_text); | |
| 135 if (field_text.empty()) | |
| 136 return; | |
| 137 | |
| 138 base::string16 value; | |
| 139 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
| 140 if (field_text == field->option_values[i] || | |
| 141 field_text == field->option_contents[i]) { | |
| 142 // An exact match, use it. | |
| 143 value = field->option_values[i]; | |
| 144 break; | |
| 145 } | |
| 146 | |
| 147 if (field_text_lower == StringToLowerASCII(field->option_values[i]) || | |
| 148 field_text_lower == StringToLowerASCII(field->option_contents[i])) { | |
| 149 // A match, but not in the same case. Save it in case an exact match is | |
| 150 // not found. | |
| 151 value = field->option_values[i]; | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 if (!value.empty()) { | |
| 156 field->value = value; | |
| 157 return; | |
| 158 } | |
| 159 | |
| 160 if (type == ADDRESS_HOME_STATE || type == ADDRESS_BILLING_STATE) { | |
| 161 FillStateSelectControl(field_text, field); | |
| 162 } else if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) { | |
| 163 FillCountrySelectControl(app_locale, field); | |
| 164 } else if (type == CREDIT_CARD_EXP_MONTH) { | |
| 165 FillExpirationMonthSelectControl(field_text, field); | |
| 166 } else if (type == CREDIT_CARD_EXP_4_DIGIT_YEAR) { | |
| 167 // Attempt to fill the year as a 2-digit year. This compensates for the | |
| 168 // fact that our heuristics do not always correctly detect when a website | |
| 169 // requests a 2-digit rather than a 4-digit year. | |
| 170 FillSelectControl(CREDIT_CARD_EXP_2_DIGIT_YEAR, app_locale, field); | |
| 171 } else if (type == CREDIT_CARD_TYPE) { | |
| 172 FillCreditCardTypeSelectControl(field_text, field); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 bool AutofillDataModel::FillCountrySelectControl( | |
| 177 const std::string& app_locale, | |
| 178 FormFieldData* field_data) const { | |
| 179 return false; | |
| 180 } | |
| 181 | |
| 182 bool AutofillDataModel::IsVerified() const { | |
| 183 return !origin_.empty() && !GURL(origin_).is_valid(); | |
| 184 } | |
| 185 | |
| 186 } // namespace autofill | |
| OLD | NEW |