Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / autofill / core / browser / form_structure.cc
blob8a555dcfd9fb5c54c3c3cb16b7b8384b913c19b4
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.
5 #include "components/autofill/core/browser/form_structure.h"
7 #include <utility>
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/i18n/case_conversion.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/sha1.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "components/autofill/core/browser/autofill_metrics.h"
23 #include "components/autofill/core/browser/autofill_type.h"
24 #include "components/autofill/core/browser/autofill_xml_parser.h"
25 #include "components/autofill/core/browser/field_types.h"
26 #include "components/autofill/core/browser/form_field.h"
27 #include "components/autofill/core/common/autofill_constants.h"
28 #include "components/autofill/core/common/form_data.h"
29 #include "components/autofill/core/common/form_data_predictions.h"
30 #include "components/autofill/core/common/form_field_data.h"
31 #include "components/autofill/core/common/form_field_data_predictions.h"
32 #include "components/rappor/rappor_service.h"
33 #include "components/rappor/rappor_utils.h"
34 #include "third_party/icu/source/i18n/unicode/regex.h"
35 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
37 namespace autofill {
38 namespace {
40 // XML elements and attributes.
41 const char kAttributeAutofillUsed[] = "autofillused";
42 const char kAttributeAutofillType[] = "autofilltype";
43 const char kAttributeClientVersion[] = "clientversion";
44 const char kAttributeDataPresent[] = "datapresent";
45 const char kAttributeFieldID[] = "fieldid";
46 const char kAttributeFieldType[] = "fieldtype";
47 const char kAttributeFieldLabel[] = "label";
48 const char kAttributeFormSignature[] = "formsignature";
49 const char kAttributeName[] = "name";
50 const char kAttributeSignature[] = "signature";
51 const char kAttributeControlType[] = "type";
52 const char kAttributeAutocomplete[] = "autocomplete";
53 const char kAttributeLoginFormSignature[] = "loginformsignature";
54 const char kClientVersion[] = "6.1.1715.1442/en (GGLL)";
55 const char kXMLDeclaration[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
56 const char kXMLElementAutofillQuery[] = "autofillquery";
57 const char kXMLElementAutofillUpload[] = "autofillupload";
58 const char kXMLElementFieldAssignments[] = "fieldassignments";
59 const char kXMLElementField[] = "field";
60 const char kXMLElementFields[] = "fields";
61 const char kXMLElementForm[] = "form";
62 const char kBillingMode[] = "billing";
63 const char kShippingMode[] = "shipping";
65 // Strip away >= 5 consecutive digits.
66 const char kIgnorePatternInFieldName[] = "\\d{5,}+";
68 // A form is considered to have a high prediction mismatch rate if the number of
69 // mismatches exceeds this threshold.
70 const int kNumberOfMismatchesThreshold = 3;
72 // Returns whether sending autofill field metadata to the server is enabled.
73 bool IsAutofillFieldMetadataEnabled() {
74 const std::string group_name =
75 base::FieldTrialList::FindFullName("AutofillFieldMetadata");
76 return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE);
79 // Helper for |EncodeUploadRequest()| that creates a bit field corresponding to
80 // |available_field_types| and returns the hex representation as a string.
81 std::string EncodeFieldTypes(const ServerFieldTypeSet& available_field_types) {
82 // There are |MAX_VALID_FIELD_TYPE| different field types and 8 bits per byte,
83 // so we need ceil(MAX_VALID_FIELD_TYPE / 8) bytes to encode the bit field.
84 const size_t kNumBytes = (MAX_VALID_FIELD_TYPE + 0x7) / 8;
86 // Pack the types in |available_field_types| into |bit_field|.
87 std::vector<uint8> bit_field(kNumBytes, 0);
88 for (ServerFieldTypeSet::const_iterator field_type =
89 available_field_types.begin();
90 field_type != available_field_types.end();
91 ++field_type) {
92 // Set the appropriate bit in the field. The bit we set is the one
93 // |field_type| % 8 from the left of the byte.
94 const size_t byte = *field_type / 8;
95 const size_t bit = 0x80 >> (*field_type % 8);
96 DCHECK(byte < bit_field.size());
97 bit_field[byte] |= bit;
100 // Discard any trailing zeroes.
101 // If there are no available types, we return the empty string.
102 size_t data_end = bit_field.size();
103 for (; data_end > 0 && !bit_field[data_end - 1]; --data_end) {
106 // Print all meaningfull bytes into a string.
107 std::string data_presence;
108 data_presence.reserve(data_end * 2 + 1);
109 for (size_t i = 0; i < data_end; ++i) {
110 base::StringAppendF(&data_presence, "%02x", bit_field[i]);
113 return data_presence;
116 // Helper for |EncodeFormRequest()| and |EncodeFieldForUpload| that returns an
117 // XmlElement for the given field in query xml, and also add it to the parent
118 // XmlElement.
119 buzz::XmlElement* EncodeFieldForQuery(const AutofillField& field,
120 buzz::XmlElement* parent) {
121 buzz::XmlElement* field_element = new buzz::XmlElement(
122 buzz::QName(kXMLElementField));
123 field_element->SetAttr(buzz::QName(kAttributeSignature),
124 field.FieldSignature());
125 if (IsAutofillFieldMetadataEnabled()) {
126 if (!field.name.empty()) {
127 field_element->SetAttr(buzz::QName(kAttributeName),
128 base::UTF16ToUTF8(field.name));
130 field_element->SetAttr(buzz::QName(kAttributeControlType),
131 field.form_control_type);
132 if (!field.label.empty()) {
133 field_element->SetAttr(buzz::QName(kAttributeFieldLabel),
134 base::UTF16ToUTF8(field.label));
137 parent->AddElement(field_element);
138 return field_element;
141 // Helper for |EncodeFormRequest()| that creates XmlElements for the given field
142 // in upload xml, and also add them to the parent XmlElement.
143 void EncodeFieldForUpload(const AutofillField& field,
144 buzz::XmlElement* parent) {
145 // Don't upload checkable fields.
146 if (field.is_checkable)
147 return;
149 ServerFieldTypeSet types = field.possible_types();
150 // |types| could be empty in unit-tests only.
151 for (ServerFieldTypeSet::iterator field_type = types.begin();
152 field_type != types.end(); ++field_type) {
153 // We use the same field elements as the query and add a few more below.
154 buzz::XmlElement* field_element = EncodeFieldForQuery(field, parent);
156 if (IsAutofillFieldMetadataEnabled() &&
157 !field.autocomplete_attribute.empty()) {
158 field_element->SetAttr(buzz::QName(kAttributeAutocomplete),
159 field.autocomplete_attribute);
162 field_element->SetAttr(buzz::QName(kAttributeAutofillType),
163 base::IntToString(*field_type));
167 // Helper for |EncodeFormRequest()| that creates XmlElements for the given field
168 // in field assignments xml, and also add them to the parent XmlElement.
169 void EncodeFieldForFieldAssignments(const AutofillField& field,
170 buzz::XmlElement* parent) {
171 ServerFieldTypeSet types = field.possible_types();
172 for (ServerFieldTypeSet::iterator field_type = types.begin();
173 field_type != types.end(); ++field_type) {
174 buzz::XmlElement *field_element = new buzz::XmlElement(
175 buzz::QName(kXMLElementFields));
177 field_element->SetAttr(buzz::QName(kAttributeFieldID),
178 field.FieldSignature());
179 field_element->SetAttr(buzz::QName(kAttributeFieldType),
180 base::IntToString(*field_type));
181 field_element->SetAttr(buzz::QName(kAttributeName),
182 base::UTF16ToUTF8(field.name));
183 parent->AddElement(field_element);
187 // Returns |true| iff the |token| is a type hint for a contact field, as
188 // specified in the implementation section of http://is.gd/whatwg_autocomplete
189 // Note that "fax" and "pager" are intentionally ignored, as Chrome does not
190 // support filling either type of information.
191 bool IsContactTypeHint(const std::string& token) {
192 return token == "home" || token == "work" || token == "mobile";
195 // Returns |true| iff the |token| is a type hint appropriate for a field of the
196 // given |field_type|, as specified in the implementation section of
197 // http://is.gd/whatwg_autocomplete
198 bool ContactTypeHintMatchesFieldType(const std::string& token,
199 HtmlFieldType field_type) {
200 // The "home" and "work" type hints are only appropriate for email and phone
201 // number field types.
202 if (token == "home" || token == "work") {
203 return field_type == HTML_TYPE_EMAIL ||
204 (field_type >= HTML_TYPE_TEL &&
205 field_type <= HTML_TYPE_TEL_LOCAL_SUFFIX);
208 // The "mobile" type hint is only appropriate for phone number field types.
209 // Note that "fax" and "pager" are intentionally ignored, as Chrome does not
210 // support filling either type of information.
211 if (token == "mobile") {
212 return field_type >= HTML_TYPE_TEL &&
213 field_type <= HTML_TYPE_TEL_LOCAL_SUFFIX;
216 return false;
219 // Returns the Chrome Autofill-supported field type corresponding to the given
220 // |autocomplete_attribute_value|, if there is one, in the context of the given
221 // |field|. Chrome Autofill supports a subset of the field types listed at
222 // http://is.gd/whatwg_autocomplete
223 HtmlFieldType FieldTypeFromAutocompleteAttributeValue(
224 const std::string& autocomplete_attribute_value,
225 const AutofillField& field) {
226 if (autocomplete_attribute_value == "name")
227 return HTML_TYPE_NAME;
229 if (autocomplete_attribute_value == "given-name")
230 return HTML_TYPE_GIVEN_NAME;
232 if (autocomplete_attribute_value == "additional-name") {
233 if (field.max_length == 1)
234 return HTML_TYPE_ADDITIONAL_NAME_INITIAL;
235 else
236 return HTML_TYPE_ADDITIONAL_NAME;
239 if (autocomplete_attribute_value == "family-name")
240 return HTML_TYPE_FAMILY_NAME;
242 if (autocomplete_attribute_value == "organization")
243 return HTML_TYPE_ORGANIZATION;
245 if (autocomplete_attribute_value == "street-address")
246 return HTML_TYPE_STREET_ADDRESS;
248 if (autocomplete_attribute_value == "address-line1")
249 return HTML_TYPE_ADDRESS_LINE1;
251 if (autocomplete_attribute_value == "address-line2")
252 return HTML_TYPE_ADDRESS_LINE2;
254 if (autocomplete_attribute_value == "address-line3")
255 return HTML_TYPE_ADDRESS_LINE3;
257 // TODO(estade): remove support for "locality" and "region".
258 if (autocomplete_attribute_value == "locality")
259 return HTML_TYPE_ADDRESS_LEVEL2;
261 if (autocomplete_attribute_value == "region")
262 return HTML_TYPE_ADDRESS_LEVEL1;
264 if (autocomplete_attribute_value == "address-level1")
265 return HTML_TYPE_ADDRESS_LEVEL1;
267 if (autocomplete_attribute_value == "address-level2")
268 return HTML_TYPE_ADDRESS_LEVEL2;
270 if (autocomplete_attribute_value == "address-level3")
271 return HTML_TYPE_ADDRESS_LEVEL3;
273 if (autocomplete_attribute_value == "country")
274 return HTML_TYPE_COUNTRY_CODE;
276 if (autocomplete_attribute_value == "country-name")
277 return HTML_TYPE_COUNTRY_NAME;
279 if (autocomplete_attribute_value == "postal-code")
280 return HTML_TYPE_POSTAL_CODE;
282 // content_switches.h isn't accessible from here, hence we have
283 // to copy the string literal. This should be removed soon anyway.
284 if (autocomplete_attribute_value == "address" &&
285 base::CommandLine::ForCurrentProcess()->HasSwitch(
286 "enable-experimental-web-platform-features")) {
287 return HTML_TYPE_FULL_ADDRESS;
290 if (autocomplete_attribute_value == "cc-name")
291 return HTML_TYPE_CREDIT_CARD_NAME;
293 if (autocomplete_attribute_value == "cc-number")
294 return HTML_TYPE_CREDIT_CARD_NUMBER;
296 if (autocomplete_attribute_value == "cc-exp") {
297 if (field.max_length == 5)
298 return HTML_TYPE_CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR;
299 else if (field.max_length == 7)
300 return HTML_TYPE_CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR;
301 else
302 return HTML_TYPE_CREDIT_CARD_EXP;
305 if (autocomplete_attribute_value == "cc-exp-month")
306 return HTML_TYPE_CREDIT_CARD_EXP_MONTH;
308 if (autocomplete_attribute_value == "cc-exp-year") {
309 if (field.max_length == 2)
310 return HTML_TYPE_CREDIT_CARD_EXP_2_DIGIT_YEAR;
311 else if (field.max_length == 4)
312 return HTML_TYPE_CREDIT_CARD_EXP_4_DIGIT_YEAR;
313 else
314 return HTML_TYPE_CREDIT_CARD_EXP_YEAR;
317 if (autocomplete_attribute_value == "cc-csc")
318 return HTML_TYPE_CREDIT_CARD_VERIFICATION_CODE;
320 if (autocomplete_attribute_value == "cc-type")
321 return HTML_TYPE_CREDIT_CARD_TYPE;
323 if (autocomplete_attribute_value == "transaction-amount")
324 return HTML_TYPE_TRANSACTION_AMOUNT;
326 if (autocomplete_attribute_value == "transaction-currency")
327 return HTML_TYPE_TRANSACTION_CURRENCY;
329 if (autocomplete_attribute_value == "tel")
330 return HTML_TYPE_TEL;
332 if (autocomplete_attribute_value == "tel-country-code")
333 return HTML_TYPE_TEL_COUNTRY_CODE;
335 if (autocomplete_attribute_value == "tel-national")
336 return HTML_TYPE_TEL_NATIONAL;
338 if (autocomplete_attribute_value == "tel-area-code")
339 return HTML_TYPE_TEL_AREA_CODE;
341 if (autocomplete_attribute_value == "tel-local")
342 return HTML_TYPE_TEL_LOCAL;
344 if (autocomplete_attribute_value == "tel-local-prefix")
345 return HTML_TYPE_TEL_LOCAL_PREFIX;
347 if (autocomplete_attribute_value == "tel-local-suffix")
348 return HTML_TYPE_TEL_LOCAL_SUFFIX;
350 if (autocomplete_attribute_value == "email")
351 return HTML_TYPE_EMAIL;
353 return HTML_TYPE_UNKNOWN;
356 std::string StripDigitsIfRequired(const base::string16& input) {
357 UErrorCode status = U_ZERO_ERROR;
358 CR_DEFINE_STATIC_LOCAL(icu::UnicodeString, icu_pattern,
359 (kIgnorePatternInFieldName));
360 CR_DEFINE_STATIC_LOCAL(icu::RegexMatcher, matcher,
361 (icu_pattern, UREGEX_CASE_INSENSITIVE, status));
362 DCHECK_EQ(status, U_ZERO_ERROR);
364 icu::UnicodeString icu_input(input.data(), input.length());
365 matcher.reset(icu_input);
367 icu::UnicodeString replaced_string = matcher.replaceAll("", status);
369 std::string return_string;
370 status = U_ZERO_ERROR;
371 base::UTF16ToUTF8(replaced_string.getBuffer(),
372 static_cast<size_t>(replaced_string.length()),
373 &return_string);
374 if (status != U_ZERO_ERROR) {
375 DVLOG(1) << "Couldn't strip digits in " << base::UTF16ToUTF8(input);
376 return base::UTF16ToUTF8(input);
379 return return_string;
382 } // namespace
384 FormStructure::FormStructure(const FormData& form)
385 : form_name_(form.name),
386 source_url_(form.origin),
387 target_url_(form.action),
388 autofill_count_(0),
389 active_field_count_(0),
390 upload_required_(USE_UPLOAD_RATES),
391 has_author_specified_types_(false),
392 has_password_field_(false),
393 is_form_tag_(form.is_form_tag) {
394 // Copy the form fields.
395 std::map<base::string16, size_t> unique_names;
396 for (const FormFieldData& field : form.fields) {
397 if (!ShouldSkipField(field)) {
398 // Add all supported form fields (including with empty names) to the
399 // signature. This is a requirement for Autofill servers.
400 form_signature_field_names_.append("&");
401 form_signature_field_names_.append(StripDigitsIfRequired(field.name));
403 ++active_field_count_;
406 if (field.form_control_type == "password")
407 has_password_field_ = true;
409 // Generate a unique name for this field by appending a counter to the name.
410 // Make sure to prepend the counter with a non-numeric digit so that we are
411 // guaranteed to avoid collisions.
412 base::string16 unique_name =
413 field.name + base::ASCIIToUTF16("_") +
414 base::IntToString16(++unique_names[field.name]);
415 fields_.push_back(new AutofillField(field, unique_name));
419 FormStructure::~FormStructure() {}
421 void FormStructure::DetermineHeuristicTypes() {
422 // First, try to detect field types based on each field's |autocomplete|
423 // attribute value. If there is at least one form field that specifies an
424 // autocomplete type hint, don't try to apply other heuristics to match fields
425 // in this form.
426 bool has_author_specified_sections;
427 ParseFieldTypesFromAutocompleteAttributes(&has_author_specified_types_,
428 &has_author_specified_sections);
430 if (!has_author_specified_types_) {
431 ServerFieldTypeMap field_type_map;
432 FormField::ParseFormFields(fields_.get(), is_form_tag_, &field_type_map);
433 for (size_t i = 0; i < field_count(); ++i) {
434 AutofillField* field = fields_[i];
435 ServerFieldTypeMap::iterator iter =
436 field_type_map.find(field->unique_name());
437 if (iter != field_type_map.end())
438 field->set_heuristic_type(iter->second);
442 UpdateAutofillCount();
443 IdentifySections(has_author_specified_sections);
445 if (IsAutofillable()) {
446 AutofillMetrics::LogDeveloperEngagementMetric(
447 AutofillMetrics::FILLABLE_FORM_PARSED);
448 if (has_author_specified_types_) {
449 AutofillMetrics::LogDeveloperEngagementMetric(
450 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS);
455 bool FormStructure::EncodeUploadRequest(
456 const ServerFieldTypeSet& available_field_types,
457 bool form_was_autofilled,
458 const std::string& login_form_signature,
459 std::string* encoded_xml) const {
460 DCHECK(ShouldBeCrowdsourced());
462 // Verify that |available_field_types| agrees with the possible field types we
463 // are uploading.
464 for (std::vector<AutofillField*>::const_iterator field = begin();
465 field != end();
466 ++field) {
467 for (ServerFieldTypeSet::const_iterator type =
468 (*field)->possible_types().begin();
469 type != (*field)->possible_types().end();
470 ++type) {
471 DCHECK(*type == UNKNOWN_TYPE ||
472 *type == EMPTY_TYPE ||
473 available_field_types.count(*type));
477 // Set up the <autofillupload> element and its attributes.
478 buzz::XmlElement autofill_request_xml(
479 (buzz::QName(kXMLElementAutofillUpload)));
480 autofill_request_xml.SetAttr(buzz::QName(kAttributeClientVersion),
481 kClientVersion);
482 autofill_request_xml.SetAttr(buzz::QName(kAttributeFormSignature),
483 FormSignature());
484 autofill_request_xml.SetAttr(buzz::QName(kAttributeAutofillUsed),
485 form_was_autofilled ? "true" : "false");
486 autofill_request_xml.SetAttr(buzz::QName(kAttributeDataPresent),
487 EncodeFieldTypes(available_field_types).c_str());
489 if (!login_form_signature.empty()) {
490 autofill_request_xml.SetAttr(buzz::QName(kAttributeLoginFormSignature),
491 login_form_signature);
494 if (!EncodeFormRequest(FormStructure::UPLOAD, &autofill_request_xml))
495 return false; // Malformed form, skip it.
497 // Obtain the XML structure as a string.
498 *encoded_xml = kXMLDeclaration;
499 *encoded_xml += autofill_request_xml.Str().c_str();
501 // To enable this logging, run with the flag --vmodule="form_structure=2".
502 VLOG(2) << "\n" << *encoded_xml;
504 return true;
507 bool FormStructure::EncodeFieldAssignments(
508 const ServerFieldTypeSet& available_field_types,
509 std::string* encoded_xml) const {
510 DCHECK(ShouldBeCrowdsourced());
512 // Set up the <fieldassignments> element and its attributes.
513 buzz::XmlElement autofill_request_xml(
514 (buzz::QName(kXMLElementFieldAssignments)));
515 autofill_request_xml.SetAttr(buzz::QName(kAttributeFormSignature),
516 FormSignature());
518 if (!EncodeFormRequest(FormStructure::FIELD_ASSIGNMENTS,
519 &autofill_request_xml))
520 return false; // Malformed form, skip it.
522 // Obtain the XML structure as a string.
523 *encoded_xml = kXMLDeclaration;
524 *encoded_xml += autofill_request_xml.Str().c_str();
526 return true;
529 // static
530 bool FormStructure::EncodeQueryRequest(
531 const std::vector<FormStructure*>& forms,
532 std::vector<std::string>* encoded_signatures,
533 std::string* encoded_xml) {
534 DCHECK(encoded_signatures);
535 DCHECK(encoded_xml);
536 encoded_xml->clear();
537 encoded_signatures->clear();
538 encoded_signatures->reserve(forms.size());
540 // Set up the <autofillquery> element and attributes.
541 buzz::XmlElement autofill_request_xml(
542 (buzz::QName(kXMLElementAutofillQuery)));
543 autofill_request_xml.SetAttr(buzz::QName(kAttributeClientVersion),
544 kClientVersion);
546 // Some badly formatted web sites repeat forms - detect that and encode only
547 // one form as returned data would be the same for all the repeated forms.
548 std::set<std::string> processed_forms;
549 for (ScopedVector<FormStructure>::const_iterator it = forms.begin();
550 it != forms.end();
551 ++it) {
552 std::string signature((*it)->FormSignature());
553 if (processed_forms.find(signature) != processed_forms.end())
554 continue;
555 processed_forms.insert(signature);
556 scoped_ptr<buzz::XmlElement> encompassing_xml_element(
557 new buzz::XmlElement(buzz::QName(kXMLElementForm)));
558 encompassing_xml_element->SetAttr(buzz::QName(kAttributeSignature),
559 signature);
561 if (!(*it)->EncodeFormRequest(FormStructure::QUERY,
562 encompassing_xml_element.get()))
563 continue; // Malformed form, skip it.
565 autofill_request_xml.AddElement(encompassing_xml_element.release());
566 encoded_signatures->push_back(signature);
569 if (!encoded_signatures->size())
570 return false;
572 // Note: Chrome used to also set 'accepts="e"' (where 'e' is for experiments),
573 // but no longer sets this because support for experiments is deprecated. If
574 // it ever resurfaces, re-add code here to set the attribute accordingly.
576 // Obtain the XML structure as a string.
577 *encoded_xml = kXMLDeclaration;
578 *encoded_xml += autofill_request_xml.Str().c_str();
580 return true;
583 // static
584 void FormStructure::ParseQueryResponse(const std::string& response_xml,
585 const std::vector<FormStructure*>& forms,
586 rappor::RapporService* rappor_service) {
587 AutofillMetrics::LogServerQueryMetric(
588 AutofillMetrics::QUERY_RESPONSE_RECEIVED);
590 // Parse the field types from the server response to the query.
591 std::vector<AutofillServerFieldInfo> field_infos;
592 UploadRequired upload_required;
593 AutofillQueryXmlParser parse_handler(&field_infos,
594 &upload_required);
595 buzz::XmlParser parser(&parse_handler);
596 parser.Parse(response_xml.c_str(), response_xml.length(), true);
597 if (!parse_handler.succeeded())
598 return;
600 AutofillMetrics::LogServerQueryMetric(AutofillMetrics::QUERY_RESPONSE_PARSED);
602 bool heuristics_detected_fillable_field = false;
603 bool query_response_overrode_heuristics = false;
605 // Copy the field types into the actual form.
606 std::vector<AutofillServerFieldInfo>::iterator current_info =
607 field_infos.begin();
608 for (std::vector<FormStructure*>::const_iterator iter = forms.begin();
609 iter != forms.end(); ++iter) {
610 FormStructure* form = *iter;
611 form->upload_required_ = upload_required;
613 bool query_response_has_no_server_data = true;
614 for (std::vector<AutofillField*>::iterator field = form->fields_.begin();
615 field != form->fields_.end(); ++field) {
616 if (form->ShouldSkipField(**field))
617 continue;
619 // In some cases *successful* response does not return all the fields.
620 // Quit the update of the types then.
621 if (current_info == field_infos.end())
622 break;
624 query_response_has_no_server_data &=
625 current_info->field_type == NO_SERVER_DATA;
627 // If |form->has_author_specified_types| only password fields should be
628 // updated.
629 if (!form->has_author_specified_types_ ||
630 (*field)->form_control_type == "password") {
631 // UNKNOWN_TYPE is reserved for use by the client.
632 DCHECK_NE(current_info->field_type, UNKNOWN_TYPE);
634 ServerFieldType heuristic_type = (*field)->heuristic_type();
635 if (heuristic_type != UNKNOWN_TYPE)
636 heuristics_detected_fillable_field = true;
638 (*field)->set_server_type(current_info->field_type);
639 if (heuristic_type != (*field)->Type().GetStorableType())
640 query_response_overrode_heuristics = true;
642 // Copy default value into the field if available.
643 if (!current_info->default_value.empty())
644 (*field)->set_default_value(current_info->default_value);
647 ++current_info;
650 AutofillMetrics::LogServerResponseHasDataForForm(
651 !query_response_has_no_server_data);
652 if (query_response_has_no_server_data && form->source_url().is_valid()) {
653 rappor::SampleDomainAndRegistryFromGURL(
654 rappor_service, "Autofill.QueryResponseHasNoServerDataForForm",
655 form->source_url());
658 form->UpdateAutofillCount();
659 form->IdentifySections(false);
662 AutofillMetrics::ServerQueryMetric metric;
663 if (query_response_overrode_heuristics) {
664 if (heuristics_detected_fillable_field) {
665 metric = AutofillMetrics::QUERY_RESPONSE_OVERRODE_LOCAL_HEURISTICS;
666 } else {
667 metric = AutofillMetrics::QUERY_RESPONSE_WITH_NO_LOCAL_HEURISTICS;
669 } else {
670 metric = AutofillMetrics::QUERY_RESPONSE_MATCHED_LOCAL_HEURISTICS;
672 AutofillMetrics::LogServerQueryMetric(metric);
675 // static
676 std::vector<FormDataPredictions> FormStructure::GetFieldTypePredictions(
677 const std::vector<FormStructure*>& form_structures) {
678 std::vector<FormDataPredictions> forms;
679 forms.reserve(form_structures.size());
680 for (size_t i = 0; i < form_structures.size(); ++i) {
681 FormStructure* form_structure = form_structures[i];
682 FormDataPredictions form;
683 form.data.name = form_structure->form_name_;
684 form.data.origin = form_structure->source_url_;
685 form.data.action = form_structure->target_url_;
686 form.data.is_form_tag = form_structure->is_form_tag_;
687 form.signature = form_structure->FormSignature();
689 for (std::vector<AutofillField*>::const_iterator field =
690 form_structure->fields_.begin();
691 field != form_structure->fields_.end(); ++field) {
692 form.data.fields.push_back(FormFieldData(**field));
694 FormFieldDataPredictions annotated_field;
695 annotated_field.signature = (*field)->FieldSignature();
696 annotated_field.heuristic_type =
697 AutofillType((*field)->heuristic_type()).ToString();
698 annotated_field.server_type =
699 AutofillType((*field)->server_type()).ToString();
700 annotated_field.overall_type = (*field)->Type().ToString();
701 form.fields.push_back(annotated_field);
704 forms.push_back(form);
706 return forms;
709 std::string FormStructure::FormSignature() const {
710 std::string scheme(target_url_.scheme());
711 std::string host(target_url_.host());
713 // If target host or scheme is empty, set scheme and host of source url.
714 // This is done to match the Toolbar's behavior.
715 if (scheme.empty() || host.empty()) {
716 scheme = source_url_.scheme();
717 host = source_url_.host();
720 std::string form_string = scheme + "://" + host + "&" +
721 base::UTF16ToUTF8(form_name_) +
722 form_signature_field_names_;
724 return Hash64Bit(form_string);
727 bool FormStructure::ShouldSkipField(const FormFieldData& field) const {
728 return field.is_checkable;
731 bool FormStructure::IsAutofillable() const {
732 if (autofill_count() < kRequiredAutofillFields)
733 return false;
735 return ShouldBeParsed();
738 void FormStructure::UpdateAutofillCount() {
739 autofill_count_ = 0;
740 for (std::vector<AutofillField*>::const_iterator iter = begin();
741 iter != end(); ++iter) {
742 AutofillField* field = *iter;
743 if (field && field->IsFieldFillable())
744 ++autofill_count_;
748 bool FormStructure::ShouldBeParsed() const {
749 if (active_field_count() < kRequiredAutofillFields)
750 return false;
752 // Rule out http(s)://*/search?...
753 // e.g. http://www.google.com/search?q=...
754 // http://search.yahoo.com/search?p=...
755 if (target_url_.path() == "/search")
756 return false;
758 bool has_text_field = false;
759 for (std::vector<AutofillField*>::const_iterator it = begin();
760 it != end() && !has_text_field; ++it) {
761 has_text_field |= (*it)->form_control_type != "select-one";
764 return has_text_field;
767 bool FormStructure::ShouldBeCrowdsourced() const {
768 return (has_password_field_ || !has_author_specified_types_) &&
769 ShouldBeParsed();
772 void FormStructure::UpdateFromCache(const FormStructure& cached_form) {
773 // Map from field signatures to cached fields.
774 std::map<std::string, const AutofillField*> cached_fields;
775 for (size_t i = 0; i < cached_form.field_count(); ++i) {
776 const AutofillField* field = cached_form.field(i);
777 cached_fields[field->FieldSignature()] = field;
780 for (std::vector<AutofillField*>::const_iterator iter = begin();
781 iter != end(); ++iter) {
782 AutofillField* field = *iter;
784 std::map<std::string, const AutofillField*>::const_iterator
785 cached_field = cached_fields.find(field->FieldSignature());
786 if (cached_field != cached_fields.end()) {
787 if (field->form_control_type != "select-one" &&
788 field->value == cached_field->second->value) {
789 // From the perspective of learning user data, text fields containing
790 // default values are equivalent to empty fields.
791 field->value = base::string16();
794 // Transfer attributes of the cached AutofillField to the newly created
795 // AutofillField.
796 field->set_heuristic_type(cached_field->second->heuristic_type());
797 field->set_server_type(cached_field->second->server_type());
798 field->SetHtmlType(cached_field->second->html_type(),
799 cached_field->second->html_mode());
800 field->set_previously_autofilled(
801 cached_field->second->previously_autofilled());
805 UpdateAutofillCount();
807 // The form signature should match between query and upload requests to the
808 // server. On many websites, form elements are dynamically added, removed, or
809 // rearranged via JavaScript between page load and form submission, so we
810 // copy over the |form_signature_field_names_| corresponding to the query
811 // request.
812 DCHECK_EQ(cached_form.form_name_, form_name_);
813 DCHECK_EQ(cached_form.source_url_, source_url_);
814 DCHECK_EQ(cached_form.target_url_, target_url_);
815 form_signature_field_names_ = cached_form.form_signature_field_names_;
818 void FormStructure::LogQualityMetrics(
819 const base::TimeTicks& load_time,
820 const base::TimeTicks& interaction_time,
821 const base::TimeTicks& submission_time,
822 rappor::RapporService* rappor_service) const {
823 size_t num_detected_field_types = 0;
824 size_t num_server_mismatches = 0;
825 size_t num_heuristic_mismatches = 0;
826 size_t num_edited_autofilled_fields = 0;
827 bool did_autofill_all_possible_fields = true;
828 bool did_autofill_some_possible_fields = false;
829 for (size_t i = 0; i < field_count(); ++i) {
830 const AutofillField* field = this->field(i);
832 // No further logging for password fields. Those are primarily related to a
833 // different feature code path, and so make more sense to track outside of
834 // this metric.
835 if (field->form_control_type == "password")
836 continue;
838 // We count fields that were autofilled but later modified, regardless of
839 // whether the data now in the field is recognized.
840 if (field->previously_autofilled())
841 num_edited_autofilled_fields++;
843 // No further logging for empty fields nor for fields where the entered data
844 // does not appear to already exist in the user's stored Autofill data.
845 const ServerFieldTypeSet& field_types = field->possible_types();
846 DCHECK(!field_types.empty());
847 if (field_types.count(EMPTY_TYPE) || field_types.count(UNKNOWN_TYPE))
848 continue;
850 ++num_detected_field_types;
851 if (field->is_autofilled)
852 did_autofill_some_possible_fields = true;
853 else
854 did_autofill_all_possible_fields = false;
856 // Collapse field types that Chrome treats as identical, e.g. home and
857 // billing address fields.
858 ServerFieldTypeSet collapsed_field_types;
859 for (ServerFieldTypeSet::const_iterator it = field_types.begin();
860 it != field_types.end();
861 ++it) {
862 // Since we currently only support US phone numbers, the (city code + main
863 // digits) number is almost always identical to the whole phone number.
864 // TODO(isherman): Improve this logic once we add support for
865 // international numbers.
866 if (*it == PHONE_HOME_CITY_AND_NUMBER)
867 collapsed_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
868 else
869 collapsed_field_types.insert(AutofillType(*it).GetStorableType());
872 // Capture the field's type, if it is unambiguous.
873 ServerFieldType field_type = UNKNOWN_TYPE;
874 if (collapsed_field_types.size() == 1)
875 field_type = *collapsed_field_types.begin();
877 ServerFieldType heuristic_type =
878 AutofillType(field->heuristic_type()).GetStorableType();
879 ServerFieldType server_type =
880 AutofillType(field->server_type()).GetStorableType();
881 ServerFieldType predicted_type = field->Type().GetStorableType();
883 // Log heuristic, server, and overall type quality metrics, independently of
884 // whether the field was autofilled.
885 if (heuristic_type == UNKNOWN_TYPE) {
886 AutofillMetrics::LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
887 field_type);
888 } else if (field_types.count(heuristic_type)) {
889 AutofillMetrics::LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
890 field_type);
891 } else {
892 ++num_heuristic_mismatches;
893 AutofillMetrics::LogHeuristicTypePrediction(
894 AutofillMetrics::TYPE_MISMATCH, field_type);
897 if (server_type == NO_SERVER_DATA) {
898 AutofillMetrics::LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
899 field_type);
900 } else if (field_types.count(server_type)) {
901 AutofillMetrics::LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
902 field_type);
903 } else {
904 ++num_server_mismatches;
905 AutofillMetrics::LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
906 field_type);
909 if (predicted_type == UNKNOWN_TYPE) {
910 AutofillMetrics::LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
911 field_type);
912 } else if (field_types.count(predicted_type)) {
913 AutofillMetrics::LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
914 field_type);
915 } else {
916 AutofillMetrics::LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
917 field_type);
921 AutofillMetrics::LogNumberOfEditedAutofilledFieldsAtSubmission(
922 num_edited_autofilled_fields);
924 if (num_detected_field_types < kRequiredAutofillFields) {
925 AutofillMetrics::LogUserHappinessMetric(
926 AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM);
927 } else {
928 if (did_autofill_all_possible_fields) {
929 AutofillMetrics::LogUserHappinessMetric(
930 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL);
931 } else if (did_autofill_some_possible_fields) {
932 AutofillMetrics::LogUserHappinessMetric(
933 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME);
934 } else {
935 AutofillMetrics::LogUserHappinessMetric(
936 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE);
939 // Log some RAPPOR metrics for problematic cases.
940 if (num_server_mismatches >= kNumberOfMismatchesThreshold) {
941 rappor::SampleDomainAndRegistryFromGURL(
942 rappor_service, "Autofill.HighNumberOfServerMismatches", source_url_);
944 if (num_heuristic_mismatches >= kNumberOfMismatchesThreshold) {
945 rappor::SampleDomainAndRegistryFromGURL(
946 rappor_service, "Autofill.HighNumberOfHeuristicMismatches",
947 source_url_);
950 // Unlike the other times, the |submission_time| should always be available.
951 DCHECK(!submission_time.is_null());
953 // The |load_time| might be unset, in the case that the form was dynamically
954 // added to the DOM.
955 if (!load_time.is_null()) {
956 // Submission should always chronologically follow form load.
957 DCHECK(submission_time > load_time);
958 base::TimeDelta elapsed = submission_time - load_time;
959 if (did_autofill_some_possible_fields)
960 AutofillMetrics::LogFormFillDurationFromLoadWithAutofill(elapsed);
961 else
962 AutofillMetrics::LogFormFillDurationFromLoadWithoutAutofill(elapsed);
965 // The |interaction_time| might be unset, in the case that the user
966 // submitted a blank form.
967 if (!interaction_time.is_null()) {
968 // Submission should always chronologically follow interaction.
969 DCHECK(submission_time > interaction_time);
970 base::TimeDelta elapsed = submission_time - interaction_time;
971 if (did_autofill_some_possible_fields) {
972 AutofillMetrics::LogFormFillDurationFromInteractionWithAutofill(
973 elapsed);
974 } else {
975 AutofillMetrics::LogFormFillDurationFromInteractionWithoutAutofill(
976 elapsed);
982 const AutofillField* FormStructure::field(size_t index) const {
983 if (index >= fields_.size()) {
984 NOTREACHED();
985 return NULL;
988 return fields_[index];
991 AutofillField* FormStructure::field(size_t index) {
992 return const_cast<AutofillField*>(
993 static_cast<const FormStructure*>(this)->field(index));
996 size_t FormStructure::field_count() const {
997 return fields_.size();
1000 size_t FormStructure::active_field_count() const {
1001 return active_field_count_;
1004 FormData FormStructure::ToFormData() const {
1005 FormData data;
1006 data.name = form_name_;
1007 data.origin = source_url_;
1008 data.action = target_url_;
1010 for (size_t i = 0; i < fields_.size(); ++i) {
1011 data.fields.push_back(FormFieldData(*fields_[i]));
1014 return data;
1017 bool FormStructure::operator==(const FormData& form) const {
1018 // TODO(jhawkins): Is this enough to differentiate a form?
1019 if (form_name_ == form.name &&
1020 source_url_ == form.origin &&
1021 target_url_ == form.action) {
1022 return true;
1025 // TODO(jhawkins): Compare field names, IDs and labels once we have labels
1026 // set up.
1028 return false;
1031 bool FormStructure::operator!=(const FormData& form) const {
1032 return !operator==(form);
1035 std::string FormStructure::Hash64Bit(const std::string& str) {
1036 std::string hash_bin = base::SHA1HashString(str);
1037 DCHECK_EQ(base::kSHA1Length, hash_bin.length());
1039 uint64 hash64 = (((static_cast<uint64>(hash_bin[0])) & 0xFF) << 56) |
1040 (((static_cast<uint64>(hash_bin[1])) & 0xFF) << 48) |
1041 (((static_cast<uint64>(hash_bin[2])) & 0xFF) << 40) |
1042 (((static_cast<uint64>(hash_bin[3])) & 0xFF) << 32) |
1043 (((static_cast<uint64>(hash_bin[4])) & 0xFF) << 24) |
1044 (((static_cast<uint64>(hash_bin[5])) & 0xFF) << 16) |
1045 (((static_cast<uint64>(hash_bin[6])) & 0xFF) << 8) |
1046 ((static_cast<uint64>(hash_bin[7])) & 0xFF);
1048 return base::Uint64ToString(hash64);
1051 bool FormStructure::EncodeFormRequest(
1052 FormStructure::EncodeRequestType request_type,
1053 buzz::XmlElement* encompassing_xml_element) const {
1054 if (!field_count()) // Nothing to add.
1055 return false;
1057 // Some badly formatted web sites repeat fields - limit number of fields to
1058 // 48, which is far larger than any valid form and XML still fits into 2K.
1059 // Do not send requests for forms with more than this many fields, as they are
1060 // near certainly not valid/auto-fillable.
1061 const size_t kMaxFieldsOnTheForm = 48;
1062 if (field_count() > kMaxFieldsOnTheForm)
1063 return false;
1065 // Add the child nodes for the form fields.
1066 for (size_t index = 0; index < field_count(); ++index) {
1067 const AutofillField* field = fields_[index];
1068 switch (request_type) {
1069 case FormStructure::UPLOAD:
1070 EncodeFieldForUpload(*field, encompassing_xml_element);
1071 break;
1072 case FormStructure::QUERY:
1073 if (ShouldSkipField(*field))
1074 continue;
1075 EncodeFieldForQuery(*field, encompassing_xml_element);
1076 break;
1077 case FormStructure::FIELD_ASSIGNMENTS:
1078 EncodeFieldForFieldAssignments(*field, encompassing_xml_element);
1079 break;
1082 return true;
1085 void FormStructure::ParseFieldTypesFromAutocompleteAttributes(
1086 bool* found_types,
1087 bool* found_sections) {
1088 const std::string kDefaultSection = "-default";
1090 *found_types = false;
1091 *found_sections = false;
1092 for (std::vector<AutofillField*>::iterator it = fields_.begin();
1093 it != fields_.end(); ++it) {
1094 AutofillField* field = *it;
1096 // To prevent potential section name collisions, add a default suffix for
1097 // other fields. Without this, 'autocomplete' attribute values
1098 // "section--shipping street-address" and "shipping street-address" would be
1099 // parsed identically, given the section handling code below. We do this
1100 // before any validation so that fields with invalid attributes still end up
1101 // in the default section. These default section names will be overridden
1102 // by subsequent heuristic parsing steps if there are no author-specified
1103 // section names.
1104 field->set_section(kDefaultSection);
1106 // Canonicalize the attribute value by trimming whitespace, collapsing
1107 // non-space characters (e.g. tab) to spaces, and converting to lowercase.
1108 std::string autocomplete_attribute =
1109 base::CollapseWhitespaceASCII(field->autocomplete_attribute, false);
1110 autocomplete_attribute = base::ToLowerASCII(autocomplete_attribute);
1112 // The autocomplete attribute is overloaded: it can specify either a field
1113 // type hint or whether autocomplete should be enabled at all. Ignore the
1114 // latter type of attribute value.
1115 if (autocomplete_attribute.empty() ||
1116 autocomplete_attribute == "on" ||
1117 autocomplete_attribute == "off") {
1118 continue;
1121 // Any other value, even it is invalid, is considered to be a type hint.
1122 // This allows a website's author to specify an attribute like
1123 // autocomplete="other" on a field to disable all Autofill heuristics for
1124 // the form.
1125 *found_types = true;
1127 // Tokenize the attribute value. Per the spec, the tokens are parsed in
1128 // reverse order.
1129 std::vector<std::string> tokens = base::SplitString(
1130 autocomplete_attribute, " ", base::KEEP_WHITESPACE,
1131 base::SPLIT_WANT_NONEMPTY);
1133 // The final token must be the field type.
1134 // If it is not one of the known types, abort.
1135 DCHECK(!tokens.empty());
1136 std::string field_type_token = tokens.back();
1137 tokens.pop_back();
1138 HtmlFieldType field_type =
1139 FieldTypeFromAutocompleteAttributeValue(field_type_token, *field);
1140 if (field_type == HTML_TYPE_UNKNOWN)
1141 continue;
1143 // The preceding token, if any, may be a type hint.
1144 if (!tokens.empty() && IsContactTypeHint(tokens.back())) {
1145 // If it is, it must match the field type; otherwise, abort.
1146 // Note that an invalid token invalidates the entire attribute value, even
1147 // if the other tokens are valid.
1148 if (!ContactTypeHintMatchesFieldType(tokens.back(), field_type))
1149 continue;
1151 // Chrome Autofill ignores these type hints.
1152 tokens.pop_back();
1155 // The preceding token, if any, may be a fixed string that is either
1156 // "shipping" or "billing". Chrome Autofill treats these as implicit
1157 // section name suffixes.
1158 DCHECK_EQ(kDefaultSection, field->section());
1159 std::string section = field->section();
1160 HtmlFieldMode mode = HTML_MODE_NONE;
1161 if (!tokens.empty()) {
1162 if (tokens.back() == kShippingMode)
1163 mode = HTML_MODE_SHIPPING;
1164 else if (tokens.back() == kBillingMode)
1165 mode = HTML_MODE_BILLING;
1168 if (mode != HTML_MODE_NONE) {
1169 section = "-" + tokens.back();
1170 tokens.pop_back();
1173 // The preceding token, if any, may be a named section.
1174 const std::string kSectionPrefix = "section-";
1175 if (!tokens.empty() &&
1176 base::StartsWith(tokens.back(), kSectionPrefix,
1177 base::CompareCase::SENSITIVE)) {
1178 // Prepend this section name to the suffix set in the preceding block.
1179 section = tokens.back().substr(kSectionPrefix.size()) + section;
1180 tokens.pop_back();
1183 // No other tokens are allowed. If there are any remaining, abort.
1184 if (!tokens.empty())
1185 continue;
1187 if (section != kDefaultSection) {
1188 *found_sections = true;
1189 field->set_section(section);
1192 // No errors encountered while parsing!
1193 // Update the |field|'s type based on what was parsed from the attribute.
1194 field->SetHtmlType(field_type, mode);
1198 bool FormStructure::FillFields(
1199 const std::vector<ServerFieldType>& types,
1200 const InputFieldComparator& matches,
1201 const base::Callback<base::string16(const AutofillType&)>& get_info,
1202 const std::string& address_language_code,
1203 const std::string& app_locale) {
1204 bool filled_something = false;
1205 for (size_t i = 0; i < field_count(); ++i) {
1206 for (size_t j = 0; j < types.size(); ++j) {
1207 if (matches.Run(types[j], *field(i))) {
1208 AutofillField::FillFormField(*field(i),
1209 get_info.Run(field(i)->Type()),
1210 address_language_code,
1211 app_locale,
1212 field(i));
1213 filled_something = true;
1214 break;
1218 return filled_something;
1221 std::set<base::string16> FormStructure::PossibleValues(ServerFieldType type) {
1222 std::set<base::string16> values;
1223 AutofillType target_type(type);
1224 for (std::vector<AutofillField*>::iterator iter = fields_.begin();
1225 iter != fields_.end(); ++iter) {
1226 AutofillField* field = *iter;
1227 if (field->Type().GetStorableType() != target_type.GetStorableType() ||
1228 field->Type().group() != target_type.group()) {
1229 continue;
1232 // No option values; anything goes.
1233 if (field->option_values.empty())
1234 return std::set<base::string16>();
1236 for (size_t i = 0; i < field->option_values.size(); ++i) {
1237 if (!field->option_values[i].empty())
1238 values.insert(base::i18n::ToUpper(field->option_values[i]));
1241 for (size_t i = 0; i < field->option_contents.size(); ++i) {
1242 if (!field->option_contents[i].empty())
1243 values.insert(base::i18n::ToUpper(field->option_contents[i]));
1247 return values;
1250 base::string16 FormStructure::GetUniqueValue(HtmlFieldType type) const {
1251 base::string16 value;
1252 for (std::vector<AutofillField*>::const_iterator iter = fields_.begin();
1253 iter != fields_.end(); ++iter) {
1254 const AutofillField* field = *iter;
1255 if (field->html_type() != type)
1256 continue;
1258 // More than one value found; abort rather than choosing one arbitrarily.
1259 if (!value.empty() && !field->value.empty())
1260 return base::string16();
1262 value = field->value;
1265 return value;
1268 void FormStructure::IdentifySections(bool has_author_specified_sections) {
1269 if (fields_.empty())
1270 return;
1272 if (!has_author_specified_sections) {
1273 // Name sections after the first field in the section.
1274 base::string16 current_section = fields_.front()->unique_name();
1276 // Keep track of the types we've seen in this section.
1277 std::set<ServerFieldType> seen_types;
1278 ServerFieldType previous_type = UNKNOWN_TYPE;
1280 for (AutofillField* field : fields_) {
1281 const ServerFieldType current_type = field->Type().GetStorableType();
1283 bool already_saw_current_type = seen_types.count(current_type) > 0;
1285 // Forms often ask for multiple phone numbers -- e.g. both a daytime and
1286 // evening phone number. Our phone number detection is also generally a
1287 // little off. Hence, ignore this field type as a signal here.
1288 if (AutofillType(current_type).group() == PHONE_HOME)
1289 already_saw_current_type = false;
1291 // Ignore non-focusable field and presentation role fields while inferring
1292 // boundaries between sections.
1293 bool ignored_field = !field->is_focusable ||
1294 field->role == FormFieldData::ROLE_ATTRIBUTE_PRESENTATION;
1295 if (ignored_field)
1296 already_saw_current_type = false;
1298 // Some forms have adjacent fields of the same type. Two common examples:
1299 // * Forms with two email fields, where the second is meant to "confirm"
1300 // the first.
1301 // * Forms with a <select> menu for states in some countries, and a
1302 // freeform <input> field for states in other countries. (Usually,
1303 // only one of these two will be visible for any given choice of
1304 // country.)
1305 // Generally, adjacent fields of the same type belong in the same logical
1306 // section.
1307 if (current_type == previous_type)
1308 already_saw_current_type = false;
1310 if (current_type != UNKNOWN_TYPE && already_saw_current_type) {
1311 // We reached the end of a section, so start a new section.
1312 seen_types.clear();
1313 current_section = field->unique_name();
1316 // Only consider a type "seen" if it was not ignored. Some forms have
1317 // sections for different locales, only one of which is enabled at a
1318 // time. Each section may duplicate some information (e.g. postal code)
1319 // and we don't want that to cause section splits.
1320 // Also only set |previous_type| when the field was not ignored. This
1321 // prevents ignored fields from breaking up fields that are otherwise
1322 // adjacent.
1323 if (!ignored_field) {
1324 seen_types.insert(current_type);
1325 previous_type = current_type;
1328 field->set_section(base::UTF16ToUTF8(current_section));
1332 // Ensure that credit card and address fields are in separate sections.
1333 // This simplifies the section-aware logic in autofill_manager.cc.
1334 for (AutofillField* field : fields_) {
1335 FieldTypeGroup field_type_group = field->Type().group();
1336 if (field_type_group == CREDIT_CARD)
1337 field->set_section(field->section() + "-cc");
1338 else
1339 field->set_section(field->section() + "-default");
1343 } // namespace autofill