form_structure.h revision dc0f95d653279beabeb9817299e2902918ba123e
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/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 19namespace buzz { 20 class XmlElement; 21} // namespace buzz 22 23enum RequestMethod { 24 GET, 25 POST 26}; 27 28enum UploadRequired { 29 UPLOAD_NOT_REQUIRED, 30 UPLOAD_REQUIRED, 31 USE_UPLOAD_RATES 32}; 33 34class 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. 38class FormStructure { 39 public: 40 explicit FormStructure(const webkit_glue::FormData& form); 41 virtual ~FormStructure(); 42 43 // Encodes the XML upload request from this FormStructure. 44 bool EncodeUploadRequest(bool auto_fill_used, std::string* encoded_xml) const; 45 46 // Encodes the XML query request for the set of forms. 47 // All fields are returned in one XML. For example, there are three forms, 48 // with 2, 4, and 3 fields. The returned XML would have type info for 9 49 // fields, first two of which would be for the first form, next 4 for the 50 // second, and the rest is for the third. 51 static bool EncodeQueryRequest(const ScopedVector<FormStructure>& forms, 52 std::vector<std::string>* encoded_signatures, 53 std::string* encoded_xml); 54 55 // Parses the field types from the server query response. |forms| must be the 56 // same as the one passed to EncodeQueryRequest when constructing the query. 57 static void ParseQueryResponse(const std::string& response_xml, 58 const std::vector<FormStructure*>& forms, 59 UploadRequired* upload_required, 60 const AutofillMetrics& metric_logger); 61 62 // The unique signature for this form, composed of the target url domain, 63 // the form name, and the form field names in a 64-bit hash. 64 std::string FormSignature() const; 65 66 // Runs a quick heuristic to rule out forms that are obviously not 67 // auto-fillable, like google/yahoo/msn search, etc. The requirement that the 68 // form's method be POST is only applied if |require_method_post| is true. 69 bool IsAutoFillable(bool require_method_post) const; 70 71 // Returns true if at least one of the form fields relevant for AutoFill 72 // is not empty. 73 bool HasAutoFillableValues() 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 possible types for the field at |index|. 87 void set_possible_types(int index, const FieldTypeSet& types); 88 89 const AutofillField* field(int index) const; 90 size_t field_count() const; 91 92 // Returns the number of fields that are able to be autofilled. 93 size_t autofill_count() const { return autofill_count_; } 94 95 // Used for iterating over the fields. 96 std::vector<AutofillField*>::const_iterator begin() const { 97 return fields_.begin(); 98 } 99 std::vector<AutofillField*>::const_iterator end() const { 100 return fields_.end(); 101 } 102 103 const GURL& source_url() const { return source_url_; } 104 105 virtual std::string server_experiment_id() const; 106 107 bool operator==(const webkit_glue::FormData& form) const; 108 bool operator!=(const webkit_glue::FormData& form) const; 109 110 protected: 111 // For tests. 112 ScopedVector<AutofillField>* fields() { return &fields_; } 113 114 private: 115 friend class FormStructureTest; 116 // 64-bit hash of the string - used in FormSignature and unit-tests. 117 static std::string Hash64Bit(const std::string& str); 118 119 enum EncodeRequestType { 120 QUERY, 121 UPLOAD, 122 }; 123 124 // Runs several heuristics against the form fields to determine their possible 125 // types. 126 void GetHeuristicAutofillTypes(); 127 128 // Associates the field with the heuristic type for each of the field views. 129 void GetHeuristicFieldInfo(FieldTypeMap* field_types_map); 130 131 // Adds form info to |encompassing_xml_element|. |request_type| indicates if 132 // it is a query or upload. 133 bool EncodeFormRequest(EncodeRequestType request_type, 134 buzz::XmlElement* encompassing_xml_element) const; 135 136 // Helper for EncodeUploadRequest() that collects presense of all data in the 137 // form structure and converts it to string for uploading. 138 std::string ConvertPresenceBitsToString() const; 139 140 // The name of the form. 141 string16 form_name_; 142 143 // The source URL. 144 GURL source_url_; 145 146 // The target URL. 147 GURL target_url_; 148 149 bool has_credit_card_field_; 150 bool has_autofillable_field_; 151 bool has_password_fields_; 152 153 // The number of fields able to be auto-filled. 154 size_t autofill_count_; 155 156 // A vector of all the input fields in the form. The vector is terminated by 157 // a NULL entry. 158 ScopedVector<AutofillField> fields_; 159 160 // The names of the form input elements, that are part of the form signature. 161 // The string starts with "&" and the names are also separated by the "&" 162 // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name" 163 std::string form_signature_field_names_; 164 165 // The server experiment corresponding to the server types returned for this 166 // form. 167 std::string server_experiment_id_; 168 169 // GET or POST. 170 RequestMethod method_; 171 172 DISALLOW_COPY_AND_ASSIGN(FormStructure); 173}; 174 175#endif // CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_ 176