1 // Copyright 2013 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.
5 #include "chrome/browser/ui/android/autofill/autofill_dialog_result.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
16 #include "components/autofill/content/browser/wallet/full_wallet.h"
17 #include "components/autofill/core/browser/credit_card.h"
18 #include "components/autofill/core/common/form_data.h"
19 #include "jni/AutofillDialogResult_jni.h"
25 std::string
ConvertNullOrJavaStringToUTF8(JNIEnv
* env
, jstring str
) {
26 return str
? base::android::ConvertJavaStringToUTF8(env
, str
) : std::string();
29 base::string16
ConvertNullOrJavaStringToUTF16(JNIEnv
* env
, jstring str
) {
30 return str
? base::android::ConvertJavaStringToUTF16(env
, str
)
34 #define FETCH_JFIELD(env, jobj, cls, getter) \
35 (Java_##cls##_get##getter((env), (jobj)))
37 #define FETCH_JSTRING(utf, env, jobj, cls, getter) \
38 (ConvertNullOrJavaStringTo##utf( \
39 (env), FETCH_JFIELD((env), (jobj), cls, getter).obj()))
41 scoped_ptr
<wallet::Address
> ParseJavaWalletAddress(
42 JNIEnv
* env
, jobject address
) {
44 return scoped_ptr
<wallet::Address
>();
46 const base::string16 recipient_name
=
47 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, Name
);
49 const base::string16 street_address
=
50 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, StreetAddress
);
51 std::vector
<base::string16
> address_lines
= base::SplitString(
52 street_address
, base::string16(1, '\n'),
53 base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
55 const base::string16 locality_name
=
56 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, Locality
);
57 const base::string16 dependent_locality_name
=
58 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, DependentLocality
);
59 const base::string16 administrative_area_name
=
60 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, AdministrativeArea
);
61 const base::string16 postal_code_number
=
62 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, PostalCode
);
63 const base::string16 sorting_code
=
64 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, SortingCode
);
65 const base::string16 phone_number
=
66 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, PhoneNumber
);
67 const std::string country_name_code
=
68 FETCH_JSTRING(UTF8
, env
, address
, ResultAddress
, CountryCode
);
69 DCHECK(!country_name_code
.empty());
70 const std::string language_code
=
71 FETCH_JSTRING(UTF8
, env
, address
, ResultAddress
, LanguageCode
);
73 return scoped_ptr
<wallet::Address
>(new wallet::Address(
78 dependent_locality_name
,
79 administrative_area_name
,
87 scoped_ptr
<wallet::FullWallet
> ParseJavaWallet(JNIEnv
* env
, jobject wallet
) {
88 const ScopedJavaLocalRef
<jobject
> billing_address(
89 FETCH_JFIELD(env
, wallet
, ResultWallet
, BillingAddress
));
90 const ScopedJavaLocalRef
<jobject
> shipping_address(
91 FETCH_JFIELD(env
, wallet
, ResultWallet
, ShippingAddress
));
92 const ScopedJavaLocalRef
<jobject
> card(
93 FETCH_JFIELD(env
, wallet
, ResultWallet
, Card
));
95 const int expiration_month
=
96 FETCH_JFIELD(env
, card
.obj(), ResultCard
, ExpirationMonth
);
97 const int expiration_year
=
98 FETCH_JFIELD(env
, card
.obj(), ResultCard
, ExpirationYear
);
99 const std::string pan
=
100 FETCH_JSTRING(UTF8
, env
, card
.obj(), ResultCard
, Pan
);
101 const std::string cvn
=
102 FETCH_JSTRING(UTF8
, env
, card
.obj(), ResultCard
, Cvn
);
104 return wallet::FullWallet::CreateFullWalletFromClearText(
109 ParseJavaWalletAddress(env
, billing_address
.obj()),
110 ParseJavaWalletAddress(env
, shipping_address
.obj()));
113 base::string16
ParseWalletEmail(JNIEnv
* env
, jobject wallet
) {
114 return FETCH_JSTRING(UTF16
, env
, wallet
, ResultWallet
, Email
);
117 std::string
ParseGoogleTransactionId(JNIEnv
* env
, jobject wallet
) {
118 return FETCH_JSTRING(UTF8
, env
, wallet
, ResultWallet
, GoogleTransactionId
);
127 scoped_ptr
<wallet::FullWallet
> AutofillDialogResult::ConvertFromJava(
128 JNIEnv
* env
, jobject wallet
) {
129 return ParseJavaWallet(env
, wallet
);
133 base::string16
AutofillDialogResult::GetWalletEmail(
134 JNIEnv
* env
, jobject wallet
) {
135 return ParseWalletEmail(env
, wallet
);
139 std::string
AutofillDialogResult::GetWalletGoogleTransactionId(
140 JNIEnv
* env
, jobject wallet
) {
141 return ParseGoogleTransactionId(env
, wallet
);
145 bool AutofillDialogResult::RegisterAutofillDialogResult(JNIEnv
* env
) {
146 return RegisterNativesImpl(env
);
149 } // namespace autofill