blob: 110616a7b5feaa80ab34840c349127133ef5fea7 [file] [log] [blame]
[email protected]7b37fbb2011-03-07 16:16:031// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]3db3ff62010-01-07 00:35:392// 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]9b435be92010-03-04 21:20:159#include "chrome/browser/autofill/autofill_profile.h"
[email protected]3db3ff62010-01-07 00:35:3910#include "chrome/browser/autofill/autofill_type.h"
11#include "chrome/browser/autofill/field_types.h"
12
[email protected]9b435be92010-03-04 21:20:1513namespace {
[email protected]3db3ff62010-01-07 00:35:3914
[email protected]9b435be92010-03-04 21:20:1515const char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 };
16
17// The number of digits in a phone number.
18const size_t kPhoneNumberLength = 7;
19
20// The number of digits in an area code.
21const size_t kPhoneCityCodeLength = 3;
22
[email protected]7b37fbb2011-03-07 16:16:0323const 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]3db3ff62010-01-07 00:35:3929};
30
[email protected]9b435be92010-03-04 21:20:1531const int kAutoFillPhoneLength = arraysize(kAutoFillPhoneTypes);
32
33} // namespace
[email protected]3db3ff62010-01-07 00:35:3934
[email protected]8e383412010-10-19 16:57:0335PhoneNumber::PhoneNumber() {}
36
[email protected]43897242011-03-09 03:52:1137PhoneNumber::PhoneNumber(const PhoneNumber& number) : FormGroup() {
38 *this = number;
39}
40
[email protected]8e383412010-10-19 16:57:0341PhoneNumber::~PhoneNumber() {}
42
[email protected]43897242011-03-09 03:52:1143PhoneNumber& 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]3db3ff62010-01-07 00:35:3953void 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]cea1d112010-07-01 00:57:3376void 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]7b37fbb2011-03-07 16:16:0395string16 PhoneNumber::GetFieldText(const AutofillType& type) const {
[email protected]7127ca8f2011-03-01 22:14:1896 AutofillFieldType field_type = type.field_type();
[email protected]3db3ff62010-01-07 00:35:3997 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]cea1d112010-07-01 00:57:33112 return string16();
[email protected]3db3ff62010-01-07 00:35:39113}
114
[email protected]7b37fbb2011-03-07 16:16:03115void PhoneNumber::FindInfoMatches(const AutofillType& type,
[email protected]3db3ff62010-01-07 00:35:39116 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]7b37fbb2011-03-07 16:16:03140void PhoneNumber::SetInfo(const AutofillType& type, const string16& value) {
[email protected]3db3ff62010-01-07 00:35:39141 string16 number(value);
142 StripPunctuation(&number);
143 if (!Validate(number))
144 return;
145
146 FieldTypeSubGroup subgroup = type.subgroup();
[email protected]7b37fbb2011-03-07 16:16:03147 if (subgroup == AutofillType::PHONE_NUMBER)
[email protected]3db3ff62010-01-07 00:35:39148 set_number(number);
[email protected]7b37fbb2011-03-07 16:16:03149 else if (subgroup == AutofillType::PHONE_CITY_CODE)
[email protected]3db3ff62010-01-07 00:35:39150 set_city_code(number);
[email protected]7b37fbb2011-03-07 16:16:03151 else if (subgroup == AutofillType::PHONE_COUNTRY_CODE)
[email protected]3db3ff62010-01-07 00:35:39152 set_country_code(number);
[email protected]7b37fbb2011-03-07 16:16:03153 else if (subgroup == AutofillType::PHONE_CITY_AND_NUMBER ||
154 subgroup == AutofillType::PHONE_WHOLE_NUMBER)
[email protected]afbd3542010-03-17 20:31:07155 set_whole_number(number);
[email protected]3db3ff62010-01-07 00:35:39156 else
157 NOTREACHED();
[email protected]3db3ff62010-01-07 00:35:39158}
159
[email protected]9b435be92010-03-04 21:20:15160// Static.
[email protected]f8e7b62d2010-06-03 18:12:54161bool PhoneNumber::ParsePhoneNumber(const string16& value,
[email protected]9b435be92010-03-04 21:20:15162 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]f8e7b62d2010-06-03 18:12:54180 return false;
[email protected]9b435be92010-03-04 21:20:15181
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]f8e7b62d2010-06-03 18:12:54187 return true;
[email protected]9b435be92010-03-04 21:20:15188
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]f8e7b62d2010-06-03 18:12:54194 return true;
[email protected]9b435be92010-03-04 21:20:15195
196 // Treat any remaining digits as the country code.
197 *country_code = working;
[email protected]f8e7b62d2010-06-03 18:12:54198 return true;
[email protected]9b435be92010-03-04 21:20:15199}
200
[email protected]3db3ff62010-01-07 00:35:39201string16 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
215void PhoneNumber::set_number(const string16& number) {
216 string16 digits(number);
217 StripPunctuation(&digits);
218 number_ = digits;
219}
220
[email protected]afbd3542010-03-17 20:31:07221void 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]3db3ff62010-01-07 00:35:39229bool 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]7b37fbb2011-03-07 16:16:03238 if (subgroup == AutofillType::PHONE_NUMBER &&
[email protected]3db3ff62010-01-07 00:35:39239 StartsWith(number(), info, true)) {
240 *match = number();
[email protected]7b37fbb2011-03-07 16:16:03241 } else if (subgroup == AutofillType::PHONE_CITY_CODE &&
[email protected]3db3ff62010-01-07 00:35:39242 StartsWith(city_code(), info, true)) {
243 *match = city_code();
[email protected]7b37fbb2011-03-07 16:16:03244 } else if (subgroup == AutofillType::PHONE_COUNTRY_CODE &&
[email protected]3db3ff62010-01-07 00:35:39245 StartsWith(country_code(), info, true)) {
246 *match = country_code();
[email protected]7b37fbb2011-03-07 16:16:03247 } else if (subgroup == AutofillType::PHONE_CITY_AND_NUMBER &&
[email protected]3db3ff62010-01-07 00:35:39248 StartsWith(CityAndNumber(), info, true)) {
249 *match = CityAndNumber();
[email protected]7b37fbb2011-03-07 16:16:03250 } else if (subgroup == AutofillType::PHONE_WHOLE_NUMBER &&
[email protected]3db3ff62010-01-07 00:35:39251 StartsWith(WholeNumber(), info, true)) {
252 *match = WholeNumber();
253 }
254
255 return !match->empty();
256}
257
258bool PhoneNumber::IsNumber(const string16& text) const {
[email protected]48db6fb2010-05-25 16:48:01259 if (text.length() > number_.length())
[email protected]3db3ff62010-01-07 00:35:39260 return false;
261
262 return StartsWith(number_, text, false);
263}
264
265bool PhoneNumber::IsCityCode(const string16& text) const {
[email protected]48db6fb2010-05-25 16:48:01266 if (text.length() > city_code_.length())
[email protected]3db3ff62010-01-07 00:35:39267 return false;
268
269 return StartsWith(city_code_, text, false);
270}
271
272bool PhoneNumber::IsCountryCode(const string16& text) const {
[email protected]48db6fb2010-05-25 16:48:01273 if (text.length() > country_code_.length())
[email protected]3db3ff62010-01-07 00:35:39274 return false;
275
276 return StartsWith(country_code_, text, false);
277}
278
279bool 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
287bool 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
295bool 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]9b435be92010-03-04 21:20:15304// Static.
305void PhoneNumber::StripPunctuation(string16* number) {
306 RemoveChars(*number, kPhoneNumberSeparators, number);
[email protected]3db3ff62010-01-07 00:35:39307}