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_FIELD_H_ 6 #define CHROME_BROWSER_AUTOFILL_FORM_FIELD_H_ 7 #pragma once 8 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/string16.h" 13 #include "chrome/browser/autofill/autofill_type.h" 14 15 class AutofillField; 16 class FormStructure; 17 18 extern const char kEcmlShipToTitle[]; 19 extern const char kEcmlShipToFirstName[]; 20 extern const char kEcmlShipToMiddleName[]; 21 extern const char kEcmlShipToLastName[]; 22 extern const char kEcmlShipToNameSuffix[]; 23 extern const char kEcmlShipToCompanyName[]; 24 extern const char kEcmlShipToAddress1[]; 25 extern const char kEcmlShipToAddress2[]; 26 extern const char kEcmlShipToAddress3[]; 27 extern const char kEcmlShipToCity[]; 28 extern const char kEcmlShipToStateProv[]; 29 extern const char kEcmlShipToPostalCode[]; 30 extern const char kEcmlShipToCountry[]; 31 extern const char kEcmlShipToPhone[]; 32 extern const char kEcmlShipToEmail[]; 33 34 // billing name/address fields 35 extern const char kEcmlBillToTitle[]; 36 extern const char kEcmlBillToFirstName[]; 37 extern const char kEcmlBillToMiddleName[]; 38 extern const char kEcmlBillToLastName[]; 39 extern const char kEcmlBillToNameSuffix[]; 40 extern const char kEcmlBillToCompanyName[]; 41 extern const char kEcmlBillToAddress1[]; 42 extern const char kEcmlBillToAddress2[]; 43 extern const char kEcmlBillToAddress3[]; 44 extern const char kEcmlBillToCity[]; 45 extern const char kEcmlBillToStateProv[]; 46 extern const char kEcmlBillToPostalCode[]; 47 extern const char kEcmlBillToCountry[]; 48 extern const char kEcmlBillToPhone[]; 49 extern const char kEcmlBillToEmail[]; 50 51 // credit card fields 52 extern const char kEcmlCardHolder[]; 53 extern const char kEcmlCardType[]; 54 extern const char kEcmlCardNumber[]; 55 extern const char kEcmlCardVerification[]; 56 extern const char kEcmlCardExpireDay[]; 57 extern const char kEcmlCardExpireMonth[]; 58 extern const char kEcmlCardExpireYear[]; 59 60 enum FormFieldType { 61 kAddressType, 62 kCreditCardType, 63 kOtherFieldType 64 }; 65 66 // Represents a logical form field in a web form. Classes that implement this 67 // interface can identify themselves as a particular type of form field, e.g. 68 // name, phone number, or address field. 69 class FormField { 70 public: 71 virtual ~FormField() {} 72 73 // Associates the available AutofillTypes of a FormField into 74 // |field_type_map|. 75 virtual bool GetFieldInfo(FieldTypeMap* field_type_map) const = 0; 76 77 // Returns the type of form field of the class implementing this interface. 78 virtual FormFieldType GetFormFieldType() const; 79 80 // Returns true if |field| contains the regexp |pattern| in the name or label. 81 // If |match_label_only| is true, then only the field's label is considered. 82 static bool Match(AutofillField* field, 83 const string16& pattern, 84 bool match_label_only); 85 86 // Parses a field using the different field views we know about. |is_ecml| 87 // should be true when the field conforms to the ECML specification. 88 static FormField* ParseFormField( 89 std::vector<AutofillField*>::const_iterator* field, 90 bool is_ecml); 91 92 // Attempts to parse a text field with the given pattern; returns true on 93 // success, but doesn't return the actual text field itself. 94 static bool ParseText(std::vector<AutofillField*>::const_iterator* iter, 95 const string16& pattern); 96 97 // Attempts to parse a text field with the given pattern. Returns true on 98 // success and fills |dest| with a pointer to the field. 99 static bool ParseText(std::vector<AutofillField*>::const_iterator* iter, 100 const string16& pattern, 101 AutofillField** dest); 102 103 // Attempts to parse a text field with an empty name or label. Returns true 104 // on success and fills |dest| with a pointer to the field. 105 static bool ParseEmptyText(std::vector<AutofillField*>::const_iterator* iter, 106 AutofillField** dest); 107 108 // Attempts to parse a text field label with the given pattern. Returns true 109 // on success and fills |dest| with a pointer to the field. 110 static bool ParseLabelText(std::vector<AutofillField*>::const_iterator* iter, 111 const string16& pattern, 112 AutofillField** dest); 113 114 // Attempts to parse a control with an empty label. 115 static bool ParseEmpty(std::vector<AutofillField*>::const_iterator* iter); 116 117 // Adds an association between a field and a type to |field_type_map|. 118 static bool Add(FieldTypeMap* field_type_map, AutofillField* field, 119 const AutofillType& type); 120 121 protected: 122 // Only derived classes may instantiate. 123 FormField() {} 124 125 // Note: ECML compliance checking has been modified to accommodate Google 126 // Checkout field name limitation. All ECML compliant web forms will be 127 // recognized correctly as such however the restrictions on having exactly 128 // ECML compliant names have been loosened to only require that field names 129 // be prefixed with an ECML compliant name in order to accommodate checkout. 130 // Additionally we allow the use of '.' as a word delimiter in addition to the 131 // ECML standard '_' (see FormField::FormField for details). 132 static string16 GetEcmlPattern(const char* ecml_name); 133 static string16 GetEcmlPattern(const char* ecml_name1, 134 const char* ecml_name2, 135 char pattern_operator); 136 137 private: 138 static bool ParseText(std::vector<AutofillField*>::const_iterator* iter, 139 const string16& pattern, 140 AutofillField** dest, 141 bool match_label_only); 142 143 // For empty strings we need to test that both label and name are empty. 144 static bool ParseLabelAndName( 145 std::vector<AutofillField*>::const_iterator* iter, 146 const string16& pattern, 147 AutofillField** dest); 148 149 DISALLOW_COPY_AND_ASSIGN(FormField); 150 }; 151 152 class FormFieldSet : public std::vector<FormField*> { 153 public: 154 explicit FormFieldSet(FormStructure* form); 155 156 ~FormFieldSet() { 157 for (iterator i = begin(); i != end(); ++i) 158 delete *i; 159 } 160 161 private: 162 // Checks if any of the labels are named according to the ECML standard. 163 // Returns true if at least one ECML named element is found. 164 bool CheckECML(FormStructure* fields); 165 166 DISALLOW_COPY_AND_ASSIGN(FormFieldSet); 167 }; 168 169 // Parsing utilities. 170 namespace autofill { 171 172 // Case-insensitive regular expression matching. 173 // Returns true if |pattern| is found in |input|. 174 bool MatchString(const string16& input, const string16& pattern); 175 176 } // namespace autofill 177 178 #endif // CHROME_BROWSER_AUTOFILL_FORM_FIELD_H_ 179