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