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

Side by Side Diff: chrome/browser/autofill/android/phone_number_util_android.cc

Issue 2743763003: [Payments] Format and validate phone number by using libphonenumber (Closed)
Patch Set: formats Created 3 years, 9 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 unified diff | Download patch
« no previous file with comments | « chrome/browser/autofill/android/phone_number_util_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 namespace autofill {
15
16 namespace {
17 using ::base::android::ConvertJavaStringToUTF8;
18 using ::base::android::ConvertUTF8ToJavaString;
19 using ::base::android::JavaParamRef;
20 using ::base::android::ScopedJavaLocalRef;
21 using ::i18n::phonenumbers::PhoneNumber;
22 using ::i18n::phonenumbers::PhoneNumberUtil;
23 } // namespace
24
25 // Formats the given number |jphone_number| to
26 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format
27 // by using i18n::phonenumbers::PhoneNumberUtil::Format.
28 ScopedJavaLocalRef<jstring> 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 =
34 autofill::AutofillCountry::CountryCodeForLocale(
35 g_browser_process->GetApplicationLocale());
36
37 PhoneNumber parsed_number;
38 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
39 if (phone_number_util->Parse(phone_number, default_region_code,
40 &parsed_number) !=
41 PhoneNumberUtil::NO_PARSING_ERROR) {
42 return ConvertUTF8ToJavaString(env, phone_number);
43 }
44
45 std::string formatted_number;
46 phone_number_util->Format(parsed_number,
47 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL,
48 &formatted_number);
49
50 return ConvertUTF8ToJavaString(env, formatted_number);
51 }
52
53 // Checks whether the given number |jphone_number| is valid by using
54 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
55 jboolean IsValidNumber(JNIEnv* env,
56 const base::android::JavaParamRef<jclass>& jcaller,
57 const JavaParamRef<jstring>& jphone_number) {
58 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number);
59 const std::string default_region_code =
60 autofill::AutofillCountry::CountryCodeForLocale(
61 g_browser_process->GetApplicationLocale());
62
63 PhoneNumber parsed_number;
64 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
65 if (phone_number_util->Parse(phone_number, default_region_code,
66 &parsed_number) !=
67 PhoneNumberUtil::NO_PARSING_ERROR) {
68 return false;
69 }
70
71 return phone_number_util->IsValidNumber(parsed_number);
72 }
73
74 } // namespace autofill
75
76 // static
77 bool RegisterPhoneNumberUtil(JNIEnv* env) {
78 return autofill::RegisterNativesImpl(env);
79 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/android/phone_number_util_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698