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