OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autofill/phone_number.h" | 5 #include "chrome/browser/autofill/phone_number.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 // The number of digits in a phone number. | 21 // The number of digits in a phone number. |
22 const size_t kPhoneNumberLength = 7; | 22 const size_t kPhoneNumberLength = 7; |
23 | 23 |
24 // The number of digits in an area code. | 24 // The number of digits in an area code. |
25 const size_t kPhoneCityCodeLength = 3; | 25 const size_t kPhoneCityCodeLength = 3; |
26 | 26 |
27 void StripPunctuation(string16* number) { | 27 void StripPunctuation(string16* number) { |
28 RemoveChars(*number, kPhoneNumberSeparators, number); | 28 RemoveChars(*number, kPhoneNumberSeparators, number); |
29 } | 29 } |
30 | 30 |
| 31 // Returns the region code for this phone number, which is an ISO 3166 2-letter |
| 32 // country code. The returned value is based on the |profile|; if the |profile| |
| 33 // does not have a country code associated with it, falls back to the country |
| 34 // code corresponding to the |app_locale|. |
| 35 std::string GetRegion(const AutofillProfile& profile, |
| 36 const std::string& app_locale) { |
| 37 std::string country_code = profile.CountryCode(); |
| 38 if (!country_code.empty()) |
| 39 return country_code; |
| 40 |
| 41 return AutofillCountry::CountryCodeForLocale(app_locale); |
| 42 } |
| 43 |
31 } // namespace | 44 } // namespace |
32 | 45 |
33 PhoneNumber::PhoneNumber(AutofillProfile* profile) | 46 PhoneNumber::PhoneNumber(AutofillProfile* profile) |
34 : profile_(profile) { | 47 : profile_(profile) { |
35 } | 48 } |
36 | 49 |
37 PhoneNumber::PhoneNumber(const PhoneNumber& number) | 50 PhoneNumber::PhoneNumber(const PhoneNumber& number) |
38 : FormGroup(), | 51 : FormGroup(), |
39 profile_(NULL) { | 52 profile_(NULL) { |
40 *this = number; | 53 *this = number; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // If the phone cannot be normalized, returns the stored value verbatim. | 103 // If the phone cannot be normalized, returns the stored value verbatim. |
91 string16 PhoneNumber::GetInfo(AutofillFieldType type, | 104 string16 PhoneNumber::GetInfo(AutofillFieldType type, |
92 const std::string& app_locale) const { | 105 const std::string& app_locale) const { |
93 if (type == PHONE_HOME_WHOLE_NUMBER) { | 106 if (type == PHONE_HOME_WHOLE_NUMBER) { |
94 // Whole numbers require special handling: If normalization for the number | 107 // Whole numbers require special handling: If normalization for the number |
95 // fails, return the non-normalized number instead. | 108 // fails, return the non-normalized number instead. |
96 string16 phone = GetRawInfo(type); | 109 string16 phone = GetRawInfo(type); |
97 | 110 |
98 // TODO(isherman): Can/should this use the cached_parsed_phone_? | 111 // TODO(isherman): Can/should this use the cached_parsed_phone_? |
99 string16 normalized_phone = | 112 string16 normalized_phone = |
100 autofill_i18n::NormalizePhoneNumber(phone, GetRegion(app_locale)); | 113 autofill_i18n::NormalizePhoneNumber(phone, |
| 114 GetRegion(*profile_, app_locale)); |
101 return !normalized_phone.empty() ? normalized_phone : phone; | 115 return !normalized_phone.empty() ? normalized_phone : phone; |
102 } | 116 } |
103 | 117 |
104 UpdateCacheIfNeeded(app_locale); | 118 UpdateCacheIfNeeded(app_locale); |
105 if (!cached_parsed_phone_.IsValidNumber()) | 119 if (!cached_parsed_phone_.IsValidNumber()) |
106 return string16(); | 120 return string16(); |
107 | 121 |
108 switch (type) { | 122 switch (type) { |
109 case PHONE_HOME_NUMBER: | 123 case PHONE_HOME_NUMBER: |
110 return cached_parsed_phone_.GetNumber(); | 124 return cached_parsed_phone_.GetNumber(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 void PhoneNumber::GetMatchingTypes(const string16& text, | 163 void PhoneNumber::GetMatchingTypes(const string16& text, |
150 const std::string& app_locale, | 164 const std::string& app_locale, |
151 FieldTypeSet* matching_types) const { | 165 FieldTypeSet* matching_types) const { |
152 string16 stripped_text = text; | 166 string16 stripped_text = text; |
153 StripPunctuation(&stripped_text); | 167 StripPunctuation(&stripped_text); |
154 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); | 168 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); |
155 | 169 |
156 // For US numbers, also compare to the three-digit prefix and the four-digit | 170 // For US numbers, also compare to the three-digit prefix and the four-digit |
157 // suffix, since web sites often split numbers into these two fields. | 171 // suffix, since web sites often split numbers into these two fields. |
158 string16 number = GetInfo(PHONE_HOME_NUMBER, app_locale); | 172 string16 number = GetInfo(PHONE_HOME_NUMBER, app_locale); |
159 if (GetRegion(app_locale) == "US" && | 173 if (GetRegion(*profile_, app_locale) == "US" && |
160 number.size() == (kPrefixLength + kSuffixLength)) { | 174 number.size() == (kPrefixLength + kSuffixLength)) { |
161 string16 prefix = number.substr(kPrefixOffset, kPrefixLength); | 175 string16 prefix = number.substr(kPrefixOffset, kPrefixLength); |
162 string16 suffix = number.substr(kSuffixOffset, kSuffixLength); | 176 string16 suffix = number.substr(kSuffixOffset, kSuffixLength); |
163 if (text == prefix || text == suffix) | 177 if (text == prefix || text == suffix) |
164 matching_types->insert(PHONE_HOME_NUMBER); | 178 matching_types->insert(PHONE_HOME_NUMBER); |
165 } | 179 } |
166 | 180 |
167 string16 whole_number = GetInfo(PHONE_HOME_WHOLE_NUMBER, app_locale); | 181 string16 whole_number = GetInfo(PHONE_HOME_WHOLE_NUMBER, app_locale); |
168 if (!whole_number.empty()) { | 182 if (!whole_number.empty()) { |
169 string16 normalized_number = | 183 string16 normalized_number = |
170 autofill_i18n::NormalizePhoneNumber(text, GetRegion(app_locale)); | 184 autofill_i18n::NormalizePhoneNumber(text, |
| 185 GetRegion(*profile_, app_locale)); |
171 if (normalized_number == whole_number) | 186 if (normalized_number == whole_number) |
172 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); | 187 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); |
173 } | 188 } |
174 } | 189 } |
175 | 190 |
176 std::string PhoneNumber::GetRegion(const std::string& app_locale) const { | |
177 const std::string country_code = profile_->CountryCode(); | |
178 if (country_code.empty()) | |
179 return AutofillCountry::CountryCodeForLocale(app_locale); | |
180 | |
181 return country_code; | |
182 } | |
183 | |
184 void PhoneNumber::UpdateCacheIfNeeded(const std::string& app_locale) const { | 191 void PhoneNumber::UpdateCacheIfNeeded(const std::string& app_locale) const { |
185 std::string region = GetRegion(app_locale); | 192 std::string region = GetRegion(*profile_, app_locale); |
186 if (!number_.empty() && cached_parsed_phone_.GetRegion() != region) | 193 if (!number_.empty() && cached_parsed_phone_.GetRegion() != region) |
187 cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, region); | 194 cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, region); |
188 } | 195 } |
189 | 196 |
190 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { | 197 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { |
191 } | 198 } |
192 | 199 |
193 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { | 200 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { |
194 } | 201 } |
195 | 202 |
(...skipping 20 matching lines...) Expand all Loading... |
216 } | 223 } |
217 | 224 |
218 if (field_type == PHONE_HOME_NUMBER) { | 225 if (field_type == PHONE_HOME_NUMBER) { |
219 phone_.append(value); | 226 phone_.append(value); |
220 return true; | 227 return true; |
221 } | 228 } |
222 | 229 |
223 return false; | 230 return false; |
224 } | 231 } |
225 | 232 |
226 bool PhoneNumber::PhoneCombineHelper::ParseNumber(const std::string& region, | 233 bool PhoneNumber::PhoneCombineHelper::ParseNumber( |
227 string16* value) { | 234 const AutofillProfile& profile, |
| 235 const std::string& app_locale, |
| 236 string16* value) { |
| 237 if (IsEmpty()) |
| 238 return false; |
| 239 |
228 if (!whole_number_.empty()) { | 240 if (!whole_number_.empty()) { |
229 *value = whole_number_; | 241 *value = whole_number_; |
230 return true; | 242 return true; |
231 } | 243 } |
232 | 244 |
233 return autofill_i18n::ConstructPhoneNumber( | 245 return autofill_i18n::ConstructPhoneNumber( |
234 country_, city_, phone_, region, | 246 country_, city_, phone_, GetRegion(profile, app_locale), |
235 (country_.empty() ? | 247 (country_.empty() ? |
236 autofill_i18n::NATIONAL : autofill_i18n::INTERNATIONAL), | 248 autofill_i18n::NATIONAL : autofill_i18n::INTERNATIONAL), |
237 value); | 249 value); |
238 } | 250 } |
239 | 251 |
240 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { | 252 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { |
241 return phone_.empty() && whole_number_.empty(); | 253 return phone_.empty() && whole_number_.empty(); |
242 } | 254 } |
OLD | NEW |