| 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_i18n.h" | 5 #include "components/autofill/core/browser/phone_number_i18n.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 PhoneNumberUtil::PhoneNumberFormat format = | 59 PhoneNumberUtil::PhoneNumberFormat format = |
| 60 country_code.empty() ? | 60 country_code.empty() ? |
| 61 PhoneNumberUtil::NATIONAL : | 61 PhoneNumberUtil::NATIONAL : |
| 62 PhoneNumberUtil::INTERNATIONAL; | 62 PhoneNumberUtil::INTERNATIONAL; |
| 63 | 63 |
| 64 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 64 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 65 std::string processed_number; | 65 std::string processed_number; |
| 66 phone_util->Format(number, format, &processed_number); | 66 phone_util->Format(number, format, &processed_number); |
| 67 | 67 |
| 68 if (formatted_number) | 68 if (formatted_number) |
| 69 *formatted_number = UTF8ToUTF16(processed_number); | 69 *formatted_number = base::UTF8ToUTF16(processed_number); |
| 70 | 70 |
| 71 if (normalized_number) { | 71 if (normalized_number) { |
| 72 phone_util->NormalizeDigitsOnly(&processed_number); | 72 phone_util->NormalizeDigitsOnly(&processed_number); |
| 73 *normalized_number = UTF8ToUTF16(processed_number); | 73 *normalized_number = base::UTF8ToUTF16(processed_number); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 } // namespace | 77 } // namespace |
| 78 | 78 |
| 79 namespace i18n { | 79 namespace i18n { |
| 80 | 80 |
| 81 // Parses the number stored in |value| as it should be interpreted in the given | 81 // Parses the number stored in |value| as it should be interpreted in the given |
| 82 // |region|, and stores the results into the remaining arguments. The |region| | 82 // |region|, and stores the results into the remaining arguments. The |region| |
| 83 // should be sanitized prior to calling this function. | 83 // should be sanitized prior to calling this function. |
| 84 bool ParsePhoneNumber(const base::string16& value, | 84 bool ParsePhoneNumber(const base::string16& value, |
| 85 const std::string& region, | 85 const std::string& region, |
| 86 base::string16* country_code, | 86 base::string16* country_code, |
| 87 base::string16* city_code, | 87 base::string16* city_code, |
| 88 base::string16* number, | 88 base::string16* number, |
| 89 PhoneNumber* i18n_number) { | 89 PhoneNumber* i18n_number) { |
| 90 country_code->clear(); | 90 country_code->clear(); |
| 91 city_code->clear(); | 91 city_code->clear(); |
| 92 number->clear(); | 92 number->clear(); |
| 93 *i18n_number = PhoneNumber(); | 93 *i18n_number = PhoneNumber(); |
| 94 | 94 |
| 95 std::string number_text(UTF16ToUTF8(value)); | 95 std::string number_text(base::UTF16ToUTF8(value)); |
| 96 | 96 |
| 97 // Parse phone number based on the region. | 97 // Parse phone number based on the region. |
| 98 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 98 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 99 | 99 |
| 100 // The |region| should already be sanitized. | 100 // The |region| should already be sanitized. |
| 101 DCHECK_EQ(2U, region.size()); | 101 DCHECK_EQ(2U, region.size()); |
| 102 if (phone_util->Parse(number_text, region.c_str(), i18n_number) != | 102 if (phone_util->Parse(number_text, region.c_str(), i18n_number) != |
| 103 PhoneNumberUtil::NO_PARSING_ERROR) { | 103 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 104 return false; | 104 return false; |
| 105 } | 105 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 121 area_length = destination_length; | 121 area_length = destination_length; |
| 122 | 122 |
| 123 std::string area_code; | 123 std::string area_code; |
| 124 std::string subscriber_number; | 124 std::string subscriber_number; |
| 125 if (area_length > 0) { | 125 if (area_length > 0) { |
| 126 area_code = national_significant_number.substr(0, area_length); | 126 area_code = national_significant_number.substr(0, area_length); |
| 127 subscriber_number = national_significant_number.substr(area_length); | 127 subscriber_number = national_significant_number.substr(area_length); |
| 128 } else { | 128 } else { |
| 129 subscriber_number = national_significant_number; | 129 subscriber_number = national_significant_number; |
| 130 } | 130 } |
| 131 *number = UTF8ToUTF16(subscriber_number); | 131 *number = base::UTF8ToUTF16(subscriber_number); |
| 132 *city_code = UTF8ToUTF16(area_code); | 132 *city_code = base::UTF8ToUTF16(area_code); |
| 133 *country_code = base::string16(); | 133 *country_code = base::string16(); |
| 134 | 134 |
| 135 phone_util->NormalizeDigitsOnly(&number_text); | 135 phone_util->NormalizeDigitsOnly(&number_text); |
| 136 base::string16 normalized_number(UTF8ToUTF16(number_text)); | 136 base::string16 normalized_number(base::UTF8ToUTF16(number_text)); |
| 137 | 137 |
| 138 // Check if parsed number has a country code that was not inferred from the | 138 // Check if parsed number has a country code that was not inferred from the |
| 139 // region. | 139 // region. |
| 140 if (i18n_number->has_country_code()) { | 140 if (i18n_number->has_country_code()) { |
| 141 *country_code = UTF8ToUTF16( | 141 *country_code = base::UTF8ToUTF16( |
| 142 base::StringPrintf("%d", i18n_number->country_code())); | 142 base::StringPrintf("%d", i18n_number->country_code())); |
| 143 if (normalized_number.length() <= national_significant_number.length() && | 143 if (normalized_number.length() <= national_significant_number.length() && |
| 144 !StartsWith(normalized_number, *country_code, | 144 !StartsWith(normalized_number, *country_code, |
| 145 true /* case_sensitive */)) { | 145 true /* case_sensitive */)) { |
| 146 country_code->clear(); | 146 country_code->clear(); |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 return true; | 150 return true; |
| 151 } | 151 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 const base::string16& number_b, | 193 const base::string16& number_b, |
| 194 const std::string& raw_region, | 194 const std::string& raw_region, |
| 195 const std::string& app_locale) { | 195 const std::string& app_locale) { |
| 196 // Sanitize the provided |raw_region| before trying to use it for parsing. | 196 // Sanitize the provided |raw_region| before trying to use it for parsing. |
| 197 const std::string region = SanitizeRegion(raw_region, app_locale); | 197 const std::string region = SanitizeRegion(raw_region, app_locale); |
| 198 | 198 |
| 199 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 199 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 200 | 200 |
| 201 // Parse phone numbers based on the region | 201 // Parse phone numbers based on the region |
| 202 PhoneNumber i18n_number1; | 202 PhoneNumber i18n_number1; |
| 203 if (phone_util->Parse(UTF16ToUTF8(number_a), region.c_str(), &i18n_number1) != | 203 if (phone_util->Parse( |
| 204 PhoneNumberUtil::NO_PARSING_ERROR) { | 204 base::UTF16ToUTF8(number_a), region.c_str(), &i18n_number1) != |
| 205 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 205 return false; | 206 return false; |
| 206 } | 207 } |
| 207 | 208 |
| 208 PhoneNumber i18n_number2; | 209 PhoneNumber i18n_number2; |
| 209 if (phone_util->Parse(UTF16ToUTF8(number_b), region.c_str(), &i18n_number2) != | 210 if (phone_util->Parse( |
| 210 PhoneNumberUtil::NO_PARSING_ERROR) { | 211 base::UTF16ToUTF8(number_b), region.c_str(), &i18n_number2) != |
| 212 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 211 return false; | 213 return false; |
| 212 } | 214 } |
| 213 | 215 |
| 214 switch (phone_util->IsNumberMatch(i18n_number1, i18n_number2)) { | 216 switch (phone_util->IsNumberMatch(i18n_number1, i18n_number2)) { |
| 215 case PhoneNumberUtil::INVALID_NUMBER: | 217 case PhoneNumberUtil::INVALID_NUMBER: |
| 216 case PhoneNumberUtil::NO_MATCH: | 218 case PhoneNumberUtil::NO_MATCH: |
| 217 return false; | 219 return false; |
| 218 case PhoneNumberUtil::SHORT_NSN_MATCH: | 220 case PhoneNumberUtil::SHORT_NSN_MATCH: |
| 219 return false; | 221 return false; |
| 220 case PhoneNumberUtil::NSN_MATCH: | 222 case PhoneNumberUtil::NSN_MATCH: |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 number_ = other.number_; | 299 number_ = other.number_; |
| 298 | 300 |
| 299 formatted_number_ = other.formatted_number_; | 301 formatted_number_ = other.formatted_number_; |
| 300 whole_number_ = other.whole_number_; | 302 whole_number_ = other.whole_number_; |
| 301 | 303 |
| 302 return *this; | 304 return *this; |
| 303 } | 305 } |
| 304 | 306 |
| 305 } // namespace i18n | 307 } // namespace i18n |
| 306 } // namespace autofill | 308 } // namespace autofill |
| OLD | NEW |