- add sources.
[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.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     private static boolean sAllowInsecureDialogsForTesting = false;
21
22     private int mNativeDelegate;  // could be 0 after onDestroy().
23     private AutofillDialog mDialog;
24
25     /**
26      * An interface to the two possible continuations for the dialog.
27      * The dialog is expected to be dismissed when either of the calls is made.
28      */
29     public interface AutofillDialogDelegate {
30         /**
31          * Cancels the requestAutocomplete.
32          */
33         void dialogCancel();
34
35         /**
36          * Submits the data to the web-page and persists the last account/card/address choices.
37          * @param fullWallet Resulting billing/shipping information obtained from the user
38          * @param lastUsedChoiceIsAutofill Whether the last selected data source is Autofill
39          * @param lastUsedAccountName The last selected account name, or null
40          * @param guidLastUsedBilling GUID of the last selected Autofill billing address, or null
41          * @param guidLastUsedShipping GUID of the last selected Autofill shipping address, or null
42          * @param guidLastUsedCard GUID of the last selected Autofill credit card, or null
43          */
44         void dialogContinue(
45                 AutofillDialogResult.ResultWallet fullWallet,
46                 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
47                 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
48     }
49
50     /**
51      * An interface that exposes the necessary functionality for an Autofill dialog.
52      * Note that all information necessary to construct the dialog is passed to the factory.
53      */
54     public interface AutofillDialog {
55         /**
56          * Notifies the dialog that the C++ side is gone.
57          * The dialog needs to clear its reference to the no longer valid AutofillDialogDelegate.
58          */
59         void onDestroy();
60     }
61
62     /**
63      * An interface to the factory that creates Autofill dialogs.
64      */
65     public interface AutofillDialogFactory {
66         /**
67          * Creates the dialog.
68          * Reasonable attempts should be made to respect "initial choices",
69          * Initial choices don't have to be self-consistent or valid.
70          *
71          * @param delegate Continuations for the dialog
72          * @param windowAndroid Context in which the dialog should be shown
73          * @param requestFullBillingAddress Whether the full billing address is required
74          * @param requestShippingAddress Whether the shipping address is required
75          * @param requestPhoneNumbers Whether the phone numbers are required in addresses
76          * @param incognitoMode True if the dialog started from an incognito tab
77          * @param initialChoiceIsAutofill Whether the selected data source should be Autofill
78          * @param initialAccountName Account to be selected, or null
79          * @param initialBillingGuid GUID of the initial billing address selection in Autofill
80          * @param initialShippingGuid GUID of the initial shipping address selection in Autofill
81          * @param initialCardGuid GUID of the initial credit card selection in Autofill
82          * @param merchantDomain Scheme+origin for the originating web page, 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     }
96
97     /**
98      * Sets the factory to be used.
99      * @param factory An instance of the AutofillDialogFactory that will handle requests.
100      */
101     public static void setDialogFactory(AutofillDialogFactory factory) {
102         sDialogFactory = factory;
103     }
104
105     @VisibleForTesting
106     public static void allowInsecureDialogsForTesting() {
107         sAllowInsecureDialogsForTesting = true;
108     }
109
110     private AutofillDialogControllerAndroid(
111             final int nativeAutofillDialogControllerAndroid,
112             final WindowAndroid windowAndroid,
113             final boolean requestFullBillingAddress, final boolean requestShippingAddress,
114             final boolean requestPhoneNumbers,
115             final boolean incognitoMode,
116             final boolean initialChoiceIsAutofill, final String initialWalletAccountName,
117             final String initialBillingGuid, final String initialShippingGuid,
118             final String initialCardGuid,
119             final String merchantDomain) {
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         if (mDialog == null) {
155             nativeDialogCancel(mNativeDelegate);
156             return;
157         }
158     }
159
160     @CalledByNative
161     private static AutofillDialogControllerAndroid create(
162             final int nativeAutofillDialogControllerAndroid,
163             final WindowAndroid windowAndroid,
164             final boolean requestFullBillingAddress, final boolean requestShippingAddress,
165             final boolean requestPhoneNumbers,
166             final boolean incognitoMode,
167             final boolean initialChoiceIsAutofill, final String initialWalletAccountName,
168             final String initialBillingGuid, final String initialShippingGuid,
169             final String initialCreditCardGuid,
170             final String merchantDomain) {
171         return new AutofillDialogControllerAndroid(
172                 nativeAutofillDialogControllerAndroid, windowAndroid,
173                 requestFullBillingAddress, requestShippingAddress, requestPhoneNumbers,
174                 incognitoMode,
175                 initialChoiceIsAutofill, initialWalletAccountName,
176                 initialBillingGuid, initialShippingGuid,
177                 initialCreditCardGuid,
178                 merchantDomain);
179     }
180
181     @CalledByNative
182     private static boolean isDialogAllowed(boolean requestsCreditCardInformation,
183             boolean isTransmissionSecure, boolean isInvokedFromTheSameOrigin) {
184         if (!requestsCreditCardInformation) return true;
185         if (isTransmissionSecure && isInvokedFromTheSameOrigin) return true;
186         return sAllowInsecureDialogsForTesting;
187     }
188
189     @CalledByNative
190     private void onDestroy() {
191         if (mNativeDelegate == 0) return;
192
193         if (mDialog != null) mDialog.onDestroy();
194
195         mDialog = null;
196         mNativeDelegate = 0;
197     }
198
199     // Calls from Java to C++ AutofillDialogControllerAndroid:
200
201     private native void nativeDialogCancel(int nativeAutofillDialogControllerAndroid);
202     private native void nativeDialogContinue(int nativeAutofillDialogControllerAndroid,
203             Object fullWallet,
204             boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
205             String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
206 }