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_PHONE_FIELD_H_
6#define CHROME_BROWSER_AUTOFILL_PHONE_FIELD_H_
7#pragma once
8
9#include <vector>
10
11#include "base/memory/scoped_ptr.h"
12#include "chrome/browser/autofill/autofill_type.h"
13#include "chrome/browser/autofill/form_field.h"
14#include "chrome/browser/autofill/phone_number.h"
15
16class AutofillField;
17
18// A phone number in one of the following formats:
19// - area code, prefix, suffix
20// - area code, number
21// - number
22class PhoneField : public FormField {
23 public:
24  virtual ~PhoneField();
25
26  static PhoneField* Parse(std::vector<AutofillField*>::const_iterator* iter,
27                           bool is_ecml);
28  static PhoneField* ParseECML(
29      std::vector<AutofillField*>::const_iterator* iter);
30
31  virtual bool GetFieldInfo(FieldTypeMap* field_type_map) const;
32
33 private:
34  PhoneField();
35
36  enum PhoneType {
37    PHONE_TYPE_FIRST = 0,
38    HOME_PHONE = PHONE_TYPE_FIRST,
39    FAX_PHONE,
40
41    // Must be last.
42    PHONE_TYPE_MAX,
43  };
44
45  // Some field names are different for phone and fax.
46  string16 GetCountryRegex() const;
47  // This string includes all area code separators, including NoText.
48  string16 GetAreaRegex() const;
49  // Separator of the area code in the case fields are formatted without
50  // any text indicating what fields are (e.g. field1 "(" field2 ")" field3 "-"
51  // field4 means Country Code, Area Code, Prefix, Suffix)
52  string16 GetAreaNoTextRegex() const;
53  string16 GetPhoneRegex() const;
54  string16 GetPrefixSeparatorRegex() const;
55  string16 GetPrefixRegex() const;
56  string16 GetSuffixSeparatorRegex() const;
57  string16 GetSuffixRegex() const;
58  string16 GetExtensionRegex() const;
59
60  // This is for easy description of the possible parsing paths of the phone
61  // fields.
62  enum RegexType {
63    REGEX_COUNTRY,
64    REGEX_AREA,
65    REGEX_AREA_NOTEXT,
66    REGEX_PHONE,
67    REGEX_PREFIX_SEPARATOR,
68    REGEX_PREFIX,
69    REGEX_SUFFIX_SEPARATOR,
70    REGEX_SUFFIX,
71    REGEX_EXTENSION,
72
73    // Separates regexps in grammar.
74    REGEX_SEPARATOR,
75  };
76
77  string16 GetRegExp(RegexType regex_id) const;
78
79  // |field| - field to fill up on successful parsing.
80  // |iter| - in/out. Form field iterator, points to the first field that is
81  //   attempted to be parsed. If parsing successful, points to the first field
82  //   after parsed fields.
83  // |regular_phone| - true if the parsed phone is a HOME phone, false
84  //   otherwise.
85  static bool ParseInternal(PhoneField* field,
86                            std::vector<AutofillField*>::const_iterator* iter,
87                            bool regular_phone);
88
89  void SetPhoneType(PhoneType phone_type);
90
91  // Field types are different as well, so we create a temporary phone number,
92  // to get relevant field types.
93  scoped_ptr<PhoneNumber> number_;
94  PhoneType phone_type_;
95
96
97  // Parsed fields.
98  enum PhonePart {
99    FIELD_NONE = -1,
100    FIELD_COUNTRY_CODE,
101    FIELD_AREA_CODE,
102    FIELD_PHONE,
103    FIELD_SUFFIX,
104    FIELD_EXTENSION,
105
106    FIELD_MAX,
107  };
108
109  // FIELD_PHONE is always present; holds suffix if prefix is present.
110  // The rest could be NULL.
111  AutofillField* parsed_phone_fields_[FIELD_MAX];
112
113  static struct Parser {
114    RegexType regex;       // Field matching reg-ex.
115    PhonePart phone_part;  // Index of the field.
116    int max_size;          // Max size of the field to match. 0 means any.
117  } phone_field_grammars_[];
118
119  DISALLOW_COPY_AND_ASSIGN(PhoneField);
120};
121
122#endif  // CHROME_BROWSER_AUTOFILL_PHONE_FIELD_H_
123