[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] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 23 | const AutofillType::FieldTypeSubGroup kAutoFillPhoneTypes[] = { |
| 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] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 31 | const int kAutoFillPhoneLength = arraysize(kAutoFillPhoneTypes); |
| 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] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 95 | string16 PhoneNumber::GetFieldText(const AutofillType& type) const { |
[email protected] | 7127ca8f | 2011-03-01 22:14:18 | [diff] [blame] | 96 | AutofillFieldType field_type = type.field_type(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 97 | if (field_type == GetNumberType()) |
| 98 | return number(); |
| 99 | |
| 100 | if (field_type == GetCityCodeType()) |
| 101 | return city_code(); |
| 102 | |
| 103 | if (field_type == GetCountryCodeType()) |
| 104 | return country_code(); |
| 105 | |
| 106 | if (field_type == GetCityAndNumberType()) |
| 107 | return CityAndNumber(); |
| 108 | |
| 109 | if (field_type == GetWholeNumberType()) |
| 110 | return WholeNumber(); |
| 111 | |
[email protected] | cea1d11 | 2010-07-01 00:57:33 | [diff] [blame] | 112 | return string16(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 115 | void PhoneNumber::FindInfoMatches(const AutofillType& type, |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 116 | const string16& info, |
| 117 | std::vector<string16>* matched_text) const { |
| 118 | if (matched_text == NULL) { |
| 119 | DLOG(ERROR) << "NULL matched vector passed in"; |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | string16 number(info); |
| 124 | StripPunctuation(&number); |
| 125 | if (!Validate(number)) |
| 126 | return; |
| 127 | |
| 128 | string16 match; |
| 129 | if (type.field_type() == UNKNOWN_TYPE) { |
| 130 | for (int i = 0; i < kAutoFillPhoneLength; ++i) { |
| 131 | if (FindInfoMatchesHelper(kAutoFillPhoneTypes[i], info, &match)) |
| 132 | matched_text->push_back(match); |
| 133 | } |
| 134 | } else { |
| 135 | if (FindInfoMatchesHelper(type.subgroup(), info, &match)) |
| 136 | matched_text->push_back(match); |
| 137 | } |
| 138 | } |
| 139 | |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 140 | void PhoneNumber::SetInfo(const AutofillType& type, const string16& value) { |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 141 | string16 number(value); |
| 142 | StripPunctuation(&number); |
| 143 | if (!Validate(number)) |
| 144 | return; |
| 145 | |
| 146 | FieldTypeSubGroup subgroup = type.subgroup(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 147 | if (subgroup == AutofillType::PHONE_NUMBER) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 148 | set_number(number); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 149 | else if (subgroup == AutofillType::PHONE_CITY_CODE) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 150 | set_city_code(number); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 151 | else if (subgroup == AutofillType::PHONE_COUNTRY_CODE) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 152 | set_country_code(number); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 153 | else if (subgroup == AutofillType::PHONE_CITY_AND_NUMBER || |
| 154 | subgroup == AutofillType::PHONE_WHOLE_NUMBER) |
[email protected] | afbd354 | 2010-03-17 20:31:07 | [diff] [blame] | 155 | set_whole_number(number); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 156 | else |
| 157 | NOTREACHED(); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 160 | // Static. |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 161 | bool PhoneNumber::ParsePhoneNumber(const string16& value, |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 162 | string16* number, |
| 163 | string16* city_code, |
| 164 | string16* country_code) { |
| 165 | DCHECK(number); |
| 166 | DCHECK(city_code); |
| 167 | DCHECK(country_code); |
| 168 | |
| 169 | // Make a working copy of value. |
| 170 | string16 working = value; |
| 171 | |
| 172 | *number = string16(); |
| 173 | *city_code = string16(); |
| 174 | *country_code = string16(); |
| 175 | |
| 176 | // First remove any punctuation. |
| 177 | StripPunctuation(&working); |
| 178 | |
| 179 | if (working.size() < kPhoneNumberLength) |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 180 | return false; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 181 | |
| 182 | // Treat the last 7 digits as the number. |
| 183 | *number = working.substr(working.size() - kPhoneNumberLength, |
| 184 | kPhoneNumberLength); |
| 185 | working.resize(working.size() - kPhoneNumberLength); |
| 186 | if (working.size() < kPhoneCityCodeLength) |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 187 | return true; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 188 | |
| 189 | // Treat the next three digits as the city code. |
| 190 | *city_code = working.substr(working.size() - kPhoneCityCodeLength, |
| 191 | kPhoneCityCodeLength); |
| 192 | working.resize(working.size() - kPhoneCityCodeLength); |
| 193 | if (working.empty()) |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 194 | return true; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 195 | |
| 196 | // Treat any remaining digits as the country code. |
| 197 | *country_code = working; |
[email protected] | f8e7b62d | 2010-06-03 18:12:54 | [diff] [blame] | 198 | return true; |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 201 | string16 PhoneNumber::WholeNumber() const { |
| 202 | string16 whole_number; |
| 203 | if (!country_code_.empty()) |
| 204 | whole_number.append(country_code_); |
| 205 | |
| 206 | if (!city_code_.empty()) |
| 207 | whole_number.append(city_code_); |
| 208 | |
| 209 | if (!number_.empty()) |
| 210 | whole_number.append(number_); |
| 211 | |
| 212 | return whole_number; |
| 213 | } |
| 214 | |
| 215 | void PhoneNumber::set_number(const string16& number) { |
| 216 | string16 digits(number); |
| 217 | StripPunctuation(&digits); |
| 218 | number_ = digits; |
| 219 | } |
| 220 | |
[email protected] | afbd354 | 2010-03-17 20:31:07 | [diff] [blame] | 221 | void PhoneNumber::set_whole_number(const string16& whole_number) { |
| 222 | string16 number, city_code, country_code; |
| 223 | ParsePhoneNumber(whole_number, &number, &city_code, &country_code); |
| 224 | set_number(number); |
| 225 | set_city_code(city_code); |
| 226 | set_country_code(country_code); |
| 227 | } |
| 228 | |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 229 | bool PhoneNumber::FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup, |
| 230 | const string16& info, |
| 231 | string16* match) const { |
| 232 | if (match == NULL) { |
| 233 | DLOG(ERROR) << "NULL match string passed in"; |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | match->clear(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 238 | if (subgroup == AutofillType::PHONE_NUMBER && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 239 | StartsWith(number(), info, true)) { |
| 240 | *match = number(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 241 | } else if (subgroup == AutofillType::PHONE_CITY_CODE && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 242 | StartsWith(city_code(), info, true)) { |
| 243 | *match = city_code(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 244 | } else if (subgroup == AutofillType::PHONE_COUNTRY_CODE && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 245 | StartsWith(country_code(), info, true)) { |
| 246 | *match = country_code(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 247 | } else if (subgroup == AutofillType::PHONE_CITY_AND_NUMBER && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 248 | StartsWith(CityAndNumber(), info, true)) { |
| 249 | *match = CityAndNumber(); |
[email protected] | 7b37fbb | 2011-03-07 16:16:03 | [diff] [blame] | 250 | } else if (subgroup == AutofillType::PHONE_WHOLE_NUMBER && |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 251 | StartsWith(WholeNumber(), info, true)) { |
| 252 | *match = WholeNumber(); |
| 253 | } |
| 254 | |
| 255 | return !match->empty(); |
| 256 | } |
| 257 | |
| 258 | bool PhoneNumber::IsNumber(const string16& text) const { |
[email protected] | 48db6fb | 2010-05-25 16:48:01 | [diff] [blame] | 259 | if (text.length() > number_.length()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 260 | return false; |
| 261 | |
| 262 | return StartsWith(number_, text, false); |
| 263 | } |
| 264 | |
| 265 | bool PhoneNumber::IsCityCode(const string16& text) const { |
[email protected] | 48db6fb | 2010-05-25 16:48:01 | [diff] [blame] | 266 | if (text.length() > city_code_.length()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 267 | return false; |
| 268 | |
| 269 | return StartsWith(city_code_, text, false); |
| 270 | } |
| 271 | |
| 272 | bool PhoneNumber::IsCountryCode(const string16& text) const { |
[email protected] | 48db6fb | 2010-05-25 16:48:01 | [diff] [blame] | 273 | if (text.length() > country_code_.length()) |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 274 | return false; |
| 275 | |
| 276 | return StartsWith(country_code_, text, false); |
| 277 | } |
| 278 | |
| 279 | bool PhoneNumber::IsCityAndNumber(const string16& text) const { |
| 280 | string16 city_and_number(CityAndNumber()); |
| 281 | if (text.length() > city_and_number.length()) |
| 282 | return false; |
| 283 | |
| 284 | return StartsWith(city_and_number, text, false); |
| 285 | } |
| 286 | |
| 287 | bool PhoneNumber::IsWholeNumber(const string16& text) const { |
| 288 | string16 whole_number(WholeNumber()); |
| 289 | if (text.length() > whole_number.length()) |
| 290 | return false; |
| 291 | |
| 292 | return StartsWith(whole_number, text, false); |
| 293 | } |
| 294 | |
| 295 | bool PhoneNumber::Validate(const string16& number) const { |
| 296 | for (size_t i = 0; i < number.length(); ++i) { |
| 297 | if (!IsAsciiDigit(number[i])) |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | |
[email protected] | 9b435be9 | 2010-03-04 21:20:15 | [diff] [blame] | 304 | // Static. |
| 305 | void PhoneNumber::StripPunctuation(string16* number) { |
| 306 | RemoveChars(*number, kPhoneNumberSeparators, number); |
[email protected] | 3db3ff6 | 2010-01-07 00:35:39 | [diff] [blame] | 307 | } |