| 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 "chrome/browser/autofill/android/phone_number_util_android.h" | 5 #include "chrome/browser/autofill/android/phone_number_util_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "components/autofill/core/browser/autofill_country.h" | 10 #include "components/autofill/core/browser/autofill_country.h" |
| 11 #include "jni/PhoneNumberUtil_jni.h" | 11 #include "jni/PhoneNumberUtil_jni.h" |
| 12 #include "third_party/libphonenumber/phonenumber_api.h" | 12 #include "third_party/libphonenumber/phonenumber_api.h" |
| 13 | 13 |
| 14 namespace autofill { | 14 namespace autofill { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 using ::base::android::ConvertJavaStringToUTF8; | 17 using ::base::android::ConvertJavaStringToUTF8; |
| 18 using ::base::android::ConvertUTF8ToJavaString; | 18 using ::base::android::ConvertUTF8ToJavaString; |
| 19 using ::base::android::JavaParamRef; | 19 using ::base::android::JavaParamRef; |
| 20 using ::base::android::ScopedJavaLocalRef; | 20 using ::base::android::ScopedJavaLocalRef; |
| 21 using ::i18n::phonenumbers::PhoneNumber; | 21 using ::i18n::phonenumbers::PhoneNumber; |
| 22 using ::i18n::phonenumbers::PhoneNumberUtil; | 22 using ::i18n::phonenumbers::PhoneNumberUtil; |
| 23 } // namespace | |
| 24 | 23 |
| 25 // Formats the given number |jphone_number| to | 24 // Formats the |phone_number| to the specified |format|. Returns the original |
| 26 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format | 25 // number if the operation is not possible. |
| 27 // by using i18n::phonenumbers::PhoneNumberUtil::Format. | 26 std::string FormatPhoneNumber(const std::string& phone_number, |
| 28 ScopedJavaLocalRef<jstring> Format( | 27 PhoneNumberUtil::PhoneNumberFormat format) { |
| 29 JNIEnv* env, | |
| 30 const base::android::JavaParamRef<jclass>& jcaller, | |
| 31 const JavaParamRef<jstring>& jphone_number) { | |
| 32 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | |
| 33 const std::string default_region_code = | 28 const std::string default_region_code = |
| 34 autofill::AutofillCountry::CountryCodeForLocale( | 29 autofill::AutofillCountry::CountryCodeForLocale( |
| 35 g_browser_process->GetApplicationLocale()); | 30 g_browser_process->GetApplicationLocale()); |
| 36 | 31 |
| 37 PhoneNumber parsed_number; | 32 PhoneNumber parsed_number; |
| 38 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | 33 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); |
| 39 if (phone_number_util->Parse(phone_number, default_region_code, | 34 if (phone_number_util->Parse(phone_number, default_region_code, |
| 40 &parsed_number) != | 35 &parsed_number) != |
| 41 PhoneNumberUtil::NO_PARSING_ERROR) { | 36 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 42 return ConvertUTF8ToJavaString(env, phone_number); | 37 return phone_number; |
| 43 } | 38 } |
| 44 | 39 |
| 45 std::string formatted_number; | 40 std::string formatted_number; |
| 46 phone_number_util->Format(parsed_number, | 41 phone_number_util->Format(parsed_number, format, &formatted_number); |
| 47 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL, | 42 return formatted_number; |
| 48 &formatted_number); | 43 } |
| 49 | 44 |
| 50 return ConvertUTF8ToJavaString(env, formatted_number); | 45 } // namespace |
| 46 |
| 47 // Formats the given number |jphone_number| to |
| 48 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format |
| 49 // by using i18n::phonenumbers::PhoneNumberUtil::Format. |
| 50 ScopedJavaLocalRef<jstring> FormatForDisplay( |
| 51 JNIEnv* env, |
| 52 const base::android::JavaParamRef<jclass>& jcaller, |
| 53 const JavaParamRef<jstring>& jphone_number) { |
| 54 return ConvertUTF8ToJavaString( |
| 55 env, |
| 56 FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), |
| 57 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL)); |
| 58 } |
| 59 |
| 60 // Formats the given number |jphone_number| to |
| 61 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164 format by using |
| 62 // i18n::phonenumbers::PhoneNumberUtil::Format , as defined in the Payment |
| 63 // Request spec |
| 64 // (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm) |
| 65 ScopedJavaLocalRef<jstring> FormatForResponse( |
| 66 JNIEnv* env, |
| 67 const base::android::JavaParamRef<jclass>& jcaller, |
| 68 const JavaParamRef<jstring>& jphone_number) { |
| 69 return ConvertUTF8ToJavaString( |
| 70 env, FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), |
| 71 PhoneNumberUtil::PhoneNumberFormat::E164)); |
| 51 } | 72 } |
| 52 | 73 |
| 53 // Checks whether the given number |jphone_number| is valid by using | 74 // Checks whether the given number |jphone_number| is valid by using |
| 54 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. | 75 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. |
| 55 jboolean IsValidNumber(JNIEnv* env, | 76 jboolean IsValidNumber(JNIEnv* env, |
| 56 const base::android::JavaParamRef<jclass>& jcaller, | 77 const base::android::JavaParamRef<jclass>& jcaller, |
| 57 const JavaParamRef<jstring>& jphone_number) { | 78 const JavaParamRef<jstring>& jphone_number) { |
| 58 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | 79 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); |
| 59 const std::string default_region_code = | 80 const std::string default_region_code = |
| 60 autofill::AutofillCountry::CountryCodeForLocale( | 81 autofill::AutofillCountry::CountryCodeForLocale( |
| 61 g_browser_process->GetApplicationLocale()); | 82 g_browser_process->GetApplicationLocale()); |
| 62 | 83 |
| 63 PhoneNumber parsed_number; | 84 PhoneNumber parsed_number; |
| 64 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | 85 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); |
| 65 if (phone_number_util->Parse(phone_number, default_region_code, | 86 if (phone_number_util->Parse(phone_number, default_region_code, |
| 66 &parsed_number) != | 87 &parsed_number) != |
| 67 PhoneNumberUtil::NO_PARSING_ERROR) { | 88 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 68 return false; | 89 return false; |
| 69 } | 90 } |
| 70 | 91 |
| 71 return phone_number_util->IsValidNumber(parsed_number); | 92 return phone_number_util->IsValidNumber(parsed_number); |
| 72 } | 93 } |
| 73 | 94 |
| 74 } // namespace autofill | 95 } // namespace autofill |
| 75 | 96 |
| 76 // static | 97 // static |
| 77 bool RegisterPhoneNumberUtil(JNIEnv* env) { | 98 bool RegisterPhoneNumberUtil(JNIEnv* env) { |
| 78 return autofill::RegisterNativesImpl(env); | 99 return autofill::RegisterNativesImpl(env); |
| 79 } | 100 } |
| OLD | NEW |