blob: f52e6762cd902781376fdbfb3ed0db8e321c901b [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
5#include "chrome/browser/autofill/form_group.h"
6
[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]4d11fcd2012-12-05 05:34:4814#include "chrome/browser/autofill/autofill_country.h"
[email protected]64853b1b2012-11-13 08:18:4615#include "chrome/common/form_field_data.h"
16#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
30 static string16 Abbreviation(const string16& name);
31 static string16 FullName(const string16& abbreviation);
32};
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
90string16 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
98string16 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
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|.
125bool 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
141bool 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
162bool 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|.
179bool 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
[email protected]8da04b852012-11-16 22:10:39211std::string FormGroup::GetGUID() const {
212 NOTREACHED();
213 return std::string();
214}
215
[email protected]fcfece42011-07-22 08:29:32216void FormGroup::GetMatchingTypes(const 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]4d11fcd2012-12-05 05:34:48247string16 FormGroup::GetInfo(AutofillFieldType type,
248 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,
253 const string16& value,
254 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,
261 FormFieldData* field_data) const {
262 NOTREACHED();
263}
264
[email protected]64853b1b2012-11-13 08:18:46265void 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
[email protected]4d11fcd2012-12-05 05:34:48271 const std::string app_locale = AutofillCountry::ApplicationLocale();
272 string16 field_text = GetInfo(type, app_locale);
[email protected]64853b1b2012-11-13 08:18:46273 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
315bool FormGroup::FillCountrySelectControl(FormFieldData* field_data) const {
316 return false;
317}
318
319// static
320bool FormGroup::IsValidState(const string16& value) {
321 return !State::Abbreviation(value).empty() || !State::FullName(value).empty();
322}