Chromium Code Reviews| 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 | 23 |
| 24 // Formats the |phone_number| to the specified |format|. Returns the original | 24 // Formats the |phone_number| to the specified |format| based on a given |
| 25 // number if the operation is not possible. | 25 // country code. Returns the original number if the operation is not possible. |
| 26 std::string FormatPhoneNumber(const std::string& phone_number, | 26 std::string FormatPhoneNumberWithCountryCode( |
| 27 PhoneNumberUtil::PhoneNumberFormat format) { | 27 const std::string& phone_number, |
| 28 const std::string default_region_code = | 28 const std::string& countryCode, |
| 29 autofill::AutofillCountry::CountryCodeForLocale( | 29 PhoneNumberUtil::PhoneNumberFormat format) { |
| 30 g_browser_process->GetApplicationLocale()); | |
| 31 | |
| 32 PhoneNumber parsed_number; | 30 PhoneNumber parsed_number; |
| 33 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | 31 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); |
| 34 if (phone_number_util->Parse(phone_number, default_region_code, | 32 if (phone_number_util->Parse(phone_number, countryCode, &parsed_number) != |
| 35 &parsed_number) != | |
| 36 PhoneNumberUtil::NO_PARSING_ERROR) { | 33 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 37 return phone_number; | 34 return phone_number; |
| 38 } | 35 } |
| 39 | 36 |
| 40 std::string formatted_number; | 37 std::string formatted_number; |
| 41 phone_number_util->Format(parsed_number, format, &formatted_number); | 38 phone_number_util->Format(parsed_number, format, &formatted_number); |
| 42 return formatted_number; | 39 return formatted_number; |
| 43 } | 40 } |
| 44 | 41 |
| 42 // Formats the |phone_number| to the specified |format|. Use application locale | |
| 43 // to determine country code. Returns the original number if the operation is | |
| 44 // not possible. | |
| 45 std::string FormatPhoneNumber(const std::string& phone_number, | |
| 46 PhoneNumberUtil::PhoneNumberFormat format) { | |
| 47 return FormatPhoneNumberWithCountryCode( | |
| 48 phone_number, | |
| 49 autofill::AutofillCountry::CountryCodeForLocale( | |
| 50 g_browser_process->GetApplicationLocale()), | |
| 51 format); | |
| 52 } | |
| 53 | |
| 54 // Checks whether the given number |jphone_number| is valid by using | |
| 55 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. | |
| 56 bool IsValidNumberImpl(const std::string& phone_number, | |
| 57 const std::string& country_code) { | |
| 58 PhoneNumber parsed_number; | |
| 59 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | |
| 60 if (phone_number_util->Parse(phone_number, country_code, &parsed_number) != | |
| 61 PhoneNumberUtil::NO_PARSING_ERROR) { | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 return phone_number_util->IsValidNumber(parsed_number); | |
| 66 } | |
| 67 | |
| 45 } // namespace | 68 } // namespace |
| 46 | 69 |
| 47 // Formats the given number |jphone_number| to | 70 // Formats the given number |jphone_number| for a given country to |
| 48 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format | 71 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format |
| 49 // by using i18n::phonenumbers::PhoneNumberUtil::Format. | 72 // by using i18n::phonenumbers::PhoneNumberUtil::Format. |
| 73 ScopedJavaLocalRef<jstring> FormatForDisplayWithCountryCode( | |
| 74 JNIEnv* env, | |
| 75 const base::android::JavaParamRef<jclass>& jcaller, | |
| 76 const JavaParamRef<jstring>& jphone_number, | |
| 77 const JavaParamRef<jstring>& jcountry_code) { | |
| 78 return ConvertUTF8ToJavaString( | |
| 79 env, FormatPhoneNumberWithCountryCode( | |
| 80 ConvertJavaStringToUTF8(env, jphone_number), | |
| 81 ConvertJavaStringToUTF8(env, jcountry_code), | |
| 82 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL)); | |
| 83 } | |
| 84 | |
| 85 // Formats the given number |jphone_number| | |
|
gogerald1
2017/06/08 16:20:43
* to *
wuandy1
2017/06/08 20:34:58
Done.
| |
| 86 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format | |
| 87 // by using i18n::phonenumbers::PhoneNumberUtil::Format. It uses application | |
| 88 // locale's country code. | |
| 50 ScopedJavaLocalRef<jstring> FormatForDisplay( | 89 ScopedJavaLocalRef<jstring> FormatForDisplay( |
| 51 JNIEnv* env, | 90 JNIEnv* env, |
| 52 const base::android::JavaParamRef<jclass>& jcaller, | 91 const base::android::JavaParamRef<jclass>& jcaller, |
| 53 const JavaParamRef<jstring>& jphone_number) { | 92 const JavaParamRef<jstring>& jphone_number) { |
| 54 return ConvertUTF8ToJavaString( | 93 return ConvertUTF8ToJavaString( |
| 55 env, | 94 env, |
| 56 FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), | 95 FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), |
| 57 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL)); | 96 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL)); |
| 58 } | 97 } |
| 59 | 98 |
| 60 // Formats the given number |jphone_number| to | 99 // Formats the given number |jphone_number| to |
| 61 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164 format by using | 100 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164 format by using |
| 62 // i18n::phonenumbers::PhoneNumberUtil::Format , as defined in the Payment | 101 // i18n::phonenumbers::PhoneNumberUtil::Format , as defined in the Payment |
| 63 // Request spec | 102 // Request spec |
| 64 // (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm) | 103 // (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm) |
| 65 ScopedJavaLocalRef<jstring> FormatForResponse( | 104 ScopedJavaLocalRef<jstring> FormatForResponse( |
| 66 JNIEnv* env, | 105 JNIEnv* env, |
| 67 const base::android::JavaParamRef<jclass>& jcaller, | 106 const base::android::JavaParamRef<jclass>& jcaller, |
| 68 const JavaParamRef<jstring>& jphone_number) { | 107 const JavaParamRef<jstring>& jphone_number) { |
| 69 return ConvertUTF8ToJavaString( | 108 return ConvertUTF8ToJavaString( |
| 70 env, FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), | 109 env, FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), |
| 71 PhoneNumberUtil::PhoneNumberFormat::E164)); | 110 PhoneNumberUtil::PhoneNumberFormat::E164)); |
| 72 } | 111 } |
| 73 | 112 |
| 74 // Checks whether the given number |jphone_number| is valid by using | 113 // Checks whether the given number |jphone_number| is valid by using |
| 75 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. | 114 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. It uses application's |
| 115 // locale's country code. | |
| 76 jboolean IsValidNumber(JNIEnv* env, | 116 jboolean IsValidNumber(JNIEnv* env, |
| 77 const base::android::JavaParamRef<jclass>& jcaller, | 117 const base::android::JavaParamRef<jclass>& jcaller, |
| 78 const JavaParamRef<jstring>& jphone_number) { | 118 const JavaParamRef<jstring>& jphone_number) { |
| 79 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | 119 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); |
| 80 const std::string default_region_code = | 120 const std::string default_region_code = |
| 81 autofill::AutofillCountry::CountryCodeForLocale( | 121 autofill::AutofillCountry::CountryCodeForLocale( |
| 82 g_browser_process->GetApplicationLocale()); | 122 g_browser_process->GetApplicationLocale()); |
| 83 | 123 |
| 84 PhoneNumber parsed_number; | 124 return IsValidNumberImpl(phone_number, default_region_code); |
| 85 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | 125 } |
| 86 if (phone_number_util->Parse(phone_number, default_region_code, | |
| 87 &parsed_number) != | |
| 88 PhoneNumberUtil::NO_PARSING_ERROR) { | |
| 89 return false; | |
| 90 } | |
| 91 | 126 |
| 92 return phone_number_util->IsValidNumber(parsed_number); | 127 // Checks whether the given number |jphone_number| is valid for a given country |
| 128 // by using i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. | |
| 129 jboolean IsValidNumberWithCountryCode( | |
| 130 JNIEnv* env, | |
| 131 const base::android::JavaParamRef<jclass>& jcaller, | |
| 132 const JavaParamRef<jstring>& jphone_number, | |
| 133 const JavaParamRef<jstring>& jcountry_code) { | |
| 134 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | |
| 135 const std::string country_code = ConvertJavaStringToUTF8(env, jcountry_code); | |
| 136 | |
| 137 return IsValidNumberImpl(phone_number, country_code); | |
| 93 } | 138 } |
| 94 | 139 |
| 95 } // namespace autofill | 140 } // namespace autofill |
| 96 | 141 |
| 97 // static | 142 // static |
| 98 bool RegisterPhoneNumberUtil(JNIEnv* env) { | 143 bool RegisterPhoneNumberUtil(JNIEnv* env) { |
| 99 return autofill::RegisterNativesImpl(env); | 144 return autofill::RegisterNativesImpl(env); |
| 100 } | 145 } |
| OLD | NEW |