[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/autofill/phone_number.h" |
| 6 | |
| 7 | #include "base/basictypes.h" |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 8 | #include "base/string_number_conversions.h" |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 9 | #include "base/string_util.h" |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 11 | #include "chrome/browser/autofill/autofill_profile.h" |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 12 | #include "chrome/browser/autofill/autofill_type.h" |
| 13 | #include "chrome/browser/autofill/field_types.h" |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 14 | #include "chrome/browser/autofill/phone_number_i18n.h" |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 15 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 16 | namespace { |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 17 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 18 | const char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 }; |
| 19 | |
| 20 | // The number of digits in a phone number. |
| 21 | const size_t kPhoneNumberLength = 7; |
| 22 | |
| 23 | // The number of digits in an area code. |
| 24 | const size_t kPhoneCityCodeLength = 3; |
| 25 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 26 | const AutofillType::FieldTypeSubGroup kAutofillPhoneTypes[] = { |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 27 | AutofillType::PHONE_NUMBER, |
| 28 | AutofillType::PHONE_CITY_CODE, |
| 29 | AutofillType::PHONE_COUNTRY_CODE, |
| 30 | AutofillType::PHONE_CITY_AND_NUMBER, |
| 31 | AutofillType::PHONE_WHOLE_NUMBER, |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 32 | }; |
| 33 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 34 | const int kAutofillPhoneLength = arraysize(kAutofillPhoneTypes); |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 35 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 36 | void StripPunctuation(string16* number) { |
| 37 | RemoveChars(*number, kPhoneNumberSeparators, number); |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 40 | } // namespace |
| 41 | |
| 42 | PhoneNumber::PhoneNumber(AutofillProfile* profile) |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 43 | : profile_(profile) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 44 | } |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 45 | |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 46 | PhoneNumber::PhoneNumber(const PhoneNumber& number) : FormGroup() { |
| 47 | *this = number; |
| 48 | } |
| 49 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 50 | PhoneNumber::~PhoneNumber() {} |
| 51 | |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 52 | PhoneNumber& PhoneNumber::operator=(const PhoneNumber& number) { |
| 53 | if (this == &number) |
| 54 | return *this; |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 55 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 56 | number_ = number.number_; |
| 57 | profile_ = number.profile_; |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 58 | cached_parsed_phone_ = number.cached_parsed_phone_; |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 59 | return *this; |
| 60 | } |
| 61 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 62 | void PhoneNumber::GetSupportedTypes(FieldTypeSet* supported_types) const { |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 63 | supported_types->insert(PHONE_HOME_WHOLE_NUMBER); |
| 64 | supported_types->insert(PHONE_HOME_NUMBER); |
| 65 | supported_types->insert(PHONE_HOME_CITY_CODE); |
| 66 | supported_types->insert(PHONE_HOME_CITY_AND_NUMBER); |
| 67 | supported_types->insert(PHONE_HOME_COUNTRY_CODE); |
[email protected] | cea1d11 | 2010-07-01 00:57:33 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 70 | string16 PhoneNumber::GetInfo(AutofillFieldType type) const { |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 71 | if (type == PHONE_HOME_WHOLE_NUMBER) |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 72 | return number_; |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 73 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 74 | UpdateCacheIfNeeded(); |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 75 | if (!cached_parsed_phone_.IsValidNumber()) |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 76 | return string16(); |
| 77 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 78 | if (type == PHONE_HOME_NUMBER) |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 79 | return cached_parsed_phone_.GetNumber(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 80 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 81 | if (type == PHONE_HOME_CITY_CODE) |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 82 | return cached_parsed_phone_.GetCityCode(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 83 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 84 | if (type == PHONE_HOME_COUNTRY_CODE) |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 85 | return cached_parsed_phone_.GetCountryCode(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 86 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 87 | if (type == PHONE_HOME_CITY_AND_NUMBER) { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 88 | string16 city_and_local(cached_parsed_phone_.GetCityCode()); |
| 89 | city_and_local.append(cached_parsed_phone_.GetNumber()); |
[email protected] | 6f113af | 2011-06-03 05:10:43 | [diff] [blame] | 90 | return city_and_local; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 91 | } |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 92 | |
[email protected] | cea1d11 | 2010-07-01 00:57:33 | [diff] [blame] | 93 | return string16(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 96 | void PhoneNumber::SetInfo(AutofillFieldType type, const string16& value) { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 97 | FieldTypeSubGroup subgroup = AutofillType(type).subgroup(); |
| 98 | if (subgroup != AutofillType::PHONE_CITY_AND_NUMBER && |
| 99 | subgroup != AutofillType::PHONE_WHOLE_NUMBER) { |
[email protected] | 08f691b | 2011-07-26 23:22:03 | [diff] [blame] | 100 | // Only full phone numbers should be set directly. The remaining field |
| 101 | // field types are read-only. |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
| 105 | number_ = value; |
| 106 | cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, locale()); |
| 107 | } |
| 108 | |
| 109 | // Normalize phones if |type| is a whole number: |
| 110 | // (650)2345678 -> 6502345678 |
| 111 | // 1-800-FLOWERS -> 18003569377 |
| 112 | // If the phone cannot be normalized, returns the stored value verbatim. |
| 113 | string16 PhoneNumber::GetCanonicalizedInfo(AutofillFieldType type) const { |
| 114 | string16 phone = GetInfo(type); |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 115 | if (type != PHONE_HOME_WHOLE_NUMBER) |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 116 | return phone; |
| 117 | |
| 118 | string16 normalized_phone = autofill_i18n::NormalizePhoneNumber(phone, |
| 119 | locale()); |
| 120 | if (!normalized_phone.empty()) |
| 121 | return normalized_phone; |
| 122 | |
| 123 | return phone; |
| 124 | } |
| 125 | |
| 126 | bool PhoneNumber::SetCanonicalizedInfo(AutofillFieldType type, |
| 127 | const string16& value) { |
| 128 | string16 number = value; |
| 129 | StripPunctuation(&number); |
| 130 | SetInfo(type, number); |
| 131 | |
| 132 | return NormalizePhone(); |
| 133 | } |
| 134 | |
| 135 | void PhoneNumber::GetMatchingTypes(const string16& text, |
| 136 | FieldTypeSet* matching_types) const { |
| 137 | string16 stripped_text = text; |
| 138 | StripPunctuation(&stripped_text); |
| 139 | FormGroup::GetMatchingTypes(stripped_text, matching_types); |
| 140 | |
| 141 | // For US numbers, also compare to the three-digit prefix and the four-digit |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 142 | // suffix, since web sites often split numbers into these two fields. |
| 143 | string16 number = GetCanonicalizedInfo(PHONE_HOME_NUMBER); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 144 | if (locale() == "US" && number.size() == (kPrefixLength + kSuffixLength)) { |
| 145 | string16 prefix = number.substr(kPrefixOffset, kPrefixLength); |
| 146 | string16 suffix = number.substr(kSuffixOffset, kSuffixLength); |
| 147 | if (text == prefix || text == suffix) |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 148 | matching_types->insert(PHONE_HOME_NUMBER); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 151 | string16 whole_number = GetCanonicalizedInfo(PHONE_HOME_WHOLE_NUMBER); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 152 | if (!whole_number.empty() && |
| 153 | autofill_i18n::NormalizePhoneNumber(text, locale()) == whole_number) { |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 154 | matching_types->insert(PHONE_HOME_WHOLE_NUMBER); |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | bool PhoneNumber::NormalizePhone() { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 159 | // Empty number does not need normalization. |
| 160 | if (number_.empty()) |
| 161 | return true; |
| 162 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 163 | UpdateCacheIfNeeded(); |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 164 | number_ = cached_parsed_phone_.GetWholeNumber(); |
[email protected] | f467247 | 2011-05-27 07:21:01 | [diff] [blame] | 165 | return !number_.empty(); |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 168 | std::string PhoneNumber::locale() const { |
| 169 | if (!profile_) { |
| 170 | NOTREACHED(); |
| 171 | return "US"; |
| 172 | } |
| 173 | |
| 174 | return profile_->CountryCode(); |
| 175 | } |
| 176 | |
| 177 | void PhoneNumber::UpdateCacheIfNeeded() const { |
| 178 | if (!number_.empty() && cached_parsed_phone_.GetLocale() != locale()) |
| 179 | cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, locale()); |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 182 | PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { |
| 186 | } |
| 187 | |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 188 | bool PhoneNumber::PhoneCombineHelper::SetInfo(AutofillFieldType field_type, |
| 189 | const string16& value) { |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 190 | if (field_type == PHONE_HOME_COUNTRY_CODE) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 191 | country_ = value; |
| 192 | return true; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 195 | if (field_type == PHONE_HOME_CITY_CODE) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 196 | city_ = value; |
| 197 | return true; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 200 | if (field_type == PHONE_HOME_CITY_AND_NUMBER) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 201 | phone_ = value; |
| 202 | return true; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 205 | if (field_type == PHONE_HOME_WHOLE_NUMBER) { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 206 | whole_number_ = value; |
| 207 | return true; |
| 208 | } |
| 209 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame^] | 210 | if (field_type == PHONE_HOME_NUMBER) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 211 | phone_.append(value); |
| 212 | return true; |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 213 | } |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 214 | |
| 215 | return false; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 218 | bool PhoneNumber::PhoneCombineHelper::ParseNumber(const std::string& locale, |
| 219 | string16* value) { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 220 | if (!whole_number_.empty()) { |
| 221 | *value = whole_number_; |
| 222 | return true; |
| 223 | } |
| 224 | |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 225 | return autofill_i18n::ConstructPhoneNumber( |
| 226 | country_, city_, phone_, |
| 227 | locale, |
| 228 | (country_.empty() ? |
| 229 | autofill_i18n::NATIONAL : autofill_i18n::INTERNATIONAL), |
| 230 | value); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 233 | bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { |
| 234 | return phone_.empty() && whole_number_.empty(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 235 | } |