1 // Copyright 2013 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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FIELD_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FIELD_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "components/autofill/core/browser/field_types.h"
19 class AutofillScanner
;
21 // Represents a logical form field in a web form. Classes that implement this
22 // interface can identify themselves as a particular type of form field, e.g.
23 // name, phone number, or address field.
26 virtual ~FormField() {}
28 // Classifies each field in |fields| with its heuristically detected type.
29 // The association is stored into |map|. Each field has a derived unique name
30 // that is used as the key into the |map|.
31 static void ParseFormFields(const std::vector
<AutofillField
*>& fields
,
33 ServerFieldTypeMap
* map
);
36 // A bit-field used for matching specific parts of a field in question.
46 MATCH_TELEPHONE
= 1 << 5,
47 MATCH_SELECT
= 1 << 6,
48 MATCH_TEXT_AREA
= 1 << 7,
49 MATCH_PASSWORD
= 1 << 8,
51 MATCH_TEXT
| MATCH_EMAIL
| MATCH_TELEPHONE
| MATCH_SELECT
|
52 MATCH_TEXT_AREA
| MATCH_PASSWORD
,
54 // By default match label and name for input/text types.
55 MATCH_DEFAULT
= MATCH_LABEL
| MATCH_NAME
| MATCH_VALUE
| MATCH_TEXT
,
58 // Only derived classes may instantiate.
61 // Attempts to parse a form field with the given pattern. Returns true on
62 // success and fills |match| with a pointer to the field.
63 static bool ParseField(AutofillScanner
* scanner
,
64 const base::string16
& pattern
,
65 AutofillField
** match
);
67 // Parses the stream of fields in |scanner| with regular expression |pattern|
68 // as specified in the |match_type| bit field (see |MatchType|). If |match|
69 // is non-NULL and the pattern matches, the matched field is returned.
70 // A |true| result is returned in the case of a successful match, false
72 static bool ParseFieldSpecifics(AutofillScanner
* scanner
,
73 const base::string16
& pattern
,
75 AutofillField
** match
);
77 // Attempts to parse a field with an empty label. Returns true
78 // on success and fills |match| with a pointer to the field.
79 static bool ParseEmptyLabel(AutofillScanner
* scanner
, AutofillField
** match
);
81 // Adds an association between a field and a type to |map|.
82 static bool AddClassification(const AutofillField
* field
,
84 ServerFieldTypeMap
* map
);
86 // Derived classes must implement this interface to supply field type
87 // information. |ParseFormFields| coordinates the parsing and extraction
88 // of types from an input vector of |AutofillField| objects and delegates
89 // the type extraction via this method.
90 virtual bool ClassifyField(ServerFieldTypeMap
* map
) const = 0;
93 FRIEND_TEST_ALL_PREFIXES(FormFieldTest
, Match
);
95 // Function pointer type for the parsing function that should be passed to the
96 // ParseFormFieldsPass() helper function.
97 typedef scoped_ptr
<FormField
> ParseFunction(AutofillScanner
* scanner
);
99 // Matches |pattern| to the contents of the field at the head of the
101 // Returns |true| if a match is found according to |match_type|, and |false|
103 static bool MatchAndAdvance(AutofillScanner
* scanner
,
104 const base::string16
& pattern
,
106 AutofillField
** match
);
108 // Matches the regular expression |pattern| against the components of |field|
109 // as specified in the |match_type| bit field (see |MatchType|).
110 static bool Match(const AutofillField
* field
,
111 const base::string16
& pattern
,
114 // Perform a "pass" over the |fields| where each pass uses the supplied
115 // |parse| method to match content to a given field type.
116 // |fields| is both an input and an output parameter. Upon exit |fields|
117 // holds any remaining unclassified fields for further processing.
118 // Classification results of the processed fields are stored in |map|.
119 static void ParseFormFieldsPass(ParseFunction parse
,
120 std::vector
<AutofillField
*>* fields
,
121 ServerFieldTypeMap
* map
);
123 // Returns true iff |type| matches |match_type|.
124 static bool MatchesFormControlType(const std::string
& type
, int match_type
);
126 DISALLOW_COPY_AND_ASSIGN(FormField
);
129 } // namespace autofill
131 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FIELD_H_