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 #pragma once 8 9 #include <string> 10 #include <vector> 11 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" 17 #include "webkit/glue/form_data.h" 18 19 namespace buzz { 20 class XmlElement; 21 } // namespace buzz 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 // FormStructure stores a single HTML form together with the values entered 37 // in the fields along with additional information needed by Autofill. 38 class FormStructure { 39 public: 40 explicit FormStructure(const webkit_glue::FormData& form); 41 virtual ~FormStructure(); 42 43 // Runs several heuristics against the form fields to determine their possible 44 // types. 45 void DetermineHeuristicTypes(); 46 47 // Encodes the XML upload request from this FormStructure. 48 bool EncodeUploadRequest(bool autofill_used, std::string* encoded_xml) const; 49 50 // Encodes the XML query request for the set of forms. 51 // All fields are returned in one XML. For example, there are three forms, 52 // with 2, 4, and 3 fields. The returned XML would have type info for 9 53 // fields, first two of which would be for the first form, next 4 for the 54 // second, and the rest is for the third. 55 static bool EncodeQueryRequest(const ScopedVector<FormStructure>& forms, 56 std::vector<std::string>* encoded_signatures, 57 std::string* encoded_xml); 58 59 // Parses the field types from the server query response. |forms| must be the 60 // same as the one passed to EncodeQueryRequest when constructing the query. 61 static void ParseQueryResponse(const std::string& response_xml, 62 const std::vector<FormStructure*>& forms, 63 UploadRequired* upload_required, 64 const AutofillMetrics& metric_logger); 65 66 // The unique signature for this form, composed of the target url domain, 67 // the form name, and the form field names in a 64-bit hash. 68 std::string FormSignature() const; 69 70 // Runs a quick heuristic to rule out forms that are obviously not 71 // auto-fillable, like google/yahoo/msn search, etc. The requirement that the 72 // form's method be POST is only applied if |require_method_post| is true. 73 bool IsAutofillable(bool require_method_post) const; 74 75 // Resets |autofill_count_| and counts the number of auto-fillable fields. 76 // This is used when we receive server data for form fields. At that time, 77 // we may have more known fields than just the number of fields we matched 78 // heuristically. 79 void UpdateAutofillCount(); 80 81 // Returns true if this form matches the structural requirements for Autofill. 82 // The requirement that the form's method be POST is only applied if 83 // |require_method_post| is true. 84 bool ShouldBeParsed(bool require_method_post) const; 85 86 // Sets the field types and experiment id to be those set for |cached_form|. 87 void UpdateFromCache(const FormStructure& cached_form); 88 89 // Logs quality metrics for |this|, which should be a user-submitted form. 90 // This method should only be called after the possible field types have been 91 // set for each field. 92 void LogQualityMetrics(const AutofillMetrics& metric_logger) const; 93 94 // Sets the possible types for the field at |index|. 95 void set_possible_types(int index, const FieldTypeSet& types); 96 97 const AutofillField* field(int index) const; 98 size_t field_count() const; 99 100 // Returns the number of fields that are able to be autofilled. autofill_count()101 size_t autofill_count() const { return autofill_count_; } 102 103 // Used for iterating over the fields. begin()104 std::vector<AutofillField*>::const_iterator begin() const { 105 return fields_.begin(); 106 } end()107 std::vector<AutofillField*>::const_iterator end() const { 108 return fields_.end(); 109 } 110 source_url()111 const GURL& source_url() const { return source_url_; } 112 113 virtual std::string server_experiment_id() const; 114 115 bool operator==(const webkit_glue::FormData& form) const; 116 bool operator!=(const webkit_glue::FormData& form) const; 117 118 protected: 119 // For tests. fields()120 ScopedVector<AutofillField>* fields() { return &fields_; } 121 122 private: 123 friend class FormStructureTest; 124 // 64-bit hash of the string - used in FormSignature and unit-tests. 125 static std::string Hash64Bit(const std::string& str); 126 127 enum EncodeRequestType { 128 QUERY, 129 UPLOAD, 130 }; 131 132 // Associates the field with the heuristic type for each of the field views. 133 void GetHeuristicFieldInfo(FieldTypeMap* field_types_map); 134 135 // Adds form info to |encompassing_xml_element|. |request_type| indicates if 136 // it is a query or upload. 137 bool EncodeFormRequest(EncodeRequestType request_type, 138 buzz::XmlElement* encompassing_xml_element) const; 139 140 // Helper for EncodeUploadRequest() that collects presense of all data in the 141 // form structure and converts it to string for uploading. 142 std::string ConvertPresenceBitsToString() const; 143 144 // The name of the form. 145 string16 form_name_; 146 147 // The source URL. 148 GURL source_url_; 149 150 // The target URL. 151 GURL target_url_; 152 153 bool has_credit_card_field_; 154 bool has_autofillable_field_; 155 bool has_password_fields_; 156 157 // The number of fields able to be auto-filled. 158 size_t autofill_count_; 159 160 // A vector of all the input fields in the form. The vector is terminated by 161 // a NULL entry. 162 ScopedVector<AutofillField> fields_; 163 164 // The names of the form input elements, that are part of the form signature. 165 // The string starts with "&" and the names are also separated by the "&" 166 // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name" 167 std::string form_signature_field_names_; 168 169 // The server experiment corresponding to the server types returned for this 170 // form. 171 std::string server_experiment_id_; 172 173 // GET or POST. 174 RequestMethod method_; 175 176 DISALLOW_COPY_AND_ASSIGN(FormStructure); 177 }; 178 179 #endif // CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_ 180