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