Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2416)

Unified Diff: chrome/browser/autofill/android/phone_number_util_android.cc

Issue 2952673002: [Payments] Format and validate phone number according to selected country in address editor (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/autofill/android/phone_number_util_android.cc
diff --git a/chrome/browser/autofill/android/phone_number_util_android.cc b/chrome/browser/autofill/android/phone_number_util_android.cc
index 6aff62a456b6b6eaf8f3d93cb57a335ebb696e08..52125a2ba2059fcd8b868276ee46e795074992f5 100644
--- a/chrome/browser/autofill/android/phone_number_util_android.cc
+++ b/chrome/browser/autofill/android/phone_number_util_android.cc
@@ -21,18 +21,15 @@ using ::base::android::ScopedJavaLocalRef;
using ::i18n::phonenumbers::PhoneNumber;
using ::i18n::phonenumbers::PhoneNumberUtil;
-// Formats the |phone_number| to the specified |format|. Returns the original
-// number if the operation is not possible.
-std::string FormatPhoneNumber(const std::string& phone_number,
- PhoneNumberUtil::PhoneNumberFormat format) {
- const std::string default_region_code =
- autofill::AutofillCountry::CountryCodeForLocale(
- g_browser_process->GetApplicationLocale());
-
+// Formats the |phone_number| to the specified |format| for the given country
+// |country_code|. Returns the original number if the operation is not possible.
+std::string FormatPhoneNumberWithCountryCode(
+ const std::string& phone_number,
+ const std::string& country_code,
+ PhoneNumberUtil::PhoneNumberFormat format) {
PhoneNumber parsed_number;
PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
- if (phone_number_util->Parse(phone_number, default_region_code,
- &parsed_number) !=
+ if (phone_number_util->Parse(phone_number, country_code, &parsed_number) !=
PhoneNumberUtil::NO_PARSING_ERROR) {
return phone_number;
}
@@ -42,19 +39,53 @@ std::string FormatPhoneNumber(const std::string& phone_number,
return formatted_number;
}
+// Formats the |phone_number| to the specified |format|. Use application locale
+// to determine country code. Returns the original number if the operation is
+// not possible.
+std::string FormatPhoneNumber(const std::string& phone_number,
+ PhoneNumberUtil::PhoneNumberFormat format) {
+ return FormatPhoneNumberWithCountryCode(
+ phone_number,
+ autofill::AutofillCountry::CountryCodeForLocale(
+ g_browser_process->GetApplicationLocale()),
+ format);
+}
+
+// Checks whether the given number |jphone_number| is valid by using
+// i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
+bool IsValidNumberImpl(const std::string& phone_number,
+ const std::string& country_code) {
+ PhoneNumber parsed_number;
+ PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
+ if (phone_number_util->Parse(phone_number, country_code, &parsed_number) !=
+ PhoneNumberUtil::NO_PARSING_ERROR) {
+ return false;
+ }
+
+ return phone_number_util->IsValidNumber(parsed_number);
+}
+
} // namespace
-// Formats the given number |jphone_number| to
+// Formats the given number |jphone_number| for the given country
+// |jcountry_code| to
// i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format
// by using i18n::phonenumbers::PhoneNumberUtil::Format.
ScopedJavaLocalRef<jstring> FormatForDisplay(
JNIEnv* env,
const base::android::JavaParamRef<jclass>& jcaller,
- const JavaParamRef<jstring>& jphone_number) {
- return ConvertUTF8ToJavaString(
- env,
- FormatPhoneNumber(ConvertJavaStringToUTF8(env, jphone_number),
- PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL));
+ const JavaParamRef<jstring>& jphone_number,
+ const JavaParamRef<jstring>& jcountry_code) {
+ return jcountry_code.is_null()
+ ? ConvertUTF8ToJavaString(
+ 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.
+ ConvertJavaStringToUTF8(env, jphone_number),
+ PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL))
+ : ConvertUTF8ToJavaString(
+ env, FormatPhoneNumberWithCountryCode(
+ ConvertJavaStringToUTF8(env, jphone_number),
+ ConvertJavaStringToUTF8(env, jcountry_code),
+ PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL));
}
// Formats the given number |jphone_number| to
@@ -71,25 +102,19 @@ ScopedJavaLocalRef<jstring> FormatForResponse(
PhoneNumberUtil::PhoneNumberFormat::E164));
}
-// Checks whether the given number |jphone_number| is valid by using
-// i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
+// Checks whether the given number |jphone_number| is valid for a given country
+// |jcountry_code| by using i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
jboolean IsValidNumber(JNIEnv* env,
const base::android::JavaParamRef<jclass>& jcaller,
- const JavaParamRef<jstring>& jphone_number) {
+ const JavaParamRef<jstring>& jphone_number,
+ const JavaParamRef<jstring>& jcountry_code) {
const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number);
- const std::string default_region_code =
- autofill::AutofillCountry::CountryCodeForLocale(
- g_browser_process->GetApplicationLocale());
-
- PhoneNumber parsed_number;
- PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
- if (phone_number_util->Parse(phone_number, default_region_code,
- &parsed_number) !=
- PhoneNumberUtil::NO_PARSING_ERROR) {
- return false;
- }
+ const std::string country_code =
+ jcountry_code.is_null() ? autofill::AutofillCountry::CountryCodeForLocale(
+ g_browser_process->GetApplicationLocale())
+ : ConvertJavaStringToUTF8(env, jcountry_code);
- return phone_number_util->IsValidNumber(parsed_number);
+ return IsValidNumberImpl(phone_number, country_code);
}
} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698