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