OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/core/browser/phone_number.h" | 5 #include "components/autofill/core/browser/phone_number.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "components/autofill/core/browser/autofill_country.h" | 10 #include "components/autofill/core/browser/autofill_country.h" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 number_, GetRegion(*profile_, app_locale)).empty()) { | 142 number_, GetRegion(*profile_, app_locale)).empty()) { |
143 // The number doesn't make sense for this region; clear it. | 143 // The number doesn't make sense for this region; clear it. |
144 number_.clear(); | 144 number_.clear(); |
145 } | 145 } |
146 return !number_.empty(); | 146 return !number_.empty(); |
147 } | 147 } |
148 | 148 |
149 void PhoneNumber::GetMatchingTypes(const base::string16& text, | 149 void PhoneNumber::GetMatchingTypes(const base::string16& text, |
150 const std::string& app_locale, | 150 const std::string& app_locale, |
151 ServerFieldTypeSet* matching_types) const { | 151 ServerFieldTypeSet* matching_types) const { |
| 152 // Strip the common phone number non numerical characters before calling the |
| 153 // base matching type function. For example, the |text| "(514) 121-1523" |
| 154 // would become the stripped text "5141211523". Since the base matching |
| 155 // function only does simple canonicalization to match against the stored |
| 156 // data, some domain specific cases will be covered below. |
152 base::string16 stripped_text = text; | 157 base::string16 stripped_text = text; |
153 base::RemoveChars(stripped_text, base::ASCIIToUTF16(" .()-"), &stripped_text); | 158 base::RemoveChars(stripped_text, base::ASCIIToUTF16(" .()-"), &stripped_text); |
154 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); | 159 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); |
155 | 160 |
156 // For US numbers, also compare to the three-digit prefix and the four-digit | 161 // 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. | 162 // suffix, since web sites often split numbers into these two fields. |
158 base::string16 number = GetInfo(AutofillType(PHONE_HOME_NUMBER), app_locale); | 163 base::string16 number = GetInfo(AutofillType(PHONE_HOME_NUMBER), app_locale); |
159 if (GetRegion(*profile_, app_locale) == "US" && | 164 if (GetRegion(*profile_, app_locale) == "US" && |
160 number.size() == (kPrefixLength + kSuffixLength)) { | 165 number.size() == (kPrefixLength + kSuffixLength)) { |
161 base::string16 prefix = number.substr(kPrefixOffset, kPrefixLength); | 166 base::string16 prefix = number.substr(kPrefixOffset, kPrefixLength); |
162 base::string16 suffix = number.substr(kSuffixOffset, kSuffixLength); | 167 base::string16 suffix = number.substr(kSuffixOffset, kSuffixLength); |
163 if (text == prefix || text == suffix) | 168 if (text == prefix || text == suffix) |
164 matching_types->insert(PHONE_HOME_NUMBER); | 169 matching_types->insert(PHONE_HOME_NUMBER); |
165 } | 170 } |
166 | 171 |
167 base::string16 whole_number = | 172 // TODO(crbug.com/581391): Investigate the use of PhoneNumberUtil when |
168 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), app_locale); | 173 // matching phone numbers for upload. |
169 if (!whole_number.empty()) { | 174 // If there is not already a match for PHONE_HOME_WHOLE_NUMBER, normalize the |
170 base::string16 normalized_number = | 175 // |text| based on the app_locale before comparing it to the whole number. For |
171 i18n::NormalizePhoneNumber(text, GetRegion(*profile_, app_locale)); | 176 // example, the France number "33 2 49 19 70 70" would be normalized to |
172 if (normalized_number == whole_number) | 177 // "+33249197070" whereas the US number "+1 (234) 567-8901" would be |
173 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); | 178 // normalized to "12345678901". |
| 179 if (matching_types->find(PHONE_HOME_WHOLE_NUMBER) == matching_types->end()) { |
| 180 base::string16 whole_number = |
| 181 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), app_locale); |
| 182 if (!whole_number.empty()) { |
| 183 base::string16 normalized_number = |
| 184 i18n::NormalizePhoneNumber(text, GetRegion(*profile_, app_locale)); |
| 185 if (normalized_number == whole_number) |
| 186 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); |
| 187 } |
| 188 } |
| 189 |
| 190 // If both PHONE_HOME_CITY_AND_NUMBER and PHONE_HOME_WHOLE_NUMBER are matched, |
| 191 // it means there is no country code in the profile's phone number. In that |
| 192 // case, we should only return PHONE_HOME_CITY_AND_NUMBER because it's more |
| 193 // precise. |
| 194 ServerFieldTypeSet::iterator whole_number_iterator = |
| 195 matching_types->find(PHONE_HOME_WHOLE_NUMBER); |
| 196 if (whole_number_iterator != matching_types->end() && |
| 197 matching_types->find(PHONE_HOME_CITY_AND_NUMBER) != |
| 198 matching_types->end()) { |
| 199 matching_types->erase(whole_number_iterator); |
174 } | 200 } |
175 } | 201 } |
176 | 202 |
177 void PhoneNumber::UpdateCacheIfNeeded(const std::string& app_locale) const { | 203 void PhoneNumber::UpdateCacheIfNeeded(const std::string& app_locale) const { |
178 std::string region = GetRegion(*profile_, app_locale); | 204 std::string region = GetRegion(*profile_, app_locale); |
179 if (!number_.empty() && cached_parsed_phone_.region() != region) | 205 if (!number_.empty() && cached_parsed_phone_.region() != region) |
180 cached_parsed_phone_ = i18n::PhoneObject(number_, region); | 206 cached_parsed_phone_ = i18n::PhoneObject(number_, region); |
181 } | 207 } |
182 | 208 |
183 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { | 209 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 | 257 |
232 return i18n::ConstructPhoneNumber( | 258 return i18n::ConstructPhoneNumber( |
233 country_, city_, phone_, GetRegion(profile, app_locale), value); | 259 country_, city_, phone_, GetRegion(profile, app_locale), value); |
234 } | 260 } |
235 | 261 |
236 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { | 262 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { |
237 return phone_.empty() && whole_number_.empty(); | 263 return phone_.empty() && whole_number_.empty(); |
238 } | 264 } |
239 | 265 |
240 } // namespace autofill | 266 } // namespace autofill |
OLD | NEW |