blob: 4ed49c710730be524afacb151839ce7469e0e602 [file] [log] [blame]
[email protected]7b37fbb2011-03-07 16:16:031// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]a7b29dd2010-07-12 22:40:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e4b2fa32013-03-09 22:56:565#include "components/autofill/browser/form_group.h"
[email protected]a7b29dd2010-07-12 22:40:066
[email protected]64853b1b2012-11-13 08:18:467#include <algorithm>
[email protected]b7038642011-01-30 22:46:448#include <iterator>
9
[email protected]64853b1b2012-11-13 08:18:4610#include "base/basictypes.h"
11#include "base/logging.h"
[email protected]3ea1b182013-02-08 22:38:4112#include "base/strings/string_number_conversions.h"
[email protected]64853b1b2012-11-13 08:18:4613#include "base/utf_string_conversions.h"
[email protected]e4b2fa32013-03-09 22:56:5614#include "components/autofill/browser/autofill_country.h"
[email protected]edf48d42013-03-07 05:44:4315#include "components/autofill/common/form_field_data.h"
[email protected]64853b1b2012-11-13 08:18:4616#include "grit/generated_resources.h"
17#include "ui/base/l10n/l10n_util.h"
18
19namespace {
20
21// TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039.
22
23class State {
24 public:
25 const char* name;
26 const char* abbreviation;
27
28 static const State all_states[];
29
[email protected]d5ca8fb2013-04-11 17:54:3130 static base::string16 Abbreviation(const base::string16& name);
31 static base::string16 FullName(const base::string16& abbreviation);
[email protected]64853b1b2012-11-13 08:18:4632};
33
34const 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
[email protected]d5ca8fb2013-04-11 17:54:3190base::string16 State::Abbreviation(const base::string16& name) {
[email protected]64853b1b2012-11-13 08:18:4691 for (const State* state = all_states; state->name; ++state) {
92 if (LowerCaseEqualsASCII(name, state->name))
93 return ASCIIToUTF16(state->abbreviation);
94 }
[email protected]d5ca8fb2013-04-11 17:54:3195 return base::string16();
[email protected]64853b1b2012-11-13 08:18:4696}
97
[email protected]d5ca8fb2013-04-11 17:54:3198base::string16 State::FullName(const base::string16& abbreviation) {
[email protected]64853b1b2012-11-13 08:18:4699 for (const State* state = all_states; state->name; ++state) {
100 if (LowerCaseEqualsASCII(abbreviation, state->abbreviation))
101 return ASCIIToUTF16(state->name);
102 }
[email protected]d5ca8fb2013-04-11 17:54:31103 return base::string16();
[email protected]64853b1b2012-11-13 08:18:46104}
105
106const 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
112const 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
118const 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|.
[email protected]d5ca8fb2013-04-11 17:54:31125bool SetSelectControlValue(const base::string16& value,
[email protected]64853b1b2012-11-13 08:18:46126 FormFieldData* field) {
[email protected]d5ca8fb2013-04-11 17:54:31127 base::string16 value_lowercase = StringToLowerASCII(value);
[email protected]64853b1b2012-11-13 08:18:46128
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
[email protected]d5ca8fb2013-04-11 17:54:31141bool FillStateSelectControl(const base::string16& value,
[email protected]64853b1b2012-11-13 08:18:46142 FormFieldData* field) {
[email protected]d5ca8fb2013-04-11 17:54:31143 base::string16 abbrev, full;
[email protected]64853b1b2012-11-13 08:18:46144 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
[email protected]d5ca8fb2013-04-11 17:54:31162bool FillExpirationMonthSelectControl(const base::string16& value,
[email protected]64853b1b2012-11-13 08:18:46163 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|.
[email protected]d5ca8fb2013-04-11 17:54:31179bool FillCreditCardTypeSelectControl(const base::string16& value,
[email protected]64853b1b2012-11-13 08:18:46180 FormFieldData* field) {
181 // Try stripping off spaces.
[email protected]d5ca8fb2013-04-11 17:54:31182 base::string16 value_stripped;
[email protected]64853b1b2012-11-13 08:18:46183 RemoveChars(StringToLowerASCII(value), kWhitespaceUTF16, &value_stripped);
184
185 for (size_t i = 0; i < field->option_values.size(); ++i) {
[email protected]d5ca8fb2013-04-11 17:54:31186 base::string16 option_value_lowercase;
[email protected]64853b1b2012-11-13 08:18:46187 RemoveChars(StringToLowerASCII(field->option_values[i]), kWhitespaceUTF16,
188 &option_value_lowercase);
[email protected]d5ca8fb2013-04-11 17:54:31189 base::string16 option_contents_lowercase;
[email protected]64853b1b2012-11-13 08:18:46190 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
[email protected]8da04b852012-11-16 22:10:39211std::string FormGroup::GetGUID() const {
212 NOTREACHED();
213 return std::string();
214}
215
[email protected]d5ca8fb2013-04-11 17:54:31216void FormGroup::GetMatchingTypes(const base::string16& text,
[email protected]4d11fcd2012-12-05 05:34:48217 const std::string& app_locale,
[email protected]fcfece42011-07-22 08:29:32218 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.
[email protected]4d11fcd2012-12-05 05:34:48231 if (GetInfo(*type, app_locale) == text)
[email protected]fcfece42011-07-22 08:29:32232 matching_types->insert(*type);
233 }
234}
235
[email protected]4d11fcd2012-12-05 05:34:48236void FormGroup::GetNonEmptyTypes(const std::string& app_locale,
237 FieldTypeSet* non_empty_types) const {
[email protected]fcfece42011-07-22 08:29:32238 FieldTypeSet types;
239 GetSupportedTypes(&types);
240 for (FieldTypeSet::const_iterator type = types.begin();
241 type != types.end(); ++type) {
[email protected]4d11fcd2012-12-05 05:34:48242 if (!GetInfo(*type, app_locale).empty())
[email protected]fcfece42011-07-22 08:29:32243 non_empty_types->insert(*type);
244 }
245}
246
[email protected]d5ca8fb2013-04-11 17:54:31247base::string16 FormGroup::GetInfo(AutofillFieldType type,
[email protected]4d11fcd2012-12-05 05:34:48248 const std::string& app_locale) const {
[email protected]6b44b582012-11-10 06:31:18249 return GetRawInfo(type);
[email protected]fcfece42011-07-22 08:29:32250}
251
[email protected]4d11fcd2012-12-05 05:34:48252bool FormGroup::SetInfo(AutofillFieldType type,
[email protected]d5ca8fb2013-04-11 17:54:31253 const base::string16& value,
[email protected]4d11fcd2012-12-05 05:34:48254 const std::string& app_locale) {
[email protected]6b44b582012-11-10 06:31:18255 SetRawInfo(type, value);
[email protected]fcfece42011-07-22 08:29:32256 return true;
257}
[email protected]64853b1b2012-11-13 08:18:46258
[email protected]8da04b852012-11-16 22:10:39259void FormGroup::FillFormField(const AutofillField& field,
260 size_t variant,
[email protected]0b2f513b2013-04-05 20:13:23261 const std::string& app_locale,
[email protected]8da04b852012-11-16 22:10:39262 FormFieldData* field_data) const {
263 NOTREACHED();
264}
265
[email protected]64853b1b2012-11-13 08:18:46266void FormGroup::FillSelectControl(AutofillFieldType type,
[email protected]0b2f513b2013-04-05 20:13:23267 const std::string& app_locale,
[email protected]64853b1b2012-11-13 08:18:46268 FormFieldData* field) const {
269 DCHECK(field);
270 DCHECK_EQ("select-one", field->form_control_type);
271 DCHECK_EQ(field->option_values.size(), field->option_contents.size());
272
[email protected]d5ca8fb2013-04-11 17:54:31273 base::string16 field_text = GetInfo(type, app_locale);
274 base::string16 field_text_lower = StringToLowerASCII(field_text);
[email protected]64853b1b2012-11-13 08:18:46275 if (field_text.empty())
276 return;
277
[email protected]d5ca8fb2013-04-11 17:54:31278 base::string16 value;
[email protected]64853b1b2012-11-13 08:18:46279 for (size_t i = 0; i < field->option_values.size(); ++i) {
280 if (field_text == field->option_values[i] ||
281 field_text == field->option_contents[i]) {
282 // An exact match, use it.
283 value = field->option_values[i];
284 break;
285 }
286
287 if (field_text_lower == StringToLowerASCII(field->option_values[i]) ||
288 field_text_lower == StringToLowerASCII(field->option_contents[i])) {
289 // A match, but not in the same case. Save it in case an exact match is
290 // not found.
291 value = field->option_values[i];
292 }
293 }
294
295 if (!value.empty()) {
296 field->value = value;
297 return;
298 }
299
300 if (type == ADDRESS_HOME_STATE || type == ADDRESS_BILLING_STATE) {
301 FillStateSelectControl(field_text, field);
302 } else if (type == ADDRESS_HOME_COUNTRY || type == ADDRESS_BILLING_COUNTRY) {
[email protected]0b2f513b2013-04-05 20:13:23303 FillCountrySelectControl(app_locale, field);
[email protected]64853b1b2012-11-13 08:18:46304 } else if (type == CREDIT_CARD_EXP_MONTH) {
305 FillExpirationMonthSelectControl(field_text, field);
306 } else if (type == CREDIT_CARD_EXP_4_DIGIT_YEAR) {
307 // Attempt to fill the year as a 2-digit year. This compensates for the
308 // fact that our heuristics do not always correctly detect when a website
309 // requests a 2-digit rather than a 4-digit year.
[email protected]0b2f513b2013-04-05 20:13:23310 FillSelectControl(CREDIT_CARD_EXP_2_DIGIT_YEAR, app_locale, field);
[email protected]64853b1b2012-11-13 08:18:46311 } else if (type == CREDIT_CARD_TYPE) {
312 FillCreditCardTypeSelectControl(field_text, field);
313 }
314}
315
[email protected]0b2f513b2013-04-05 20:13:23316bool FormGroup::FillCountrySelectControl(const std::string& app_locale,
317 FormFieldData* field_data) const {
[email protected]64853b1b2012-11-13 08:18:46318 return false;
319}
320
321// static
[email protected]d5ca8fb2013-04-11 17:54:31322bool FormGroup::IsValidState(const base::string16& value) {
[email protected]64853b1b2012-11-13 08:18:46323 return !State::Abbreviation(value).empty() || !State::FullName(value).empty();
324}