| 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 "components/autofill/browser/form_group.h" | 5 #include "components/autofill/browser/form_group.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "components/autofill/browser/autofill_country.h" | 14 #include "components/autofill/browser/autofill_country.h" |
| 15 #include "components/autofill/common/form_field_data.h" | 15 #include "components/autofill/common/form_field_data.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039. | 21 // TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039. |
| 22 | 22 |
| 23 class State { | 23 class State { |
| 24 public: | 24 public: |
| 25 const char* name; | 25 const char* name; |
| 26 const char* abbreviation; | 26 const char* abbreviation; |
| 27 | 27 |
| 28 static const State all_states[]; | 28 static const State all_states[]; |
| 29 | 29 |
| 30 static string16 Abbreviation(const string16& name); | 30 static base::string16 Abbreviation(const base::string16& name); |
| 31 static string16 FullName(const string16& abbreviation); | 31 static base::string16 FullName(const base::string16& abbreviation); |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 const State State::all_states[] = { | 34 const State State::all_states[] = { |
| 35 { "alabama", "al" }, | 35 { "alabama", "al" }, |
| 36 { "alaska", "ak" }, | 36 { "alaska", "ak" }, |
| 37 { "arizona", "az" }, | 37 { "arizona", "az" }, |
| 38 { "arkansas", "ar" }, | 38 { "arkansas", "ar" }, |
| 39 { "california", "ca" }, | 39 { "california", "ca" }, |
| 40 { "colorado", "co" }, | 40 { "colorado", "co" }, |
| 41 { "connecticut", "ct" }, | 41 { "connecticut", "ct" }, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 { "utah", "ut" }, | 80 { "utah", "ut" }, |
| 81 { "vermont", "vt" }, | 81 { "vermont", "vt" }, |
| 82 { "virginia", "va" }, | 82 { "virginia", "va" }, |
| 83 { "washington", "wa" }, | 83 { "washington", "wa" }, |
| 84 { "west virginia", "wv" }, | 84 { "west virginia", "wv" }, |
| 85 { "wisconsin", "wi" }, | 85 { "wisconsin", "wi" }, |
| 86 { "wyoming", "wy" }, | 86 { "wyoming", "wy" }, |
| 87 { NULL, NULL } | 87 { NULL, NULL } |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 string16 State::Abbreviation(const string16& name) { | 90 base::string16 State::Abbreviation(const base::string16& name) { |
| 91 for (const State* state = all_states; state->name; ++state) { | 91 for (const State* state = all_states; state->name; ++state) { |
| 92 if (LowerCaseEqualsASCII(name, state->name)) | 92 if (LowerCaseEqualsASCII(name, state->name)) |
| 93 return ASCIIToUTF16(state->abbreviation); | 93 return ASCIIToUTF16(state->abbreviation); |
| 94 } | 94 } |
| 95 return string16(); | 95 return base::string16(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 string16 State::FullName(const string16& abbreviation) { | 98 base::string16 State::FullName(const base::string16& abbreviation) { |
| 99 for (const State* state = all_states; state->name; ++state) { | 99 for (const State* state = all_states; state->name; ++state) { |
| 100 if (LowerCaseEqualsASCII(abbreviation, state->abbreviation)) | 100 if (LowerCaseEqualsASCII(abbreviation, state->abbreviation)) |
| 101 return ASCIIToUTF16(state->name); | 101 return ASCIIToUTF16(state->name); |
| 102 } | 102 } |
| 103 return string16(); | 103 return base::string16(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 const char* const kMonthsAbbreviated[] = { | 106 const char* const kMonthsAbbreviated[] = { |
| 107 NULL, // Padding so index 1 = month 1 = January. | 107 NULL, // Padding so index 1 = month 1 = January. |
| 108 "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 108 "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 109 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | 109 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 const char* const kMonthsFull[] = { | 112 const char* const kMonthsFull[] = { |
| 113 NULL, // Padding so index 1 = month 1 = January. | 113 NULL, // Padding so index 1 = month 1 = January. |
| 114 "January", "February", "March", "April", "May", "June", | 114 "January", "February", "March", "April", "May", "June", |
| 115 "July", "August", "September", "October", "November", "December", | 115 "July", "August", "September", "October", "November", "December", |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 const char* const kMonthsNumeric[] = { | 118 const char* const kMonthsNumeric[] = { |
| 119 NULL, // Padding so index 1 = month 1 = January. | 119 NULL, // Padding so index 1 = month 1 = January. |
| 120 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", | 120 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 // Returns true if the value was successfully set, meaning |value| was found in | 123 // Returns true if the value was successfully set, meaning |value| was found in |
| 124 // the list of select options in |field|. | 124 // the list of select options in |field|. |
| 125 bool SetSelectControlValue(const string16& value, | 125 bool SetSelectControlValue(const base::string16& value, |
| 126 FormFieldData* field) { | 126 FormFieldData* field) { |
| 127 string16 value_lowercase = StringToLowerASCII(value); | 127 base::string16 value_lowercase = StringToLowerASCII(value); |
| 128 | 128 |
| 129 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | 129 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); |
| 130 for (size_t i = 0; i < field->option_values.size(); ++i) { | 130 for (size_t i = 0; i < field->option_values.size(); ++i) { |
| 131 if (value_lowercase == StringToLowerASCII(field->option_values[i]) || | 131 if (value_lowercase == StringToLowerASCII(field->option_values[i]) || |
| 132 value_lowercase == StringToLowerASCII(field->option_contents[i])) { | 132 value_lowercase == StringToLowerASCII(field->option_contents[i])) { |
| 133 field->value = field->option_values[i]; | 133 field->value = field->option_values[i]; |
| 134 return true; | 134 return true; |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 return false; | 138 return false; |
| 139 } | 139 } |
| 140 | 140 |
| 141 bool FillStateSelectControl(const string16& value, | 141 bool FillStateSelectControl(const base::string16& value, |
| 142 FormFieldData* field) { | 142 FormFieldData* field) { |
| 143 string16 abbrev, full; | 143 base::string16 abbrev, full; |
| 144 if (value.size() < 4U) { | 144 if (value.size() < 4U) { |
| 145 abbrev = value; | 145 abbrev = value; |
| 146 full = State::FullName(value); | 146 full = State::FullName(value); |
| 147 } else { | 147 } else { |
| 148 abbrev = State::Abbreviation(value); | 148 abbrev = State::Abbreviation(value); |
| 149 full = value; | 149 full = value; |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Try the abbreviation name first. | 152 // Try the abbreviation name first. |
| 153 if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) | 153 if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) |
| 154 return true; | 154 return true; |
| 155 | 155 |
| 156 if (full.empty()) | 156 if (full.empty()) |
| 157 return false; | 157 return false; |
| 158 | 158 |
| 159 return SetSelectControlValue(full, field); | 159 return SetSelectControlValue(full, field); |
| 160 } | 160 } |
| 161 | 161 |
| 162 bool FillExpirationMonthSelectControl(const string16& value, | 162 bool FillExpirationMonthSelectControl(const base::string16& value, |
| 163 FormFieldData* field) { | 163 FormFieldData* field) { |
| 164 int index = 0; | 164 int index = 0; |
| 165 if (!base::StringToInt(value, &index) || | 165 if (!base::StringToInt(value, &index) || |
| 166 index <= 0 || | 166 index <= 0 || |
| 167 static_cast<size_t>(index) >= arraysize(kMonthsFull)) | 167 static_cast<size_t>(index) >= arraysize(kMonthsFull)) |
| 168 return false; | 168 return false; |
| 169 | 169 |
| 170 bool filled = | 170 bool filled = |
| 171 SetSelectControlValue(ASCIIToUTF16(kMonthsAbbreviated[index]), field) || | 171 SetSelectControlValue(ASCIIToUTF16(kMonthsAbbreviated[index]), field) || |
| 172 SetSelectControlValue(ASCIIToUTF16(kMonthsFull[index]), field) || | 172 SetSelectControlValue(ASCIIToUTF16(kMonthsFull[index]), field) || |
| 173 SetSelectControlValue(ASCIIToUTF16(kMonthsNumeric[index]), field); | 173 SetSelectControlValue(ASCIIToUTF16(kMonthsNumeric[index]), field); |
| 174 return filled; | 174 return filled; |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Try to fill a credit card type |value| (Visa, MasterCard, etc.) into the | 177 // Try to fill a credit card type |value| (Visa, MasterCard, etc.) into the |
| 178 // given |field|. | 178 // given |field|. |
| 179 bool FillCreditCardTypeSelectControl(const string16& value, | 179 bool FillCreditCardTypeSelectControl(const base::string16& value, |
| 180 FormFieldData* field) { | 180 FormFieldData* field) { |
| 181 // Try stripping off spaces. | 181 // Try stripping off spaces. |
| 182 string16 value_stripped; | 182 base::string16 value_stripped; |
| 183 RemoveChars(StringToLowerASCII(value), kWhitespaceUTF16, &value_stripped); | 183 RemoveChars(StringToLowerASCII(value), kWhitespaceUTF16, &value_stripped); |
| 184 | 184 |
| 185 for (size_t i = 0; i < field->option_values.size(); ++i) { | 185 for (size_t i = 0; i < field->option_values.size(); ++i) { |
| 186 string16 option_value_lowercase; | 186 base::string16 option_value_lowercase; |
| 187 RemoveChars(StringToLowerASCII(field->option_values[i]), kWhitespaceUTF16, | 187 RemoveChars(StringToLowerASCII(field->option_values[i]), kWhitespaceUTF16, |
| 188 &option_value_lowercase); | 188 &option_value_lowercase); |
| 189 string16 option_contents_lowercase; | 189 base::string16 option_contents_lowercase; |
| 190 RemoveChars(StringToLowerASCII(field->option_contents[i]), kWhitespaceUTF16, | 190 RemoveChars(StringToLowerASCII(field->option_contents[i]), kWhitespaceUTF16, |
| 191 &option_contents_lowercase); | 191 &option_contents_lowercase); |
| 192 | 192 |
| 193 // Perform a case-insensitive comparison; but fill the form with the | 193 // Perform a case-insensitive comparison; but fill the form with the |
| 194 // original text, not the lowercased version. | 194 // original text, not the lowercased version. |
| 195 if (value_stripped == option_value_lowercase || | 195 if (value_stripped == option_value_lowercase || |
| 196 value_stripped == option_contents_lowercase) { | 196 value_stripped == option_contents_lowercase) { |
| 197 field->value = field->option_values[i]; | 197 field->value = field->option_values[i]; |
| 198 return true; | 198 return true; |
| 199 } | 199 } |
| 200 } | 200 } |
| 201 | 201 |
| 202 // For American Express, also try filling as "AmEx". | 202 // For American Express, also try filling as "AmEx". |
| 203 if (value == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX)) | 203 if (value == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX)) |
| 204 return FillCreditCardTypeSelectControl(ASCIIToUTF16("AmEx"), field); | 204 return FillCreditCardTypeSelectControl(ASCIIToUTF16("AmEx"), field); |
| 205 | 205 |
| 206 return false; | 206 return false; |
| 207 } | 207 } |
| 208 | 208 |
| 209 } // namespace | 209 } // namespace |
| 210 | 210 |
| 211 std::string FormGroup::GetGUID() const { | 211 std::string FormGroup::GetGUID() const { |
| 212 NOTREACHED(); | 212 NOTREACHED(); |
| 213 return std::string(); | 213 return std::string(); |
| 214 } | 214 } |
| 215 | 215 |
| 216 void FormGroup::GetMatchingTypes(const string16& text, | 216 void FormGroup::GetMatchingTypes(const base::string16& text, |
| 217 const std::string& app_locale, | 217 const std::string& app_locale, |
| 218 FieldTypeSet* matching_types) const { | 218 FieldTypeSet* matching_types) const { |
| 219 if (text.empty()) { | 219 if (text.empty()) { |
| 220 matching_types->insert(EMPTY_TYPE); | 220 matching_types->insert(EMPTY_TYPE); |
| 221 return; | 221 return; |
| 222 } | 222 } |
| 223 | 223 |
| 224 FieldTypeSet types; | 224 FieldTypeSet types; |
| 225 GetSupportedTypes(&types); | 225 GetSupportedTypes(&types); |
| 226 for (FieldTypeSet::const_iterator type = types.begin(); | 226 for (FieldTypeSet::const_iterator type = types.begin(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 237 FieldTypeSet* non_empty_types) const { | 237 FieldTypeSet* non_empty_types) const { |
| 238 FieldTypeSet types; | 238 FieldTypeSet types; |
| 239 GetSupportedTypes(&types); | 239 GetSupportedTypes(&types); |
| 240 for (FieldTypeSet::const_iterator type = types.begin(); | 240 for (FieldTypeSet::const_iterator type = types.begin(); |
| 241 type != types.end(); ++type) { | 241 type != types.end(); ++type) { |
| 242 if (!GetInfo(*type, app_locale).empty()) | 242 if (!GetInfo(*type, app_locale).empty()) |
| 243 non_empty_types->insert(*type); | 243 non_empty_types->insert(*type); |
| 244 } | 244 } |
| 245 } | 245 } |
| 246 | 246 |
| 247 string16 FormGroup::GetInfo(AutofillFieldType type, | 247 base::string16 FormGroup::GetInfo(AutofillFieldType type, |
| 248 const std::string& app_locale) const { | 248 const std::string& app_locale) const { |
| 249 return GetRawInfo(type); | 249 return GetRawInfo(type); |
| 250 } | 250 } |
| 251 | 251 |
| 252 bool FormGroup::SetInfo(AutofillFieldType type, | 252 bool FormGroup::SetInfo(AutofillFieldType type, |
| 253 const string16& value, | 253 const base::string16& value, |
| 254 const std::string& app_locale) { | 254 const std::string& app_locale) { |
| 255 SetRawInfo(type, value); | 255 SetRawInfo(type, value); |
| 256 return true; | 256 return true; |
| 257 } | 257 } |
| 258 | 258 |
| 259 void FormGroup::FillFormField(const AutofillField& field, | 259 void FormGroup::FillFormField(const AutofillField& field, |
| 260 size_t variant, | 260 size_t variant, |
| 261 const std::string& app_locale, | 261 const std::string& app_locale, |
| 262 FormFieldData* field_data) const { | 262 FormFieldData* field_data) const { |
| 263 NOTREACHED(); | 263 NOTREACHED(); |
| 264 } | 264 } |
| 265 | 265 |
| 266 void FormGroup::FillSelectControl(AutofillFieldType type, | 266 void FormGroup::FillSelectControl(AutofillFieldType type, |
| 267 const std::string& app_locale, | 267 const std::string& app_locale, |
| 268 FormFieldData* field) const { | 268 FormFieldData* field) const { |
| 269 DCHECK(field); | 269 DCHECK(field); |
| 270 DCHECK_EQ("select-one", field->form_control_type); | 270 DCHECK_EQ("select-one", field->form_control_type); |
| 271 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | 271 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); |
| 272 | 272 |
| 273 string16 field_text = GetInfo(type, app_locale); | 273 base::string16 field_text = GetInfo(type, app_locale); |
| 274 string16 field_text_lower = StringToLowerASCII(field_text); | 274 base::string16 field_text_lower = StringToLowerASCII(field_text); |
| 275 if (field_text.empty()) | 275 if (field_text.empty()) |
| 276 return; | 276 return; |
| 277 | 277 |
| 278 string16 value; | 278 base::string16 value; |
| 279 for (size_t i = 0; i < field->option_values.size(); ++i) { | 279 for (size_t i = 0; i < field->option_values.size(); ++i) { |
| 280 if (field_text == field->option_values[i] || | 280 if (field_text == field->option_values[i] || |
| 281 field_text == field->option_contents[i]) { | 281 field_text == field->option_contents[i]) { |
| 282 // An exact match, use it. | 282 // An exact match, use it. |
| 283 value = field->option_values[i]; | 283 value = field->option_values[i]; |
| 284 break; | 284 break; |
| 285 } | 285 } |
| 286 | 286 |
| 287 if (field_text_lower == StringToLowerASCII(field->option_values[i]) || | 287 if (field_text_lower == StringToLowerASCII(field->option_values[i]) || |
| 288 field_text_lower == StringToLowerASCII(field->option_contents[i])) { | 288 field_text_lower == StringToLowerASCII(field->option_contents[i])) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 312 FillCreditCardTypeSelectControl(field_text, field); | 312 FillCreditCardTypeSelectControl(field_text, field); |
| 313 } | 313 } |
| 314 } | 314 } |
| 315 | 315 |
| 316 bool FormGroup::FillCountrySelectControl(const std::string& app_locale, | 316 bool FormGroup::FillCountrySelectControl(const std::string& app_locale, |
| 317 FormFieldData* field_data) const { | 317 FormFieldData* field_data) const { |
| 318 return false; | 318 return false; |
| 319 } | 319 } |
| 320 | 320 |
| 321 // static | 321 // static |
| 322 bool FormGroup::IsValidState(const string16& value) { | 322 bool FormGroup::IsValidState(const base::string16& value) { |
| 323 return !State::Abbreviation(value).empty() || !State::FullName(value).empty(); | 323 return !State::Abbreviation(value).empty() || !State::FullName(value).empty(); |
| 324 } | 324 } |
| OLD | NEW |