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