blob: 97bb963c1c2bfef7f378ae3472529f08d7adc98e [file] [log] [blame]
[email protected]3db3ff62010-01-07 00:35:391// Copyright (c) 2010 The Chromium Authors. All rights reserved.
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"
9#include "chrome/browser/autofill/autofill_type.h"
10#include "chrome/browser/autofill/field_types.h"
11
12static const string16 kPhoneNumberSeparators = ASCIIToUTF16(" .()-");
13
14static const AutoFillType::FieldTypeSubGroup kAutoFillPhoneTypes[] = {
15 AutoFillType::PHONE_NUMBER,
16 AutoFillType::PHONE_CITY_CODE,
17 AutoFillType::PHONE_COUNTRY_CODE,
18 AutoFillType::PHONE_CITY_AND_NUMBER,
19 AutoFillType::PHONE_WHOLE_NUMBER,
20};
21
22static const int kAutoFillPhoneLength = arraysize(kAutoFillPhoneTypes);
23
24void PhoneNumber::GetPossibleFieldTypes(const string16& text,
25 FieldTypeSet* possible_types) const {
26 string16 stripped_text(text);
27 StripPunctuation(&stripped_text);
28 if (!Validate(stripped_text))
29 return;
30
31 if (IsNumber(stripped_text))
32 possible_types->insert(GetNumberType());
33
34 if (IsCityCode(stripped_text))
35 possible_types->insert(GetCityCodeType());
36
37 if (IsCountryCode(stripped_text))
38 possible_types->insert(GetCountryCodeType());
39
40 if (IsCityAndNumber(stripped_text))
41 possible_types->insert(GetCityAndNumberType());
42
43 if (IsWholeNumber(stripped_text))
44 possible_types->insert(GetWholeNumberType());
45}
46
47string16 PhoneNumber::GetFieldText(const AutoFillType& type) const {
48 AutoFillFieldType field_type = type.field_type();
49 if (field_type == GetNumberType())
50 return number();
51
52 if (field_type == GetCityCodeType())
53 return city_code();
54
55 if (field_type == GetCountryCodeType())
56 return country_code();
57
58 if (field_type == GetCityAndNumberType())
59 return CityAndNumber();
60
61 if (field_type == GetWholeNumberType())
62 return WholeNumber();
63
64 return EmptyString16();
65}
66
67void PhoneNumber::FindInfoMatches(const AutoFillType& type,
68 const string16& info,
69 std::vector<string16>* matched_text) const {
70 if (matched_text == NULL) {
71 DLOG(ERROR) << "NULL matched vector passed in";
72 return;
73 }
74
75 string16 number(info);
76 StripPunctuation(&number);
77 if (!Validate(number))
78 return;
79
80 string16 match;
81 if (type.field_type() == UNKNOWN_TYPE) {
82 for (int i = 0; i < kAutoFillPhoneLength; ++i) {
83 if (FindInfoMatchesHelper(kAutoFillPhoneTypes[i], info, &match))
84 matched_text->push_back(match);
85 }
86 } else {
87 if (FindInfoMatchesHelper(type.subgroup(), info, &match))
88 matched_text->push_back(match);
89 }
90}
91
92void PhoneNumber::SetInfo(const AutoFillType& type, const string16& value) {
93 string16 number(value);
94 StripPunctuation(&number);
95 if (!Validate(number))
96 return;
97
98 FieldTypeSubGroup subgroup = type.subgroup();
99 if (subgroup == AutoFillType::PHONE_NUMBER)
100 set_number(number);
101 else if (subgroup == AutoFillType::PHONE_CITY_CODE)
102 set_city_code(number);
103 else if (subgroup == AutoFillType::PHONE_COUNTRY_CODE)
104 set_country_code(number);
105 else
106 NOTREACHED();
107 // TODO(jhawkins): Add extension support.
108 // else if (subgroup == AutoFillType::PHONE_EXTENSION)
109 // set_extension(number);
110}
111
112string16 PhoneNumber::WholeNumber() const {
113 string16 whole_number;
114 if (!country_code_.empty())
115 whole_number.append(country_code_);
116
117 if (!city_code_.empty())
118 whole_number.append(city_code_);
119
120 if (!number_.empty())
121 whole_number.append(number_);
122
123 return whole_number;
124}
125
126void PhoneNumber::set_number(const string16& number) {
127 string16 digits(number);
128 StripPunctuation(&digits);
129 number_ = digits;
130}
131
132PhoneNumber::PhoneNumber(const PhoneNumber& phone_number)
133 : country_code_(phone_number.country_code_),
134 city_code_(phone_number.city_code_),
135 number_(phone_number.number_),
136 extension_(phone_number.extension_) {
137}
138
139bool PhoneNumber::FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup,
140 const string16& info,
141 string16* match) const {
142 if (match == NULL) {
143 DLOG(ERROR) << "NULL match string passed in";
144 return false;
145 }
146
147 match->clear();
148 if (subgroup == AutoFillType::PHONE_NUMBER &&
149 StartsWith(number(), info, true)) {
150 *match = number();
151 } else if (subgroup == AutoFillType::PHONE_CITY_CODE &&
152 StartsWith(city_code(), info, true)) {
153 *match = city_code();
154 } else if (subgroup == AutoFillType::PHONE_COUNTRY_CODE &&
155 StartsWith(country_code(), info, true)) {
156 *match = country_code();
157 } else if (subgroup == AutoFillType::PHONE_CITY_AND_NUMBER &&
158 StartsWith(CityAndNumber(), info, true)) {
159 *match = CityAndNumber();
160 } else if (subgroup == AutoFillType::PHONE_WHOLE_NUMBER &&
161 StartsWith(WholeNumber(), info, true)) {
162 *match = WholeNumber();
163 }
164
165 return !match->empty();
166}
167
168bool PhoneNumber::IsNumber(const string16& text) const {
169 if (text.length() <= number_.length())
170 return false;
171
172 return StartsWith(number_, text, false);
173}
174
175bool PhoneNumber::IsCityCode(const string16& text) const {
176 if (text.length() <= city_code_.length())
177 return false;
178
179 return StartsWith(city_code_, text, false);
180}
181
182bool PhoneNumber::IsCountryCode(const string16& text) const {
183 if (text.length() <= country_code_.length())
184 return false;
185
186 return StartsWith(country_code_, text, false);
187}
188
189bool PhoneNumber::IsCityAndNumber(const string16& text) const {
190 string16 city_and_number(CityAndNumber());
191 if (text.length() > city_and_number.length())
192 return false;
193
194 return StartsWith(city_and_number, text, false);
195}
196
197bool PhoneNumber::IsWholeNumber(const string16& text) const {
198 string16 whole_number(WholeNumber());
199 if (text.length() > whole_number.length())
200 return false;
201
202 return StartsWith(whole_number, text, false);
203}
204
205bool PhoneNumber::Validate(const string16& number) const {
206 for (size_t i = 0; i < number.length(); ++i) {
207 if (!IsAsciiDigit(number[i]))
208 return false;
209 }
210
211 return true;
212}
213
214void PhoneNumber::StripPunctuation(string16* number) const {
215 TrimString(*number, kPhoneNumberSeparators.c_str(), number);
216}