NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / android / autofill / autofill_dialog_result.cc
blob818eed60032a2b3eaa9c9dedf5b0e137ce46da02
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 std::vector<base::string16> address_lines;
49 address_lines.push_back(
50 FETCH_JSTRING(UTF16, env, address, ResultAddress, Address1));
51 address_lines.push_back(
52 FETCH_JSTRING(UTF16, env, address, ResultAddress, Address2));
53 const base::string16 locality_name =
54 FETCH_JSTRING(UTF16, env, address, ResultAddress, City);
55 const base::string16 administrative_area_name =
56 FETCH_JSTRING(UTF16, env, address, ResultAddress, State);
57 const base::string16 postal_code_number =
58 FETCH_JSTRING(UTF16, env, address, ResultAddress, PostalCode);
59 const base::string16 phone_number =
60 FETCH_JSTRING(UTF16, env, address, ResultAddress, PhoneNumber);
61 const std::string country_name_code =
62 FETCH_JSTRING(UTF8, env, address, ResultAddress, CountryCode);
63 DCHECK(!country_name_code.empty());
65 // TODO(aruslan): get these from the JavaWalletAddress.
66 const base::string16 dependent_locality_name;
67 const base::string16 sorting_code;
69 return scoped_ptr<wallet::Address>(new wallet::Address(
70 country_name_code,
71 recipient_name,
72 address_lines,
73 locality_name,
74 dependent_locality_name,
75 administrative_area_name,
76 postal_code_number,
77 sorting_code,
78 phone_number,
79 std::string()));
82 scoped_ptr<wallet::FullWallet> ParseJavaWallet(JNIEnv* env, jobject wallet) {
83 const ScopedJavaLocalRef<jobject> billing_address(
84 FETCH_JFIELD(env, wallet, ResultWallet, BillingAddress));
85 const ScopedJavaLocalRef<jobject> shipping_address(
86 FETCH_JFIELD(env, wallet, ResultWallet, ShippingAddress));
87 const ScopedJavaLocalRef<jobject> card(
88 FETCH_JFIELD(env, wallet, ResultWallet, Card));
90 const int expiration_month =
91 FETCH_JFIELD(env, card.obj(), ResultCard, ExpirationMonth);
92 const int expiration_year =
93 FETCH_JFIELD(env, card.obj(), ResultCard, ExpirationYear);
94 const std::string pan =
95 FETCH_JSTRING(UTF8, env, card.obj(), ResultCard, Pan);
96 const std::string cvn =
97 FETCH_JSTRING(UTF8, env, card.obj(), ResultCard, Cvn);
99 return wallet::FullWallet::CreateFullWalletFromClearText(
100 expiration_month,
101 expiration_year,
102 pan,
103 cvn,
104 ParseJavaWalletAddress(env, billing_address.obj()),
105 ParseJavaWalletAddress(env, shipping_address.obj()));
108 base::string16 ParseWalletEmail(JNIEnv* env, jobject wallet) {
109 return FETCH_JSTRING(UTF16, env, wallet, ResultWallet, Email);
112 std::string ParseGoogleTransactionId(JNIEnv* env, jobject wallet) {
113 return FETCH_JSTRING(UTF8, env, wallet, ResultWallet, GoogleTransactionId);
116 #undef FETCH_JSTRING
117 #undef FETCH_FIELD
119 } // namespace
121 // static
122 scoped_ptr<wallet::FullWallet> AutofillDialogResult::ConvertFromJava(
123 JNIEnv* env, jobject wallet) {
124 return ParseJavaWallet(env, wallet);
127 // static
128 base::string16 AutofillDialogResult::GetWalletEmail(
129 JNIEnv* env, jobject wallet) {
130 return ParseWalletEmail(env, wallet);
133 // static
134 std::string AutofillDialogResult::GetWalletGoogleTransactionId(
135 JNIEnv* env, jobject wallet) {
136 return ParseGoogleTransactionId(env, wallet);
139 // static
140 bool AutofillDialogResult::RegisterAutofillDialogResult(JNIEnv* env) {
141 return RegisterNativesImpl(env);
144 } // namespace autofill