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