form_field.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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/field_types.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                              ServerFieldTypeMap* 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_TEXT_AREA  = 1 << 7,
47    MATCH_ALL_INPUTS =
48        MATCH_TEXT | MATCH_EMAIL | MATCH_TELEPHONE | MATCH_SELECT |
49        MATCH_TEXT_AREA,
50
51    // By default match label and name for input/text types.
52    MATCH_DEFAULT    = MATCH_LABEL | MATCH_NAME | MATCH_VALUE | MATCH_TEXT,
53  };
54
55  // Only derived classes may instantiate.
56  FormField() {}
57
58  // Attempts to parse a form field with the given pattern.  Returns true on
59  // success and fills |match| with a pointer to the field.
60  static bool ParseField(AutofillScanner* scanner,
61                         const base::string16& pattern,
62                         const AutofillField** match);
63
64  // Parses the stream of fields in |scanner| with regular expression |pattern|
65  // as specified in the |match_type| bit field (see |MatchType|).  If |match|
66  // is non-NULL and the pattern matches, the matched field is returned.
67  // A |true| result is returned in the case of a successful match, false
68  // otherwise.
69  static bool ParseFieldSpecifics(AutofillScanner* scanner,
70                                  const base::string16& pattern,
71                                  int match_type,
72                                  const AutofillField** match);
73
74  // Attempts to parse a field with an empty label.  Returns true
75  // on success and fills |match| with a pointer to the field.
76  static bool ParseEmptyLabel(AutofillScanner* scanner,
77                              const AutofillField** match);
78
79  // Adds an association between a field and a type to |map|.
80  static bool AddClassification(const AutofillField* field,
81                                ServerFieldType type,
82                                ServerFieldTypeMap* map);
83
84  // Derived classes must implement this interface to supply field type
85  // information.  |ParseFormFields| coordinates the parsing and extraction
86  // of types from an input vector of |AutofillField| objects and delegates
87  // the type extraction via this method.
88  virtual bool ClassifyField(ServerFieldTypeMap* map) const = 0;
89
90 private:
91  FRIEND_TEST_ALL_PREFIXES(FormFieldTest, Match);
92
93  // Function pointer type for the parsing function that should be passed to the
94  // ParseFormFieldsPass() helper function.
95  typedef FormField* ParseFunction(AutofillScanner* scanner);
96
97  // Matches |pattern| to the contents of the field at the head of the
98  // |scanner|.
99  // Returns |true| if a match is found according to |match_type|, and |false|
100  // otherwise.
101  static bool MatchAndAdvance(AutofillScanner* scanner,
102                              const base::string16& pattern,
103                              int match_type,
104                              const AutofillField** match);
105
106  // Matches the regular expression |pattern| against the components of |field|
107  // as specified in the |match_type| bit field (see |MatchType|).
108  static bool Match(const AutofillField* field,
109                    const base::string16& pattern,
110                    int match_type);
111
112  // Perform a "pass" over the |fields| where each pass uses the supplied
113  // |parse| method to match content to a given field type.
114  // |fields| is both an input and an output parameter.  Upon exit |fields|
115  // holds any remaining unclassified fields for further processing.
116  // Classification results of the processed fields are stored in |map|.
117  static void ParseFormFieldsPass(ParseFunction parse,
118                                  std::vector<const AutofillField*>* fields,
119                                  ServerFieldTypeMap* map);
120
121  // Returns true iff |type| matches |match_type|.
122  static bool MatchesFormControlType(const std::string& type, int match_type);
123
124  DISALLOW_COPY_AND_ASSIGN(FormField);
125};
126
127}  // namespace autofill
128
129#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_FIELD_H_
130