[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 | |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 5 | #include "components/autofill/browser/phone_number.h" |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 6 | |
| 7 | #include "base/basictypes.h" |
| 8 | #include "base/string_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 11 | #include "components/autofill/browser/autofill_country.h" |
| 12 | #include "components/autofill/browser/autofill_profile.h" |
| 13 | #include "components/autofill/browser/autofill_type.h" |
| 14 | #include "components/autofill/browser/field_types.h" |
| 15 | #include "components/autofill/browser/phone_number_i18n.h" |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 16 | |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 17 | namespace autofill { |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 18 | namespace { |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 19 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 20 | const char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 }; |
| 21 | |
| 22 | // The number of digits in a phone number. |
| 23 | const size_t kPhoneNumberLength = 7; |
| 24 | |
| 25 | // The number of digits in an area code. |
| 26 | const size_t kPhoneCityCodeLength = 3; |
| 27 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 28 | void StripPunctuation(base::string16* number) { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 29 | RemoveChars(*number, kPhoneNumberSeparators, number); |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 3de6a598 | 2013-01-10 01:45:44 | [diff] [blame] | 32 | // Returns the region code for this phone number, which is an ISO 3166 2-letter |
| 33 | // country code. The returned value is based on the |profile|; if the |profile| |
| 34 | // does not have a country code associated with it, falls back to the country |
| 35 | // code corresponding to the |app_locale|. |
| 36 | std::string GetRegion(const AutofillProfile& profile, |
| 37 | const std::string& app_locale) { |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 38 | base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY); |
[email protected] | 3de6a598 | 2013-01-10 01:45:44 | [diff] [blame] | 39 | if (!country_code.empty()) |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame] | 40 | return UTF16ToASCII(country_code); |
[email protected] | 3de6a598 | 2013-01-10 01:45:44 | [diff] [blame] | 41 | |
| 42 | return AutofillCountry::CountryCodeForLocale(app_locale); |
| 43 | } |
| 44 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 45 | } // namespace |
| 46 | |
| 47 | PhoneNumber::PhoneNumber(AutofillProfile* profile) |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 48 | : profile_(profile) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 49 | } |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 50 | |
[email protected] | 651dd5c3 | 2011-09-27 17:04:38 | [diff] [blame] | 51 | PhoneNumber::PhoneNumber(const PhoneNumber& number) |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 52 | : profile_(NULL) { |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 53 | *this = number; |
| 54 | } |
| 55 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 56 | PhoneNumber::~PhoneNumber() {} |
| 57 | |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 58 | PhoneNumber& PhoneNumber::operator=(const PhoneNumber& number) { |
| 59 | if (this == &number) |
| 60 | return *this; |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 61 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 62 | number_ = number.number_; |
| 63 | profile_ = number.profile_; |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 64 | cached_parsed_phone_ = number.cached_parsed_phone_; |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 65 | return *this; |
| 66 | } |
| 67 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 68 | void PhoneNumber::GetSupportedTypes(FieldTypeSet* supported_types) const { |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 69 | supported_types->insert(PHONE_HOME_WHOLE_NUMBER); |
| 70 | supported_types->insert(PHONE_HOME_NUMBER); |
| 71 | supported_types->insert(PHONE_HOME_CITY_CODE); |
| 72 | supported_types->insert(PHONE_HOME_CITY_AND_NUMBER); |
| 73 | supported_types->insert(PHONE_HOME_COUNTRY_CODE); |
[email protected] | cea1d11 | 2010-07-01 00:57:33 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 76 | base::string16 PhoneNumber::GetRawInfo(AutofillFieldType type) const { |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 77 | if (type == PHONE_HOME_WHOLE_NUMBER) |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 78 | return number_; |
[email protected] | 35734e9 | 2011-06-24 21:06:25 | [diff] [blame] | 79 | |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 80 | // Only the whole number is available as raw data. All of the other types are |
| 81 | // parsed from this raw info, and parsing requires knowledge of the phone |
| 82 | // number's region, which is only available via GetInfo(). |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 83 | return base::string16(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 86 | void PhoneNumber::SetRawInfo(AutofillFieldType type, |
| 87 | const base::string16& value) { |
[email protected] | 1340529 | 2011-10-07 22:37:09 | [diff] [blame] | 88 | if (type != PHONE_HOME_CITY_AND_NUMBER && |
| 89 | type != PHONE_HOME_WHOLE_NUMBER) { |
[email protected] | 08f691b | 2011-07-26 23:22:03 | [diff] [blame] | 90 | // Only full phone numbers should be set directly. The remaining field |
| 91 | // field types are read-only. |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
| 95 | number_ = value; |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 96 | |
| 97 | // Invalidate the cached number. |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 98 | cached_parsed_phone_ = i18n::PhoneObject(); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Normalize phones if |type| is a whole number: |
| 102 | // (650)2345678 -> 6502345678 |
| 103 | // 1-800-FLOWERS -> 18003569377 |
| 104 | // If the phone cannot be normalized, returns the stored value verbatim. |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 105 | base::string16 PhoneNumber::GetInfo(AutofillFieldType type, |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 106 | const std::string& app_locale) const { |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 107 | UpdateCacheIfNeeded(app_locale); |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 108 | |
| 109 | // Queries for whole numbers will return the non-normalized number if |
| 110 | // normalization for the number fails. All other field types require |
| 111 | // normalization. |
| 112 | if (type != PHONE_HOME_WHOLE_NUMBER && !cached_parsed_phone_.IsValidNumber()) |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 113 | return base::string16(); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 114 | |
| 115 | switch (type) { |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 116 | case PHONE_HOME_WHOLE_NUMBER: |
| 117 | return cached_parsed_phone_.GetWholeNumber(); |
| 118 | |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 119 | case PHONE_HOME_NUMBER: |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 120 | return cached_parsed_phone_.number(); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 121 | |
| 122 | case PHONE_HOME_CITY_CODE: |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 123 | return cached_parsed_phone_.city_code(); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 124 | |
| 125 | case PHONE_HOME_COUNTRY_CODE: |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 126 | return cached_parsed_phone_.country_code(); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 127 | |
| 128 | case PHONE_HOME_CITY_AND_NUMBER: |
| 129 | return |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 130 | cached_parsed_phone_.city_code() + cached_parsed_phone_.number(); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 131 | |
| 132 | default: |
| 133 | NOTREACHED(); |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 134 | return base::string16(); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 135 | } |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 138 | bool PhoneNumber::SetInfo(AutofillFieldType type, |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 139 | const base::string16& value, |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 140 | const std::string& app_locale) { |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 141 | SetRawInfo(type, value); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 142 | |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 143 | if (number_.empty()) |
| 144 | return true; |
| 145 | |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 146 | // Store a formatted (i.e., pretty printed) version of the number. |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 147 | UpdateCacheIfNeeded(app_locale); |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 148 | number_ = cached_parsed_phone_.GetFormattedNumber(); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 149 | return !number_.empty(); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 152 | void PhoneNumber::GetMatchingTypes(const base::string16& text, |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 153 | const std::string& app_locale, |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 154 | FieldTypeSet* matching_types) const { |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 155 | base::string16 stripped_text = text; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 156 | StripPunctuation(&stripped_text); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 157 | FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 158 | |
| 159 | // 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] | 160 | // suffix, since web sites often split numbers into these two fields. |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 161 | base::string16 number = GetInfo(PHONE_HOME_NUMBER, app_locale); |
[email protected] | 3de6a598 | 2013-01-10 01:45:44 | [diff] [blame] | 162 | if (GetRegion(*profile_, app_locale) == "US" && |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 163 | number.size() == (kPrefixLength + kSuffixLength)) { |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 164 | base::string16 prefix = number.substr(kPrefixOffset, kPrefixLength); |
| 165 | base::string16 suffix = number.substr(kSuffixOffset, kSuffixLength); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 166 | if (text == prefix || text == suffix) |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 167 | matching_types->insert(PHONE_HOME_NUMBER); |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 170 | base::string16 whole_number = GetInfo(PHONE_HOME_WHOLE_NUMBER, app_locale); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 171 | if (!whole_number.empty()) { |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 172 | base::string16 normalized_number = |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 173 | i18n::NormalizePhoneNumber(text, GetRegion(*profile_, app_locale)); |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 174 | if (normalized_number == whole_number) |
| 175 | matching_types->insert(PHONE_HOME_WHOLE_NUMBER); |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
[email protected] | 4d11fcd | 2012-12-05 05:34:48 | [diff] [blame] | 179 | void PhoneNumber::UpdateCacheIfNeeded(const std::string& app_locale) const { |
[email protected] | 3de6a598 | 2013-01-10 01:45:44 | [diff] [blame] | 180 | std::string region = GetRegion(*profile_, app_locale); |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 181 | if (!number_.empty() && cached_parsed_phone_.region() != region) |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 182 | cached_parsed_phone_ = i18n::PhoneObject(number_, region); |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 185 | PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { |
| 189 | } |
| 190 | |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 191 | bool PhoneNumber::PhoneCombineHelper::SetInfo(AutofillFieldType field_type, |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 192 | const base::string16& value) { |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 193 | if (field_type == PHONE_HOME_COUNTRY_CODE) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 194 | country_ = value; |
| 195 | return true; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 198 | if (field_type == PHONE_HOME_CITY_CODE) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 199 | city_ = value; |
| 200 | return true; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 203 | if (field_type == PHONE_HOME_CITY_AND_NUMBER) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 204 | phone_ = value; |
| 205 | return true; |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 208 | if (field_type == PHONE_HOME_WHOLE_NUMBER) { |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 209 | whole_number_ = value; |
| 210 | return true; |
| 211 | } |
| 212 | |
[email protected] | 4ef55f8c1 | 2011-09-17 00:06:54 | [diff] [blame] | 213 | if (field_type == PHONE_HOME_NUMBER) { |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 214 | phone_.append(value); |
| 215 | return true; |
[email protected] | 11c2bdd1 | 2011-05-23 18:24:32 | [diff] [blame] | 216 | } |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 217 | |
| 218 | return false; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | 3de6a598 | 2013-01-10 01:45:44 | [diff] [blame] | 221 | bool PhoneNumber::PhoneCombineHelper::ParseNumber( |
| 222 | const AutofillProfile& profile, |
| 223 | const std::string& app_locale, |
[email protected] | d5ca8fb | 2013-04-11 17:54:31 | [diff] [blame] | 224 | base::string16* value) { |
[email protected] | 3de6a598 | 2013-01-10 01:45:44 | [diff] [blame] | 225 | if (IsEmpty()) |
| 226 | return false; |
| 227 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 228 | if (!whole_number_.empty()) { |
| 229 | *value = whole_number_; |
| 230 | return true; |
| 231 | } |
| 232 | |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 233 | return i18n::ConstructPhoneNumber( |
[email protected] | 7df904d | 2013-01-15 22:33:22 | [diff] [blame] | 234 | country_, city_, phone_, GetRegion(profile, app_locale), value); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | fcfece4 | 2011-07-22 08:29:32 | [diff] [blame] | 237 | bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { |
| 238 | return phone_.empty() && whole_number_.empty(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 239 | } |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 240 | |
| 241 | } // namespace autofill |