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 com.google.common.annotations.VisibleForTesting;
9 import org.chromium.base.CalledByNative;
10 import org.chromium.base.JNINamespace;
11 import org.chromium.ui.base.WindowAndroid;
14 * Java-side AutofillDialog and AutofillDialogFactory interfaces, and
15 * JNI glue for C++ AutofillDialogControllerAndroid.
17 @JNINamespace("autofill")
18 public class AutofillDialogControllerAndroid {
19 private static AutofillDialogFactory sDialogFactory;
21 private long mNativeDelegate; // could be 0 after onDestroy().
22 private AutofillDialog mDialog;
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.
28 public interface AutofillDialogDelegate {
30 * Cancels the requestAutocomplete.
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
44 AutofillDialogResult.ResultWallet fullWallet,
45 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
46 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
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.
53 public interface AutofillDialog {
55 * Notifies the dialog that the C++ side is gone.
56 * The dialog needs to clear its reference to the no longer valid AutofillDialogDelegate.
62 * An interface to the factory that creates Autofill dialogs.
64 public interface AutofillDialogFactory {
67 * Reasonable attempts should be made to respect "initial choices",
68 * Initial choices don't have to be self-consistent or valid.
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
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);
101 * Sets the factory to be used.
102 * @param factory An instance of the AutofillDialogFactory that will handle requests.
104 public static void setDialogFactory(AutofillDialogFactory factory) {
105 sDialogFactory = factory;
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;
123 if (sDialogFactory == null) {
124 nativeDialogCancel(mNativeDelegate);
128 AutofillDialogDelegate delegate = new AutofillDialogDelegate() {
130 public void dialogCancel() {
131 nativeDialogCancel(mNativeDelegate);
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);
146 mDialog = sDialogFactory.createDialog(
149 requestFullBillingAddress, requestShippingAddress,
152 initialChoiceIsAutofill, initialWalletAccountName,
153 initialBillingGuid, initialShippingGuid, initialCardGuid,
157 if (mDialog == null) {
158 nativeDialogCancel(mNativeDelegate);
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,
180 initialChoiceIsAutofill, initialWalletAccountName,
181 initialBillingGuid, initialShippingGuid,
182 initialCreditCardGuid,
189 private static boolean isDialogAllowed(boolean isInvokedFromTheSameOrigin) {
190 // TODO(aruslan): cross-origin invocations should be allowed with a
192 return isInvokedFromTheSameOrigin;
196 private void onDestroy() {
197 if (mNativeDelegate == 0) return;
199 if (mDialog != null) mDialog.onDestroy();
205 // Calls from Java to C++ AutofillDialogControllerAndroid:
207 private native void nativeDialogCancel(long nativeAutofillDialogControllerAndroid);
208 private native void nativeDialogContinue(long nativeAutofillDialogControllerAndroid,
210 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
211 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);