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