[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" |
| 8 | #include "base/string_util.h" |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 9 | #include "chrome/browser/autofill/autofill_profile.h" |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 10 | #include "chrome/browser/autofill/autofill_type.h" |
| 11 | #include "chrome/browser/autofill/field_types.h" |
| 12 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 13 | namespace { |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 14 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 15 | const char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 }; |
| 16 | |
| 17 | // The number of digits in a phone number. |
| 18 | const size_t kPhoneNumberLength = 7; |
| 19 | |
| 20 | // The number of digits in an area code. |
| 21 | const size_t kPhoneCityCodeLength = 3; |
| 22 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame^] | 23 | const AutofillType::FieldTypeSubGroup kAutofillPhoneTypes[] = { |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 24 | AutofillType::PHONE_NUMBER, |
| 25 | AutofillType::PHONE_CITY_CODE, |
| 26 | AutofillType::PHONE_COUNTRY_CODE, |
| 27 | AutofillType::PHONE_CITY_AND_NUMBER, |
| 28 | AutofillType::PHONE_WHOLE_NUMBER, |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 29 | }; |
| 30 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame^] | 31 | const int kAutofillPhoneLength = arraysize(kAutofillPhoneTypes); |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 32 | |
| 33 | } // namespace |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 34 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 35 | PhoneNumber::PhoneNumber() {} |
| 36 | |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 37 | PhoneNumber::PhoneNumber(const PhoneNumber& number) : FormGroup() { |
| 38 | *this = number; |
| 39 | } |
| 40 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 41 | PhoneNumber::~PhoneNumber() {} |
| 42 | |
[email protected] | 4389724 | 2011-03-09 03:52:11 | [diff] [blame] | 43 | PhoneNumber& PhoneNumber::operator=(const PhoneNumber& number) { |
| 44 | if (this == &number) |
| 45 | return *this; |
| 46 | country_code_ = number.country_code_; |
| 47 | city_code_ = number.city_code_; |
| 48 | number_ = number.number_; |
| 49 | extension_ = number.extension_; |
| 50 | return *this; |
| 51 | } |
| 52 | |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 53 | void PhoneNumber::GetPossibleFieldTypes(const string16& text, |
| 54 | FieldTypeSet* possible_types) const { |
| 55 | string16 stripped_text(text); |
| 56 | StripPunctuation(&stripped_text); |
| 57 | if (!Validate(stripped_text)) |
| 58 | return; |
| 59 | |
| 60 | if (IsNumber(stripped_text)) |
| 61 | possible_types->insert(GetNumberType()); |
| 62 | |
| 63 | if (IsCityCode(stripped_text)) |
| 64 | possible_types->insert(GetCityCodeType()); |
| 65 | |
| 66 | if (IsCountryCode(stripped_text)) |
| 67 | possible_types->insert(GetCountryCodeType()); |
| 68 | |
| 69 | if (IsCityAndNumber(stripped_text)) |
| 70 | possible_types->insert(GetCityAndNumberType()); |
| 71 | |
| 72 | if (IsWholeNumber(stripped_text)) |
| 73 | possible_types->insert(GetWholeNumberType()); |
| 74 | } |
| 75 | |
[email protected] | cea1d11 | 2010-07-01 00:57:33 | [diff] [blame] | 76 | void PhoneNumber::GetAvailableFieldTypes(FieldTypeSet* available_types) const { |
| 77 | DCHECK(available_types); |
| 78 | |
| 79 | if (!number().empty()) |
| 80 | available_types->insert(GetNumberType()); |
| 81 | |
| 82 | if (!city_code().empty()) |
| 83 | available_types->insert(GetCityCodeType()); |
| 84 | |
| 85 | if (!country_code().empty()) |
| 86 | available_types->insert(GetCountryCodeType()); |
| 87 | |
| 88 | if (!CityAndNumber().empty()) |
| 89 | available_types->insert(GetCityAndNumberType()); |
| 90 | |
| 91 | if (!WholeNumber().empty()) |
| 92 | available_types->insert(GetWholeNumberType()); |
| 93 | } |
| 94 | |
[email protected] | 3b3a0ca7 | 2011-03-17 23:04:55 | [diff] [blame] | 95 | string16 PhoneNumber::GetInfo(AutofillFieldType type) const { |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 96 | if (type == GetNumberType()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 97 | return number(); |
| 98 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 99 | if (type == GetCityCodeType()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 100 | return city_code(); |
| 101 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 102 | if (type == GetCountryCodeType()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 103 | return country_code(); |
| 104 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 105 | if (type == GetCityAndNumberType()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 106 | return CityAndNumber(); |
| 107 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 108 | if (type == GetWholeNumberType()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 109 | return WholeNumber(); |
| 110 | |
[email protected] | cea1d11 | 2010-07-01 00:57:33 | [diff] [blame] | 111 | return string16(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 114 | void PhoneNumber::FindInfoMatches(AutofillFieldType type, |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 115 | const string16& info, |
| 116 | std::vector<string16>* matched_text) const { |
| 117 | if (matched_text == NULL) { |
| 118 | DLOG(ERROR) << "NULL matched vector passed in"; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | string16 number(info); |
| 123 | StripPunctuation(&number); |
| 124 | if (!Validate(number)) |
| 125 | return; |
| 126 | |
| 127 | string16 match; |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 128 | if (type == UNKNOWN_TYPE) { |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame^] | 129 | for (int i = 0; i < kAutofillPhoneLength; ++i) { |
| 130 | if (FindInfoMatchesHelper(kAutofillPhoneTypes[i], info, &match)) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 131 | matched_text->push_back(match); |
| 132 | } |
| 133 | } else { |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 134 | if (FindInfoMatchesHelper(AutofillType(type).subgroup(), info, &match)) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 135 | matched_text->push_back(match); |
| 136 | } |
| 137 | } |
| 138 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 139 | void PhoneNumber::SetInfo(AutofillFieldType type, const string16& value) { |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 140 | string16 number(value); |
| 141 | StripPunctuation(&number); |
| 142 | if (!Validate(number)) |
| 143 | return; |
| 144 | |
[email protected] | 1ffe8e99 | 2011-03-17 06:26:29 | [diff] [blame] | 145 | FieldTypeSubGroup subgroup = AutofillType(type).subgroup(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 146 | if (subgroup == AutofillType::PHONE_NUMBER) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 147 | set_number(number); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 148 | else if (subgroup == AutofillType::PHONE_CITY_CODE) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 149 | set_city_code(number); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 150 | else if (subgroup == AutofillType::PHONE_COUNTRY_CODE) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 151 | set_country_code(number); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 152 | else if (subgroup == AutofillType::PHONE_CITY_AND_NUMBER || |
| 153 | subgroup == AutofillType::PHONE_WHOLE_NUMBER) |
[email protected] | afbd354 | 2010-03-17 20:31:07 | [diff] [blame] | 154 | set_whole_number(number); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 155 | else |
| 156 | NOTREACHED(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 159 | // Static. |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 160 | bool PhoneNumber::ParsePhoneNumber(const string16& value, |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 161 | string16* number, |
| 162 | string16* city_code, |
| 163 | string16* country_code) { |
| 164 | DCHECK(number); |
| 165 | DCHECK(city_code); |
| 166 | DCHECK(country_code); |
| 167 | |
| 168 | // Make a working copy of value. |
| 169 | string16 working = value; |
| 170 | |
| 171 | *number = string16(); |
| 172 | *city_code = string16(); |
| 173 | *country_code = string16(); |
| 174 | |
| 175 | // First remove any punctuation. |
| 176 | StripPunctuation(&working); |
| 177 | |
| 178 | if (working.size() < kPhoneNumberLength) |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 179 | return false; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 180 | |
| 181 | // Treat the last 7 digits as the number. |
| 182 | *number = working.substr(working.size() - kPhoneNumberLength, |
| 183 | kPhoneNumberLength); |
| 184 | working.resize(working.size() - kPhoneNumberLength); |
| 185 | if (working.size() < kPhoneCityCodeLength) |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 186 | return true; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 187 | |
| 188 | // Treat the next three digits as the city code. |
| 189 | *city_code = working.substr(working.size() - kPhoneCityCodeLength, |
| 190 | kPhoneCityCodeLength); |
| 191 | working.resize(working.size() - kPhoneCityCodeLength); |
| 192 | if (working.empty()) |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 193 | return true; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 194 | |
| 195 | // Treat any remaining digits as the country code. |
| 196 | *country_code = working; |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 197 | return true; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 200 | string16 PhoneNumber::WholeNumber() const { |
| 201 | string16 whole_number; |
| 202 | if (!country_code_.empty()) |
| 203 | whole_number.append(country_code_); |
| 204 | |
| 205 | if (!city_code_.empty()) |
| 206 | whole_number.append(city_code_); |
| 207 | |
| 208 | if (!number_.empty()) |
| 209 | whole_number.append(number_); |
| 210 | |
| 211 | return whole_number; |
| 212 | } |
| 213 | |
| 214 | void PhoneNumber::set_number(const string16& number) { |
| 215 | string16 digits(number); |
| 216 | StripPunctuation(&digits); |
| 217 | number_ = digits; |
| 218 | } |
| 219 | |
[email protected] | afbd354 | 2010-03-17 20:31:07 | [diff] [blame] | 220 | void PhoneNumber::set_whole_number(const string16& whole_number) { |
| 221 | string16 number, city_code, country_code; |
| 222 | ParsePhoneNumber(whole_number, &number, &city_code, &country_code); |
| 223 | set_number(number); |
| 224 | set_city_code(city_code); |
| 225 | set_country_code(country_code); |
| 226 | } |
| 227 | |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 228 | bool PhoneNumber::FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup, |
| 229 | const string16& info, |
| 230 | string16* match) const { |
| 231 | if (match == NULL) { |
| 232 | DLOG(ERROR) << "NULL match string passed in"; |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | match->clear(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 237 | if (subgroup == AutofillType::PHONE_NUMBER && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 238 | StartsWith(number(), info, true)) { |
| 239 | *match = number(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 240 | } else if (subgroup == AutofillType::PHONE_CITY_CODE && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 241 | StartsWith(city_code(), info, true)) { |
| 242 | *match = city_code(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 243 | } else if (subgroup == AutofillType::PHONE_COUNTRY_CODE && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 244 | StartsWith(country_code(), info, true)) { |
| 245 | *match = country_code(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 246 | } else if (subgroup == AutofillType::PHONE_CITY_AND_NUMBER && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 247 | StartsWith(CityAndNumber(), info, true)) { |
| 248 | *match = CityAndNumber(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 249 | } else if (subgroup == AutofillType::PHONE_WHOLE_NUMBER && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 250 | StartsWith(WholeNumber(), info, true)) { |
| 251 | *match = WholeNumber(); |
| 252 | } |
| 253 | |
| 254 | return !match->empty(); |
| 255 | } |
| 256 | |
| 257 | bool PhoneNumber::IsNumber(const string16& text) const { |
[email protected] | 48db6fb | 2010-05-25 16:48:01 | [diff] [blame] | 258 | if (text.length() > number_.length()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 259 | return false; |
| 260 | |
| 261 | return StartsWith(number_, text, false); |
| 262 | } |
| 263 | |
| 264 | bool PhoneNumber::IsCityCode(const string16& text) const { |
[email protected] | 48db6fb | 2010-05-25 16:48:01 | [diff] [blame] | 265 | if (text.length() > city_code_.length()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 266 | return false; |
| 267 | |
| 268 | return StartsWith(city_code_, text, false); |
| 269 | } |
| 270 | |
| 271 | bool PhoneNumber::IsCountryCode(const string16& text) const { |
[email protected] | 48db6fb | 2010-05-25 16:48:01 | [diff] [blame] | 272 | if (text.length() > country_code_.length()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 273 | return false; |
| 274 | |
| 275 | return StartsWith(country_code_, text, false); |
| 276 | } |
| 277 | |
| 278 | bool PhoneNumber::IsCityAndNumber(const string16& text) const { |
| 279 | string16 city_and_number(CityAndNumber()); |
| 280 | if (text.length() > city_and_number.length()) |
| 281 | return false; |
| 282 | |
| 283 | return StartsWith(city_and_number, text, false); |
| 284 | } |
| 285 | |
| 286 | bool PhoneNumber::IsWholeNumber(const string16& text) const { |
| 287 | string16 whole_number(WholeNumber()); |
| 288 | if (text.length() > whole_number.length()) |
| 289 | return false; |
| 290 | |
| 291 | return StartsWith(whole_number, text, false); |
| 292 | } |
| 293 | |
| 294 | bool PhoneNumber::Validate(const string16& number) const { |
| 295 | for (size_t i = 0; i < number.length(); ++i) { |
| 296 | if (!IsAsciiDigit(number[i])) |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | return true; |
| 301 | } |
| 302 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 303 | // Static. |
| 304 | void PhoneNumber::StripPunctuation(string16* number) { |
| 305 | RemoveChars(*number, kPhoneNumberSeparators, number); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 306 | } |