Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/autofill/android/phone_number_util_android.h" | |
| 6 | |
| 7 #include "base/android/jni_string.h" | |
| 8 #include "base/android/scoped_java_ref.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "components/autofill/core/browser/autofill_country.h" | |
| 11 #include "jni/PhoneNumberUtil_jni.h" | |
| 12 #include "third_party/libphonenumber/phonenumber_api.h" | |
| 13 | |
| 14 using base::android::ConvertJavaStringToUTF8; | |
| 15 using base::android::ConvertUTF8ToJavaString; | |
| 16 using base::android::JavaParamRef; | |
| 17 using base::android::ScopedJavaLocalRef; | |
| 18 using ::i18n::phonenumbers::PhoneNumber; | |
| 19 using ::i18n::phonenumbers::PhoneNumberUtil; | |
| 20 | |
| 21 namespace autofill { | |
| 22 | |
| 23 // Formats the given number |jphone_number| by using | |
| 24 // i18n::phonenumbers::PhoneNumberUtil::Format. | |
|
Mathieu
2017/03/14 00:36:23
Mention that we are choosing INTERNATIONAL and pro
gogerald1
2017/03/14 15:32:59
Done.
| |
| 25 ScopedJavaLocalRef<jstring> Format( | |
| 26 JNIEnv* env, | |
| 27 const base::android::JavaParamRef<jclass>& jcaller, | |
| 28 const JavaParamRef<jstring>& jphone_number) { | |
| 29 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | |
| 30 const std::string default_region_code = | |
| 31 autofill::AutofillCountry::CountryCodeForLocale( | |
| 32 g_browser_process->GetApplicationLocale()); | |
| 33 | |
| 34 PhoneNumber parsed_number; | |
| 35 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | |
| 36 if (phone_number_util->Parse(phone_number, default_region_code, | |
| 37 &parsed_number) != | |
| 38 PhoneNumberUtil::NO_PARSING_ERROR) { | |
| 39 return ConvertUTF8ToJavaString(env, phone_number); | |
| 40 } | |
| 41 | |
| 42 std::string formtted_number; | |
|
Mathieu
2017/03/14 00:36:23
*formatted
gogerald1
2017/03/14 15:32:59
Done.
| |
| 43 phone_number_util->Format(parsed_number, | |
| 44 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL, | |
| 45 &formtted_number); | |
| 46 | |
| 47 return ConvertUTF8ToJavaString(env, formtted_number); | |
| 48 } | |
| 49 | |
| 50 // Checks whether the given number |jphone_number| is valid by using | |
| 51 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. | |
| 52 jboolean IsValidNumber(JNIEnv* env, | |
| 53 const base::android::JavaParamRef<jclass>& jcaller, | |
| 54 const JavaParamRef<jstring>& jphone_number) { | |
| 55 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | |
| 56 const std::string default_region_code = | |
| 57 autofill::AutofillCountry::CountryCodeForLocale( | |
| 58 g_browser_process->GetApplicationLocale()); | |
| 59 | |
| 60 PhoneNumber parsed_number; | |
| 61 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | |
| 62 if (phone_number_util->Parse(phone_number, default_region_code, | |
| 63 &parsed_number) != | |
| 64 PhoneNumberUtil::NO_PARSING_ERROR) { | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 return phone_number_util->IsValidNumber(parsed_number); | |
| 69 } | |
| 70 | |
| 71 } // namespace autofill | |
| 72 | |
| 73 // static | |
| 74 bool RegisterPhoneNumberUtil(JNIEnv* env) { | |
| 75 return autofill::RegisterNativesImpl(env); | |
| 76 } | |
| OLD | NEW |