Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: components/autofill/core/browser/form_structure.cc

Issue 22040002: [Autofill] Add a separate enumeration for HTML field type hints. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix browser test Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/core/browser/form_structure.h" 5 #include "components/autofill/core/browser/form_structure.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 const char kAcceptedFeaturesExperiment[] = "e"; // e=experiments 50 const char kAcceptedFeaturesExperiment[] = "e"; // e=experiments
51 const char kAcceptedFeaturesAutocheckoutExperiment[] = "a,e"; // a=autocheckout 51 const char kAcceptedFeaturesAutocheckoutExperiment[] = "a,e"; // a=autocheckout
52 const char kClientVersion[] = "6.1.1715.1442/en (GGLL)"; 52 const char kClientVersion[] = "6.1.1715.1442/en (GGLL)";
53 const char kXMLDeclaration[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 53 const char kXMLDeclaration[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
54 const char kXMLElementAutofillQuery[] = "autofillquery"; 54 const char kXMLElementAutofillQuery[] = "autofillquery";
55 const char kXMLElementAutofillUpload[] = "autofillupload"; 55 const char kXMLElementAutofillUpload[] = "autofillupload";
56 const char kXMLElementFieldAssignments[] = "fieldassignments"; 56 const char kXMLElementFieldAssignments[] = "fieldassignments";
57 const char kXMLElementField[] = "field"; 57 const char kXMLElementField[] = "field";
58 const char kXMLElementFields[] = "fields"; 58 const char kXMLElementFields[] = "fields";
59 const char kXMLElementForm[] = "form"; 59 const char kXMLElementForm[] = "form";
60 const char kBillingSection[] = "billing"; 60 const char kBillingMode[] = "billing";
61 const char kShippingSection[] = "shipping"; 61 const char kShippingMode[] = "shipping";
62 62
63 // Stip away >= 5 consecutive digits. 63 // Stip away >= 5 consecutive digits.
64 const char kIgnorePatternInFieldName[] = "\\d{5,}+"; 64 const char kIgnorePatternInFieldName[] = "\\d{5,}+";
65 65
66 // Helper for |EncodeUploadRequest()| that creates a bit field corresponding to 66 // Helper for |EncodeUploadRequest()| that creates a bit field corresponding to
67 // |available_field_types| and returns the hex representation as a string. 67 // |available_field_types| and returns the hex representation as a string.
68 std::string EncodeFieldTypes(const ServerFieldTypeSet& available_field_types) { 68 std::string EncodeFieldTypes(const ServerFieldTypeSet& available_field_types) {
69 // There are |MAX_VALID_FIELD_TYPE| different field types and 8 bits per byte, 69 // There are |MAX_VALID_FIELD_TYPE| different field types and 8 bits per byte,
70 // so we need ceil(MAX_VALID_FIELD_TYPE / 8) bytes to encode the bit field. 70 // so we need ceil(MAX_VALID_FIELD_TYPE / 8) bytes to encode the bit field.
71 const size_t kNumBytes = (MAX_VALID_FIELD_TYPE + 0x7) / 8; 71 const size_t kNumBytes = (MAX_VALID_FIELD_TYPE + 0x7) / 8;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 // specified in the implementation section of http://is.gd/whatwg_autocomplete 158 // specified in the implementation section of http://is.gd/whatwg_autocomplete
159 // Note that "fax" and "pager" are intentionally ignored, as Chrome does not 159 // Note that "fax" and "pager" are intentionally ignored, as Chrome does not
160 // support filling either type of information. 160 // support filling either type of information.
161 bool IsContactTypeHint(const std::string& token) { 161 bool IsContactTypeHint(const std::string& token) {
162 return token == "home" || token == "work" || token == "mobile"; 162 return token == "home" || token == "work" || token == "mobile";
163 } 163 }
164 164
165 // Returns |true| iff the |token| is a type hint appropriate for a field of the 165 // Returns |true| iff the |token| is a type hint appropriate for a field of the
166 // given |field_type|, as specified in the implementation section of 166 // given |field_type|, as specified in the implementation section of
167 // http://is.gd/whatwg_autocomplete 167 // http://is.gd/whatwg_autocomplete
168 // TODO(isherman): This should use HTML field types, not native ones.
169 bool ContactTypeHintMatchesFieldType(const std::string& token, 168 bool ContactTypeHintMatchesFieldType(const std::string& token,
170 ServerFieldType field_type) { 169 HtmlFieldType field_type) {
171 // The "home" and "work" type hints are only appropriate for email and phone 170 // The "home" and "work" type hints are only appropriate for email and phone
172 // number field types. 171 // number field types.
173 if (token == "home" || token == "work") { 172 if (token == "home" || token == "work") {
174 return field_type == EMAIL_ADDRESS || 173 return field_type == HTML_TYPE_EMAIL ||
175 (field_type >= PHONE_HOME_NUMBER && 174 (field_type >= HTML_TYPE_TEL &&
176 field_type <= PHONE_HOME_WHOLE_NUMBER); 175 field_type <= HTML_TYPE_TEL_LOCAL_SUFFIX);
177 } 176 }
178 177
179 // The "mobile" type hint is only appropriate for phone number field types. 178 // The "mobile" type hint is only appropriate for phone number field types.
180 // Note that "fax" and "pager" are intentionally ignored, as Chrome does not 179 // Note that "fax" and "pager" are intentionally ignored, as Chrome does not
181 // support filling either type of information. 180 // support filling either type of information.
182 if (token == "mobile") { 181 if (token == "mobile") {
183 return field_type >= PHONE_HOME_NUMBER && 182 return field_type >= HTML_TYPE_TEL &&
184 field_type <= PHONE_HOME_WHOLE_NUMBER; 183 field_type <= HTML_TYPE_TEL_LOCAL_SUFFIX;
185 } 184 }
186 185
187 return false; 186 return false;
188 } 187 }
189 188
190 // Returns the Chrome Autofill-supported field type corresponding to the given 189 // Returns the Chrome Autofill-supported field type corresponding to the given
191 // |autocomplete_type|, if there is one, in the context of the given |field|. 190 // |autocomplete_attribute_value|, if there is one, in the context of the given
192 // Chrome Autofill supports a subset of the field types listed at 191 // |field|. Chrome Autofill supports a subset of the field types listed at
193 // http://is.gd/whatwg_autocomplete 192 // http://is.gd/whatwg_autocomplete
194 // TODO(isherman): This should use HTML field types, not native ones. 193 HtmlFieldType FieldTypeFromAutocompleteAttributeValue(
195 ServerFieldType FieldTypeFromAutocompleteType( 194 const std::string& autocomplete_attribute_value,
196 const std::string& autocomplete_type,
197 const AutofillField& field) { 195 const AutofillField& field) {
198 if (autocomplete_type == "name") 196 if (autocomplete_attribute_value == "name")
199 return NAME_FULL; 197 return HTML_TYPE_NAME;
200 198
201 if (autocomplete_type == "given-name") 199 if (autocomplete_attribute_value == "given-name")
202 return NAME_FIRST; 200 return HTML_TYPE_GIVEN_NAME;
203 201
204 if (autocomplete_type == "additional-name") { 202 if (autocomplete_attribute_value == "additional-name") {
205 if (field.max_length == 1) 203 if (field.max_length == 1)
206 return NAME_MIDDLE_INITIAL; 204 return HTML_TYPE_ADDITIONAL_NAME_INITIAL;
207 else 205 else
208 return NAME_MIDDLE; 206 return HTML_TYPE_ADDITIONAL_NAME;
209 } 207 }
210 208
211 if (autocomplete_type == "family-name") 209 if (autocomplete_attribute_value == "family-name")
212 return NAME_LAST; 210 return HTML_TYPE_FAMILY_NAME;
213 211
214 if (autocomplete_type == "honorific-suffix") 212 if (autocomplete_attribute_value == "organization")
215 return NAME_SUFFIX; 213 return HTML_TYPE_ORGANIZATION;
216 214
217 if (autocomplete_type == "organization") 215 if (autocomplete_attribute_value == "address-line1")
218 return COMPANY_NAME; 216 return HTML_TYPE_ADDRESS_LINE1;
219 217
220 if (autocomplete_type == "address-line1") 218 if (autocomplete_attribute_value == "address-line2")
221 return ADDRESS_HOME_LINE1; 219 return HTML_TYPE_ADDRESS_LINE2;
222 220
223 if (autocomplete_type == "address-line2") 221 if (autocomplete_attribute_value == "locality")
224 return ADDRESS_HOME_LINE2; 222 return HTML_TYPE_LOCALITY;
225 223
226 if (autocomplete_type == "locality") 224 if (autocomplete_attribute_value == "region")
227 return ADDRESS_HOME_CITY; 225 return HTML_TYPE_REGION;
228 226
229 if (autocomplete_type == "region") 227 if (autocomplete_attribute_value == "country")
230 return ADDRESS_HOME_STATE; 228 return HTML_TYPE_COUNTRY_CODE;
231 229
232 if (autocomplete_type == "country") 230 if (autocomplete_attribute_value == "country-name")
233 return ADDRESS_HOME_COUNTRY; 231 return HTML_TYPE_COUNTRY_NAME;
234 232
235 if (autocomplete_type == "postal-code") 233 if (autocomplete_attribute_value == "postal-code")
236 return ADDRESS_HOME_ZIP; 234 return HTML_TYPE_POSTAL_CODE;
237 235
238 if (autocomplete_type == "cc-name") 236 if (autocomplete_attribute_value == "cc-name")
239 return CREDIT_CARD_NAME; 237 return HTML_TYPE_CREDIT_CARD_NAME;
240 238
241 if (autocomplete_type == "cc-number") 239 if (autocomplete_attribute_value == "cc-number")
242 return CREDIT_CARD_NUMBER; 240 return HTML_TYPE_CREDIT_CARD_NUMBER;
243 241
244 if (autocomplete_type == "cc-exp") { 242 if (autocomplete_attribute_value == "cc-exp") {
245 if (field.max_length == 5) 243 if (field.max_length == 5)
246 return CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; 244 return HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR;
245 else if (field.max_length == 7)
246 return HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR;
247 else 247 else
248 return CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR; 248 return HTML_TYPE_CREDIT_CARD_EXP;
249 } 249 }
250 250
251 if (autocomplete_type == "cc-exp-month") 251 if (autocomplete_attribute_value == "cc-exp-month")
252 return CREDIT_CARD_EXP_MONTH; 252 return HTML_TYPE_CREDIT_CARD_EXP_MONTH;
253 253
254 if (autocomplete_type == "cc-exp-year") { 254 if (autocomplete_attribute_value == "cc-exp-year") {
255 if (field.max_length == 2) 255 if (field.max_length == 2)
256 return CREDIT_CARD_EXP_2_DIGIT_YEAR; 256 return HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR;
257 else if (field.max_length == 4)
258 return HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR;
257 else 259 else
258 return CREDIT_CARD_EXP_4_DIGIT_YEAR; 260 return HTML_TYPE_CREDIT_CARD_EXP_YEAR;
259 } 261 }
260 262
261 if (autocomplete_type == "cc-csc") 263 if (autocomplete_attribute_value == "cc-csc")
262 return CREDIT_CARD_VERIFICATION_CODE; 264 return HTML_TYPE_CREDIT_CARD_VERIFICATION_CODE;
263 265
264 if (autocomplete_type == "cc-type") 266 if (autocomplete_attribute_value == "cc-type")
265 return CREDIT_CARD_TYPE; 267 return HTML_TYPE_CREDIT_CARD_TYPE;
266 268
267 if (autocomplete_type == "tel") 269 if (autocomplete_attribute_value == "tel")
268 return PHONE_HOME_WHOLE_NUMBER; 270 return HTML_TYPE_TEL;
269 271
270 if (autocomplete_type == "tel-country-code") 272 if (autocomplete_attribute_value == "tel-country-code")
271 return PHONE_HOME_COUNTRY_CODE; 273 return HTML_TYPE_TEL_COUNTRY_CODE;
272 274
273 if (autocomplete_type == "tel-national") 275 if (autocomplete_attribute_value == "tel-national")
274 return PHONE_HOME_CITY_AND_NUMBER; 276 return HTML_TYPE_TEL_NATIONAL;
275 277
276 if (autocomplete_type == "tel-area-code") 278 if (autocomplete_attribute_value == "tel-area-code")
277 return PHONE_HOME_CITY_CODE; 279 return HTML_TYPE_TEL_AREA_CODE;
278 280
279 if (autocomplete_type == "tel-local") 281 if (autocomplete_attribute_value == "tel-local")
280 return PHONE_HOME_NUMBER; 282 return HTML_TYPE_TEL_LOCAL;
281 283
282 if (autocomplete_type == "tel-local-prefix") 284 if (autocomplete_attribute_value == "tel-local-prefix")
283 return PHONE_HOME_NUMBER; 285 return HTML_TYPE_TEL_LOCAL_PREFIX;
284 286
285 if (autocomplete_type == "tel-local-suffix") 287 if (autocomplete_attribute_value == "tel-local-suffix")
286 return PHONE_HOME_NUMBER; 288 return HTML_TYPE_TEL_LOCAL_SUFFIX;
287 289
288 if (autocomplete_type == "email") 290 if (autocomplete_attribute_value == "email")
289 return EMAIL_ADDRESS; 291 return HTML_TYPE_EMAIL;
290 292
291 return UNKNOWN_TYPE; 293 return HTML_TYPE_UNKNOWN;
292 } 294 }
293 295
294 std::string StripDigitsIfRequired(const base::string16& input) { 296 std::string StripDigitsIfRequired(const base::string16& input) {
295 UErrorCode status = U_ZERO_ERROR; 297 UErrorCode status = U_ZERO_ERROR;
296 CR_DEFINE_STATIC_LOCAL(icu::UnicodeString, icu_pattern, 298 CR_DEFINE_STATIC_LOCAL(icu::UnicodeString, icu_pattern,
297 (kIgnorePatternInFieldName)); 299 (kIgnorePatternInFieldName));
298 CR_DEFINE_STATIC_LOCAL(icu::RegexMatcher, matcher, 300 CR_DEFINE_STATIC_LOCAL(icu::RegexMatcher, matcher,
299 (icu_pattern, UREGEX_CASE_INSENSITIVE, status)); 301 (icu_pattern, UREGEX_CASE_INSENSITIVE, status));
300 DCHECK_EQ(status, U_ZERO_ERROR); 302 DCHECK_EQ(status, U_ZERO_ERROR);
301 303
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 372
371 FormStructure::~FormStructure() {} 373 FormStructure::~FormStructure() {}
372 374
373 void FormStructure::DetermineHeuristicTypes( 375 void FormStructure::DetermineHeuristicTypes(
374 const AutofillMetrics& metric_logger) { 376 const AutofillMetrics& metric_logger) {
375 // First, try to detect field types based on each field's |autocomplete| 377 // First, try to detect field types based on each field's |autocomplete|
376 // attribute value. If there is at least one form field that specifies an 378 // attribute value. If there is at least one form field that specifies an
377 // autocomplete type hint, don't try to apply other heuristics to match fields 379 // autocomplete type hint, don't try to apply other heuristics to match fields
378 // in this form. 380 // in this form.
379 bool has_author_specified_sections; 381 bool has_author_specified_sections;
380 ParseFieldTypesFromAutocompleteAttributes(PARSE_FOR_AUTOFILL, 382 ParseFieldTypesFromAutocompleteAttributes(&has_author_specified_types_,
381 &has_author_specified_types_,
382 &has_author_specified_sections); 383 &has_author_specified_sections);
383 384
384 if (!has_author_specified_types_) { 385 if (!has_author_specified_types_) {
385 ServerFieldTypeMap field_type_map; 386 ServerFieldTypeMap field_type_map;
386 FormField::ParseFormFields(fields_.get(), &field_type_map); 387 FormField::ParseFormFields(fields_.get(), &field_type_map);
387 for (size_t i = 0; i < field_count(); ++i) { 388 for (size_t i = 0; i < field_count(); ++i) {
388 AutofillField* field = fields_[i]; 389 AutofillField* field = fields_[i];
389 ServerFieldTypeMap::iterator iter = 390 ServerFieldTypeMap::iterator iter =
390 field_type_map.find(field->unique_name()); 391 field_type_map.find(field->unique_name());
391 if (iter != field_type_map.end()) 392 if (iter != field_type_map.end())
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 break; 601 break;
601 602
602 // UNKNOWN_TYPE is reserved for use by the client. 603 // UNKNOWN_TYPE is reserved for use by the client.
603 DCHECK_NE(current_info->field_type, UNKNOWN_TYPE); 604 DCHECK_NE(current_info->field_type, UNKNOWN_TYPE);
604 605
605 ServerFieldType heuristic_type = (*field)->heuristic_type(); 606 ServerFieldType heuristic_type = (*field)->heuristic_type();
606 if (heuristic_type != UNKNOWN_TYPE) 607 if (heuristic_type != UNKNOWN_TYPE)
607 heuristics_detected_fillable_field = true; 608 heuristics_detected_fillable_field = true;
608 609
609 (*field)->set_server_type(current_info->field_type); 610 (*field)->set_server_type(current_info->field_type);
610 if (heuristic_type != (*field)->Type().server_type()) 611 if (heuristic_type != (*field)->Type().GetStorableType())
611 query_response_overrode_heuristics = true; 612 query_response_overrode_heuristics = true;
612 613
613 // Copy default value into the field if available. 614 // Copy default value into the field if available.
614 if (!current_info->default_value.empty()) 615 if (!current_info->default_value.empty())
615 (*field)->set_default_value(current_info->default_value); 616 (*field)->set_default_value(current_info->default_value);
616 617
617 ++current_info; 618 ++current_info;
618 } 619 }
619 620
620 form->UpdateAutofillCount(); 621 form->UpdateAutofillCount();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 form.experiment_id = form_structure->server_experiment_id_; 653 form.experiment_id = form_structure->server_experiment_id_;
653 654
654 for (std::vector<AutofillField*>::const_iterator field = 655 for (std::vector<AutofillField*>::const_iterator field =
655 form_structure->fields_.begin(); 656 form_structure->fields_.begin();
656 field != form_structure->fields_.end(); ++field) { 657 field != form_structure->fields_.end(); ++field) {
657 form.data.fields.push_back(FormFieldData(**field)); 658 form.data.fields.push_back(FormFieldData(**field));
658 659
659 FormFieldDataPredictions annotated_field; 660 FormFieldDataPredictions annotated_field;
660 annotated_field.signature = (*field)->FieldSignature(); 661 annotated_field.signature = (*field)->FieldSignature();
661 annotated_field.heuristic_type = 662 annotated_field.heuristic_type =
662 AutofillType::FieldTypeToString((*field)->heuristic_type()); 663 AutofillType((*field)->heuristic_type()).ToString();
663 annotated_field.server_type = 664 annotated_field.server_type =
664 AutofillType::FieldTypeToString((*field)->server_type()); 665 AutofillType((*field)->server_type()).ToString();
665 annotated_field.overall_type = 666 annotated_field.overall_type = (*field)->Type().ToString();
666 AutofillType::FieldTypeToString((*field)->Type().server_type());
667 form.fields.push_back(annotated_field); 667 form.fields.push_back(annotated_field);
668 } 668 }
669 669
670 forms->push_back(form); 670 forms->push_back(form);
671 } 671 }
672 } 672 }
673 673
674 std::string FormStructure::FormSignature() const { 674 std::string FormStructure::FormSignature() const {
675 std::string scheme(target_url_.scheme()); 675 std::string scheme(target_url_.scheme());
676 std::string host(target_url_.host()); 676 std::string host(target_url_.host());
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
828 for (ServerFieldTypeSet::const_iterator it = field_types.begin(); 828 for (ServerFieldTypeSet::const_iterator it = field_types.begin();
829 it != field_types.end(); 829 it != field_types.end();
830 ++it) { 830 ++it) {
831 // Since we currently only support US phone numbers, the (city code + main 831 // Since we currently only support US phone numbers, the (city code + main
832 // digits) number is almost always identical to the whole phone number. 832 // digits) number is almost always identical to the whole phone number.
833 // TODO(isherman): Improve this logic once we add support for 833 // TODO(isherman): Improve this logic once we add support for
834 // international numbers. 834 // international numbers.
835 if (*it == PHONE_HOME_CITY_AND_NUMBER) 835 if (*it == PHONE_HOME_CITY_AND_NUMBER)
836 collapsed_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 836 collapsed_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
837 else 837 else
838 collapsed_field_types.insert(AutofillType::GetEquivalentFieldType(*it)); 838 collapsed_field_types.insert(AutofillType(*it).GetStorableType());
839 } 839 }
840 840
841 // Capture the field's type, if it is unambiguous. 841 // Capture the field's type, if it is unambiguous.
842 ServerFieldType field_type = UNKNOWN_TYPE; 842 ServerFieldType field_type = UNKNOWN_TYPE;
843 if (collapsed_field_types.size() == 1) 843 if (collapsed_field_types.size() == 1)
844 field_type = *collapsed_field_types.begin(); 844 field_type = *collapsed_field_types.begin();
845 845
846 ServerFieldType heuristic_type = field->heuristic_type(); 846 ServerFieldType heuristic_type =
847 ServerFieldType server_type = field->server_type(); 847 AutofillType(field->heuristic_type()).GetStorableType();
848 ServerFieldType predicted_type = field->Type().server_type(); 848 ServerFieldType server_type =
849 AutofillType(field->server_type()).GetStorableType();
850 ServerFieldType predicted_type = field->Type().GetStorableType();
849 851
850 // Log heuristic, server, and overall type quality metrics, independently of 852 // Log heuristic, server, and overall type quality metrics, independently of
851 // whether the field was autofilled. 853 // whether the field was autofilled.
852 if (heuristic_type == UNKNOWN_TYPE) { 854 if (heuristic_type == UNKNOWN_TYPE) {
853 metric_logger.LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN, 855 metric_logger.LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
854 field_type, experiment_id); 856 field_type, experiment_id);
855 } else if (field_types.count(heuristic_type)) { 857 } else if (field_types.count(heuristic_type)) {
856 metric_logger.LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH, 858 metric_logger.LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
857 field_type, experiment_id); 859 field_type, experiment_id);
858 } else { 860 } else {
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
1073 break; 1075 break;
1074 case FormStructure::FIELD_ASSIGNMENTS: 1076 case FormStructure::FIELD_ASSIGNMENTS:
1075 EncodeFieldForFieldAssignments(*field, encompassing_xml_element); 1077 EncodeFieldForFieldAssignments(*field, encompassing_xml_element);
1076 break; 1078 break;
1077 } 1079 }
1078 } 1080 }
1079 return true; 1081 return true;
1080 } 1082 }
1081 1083
1082 void FormStructure::ParseFieldTypesFromAutocompleteAttributes( 1084 void FormStructure::ParseFieldTypesFromAutocompleteAttributes(
1083 ParseTarget parse_target,
1084 bool* found_types, 1085 bool* found_types,
1085 bool* found_sections) { 1086 bool* found_sections) {
1086 const std::string kDefaultSection = "-default"; 1087 const std::string kDefaultSection = "-default";
1087 1088
1088 *found_types = false; 1089 *found_types = false;
1089 *found_sections = false; 1090 *found_sections = false;
1090 for (std::vector<AutofillField*>::iterator it = fields_.begin(); 1091 for (std::vector<AutofillField*>::iterator it = fields_.begin();
1091 it != fields_.end(); ++it) { 1092 it != fields_.end(); ++it) {
1092 AutofillField* field = *it; 1093 AutofillField* field = *it;
1093 1094
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1125 // Tokenize the attribute value. Per the spec, the tokens are parsed in 1126 // Tokenize the attribute value. Per the spec, the tokens are parsed in
1126 // reverse order. 1127 // reverse order.
1127 std::vector<std::string> tokens; 1128 std::vector<std::string> tokens;
1128 Tokenize(autocomplete_attribute, " ", &tokens); 1129 Tokenize(autocomplete_attribute, " ", &tokens);
1129 1130
1130 // The final token must be the field type. 1131 // The final token must be the field type.
1131 // If it is not one of the known types, abort. 1132 // If it is not one of the known types, abort.
1132 DCHECK(!tokens.empty()); 1133 DCHECK(!tokens.empty());
1133 std::string field_type_token = tokens.back(); 1134 std::string field_type_token = tokens.back();
1134 tokens.pop_back(); 1135 tokens.pop_back();
1135 ServerFieldType field_type = 1136 HtmlFieldType field_type =
1136 FieldTypeFromAutocompleteType(field_type_token, *field); 1137 FieldTypeFromAutocompleteAttributeValue(field_type_token, *field);
1137 if (field_type == UNKNOWN_TYPE) 1138 if (field_type == HTML_TYPE_UNKNOWN)
1138 continue; 1139 continue;
1139 1140
1140 // The preceding token, if any, may be a type hint. 1141 // The preceding token, if any, may be a type hint.
1141 if (!tokens.empty() && IsContactTypeHint(tokens.back())) { 1142 if (!tokens.empty() && IsContactTypeHint(tokens.back())) {
1142 // If it is, it must match the field type; otherwise, abort. 1143 // If it is, it must match the field type; otherwise, abort.
1143 // Note that an invalid token invalidates the entire attribute value, even 1144 // Note that an invalid token invalidates the entire attribute value, even
1144 // if the other tokens are valid. 1145 // if the other tokens are valid.
1145 if (!ContactTypeHintMatchesFieldType(tokens.back(), field_type)) 1146 if (!ContactTypeHintMatchesFieldType(tokens.back(), field_type))
1146 continue; 1147 continue;
1147 1148
1148 // Chrome Autofill ignores these type hints. 1149 // Chrome Autofill ignores these type hints.
1149 tokens.pop_back(); 1150 tokens.pop_back();
1150 } 1151 }
1151 1152
1152 // The preceding token, if any, may be a fixed string that is either 1153 // The preceding token, if any, may be a fixed string that is either
1153 // "shipping" or "billing". Chrome Autofill treats these as implicit 1154 // "shipping" or "billing". Chrome Autofill treats these as implicit
1154 // section name suffixes. 1155 // section name suffixes.
1155 DCHECK_EQ(kDefaultSection, field->section()); 1156 DCHECK_EQ(kDefaultSection, field->section());
1156 std::string section = field->section(); 1157 std::string section = field->section();
1157 if (!tokens.empty() && 1158 HtmlFieldMode mode = HTML_MODE_NONE;
1158 (tokens.back() == kShippingSection || 1159 if (!tokens.empty()) {
1159 tokens.back() == kBillingSection)) { 1160 if (tokens.back() == kShippingMode)
1160 // Set Autofill field type to billing if section is billing. 1161 mode = HTML_MODE_SHIPPING;
1161 if (tokens.back() == kBillingSection) { 1162 else if (tokens.back() == kBillingMode)
1162 field_type = AutofillType::GetEquivalentBillingFieldType(field_type); 1163 mode = HTML_MODE_BILLING;
1164 }
1163 1165
1164 // The Autofill dialog uses the type CREDIT_CARD_NAME to refer to both 1166 if (mode != HTML_MODE_NONE) {
1165 // the credit card holder's name and the name on the billing address.
1166 if (parse_target == PARSE_FOR_AUTOFILL_DIALOG &&
1167 field_type == NAME_FULL) {
1168 field_type = CREDIT_CARD_NAME;
1169 }
1170 }
1171
1172 section = "-" + tokens.back(); 1167 section = "-" + tokens.back();
1173 tokens.pop_back(); 1168 tokens.pop_back();
1174 } 1169 }
1175 1170
1176 // The preceding token, if any, may be a named section. 1171 // The preceding token, if any, may be a named section.
1177 const std::string kSectionPrefix = "section-"; 1172 const std::string kSectionPrefix = "section-";
1178 if (!tokens.empty() && 1173 if (!tokens.empty() &&
1179 StartsWithASCII(tokens.back(), kSectionPrefix, true)) { 1174 StartsWithASCII(tokens.back(), kSectionPrefix, true)) {
1180 // Prepend this section name to the suffix set in the preceding block. 1175 // Prepend this section name to the suffix set in the preceding block.
1181 section = tokens.back().substr(kSectionPrefix.size()) + section; 1176 section = tokens.back().substr(kSectionPrefix.size()) + section;
1182 tokens.pop_back(); 1177 tokens.pop_back();
1183 } 1178 }
1184 1179
1185 // No other tokens are allowed. If there are any remaining, abort. 1180 // No other tokens are allowed. If there are any remaining, abort.
1186 if (!tokens.empty()) 1181 if (!tokens.empty())
1187 continue; 1182 continue;
1188 1183
1189 if (section != kDefaultSection) { 1184 if (section != kDefaultSection) {
1190 *found_sections = true; 1185 *found_sections = true;
1191 field->set_section(section); 1186 field->set_section(section);
1192 } 1187 }
1193 1188
1194 // No errors encountered while parsing! 1189 // No errors encountered while parsing!
1195 // Update the |field|'s type based on what was parsed from the attribute. 1190 // Update the |field|'s type based on what was parsed from the attribute.
1196 field->set_heuristic_type(field_type); 1191 field->SetHtmlType(field_type, mode);
1197 if (field_type_token == "tel-local-prefix")
1198 field->set_phone_part(AutofillField::PHONE_PREFIX);
1199 else if (field_type_token == "tel-local-suffix")
1200 field->set_phone_part(AutofillField::PHONE_SUFFIX);
1201 } 1192 }
1202 } 1193 }
1203 1194
1204 void FormStructure::IdentifySections(bool has_author_specified_sections) { 1195 void FormStructure::IdentifySections(bool has_author_specified_sections) {
1205 if (fields_.empty()) 1196 if (fields_.empty())
1206 return; 1197 return;
1207 1198
1208 if (!has_author_specified_sections) { 1199 if (!has_author_specified_sections) {
1209 // Name sections after the first field in the section. 1200 // Name sections after the first field in the section.
1210 base::string16 current_section = fields_.front()->unique_name(); 1201 base::string16 current_section = fields_.front()->unique_name();
1211 1202
1212 // Keep track of the types we've seen in this section. 1203 // Keep track of the types we've seen in this section.
1213 std::set<ServerFieldType> seen_types; 1204 std::set<ServerFieldType> seen_types;
1214 ServerFieldType previous_type = UNKNOWN_TYPE; 1205 ServerFieldType previous_type = UNKNOWN_TYPE;
1215 1206
1216 for (std::vector<AutofillField*>::iterator field = fields_.begin(); 1207 for (std::vector<AutofillField*>::iterator field = fields_.begin();
1217 field != fields_.end(); ++field) { 1208 field != fields_.end(); ++field) {
1218 const ServerFieldType current_type = 1209 const ServerFieldType current_type = (*field)->Type().GetStorableType();
1219 AutofillType::GetEquivalentFieldType((*field)->Type().server_type());
1220 1210
1221 bool already_saw_current_type = seen_types.count(current_type) > 0; 1211 bool already_saw_current_type = seen_types.count(current_type) > 0;
1222 1212
1223 // Forms often ask for multiple phone numbers -- e.g. both a daytime and 1213 // Forms often ask for multiple phone numbers -- e.g. both a daytime and
1224 // evening phone number. Our phone number detection is also generally a 1214 // evening phone number. Our phone number detection is also generally a
1225 // little off. Hence, ignore this field type as a signal here. 1215 // little off. Hence, ignore this field type as a signal here.
1226 if (AutofillType(current_type).group() == PHONE_HOME) 1216 if (AutofillType(current_type).group() == PHONE_HOME)
1227 already_saw_current_type = false; 1217 already_saw_current_type = false;
1228 1218
1229 // Some forms have adjacent fields of the same type. Two common examples: 1219 // Some forms have adjacent fields of the same type. Two common examples:
(...skipping 27 matching lines...) Expand all
1257 field != fields_.end(); ++field) { 1247 field != fields_.end(); ++field) {
1258 FieldTypeGroup field_type_group = (*field)->Type().group(); 1248 FieldTypeGroup field_type_group = (*field)->Type().group();
1259 if (field_type_group == CREDIT_CARD) 1249 if (field_type_group == CREDIT_CARD)
1260 (*field)->set_section((*field)->section() + "-cc"); 1250 (*field)->set_section((*field)->section() + "-cc");
1261 else 1251 else
1262 (*field)->set_section((*field)->section() + "-default"); 1252 (*field)->set_section((*field)->section() + "-default");
1263 } 1253 }
1264 } 1254 }
1265 1255
1266 } // namespace autofill 1256 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/form_structure.h ('k') | components/autofill/core/browser/form_structure_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698