| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/payments/core/payment_request_data_util.h" | 5 #include "components/payments/core/payment_request_data_util.h" |
| 6 | 6 |
| 7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
| 8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_country.h" | 10 #include "components/autofill/core/browser/autofill_country.h" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 base::string16 GetFormattedPhoneNumberForDisplay( | 155 base::string16 GetFormattedPhoneNumberForDisplay( |
| 156 const autofill::AutofillProfile& profile, | 156 const autofill::AutofillProfile& profile, |
| 157 const std::string& locale) { | 157 const std::string& locale) { |
| 158 // Since the "+" is removed for some country's phone numbers, try to add a "+" |
| 159 // and see if it is a valid phone number for a country. |
| 160 // Having two "+" in front of a number has no effect on the formatted number. |
| 161 // The reason for this is international phone numbers for another country. For |
| 162 // example, without adding a "+", the US number 1-415-123-1234 for an AU |
| 163 // address would be wrongly formatted as +61 1-415-123-1234 which is invalid. |
| 164 std::string phone = base::UTF16ToUTF8(profile.GetInfo( |
| 165 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), locale)); |
| 166 std::string tentative_intl_phone = "+" + phone; |
| 167 |
| 168 autofill::i18n::PhoneObject phone_obj( |
| 169 base::UTF8ToUTF16(phone), GetCountryCodeWithFallback(&profile, locale)); |
| 170 autofill::i18n::PhoneObject tentative_intl_phone_obj( |
| 171 base::UTF8ToUTF16(tentative_intl_phone), |
| 172 GetCountryCodeWithFallback(&profile, locale)); |
| 173 |
| 174 // Always favor the tentative international phone number if it's determined as |
| 175 // being a valid number. |
| 176 if (!tentative_intl_phone_obj.region().empty()) { |
| 177 return base::UTF8ToUTF16(FormatPhoneForDisplay( |
| 178 tentative_intl_phone, GetCountryCodeWithFallback(&profile, locale))); |
| 179 } |
| 180 |
| 158 return base::UTF8ToUTF16(FormatPhoneForDisplay( | 181 return base::UTF8ToUTF16(FormatPhoneForDisplay( |
| 159 base::UTF16ToUTF8(profile.GetInfo( | 182 phone, GetCountryCodeWithFallback(&profile, locale))); |
| 160 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), locale)), | |
| 161 GetCountryCodeWithFallback(&profile, locale))); | |
| 162 } | 183 } |
| 163 | 184 |
| 164 std::string FormatPhoneForDisplay(const std::string& phone_number, | 185 std::string FormatPhoneForDisplay(const std::string& phone_number, |
| 165 const std::string& country_code) { | 186 const std::string& country_code) { |
| 166 return FormatPhoneNumber(phone_number, country_code, | 187 return FormatPhoneNumber(phone_number, country_code, |
| 167 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL); | 188 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL); |
| 168 } | 189 } |
| 169 | 190 |
| 170 std::string FormatPhoneForResponse(const std::string& phone_number, | 191 std::string FormatPhoneForResponse(const std::string& phone_number, |
| 171 const std::string& country_code) { | 192 const std::string& country_code) { |
| 172 return FormatPhoneNumber(phone_number, country_code, | 193 return FormatPhoneNumber(phone_number, country_code, |
| 173 PhoneNumberUtil::PhoneNumberFormat::E164); | 194 PhoneNumberUtil::PhoneNumberFormat::E164); |
| 174 } | 195 } |
| 175 | 196 |
| 176 std::string GetCountryCodeWithFallback(const autofill::AutofillProfile* profile, | 197 std::string GetCountryCodeWithFallback(const autofill::AutofillProfile* profile, |
| 177 const std::string& app_locale) { | 198 const std::string& app_locale) { |
| 178 std::string country_code = | 199 std::string country_code = |
| 179 base::UTF16ToUTF8(profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); | 200 base::UTF16ToUTF8(profile->GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); |
| 180 if (!autofill::data_util::IsValidCountryCode(country_code)) | 201 if (!autofill::data_util::IsValidCountryCode(country_code)) |
| 181 country_code = autofill::AutofillCountry::CountryCodeForLocale(app_locale); | 202 country_code = autofill::AutofillCountry::CountryCodeForLocale(app_locale); |
| 182 return country_code; | 203 return country_code; |
| 183 } | 204 } |
| 184 | 205 |
| 185 } // namespace data_util | 206 } // namespace data_util |
| 186 } // namespace payments | 207 } // namespace payments |
| OLD | NEW |