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 std::vector
<base::string16
> address_lines
;
50 const base::string16 street_address
=
51 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, StreetAddress
);
52 base::SplitString(street_address
, base::char16('\n'), &address_lines
);
54 const base::string16 locality_name
=
55 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, Locality
);
56 const base::string16 dependent_locality_name
=
57 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, DependentLocality
);
58 const base::string16 administrative_area_name
=
59 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, AdministrativeArea
);
60 const base::string16 postal_code_number
=
61 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, PostalCode
);
62 const base::string16 sorting_code
=
63 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, SortingCode
);
64 const base::string16 phone_number
=
65 FETCH_JSTRING(UTF16
, env
, address
, ResultAddress
, PhoneNumber
);
66 const std::string country_name_code
=
67 FETCH_JSTRING(UTF8
, env
, address
, ResultAddress
, CountryCode
);
68 DCHECK(!country_name_code
.empty());
69 const std::string language_code
=
70 FETCH_JSTRING(UTF8
, env
, address
, ResultAddress
, LanguageCode
);
72 return scoped_ptr
<wallet::Address
>(new wallet::Address(
77 dependent_locality_name
,
78 administrative_area_name
,
86 scoped_ptr
<wallet::FullWallet
> ParseJavaWallet(JNIEnv
* env
, jobject wallet
) {
87 const ScopedJavaLocalRef
<jobject
> billing_address(
88 FETCH_JFIELD(env
, wallet
, ResultWallet
, BillingAddress
));
89 const ScopedJavaLocalRef
<jobject
> shipping_address(
90 FETCH_JFIELD(env
, wallet
, ResultWallet
, ShippingAddress
));
91 const ScopedJavaLocalRef
<jobject
> card(
92 FETCH_JFIELD(env
, wallet
, ResultWallet
, Card
));
94 const int expiration_month
=
95 FETCH_JFIELD(env
, card
.obj(), ResultCard
, ExpirationMonth
);
96 const int expiration_year
=
97 FETCH_JFIELD(env
, card
.obj(), ResultCard
, ExpirationYear
);
98 const std::string pan
=
99 FETCH_JSTRING(UTF8
, env
, card
.obj(), ResultCard
, Pan
);
100 const std::string cvn
=
101 FETCH_JSTRING(UTF8
, env
, card
.obj(), ResultCard
, Cvn
);
103 return wallet::FullWallet::CreateFullWalletFromClearText(
108 ParseJavaWalletAddress(env
, billing_address
.obj()),
109 ParseJavaWalletAddress(env
, shipping_address
.obj()));
112 base::string16
ParseWalletEmail(JNIEnv
* env
, jobject wallet
) {
113 return FETCH_JSTRING(UTF16
, env
, wallet
, ResultWallet
, Email
);
116 std::string
ParseGoogleTransactionId(JNIEnv
* env
, jobject wallet
) {
117 return FETCH_JSTRING(UTF8
, env
, wallet
, ResultWallet
, GoogleTransactionId
);
126 scoped_ptr
<wallet::FullWallet
> AutofillDialogResult::ConvertFromJava(
127 JNIEnv
* env
, jobject wallet
) {
128 return ParseJavaWallet(env
, wallet
);
132 base::string16
AutofillDialogResult::GetWalletEmail(
133 JNIEnv
* env
, jobject wallet
) {
134 return ParseWalletEmail(env
, wallet
);
138 std::string
AutofillDialogResult::GetWalletGoogleTransactionId(
139 JNIEnv
* env
, jobject wallet
) {
140 return ParseGoogleTransactionId(env
, wallet
);
144 bool AutofillDialogResult::RegisterAutofillDialogResult(JNIEnv
* env
) {
145 return RegisterNativesImpl(env
);
148 } // namespace autofill