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

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: Fix typo and comments 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
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 using base::android::ConvertJavaStringToUTF8;
please use gerrit instead 2017/03/14 18:17:15 Prepend "base" with "::" and put these "using" sta
gogerald1 2017/03/14 19:32:33 Done.
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| to
24 // i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL format
25 // by using i18n::phonenumbers::PhoneNumberUtil::Format.
26 ScopedJavaLocalRef<jstring> Format(
27 JNIEnv* env,
28 const base::android::JavaParamRef<jclass>& jcaller,
29 const JavaParamRef<jstring>& jphone_number) {
30 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number);
31 const std::string default_region_code =
32 autofill::AutofillCountry::CountryCodeForLocale(
33 g_browser_process->GetApplicationLocale());
34
35 PhoneNumber parsed_number;
36 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
37 if (phone_number_util->Parse(phone_number, default_region_code,
38 &parsed_number) !=
39 PhoneNumberUtil::NO_PARSING_ERROR) {
40 return ConvertUTF8ToJavaString(env, phone_number);
41 }
42
43 std::string formatted_number;
44 phone_number_util->Format(parsed_number,
45 PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL,
46 &formatted_number);
47
48 return ConvertUTF8ToJavaString(env, formatted_number);
49 }
50
51 // Checks whether the given number |jphone_number| is valid by using
52 // i18n::phonenumbers::PhoneNumberUtil::IsValidNumber.
53 jboolean IsValidNumber(JNIEnv* env,
54 const base::android::JavaParamRef<jclass>& jcaller,
55 const JavaParamRef<jstring>& jphone_number) {
56 const std::string phone_number = ConvertJavaStringToUTF8(env, jphone_number);
57 const std::string default_region_code =
58 autofill::AutofillCountry::CountryCodeForLocale(
59 g_browser_process->GetApplicationLocale());
60
61 PhoneNumber parsed_number;
62 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance();
63 if (phone_number_util->Parse(phone_number, default_region_code,
64 &parsed_number) !=
65 PhoneNumberUtil::NO_PARSING_ERROR) {
66 return false;
67 }
68
69 return phone_number_util->IsValidNumber(parsed_number);
70 }
71
72 } // namespace autofill
73
74 // static
75 bool RegisterPhoneNumberUtil(JNIEnv* env) {
76 return autofill::RegisterNativesImpl(env);
77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698