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 package org.chromium.chrome.browser.autofill;
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;
13 * Java-side AutofillDialog and AutofillDialogFactory interfaces, and
14 * JNI glue for C++ AutofillDialogControllerAndroid.
16 @JNINamespace("autofill")
17 public class AutofillDialogControllerAndroid {
18 private static AutofillDialogFactory sDialogFactory;
20 private long mNativeDelegate; // could be 0 after onDestroy().
21 private AutofillDialog mDialog;
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.
27 public interface AutofillDialogDelegate {
29 * Cancels the requestAutocomplete.
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
43 AutofillDialogResult.ResultWallet fullWallet,
44 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
45 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
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.
52 public interface AutofillDialog {
54 * Notifies the dialog that the C++ side is gone.
55 * The dialog needs to clear its reference to the no longer valid AutofillDialogDelegate.
61 * An interface to the factory that creates Autofill dialogs.
63 public interface AutofillDialogFactory {
66 * Reasonable attempts should be made to respect "initial choices",
67 * Initial choices don't have to be self-consistent or valid.
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
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);
100 * Sets the factory to be used.
101 * @param factory An instance of the AutofillDialogFactory that will handle requests.
103 public static void setDialogFactory(AutofillDialogFactory factory) {
104 sDialogFactory = factory;
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;
122 if (sDialogFactory == null) {
123 nativeDialogCancel(mNativeDelegate);
127 AutofillDialogDelegate delegate = new AutofillDialogDelegate() {
129 public void dialogCancel() {
130 nativeDialogCancel(mNativeDelegate);
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);
145 mDialog = sDialogFactory.createDialog(
148 requestFullBillingAddress, requestShippingAddress,
151 initialChoiceIsAutofill, initialWalletAccountName,
152 initialBillingGuid, initialShippingGuid, initialCardGuid,
156 if (mDialog == null) {
157 nativeDialogCancel(mNativeDelegate);
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,
179 initialChoiceIsAutofill, initialWalletAccountName,
180 initialBillingGuid, initialShippingGuid,
181 initialCreditCardGuid,
188 private static boolean isDialogAllowed(boolean isInvokedFromTheSameOrigin) {
189 // TODO(aruslan): cross-origin invocations should be allowed with a
191 return isInvokedFromTheSameOrigin;
195 private void onDestroy() {
196 if (mNativeDelegate == 0) return;
198 if (mDialog != null) mDialog.onDestroy();
204 // Calls from Java to C++ AutofillDialogControllerAndroid:
206 private native void nativeDialogCancel(long nativeAutofillDialogControllerAndroid);
207 private native void nativeDialogContinue(long nativeAutofillDialogControllerAndroid,
209 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
210 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);