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 } // 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(std::string phone_number, |
|
please use gerrit instead
2017/04/04 18:28:22
const-ref the input phone_number.
sebsg
2017/04/04 19:55:57
Done.
| |
| 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. | |
| 63 ScopedJavaLocalRef<jstring> FormatForResponse( | |
| 64 JNIEnv* env, | |
| 65 const base::android::JavaParamRef<jclass>& jcaller, | |
| 66 const JavaParamRef<jstring>& jphone_number) { | |
| 67 return ConvertUTF8ToJavaString( | |
| 68 env, FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), | |
| 69 PhoneNumberUtil::PhoneNumberFormat::E164)); | |
|
please use gerrit instead
2017/04/04 18:28:22
Link to spec text that mentions E164.
sebsg
2017/04/04 19:55:57
Done.
| |
| 51 } | 70 } |
| 52 | 71 |
| 53 // Checks whether the given number |jphone_number| is valid by using | 72 // Checks whether the given number |jphone_number| is valid by using |
| 54 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. | 73 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. |
| 55 jboolean IsValidNumber(JNIEnv* env, | 74 jboolean IsValidNumber(JNIEnv* env, |
| 56 const base::android::JavaParamRef<jclass>& jcaller, | 75 const base::android::JavaParamRef<jclass>& jcaller, |
| 57 const JavaParamRef<jstring>& jphone_number) { | 76 const JavaParamRef<jstring>& jphone_number) { |
| 58 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | 77 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); |
| 59 const std::string default_region_code = | 78 const std::string default_region_code = |
| 60 autofill::AutofillCountry::CountryCodeForLocale( | 79 autofill::AutofillCountry::CountryCodeForLocale( |
| 61 g_browser_process->GetApplicationLocale()); | 80 g_browser_process->GetApplicationLocale()); |
| 62 | 81 |
| 63 PhoneNumber parsed_number; | 82 PhoneNumber parsed_number; |
| 64 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | 83 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); |
| 65 if (phone_number_util->Parse(phone_number, default_region_code, | 84 if (phone_number_util->Parse(phone_number, default_region_code, |
| 66 &parsed_number) != | 85 &parsed_number) != |
| 67 PhoneNumberUtil::NO_PARSING_ERROR) { | 86 PhoneNumberUtil::NO_PARSING_ERROR) { |
| 68 return false; | 87 return false; |
| 69 } | 88 } |
| 70 | 89 |
| 71 return phone_number_util->IsValidNumber(parsed_number); | 90 return phone_number_util->IsValidNumber(parsed_number); |
| 72 } | 91 } |
| 73 | 92 |
| 74 } // namespace autofill | 93 } // namespace autofill |
| 75 | 94 |
| 76 // static | 95 // static |
| 77 bool RegisterPhoneNumberUtil(JNIEnv* env) { | 96 bool RegisterPhoneNumberUtil(JNIEnv* env) { |
| 78 return autofill::RegisterNativesImpl(env); | 97 return autofill::RegisterNativesImpl(env); |
| 79 } | 98 } |
| OLD | NEW |