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