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 "chrome/browser/autofill/select_control_handler.h" | 5 #include "chrome/browser/autofill/select_control_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autofill/autofill_country.h" | 13 #include "chrome/browser/autofill/autofill_country.h" |
14 #include "chrome/browser/autofill/autofill_profile.h" | 14 #include "chrome/browser/autofill/autofill_profile.h" |
15 #include "chrome/browser/autofill/form_group.h" | 15 #include "chrome/browser/autofill/form_group.h" |
16 #include "webkit/forms/form_field.h" | 16 #include "chrome/common/form_field_data.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039. | 20 // TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039. |
21 | 21 |
22 class State { | 22 class State { |
23 public: | 23 public: |
24 const char* name; | 24 const char* name; |
25 const char* abbreviation; | 25 const char* abbreviation; |
26 | 26 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 }; | 113 }; |
114 | 114 |
115 const char* const kMonthsNumeric[] = { | 115 const char* const kMonthsNumeric[] = { |
116 NULL, // Padding so index 1 = month 1 = January. | 116 NULL, // Padding so index 1 = month 1 = January. |
117 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", | 117 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", |
118 }; | 118 }; |
119 | 119 |
120 // Returns true if the value was successfully set, meaning |value| was found in | 120 // Returns true if the value was successfully set, meaning |value| was found in |
121 // the list of select options in |field|. | 121 // the list of select options in |field|. |
122 bool SetSelectControlValue(const string16& value, | 122 bool SetSelectControlValue(const string16& value, |
123 webkit::forms::FormField* field) { | 123 FormFieldData* field) { |
124 string16 value_lowercase = StringToLowerASCII(value); | 124 string16 value_lowercase = StringToLowerASCII(value); |
125 | 125 |
126 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | 126 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); |
127 for (size_t i = 0; i < field->option_values.size(); ++i) { | 127 for (size_t i = 0; i < field->option_values.size(); ++i) { |
128 if (value_lowercase == StringToLowerASCII(field->option_values[i]) || | 128 if (value_lowercase == StringToLowerASCII(field->option_values[i]) || |
129 value_lowercase == StringToLowerASCII(field->option_contents[i])) { | 129 value_lowercase == StringToLowerASCII(field->option_contents[i])) { |
130 field->value = field->option_values[i]; | 130 field->value = field->option_values[i]; |
131 return true; | 131 return true; |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 return false; | 135 return false; |
136 } | 136 } |
137 | 137 |
138 bool FillStateSelectControl(const string16& value, | 138 bool FillStateSelectControl(const string16& value, |
139 webkit::forms::FormField* field) { | 139 FormFieldData* field) { |
140 string16 abbrev, full; | 140 string16 abbrev, full; |
141 if (value.size() < 4U) { | 141 if (value.size() < 4U) { |
142 abbrev = value; | 142 abbrev = value; |
143 full = State::FullName(value); | 143 full = State::FullName(value); |
144 } else { | 144 } else { |
145 abbrev = State::Abbreviation(value); | 145 abbrev = State::Abbreviation(value); |
146 full = value; | 146 full = value; |
147 } | 147 } |
148 | 148 |
149 // Try the abbreviation name first. | 149 // Try the abbreviation name first. |
150 if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) | 150 if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) |
151 return true; | 151 return true; |
152 | 152 |
153 if (full.empty()) | 153 if (full.empty()) |
154 return false; | 154 return false; |
155 | 155 |
156 return SetSelectControlValue(full, field); | 156 return SetSelectControlValue(full, field); |
157 } | 157 } |
158 | 158 |
159 bool FillCountrySelectControl(const FormGroup& form_group, | 159 bool FillCountrySelectControl(const FormGroup& form_group, |
160 webkit::forms::FormField* field) { | 160 FormFieldData* field) { |
161 const AutofillProfile& profile = | 161 const AutofillProfile& profile = |
162 static_cast<const AutofillProfile&>(form_group); | 162 static_cast<const AutofillProfile&>(form_group); |
163 std::string country_code = profile.CountryCode(); | 163 std::string country_code = profile.CountryCode(); |
164 std::string app_locale = AutofillCountry::ApplicationLocale(); | 164 std::string app_locale = AutofillCountry::ApplicationLocale(); |
165 | 165 |
166 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | 166 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); |
167 for (size_t i = 0; i < field->option_values.size(); ++i) { | 167 for (size_t i = 0; i < field->option_values.size(); ++i) { |
168 // Canonicalize each <option> value to a country code, and compare to the | 168 // Canonicalize each <option> value to a country code, and compare to the |
169 // target country code. | 169 // target country code. |
170 string16 value = field->option_values[i]; | 170 string16 value = field->option_values[i]; |
171 string16 contents = field->option_contents[i]; | 171 string16 contents = field->option_contents[i]; |
172 if (country_code == AutofillCountry::GetCountryCode(value, app_locale) || | 172 if (country_code == AutofillCountry::GetCountryCode(value, app_locale) || |
173 country_code == AutofillCountry::GetCountryCode(contents, app_locale)) { | 173 country_code == AutofillCountry::GetCountryCode(contents, app_locale)) { |
174 field->value = value; | 174 field->value = value; |
175 return true; | 175 return true; |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
179 return false; | 179 return false; |
180 } | 180 } |
181 | 181 |
182 bool FillExpirationMonthSelectControl(const string16& value, | 182 bool FillExpirationMonthSelectControl(const string16& value, |
183 webkit::forms::FormField* field) { | 183 FormFieldData* field) { |
184 int index = 0; | 184 int index = 0; |
185 if (!base::StringToInt(value, &index) || | 185 if (!base::StringToInt(value, &index) || |
186 index <= 0 || | 186 index <= 0 || |
187 static_cast<size_t>(index) >= arraysize(kMonthsFull)) | 187 static_cast<size_t>(index) >= arraysize(kMonthsFull)) |
188 return false; | 188 return false; |
189 | 189 |
190 bool filled = | 190 bool filled = |
191 SetSelectControlValue(ASCIIToUTF16(kMonthsAbbreviated[index]), field) || | 191 SetSelectControlValue(ASCIIToUTF16(kMonthsAbbreviated[index]), field) || |
192 SetSelectControlValue(ASCIIToUTF16(kMonthsFull[index]), field) || | 192 SetSelectControlValue(ASCIIToUTF16(kMonthsFull[index]), field) || |
193 SetSelectControlValue(ASCIIToUTF16(kMonthsNumeric[index]), field); | 193 SetSelectControlValue(ASCIIToUTF16(kMonthsNumeric[index]), field); |
194 return filled; | 194 return filled; |
195 } | 195 } |
196 | 196 |
197 } // namespace | 197 } // namespace |
198 | 198 |
199 namespace autofill { | 199 namespace autofill { |
200 | 200 |
201 void FillSelectControl(const FormGroup& form_group, | 201 void FillSelectControl(const FormGroup& form_group, |
202 AutofillFieldType type, | 202 AutofillFieldType type, |
203 webkit::forms::FormField* field) { | 203 FormFieldData* field) { |
204 DCHECK(field); | 204 DCHECK(field); |
205 DCHECK_EQ(ASCIIToUTF16("select-one"), field->form_control_type); | 205 DCHECK_EQ(ASCIIToUTF16("select-one"), field->form_control_type); |
206 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); | 206 DCHECK_EQ(field->option_values.size(), field->option_contents.size()); |
207 | 207 |
208 string16 field_text = form_group.GetCanonicalizedInfo(type); | 208 string16 field_text = form_group.GetCanonicalizedInfo(type); |
209 string16 field_text_lower = StringToLowerASCII(field_text); | 209 string16 field_text_lower = StringToLowerASCII(field_text); |
210 if (field_text.empty()) | 210 if (field_text.empty()) |
211 return; | 211 return; |
212 | 212 |
213 string16 value; | 213 string16 value; |
(...skipping 26 matching lines...) Expand all Loading... |
240 FillExpirationMonthSelectControl(field_text, field); | 240 FillExpirationMonthSelectControl(field_text, field); |
241 | 241 |
242 return; | 242 return; |
243 } | 243 } |
244 | 244 |
245 bool IsValidState(const string16& value) { | 245 bool IsValidState(const string16& value) { |
246 return !State::Abbreviation(value).empty() || !State::FullName(value).empty(); | 246 return !State::Abbreviation(value).empty() || !State::FullName(value).empty(); |
247 } | 247 } |
248 | 248 |
249 } // namespace autofill | 249 } // namespace autofill |
OLD | NEW |