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