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_
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"
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 explicit FormStructure(const FormData
& form
);
51 virtual ~FormStructure();
53 // Runs several heuristics against the form fields to determine their possible
55 void DetermineHeuristicTypes();
57 // Encodes the XML upload request from this FormStructure.
58 // In some cases, a |login_form_signature| is included as part of the upload.
59 // This field is empty when sending upload requests for non-login forms.
60 bool EncodeUploadRequest(const ServerFieldTypeSet
& available_field_types
,
61 bool form_was_autofilled
,
62 const std::string
& login_form_signature
,
63 std::string
* encoded_xml
) const;
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
69 bool EncodeFieldAssignments(const ServerFieldTypeSet
& available_field_types
,
70 std::string
* encoded_xml
) const;
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
);
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 // |rappor_service| may be null.
84 static void ParseQueryResponse(const std::string
& response_xml
,
85 const std::vector
<FormStructure
*>& forms
,
86 rappor::RapporService
* rappor_service
);
88 // Returns predictions using the details from the given |form_structures| and
89 // their fields' predicted types.
90 static std::vector
<FormDataPredictions
> GetFieldTypePredictions(
91 const std::vector
<FormStructure
*>& form_structures
);
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;
97 // Runs a quick heuristic to rule out forms that are obviously not
98 // auto-fillable, like google/yahoo/msn search, etc.
99 bool IsAutofillable() const;
101 // Resets |autofill_count_| and counts the number of auto-fillable fields.
102 // This is used when we receive server data for form fields. At that time,
103 // we may have more known fields than just the number of fields we matched
105 void UpdateAutofillCount();
107 // Returns true if this form matches the structural requirements for Autofill.
108 bool ShouldBeParsed() const;
110 // Returns true if we should query the crowdsourcing server to determine this
111 // form's field types. If the form includes author-specified types, this will
112 // return false unless there are password fields in the form. If there are no
113 // password fields the assumption is that the author has expressed their
114 // intent and crowdsourced data should not be used to override this. Password
115 // fields are different because there is no way to specify password generation
117 bool ShouldBeCrowdsourced() const;
119 // Sets the field types to be those set for |cached_form|.
120 void UpdateFromCache(const FormStructure
& cached_form
);
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 base::TimeTicks
& load_time
,
128 const base::TimeTicks
& interaction_time
,
129 const base::TimeTicks
& submission_time
,
130 rappor::RapporService
* rappor_service
) const;
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
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
);
142 // Determines whether |type| and |field| match.
143 typedef base::Callback
<bool(ServerFieldType type
,
144 const AutofillField
& field
)>
145 InputFieldComparator
;
147 // Fills in |fields_| that match |types| (via |matches|) with info from
148 // |get_info|. Uses |address_language_code| to determine line separators when
149 // collapsing street address lines into a single-line input text field.
151 const std::vector
<ServerFieldType
>& types
,
152 const InputFieldComparator
& matches
,
153 const base::Callback
<base::string16(const AutofillType
&)>& get_info
,
154 const std::string
& address_language_code
,
155 const std::string
& app_locale
);
157 // Returns the values that can be filled into the form structure for the
158 // given type. For example, there's no way to fill in a value of "The Moon"
159 // into ADDRESS_HOME_STATE if the form only has a
160 // <select autocomplete="region"> with no "The Moon" option. Returns an
161 // empty set if the form doesn't reference the given type or if all inputs
162 // are accepted (e.g., <input type="text" autocomplete="region">).
163 // All returned values are standardized to upper case.
164 std::set
<base::string16
> PossibleValues(ServerFieldType type
);
166 // Gets the form's current value for |type|. For example, it may return
167 // the contents of a text input or the currently selected <option>.
168 base::string16
GetUniqueValue(HtmlFieldType type
) const;
170 const AutofillField
* field(size_t index
) const;
171 AutofillField
* field(size_t index
);
172 size_t field_count() const;
174 // Returns the number of fields that are able to be autofilled.
175 size_t autofill_count() const { return autofill_count_
; }
177 // Used for iterating over the fields.
178 std::vector
<AutofillField
*>::const_iterator
begin() const {
179 return fields_
.begin();
181 std::vector
<AutofillField
*>::const_iterator
end() const {
182 return fields_
.end();
185 const base::string16
& form_name() const { return form_name_
; }
187 const GURL
& source_url() const { return source_url_
; }
189 bool has_password_field() const { return has_password_field_
; }
191 void set_upload_required(UploadRequired required
) {
192 upload_required_
= required
;
194 UploadRequired
upload_required() const { return upload_required_
; }
196 // Returns a FormData containing the data this form structure knows about.
197 FormData
ToFormData() const;
199 bool operator==(const FormData
& form
) const;
200 bool operator!=(const FormData
& form
) const;
203 friend class FormStructureTest
;
204 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest
, QueryAndUploadTest
);
206 // 64-bit hash of the string - used in FormSignature and unit-tests.
207 static std::string
Hash64Bit(const std::string
& str
);
209 enum EncodeRequestType
{
215 // Adds form info to |encompassing_xml_element|. |request_type| indicates if
216 // it is a query or upload.
217 bool EncodeFormRequest(EncodeRequestType request_type
,
218 buzz::XmlElement
* encompassing_xml_element
) const;
220 // Classifies each field in |fields_| into a logical section.
221 // Sections are identified by the heuristic that a logical section should not
222 // include multiple fields of the same autofill type (with some exceptions, as
223 // described in the implementation). Sections are furthermore distinguished
224 // as either credit card or non-credit card sections.
225 // If |has_author_specified_sections| is true, only the second pass --
226 // distinguishing credit card sections from non-credit card ones -- is made.
227 void IdentifySections(bool has_author_specified_sections
);
229 // Returns true if field should be skipped when talking to Autofill server.
230 bool ShouldSkipField(const FormFieldData
& field
) const;
232 size_t active_field_count() const;
234 // The name of the form.
235 base::string16 form_name_
;
243 // The number of fields able to be auto-filled.
244 size_t autofill_count_
;
246 // A vector of all the input fields in the form.
247 ScopedVector
<AutofillField
> fields_
;
249 // The number of fields counted towards form signature and request to Autofill
251 size_t active_field_count_
;
253 // The names of the form input elements, that are part of the form signature.
254 // The string starts with "&" and the names are also separated by the "&"
255 // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name"
256 std::string form_signature_field_names_
;
258 // Whether the server expects us to always upload, never upload, or default
259 // to the stored upload rates.
260 UploadRequired upload_required_
;
262 // Whether the form includes any field types explicitly specified by the site
263 // author, via the |autocompletetype| attribute.
264 bool has_author_specified_types_
;
266 // True if the form contains at least one password field.
267 bool has_password_field_
;
269 // True if the form is a <form>.
272 DISALLOW_COPY_AND_ASSIGN(FormStructure
);
275 } // namespace autofill
277 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_