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