Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / autofill / core / browser / form_field.h
blobc27dc0896501289068f544ae65f7503bf372710c
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_
8 #include <vector>
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"
16 namespace autofill {
18 class AutofillField;
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.
24 class FormField {
25 public:
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,
32 bool is_form_tag,
33 ServerFieldTypeMap* map);
35 protected:
36 // A bit-field used for matching specific parts of a field in question.
37 enum MatchType {
38 // Attributes.
39 MATCH_LABEL = 1 << 0,
40 MATCH_NAME = 1 << 1,
42 // Input types.
43 MATCH_TEXT = 1 << 2,
44 MATCH_EMAIL = 1 << 3,
45 MATCH_TELEPHONE = 1 << 4,
46 MATCH_SELECT = 1 << 5,
47 MATCH_TEXT_AREA = 1 << 6,
48 MATCH_PASSWORD = 1 << 7,
49 MATCH_NUMBER = 1 << 8,
50 MATCH_ALL_INPUTS =
51 MATCH_TEXT | MATCH_EMAIL | MATCH_TELEPHONE | MATCH_SELECT |
52 MATCH_TEXT_AREA | MATCH_PASSWORD | MATCH_NUMBER,
54 // By default match label and name for input/text types.
55 MATCH_DEFAULT = MATCH_LABEL | MATCH_NAME | MATCH_TEXT,
58 // When parsing a field's label and name separately with a given pattern:
59 enum ParseNameLabelResult {
60 RESULT_MATCH_NONE, // No match with the label or name.
61 RESULT_MATCH_LABEL, // Only the label matches the pattern.
62 RESULT_MATCH_NAME, // Only the name matches the pattern.
63 RESULT_MATCH_NAME_LABEL // Name and label both match the pattern.
66 // Only derived classes may instantiate.
67 FormField() {}
69 // Attempts to parse a form field with the given pattern. Returns true on
70 // success and fills |match| with a pointer to the field.
71 static bool ParseField(AutofillScanner* scanner,
72 const base::string16& pattern,
73 AutofillField** match);
75 // Parses the stream of fields in |scanner| with regular expression |pattern|
76 // as specified in the |match_type| bit field (see |MatchType|). If |match|
77 // is non-NULL and the pattern matches, the matched field is returned.
78 // A |true| result is returned in the case of a successful match, false
79 // otherwise.
80 static bool ParseFieldSpecifics(AutofillScanner* scanner,
81 const base::string16& pattern,
82 int match_type,
83 AutofillField** match);
85 // Like ParseFieldSpecifics(), but applies |pattern| against the name and
86 // label of the current field separately. If the return value is
87 // RESULT_MATCH_NAME_LABEL, then |scanner| advances and |match| is filled if
88 // it is non-NULL. Otherwise |scanner| does not advance and |match| does not
89 // change.
90 static ParseNameLabelResult ParseNameAndLabelSeparately(
91 AutofillScanner* scanner,
92 const base::string16& pattern,
93 int match_type,
94 AutofillField** match);
96 // Attempts to parse a field with an empty label. Returns true
97 // on success and fills |match| with a pointer to the field.
98 static bool ParseEmptyLabel(AutofillScanner* scanner, AutofillField** match);
100 // Adds an association between a field and a type to |map|.
101 static bool AddClassification(const AutofillField* field,
102 ServerFieldType type,
103 ServerFieldTypeMap* map);
105 // Returns true iff |type| matches |match_type|.
106 static bool MatchesFormControlType(const std::string& type, int match_type);
108 // Derived classes must implement this interface to supply field type
109 // information. |ParseFormFields| coordinates the parsing and extraction
110 // of types from an input vector of |AutofillField| objects and delegates
111 // the type extraction via this method.
112 virtual bool ClassifyField(ServerFieldTypeMap* map) const = 0;
114 private:
115 FRIEND_TEST_ALL_PREFIXES(FormFieldTest, Match);
117 // Function pointer type for the parsing function that should be passed to the
118 // ParseFormFieldsPass() helper function.
119 typedef scoped_ptr<FormField> ParseFunction(AutofillScanner* scanner);
121 // Matches |pattern| to the contents of the field at the head of the
122 // |scanner|.
123 // Returns |true| if a match is found according to |match_type|, and |false|
124 // otherwise.
125 static bool MatchAndAdvance(AutofillScanner* scanner,
126 const base::string16& pattern,
127 int match_type,
128 AutofillField** match);
130 // Matches the regular expression |pattern| against the components of |field|
131 // as specified in the |match_type| bit field (see |MatchType|).
132 static bool Match(const AutofillField* field,
133 const base::string16& pattern,
134 int match_type);
136 // Perform a "pass" over the |fields| where each pass uses the supplied
137 // |parse| method to match content to a given field type.
138 // |fields| is both an input and an output parameter. Upon exit |fields|
139 // holds any remaining unclassified fields for further processing.
140 // Classification results of the processed fields are stored in |map|.
141 static void ParseFormFieldsPass(ParseFunction parse,
142 std::vector<AutofillField*>* fields,
143 ServerFieldTypeMap* map);
145 DISALLOW_COPY_AND_ASSIGN(FormField);
148 } // namespace autofill
150 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FIELD_H_