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