1 // Copyright (c) 2011 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 CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_
6 #define CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_vector.h"
13 #include "chrome/browser/autofill/autofill_field.h"
14 #include "chrome/browser/autofill/autofill_type.h"
15 #include "chrome/browser/autofill/field_types.h"
16 #include "googleurl/src/gurl.h"
19 struct FormDataPredictions
;
32 class AutofillMetrics
;
42 // FormStructure stores a single HTML form together with the values entered
43 // in the fields along with additional information needed by Autofill.
46 explicit FormStructure(const FormData
& form
);
47 virtual ~FormStructure();
49 // Runs several heuristics against the form fields to determine their possible
51 void DetermineHeuristicTypes(const AutofillMetrics
& metric_logger
);
53 // Encodes the XML upload request from this FormStructure.
54 bool EncodeUploadRequest(const FieldTypeSet
& available_field_types
,
55 bool form_was_autofilled
,
56 std::string
* encoded_xml
) const;
58 // Encodes the XML query request for the set of forms.
59 // All fields are returned in one XML. For example, there are three forms,
60 // with 2, 4, and 3 fields. The returned XML would have type info for 9
61 // fields, first two of which would be for the first form, next 4 for the
62 // second, and the rest is for the third.
63 static bool EncodeQueryRequest(const std::vector
<FormStructure
*>& forms
,
64 std::vector
<std::string
>* encoded_signatures
,
65 std::string
* encoded_xml
);
67 // Parses the field types from the server query response. |forms| must be the
68 // same as the one passed to EncodeQueryRequest when constructing the query.
69 static void ParseQueryResponse(const std::string
& response_xml
,
70 const std::vector
<FormStructure
*>& forms
,
71 const AutofillMetrics
& metric_logger
);
73 // Fills |forms| with the details from the given |form_structures| and their
74 // fields' predicted types.
75 static void GetFieldTypePredictions(
76 const std::vector
<FormStructure
*>& form_structures
,
77 std::vector
<FormDataPredictions
>* forms
);
79 // The unique signature for this form, composed of the target url domain,
80 // the form name, and the form field names in a 64-bit hash.
81 std::string
FormSignature() const;
83 // Runs a quick heuristic to rule out forms that are obviously not
84 // auto-fillable, like google/yahoo/msn search, etc. The requirement that the
85 // form's method be POST is only applied if |require_method_post| is true.
86 bool IsAutofillable(bool require_method_post
) const;
88 // Resets |autofill_count_| and counts the number of auto-fillable fields.
89 // This is used when we receive server data for form fields. At that time,
90 // we may have more known fields than just the number of fields we matched
92 void UpdateAutofillCount();
94 // Returns true if this form matches the structural requirements for Autofill.
95 // The requirement that the form's method be POST is only applied if
96 // |require_method_post| is true.
97 bool ShouldBeParsed(bool require_method_post
) const;
99 // Returns true if we should query the crowdsourcing server to determine this
100 // form's field types. If the form includes author-specified types, this will
102 bool ShouldBeCrowdsourced() const;
104 // Sets the field types and experiment id to be those set for |cached_form|.
105 void UpdateFromCache(const FormStructure
& cached_form
);
107 // Logs quality metrics for |this|, which should be a user-submitted form.
108 // This method should only be called after the possible field types have been
109 // set for each field. |interaction_time| should be a timestamp corresponding
110 // to the user's first interaction with the form. |submission_time| should be
111 // a timestamp corresponding to the form's submission.
112 void LogQualityMetrics(const AutofillMetrics
& metric_logger
,
113 const base::TimeTicks
& load_time
,
114 const base::TimeTicks
& interaction_time
,
115 const base::TimeTicks
& submission_time
) const;
117 const AutofillField
* field(size_t index
) const;
118 AutofillField
* field(size_t index
);
119 size_t field_count() const;
121 // Returns the number of fields that are able to be autofilled.
122 size_t autofill_count() const { return autofill_count_
; }
124 // Used for iterating over the fields.
125 std::vector
<AutofillField
*>::const_iterator
begin() const {
126 return fields_
.begin();
128 std::vector
<AutofillField
*>::const_iterator
end() const {
129 return fields_
.end();
132 const GURL
& source_url() const { return source_url_
; }
134 UploadRequired
upload_required() const { return upload_required_
; }
136 virtual std::string
server_experiment_id() const;
138 bool operator==(const FormData
& form
) const;
139 bool operator!=(const FormData
& form
) const;
142 friend class FormStructureTest
;
143 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest
, QueryAndUploadTest
);
144 // 64-bit hash of the string - used in FormSignature and unit-tests.
145 static std::string
Hash64Bit(const std::string
& str
);
147 enum EncodeRequestType
{
152 // Adds form info to |encompassing_xml_element|. |request_type| indicates if
153 // it is a query or upload.
154 bool EncodeFormRequest(EncodeRequestType request_type
,
155 buzz::XmlElement
* encompassing_xml_element
) const;
157 // Classifies each field in |fields_| based upon its |autocomplete| attribute,
158 // if the attribute is available. The association is stored into the field's
160 // Fills |found_types| with |true| if the attribute is available and neither
161 // empty nor set to the special values "on" or "off" for at least one field.
162 // Fills |found_sections| with |true| if the attribute specifies a section for
163 // at least one field.
164 void ParseFieldTypesFromAutocompleteAttributes(bool* found_types
,
165 bool* found_sections
);
167 // Classifies each field in |fields_| into a logical section.
168 // Sections are identified by the heuristic that a logical section should not
169 // include multiple fields of the same autofill type (with some exceptions, as
170 // described in the implementation). Sections are furthermore distinguished
171 // as either credit card or non-credit card sections.
172 // If |has_author_specified_sections| is true, only the second pass --
173 // distinguishing credit card sections from non-credit card ones -- is made.
174 void IdentifySections(bool has_author_specified_sections
);
176 // The name of the form.
185 // The number of fields able to be auto-filled.
186 size_t autofill_count_
;
188 // A vector of all the input fields in the form.
189 ScopedVector
<AutofillField
> fields_
;
191 // The names of the form input elements, that are part of the form signature.
192 // The string starts with "&" and the names are also separated by the "&"
193 // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name"
194 std::string form_signature_field_names_
;
196 // Whether the server expects us to always upload, never upload, or default
197 // to the stored upload rates.
198 UploadRequired upload_required_
;
200 // The server experiment corresponding to the server types returned for this
202 std::string server_experiment_id_
;
205 RequestMethod method_
;
207 // Whether the form includes any field types explicitly specified by the site
208 // author, via the |autocompletetype| attribute.
209 bool has_author_specified_types_
;
211 DISALLOW_COPY_AND_ASSIGN(FormStructure
);
214 #endif // CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_