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