OLD | NEW |
| (Empty) |
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 | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/string16.h" | |
13 #include "chrome/browser/autofill/autofill_type.h" | |
14 | |
15 class AutofillField; | |
16 class AutofillScanner; | |
17 | |
18 // Represents a logical form field in a web form. Classes that implement this | |
19 // interface can identify themselves as a particular type of form field, e.g. | |
20 // name, phone number, or address field. | |
21 class FormField { | |
22 public: | |
23 virtual ~FormField() {} | |
24 | |
25 // Classifies each field in |fields| with its heuristically detected type. | |
26 // The association is stored into |map|. Each field has a derived unique name | |
27 // that is used as the key into the |map|. | |
28 static void ParseFormFields(const std::vector<AutofillField*>& fields, | |
29 FieldTypeMap* map); | |
30 | |
31 protected: | |
32 // A bit-field used for matching specific parts of a field in question. | |
33 enum MatchType { | |
34 // Attributes. | |
35 MATCH_LABEL = 1 << 0, | |
36 MATCH_NAME = 1 << 1, | |
37 MATCH_VALUE = 1 << 2, | |
38 | |
39 // Input types. | |
40 MATCH_TEXT = 1 << 3, | |
41 MATCH_EMAIL = 1 << 4, | |
42 MATCH_TELEPHONE = 1 << 5, | |
43 MATCH_SELECT = 1 << 6, | |
44 MATCH_ALL_INPUTS = | |
45 MATCH_TEXT | MATCH_EMAIL | MATCH_TELEPHONE | MATCH_SELECT, | |
46 | |
47 // By default match label and name for input/text types. | |
48 MATCH_DEFAULT = MATCH_LABEL | MATCH_NAME | MATCH_VALUE | MATCH_TEXT, | |
49 }; | |
50 | |
51 // Only derived classes may instantiate. | |
52 FormField() {} | |
53 | |
54 // Attempts to parse a form field with the given pattern. Returns true on | |
55 // success and fills |match| with a pointer to the field. | |
56 static bool ParseField(AutofillScanner* scanner, | |
57 const string16& pattern, | |
58 const AutofillField** match); | |
59 | |
60 // Parses the stream of fields in |scanner| with regular expression |pattern| | |
61 // as specified in the |match_type| bit field (see |MatchType|). If |match| | |
62 // is non-NULL and the pattern matches, the matched field is returned. | |
63 // A |true| result is returned in the case of a successful match, false | |
64 // otherwise. | |
65 static bool ParseFieldSpecifics(AutofillScanner* scanner, | |
66 const string16& pattern, | |
67 int match_type, | |
68 const AutofillField** match); | |
69 | |
70 // Attempts to parse a field with an empty label. Returns true | |
71 // on success and fills |match| with a pointer to the field. | |
72 static bool ParseEmptyLabel(AutofillScanner* scanner, | |
73 const AutofillField** match); | |
74 | |
75 // Adds an association between a field and a type to |map|. | |
76 static bool AddClassification(const AutofillField* field, | |
77 AutofillFieldType type, | |
78 FieldTypeMap* map); | |
79 | |
80 // Derived classes must implement this interface to supply field type | |
81 // information. |ParseFormFields| coordinates the parsing and extraction | |
82 // of types from an input vector of |AutofillField| objects and delegates | |
83 // the type extraction via this method. | |
84 virtual bool ClassifyField(FieldTypeMap* map) const = 0; | |
85 | |
86 private: | |
87 FRIEND_TEST_ALL_PREFIXES(FormFieldTest, Match); | |
88 | |
89 // Function pointer type for the parsing function that should be passed to the | |
90 // ParseFormFieldsPass() helper function. | |
91 typedef FormField* ParseFunction(AutofillScanner* scanner); | |
92 | |
93 // Matches |pattern| to the contents of the field at the head of the | |
94 // |scanner|. | |
95 // Returns |true| if a match is found according to |match_type|, and |false| | |
96 // otherwise. | |
97 static bool MatchAndAdvance(AutofillScanner* scanner, | |
98 const string16& pattern, | |
99 int match_type, | |
100 const AutofillField** match); | |
101 | |
102 // Matches the regular expression |pattern| against the components of |field| | |
103 // as specified in the |match_type| bit field (see |MatchType|). | |
104 static bool Match(const AutofillField* field, | |
105 const string16& pattern, | |
106 int match_type); | |
107 | |
108 // Perform a "pass" over the |fields| where each pass uses the supplied | |
109 // |parse| method to match content to a given field type. | |
110 // |fields| is both an input and an output parameter. Upon exit |fields| | |
111 // holds any remaining unclassified fields for further processing. | |
112 // Classification results of the processed fields are stored in |map|. | |
113 static void ParseFormFieldsPass(ParseFunction parse, | |
114 std::vector<const AutofillField*>* fields, | |
115 FieldTypeMap* map); | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(FormField); | |
118 }; | |
119 | |
120 #endif // CHROME_BROWSER_AUTOFILL_FORM_FIELD_H_ | |
OLD | NEW |