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 nation format. | |
Ilya Sherman
2014/07/11 03:43:40
nit: "nation" -> "national"
| |
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 *formatted_number = (processed_number[0] == '+' && region_code == "US" ? | |
Ilya Sherman
2014/07/11 03:43:40
Please add comments to explain why the US is speci
| |
90 base::UTF8ToUTF16(processed_number.substr(1, | |
91 processed_number.length())) : | |
92 base::UTF8ToUTF16(processed_number)); | |
Ilya Sherman
2014/07/11 03:43:40
Hmm, this ternary expression is a bit hard to read
| |
93 } | |
70 | 94 |
71 if (normalized_number) { | 95 if (normalized_number) { |
72 phone_util->NormalizeDigitsOnly(&processed_number); | 96 // Keep the leading '+' while normalising numbers, excluding US numbers. |
97 if (processed_number[0] == '+' && region_code != "US") { | |
98 processed_number = processed_number.substr(1, processed_number.length()); | |
99 phone_util->NormalizeDigitsOnly(&processed_number); | |
100 processed_number.insert(processed_number.begin(), '+'); | |
101 } else { | |
102 phone_util->NormalizeDigitsOnly(&processed_number); | |
103 } | |
73 *normalized_number = base::UTF8ToUTF16(processed_number); | 104 *normalized_number = base::UTF8ToUTF16(processed_number); |
74 } | 105 } |
75 } | 106 } |
76 | 107 |
77 } // namespace | 108 } // namespace |
78 | 109 |
79 namespace i18n { | 110 namespace i18n { |
80 | 111 |
81 // Parses the number stored in |value| as it should be interpreted in the given | 112 // 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. | 113 // |default_region|, and stores the results into the remaining arguments. |
(...skipping 10 matching lines...) Expand all Loading... | |
93 number->clear(); | 124 number->clear(); |
94 *i18n_number = PhoneNumber(); | 125 *i18n_number = PhoneNumber(); |
95 | 126 |
96 std::string number_text(base::UTF16ToUTF8(value)); | 127 std::string number_text(base::UTF16ToUTF8(value)); |
97 | 128 |
98 // Parse phone number based on the region. | 129 // Parse phone number based on the region. |
99 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); | 130 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); |
100 | 131 |
101 // The |default_region| should already be sanitized. | 132 // The |default_region| should already be sanitized. |
102 DCHECK_EQ(2U, default_region.size()); | 133 DCHECK_EQ(2U, default_region.size()); |
103 if (phone_util->Parse(number_text, default_region, i18n_number) != | 134 if (phone_util->ParseAndKeepRawInput(number_text, |
135 default_region, | |
136 i18n_number) != | |
104 PhoneNumberUtil::NO_PARSING_ERROR) { | 137 PhoneNumberUtil::NO_PARSING_ERROR) { |
105 return false; | 138 return false; |
106 } | 139 } |
107 | 140 |
108 if (!IsValidPhoneNumber(*i18n_number)) | 141 if (!IsValidPhoneNumber(*i18n_number)) |
109 return false; | 142 return false; |
110 | 143 |
111 std::string national_significant_number; | 144 std::string national_significant_number; |
112 phone_util->GetNationalSignificantNumber(*i18n_number, | 145 phone_util->GetNationalSignificantNumber(*i18n_number, |
113 &national_significant_number); | 146 &national_significant_number); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 number_ = other.number_; | 333 number_ = other.number_; |
301 | 334 |
302 formatted_number_ = other.formatted_number_; | 335 formatted_number_ = other.formatted_number_; |
303 whole_number_ = other.whole_number_; | 336 whole_number_ = other.whole_number_; |
304 | 337 |
305 return *this; | 338 return *this; |
306 } | 339 } |
307 | 340 |
308 } // namespace i18n | 341 } // namespace i18n |
309 } // namespace autofill | 342 } // namespace autofill |
OLD | NEW |