Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / android / autofill / autofill_dialog_result.cc
blob7c223a1df940ed175f1152c18d5dc96017cc15a2
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/utf_string_conversions.h"
14 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
15 #include "components/autofill/content/browser/wallet/full_wallet.h"
16 #include "components/autofill/core/browser/credit_card.h"
17 #include "components/autofill/core/common/form_data.h"
18 #include "jni/AutofillDialogResult_jni.h"
19 #include "ui/base/l10n/l10n_util.h"
21 namespace autofill {
23 namespace {
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)
31 : base::string16();
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) {
43 if (!address)
44 return scoped_ptr<wallet::Address>();
46 const base::string16 recipient_name =
47 FETCH_JSTRING(UTF16, env, address, ResultAddress, Name);
48 const base::string16 address_line_1 =
49 FETCH_JSTRING(UTF16, env, address, ResultAddress, Address1);
50 const base::string16 address_line_2 =
51 FETCH_JSTRING(UTF16, env, address, ResultAddress, Address2);
52 const base::string16 locality_name =
53 FETCH_JSTRING(UTF16, env, address, ResultAddress, City);
54 const base::string16 administrative_area_name =
55 FETCH_JSTRING(UTF16, env, address, ResultAddress, State);
56 const base::string16 postal_code_number =
57 FETCH_JSTRING(UTF16, env, address, ResultAddress, PostalCode);
58 const base::string16 phone_number =
59 FETCH_JSTRING(UTF16, env, address, ResultAddress, PhoneNumber);
60 const std::string country_name_code =
61 FETCH_JSTRING(UTF8, env, address, ResultAddress, CountryCode);
62 DCHECK(!country_name_code.empty());
64 return scoped_ptr<wallet::Address>(new wallet::Address(
65 country_name_code,
66 recipient_name,
67 address_line_1,
68 address_line_2,
69 locality_name,
70 administrative_area_name,
71 postal_code_number,
72 phone_number,
73 std::string()));
76 scoped_ptr<wallet::FullWallet> ParseJavaWallet(JNIEnv* env, jobject wallet) {
77 const ScopedJavaLocalRef<jobject> billing_address(
78 FETCH_JFIELD(env, wallet, ResultWallet, BillingAddress));
79 const ScopedJavaLocalRef<jobject> shipping_address(
80 FETCH_JFIELD(env, wallet, ResultWallet, ShippingAddress));
81 const ScopedJavaLocalRef<jobject> card(
82 FETCH_JFIELD(env, wallet, ResultWallet, Card));
84 const int expiration_month =
85 FETCH_JFIELD(env, card.obj(), ResultCard, ExpirationMonth);
86 const int expiration_year =
87 FETCH_JFIELD(env, card.obj(), ResultCard, ExpirationYear);
88 const std::string pan =
89 FETCH_JSTRING(UTF8, env, card.obj(), ResultCard, Pan);
90 const std::string cvn =
91 FETCH_JSTRING(UTF8, env, card.obj(), ResultCard, Cvn);
93 return wallet::FullWallet::CreateFullWalletFromClearText(
94 expiration_month,
95 expiration_year,
96 pan,
97 cvn,
98 ParseJavaWalletAddress(env, billing_address.obj()),
99 ParseJavaWalletAddress(env, shipping_address.obj()));
102 base::string16 ParseWalletEmail(JNIEnv* env, jobject wallet) {
103 return FETCH_JSTRING(UTF16, env, wallet, ResultWallet, Email);
106 std::string ParseGoogleTransactionId(JNIEnv* env, jobject wallet) {
107 return FETCH_JSTRING(UTF8, env, wallet, ResultWallet, GoogleTransactionId);
110 #undef FETCH_JSTRING
111 #undef FETCH_FIELD
113 } // namespace
115 // static
116 scoped_ptr<wallet::FullWallet> AutofillDialogResult::ConvertFromJava(
117 JNIEnv* env, jobject wallet) {
118 return ParseJavaWallet(env, wallet);
121 // static
122 base::string16 AutofillDialogResult::GetWalletEmail(
123 JNIEnv* env, jobject wallet) {
124 return ParseWalletEmail(env, wallet);
127 // static
128 std::string AutofillDialogResult::GetWalletGoogleTransactionId(
129 JNIEnv* env, jobject wallet) {
130 return ParseGoogleTransactionId(env, wallet);
133 // static
134 bool AutofillDialogResult::RegisterAutofillDialogResult(JNIEnv* env) {
135 return RegisterNativesImpl(env);
138 } // namespace autofill