ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / form_structure.h
blob22347198bb3689dbb2cc3e1be28dae69d4cc47a4
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_
8 #include <set>
9 #include <string>
10 #include <vector>
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"
21 #include "url/gurl.h"
23 enum UploadRequired {
24 UPLOAD_NOT_REQUIRED,
25 UPLOAD_REQUIRED,
26 USE_UPLOAD_RATES
29 namespace base {
30 class TimeTicks;
33 namespace buzz {
34 class XmlElement;
37 namespace autofill {
39 struct FormData;
40 struct FormDataPredictions;
42 // FormStructure stores a single HTML form together with the values entered
43 // in the fields along with additional information needed by Autofill.
44 class FormStructure {
45 public:
46 explicit FormStructure(const FormData& form);
47 virtual ~FormStructure();
49 // Runs several heuristics against the form fields to determine their possible
50 // types.
51 void DetermineHeuristicTypes();
53 // Encodes the XML upload request from this FormStructure.
54 bool EncodeUploadRequest(const ServerFieldTypeSet& available_field_types,
55 bool form_was_autofilled,
56 std::string* encoded_xml) const;
58 // Encodes a XML block contains autofill field type from this FormStructure.
59 // This XML will be written VLOG only, never be sent to server. It will
60 // help make FieldAssignments and feed back to autofill server as
61 // experiment data.
62 bool EncodeFieldAssignments(const ServerFieldTypeSet& available_field_types,
63 std::string* encoded_xml) const;
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);
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(const std::string& response_xml,
77 const std::vector<FormStructure*>& forms);
79 // Returns predictions using the details from the given |form_structures| and
80 // their fields' predicted types.
81 static std::vector<FormDataPredictions> GetFieldTypePredictions(
82 const std::vector<FormStructure*>& form_structures);
84 // The unique signature for this form, composed of the target url domain,
85 // the form name, and the form field names in a 64-bit hash.
86 std::string FormSignature() const;
88 // Runs a quick heuristic to rule out forms that are obviously not
89 // auto-fillable, like google/yahoo/msn search, etc.
90 bool IsAutofillable() const;
92 // Resets |autofill_count_| and counts the number of auto-fillable fields.
93 // This is used when we receive server data for form fields. At that time,
94 // we may have more known fields than just the number of fields we matched
95 // heuristically.
96 void UpdateAutofillCount();
98 // Returns true if this form matches the structural requirements for Autofill.
99 bool ShouldBeParsed() const;
101 // Returns true if we should query the crowdsourcing server to determine this
102 // form's field types. If the form includes author-specified types, this will
103 // return false unless there are password fields in the form. If there are no
104 // password fields the assumption is that the author has expressed their
105 // intent and crowdsourced data should not be used to override this. Password
106 // fields are different because there is no way to specify password generation
107 // directly.
108 bool ShouldBeCrowdsourced() const;
110 // Sets the field types to be those set for |cached_form|.
111 void UpdateFromCache(const FormStructure& cached_form);
113 // Logs quality metrics for |this|, which should be a user-submitted form.
114 // This method should only be called after the possible field types have been
115 // set for each field. |interaction_time| should be a timestamp corresponding
116 // to the user's first interaction with the form. |submission_time| should be
117 // a timestamp corresponding to the form's submission.
118 void LogQualityMetrics(const base::TimeTicks& load_time,
119 const base::TimeTicks& interaction_time,
120 const base::TimeTicks& submission_time) const;
122 // Classifies each field in |fields_| based upon its |autocomplete| attribute,
123 // if the attribute is available. The association is stored into the field's
124 // |heuristic_type|.
125 // Fills |found_types| with |true| if the attribute is available and neither
126 // empty nor set to the special values "on" or "off" for at least one field.
127 // Fills |found_sections| with |true| if the attribute specifies a section for
128 // at least one field.
129 void ParseFieldTypesFromAutocompleteAttributes(bool* found_types,
130 bool* found_sections);
132 // Determines whether |type| and |field| match.
133 typedef base::Callback<bool(ServerFieldType type,
134 const AutofillField& field)>
135 InputFieldComparator;
137 // Fills in |fields_| that match |types| (via |matches|) with info from
138 // |get_info|. Uses |address_language_code| to determine line separators when
139 // collapsing street address lines into a single-line input text field.
140 bool FillFields(
141 const std::vector<ServerFieldType>& types,
142 const InputFieldComparator& matches,
143 const base::Callback<base::string16(const AutofillType&)>& get_info,
144 const std::string& address_language_code,
145 const std::string& app_locale);
147 // Returns the values that can be filled into the form structure for the
148 // given type. For example, there's no way to fill in a value of "The Moon"
149 // into ADDRESS_HOME_STATE if the form only has a
150 // <select autocomplete="region"> with no "The Moon" option. Returns an
151 // empty set if the form doesn't reference the given type or if all inputs
152 // are accepted (e.g., <input type="text" autocomplete="region">).
153 // All returned values are standardized to upper case.
154 std::set<base::string16> PossibleValues(ServerFieldType type);
156 // Gets the form's current value for |type|. For example, it may return
157 // the contents of a text input or the currently selected <option>.
158 base::string16 GetUniqueValue(HtmlFieldType type) const;
160 const AutofillField* field(size_t index) const;
161 AutofillField* field(size_t index);
162 size_t field_count() const;
164 // Returns the number of fields that are able to be autofilled.
165 size_t autofill_count() const { return autofill_count_; }
167 // Used for iterating over the fields.
168 std::vector<AutofillField*>::const_iterator begin() const {
169 return fields_.begin();
171 std::vector<AutofillField*>::const_iterator end() const {
172 return fields_.end();
175 const GURL& source_url() const { return source_url_; }
177 void set_upload_required(UploadRequired required) {
178 upload_required_ = required;
180 UploadRequired upload_required() const { return upload_required_; }
182 // Returns a FormData containing the data this form structure knows about.
183 // |user_submitted| is currently always false.
184 FormData ToFormData() const;
186 bool operator==(const FormData& form) const;
187 bool operator!=(const FormData& form) const;
189 private:
190 friend class FormStructureTest;
191 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
193 // 64-bit hash of the string - used in FormSignature and unit-tests.
194 static std::string Hash64Bit(const std::string& str);
196 enum EncodeRequestType {
197 QUERY,
198 UPLOAD,
199 FIELD_ASSIGNMENTS,
202 // Adds form info to |encompassing_xml_element|. |request_type| indicates if
203 // it is a query or upload.
204 bool EncodeFormRequest(EncodeRequestType request_type,
205 buzz::XmlElement* encompassing_xml_element) const;
207 // Classifies each field in |fields_| into a logical section.
208 // Sections are identified by the heuristic that a logical section should not
209 // include multiple fields of the same autofill type (with some exceptions, as
210 // described in the implementation). Sections are furthermore distinguished
211 // as either credit card or non-credit card sections.
212 // If |has_author_specified_sections| is true, only the second pass --
213 // distinguishing credit card sections from non-credit card ones -- is made.
214 void IdentifySections(bool has_author_specified_sections);
216 // Returns true if field should be skipped when talking to Autofill server.
217 bool ShouldSkipField(const FormFieldData& field) const;
219 size_t active_field_count() const;
221 // The name of the form.
222 base::string16 form_name_;
224 // The source URL.
225 GURL source_url_;
227 // The target URL.
228 GURL target_url_;
230 // The number of fields able to be auto-filled.
231 size_t autofill_count_;
233 // A vector of all the input fields in the form.
234 ScopedVector<AutofillField> fields_;
236 // The number of fields counted towards form signature and request to Autofill
237 // server.
238 size_t active_field_count_;
240 // The names of the form input elements, that are part of the form signature.
241 // The string starts with "&" and the names are also separated by the "&"
242 // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name"
243 std::string form_signature_field_names_;
245 // Whether the server expects us to always upload, never upload, or default
246 // to the stored upload rates.
247 UploadRequired upload_required_;
249 // Whether the form includes any field types explicitly specified by the site
250 // author, via the |autocompletetype| attribute.
251 bool has_author_specified_types_;
253 // True if the form contains at least one password field.
254 bool has_password_field_;
256 // True if the form is a <form>.
257 bool is_form_tag_;
259 DISALLOW_COPY_AND_ASSIGN(FormStructure);
262 } // namespace autofill
264 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_