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