Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / autofill / AutofillDialogControllerAndroid.java
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.
4
5 package org.chromium.chrome.browser.autofill;
6
7 import org.chromium.base.CalledByNative;
8 import org.chromium.base.JNINamespace;
9 import org.chromium.base.VisibleForTesting;
10 import org.chromium.ui.base.WindowAndroid;
11
12 /**
13  * Java-side AutofillDialog and AutofillDialogFactory interfaces, and
14  * JNI glue for C++ AutofillDialogControllerAndroid.
15  */
16 @JNINamespace("autofill")
17 public class AutofillDialogControllerAndroid {
18     private static AutofillDialogFactory sDialogFactory;
19
20     private long mNativeDelegate;  // could be 0 after onDestroy().
21     private AutofillDialog mDialog;
22
23     /**
24      * An interface to the two possible continuations for the dialog.
25      * The dialog is expected to be dismissed when either of the calls is made.
26      */
27     public interface AutofillDialogDelegate {
28         /**
29          * Cancels the requestAutocomplete.
30          */
31         void dialogCancel();
32
33         /**
34          * Submits the data to the web-page and persists the last account/card/address choices.
35          * @param fullWallet Resulting billing/shipping information obtained from the user
36          * @param lastUsedChoiceIsAutofill Whether the last selected data source is Autofill
37          * @param lastUsedAccountName The last selected account name, or null
38          * @param guidLastUsedBilling GUID of the last selected Autofill billing address, or null
39          * @param guidLastUsedShipping GUID of the last selected Autofill shipping address, or null
40          * @param guidLastUsedCard GUID of the last selected Autofill credit card, or null
41          */
42         void dialogContinue(
43                 AutofillDialogResult.ResultWallet fullWallet,
44                 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
45                 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
46     }
47
48     /**
49      * An interface that exposes the necessary functionality for an Autofill dialog.
50      * Note that all information necessary to construct the dialog is passed to the factory.
51      */
52     public interface AutofillDialog {
53         /**
54          * Notifies the dialog that the C++ side is gone.
55          * The dialog needs to clear its reference to the no longer valid AutofillDialogDelegate.
56          */
57         void onDestroy();
58     }
59
60     /**
61      * An interface to the factory that creates Autofill dialogs.
62      */
63     public interface AutofillDialogFactory {
64         /**
65          * Creates the dialog.
66          * Reasonable attempts should be made to respect "initial choices",
67          * Initial choices don't have to be self-consistent or valid.
68          *
69          * @param delegate Continuations for the dialog
70          * @param windowAndroid Context in which the dialog should be shown
71          * @param requestFullBillingAddress Whether the full billing address is required
72          * @param requestShippingAddress Whether the shipping address is required
73          * @param requestPhoneNumbers Whether the phone numbers are required in addresses
74          * @param incognitoMode True if the dialog started from an incognito tab
75          * @param initialChoiceIsAutofill Whether the selected data source should be Autofill
76          * @param initialAccountName Account to be selected, or null
77          * @param initialBillingGuid GUID of the initial billing address selection in Autofill
78          * @param initialShippingGuid GUID of the initial shipping address selection in Autofill
79          * @param initialCardGuid GUID of the initial credit card selection in Autofill
80          * @param merchantDomain Scheme+origin for the originating web page, or null
81          * @param shippingCountries A list of allowed shipping countries, or null
82          * @param creditCardTypes A list of allowed credit card types (e.g. "VISA"), or null
83          * @return The Autofill dialog that would later call into the delegate, or null
84          */
85         AutofillDialog createDialog(
86                 final AutofillDialogDelegate delegate,
87                 final WindowAndroid windowAndroid,
88                 final boolean requestFullBillingAddress, final boolean requestShippingAddress,
89                 final boolean requestPhoneNumbers,
90                 final boolean incognitoMode,
91                 final boolean initialChoiceIsAutofill, final String initialAccountName,
92                 final String initialBillingGuid, final String initialShippingGuid,
93                 final String initialCardGuid,
94                 final String merchantDomain,
95                 final String[] shippingCountries,
96                 final String[] creditCardTypes);
97     }
98
99     /**
100      * Sets the factory to be used.
101      * @param factory An instance of the AutofillDialogFactory that will handle requests.
102      */
103     public static void setDialogFactory(AutofillDialogFactory factory) {
104         sDialogFactory = factory;
105     }
106
107     @VisibleForTesting
108     private AutofillDialogControllerAndroid(
109             final long nativeAutofillDialogControllerAndroid,
110             final WindowAndroid windowAndroid,
111             final boolean requestFullBillingAddress, final boolean requestShippingAddress,
112             final boolean requestPhoneNumbers,
113             final boolean incognitoMode,
114             final boolean initialChoiceIsAutofill, final String initialWalletAccountName,
115             final String initialBillingGuid, final String initialShippingGuid,
116             final String initialCardGuid,
117             final String merchantDomain,
118             final String[] shippingCountries,
119             final String[] creditCardTypes) {
120         mNativeDelegate = nativeAutofillDialogControllerAndroid;
121
122         if (sDialogFactory == null) {
123             nativeDialogCancel(mNativeDelegate);
124             return;
125         }
126
127         AutofillDialogDelegate delegate = new AutofillDialogDelegate() {
128             @Override
129             public void dialogCancel() {
130                 nativeDialogCancel(mNativeDelegate);
131             }
132
133             @Override
134             public void dialogContinue(
135                     AutofillDialogResult.ResultWallet fullWallet,
136                     boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
137                     String guidLastUsedBilling, String guidLastUsedShipping,
138                     String guidLastUsedCard) {
139                 nativeDialogContinue(mNativeDelegate, fullWallet,
140                         lastUsedChoiceIsAutofill, lastUsedAccountName,
141                         guidLastUsedBilling, guidLastUsedShipping, guidLastUsedCard);
142             }
143         };
144
145         mDialog = sDialogFactory.createDialog(
146                 delegate,
147                 windowAndroid,
148                 requestFullBillingAddress, requestShippingAddress,
149                 requestPhoneNumbers,
150                 incognitoMode,
151                 initialChoiceIsAutofill, initialWalletAccountName,
152                 initialBillingGuid, initialShippingGuid, initialCardGuid,
153                 merchantDomain,
154                 shippingCountries,
155                 creditCardTypes);
156         if (mDialog == null) {
157             nativeDialogCancel(mNativeDelegate);
158             return;
159         }
160     }
161
162     @CalledByNative
163     private static AutofillDialogControllerAndroid create(
164             final long nativeAutofillDialogControllerAndroid,
165             final WindowAndroid windowAndroid,
166             final boolean requestFullBillingAddress, final boolean requestShippingAddress,
167             final boolean requestPhoneNumbers,
168             final boolean incognitoMode,
169             final boolean initialChoiceIsAutofill, final String initialWalletAccountName,
170             final String initialBillingGuid, final String initialShippingGuid,
171             final String initialCreditCardGuid,
172             final String merchantDomain,
173             final String[] shippingCountries,
174             final String[] creditCardTypes) {
175         return new AutofillDialogControllerAndroid(
176                 nativeAutofillDialogControllerAndroid, windowAndroid,
177                 requestFullBillingAddress, requestShippingAddress, requestPhoneNumbers,
178                 incognitoMode,
179                 initialChoiceIsAutofill, initialWalletAccountName,
180                 initialBillingGuid, initialShippingGuid,
181                 initialCreditCardGuid,
182                 merchantDomain,
183                 shippingCountries,
184                 creditCardTypes);
185     }
186
187     @CalledByNative
188     private static boolean isDialogAllowed(boolean isInvokedFromTheSameOrigin) {
189         // TODO(aruslan): cross-origin invocations should be allowed with a
190         // warning messge.
191         return isInvokedFromTheSameOrigin;
192     }
193
194     @CalledByNative
195     private void onDestroy() {
196         if (mNativeDelegate == 0) return;
197
198         if (mDialog != null) mDialog.onDestroy();
199
200         mDialog = null;
201         mNativeDelegate = 0;
202     }
203
204     // Calls from Java to C++ AutofillDialogControllerAndroid:
205
206     private native void nativeDialogCancel(long nativeAutofillDialogControllerAndroid);
207     private native void nativeDialogContinue(long nativeAutofillDialogControllerAndroid,
208             Object fullWallet,
209             boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
210             String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
211 }