OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_ | 5 #ifndef CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_ |
6 #define CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_ | 6 #define CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
12 #include "base/string16.h" | 13 #include "base/string16.h" |
13 #include "chrome/browser/autofill/autofill_type.h" | 14 #include "chrome/browser/autofill/autofill_type.h" |
14 #include "chrome/browser/autofill/form_group.h" | 15 #include "chrome/browser/autofill/form_group.h" |
15 | 16 |
16 // A form group that stores phone number information. | 17 // A form group that stores phone number information. |
17 class PhoneNumber : public FormGroup { | 18 class PhoneNumber : public FormGroup { |
18 public: | 19 public: |
19 PhoneNumber(); | 20 PhoneNumber(); |
20 explicit PhoneNumber(const PhoneNumber& number); | 21 explicit PhoneNumber(AutofillType::FieldTypeGroup phone_group); |
22 PhoneNumber(const PhoneNumber& number); | |
21 virtual ~PhoneNumber(); | 23 virtual ~PhoneNumber(); |
22 | 24 |
23 PhoneNumber& operator=(const PhoneNumber& number); | 25 PhoneNumber& operator=(const PhoneNumber& number); |
24 | 26 |
25 // FormGroup implementation: | 27 // FormGroup implementation: |
26 virtual void GetMatchingTypes(const string16& text, | 28 virtual void GetMatchingTypes(const string16& text, |
27 FieldTypeSet* matching_types) const; | 29 FieldTypeSet* matching_types) const; |
28 virtual void GetNonEmptyTypes(FieldTypeSet* non_empty_typess) const; | 30 virtual void GetNonEmptyTypes(FieldTypeSet* non_empty_typess) const; |
29 virtual string16 GetInfo(AutofillFieldType type) const; | 31 virtual string16 GetInfo(AutofillFieldType type) const; |
30 virtual void SetInfo(AutofillFieldType type, const string16& value); | 32 virtual void SetInfo(AutofillFieldType type, const string16& value); |
31 | 33 |
32 // Parses |value| to extract the components of a phone number. |number| | 34 // Validates |number_| and translates it into digits-only format. |
33 // returns the trailing 7 digits, |city_code| returns the next 3 digits, and | 35 // Locale must be set. |
34 // |country_code| returns any remaining digits. | 36 bool NormalizePhone(); |
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 | 37 |
42 // Size and offset of the prefix and suffix portions of phone numbers. | 38 // Size and offset of the prefix and suffix portions of phone numbers. |
43 static const int kPrefixOffset = 0; | 39 static const int kPrefixOffset = 0; |
44 static const int kPrefixLength = 3; | 40 static const int kPrefixLength = 3; |
45 static const int kSuffixOffset = 3; | 41 static const int kSuffixOffset = 3; |
46 static const int kSuffixLength = 4; | 42 static const int kSuffixLength = 4; |
47 | 43 |
44 // Sets locale for normalizing phone numbers. Must be called if you get | |
45 // normalized number or use NormalizePhone() function; | |
46 // Setting it to "", actually sets it to default locale - "US". | |
47 void set_locale(const std::string& locale); | |
48 | |
48 // The following functions should return the field type for each part of the | 49 // The following functions should return the field type for each part of the |
49 // phone number. Currently, these are either fax or home phone number types. | 50 // phone number. Currently, these are either fax or home phone number types. |
50 virtual AutofillFieldType GetNumberType() const = 0; | 51 AutofillFieldType GetNumberType() const; |
51 virtual AutofillFieldType GetCityCodeType() const = 0; | 52 AutofillFieldType GetCityCodeType() const; |
52 virtual AutofillFieldType GetCountryCodeType() const = 0; | 53 AutofillFieldType GetCountryCodeType() const; |
53 virtual AutofillFieldType GetCityAndNumberType() const = 0; | 54 AutofillFieldType GetCityAndNumberType() const; |
54 virtual AutofillFieldType GetWholeNumberType() const = 0; | 55 AutofillFieldType GetWholeNumberType() const; |
56 | |
57 // The class used to combine home phone or fax parts into a whole number. | |
58 class PhoneCombineHelper { | |
59 public: | |
60 explicit PhoneCombineHelper(AutofillType::FieldTypeGroup phone_group) | |
61 : phone_group_(phone_group) { | |
62 } | |
isherman%chromium.org_gtempaccount.com
2011/05/18 18:21:45
nit: Please move this curly brace onto the previou
GeorgeY
2011/05/19 00:30:27
Sure, less readable, but sure.
| |
63 // Sets PHONE_HOME/FAX_CITY_CODE, PHONE_HOME/FAX_COUNTRY_CODE, | |
64 // PHONE_HOME/FAX_CITY_AND_NUMBER, PHONE_HOME/FAX_NUMBER and returns true. | |
65 // For all other field types returs false. | |
66 bool SetInfo(AutofillFieldType type, const string16& value); | |
67 // Returns true if parsing was successful, false otherwise. | |
68 bool ParseNumber(const std::string& locale, string16* value); | |
69 | |
70 bool empty() const { | |
71 return phone_.empty(); | |
72 } | |
isherman%chromium.org_gtempaccount.com
2011/05/18 18:21:45
nit: Please condense this function prototype + bod
GeorgeY
2011/05/19 00:30:27
Sure, less readable, but sure.
| |
73 private: | |
74 string16 country_; | |
75 string16 city_; | |
76 string16 phone_; | |
77 AutofillType::FieldTypeGroup phone_group_; | |
78 }; | |
55 | 79 |
56 private: | 80 private: |
57 FRIEND_TEST_ALL_PREFIXES(PhoneNumberTest, Matcher); | 81 FRIEND_TEST_ALL_PREFIXES(PhoneNumberTest, Matcher); |
58 | 82 |
59 const string16& country_code() const { return country_code_; } | |
60 const string16& city_code() const { return city_code_; } | |
61 const string16& number() const { return number_; } | 83 const string16& number() const { return number_; } |
62 const string16& extension() const { return extension_; } | 84 const string16& extension() const { return extension_; } |
63 string16 CityAndNumber() const { return city_code_ + number_; } | |
64 | 85 |
65 // Returns the entire phone number as a string, without punctuation. | |
66 virtual string16 WholeNumber() const; | |
67 | |
68 void set_country_code(const string16& country_code) { | |
69 country_code_ = country_code; | |
70 } | |
71 void set_city_code(const string16& city_code) { city_code_ = city_code; } | |
72 void set_number(const string16& number); | 86 void set_number(const string16& number); |
73 void set_extension(const string16& extension) { extension_ = extension; } | 87 void set_extension(const string16& extension) { extension_ = extension; } |
74 void set_whole_number(const string16& whole_number); | |
75 | 88 |
76 // The numbers will be digits only (no punctuation), so any call to the IsX() | 89 // The numbers will be digits only (no punctuation), so any call to the IsX() |
77 // functions should first call StripPunctuation on the text. | 90 // functions should first call StripPunctuation on the text. |
78 bool IsNumber(const string16& text) const; | 91 bool IsNumber(const string16& text, const string16& number) const; |
79 bool IsCityCode(const string16& text) const; | |
80 bool IsCountryCode(const string16& text) const; | |
81 bool IsCityAndNumber(const string16& text) const; | |
82 bool IsWholeNumber(const string16& text) const; | 92 bool IsWholeNumber(const string16& text) const; |
83 | 93 |
84 // Verifies that |number| is a valid phone number. | |
85 bool Validate(const string16& number) const; | |
86 | |
87 // Removes any punctuation characters from |number|. | |
88 static void StripPunctuation(string16* number); | 94 static void StripPunctuation(string16* number); |
89 | 95 |
96 // Phone group - currently it is PHONE_HOME and PHONE_FAX. | |
97 AutofillType::FieldTypeGroup phone_group_; | |
98 // Locale for phone normalizing. | |
99 std::string locale_; | |
90 // The pieces of the phone number. | 100 // The pieces of the phone number. |
91 string16 country_code_; | |
92 string16 city_code_; // city or area code. | |
93 string16 number_; | 101 string16 number_; |
94 string16 extension_; | 102 string16 extension_; |
95 }; | 103 }; |
96 | 104 |
97 #endif // CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_ | 105 #endif // CHROME_BROWSER_AUTOFILL_PHONE_NUMBER_H_ |
OLD | NEW |