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| for the given country |
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& country_code, |
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, country_code, &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 the given country |
| 71 // |jcountry_code| to |
48 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format | 72 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format |
49 // by using i18n::phonenumbers::PhoneNumberUtil::Format. | 73 // by using i18n::phonenumbers::PhoneNumberUtil::Format. |
50 ScopedJavaLocalRef<jstring> FormatForDisplay( | 74 ScopedJavaLocalRef<jstring> FormatForDisplay( |
51 JNIEnv* env, | 75 JNIEnv* env, |
52 const base::android::JavaParamRef<jclass>& jcaller, | 76 const base::android::JavaParamRef<jclass>& jcaller, |
53 const JavaParamRef<jstring>& jphone_number) { | 77 const JavaParamRef<jstring>& jphone_number, |
| 78 const JavaParamRef<jstring>& jcountry_code) { |
54 return ConvertUTF8ToJavaString( | 79 return ConvertUTF8ToJavaString( |
55 env, | 80 env, |
56 FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), | 81 jcountry_code.is_null() |
57 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL)); | 82 ? FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), |
| 83 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL) |
| 84 : FormatPhoneNumberWithCountryCode( |
| 85 ConvertJavaStringToUTF8(env, jphone_number), |
| 86 ConvertJavaStringToUTF8(env, jcountry_code), |
| 87 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL)); |
58 } | 88 } |
59 | 89 |
60 // Formats the given number |jphone_number| to | 90 // Formats the given number |jphone_number| to |
61 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164 format by using | 91 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::E164 format by using |
62 // i18n::phonenumbers::PhoneNumberUtil::Format , as defined in the Payment | 92 // i18n::phonenumbers::PhoneNumberUtil::Format , as defined in the Payment |
63 // Request spec | 93 // Request spec |
64 // (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm) | 94 // (https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorithm) |
65 ScopedJavaLocalRef<jstring> FormatForResponse( | 95 ScopedJavaLocalRef<jstring> FormatForResponse( |
66 JNIEnv* env, | 96 JNIEnv* env, |
67 const base::android::JavaParamRef<jclass>& jcaller, | 97 const base::android::JavaParamRef<jclass>& jcaller, |
68 const JavaParamRef<jstring>& jphone_number) { | 98 const JavaParamRef<jstring>& jphone_number) { |
69 return ConvertUTF8ToJavaString( | 99 return ConvertUTF8ToJavaString( |
70 env, FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), | 100 env, FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number), |
71 PhoneNumberUtil::PhoneNumberFormat::E164)); | 101 PhoneNumberUtil::PhoneNumberFormat::E164)); |
72 } | 102 } |
73 | 103 |
74 // Checks whether the given number |jphone_number| is valid by using | 104 // Checks whether the given number |jphone_number| is valid for a given country |
75 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. | 105 // |jcountry_code| by using i18n::phonenumbers::PhoneNumberUtil::IsValidNumber. |
76 jboolean IsValidNumber(JNIEnv* env, | 106 jboolean IsValidNumber(JNIEnv* env, |
77 const base::android::JavaParamRef<jclass>& jcaller, | 107 const base::android::JavaParamRef<jclass>& jcaller, |
78 const JavaParamRef<jstring>& jphone_number) { | 108 const JavaParamRef<jstring>& jphone_number, |
| 109 const JavaParamRef<jstring>& jcountry_code) { |
79 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); | 110 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number); |
80 const std::string default_region_code = | 111 const std::string country_code = |
81 autofill::AutofillCountry::CountryCodeForLocale( | 112 jcountry_code.is_null() ? autofill::AutofillCountry::CountryCodeForLocale( |
82 g_browser_process->GetApplicationLocale()); | 113 g_browser_process->GetApplicationLocale()) |
| 114 : ConvertJavaStringToUTF8(env, jcountry_code); |
83 | 115 |
84 PhoneNumber parsed_number; | 116 return IsValidNumberImpl(phone_number, country_code); |
85 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); | |
86 if (phone_number_util->Parse(phone_number, default_region_code, | |
87 &parsed_number) != | |
88 PhoneNumberUtil::NO_PARSING_ERROR) { | |
89 return false; | |
90 } | |
91 | |
92 return phone_number_util->IsValidNumber(parsed_number); | |
93 } | 117 } |
94 | 118 |
95 } // namespace autofill | 119 } // namespace autofill |
96 | 120 |
97 // static | 121 // static |
98 bool RegisterPhoneNumberUtil(JNIEnv* env) { | 122 bool RegisterPhoneNumberUtil(JNIEnv* env) { |
99 return autofill::RegisterNativesImpl(env); | 123 return autofill::RegisterNativesImpl(env); |
100 } | 124 } |
OLD | NEW |