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 "content/browser/browser_thread.h" | |
| 13 #include "third_party/libphonenumber/cpp/src/phonenumberutil.h" | 14 #include "third_party/libphonenumber/cpp/src/phonenumberutil.h" |
| 15 #include "unicode/locid.h" | |
| 14 | 16 |
| 15 using i18n::phonenumbers::PhoneNumber; | 17 using i18n::phonenumbers::PhoneNumber; |
| 16 using i18n::phonenumbers::PhoneNumberUtil; | 18 using i18n::phonenumbers::PhoneNumberUtil; |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 std::string SanitizeLocaleCode(const std::string& locale_code) { | 22 std::string SanitizeLocaleCode(const std::string& locale_code) { |
| 21 if (locale_code.length() == 2) | 23 if (locale_code.length() == 2) |
| 22 return locale_code; | 24 return locale_code; |
| 23 | 25 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 | 225 |
| 224 // Parse phone numbers based on the locale | 226 // Parse phone numbers based on the locale |
| 225 PhoneNumber i18n_number1; | 227 PhoneNumber i18n_number1; |
| 226 if (phone_util->Parse(UTF16ToUTF8(number_a), locale.c_str(), &i18n_number1) != | 228 if (phone_util->Parse(UTF16ToUTF8(number_a), locale.c_str(), &i18n_number1) != |
| 227 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { | 229 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { |
| 228 return false; | 230 return false; |
| 229 } | 231 } |
| 230 | 232 |
| 231 PhoneNumber i18n_number2; | 233 PhoneNumber i18n_number2; |
| 232 if (phone_util->Parse(UTF16ToUTF8(number_b), locale.c_str(), &i18n_number2) != | 234 if (phone_util->Parse(UTF16ToUTF8(number_b), locale.c_str(), &i18n_number2) != |
| 233 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { | 235 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { |
|
dhollowa
2011/10/20 15:34:27
nit: indent seems off here. The 'i' should line u
| |
| 234 return false; | 236 return false; |
| 235 } | 237 } |
| 236 | 238 |
| 237 switch (phone_util->IsNumberMatch(i18n_number1, i18n_number2)) { | 239 switch (phone_util->IsNumberMatch(i18n_number1, i18n_number2)) { |
| 238 case i18n::phonenumbers::PhoneNumberUtil::INVALID_NUMBER: | 240 case i18n::phonenumbers::PhoneNumberUtil::INVALID_NUMBER: |
| 239 case i18n::phonenumbers::PhoneNumberUtil::NO_MATCH: | 241 case i18n::phonenumbers::PhoneNumberUtil::NO_MATCH: |
| 240 return false; | 242 return false; |
| 241 case i18n::phonenumbers::PhoneNumberUtil::SHORT_NSN_MATCH: | 243 case i18n::phonenumbers::PhoneNumberUtil::SHORT_NSN_MATCH: |
| 242 return false; | 244 return false; |
| 243 case i18n::phonenumbers::PhoneNumberUtil::NSN_MATCH: | 245 case i18n::phonenumbers::PhoneNumberUtil::NSN_MATCH: |
| 244 case i18n::phonenumbers::PhoneNumberUtil::EXACT_MATCH: | 246 case i18n::phonenumbers::PhoneNumberUtil::EXACT_MATCH: |
| 245 return true; | 247 return true; |
| 246 default: | 248 default: |
| 247 NOTREACHED(); | 249 NOTREACHED(); |
| 248 } | 250 } |
| 249 | 251 |
| 250 return false; | 252 return false; |
| 251 } | 253 } |
| 252 | 254 |
| 253 PhoneObject::PhoneObject(const string16& number, const std::string& locale) | 255 PhoneObject::PhoneObject(const string16& number, const std::string& locale) |
| 254 : locale_(SanitizeLocaleCode(locale)), | 256 : locale_(locale), |
| 255 i18n_number_(NULL) { | 257 i18n_number_(NULL) { |
| 258 if (locale_.empty()) { | |
| 259 // TODO(isherman): Autofill profiles should always have a locale set, but in | |
| 260 // some cases it should be marked as implicit. Otherwise, phone numbers | |
| 261 // might behave differently when they are synced across computers: | |
| 262 // http://crbug.com/100845 | |
| 263 | |
| 264 // For now, to avoid a data race on shutdown, make sure that we always | |
| 265 // provide a non-empty locale: http://crbug.com/100745 | |
| 266 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) | |
| 267 locale_ = AutofillCountry::ApplicationLocale(); | |
| 268 else | |
| 269 locale_ = icu::Locale::getDefault().getCountry(); | |
| 270 | |
| 271 if (locale_.empty()) | |
| 272 locale_ = "US"; | |
| 273 } | |
| 274 locale_ = SanitizeLocaleCode(locale_); | |
| 275 | |
| 256 scoped_ptr<PhoneNumber> i18n_number(new PhoneNumber); | 276 scoped_ptr<PhoneNumber> i18n_number(new PhoneNumber); |
| 257 if (ParsePhoneNumberInternal(number, locale_, &country_code_, &city_code_, | 277 if (ParsePhoneNumberInternal(number, locale_, &country_code_, &city_code_, |
| 258 &number_, i18n_number.get())) { | 278 &number_, i18n_number.get())) { |
| 259 // Phone successfully parsed - set |i18n_number_| object, |whole_number_| | 279 // Phone successfully parsed - set |i18n_number_| object, |whole_number_| |
| 260 // will be set on the first call to GetWholeNumber(). | 280 // will be set on the first call to GetWholeNumber(). |
| 261 i18n_number_.reset(i18n_number.release()); | 281 i18n_number_.reset(i18n_number.release()); |
| 262 } else { | 282 } else { |
| 263 // Parsing failed. Store passed phone "as is" into |whole_number_|. | 283 // Parsing failed. Store passed phone "as is" into |whole_number_|. |
| 264 whole_number_ = number; | 284 whole_number_ = number; |
| 265 } | 285 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 number_ = other.number_; | 319 number_ = other.number_; |
| 300 locale_ = other.locale_; | 320 locale_ = other.locale_; |
| 301 if (other.i18n_number_.get()) | 321 if (other.i18n_number_.get()) |
| 302 i18n_number_.reset(new PhoneNumber(*other.i18n_number_)); | 322 i18n_number_.reset(new PhoneNumber(*other.i18n_number_)); |
| 303 | 323 |
| 304 return *this; | 324 return *this; |
| 305 } | 325 } |
| 306 | 326 |
| 307 } // namespace autofill_i18n | 327 } // namespace autofill_i18n |
| 308 | 328 |
| OLD | NEW |