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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_
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"
41 class AutofillMetrics;
44 struct FormDataPredictions;
46 // FormStructure stores a single HTML form together with the values entered
47 // in the fields along with additional information needed by Autofill.
50 FormStructure(const FormData& form);
51 virtual ~FormStructure();
53 // Runs several heuristics against the form fields to determine their possible
55 void DetermineHeuristicTypes(const AutofillMetrics& metric_logger);
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;
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
66 bool EncodeFieldAssignments(const ServerFieldTypeSet& available_field_types,
67 std::string* encoded_xml) const;
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);
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);
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);
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;
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;
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
104 void UpdateAutofillCount();
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;
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
114 bool ShouldBeCrowdsourced() const;
116 // Sets the field types and experiment id to be those set for |cached_form|.
117 void UpdateFromCache(const FormStructure& cached_form);
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;
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
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);
139 const AutofillField* field(size_t index) const;
140 AutofillField* field(size_t index);
141 size_t field_count() const;
143 // Returns the number of fields that are able to be autofilled.
144 size_t autofill_count() const { return autofill_count_; }
146 // Used for iterating over the fields.
147 std::vector<AutofillField*>::const_iterator begin() const {
148 return fields_.begin();
150 std::vector<AutofillField*>::const_iterator end() const {
151 return fields_.end();
154 const GURL& source_url() const { return source_url_; }
156 void set_upload_required(UploadRequired required) {
157 upload_required_ = required;
159 UploadRequired upload_required() const { return upload_required_; }
161 virtual std::string server_experiment_id() const;
163 // Returns a FormData containing the data this form structure knows about.
164 // |user_submitted| is currently always false.
165 FormData ToFormData() const;
167 bool operator==(const FormData& form) const;
168 bool operator!=(const FormData& form) const;
171 friend class FormStructureTest;
172 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
174 // 64-bit hash of the string - used in FormSignature and unit-tests.
175 static std::string Hash64Bit(const std::string& str);
177 enum EncodeRequestType {
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;
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);
197 // Returns true if field should be skipped when talking to Autofill server.
198 bool ShouldSkipField(const FormFieldData& field) const;
200 size_t active_field_count() const;
202 // The name of the form.
203 base::string16 form_name_;
211 // The number of fields able to be auto-filled.
212 size_t autofill_count_;
214 // A vector of all the input fields in the form.
215 ScopedVector<AutofillField> fields_;
217 // The number of fields counted towards form signature and request to Autofill
219 size_t active_field_count_;
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_;
226 // Whether the server expects us to always upload, never upload, or default
227 // to the stored upload rates.
228 UploadRequired upload_required_;
230 // The server experiment corresponding to the server types returned for this
232 std::string server_experiment_id_;
235 RequestMethod method_;
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_;
241 DISALLOW_COPY_AND_ASSIGN(FormStructure);
244 } // namespace autofill
246 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_