| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/autofill/form_group.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <iterator> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "chrome/browser/autofill/autofill_country.h" | |
| 15 #include "components/autofill/common/form_field_data.h" | |
| 16 #include "grit/generated_resources.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039. | |
| 22 | |
| 23 class State { | |
| 24 public: | |
| 25 const char* name; | |
| 26 const char* abbreviation; | |
| 27 | |
| 28 static const State all_states[]; | |
| 29 | |
| 30 static string16 Abbreviation(const string16& name); | |
| 31 static string16 FullName(const string16& abbreviation); | |
| 32 }; | |
| 33 | |
| 34 const State State::all_states[] = { | |
| 35 { "alabama", "al" }, | |
| 36 { "alaska", "ak" }, | |
| 37 { "arizona", "az" }, | |
| 38 { "arkansas", "ar" }, | |
| 39 { "california", "ca" }, | |
| 40 { "colorado", "co" }, | |
| 41 { "connecticut", "ct" }, | |
| 42 { "delaware", "de" }, | |
| 43 { "district of columbia", "dc" }, | |
| 44 { "florida", "fl" }, | |
| 45 { "georgia", "ga" }, | |
| 46 { "hawaii", "hi" }, | |
| 47 { "idaho", "id" }, | |
| 48 { "illinois", "il" }, | |
| 49 { "indiana", "in" }, | |
| 50 { "iowa", "ia" }, | |
| 51 { "kansas", "ks" }, | |
| 52 { "kentucky", "ky" }, | |
| 53 { "louisiana", "la" }, | |
| 54 { "maine", "me" }, | |
| 55 { "maryland", "md" }, | |
| 56 { "massachusetts", "ma" }, | |
| 57 { "michigan", "mi" }, | |
| 58 { "minnesota", "mv" }, | |
| 59 { "mississippi", "ms" }, | |
| 60 { "missouri", "mo" }, | |
| 61 { "montana", "mt" }, | |
| 62 { "nebraska", "ne" }, | |
| 63 { "nevada", "nv" }, | |
| 64 { "new hampshire", "nh" }, | |
| 65 { "new jersey", "nj" }, | |
| 66 { "new mexico", "nm" }, | |
| 67 { "new york", "ny" }, | |
| 68 { "north carolina", "nc" }, | |
| 69 { "north dakota", "nd" }, | |
| 70 { "ohio", "oh" }, | |
| 71 { "oklahoma", "ok" }, | |
| 72 { "oregon", "or" }, | |
| 73 { "pennsylvania", "pa" }, | |
| 74 { "puerto rico", "pr" }, | |
| 75 { "rhode island", "ri" }, | |
| 76 { "south carolina", "sc" }, | |
| 77 { "south dakota", "sd" }, | |
| 78 { "tennessee", "tn" }, | |
| 79 { "texas", "tx" }, | |
| 80 { "utah", "ut" }, | |
| 81 { "vermont", "vt" }, | |
| 82 { "virginia", "va" }, | |
| 83 { "washington", "wa" }, | |
| 84 { "west virginia", "wv" }, | |
| 85 { "wisconsin", "wi" }, | |
| 86 { "wyoming", "wy" }, | |
| 87 { NULL, NULL } | |
| 88 }; | |
| 89 | |
| 90 string16 State::Abbreviation(const string16& name) { | |
| 91 for (const State* state = all_states; state->name; ++state) { | |
| 92 if (LowerCaseEqualsASCII(name, state->name)) | |
| 93 return ASCIIToUTF16(state->abbreviation); | |
| 94 } | |
| 95 return string16(); | |
| 96 } | |
| 97 | |
| 98 string16 State::FullName(const string16& abbreviation) { | |
| 99 for (const State* state = all_states; state->name; ++state) { | |
| 100 if (LowerCaseEqualsASCII(abbreviation, state->abbreviation)) | |
| 101 return ASCIIToUTF16(state->name); | |
| 102 } | |
| 103 return string16(); | |
| 104 } | |
| 105 | |
| 106 const char* const kMonthsAbbreviated[] = { | |
| 107 NULL, // Padding so index 1 = month 1 = January. | |
| 108 "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 109 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | |
| 110 }; | |
| 111 | |
| 112 const char* const kMonthsFull[] = { | |
| 113 NULL, // Padding so index 1 = month 1 = January. | |
| 114 "January", "February", "March", "April", "May", "June", | |
| 115 "July", "August", "September", "October", "November", "December", | |
| 116 }; | |
| 117 | |
| 118 const char* const kMonthsNumeric[] = { | |
| 119 NULL, // Padding so index 1 = month 1 = January. | |
| 120 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", | |
| 121 }; | |
| 122 | |
| 123 // Returns true if the value was successfully set, meaning |value| was found in | |
| 124 // the list of select options in |field|. | |
| 125 bool SetSelectControlValue(const string16& value, | |
| 126 FormFieldData* field) { | |
| 127 string16 value_lowercase = StringToLowerASCII(value); | |
| 128 | |
| 129 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | |
| 130 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
| 131 if (value_lowercase == StringToLowerASCII(field->option_values[i]) || | |
| 132 value_lowercase == StringToLowerASCII(field->option_contents[i])) { | |
| 133 field->value = field->option_values[i]; | |
| 134 return true; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 return false; | |
| 139 } | |
| 140 | |
| 141 bool FillStateSelectControl(const string16& value, | |
| 142 FormFieldData* field) { | |
| 143 string16 abbrev, full; | |
| 144 if (value.size() < 4U) { | |
| 145 abbrev = value; | |
| 146 full = State::FullName(value); | |
| 147 } else { | |
| 148 abbrev = State::Abbreviation(value); | |
| 149 full = value; | |
| 150 } | |
| 151 | |
| 152 // Try the abbreviation name first. | |
| 153 if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) | |
| 154 return true; | |
| 155 | |
| 156 if (full.empty()) | |
| 157 return false; | |
| 158 | |
| 159 return SetSelectControlValue(full, field); | |
| 160 } | |
| 161 | |
| 162 bool FillExpirationMonthSelectControl(const string16& value, | |
| 163 FormFieldData* field) { | |
| 164 int index = 0; | |
| 165 if (!base::StringToInt(value, &index) || | |
| 166 index <= 0 || | |
| 167 static_cast<size_t>(index) >= arraysize(kMonthsFull)) | |
| 168 return false; | |
| 169 | |
| 170 bool filled = | |
| 171 SetSelectControlValue(ASCIIToUTF16(kMonthsAbbreviated[index]), field) || | |
| 172 SetSelectControlValue(ASCIIToUTF16(kMonthsFull[index]), field) || | |
| 173 SetSelectControlValue(ASCIIToUTF16(kMonthsNumeric[index]), field); | |
| 174 return filled; | |
| 175 } | |
| 176 | |
| 177 // Try to fill a credit card type |value| (Visa, MasterCard, etc.) into the | |
| 178 // given |field|. | |
| 179 bool FillCreditCardTypeSelectControl(const string16& value, | |
| 180 FormFieldData* field) { | |
| 181 // Try stripping off spaces. | |
| 182 string16 value_stripped; | |
| 183 RemoveChars(StringToLowerASCII(value), kWhitespaceUTF16, &value_stripped); | |
| 184 | |
| 185 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
| 186 string16 option_value_lowercase; | |
| 187 RemoveChars(StringToLowerASCII(field->option_values[i]), kWhitespaceUTF16, | |
| 188 &option_value_lowercase); | |
| 189 string16 option_contents_lowercase; | |
| 190 RemoveChars(StringToLowerASCII(field->option_contents[i]), kWhitespaceUTF16, | |
| 191 &option_contents_lowercase); | |
| 192 | |
| 193 // Perform a case-insensitive comparison; but fill the form with the | |
| 194 // original text, not the lowercased version. | |
| 195 if (value_stripped == option_value_lowercase || | |
| 196 value_stripped == option_contents_lowercase) { | |
| 197 field->value = field->option_values[i]; | |
| 198 return true; | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 // For American Express, also try filling as "AmEx". | |
| 203 if (value == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX)) | |
| 204 return FillCreditCardTypeSelectControl(ASCIIToUTF16("AmEx"), field); | |
| 205 | |
| 206 return false; | |
| 207 } | |
| 208 | |
| 209 } // namespace | |
| 210 | |
| 211 std::string FormGroup::GetGUID() const { | |
| 212 NOTREACHED(); | |
| 213 return std::string(); | |
| 214 } | |
| 215 | |
| 216 void FormGroup::GetMatchingTypes(const string16& text, | |
| 217 const std::string& app_locale, | |
| 218 FieldTypeSet* matching_types) const { | |
| 219 if (text.empty()) { | |
| 220 matching_types->insert(EMPTY_TYPE); | |
| 221 return; | |
| 222 } | |
| 223 | |
| 224 FieldTypeSet types; | |
| 225 GetSupportedTypes(&types); | |
| 226 for (FieldTypeSet::const_iterator type = types.begin(); | |
| 227 type != types.end(); ++type) { | |
| 228 // TODO(isherman): Matches are case-sensitive for now. Let's keep an eye on | |
| 229 // this and decide whether there are compelling reasons to add case- | |
| 230 // insensitivity. | |
| 231 if (GetInfo(*type, app_locale) == text) | |
| 232 matching_types->insert(*type); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 void FormGroup::GetNonEmptyTypes(const std::string& app_locale, | |
| 237 FieldTypeSet* non_empty_types) const { | |
| 238 FieldTypeSet types; | |
| 239 GetSupportedTypes(&types); | |
| 240 for (FieldTypeSet::const_iterator type = types.begin(); | |
| 241 type != types.end(); ++type) { | |
| 242 if (!GetInfo(*type, app_locale).empty()) | |
| 243 non_empty_types->insert(*type); | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 string16 FormGroup::GetInfo(AutofillFieldType type, | |
| 248 const std::string& app_locale) const { | |
| 249 return GetRawInfo(type); | |
| 250 } | |
| 251 | |
| 252 bool FormGroup::SetInfo(AutofillFieldType type, | |
| 253 const string16& value, | |
| 254 const std::string& app_locale) { | |
| 255 SetRawInfo(type, value); | |
| 256 return true; | |
| 257 } | |
| 258 | |
| 259 void FormGroup::FillFormField(const AutofillField& field, | |
| 260 size_t variant, | |
| 261 FormFieldData* field_data) const { | |
| 262 NOTREACHED(); | |
| 263 } | |
| 264 | |
| 265 void FormGroup::FillSelectControl(AutofillFieldType type, | |
| 266 FormFieldData* field) const { | |
| 267 DCHECK(field); | |
| 268 DCHECK_EQ("select-one", field->form_control_type); | |
| 269 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | |
| 270 | |
| 271 const std::string app_locale = AutofillCountry::ApplicationLocale(); | |
| 272 string16 field_text = GetInfo(type, app_locale); | |
| 273 string16 field_text_lower = StringToLowerASCII(field_text); | |
| 274 if (field_text.empty()) | |
| 275 return; | |
| 276 | |
| 277 string16 value; | |
| 278 for (size_t i = 0; i < field->option_values.size(); ++i) { | |
| 279 if (field_text == field->option_values[i] || | |
| 280 field_text == field->option_contents[i]) { | |
| 281 // An exact match, use it. | |
| 282 value = field->option_values[i]; | |
| 283 break; | |
| 284 } | |
| 285 | |
| 286 if (field_text_lower == StringToLowerASCII(field->option_values[i]) || | |
| 287 field_text_lower == StringToLowerASCII(field->option_contents[i])) { | |
| 288 // A match, but not in the same case. Save it in case an exact match is | |
| 289 // not found. | |
| 290 value = field->option_values[i]; | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 if (!value.empty()) { | |
| 295 field->value = value; | |
| 296 return; | |
| 297 } | |
| 298 | |
| 299 if (type == ADDRESS_HOME_STATE || type == ADDRESS_BILLING_STATE) { | |
| 300 FillStateSelectControl(field_text, field); | |
| 301 } else if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) { | |
| 302 FillCountrySelectControl(field); | |
| 303 } else if (type == CREDIT_CARD_EXP_MONTH) { | |
| 304 FillExpirationMonthSelectControl(field_text, field); | |
| 305 } else if (type == CREDIT_CARD_EXP_4_DIGIT_YEAR) { | |
| 306 // Attempt to fill the year as a 2-digit year. This compensates for the | |
| 307 // fact that our heuristics do not always correctly detect when a website | |
| 308 // requests a 2-digit rather than a 4-digit year. | |
| 309 FillSelectControl(CREDIT_CARD_EXP_2_DIGIT_YEAR, field); | |
| 310 } else if (type == CREDIT_CARD_TYPE) { | |
| 311 FillCreditCardTypeSelectControl(field_text, field); | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 bool FormGroup::FillCountrySelectControl(FormFieldData* field_data) const { | |
| 316 return false; | |
| 317 } | |
| 318 | |
| 319 // static | |
| 320 bool FormGroup::IsValidState(const string16& value) { | |
| 321 return !State::Abbreviation(value).empty() || !State::FullName(value).empty(); | |
| 322 } | |
| OLD | NEW |