Chromium Code Reviews| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 const base::string16& country_code, | 56 const base::string16& country_code, |
| 57 base::string16* formatted_number, | 57 base::string16* formatted_number, |
| 58 base::string16* normalized_number) { | 58 base::string16* normalized_number) { |
| 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); | |
| 67 | 66 |
| 68 if (formatted_number) | 67 std::string region_code; |
| 69 *formatted_number = base::UTF8ToUTF16(processed_number); | 68 phone_util->GetRegionCodeForNumber(number, ®ion_code); |
| 69 | |
| 70 // Formats a phone number using the original phone number format that the | |
| 71 // number is parsed from. | |
| 72 // This applies to the following cases: | |
| 73 // The phone number is an international phone number and has a leading '+' | |
| 74 // sign. | |
| 75 // The number is from the default country. This happens mostly for numbers | |
| 76 // written in the national format. | |
| 77 if (number.has_country_code_source() && | |
| 78 ((number.country_code_source() == | |
| 79 PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN && | |
| 80 format == PhoneNumberUtil::INTERNATIONAL) || | |
| 81 number.country_code_source() == | |
| 82 PhoneNumber::FROM_DEFAULT_COUNTRY)) { | |
| 83 phone_util->FormatInOriginalFormat(number, region_code, &processed_number); | |
| 84 } else { | |
| 85 phone_util->Format(number, format, &processed_number); | |
| 86 } | |
| 87 | |
| 88 if (formatted_number) { | |
| 89 // Drop the leading '+' for US numbers as some US sites can't handle | |
| 90 // the "+" and in the US dialing "+1..." is the same as dialing "1...". | |
| 91 if (processed_number[0] == '+' && region_code == "US") { | |
| 92 *formatted_number = base::UTF8ToUTF16(processed_number.substr(1, | |
| 93 processed_number.length())); | |
|
Ilya Sherman
2014/07/11 19:15:46
nit: Please run git cl format to fix up the format
| |
| 94 } else { | |
| 95 *formatted_number = base::UTF8ToUTF16(processed_number); | |
| 96 } | |
| 97 } | |
| 70 | 98 |
| 71 if (normalized_number) { | 99 if (normalized_number) { |
| 72 phone_util->NormalizeDigitsOnly(&processed_number); | 100 // Keep the leading '+' while normalising numbers, excluding US numbers. |
| 101 if (processed_number[0] == '+' && region_code != "US") { | |
| 102 processed_number = processed_number.substr(1, processed_number.length()); | |
| 103 phone_util->NormalizeDigitsOnly(&processed_number); | |
| 104 processed_number.insert(processed_number.begin(), '+'); | |
| 105 } else { | |
| 106 phone_util->NormalizeDigitsOnly(&processed_number); | |
| 107 } | |
| 73 *normalized_number = base::UTF8ToUTF16(processed_number); | 108 *normalized_number = base::UTF8ToUTF16(processed_number); |
| 74 } | 109 } |
| 75 } | 110 } |
| 76 | 111 |
| 77 } // namespace | 112 } // namespace |
| 78 | 113 |
| 79 namespace i18n { | 114 namespace i18n { |
| 80 | 115 |
| 81 // Parses the number stored in |value| as it should be interpreted in the given | 116 // Parses the number stored in |value| as it should be interpreted in the given |
| 82 // |default_region|, and stores the results into the remaining arguments. | 117 // |default_region|, and stores the results into the remaining arguments. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 93 number->clear(); | 128 number->clear(); |
| 94 *i18n_number = PhoneNumber(); | 129 *i18n_number = PhoneNumber(); |
| 95 | 130 |
| 96 std::string number_text(base::UTF16ToUTF8(value)); | 131 std::string number_text(base::UTF16ToUTF8(value)); |
| 97 | 132 |
| 98 // Parse phone number based on the region. | 133 // Parse phone number based on the region. |
| 99 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 134 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
| 100 | 135 |
| 101 // The |default_region| should already be sanitized. | 136 // The |default_region| should already be sanitized. |
| 102 DCHECK_EQ(2U, default_region.size()); | 137 DCHECK_EQ(2U, default_region.size()); |
| 103 if (phone_util->Parse(number_text, default_region, i18n_number) != | 138 if (phone_util->ParseAndKeepRawInput(number_text, |
| 139 default_region, | |
| 140 i18n_number) != | |
| 104 PhoneNumberUtil::NO_PARSING_ERROR) { | 141 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 105 return false; | 142 return false; |
| 106 } | 143 } |
| 107 | 144 |
| 108 if (!IsValidPhoneNumber(*i18n_number)) | 145 if (!IsValidPhoneNumber(*i18n_number)) |
| 109 return false; | 146 return false; |
| 110 | 147 |
| 111 std::string national_significant_number; | 148 std::string national_significant_number; |
| 112 phone_util->GetNationalSignificantNumber(*i18n_number, | 149 phone_util->GetNationalSignificantNumber(*i18n_number, |
| 113 &national_significant_number); | 150 &national_significant_number); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 number_ = other.number_; | 337 number_ = other.number_; |
| 301 | 338 |
| 302 formatted_number_ = other.formatted_number_; | 339 formatted_number_ = other.formatted_number_; |
| 303 whole_number_ = other.whole_number_; | 340 whole_number_ = other.whole_number_; |
| 304 | 341 |
| 305 return *this; | 342 return *this; |
| 306 } | 343 } |
| 307 | 344 |
| 308 } // namespace i18n | 345 } // namespace i18n |
| 309 } // namespace autofill | 346 } // namespace autofill |
| OLD | NEW |