| 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" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 country_code->clear(); | 61 country_code->clear(); |
| 62 | 62 |
| 63 std::string number_text(UTF16ToUTF8(value)); | 63 std::string number_text(UTF16ToUTF8(value)); |
| 64 | 64 |
| 65 // Parse phone number based on the locale. | 65 // Parse phone number based on the locale. |
| 66 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 66 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 67 | 67 |
| 68 // The |locale| should already be sanitized. | 68 // The |locale| should already be sanitized. |
| 69 DCHECK_EQ(2U, locale.size()); | 69 DCHECK_EQ(2U, locale.size()); |
| 70 if (phone_util->Parse(number_text, locale.c_str(), i18n_number) != | 70 if (phone_util->Parse(number_text, locale.c_str(), i18n_number) != |
| 71 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { | 71 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { |
| 72 return false; | 72 return false; |
| 73 } | 73 } |
| 74 | 74 |
| 75 i18n::phonenumbers::PhoneNumberUtil::ValidationResult validation = | 75 i18n::phonenumbers::PhoneNumberUtil::ValidationResult validation = |
| 76 phone_util->IsPossibleNumberWithReason(*i18n_number); | 76 phone_util->IsPossibleNumberWithReason(*i18n_number); |
| 77 if (validation != i18n::phonenumbers::PhoneNumberUtil::IS_POSSIBLE) | 77 if (validation != i18n::phonenumbers::PhoneNumberUtil::IS_POSSIBLE) |
| 78 return false; | 78 return false; |
| 79 | 79 |
| 80 // This verifies that number has a valid area code (that in some cases could | 80 // This verifies that number has a valid area code (that in some cases could |
| 81 // be empty) for parsed country code. Also verifies that this is a valid | 81 // be empty) for parsed country code. Also verifies that this is a valid |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 const string16& number_b, | 217 const string16& number_b, |
| 218 const std::string& country_code) { | 218 const std::string& country_code) { |
| 219 // Sanitize the provided |country_code| before trying to use it for parsing. | 219 // Sanitize the provided |country_code| before trying to use it for parsing. |
| 220 const std::string locale = SanitizeLocaleCode(country_code); | 220 const std::string locale = SanitizeLocaleCode(country_code); |
| 221 | 221 |
| 222 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 222 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 223 | 223 |
| 224 // Parse phone numbers based on the locale | 224 // Parse phone numbers based on the locale |
| 225 PhoneNumber i18n_number1; | 225 PhoneNumber i18n_number1; |
| 226 if (phone_util->Parse(UTF16ToUTF8(number_a), locale.c_str(), &i18n_number1) != | 226 if (phone_util->Parse(UTF16ToUTF8(number_a), locale.c_str(), &i18n_number1) != |
| 227 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { | 227 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { |
| 228 return false; | 228 return false; |
| 229 } | 229 } |
| 230 | 230 |
| 231 PhoneNumber i18n_number2; | 231 PhoneNumber i18n_number2; |
| 232 if (phone_util->Parse(UTF16ToUTF8(number_b), locale.c_str(), &i18n_number2) != | 232 if (phone_util->Parse(UTF16ToUTF8(number_b), locale.c_str(), &i18n_number2) != |
| 233 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { | 233 i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { |
| 234 return false; | 234 return false; |
| 235 } | 235 } |
| 236 | 236 |
| 237 switch (phone_util->IsNumberMatch(i18n_number1, i18n_number2)) { | 237 switch (phone_util->IsNumberMatch(i18n_number1, i18n_number2)) { |
| 238 case i18n::phonenumbers::PhoneNumberUtil::INVALID_NUMBER: | 238 case i18n::phonenumbers::PhoneNumberUtil::INVALID_NUMBER: |
| 239 case i18n::phonenumbers::PhoneNumberUtil::NO_MATCH: | 239 case i18n::phonenumbers::PhoneNumberUtil::NO_MATCH: |
| 240 return false; | 240 return false; |
| 241 case i18n::phonenumbers::PhoneNumberUtil::SHORT_NSN_MATCH: | 241 case i18n::phonenumbers::PhoneNumberUtil::SHORT_NSN_MATCH: |
| 242 return false; | 242 return false; |
| 243 case i18n::phonenumbers::PhoneNumberUtil::NSN_MATCH: | 243 case i18n::phonenumbers::PhoneNumberUtil::NSN_MATCH: |
| 244 case i18n::phonenumbers::PhoneNumberUtil::EXACT_MATCH: | 244 case i18n::phonenumbers::PhoneNumberUtil::EXACT_MATCH: |
| 245 return true; | 245 return true; |
| 246 default: | 246 default: |
| 247 NOTREACHED(); | 247 NOTREACHED(); |
| 248 } | 248 } |
| 249 | 249 |
| 250 return false; | 250 return false; |
| 251 } | 251 } |
| 252 | 252 |
| 253 PhoneObject::PhoneObject(const string16& number, const std::string& locale) | 253 PhoneObject::PhoneObject(const string16& number, const std::string& locale) |
| 254 : locale_(SanitizeLocaleCode(locale)), | 254 : locale_(SanitizeLocaleCode(locale)), |
| 255 i18n_number_(NULL) { | 255 i18n_number_(NULL) { |
| 256 // TODO(isherman): Autofill profiles should always have a |locale| set, but in |
| 257 // some cases it should be marked as implicit. Otherwise, phone numbers |
| 258 // might behave differently when they are synced across computers: |
| 259 // [ http://crbug.com/100845 ]. Once the bug is fixed, add a DCHECK here to |
| 260 // verify. |
| 261 |
| 256 scoped_ptr<PhoneNumber> i18n_number(new PhoneNumber); | 262 scoped_ptr<PhoneNumber> i18n_number(new PhoneNumber); |
| 257 if (ParsePhoneNumberInternal(number, locale_, &country_code_, &city_code_, | 263 if (ParsePhoneNumberInternal(number, locale_, &country_code_, &city_code_, |
| 258 &number_, i18n_number.get())) { | 264 &number_, i18n_number.get())) { |
| 259 // Phone successfully parsed - set |i18n_number_| object, |whole_number_| | 265 // Phone successfully parsed - set |i18n_number_| object, |whole_number_| |
| 260 // will be set on the first call to GetWholeNumber(). | 266 // will be set on the first call to GetWholeNumber(). |
| 261 i18n_number_.reset(i18n_number.release()); | 267 i18n_number_.reset(i18n_number.release()); |
| 262 } else { | 268 } else { |
| 263 // Parsing failed. Store passed phone "as is" into |whole_number_|. | 269 // Parsing failed. Store passed phone "as is" into |whole_number_|. |
| 264 whole_number_ = number; | 270 whole_number_ = number; |
| 265 } | 271 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 number_ = other.number_; | 305 number_ = other.number_; |
| 300 locale_ = other.locale_; | 306 locale_ = other.locale_; |
| 301 if (other.i18n_number_.get()) | 307 if (other.i18n_number_.get()) |
| 302 i18n_number_.reset(new PhoneNumber(*other.i18n_number_)); | 308 i18n_number_.reset(new PhoneNumber(*other.i18n_number_)); |
| 303 | 309 |
| 304 return *this; | 310 return *this; |
| 305 } | 311 } |
| 306 | 312 |
| 307 } // namespace autofill_i18n | 313 } // namespace autofill_i18n |
| 308 | 314 |
| OLD | NEW |