form_structure.cc revision 3240926e260ce088908e02ac07a6cf7b0c0cbf44
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 3// found in the LICENSE file. 4 5#include "components/autofill/core/browser/form_structure.h" 6 7#include <utility> 8 9#include "base/basictypes.h" 10#include "base/command_line.h" 11#include "base/logging.h" 12#include "base/memory/scoped_ptr.h" 13#include "base/sha1.h" 14#include "base/strings/string_number_conversions.h" 15#include "base/strings/string_util.h" 16#include "base/strings/stringprintf.h" 17#include "base/strings/utf_string_conversions.h" 18#include "base/time/time.h" 19#include "components/autofill/content/browser/autocheckout_page_meta_data.h" 20#include "components/autofill/core/browser/autofill_metrics.h" 21#include "components/autofill/core/browser/autofill_type.h" 22#include "components/autofill/core/browser/autofill_xml_parser.h" 23#include "components/autofill/core/browser/field_types.h" 24#include "components/autofill/core/browser/form_field.h" 25#include "components/autofill/core/common/autofill_constants.h" 26#include "components/autofill/core/common/form_data.h" 27#include "components/autofill/core/common/form_data_predictions.h" 28#include "components/autofill/core/common/form_field_data.h" 29#include "components/autofill/core/common/form_field_data_predictions.h" 30#include "third_party/icu/source/i18n/unicode/regex.h" 31#include "third_party/libjingle/source/talk/xmllite/xmlelement.h" 32 33namespace autofill { 34namespace { 35 36const char kFormMethodPost[] = "post"; 37 38// XML elements and attributes. 39const char kAttributeAcceptedFeatures[] = "accepts"; 40const char kAttributeAutofillUsed[] = "autofillused"; 41const char kAttributeAutofillType[] = "autofilltype"; 42const char kAttributeClientVersion[] = "clientversion"; 43const char kAttributeDataPresent[] = "datapresent"; 44const char kAttributeFieldID[] = "fieldid"; 45const char kAttributeFieldType[] = "fieldtype"; 46const char kAttributeFormSignature[] = "formsignature"; 47const char kAttributeName[] = "name"; 48const char kAttributeSignature[] = "signature"; 49const char kAttributeUrlprefixSignature[] = "urlprefixsignature"; 50const char kAcceptedFeaturesExperiment[] = "e"; // e=experiments 51const char kAcceptedFeaturesAutocheckoutExperiment[] = "a,e"; // a=autocheckout 52const char kClientVersion[] = "6.1.1715.1442/en (GGLL)"; 53const char kXMLDeclaration[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 54const char kXMLElementAutofillQuery[] = "autofillquery"; 55const char kXMLElementAutofillUpload[] = "autofillupload"; 56const char kXMLElementFieldAssignments[] = "fieldassignments"; 57const char kXMLElementField[] = "field"; 58const char kXMLElementFields[] = "fields"; 59const char kXMLElementForm[] = "form"; 60const char kBillingSection[] = "billing"; 61const char kShippingSection[] = "shipping"; 62 63// Stip away >= 5 consecutive digits. 64const char kIgnorePatternInFieldName[] = "\\d{5,}+"; 65 66// Helper for |EncodeUploadRequest()| that creates a bit field corresponding to 67// |available_field_types| and returns the hex representation as a string. 68std::string EncodeFieldTypes(const ServerFieldTypeSet& available_field_types) { 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. 71 const size_t kNumBytes = (MAX_VALID_FIELD_TYPE + 0x7) / 8; 72 73 // Pack the types in |available_field_types| into |bit_field|. 74 std::vector<uint8> bit_field(kNumBytes, 0); 75 for (ServerFieldTypeSet::const_iterator field_type = 76 available_field_types.begin(); 77 field_type != available_field_types.end(); 78 ++field_type) { 79 // Set the appropriate bit in the field. The bit we set is the one 80 // |field_type| % 8 from the left of the byte. 81 const size_t byte = *field_type / 8; 82 const size_t bit = 0x80 >> (*field_type % 8); 83 DCHECK(byte < bit_field.size()); 84 bit_field[byte] |= bit; 85 } 86 87 // Discard any trailing zeroes. 88 // If there are no available types, we return the empty string. 89 size_t data_end = bit_field.size(); 90 for (; data_end > 0 && !bit_field[data_end - 1]; --data_end) { 91 } 92 93 // Print all meaningfull bytes into a string. 94 std::string data_presence; 95 data_presence.reserve(data_end * 2 + 1); 96 for (size_t i = 0; i < data_end; ++i) { 97 base::StringAppendF(&data_presence, "%02x", bit_field[i]); 98 } 99 100 return data_presence; 101} 102 103// Helper for |EncodeFormRequest()| that creates XmlElements for the given field 104// in upload xml, and also add them to the parent XmlElement. 105void EncodeFieldForUpload(const AutofillField& field, 106 buzz::XmlElement* parent) { 107 // Don't upload checkable fields. 108 if (field.is_checkable) 109 return; 110 111 ServerFieldTypeSet types = field.possible_types(); 112 // |types| could be empty in unit-tests only. 113 for (ServerFieldTypeSet::iterator field_type = types.begin(); 114 field_type != types.end(); ++field_type) { 115 buzz::XmlElement *field_element = new buzz::XmlElement( 116 buzz::QName(kXMLElementField)); 117 118 field_element->SetAttr(buzz::QName(kAttributeSignature), 119 field.FieldSignature()); 120 field_element->SetAttr(buzz::QName(kAttributeAutofillType), 121 base::IntToString(*field_type)); 122 parent->AddElement(field_element); 123 } 124} 125 126// Helper for |EncodeFormRequest()| that creates XmlElement for the given field 127// in query xml, and also add it to the parent XmlElement. 128void EncodeFieldForQuery(const AutofillField& field, 129 buzz::XmlElement* parent) { 130 buzz::XmlElement *field_element = new buzz::XmlElement( 131 buzz::QName(kXMLElementField)); 132 field_element->SetAttr(buzz::QName(kAttributeSignature), 133 field.FieldSignature()); 134 parent->AddElement(field_element); 135} 136 137// Helper for |EncodeFormRequest()| that creates XmlElements for the given field 138// in field assignments xml, and also add them to the parent XmlElement. 139void EncodeFieldForFieldAssignments(const AutofillField& field, 140 buzz::XmlElement* parent) { 141 ServerFieldTypeSet types = field.possible_types(); 142 for (ServerFieldTypeSet::iterator field_type = types.begin(); 143 field_type != types.end(); ++field_type) { 144 buzz::XmlElement *field_element = new buzz::XmlElement( 145 buzz::QName(kXMLElementFields)); 146 147 field_element->SetAttr(buzz::QName(kAttributeFieldID), 148 field.FieldSignature()); 149 field_element->SetAttr(buzz::QName(kAttributeFieldType), 150 base::IntToString(*field_type)); 151 field_element->SetAttr(buzz::QName(kAttributeName), 152 UTF16ToUTF8(field.name)); 153 parent->AddElement(field_element); 154 } 155} 156 157// Returns |true| iff the |token| is a type hint for a contact field, as 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 160// support filling either type of information. 161bool IsContactTypeHint(const std::string& token) { 162 return token == "home" || token == "work" || token == "mobile"; 163} 164 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 167// http://is.gd/whatwg_autocomplete 168// TODO(isherman): This should use HTML field types, not native ones. 169bool ContactTypeHintMatchesFieldType(const std::string& token, 170 ServerFieldType field_type) { 171 // The "home" and "work" type hints are only appropriate for email and phone 172 // number field types. 173 if (token == "home" || token == "work") { 174 return field_type == EMAIL_ADDRESS || 175 (field_type >= PHONE_HOME_NUMBER && 176 field_type <= PHONE_HOME_WHOLE_NUMBER); 177 } 178 179 // 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 181 // support filling either type of information. 182 if (token == "mobile") { 183 return field_type >= PHONE_HOME_NUMBER && 184 field_type <= PHONE_HOME_WHOLE_NUMBER; 185 } 186 187 return false; 188} 189 190// 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|. 192// Chrome Autofill supports a subset of the field types listed at 193// http://is.gd/whatwg_autocomplete 194// TODO(isherman): This should use HTML field types, not native ones. 195ServerFieldType FieldTypeFromAutocompleteType( 196 const std::string& autocomplete_type, 197 const AutofillField& field) { 198 if (autocomplete_type == "name") 199 return NAME_FULL; 200 201 if (autocomplete_type == "given-name") 202 return NAME_FIRST; 203 204 if (autocomplete_type == "additional-name") { 205 if (field.max_length == 1) 206 return NAME_MIDDLE_INITIAL; 207 else 208 return NAME_MIDDLE; 209 } 210 211 if (autocomplete_type == "family-name") 212 return NAME_LAST; 213 214 if (autocomplete_type == "honorific-suffix") 215 return NAME_SUFFIX; 216 217 if (autocomplete_type == "organization") 218 return COMPANY_NAME; 219 220 if (autocomplete_type == "address-line1") 221 return ADDRESS_HOME_LINE1; 222 223 if (autocomplete_type == "address-line2") 224 return ADDRESS_HOME_LINE2; 225 226 if (autocomplete_type == "locality") 227 return ADDRESS_HOME_CITY; 228 229 if (autocomplete_type == "region") 230 return ADDRESS_HOME_STATE; 231 232 if (autocomplete_type == "country") 233 return ADDRESS_HOME_COUNTRY; 234 235 if (autocomplete_type == "postal-code") 236 return ADDRESS_HOME_ZIP; 237 238 if (autocomplete_type == "cc-name") 239 return CREDIT_CARD_NAME; 240 241 if (autocomplete_type == "cc-number") 242 return CREDIT_CARD_NUMBER; 243 244 if (autocomplete_type == "cc-exp") { 245 if (field.max_length == 5) 246 return CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR; 247 else 248 return CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR; 249 } 250 251 if (autocomplete_type == "cc-exp-month") 252 return CREDIT_CARD_EXP_MONTH; 253 254 if (autocomplete_type == "cc-exp-year") { 255 if (field.max_length == 2) 256 return CREDIT_CARD_EXP_2_DIGIT_YEAR; 257 else 258 return CREDIT_CARD_EXP_4_DIGIT_YEAR; 259 } 260 261 if (autocomplete_type == "cc-csc") 262 return CREDIT_CARD_VERIFICATION_CODE; 263 264 if (autocomplete_type == "cc-type") 265 return CREDIT_CARD_TYPE; 266 267 if (autocomplete_type == "tel") 268 return PHONE_HOME_WHOLE_NUMBER; 269 270 if (autocomplete_type == "tel-country-code") 271 return PHONE_HOME_COUNTRY_CODE; 272 273 if (autocomplete_type == "tel-national") 274 return PHONE_HOME_CITY_AND_NUMBER; 275 276 if (autocomplete_type == "tel-area-code") 277 return PHONE_HOME_CITY_CODE; 278 279 if (autocomplete_type == "tel-local") 280 return PHONE_HOME_NUMBER; 281 282 if (autocomplete_type == "tel-local-prefix") 283 return PHONE_HOME_NUMBER; 284 285 if (autocomplete_type == "tel-local-suffix") 286 return PHONE_HOME_NUMBER; 287 288 if (autocomplete_type == "email") 289 return EMAIL_ADDRESS; 290 291 return UNKNOWN_TYPE; 292} 293 294std::string StripDigitsIfRequired(const base::string16& input) { 295 UErrorCode status = U_ZERO_ERROR; 296 CR_DEFINE_STATIC_LOCAL(icu::UnicodeString, icu_pattern, 297 (kIgnorePatternInFieldName)); 298 CR_DEFINE_STATIC_LOCAL(icu::RegexMatcher, matcher, 299 (icu_pattern, UREGEX_CASE_INSENSITIVE, status)); 300 DCHECK_EQ(status, U_ZERO_ERROR); 301 302 icu::UnicodeString icu_input(input.data(), input.length()); 303 matcher.reset(icu_input); 304 305 icu::UnicodeString replaced_string = matcher.replaceAll("", status); 306 307 std::string return_string; 308 status = U_ZERO_ERROR; 309 UTF16ToUTF8(replaced_string.getBuffer(), 310 static_cast<size_t>(replaced_string.length()), 311 &return_string); 312 if (status != U_ZERO_ERROR) { 313 DVLOG(1) << "Couldn't strip digits in " << UTF16ToUTF8(input); 314 return UTF16ToUTF8(input); 315 } 316 317 return return_string; 318} 319 320} // namespace 321 322FormStructure::FormStructure(const FormData& form, 323 const std::string& autocheckout_url_prefix) 324 : form_name_(form.name), 325 source_url_(form.origin), 326 target_url_(form.action), 327 autofill_count_(0), 328 active_field_count_(0), 329 upload_required_(USE_UPLOAD_RATES), 330 server_experiment_id_("no server response"), 331 has_author_specified_types_(false), 332 autocheckout_url_prefix_(autocheckout_url_prefix), 333 filled_by_autocheckout_(false) { 334 // Copy the form fields. 335 std::map<base::string16, size_t> unique_names; 336 for (std::vector<FormFieldData>::const_iterator field = 337 form.fields.begin(); 338 field != form.fields.end(); field++) { 339 340 if (!ShouldSkipField(*field)) { 341 // Add all supported form fields (including with empty names) to the 342 // signature. This is a requirement for Autofill servers. 343 form_signature_field_names_.append("&"); 344 form_signature_field_names_.append(StripDigitsIfRequired(field->name)); 345 346 ++active_field_count_; 347 } 348 349 // Generate a unique name for this field by appending a counter to the name. 350 // Make sure to prepend the counter with a non-numeric digit so that we are 351 // guaranteed to avoid collisions. 352 if (!unique_names.count(field->name)) 353 unique_names[field->name] = 1; 354 else 355 ++unique_names[field->name]; 356 base::string16 unique_name = field->name + ASCIIToUTF16("_") + 357 base::IntToString16(unique_names[field->name]); 358 fields_.push_back(new AutofillField(*field, unique_name)); 359 } 360 361 std::string method = UTF16ToUTF8(form.method); 362 if (StringToLowerASCII(method) == kFormMethodPost) { 363 method_ = POST; 364 } else { 365 // Either the method is 'get', or we don't know. In this case we default 366 // to GET. 367 method_ = GET; 368 } 369} 370 371FormStructure::~FormStructure() {} 372 373void FormStructure::DetermineHeuristicTypes( 374 const AutofillMetrics& metric_logger) { 375 // 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 377 // autocomplete type hint, don't try to apply other heuristics to match fields 378 // in this form. 379 bool has_author_specified_sections; 380 ParseFieldTypesFromAutocompleteAttributes(PARSE_FOR_AUTOFILL, 381 &has_author_specified_types_, 382 &has_author_specified_sections); 383 384 if (!has_author_specified_types_) { 385 ServerFieldTypeMap field_type_map; 386 FormField::ParseFormFields(fields_.get(), &field_type_map); 387 for (size_t i = 0; i < field_count(); ++i) { 388 AutofillField* field = fields_[i]; 389 ServerFieldTypeMap::iterator iter = 390 field_type_map.find(field->unique_name()); 391 if (iter != field_type_map.end()) 392 field->set_heuristic_type(iter->second); 393 } 394 } 395 396 UpdateAutofillCount(); 397 IdentifySections(has_author_specified_sections); 398 399 if (IsAutofillable(true)) { 400 metric_logger.LogDeveloperEngagementMetric( 401 AutofillMetrics::FILLABLE_FORM_PARSED); 402 if (has_author_specified_types_) { 403 metric_logger.LogDeveloperEngagementMetric( 404 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS); 405 } 406 } 407} 408 409bool FormStructure::EncodeUploadRequest( 410 const ServerFieldTypeSet& available_field_types, 411 bool form_was_autofilled, 412 std::string* encoded_xml) const { 413 DCHECK(ShouldBeCrowdsourced()); 414 415 // Verify that |available_field_types| agrees with the possible field types we 416 // are uploading. 417 for (std::vector<AutofillField*>::const_iterator field = begin(); 418 field != end(); 419 ++field) { 420 for (ServerFieldTypeSet::const_iterator type = 421 (*field)->possible_types().begin(); 422 type != (*field)->possible_types().end(); 423 ++type) { 424 DCHECK(*type == UNKNOWN_TYPE || 425 *type == EMPTY_TYPE || 426 available_field_types.count(*type)); 427 } 428 } 429 430 // Set up the <autofillupload> element and its attributes. 431 buzz::XmlElement autofill_request_xml( 432 (buzz::QName(kXMLElementAutofillUpload))); 433 autofill_request_xml.SetAttr(buzz::QName(kAttributeClientVersion), 434 kClientVersion); 435 autofill_request_xml.SetAttr(buzz::QName(kAttributeFormSignature), 436 FormSignature()); 437 autofill_request_xml.SetAttr(buzz::QName(kAttributeAutofillUsed), 438 form_was_autofilled ? "true" : "false"); 439 autofill_request_xml.SetAttr(buzz::QName(kAttributeDataPresent), 440 EncodeFieldTypes(available_field_types).c_str()); 441 442 if (!EncodeFormRequest(FormStructure::UPLOAD, &autofill_request_xml)) 443 return false; // Malformed form, skip it. 444 445 // Obtain the XML structure as a string. 446 *encoded_xml = kXMLDeclaration; 447 *encoded_xml += autofill_request_xml.Str().c_str(); 448 449 // To enable this logging, run with the flag --vmodule="form_structure=2". 450 VLOG(2) << "\n" << *encoded_xml; 451 452 return true; 453} 454 455bool FormStructure::EncodeFieldAssignments( 456 const ServerFieldTypeSet& available_field_types, 457 std::string* encoded_xml) const { 458 DCHECK(ShouldBeCrowdsourced()); 459 460 // Set up the <fieldassignments> element and its attributes. 461 buzz::XmlElement autofill_request_xml( 462 (buzz::QName(kXMLElementFieldAssignments))); 463 autofill_request_xml.SetAttr(buzz::QName(kAttributeFormSignature), 464 FormSignature()); 465 466 if (!EncodeFormRequest(FormStructure::FIELD_ASSIGNMENTS, 467 &autofill_request_xml)) 468 return false; // Malformed form, skip it. 469 470 // Obtain the XML structure as a string. 471 *encoded_xml = kXMLDeclaration; 472 *encoded_xml += autofill_request_xml.Str().c_str(); 473 474 return true; 475} 476 477// static 478bool FormStructure::EncodeQueryRequest( 479 const std::vector<FormStructure*>& forms, 480 std::vector<std::string>* encoded_signatures, 481 std::string* encoded_xml) { 482 DCHECK(encoded_signatures); 483 DCHECK(encoded_xml); 484 encoded_xml->clear(); 485 encoded_signatures->clear(); 486 encoded_signatures->reserve(forms.size()); 487 488 // Set up the <autofillquery> element and attributes. 489 buzz::XmlElement autofill_request_xml( 490 (buzz::QName(kXMLElementAutofillQuery))); 491 autofill_request_xml.SetAttr(buzz::QName(kAttributeClientVersion), 492 kClientVersion); 493 494 // autocheckout_url_prefix tells the Autofill server where the forms in the 495 // request came from, and the the Autofill server checks internal status and 496 // decide to enable Autocheckout or not and may return Autocheckout related 497 // data in the response accordingly. 498 // There is no page/frame level object associated with FormStructure that 499 // we could extract URL prefix from. But, all the forms should come from the 500 // same frame, so they should have the same Autocheckout URL prefix. Thus we 501 // use URL prefix from the first form with Autocheckout enabled. 502 std::string autocheckout_url_prefix; 503 504 // Some badly formatted web sites repeat forms - detect that and encode only 505 // one form as returned data would be the same for all the repeated forms. 506 std::set<std::string> processed_forms; 507 for (ScopedVector<FormStructure>::const_iterator it = forms.begin(); 508 it != forms.end(); 509 ++it) { 510 std::string signature((*it)->FormSignature()); 511 if (processed_forms.find(signature) != processed_forms.end()) 512 continue; 513 processed_forms.insert(signature); 514 scoped_ptr<buzz::XmlElement> encompassing_xml_element( 515 new buzz::XmlElement(buzz::QName(kXMLElementForm))); 516 encompassing_xml_element->SetAttr(buzz::QName(kAttributeSignature), 517 signature); 518 519 if (!(*it)->EncodeFormRequest(FormStructure::QUERY, 520 encompassing_xml_element.get())) 521 continue; // Malformed form, skip it. 522 523 if ((*it)->IsAutocheckoutEnabled()) { 524 if (autocheckout_url_prefix.empty()) { 525 autocheckout_url_prefix = (*it)->autocheckout_url_prefix_; 526 } else { 527 // Making sure all the forms in the request has the same url_prefix. 528 DCHECK_EQ(autocheckout_url_prefix, (*it)->autocheckout_url_prefix_); 529 } 530 } 531 532 autofill_request_xml.AddElement(encompassing_xml_element.release()); 533 encoded_signatures->push_back(signature); 534 } 535 536 if (!encoded_signatures->size()) 537 return false; 538 539 if (autocheckout_url_prefix.empty()) { 540 autofill_request_xml.SetAttr(buzz::QName(kAttributeAcceptedFeatures), 541 kAcceptedFeaturesExperiment); 542 } else { 543 autofill_request_xml.SetAttr(buzz::QName(kAttributeAcceptedFeatures), 544 kAcceptedFeaturesAutocheckoutExperiment); 545 autofill_request_xml.SetAttr(buzz::QName(kAttributeUrlprefixSignature), 546 Hash64Bit(autocheckout_url_prefix)); 547 } 548 549 // Obtain the XML structure as a string. 550 *encoded_xml = kXMLDeclaration; 551 *encoded_xml += autofill_request_xml.Str().c_str(); 552 553 return true; 554} 555 556// static 557void FormStructure::ParseQueryResponse( 558 const std::string& response_xml, 559 const std::vector<FormStructure*>& forms, 560 autofill::AutocheckoutPageMetaData* page_meta_data, 561 const AutofillMetrics& metric_logger) { 562 metric_logger.LogServerQueryMetric(AutofillMetrics::QUERY_RESPONSE_RECEIVED); 563 564 // Parse the field types from the server response to the query. 565 std::vector<AutofillServerFieldInfo> field_infos; 566 UploadRequired upload_required; 567 std::string experiment_id; 568 AutofillQueryXmlParser parse_handler(&field_infos, 569 &upload_required, 570 &experiment_id, 571 page_meta_data); 572 buzz::XmlParser parser(&parse_handler); 573 parser.Parse(response_xml.c_str(), response_xml.length(), true); 574 if (!parse_handler.succeeded()) 575 return; 576 577 metric_logger.LogServerQueryMetric(AutofillMetrics::QUERY_RESPONSE_PARSED); 578 metric_logger.LogServerExperimentIdForQuery(experiment_id); 579 580 bool heuristics_detected_fillable_field = false; 581 bool query_response_overrode_heuristics = false; 582 583 // Copy the field types into the actual form. 584 std::vector<AutofillServerFieldInfo>::iterator current_info = 585 field_infos.begin(); 586 for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); 587 iter != forms.end(); ++iter) { 588 FormStructure* form = *iter; 589 form->upload_required_ = upload_required; 590 form->server_experiment_id_ = experiment_id; 591 592 for (std::vector<AutofillField*>::iterator field = form->fields_.begin(); 593 field != form->fields_.end(); ++field) { 594 if (form->ShouldSkipField(**field)) 595 continue; 596 597 // In some cases *successful* response does not return all the fields. 598 // Quit the update of the types then. 599 if (current_info == field_infos.end()) 600 break; 601 602 // UNKNOWN_TYPE is reserved for use by the client. 603 DCHECK_NE(current_info->field_type, UNKNOWN_TYPE); 604 605 ServerFieldType heuristic_type = (*field)->heuristic_type(); 606 if (heuristic_type != UNKNOWN_TYPE) 607 heuristics_detected_fillable_field = true; 608 609 (*field)->set_server_type(current_info->field_type); 610 if (heuristic_type != (*field)->Type().server_type()) 611 query_response_overrode_heuristics = true; 612 613 // Copy default value into the field if available. 614 if (!current_info->default_value.empty()) 615 (*field)->set_default_value(current_info->default_value); 616 617 ++current_info; 618 } 619 620 form->UpdateAutofillCount(); 621 form->IdentifySections(false); 622 } 623 624 AutofillMetrics::ServerQueryMetric metric; 625 if (query_response_overrode_heuristics) { 626 if (heuristics_detected_fillable_field) { 627 metric = AutofillMetrics::QUERY_RESPONSE_OVERRODE_LOCAL_HEURISTICS; 628 } else { 629 metric = AutofillMetrics::QUERY_RESPONSE_WITH_NO_LOCAL_HEURISTICS; 630 } 631 } else { 632 metric = AutofillMetrics::QUERY_RESPONSE_MATCHED_LOCAL_HEURISTICS; 633 } 634 metric_logger.LogServerQueryMetric(metric); 635} 636 637// static 638void FormStructure::GetFieldTypePredictions( 639 const std::vector<FormStructure*>& form_structures, 640 std::vector<FormDataPredictions>* forms) { 641 forms->clear(); 642 forms->reserve(form_structures.size()); 643 for (size_t i = 0; i < form_structures.size(); ++i) { 644 FormStructure* form_structure = form_structures[i]; 645 FormDataPredictions form; 646 form.data.name = form_structure->form_name_; 647 form.data.method = 648 ASCIIToUTF16((form_structure->method_ == POST) ? "POST" : "GET"); 649 form.data.origin = form_structure->source_url_; 650 form.data.action = form_structure->target_url_; 651 form.signature = form_structure->FormSignature(); 652 form.experiment_id = form_structure->server_experiment_id_; 653 654 for (std::vector<AutofillField*>::const_iterator field = 655 form_structure->fields_.begin(); 656 field != form_structure->fields_.end(); ++field) { 657 form.data.fields.push_back(FormFieldData(**field)); 658 659 FormFieldDataPredictions annotated_field; 660 annotated_field.signature = (*field)->FieldSignature(); 661 annotated_field.heuristic_type = 662 AutofillType::FieldTypeToString((*field)->heuristic_type()); 663 annotated_field.server_type = 664 AutofillType::FieldTypeToString((*field)->server_type()); 665 annotated_field.overall_type = 666 AutofillType::FieldTypeToString((*field)->Type().server_type()); 667 form.fields.push_back(annotated_field); 668 } 669 670 forms->push_back(form); 671 } 672} 673 674std::string FormStructure::FormSignature() const { 675 std::string scheme(target_url_.scheme()); 676 std::string host(target_url_.host()); 677 678 // If target host or scheme is empty, set scheme and host of source url. 679 // This is done to match the Toolbar's behavior. 680 if (scheme.empty() || host.empty()) { 681 scheme = source_url_.scheme(); 682 host = source_url_.host(); 683 } 684 685 std::string form_string = scheme + "://" + host + "&" + 686 UTF16ToUTF8(form_name_) + 687 form_signature_field_names_; 688 689 return Hash64Bit(form_string); 690} 691 692bool FormStructure::IsAutocheckoutEnabled() const { 693 return !autocheckout_url_prefix_.empty(); 694} 695 696bool FormStructure::ShouldSkipField(const FormFieldData& field) const { 697 return (field.is_checkable || field.form_control_type == "password") && 698 !IsAutocheckoutEnabled(); 699} 700 701size_t FormStructure::RequiredFillableFields() const { 702 return IsAutocheckoutEnabled() ? 0 : kRequiredAutofillFields; 703} 704 705bool FormStructure::IsAutofillable(bool require_method_post) const { 706 if (autofill_count() < RequiredFillableFields()) 707 return false; 708 709 return ShouldBeParsed(require_method_post); 710} 711 712void FormStructure::UpdateAutofillCount() { 713 autofill_count_ = 0; 714 for (std::vector<AutofillField*>::const_iterator iter = begin(); 715 iter != end(); ++iter) { 716 AutofillField* field = *iter; 717 if (field && field->IsFieldFillable()) 718 ++autofill_count_; 719 } 720} 721 722bool FormStructure::ShouldBeParsed(bool require_method_post) const { 723 if (active_field_count() < RequiredFillableFields()) 724 return false; 725 726 // Rule out http(s)://*/search?... 727 // e.g. http://www.google.com/search?q=... 728 // http://search.yahoo.com/search?p=... 729 if (target_url_.path() == "/search") 730 return false; 731 732 if (!IsAutocheckoutEnabled()) { 733 // Make sure there is at least one text field when Autocheckout is 734 // not enabled. 735 bool has_text_field = false; 736 for (std::vector<AutofillField*>::const_iterator it = begin(); 737 it != end() && !has_text_field; ++it) { 738 has_text_field |= (*it)->form_control_type != "select-one"; 739 } 740 if (!has_text_field) 741 return false; 742 } 743 744 return !require_method_post || (method_ == POST); 745} 746 747bool FormStructure::ShouldBeCrowdsourced() const { 748 // Allow all forms in Autocheckout flow to be crowdsourced. 749 return (!has_author_specified_types_ && ShouldBeParsed(true)) || 750 IsAutocheckoutEnabled(); 751} 752 753void FormStructure::UpdateFromCache(const FormStructure& cached_form) { 754 // Map from field signatures to cached fields. 755 std::map<std::string, const AutofillField*> cached_fields; 756 for (size_t i = 0; i < cached_form.field_count(); ++i) { 757 const AutofillField* field = cached_form.field(i); 758 cached_fields[field->FieldSignature()] = field; 759 } 760 761 for (std::vector<AutofillField*>::const_iterator iter = begin(); 762 iter != end(); ++iter) { 763 AutofillField* field = *iter; 764 765 std::map<std::string, const AutofillField*>::const_iterator 766 cached_field = cached_fields.find(field->FieldSignature()); 767 if (cached_field != cached_fields.end()) { 768 if (field->form_control_type != "select-one" && 769 field->value == cached_field->second->value) { 770 // From the perspective of learning user data, text fields containing 771 // default values are equivalent to empty fields. 772 field->value = base::string16(); 773 } 774 775 field->set_heuristic_type(cached_field->second->heuristic_type()); 776 field->set_server_type(cached_field->second->server_type()); 777 } 778 } 779 780 UpdateAutofillCount(); 781 782 filled_by_autocheckout_ = cached_form.filled_by_autocheckout(); 783 server_experiment_id_ = cached_form.server_experiment_id(); 784 785 // The form signature should match between query and upload requests to the 786 // server. On many websites, form elements are dynamically added, removed, or 787 // rearranged via JavaScript between page load and form submission, so we 788 // copy over the |form_signature_field_names_| corresponding to the query 789 // request. 790 DCHECK_EQ(cached_form.form_name_, form_name_); 791 DCHECK_EQ(cached_form.source_url_, source_url_); 792 DCHECK_EQ(cached_form.target_url_, target_url_); 793 form_signature_field_names_ = cached_form.form_signature_field_names_; 794} 795 796void FormStructure::LogQualityMetrics( 797 const AutofillMetrics& metric_logger, 798 const base::TimeTicks& load_time, 799 const base::TimeTicks& interaction_time, 800 const base::TimeTicks& submission_time) const { 801 std::string experiment_id = server_experiment_id(); 802 metric_logger.LogServerExperimentIdForUpload(experiment_id); 803 804 size_t num_detected_field_types = 0; 805 bool did_autofill_all_possible_fields = true; 806 bool did_autofill_some_possible_fields = false; 807 for (size_t i = 0; i < field_count(); ++i) { 808 const AutofillField* field = this->field(i); 809 metric_logger.LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED, 810 experiment_id); 811 812 // No further logging for empty fields nor for fields where the entered data 813 // does not appear to already exist in the user's stored Autofill data. 814 const ServerFieldTypeSet& field_types = field->possible_types(); 815 DCHECK(!field_types.empty()); 816 if (field_types.count(EMPTY_TYPE) || field_types.count(UNKNOWN_TYPE)) 817 continue; 818 819 ++num_detected_field_types; 820 if (field->is_autofilled) 821 did_autofill_some_possible_fields = true; 822 else 823 did_autofill_all_possible_fields = false; 824 825 // Collapse field types that Chrome treats as identical, e.g. home and 826 // billing address fields. 827 ServerFieldTypeSet collapsed_field_types; 828 for (ServerFieldTypeSet::const_iterator it = field_types.begin(); 829 it != field_types.end(); 830 ++it) { 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. 833 // TODO(isherman): Improve this logic once we add support for 834 // international numbers. 835 if (*it == PHONE_HOME_CITY_AND_NUMBER) 836 collapsed_field_types.insert(PHONE_HOME_WHOLE_NUMBER); 837 else 838 collapsed_field_types.insert(AutofillType::GetEquivalentFieldType(*it)); 839 } 840 841 // Capture the field's type, if it is unambiguous. 842 ServerFieldType field_type = UNKNOWN_TYPE; 843 if (collapsed_field_types.size() == 1) 844 field_type = *collapsed_field_types.begin(); 845 846 ServerFieldType heuristic_type = field->heuristic_type(); 847 ServerFieldType server_type = field->server_type(); 848 ServerFieldType predicted_type = field->Type().server_type(); 849 850 // Log heuristic, server, and overall type quality metrics, independently of 851 // whether the field was autofilled. 852 if (heuristic_type == UNKNOWN_TYPE) { 853 metric_logger.LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN, 854 field_type, experiment_id); 855 } else if (field_types.count(heuristic_type)) { 856 metric_logger.LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH, 857 field_type, experiment_id); 858 } else { 859 metric_logger.LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH, 860 field_type, experiment_id); 861 } 862 863 if (server_type == NO_SERVER_DATA) { 864 metric_logger.LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN, 865 field_type, experiment_id); 866 } else if (field_types.count(server_type)) { 867 metric_logger.LogServerTypePrediction(AutofillMetrics::TYPE_MATCH, 868 field_type, experiment_id); 869 } else { 870 metric_logger.LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH, 871 field_type, experiment_id); 872 } 873 874 if (predicted_type == UNKNOWN_TYPE) { 875 metric_logger.LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN, 876 field_type, experiment_id); 877 } else if (field_types.count(predicted_type)) { 878 metric_logger.LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH, 879 field_type, experiment_id); 880 } else { 881 metric_logger.LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH, 882 field_type, experiment_id); 883 } 884 885 // TODO(isherman): <select> fields don't support |is_autofilled()|, so we 886 // have to skip them for the remaining metrics. 887 if (field->form_control_type == "select-one") 888 continue; 889 890 if (field->is_autofilled) { 891 metric_logger.LogQualityMetric(AutofillMetrics::FIELD_AUTOFILLED, 892 experiment_id); 893 } else { 894 metric_logger.LogQualityMetric(AutofillMetrics::FIELD_NOT_AUTOFILLED, 895 experiment_id); 896 897 if (heuristic_type == UNKNOWN_TYPE) { 898 metric_logger.LogQualityMetric( 899 AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN, 900 experiment_id); 901 } else if (field_types.count(heuristic_type)) { 902 metric_logger.LogQualityMetric( 903 AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH, 904 experiment_id); 905 } else { 906 metric_logger.LogQualityMetric( 907 AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH, 908 experiment_id); 909 } 910 911 if (server_type == NO_SERVER_DATA) { 912 metric_logger.LogQualityMetric( 913 AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN, 914 experiment_id); 915 } else if (field_types.count(server_type)) { 916 metric_logger.LogQualityMetric( 917 AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH, 918 experiment_id); 919 } else { 920 metric_logger.LogQualityMetric( 921 AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH, 922 experiment_id); 923 } 924 } 925 } 926 927 if (num_detected_field_types < RequiredFillableFields()) { 928 metric_logger.LogUserHappinessMetric( 929 AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM); 930 } else { 931 if (did_autofill_all_possible_fields) { 932 metric_logger.LogUserHappinessMetric( 933 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL); 934 } else if (did_autofill_some_possible_fields) { 935 metric_logger.LogUserHappinessMetric( 936 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME); 937 } else { 938 metric_logger.LogUserHappinessMetric( 939 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE); 940 } 941 942 // Unlike the other times, the |submission_time| should always be available. 943 DCHECK(!submission_time.is_null()); 944 945 // The |load_time| might be unset, in the case that the form was dynamically 946 // added to the DOM. 947 if (!load_time.is_null()) { 948 // Submission should always chronologically follow form load. 949 DCHECK(submission_time > load_time); 950 base::TimeDelta elapsed = submission_time - load_time; 951 if (did_autofill_some_possible_fields) 952 metric_logger.LogFormFillDurationFromLoadWithAutofill(elapsed); 953 else 954 metric_logger.LogFormFillDurationFromLoadWithoutAutofill(elapsed); 955 } 956 957 // The |interaction_time| might be unset, in the case that the user 958 // submitted a blank form. 959 if (!interaction_time.is_null()) { 960 // Submission should always chronologically follow interaction. 961 DCHECK(submission_time > interaction_time); 962 base::TimeDelta elapsed = submission_time - interaction_time; 963 if (did_autofill_some_possible_fields) { 964 metric_logger.LogFormFillDurationFromInteractionWithAutofill(elapsed); 965 } else { 966 metric_logger.LogFormFillDurationFromInteractionWithoutAutofill( 967 elapsed); 968 } 969 } 970 } 971} 972 973const AutofillField* FormStructure::field(size_t index) const { 974 if (index >= fields_.size()) { 975 NOTREACHED(); 976 return NULL; 977 } 978 979 return fields_[index]; 980} 981 982AutofillField* FormStructure::field(size_t index) { 983 return const_cast<AutofillField*>( 984 static_cast<const FormStructure*>(this)->field(index)); 985} 986 987size_t FormStructure::field_count() const { 988 return fields_.size(); 989} 990 991size_t FormStructure::active_field_count() const { 992 return active_field_count_; 993} 994 995std::string FormStructure::server_experiment_id() const { 996 return server_experiment_id_; 997} 998 999FormData FormStructure::ToFormData() const { 1000 // |data.user_submitted| will always be false. 1001 FormData data; 1002 data.name = form_name_; 1003 data.origin = source_url_; 1004 data.action = target_url_; 1005 data.method = ASCIIToUTF16(method_ == POST ? "POST" : "GET"); 1006 1007 for (size_t i = 0; i < fields_.size(); ++i) { 1008 data.fields.push_back(FormFieldData(*fields_[i])); 1009 } 1010 1011 return data; 1012} 1013 1014bool FormStructure::operator==(const FormData& form) const { 1015 // TODO(jhawkins): Is this enough to differentiate a form? 1016 if (form_name_ == form.name && 1017 source_url_ == form.origin && 1018 target_url_ == form.action) { 1019 return true; 1020 } 1021 1022 // TODO(jhawkins): Compare field names, IDs and labels once we have labels 1023 // set up. 1024 1025 return false; 1026} 1027 1028bool FormStructure::operator!=(const FormData& form) const { 1029 return !operator==(form); 1030} 1031 1032std::string FormStructure::Hash64Bit(const std::string& str) { 1033 std::string hash_bin = base::SHA1HashString(str); 1034 DCHECK_EQ(20U, hash_bin.length()); 1035 1036 uint64 hash64 = (((static_cast<uint64>(hash_bin[0])) & 0xFF) << 56) | 1037 (((static_cast<uint64>(hash_bin[1])) & 0xFF) << 48) | 1038 (((static_cast<uint64>(hash_bin[2])) & 0xFF) << 40) | 1039 (((static_cast<uint64>(hash_bin[3])) & 0xFF) << 32) | 1040 (((static_cast<uint64>(hash_bin[4])) & 0xFF) << 24) | 1041 (((static_cast<uint64>(hash_bin[5])) & 0xFF) << 16) | 1042 (((static_cast<uint64>(hash_bin[6])) & 0xFF) << 8) | 1043 ((static_cast<uint64>(hash_bin[7])) & 0xFF); 1044 1045 return base::Uint64ToString(hash64); 1046} 1047 1048bool FormStructure::EncodeFormRequest( 1049 FormStructure::EncodeRequestType request_type, 1050 buzz::XmlElement* encompassing_xml_element) const { 1051 if (!field_count()) // Nothing to add. 1052 return false; 1053 1054 // Some badly formatted web sites repeat fields - limit number of fields to 1055 // 48, which is far larger than any valid form and XML still fits into 2K. 1056 // Do not send requests for forms with more than this many fields, as they are 1057 // near certainly not valid/auto-fillable. 1058 const size_t kMaxFieldsOnTheForm = 48; 1059 if (field_count() > kMaxFieldsOnTheForm) 1060 return false; 1061 1062 // Add the child nodes for the form fields. 1063 for (size_t index = 0; index < field_count(); ++index) { 1064 const AutofillField* field = fields_[index]; 1065 switch (request_type) { 1066 case FormStructure::UPLOAD: 1067 EncodeFieldForUpload(*field, encompassing_xml_element); 1068 break; 1069 case FormStructure::QUERY: 1070 if (ShouldSkipField(*field)) 1071 continue; 1072 EncodeFieldForQuery(*field, encompassing_xml_element); 1073 break; 1074 case FormStructure::FIELD_ASSIGNMENTS: 1075 EncodeFieldForFieldAssignments(*field, encompassing_xml_element); 1076 break; 1077 } 1078 } 1079 return true; 1080} 1081 1082void FormStructure::ParseFieldTypesFromAutocompleteAttributes( 1083 ParseTarget parse_target, 1084 bool* found_types, 1085 bool* found_sections) { 1086 const std::string kDefaultSection = "-default"; 1087 1088 *found_types = false; 1089 *found_sections = false; 1090 for (std::vector<AutofillField*>::iterator it = fields_.begin(); 1091 it != fields_.end(); ++it) { 1092 AutofillField* field = *it; 1093 1094 // To prevent potential section name collisions, add a default suffix for 1095 // other fields. Without this, 'autocomplete' attribute values 1096 // "section--shipping street-address" and "shipping street-address" would be 1097 // parsed identically, given the section handling code below. We do this 1098 // before any validation so that fields with invalid attributes still end up 1099 // in the default section. These default section names will be overridden 1100 // by subsequent heuristic parsing steps if there are no author-specified 1101 // section names. 1102 field->set_section(kDefaultSection); 1103 1104 // Canonicalize the attribute value by trimming whitespace, collapsing 1105 // non-space characters (e.g. tab) to spaces, and converting to lowercase. 1106 std::string autocomplete_attribute = 1107 CollapseWhitespaceASCII(field->autocomplete_attribute, false); 1108 autocomplete_attribute = StringToLowerASCII(autocomplete_attribute); 1109 1110 // The autocomplete attribute is overloaded: it can specify either a field 1111 // type hint or whether autocomplete should be enabled at all. Ignore the 1112 // latter type of attribute value. 1113 if (autocomplete_attribute.empty() || 1114 autocomplete_attribute == "on" || 1115 autocomplete_attribute == "off") { 1116 continue; 1117 } 1118 1119 // Any other value, even it is invalid, is considered to be a type hint. 1120 // This allows a website's author to specify an attribute like 1121 // autocomplete="other" on a field to disable all Autofill heuristics for 1122 // the form. 1123 *found_types = true; 1124 1125 // Tokenize the attribute value. Per the spec, the tokens are parsed in 1126 // reverse order. 1127 std::vector<std::string> tokens; 1128 Tokenize(autocomplete_attribute, " ", &tokens); 1129 1130 // The final token must be the field type. 1131 // If it is not one of the known types, abort. 1132 DCHECK(!tokens.empty()); 1133 std::string field_type_token = tokens.back(); 1134 tokens.pop_back(); 1135 ServerFieldType field_type = 1136 FieldTypeFromAutocompleteType(field_type_token, *field); 1137 if (field_type == UNKNOWN_TYPE) 1138 continue; 1139 1140 // The preceding token, if any, may be a type hint. 1141 if (!tokens.empty() && IsContactTypeHint(tokens.back())) { 1142 // If it is, it must match the field type; otherwise, abort. 1143 // Note that an invalid token invalidates the entire attribute value, even 1144 // if the other tokens are valid. 1145 if (!ContactTypeHintMatchesFieldType(tokens.back(), field_type)) 1146 continue; 1147 1148 // Chrome Autofill ignores these type hints. 1149 tokens.pop_back(); 1150 } 1151 1152 // The preceding token, if any, may be a fixed string that is either 1153 // "shipping" or "billing". Chrome Autofill treats these as implicit 1154 // section name suffixes. 1155 DCHECK_EQ(kDefaultSection, field->section()); 1156 std::string section = field->section(); 1157 if (!tokens.empty() && 1158 (tokens.back() == kShippingSection || 1159 tokens.back() == kBillingSection)) { 1160 // Set Autofill field type to billing if section is billing. 1161 if (tokens.back() == kBillingSection) { 1162 field_type = AutofillType::GetEquivalentBillingFieldType(field_type); 1163 1164 // The Autofill dialog uses the type CREDIT_CARD_NAME to refer to both 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(); 1173 tokens.pop_back(); 1174 } 1175 1176 // The preceding token, if any, may be a named section. 1177 const std::string kSectionPrefix = "section-"; 1178 if (!tokens.empty() && 1179 StartsWithASCII(tokens.back(), kSectionPrefix, true)) { 1180 // Prepend this section name to the suffix set in the preceding block. 1181 section = tokens.back().substr(kSectionPrefix.size()) + section; 1182 tokens.pop_back(); 1183 } 1184 1185 // No other tokens are allowed. If there are any remaining, abort. 1186 if (!tokens.empty()) 1187 continue; 1188 1189 if (section != kDefaultSection) { 1190 *found_sections = true; 1191 field->set_section(section); 1192 } 1193 1194 // No errors encountered while parsing! 1195 // Update the |field|'s type based on what was parsed from the attribute. 1196 field->set_heuristic_type(field_type); 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 } 1202} 1203 1204void FormStructure::IdentifySections(bool has_author_specified_sections) { 1205 if (fields_.empty()) 1206 return; 1207 1208 if (!has_author_specified_sections) { 1209 // Name sections after the first field in the section. 1210 base::string16 current_section = fields_.front()->unique_name(); 1211 1212 // Keep track of the types we've seen in this section. 1213 std::set<ServerFieldType> seen_types; 1214 ServerFieldType previous_type = UNKNOWN_TYPE; 1215 1216 for (std::vector<AutofillField*>::iterator field = fields_.begin(); 1217 field != fields_.end(); ++field) { 1218 const ServerFieldType current_type = 1219 AutofillType::GetEquivalentFieldType((*field)->Type().server_type()); 1220 1221 bool already_saw_current_type = seen_types.count(current_type) > 0; 1222 1223 // 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 1225 // little off. Hence, ignore this field type as a signal here. 1226 if (AutofillType(current_type).group() == PHONE_HOME) 1227 already_saw_current_type = false; 1228 1229 // Some forms have adjacent fields of the same type. Two common examples: 1230 // * Forms with two email fields, where the second is meant to "confirm" 1231 // the first. 1232 // * Forms with a <select> menu for states in some countries, and a 1233 // freeform <input> field for states in other countries. (Usually, 1234 // only one of these two will be visible for any given choice of 1235 // country.) 1236 // Generally, adjacent fields of the same type belong in the same logical 1237 // section. 1238 if (current_type == previous_type) 1239 already_saw_current_type = false; 1240 1241 previous_type = current_type; 1242 1243 if (current_type != UNKNOWN_TYPE && already_saw_current_type) { 1244 // We reached the end of a section, so start a new section. 1245 seen_types.clear(); 1246 current_section = (*field)->unique_name(); 1247 } 1248 1249 seen_types.insert(current_type); 1250 (*field)->set_section(UTF16ToUTF8(current_section)); 1251 } 1252 } 1253 1254 // Ensure that credit card and address fields are in separate sections. 1255 // This simplifies the section-aware logic in autofill_manager.cc. 1256 for (std::vector<AutofillField*>::iterator field = fields_.begin(); 1257 field != fields_.end(); ++field) { 1258 FieldTypeGroup field_type_group = (*field)->Type().group(); 1259 if (field_type_group == CREDIT_CARD) 1260 (*field)->set_section((*field)->section() + "-cc"); 1261 else 1262 (*field)->set_section((*field)->section() + "-default"); 1263 } 1264} 1265 1266} // namespace autofill 1267