Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_ |
| 6 | #define COMPONENTS_AUTOFILL_CORE_BROWSER_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 "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" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 18 | #include "url/gurl.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 19 | |
| 20 | enum RequestMethod { |
| 21 | GET, |
| 22 | POST |
| 23 | }; |
| 24 | |
| 25 | enum UploadRequired { |
| 26 | UPLOAD_NOT_REQUIRED, |
| 27 | UPLOAD_REQUIRED, |
| 28 | USE_UPLOAD_RATES |
| 29 | }; |
| 30 | |
| 31 | namespace base { |
| 32 | class TimeTicks; |
| 33 | } |
| 34 | |
| 35 | namespace buzz { |
| 36 | class XmlElement; |
| 37 | } |
| 38 | |
| 39 | namespace autofill { |
| 40 | |
| 41 | class AutofillMetrics; |
| 42 | |
| 43 | struct AutocheckoutPageMetaData; |
| 44 | struct FormData; |
| 45 | struct FormDataPredictions; |
| 46 | |
| 47 | // FormStructure stores a single HTML form together with the values entered |
| 48 | // in the fields along with additional information needed by Autofill. |
| 49 | class FormStructure { |
| 50 | public: |
| 51 | FormStructure(const FormData& form, |
| 52 | const std::string& autocheckout_url_prefix); |
| 53 | virtual ~FormStructure(); |
| 54 | |
| 55 | // Runs several heuristics against the form fields to determine their possible |
| 56 | // types. |
| 57 | void DetermineHeuristicTypes(const AutofillMetrics& metric_logger); |
| 58 | |
| 59 | // Encodes the XML upload request from this FormStructure. |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 60 | bool EncodeUploadRequest(const ServerFieldTypeSet& available_field_types, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 61 | bool form_was_autofilled, |
| 62 | std::string* encoded_xml) const; |
| 63 | |
| 64 | // Encodes a XML block contains autofill field type from this FormStructure. |
| 65 | // This XML will be written VLOG only, never be sent to server. It will |
| 66 | // help make FieldAssignments and feed back to autofill server as |
| 67 | // experiment data. |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 68 | bool EncodeFieldAssignments(const ServerFieldTypeSet& available_field_types, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 69 | std::string* encoded_xml) const; |
| 70 | |
| 71 | // Encodes the XML query request for the set of forms. |
| 72 | // All fields are returned in one XML. For example, there are three forms, |
| 73 | // with 2, 4, and 3 fields. The returned XML would have type info for 9 |
| 74 | // fields, first two of which would be for the first form, next 4 for the |
| 75 | // second, and the rest is for the third. |
| 76 | static bool EncodeQueryRequest(const std::vector<FormStructure*>& forms, |
| 77 | std::vector<std::string>* encoded_signatures, |
| 78 | std::string* encoded_xml); |
| 79 | |
| 80 | // Parses the field types from the server query response. |forms| must be the |
| 81 | // same as the one passed to EncodeQueryRequest when constructing the query. |
| 82 | static void ParseQueryResponse( |
| 83 | const std::string& response_xml, |
| 84 | const std::vector<FormStructure*>& forms, |
| 85 | autofill::AutocheckoutPageMetaData* page_meta_data, |
| 86 | const AutofillMetrics& metric_logger); |
| 87 | |
| 88 | // Fills |forms| with the details from the given |form_structures| and their |
| 89 | // fields' predicted types. |
| 90 | static void GetFieldTypePredictions( |
| 91 | const std::vector<FormStructure*>& form_structures, |
| 92 | std::vector<FormDataPredictions>* forms); |
| 93 | |
| 94 | // The unique signature for this form, composed of the target url domain, |
| 95 | // the form name, and the form field names in a 64-bit hash. |
| 96 | std::string FormSignature() const; |
| 97 | |
| 98 | // Runs a quick heuristic to rule out forms that are obviously not |
| 99 | // auto-fillable, like google/yahoo/msn search, etc. The requirement that the |
| 100 | // form's method be POST is only applied if |require_method_post| is true. |
| 101 | bool IsAutofillable(bool require_method_post) const; |
| 102 | |
| 103 | // Resets |autofill_count_| and counts the number of auto-fillable fields. |
| 104 | // This is used when we receive server data for form fields. At that time, |
| 105 | // we may have more known fields than just the number of fields we matched |
| 106 | // heuristically. |
| 107 | void UpdateAutofillCount(); |
| 108 | |
| 109 | // Returns true if this form matches the structural requirements for Autofill. |
| 110 | // The requirement that the form's method be POST is only applied if |
| 111 | // |require_method_post| is true. |
| 112 | bool ShouldBeParsed(bool require_method_post) const; |
| 113 | |
| 114 | // Returns true if we should query the crowdsourcing server to determine this |
| 115 | // form's field types. If the form includes author-specified types, this will |
| 116 | // return false. |
| 117 | bool ShouldBeCrowdsourced() const; |
| 118 | |
| 119 | // Sets the field types and experiment id to be those set for |cached_form|. |
| 120 | void UpdateFromCache(const FormStructure& cached_form); |
| 121 | |
| 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 AutofillMetrics& metric_logger, |
| 128 | const base::TimeTicks& load_time, |
| 129 | const base::TimeTicks& interaction_time, |
| 130 | const base::TimeTicks& submission_time) const; |
| 131 | |
| 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 |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 134 | // |heuristic_type|. |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 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. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 139 | void ParseFieldTypesFromAutocompleteAttributes(bool* found_types, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 140 | bool* found_sections); |
| 141 | |
| 142 | const AutofillField* field(size_t index) const; |
| 143 | AutofillField* field(size_t index); |
| 144 | size_t field_count() const; |
| 145 | |
| 146 | // Returns the number of fields that are able to be autofilled. |
| 147 | size_t autofill_count() const { return autofill_count_; } |
| 148 | |
| 149 | // Used for iterating over the fields. |
| 150 | std::vector<AutofillField*>::const_iterator begin() const { |
| 151 | return fields_.begin(); |
| 152 | } |
| 153 | std::vector<AutofillField*>::const_iterator end() const { |
| 154 | return fields_.end(); |
| 155 | } |
| 156 | |
| 157 | const GURL& source_url() const { return source_url_; } |
| 158 | |
| 159 | UploadRequired upload_required() const { return upload_required_; } |
| 160 | |
| 161 | virtual std::string server_experiment_id() const; |
| 162 | |
| 163 | // Returns a FormData containing the data this form structure knows about. |
| 164 | // |user_submitted| is currently always false. |
| 165 | FormData ToFormData() const; |
| 166 | |
| 167 | bool filled_by_autocheckout() const { return filled_by_autocheckout_; } |
| 168 | void set_filled_by_autocheckout(bool filled_by_autocheckout) { |
| 169 | filled_by_autocheckout_ = filled_by_autocheckout; |
| 170 | } |
| 171 | |
| 172 | bool operator==(const FormData& form) const; |
| 173 | bool operator!=(const FormData& form) const; |
| 174 | |
| 175 | private: |
| 176 | friend class FormStructureTest; |
| 177 | FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest); |
| 178 | |
| 179 | // 64-bit hash of the string - used in FormSignature and unit-tests. |
| 180 | static std::string Hash64Bit(const std::string& str); |
| 181 | |
| 182 | enum EncodeRequestType { |
| 183 | QUERY, |
| 184 | UPLOAD, |
| 185 | FIELD_ASSIGNMENTS, |
| 186 | }; |
| 187 | |
| 188 | // Adds form info to |encompassing_xml_element|. |request_type| indicates if |
| 189 | // it is a query or upload. |
| 190 | bool EncodeFormRequest(EncodeRequestType request_type, |
| 191 | buzz::XmlElement* encompassing_xml_element) const; |
| 192 | |
| 193 | // Classifies each field in |fields_| into a logical section. |
| 194 | // Sections are identified by the heuristic that a logical section should not |
| 195 | // include multiple fields of the same autofill type (with some exceptions, as |
| 196 | // described in the implementation). Sections are furthermore distinguished |
| 197 | // as either credit card or non-credit card sections. |
| 198 | // If |has_author_specified_sections| is true, only the second pass -- |
| 199 | // distinguishing credit card sections from non-credit card ones -- is made. |
| 200 | void IdentifySections(bool has_author_specified_sections); |
| 201 | |
| 202 | bool IsAutocheckoutEnabled() const; |
| 203 | |
| 204 | // Returns true if field should be skipped when talking to Autofill server. |
| 205 | bool ShouldSkipField(const FormFieldData& field) const; |
| 206 | |
| 207 | // Returns the minimal number of fillable fields required to start autofill. |
| 208 | size_t RequiredFillableFields() const; |
| 209 | size_t active_field_count() const; |
| 210 | |
| 211 | // The name of the form. |
| 212 | base::string16 form_name_; |
| 213 | |
| 214 | // The source URL. |
| 215 | GURL source_url_; |
| 216 | |
| 217 | // The target URL. |
| 218 | GURL target_url_; |
| 219 | |
| 220 | // The number of fields able to be auto-filled. |
| 221 | size_t autofill_count_; |
| 222 | |
| 223 | // A vector of all the input fields in the form. |
| 224 | ScopedVector<AutofillField> fields_; |
| 225 | |
| 226 | // The number of fields counted towards form signature and request to Autofill |
| 227 | // server. |
| 228 | size_t active_field_count_; |
| 229 | |
| 230 | // The names of the form input elements, that are part of the form signature. |
| 231 | // The string starts with "&" and the names are also separated by the "&" |
| 232 | // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name" |
| 233 | std::string form_signature_field_names_; |
| 234 | |
| 235 | // Whether the server expects us to always upload, never upload, or default |
| 236 | // to the stored upload rates. |
| 237 | UploadRequired upload_required_; |
| 238 | |
| 239 | // The server experiment corresponding to the server types returned for this |
| 240 | // form. |
| 241 | std::string server_experiment_id_; |
| 242 | |
| 243 | // GET or POST. |
| 244 | RequestMethod method_; |
| 245 | |
| 246 | // Whether the form includes any field types explicitly specified by the site |
| 247 | // author, via the |autocompletetype| attribute. |
| 248 | bool has_author_specified_types_; |
| 249 | |
| 250 | // The URL prefix matched in autocheckout whitelist. An empty string implies |
| 251 | // autocheckout is not enabled for this form. |
| 252 | std::string autocheckout_url_prefix_; |
| 253 | |
| 254 | // Whether or not this form was filled by Autocheckout. |
| 255 | bool filled_by_autocheckout_; |
| 256 | |
| 257 | DISALLOW_COPY_AND_ASSIGN(FormStructure); |
| 258 | }; |
| 259 | |
| 260 | } // namespace autofill |
| 261 | |
| 262 | #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_STRUCTURE_H_ |