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