| 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_i18n.h" | 5 #include "chrome/browser/autofill/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/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autofill/autofill_country.h" | 12 #include "chrome/browser/autofill/autofill_country.h" |
| 13 #include "third_party/libphonenumber/cpp/src/phonenumberutil.h" | 13 #include "third_party/libphonenumber/src/phonenumber_api.h" |
| 14 | 14 |
| 15 using i18n::phonenumbers::PhoneNumber; | 15 using i18n::phonenumbers::PhoneNumber; |
| 16 using i18n::phonenumbers::PhoneNumberUtil; | 16 using i18n::phonenumbers::PhoneNumberUtil; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 std::string SanitizeLocaleCode(const std::string& locale_code) { | 20 std::string SanitizeLocaleCode(const std::string& locale_code) { |
| 21 if (locale_code.length() == 2) | 21 if (locale_code.length() == 2) |
| 22 return locale_code; | 22 return locale_code; |
| 23 | 23 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 if (area_length > 0) { | 101 if (area_length > 0) { |
| 102 area_code = national_significant_number.substr(0, area_length); | 102 area_code = national_significant_number.substr(0, area_length); |
| 103 subscriber_number = national_significant_number.substr(area_length); | 103 subscriber_number = national_significant_number.substr(area_length); |
| 104 } else { | 104 } else { |
| 105 subscriber_number = national_significant_number; | 105 subscriber_number = national_significant_number; |
| 106 } | 106 } |
| 107 *number = UTF8ToUTF16(subscriber_number); | 107 *number = UTF8ToUTF16(subscriber_number); |
| 108 *city_code = UTF8ToUTF16(area_code); | 108 *city_code = UTF8ToUTF16(area_code); |
| 109 *country_code = string16(); | 109 *country_code = string16(); |
| 110 | 110 |
| 111 PhoneNumberUtil::NormalizeDigitsOnly(&number_text); | 111 phone_util->NormalizeDigitsOnly(&number_text); |
| 112 string16 normalized_number(UTF8ToUTF16(number_text)); | 112 string16 normalized_number(UTF8ToUTF16(number_text)); |
| 113 // Check if parsed number has country code and it was not inferred from the | 113 // Check if parsed number has country code and it was not inferred from the |
| 114 // locale. | 114 // locale. |
| 115 if (i18n_number->has_country_code()) { | 115 if (i18n_number->has_country_code()) { |
| 116 *country_code = UTF8ToUTF16( | 116 *country_code = UTF8ToUTF16( |
| 117 base::StringPrintf("%d", i18n_number->country_code())); | 117 base::StringPrintf("%d", i18n_number->country_code())); |
| 118 if (normalized_number.length() <= national_significant_number.length() && | 118 if (normalized_number.length() <= national_significant_number.length() && |
| 119 (normalized_number.length() < country_code->length() || | 119 (normalized_number.length() < country_code->length() || |
| 120 normalized_number.compare(0, country_code->length(), *country_code))) { | 120 normalized_number.compare(0, country_code->length(), *country_code))) { |
| 121 country_code->clear(); | 121 country_code->clear(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 141 if (!autofill_i18n::ConstructPhoneNumber( | 141 if (!autofill_i18n::ConstructPhoneNumber( |
| 142 country_code, city_code, number, | 142 country_code, city_code, number, |
| 143 locale, | 143 locale, |
| 144 (country_code.empty() ? | 144 (country_code.empty() ? |
| 145 autofill_i18n::NATIONAL : autofill_i18n::INTERNATIONAL), | 145 autofill_i18n::NATIONAL : autofill_i18n::INTERNATIONAL), |
| 146 &result)) { | 146 &result)) { |
| 147 // Reconstruction failed - do not store phone. | 147 // Reconstruction failed - do not store phone. |
| 148 return string16(); | 148 return string16(); |
| 149 } | 149 } |
| 150 std::string result_utf8(UTF16ToUTF8(result)); | 150 std::string result_utf8(UTF16ToUTF8(result)); |
| 151 PhoneNumberUtil::NormalizeDigitsOnly(&result_utf8); | 151 PhoneNumberUtil::GetInstance()->NormalizeDigitsOnly(&result_utf8); |
| 152 return UTF8ToUTF16(result_utf8); | 152 return UTF8ToUTF16(result_utf8); |
| 153 } | 153 } |
| 154 | 154 |
| 155 bool ParsePhoneNumber(const string16& value, | 155 bool ParsePhoneNumber(const string16& value, |
| 156 const std::string& locale, | 156 const std::string& locale, |
| 157 string16* country_code, | 157 string16* country_code, |
| 158 string16* city_code, | 158 string16* city_code, |
| 159 string16* number) { | 159 string16* number) { |
| 160 PhoneNumber i18n_number; | 160 PhoneNumber i18n_number; |
| 161 return ParsePhoneNumberInternal(value, SanitizeLocaleCode(locale), | 161 return ParsePhoneNumberInternal(value, SanitizeLocaleCode(locale), |
| 162 country_code, city_code, number, | 162 country_code, city_code, number, |
| 163 &i18n_number); | 163 &i18n_number); |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool ConstructPhoneNumber(const string16& country_code, | 166 bool ConstructPhoneNumber(const string16& country_code, |
| 167 const string16& city_code, | 167 const string16& city_code, |
| 168 const string16& number, | 168 const string16& number, |
| 169 const std::string& locale, | 169 const std::string& locale, |
| 170 FullPhoneFormat phone_format, | 170 FullPhoneFormat phone_format, |
| 171 string16* whole_number) { | 171 string16* whole_number) { |
| 172 DCHECK(whole_number); | 172 DCHECK(whole_number); |
| 173 | 173 |
| 174 whole_number->clear(); | 174 whole_number->clear(); |
| 175 | 175 |
| 176 std::string normalized_number(UTF16ToUTF8(city_code)); | 176 std::string normalized_number(UTF16ToUTF8(city_code)); |
| 177 normalized_number.append(UTF16ToUTF8(number)); | 177 normalized_number.append(UTF16ToUTF8(number)); |
| 178 | 178 |
| 179 PhoneNumberUtil::NormalizeDigitsOnly(&normalized_number); | 179 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 180 |
| 181 phone_util->NormalizeDigitsOnly(&normalized_number); |
| 180 | 182 |
| 181 int64 number_int = 0; | 183 int64 number_int = 0; |
| 182 if (!base::StringToInt64(normalized_number, &number_int) || !number_int) | 184 if (!base::StringToInt64(normalized_number, &number_int) || !number_int) |
| 183 return false; | 185 return false; |
| 184 | 186 |
| 185 PhoneNumber i18n_number; | 187 PhoneNumber i18n_number; |
| 186 i18n_number.set_national_number(static_cast<uint64>(number_int)); | 188 i18n_number.set_national_number(static_cast<uint64>(number_int)); |
| 187 | 189 |
| 188 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | |
| 189 | |
| 190 int country_int = phone_util->GetCountryCodeForRegion( | 190 int country_int = phone_util->GetCountryCodeForRegion( |
| 191 SanitizeLocaleCode(locale)); | 191 SanitizeLocaleCode(locale)); |
| 192 if (!country_code.empty() && !base::StringToInt(country_code, &country_int)) | 192 if (!country_code.empty() && !base::StringToInt(country_code, &country_int)) |
| 193 return false; | 193 return false; |
| 194 if (country_int) | 194 if (country_int) |
| 195 i18n_number.set_country_code(country_int); | 195 i18n_number.set_country_code(country_int); |
| 196 | 196 |
| 197 i18n::phonenumbers::PhoneNumberUtil::ValidationResult validation = | 197 i18n::phonenumbers::PhoneNumberUtil::ValidationResult validation = |
| 198 phone_util->IsPossibleNumberWithReason(i18n_number); | 198 phone_util->IsPossibleNumberWithReason(i18n_number); |
| 199 if (validation != i18n::phonenumbers::PhoneNumberUtil::IS_POSSIBLE) | 199 if (validation != i18n::phonenumbers::PhoneNumberUtil::IS_POSSIBLE) |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 number_ = other.number_; | 305 number_ = other.number_; |
| 306 locale_ = other.locale_; | 306 locale_ = other.locale_; |
| 307 if (other.i18n_number_.get()) | 307 if (other.i18n_number_.get()) |
| 308 i18n_number_.reset(new PhoneNumber(*other.i18n_number_)); | 308 i18n_number_.reset(new PhoneNumber(*other.i18n_number_)); |
| 309 | 309 |
| 310 return *this; | 310 return *this; |
| 311 } | 311 } |
| 312 | 312 |
| 313 } // namespace autofill_i18n | 313 } // namespace autofill_i18n |
| 314 | 314 |
| OLD | NEW |