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 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_
      6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/callback.h"
     13 #include "base/gtest_prod_util.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/scoped_vector.h"
     16 #include "base/strings/string16.h"
     17 #include "components/autofill/core/browser/autofill_field.h"
     18 #include "components/autofill/core/browser/autofill_type.h"
     19 #include "components/autofill/core/browser/field_types.h"
     20 #include "components/autofill/core/common/web_element_descriptor.h"
     21 #include "url/gurl.h"
     22 
     23 enum UploadRequired {
     24   UPLOAD_NOT_REQUIRED,
     25   UPLOAD_REQUIRED,
     26   USE_UPLOAD_RATES
     27 };
     28 
     29 namespace base {
     30 class TimeTicks;
     31 }
     32 
     33 namespace buzz {
     34 class XmlElement;
     35 }
     36 
     37 namespace autofill {
     38 
     39 class AutofillMetrics;
     40 
     41 struct FormData;
     42 struct FormDataPredictions;
     43 
     44 // FormStructure stores a single HTML form together with the values entered
     45 // in the fields along with additional information needed by Autofill.
     46 class FormStructure {
     47  public:
     48   FormStructure(const FormData& form);
     49   virtual ~FormStructure();
     50 
     51   // Runs several heuristics against the form fields to determine their possible
     52   // types.
     53   void DetermineHeuristicTypes(const AutofillMetrics& metric_logger);
     54 
     55   // Encodes the XML upload request from this FormStructure.
     56   bool EncodeUploadRequest(const ServerFieldTypeSet& available_field_types,
     57                            bool form_was_autofilled,
     58                            std::string* encoded_xml) const;
     59 
     60   // Encodes a XML block contains autofill field type from this FormStructure.
     61   // This XML will be written VLOG only, never be sent to server. It will
     62   // help make FieldAssignments and feed back to autofill server as
     63   // experiment data.
     64   bool EncodeFieldAssignments(const ServerFieldTypeSet& available_field_types,
     65                               std::string* encoded_xml) const;
     66 
     67   // Encodes the XML query request for the set of forms.
     68   // All fields are returned in one XML. For example, there are three forms,
     69   // with 2, 4, and 3 fields. The returned XML would have type info for 9
     70   // fields, first two of which would be for the first form, next 4 for the
     71   // second, and the rest is for the third.
     72   static bool EncodeQueryRequest(const std::vector<FormStructure*>& forms,
     73                                  std::vector<std::string>* encoded_signatures,
     74                                  std::string* encoded_xml);
     75 
     76   // Parses the field types from the server query response. |forms| must be the
     77   // same as the one passed to EncodeQueryRequest when constructing the query.
     78   static void ParseQueryResponse(
     79       const std::string& response_xml,
     80       const std::vector<FormStructure*>& forms,
     81       const AutofillMetrics& metric_logger);
     82 
     83   // Fills |forms| with the details from the given |form_structures| and their
     84   // fields' predicted types.
     85   static void GetFieldTypePredictions(
     86       const std::vector<FormStructure*>& form_structures,
     87       std::vector<FormDataPredictions>* forms);
     88 
     89   // The unique signature for this form, composed of the target url domain,
     90   // the form name, and the form field names in a 64-bit hash.
     91   std::string FormSignature() const;
     92 
     93   // Runs a quick heuristic to rule out forms that are obviously not
     94   // auto-fillable, like google/yahoo/msn search, etc.
     95   bool IsAutofillable() const;
     96 
     97   // Resets |autofill_count_| and counts the number of auto-fillable fields.
     98   // This is used when we receive server data for form fields.  At that time,
     99   // we may have more known fields than just the number of fields we matched
    100   // heuristically.
    101   void UpdateAutofillCount();
    102 
    103   // Returns true if this form matches the structural requirements for Autofill.
    104   bool ShouldBeParsed() const;
    105 
    106   // Returns true if we should query the crowdsourcing server to determine this
    107   // form's field types.  If the form includes author-specified types, this will
    108   // return false.
    109   bool ShouldBeCrowdsourced() const;
    110 
    111   // Sets the field types to be those set for |cached_form|.
    112   void UpdateFromCache(const FormStructure& cached_form);
    113 
    114   // Logs quality metrics for |this|, which should be a user-submitted form.
    115   // This method should only be called after the possible field types have been
    116   // set for each field.  |interaction_time| should be a timestamp corresponding
    117   // to the user's first interaction with the form.  |submission_time| should be
    118   // a timestamp corresponding to the form's submission.
    119   void LogQualityMetrics(const AutofillMetrics& metric_logger,
    120                          const base::TimeTicks& load_time,
    121                          const base::TimeTicks& interaction_time,
    122                          const base::TimeTicks& submission_time) const;
    123 
    124   // Classifies each field in |fields_| based upon its |autocomplete| attribute,
    125   // if the attribute is available.  The association is stored into the field's
    126   // |heuristic_type|.
    127   // Fills |found_types| with |true| if the attribute is available and neither
    128   // empty nor set to the special values "on" or "off" for at least one field.
    129   // Fills |found_sections| with |true| if the attribute specifies a section for
    130   // at least one field.
    131   void ParseFieldTypesFromAutocompleteAttributes(bool* found_types,
    132                                                  bool* found_sections);
    133 
    134   // Determines whether |type| and |field| match.
    135   typedef base::Callback<bool(ServerFieldType type,
    136                               const AutofillField& field)>
    137       InputFieldComparator;
    138 
    139   // Fills in |fields_| that match |types| (via |matches|) with info from
    140   // |get_info|. Uses |address_language_code| to determine line separators when
    141   // collapsing street address lines into a single-line input text field.
    142   bool FillFields(
    143       const std::vector<ServerFieldType>& types,
    144       const InputFieldComparator& matches,
    145       const base::Callback<base::string16(const AutofillType&)>& get_info,
    146       const std::string& address_language_code,
    147       const std::string& app_locale);
    148 
    149   // Returns the values that can be filled into the form structure for the
    150   // given type. For example, there's no way to fill in a value of "The Moon"
    151   // into ADDRESS_HOME_STATE if the form only has a
    152   // <select autocomplete="region"> with no "The Moon" option. Returns an
    153   // empty set if the form doesn't reference the given type or if all inputs
    154   // are accepted (e.g., <input type="text" autocomplete="region">).
    155   // All returned values are standardized to upper case.
    156   std::set<base::string16> PossibleValues(ServerFieldType type);
    157 
    158   // Gets the form's current value for |type|. For example, it may return
    159   // the contents of a text input or the currently selected <option>.
    160   base::string16 GetUniqueValue(HtmlFieldType type) const;
    161 
    162   const AutofillField* field(size_t index) const;
    163   AutofillField* field(size_t index);
    164   size_t field_count() const;
    165 
    166   // Returns the number of fields that are able to be autofilled.
    167   size_t autofill_count() const { return autofill_count_; }
    168 
    169   // Used for iterating over the fields.
    170   std::vector<AutofillField*>::const_iterator begin() const {
    171     return fields_.begin();
    172   }
    173   std::vector<AutofillField*>::const_iterator end() const {
    174     return fields_.end();
    175   }
    176 
    177   const GURL& source_url() const { return source_url_; }
    178 
    179   void set_upload_required(UploadRequired required) {
    180     upload_required_ = required;
    181   }
    182   UploadRequired upload_required() const { return upload_required_; }
    183 
    184   // Returns a FormData containing the data this form structure knows about.
    185   // |user_submitted| is currently always false.
    186   FormData ToFormData() const;
    187 
    188   bool operator==(const FormData& form) const;
    189   bool operator!=(const FormData& form) const;
    190 
    191  private:
    192   friend class FormStructureTest;
    193   FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
    194 
    195   // 64-bit hash of the string - used in FormSignature and unit-tests.
    196   static std::string Hash64Bit(const std::string& str);
    197 
    198   enum EncodeRequestType {
    199     QUERY,
    200     UPLOAD,
    201     FIELD_ASSIGNMENTS,
    202   };
    203 
    204   // Adds form info to |encompassing_xml_element|. |request_type| indicates if
    205   // it is a query or upload.
    206   bool EncodeFormRequest(EncodeRequestType request_type,
    207                          buzz::XmlElement* encompassing_xml_element) const;
    208 
    209   // Classifies each field in |fields_| into a logical section.
    210   // Sections are identified by the heuristic that a logical section should not
    211   // include multiple fields of the same autofill type (with some exceptions, as
    212   // described in the implementation).  Sections are furthermore distinguished
    213   // as either credit card or non-credit card sections.
    214   // If |has_author_specified_sections| is true, only the second pass --
    215   // distinguishing credit card sections from non-credit card ones -- is made.
    216   void IdentifySections(bool has_author_specified_sections);
    217 
    218   // Returns true if field should be skipped when talking to Autofill server.
    219   bool ShouldSkipField(const FormFieldData& field) const;
    220 
    221   size_t active_field_count() const;
    222 
    223   // The name of the form.
    224   base::string16 form_name_;
    225 
    226   // The source URL.
    227   GURL source_url_;
    228 
    229   // The target URL.
    230   GURL target_url_;
    231 
    232   // The number of fields able to be auto-filled.
    233   size_t autofill_count_;
    234 
    235   // A vector of all the input fields in the form.
    236   ScopedVector<AutofillField> fields_;
    237 
    238   // The number of fields counted towards form signature and request to Autofill
    239   // server.
    240   size_t active_field_count_;
    241 
    242   // The names of the form input elements, that are part of the form signature.
    243   // The string starts with "&" and the names are also separated by the "&"
    244   // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name"
    245   std::string form_signature_field_names_;
    246 
    247   // Whether the server expects us to always upload, never upload, or default
    248   // to the stored upload rates.
    249   UploadRequired upload_required_;
    250 
    251   // Whether the form includes any field types explicitly specified by the site
    252   // author, via the |autocompletetype| attribute.
    253   bool has_author_specified_types_;
    254 
    255   DISALLOW_COPY_AND_ASSIGN(FormStructure);
    256 };
    257 
    258 }  // namespace autofill
    259 
    260 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_
    261