phone_number.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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_NUMBER_H_
6#define CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
7#pragma once
8
9#include <vector>
10
11#include "base/string16.h"
12#include "base/gtest_prod_util.h"
13#include "chrome/browser/autofill/form_group.h"
14
15// A form group that stores phone number information.
16class PhoneNumber : public FormGroup {
17 public:
18  PhoneNumber();
19  virtual ~PhoneNumber();
20
21  // FormGroup implementation:
22  virtual FormGroup* Clone() const = 0;
23  virtual void GetPossibleFieldTypes(const string16& text,
24                                     FieldTypeSet* possible_types) const;
25  virtual void GetAvailableFieldTypes(FieldTypeSet* available_types) const;
26  virtual void FindInfoMatches(const AutoFillType& type,
27                               const string16& info,
28                               std::vector<string16>* matched_text) const;
29  virtual string16 GetFieldText(const AutoFillType& type) const;
30  virtual void SetInfo(const AutoFillType& type, const string16& value);
31
32  // Parses |value| to extract the components of a phone number.  |number|
33  // returns the trailing 7 digits, |city_code| returns the next 3 digits, and
34  // |country_code| returns any remaining digits.
35  // Separator characters are stripped before parsing the digits.
36  // Returns true if parsing was successful, false otherwise.
37  static bool ParsePhoneNumber(const string16& value,
38                               string16* number,
39                               string16* city_code,
40                               string16* country_code);
41
42  // Size and offset of the prefix and suffix portions of phone numbers.
43  static const int kPrefixOffset = 0;
44  static const int kPrefixLength = 3;
45  static const int kSuffixOffset = 3;
46  static const int kSuffixLength = 4;
47
48 protected:
49  explicit PhoneNumber(const PhoneNumber& phone_number);
50
51 private:
52  FRIEND_TEST_ALL_PREFIXES(PhoneNumberTest, Matcher);
53
54  void operator=(const PhoneNumber& phone_number);
55
56  const string16& country_code() const { return country_code_; }
57  const string16& city_code() const { return city_code_; }
58  const string16& number() const { return number_; }
59  const string16& extension() const { return extension_; }
60  string16 CityAndNumber() const { return city_code_ + number_; }
61
62  // Returns the entire phone number as a string, without punctuation.
63  virtual string16 WholeNumber() const;
64
65  void set_country_code(const string16& country_code) {
66    country_code_ = country_code;
67  }
68  void set_city_code(const string16& city_code) { city_code_ = city_code; }
69  void set_number(const string16& number);
70  void set_extension(const string16& extension) { extension_ = extension; }
71  void set_whole_number(const string16& whole_number);
72
73  // A helper function for FindInfoMatches that only handles matching the info
74  // with the requested field type.
75  bool FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup,
76                             const string16& info,
77                             string16* match) const;
78
79  // The numbers will be digits only (no punctuation), so any call to the IsX()
80  // functions should first call StripPunctuation on the text.
81  bool IsNumber(const string16& text) const;
82  bool IsCityCode(const string16& text) const;
83  bool IsCountryCode(const string16& text) const;
84  bool IsCityAndNumber(const string16& text) const;
85  bool IsWholeNumber(const string16& text) const;
86
87  // The following functions should return the field type for each part of the
88  // phone number.  Currently, these are either fax or home phone number types.
89  virtual AutoFillFieldType GetNumberType() const = 0;
90  virtual AutoFillFieldType GetCityCodeType() const = 0;
91  virtual AutoFillFieldType GetCountryCodeType() const = 0;
92  virtual AutoFillFieldType GetCityAndNumberType() const = 0;
93  virtual AutoFillFieldType GetWholeNumberType() const = 0;
94
95  // Verifies that |number| is a valid phone number.
96  bool Validate(const string16& number) const;
97
98  // Removes any punctuation characters from |number|.
99  static void StripPunctuation(string16* number);
100
101  // The pieces of the phone number.
102  string16 country_code_;
103  string16 city_code_;  // city or area code.
104  string16 number_;
105  string16 extension_;
106};
107
108#endif  // CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_
109