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.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;
20 private static boolean sAllowInsecureDialogsForTesting = false;
22 private int mNativeDelegate; // could be 0 after onDestroy().
23 private AutofillDialog mDialog;
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.
29 public interface AutofillDialogDelegate {
31 * Cancels the requestAutocomplete.
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
45 AutofillDialogResult.ResultWallet fullWallet,
46 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
47 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);
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.
54 public interface AutofillDialog {
56 * Notifies the dialog that the C++ side is gone.
57 * The dialog needs to clear its reference to the no longer valid AutofillDialogDelegate.
63 * An interface to the factory that creates Autofill dialogs.
65 public interface AutofillDialogFactory {
68 * Reasonable attempts should be made to respect "initial choices",
69 * Initial choices don't have to be self-consistent or valid.
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
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);
98 * Sets the factory to be used.
99 * @param factory An instance of the AutofillDialogFactory that will handle requests.
101 public static void setDialogFactory(AutofillDialogFactory factory) {
102 sDialogFactory = factory;
106 public static void allowInsecureDialogsForTesting() {
107 sAllowInsecureDialogsForTesting = true;
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;
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,
154 if (mDialog == null) {
155 nativeDialogCancel(mNativeDelegate);
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,
175 initialChoiceIsAutofill, initialWalletAccountName,
176 initialBillingGuid, initialShippingGuid,
177 initialCreditCardGuid,
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;
190 private void onDestroy() {
191 if (mNativeDelegate == 0) return;
193 if (mDialog != null) mDialog.onDestroy();
199 // Calls from Java to C++ AutofillDialogControllerAndroid:
201 private native void nativeDialogCancel(int nativeAutofillDialogControllerAndroid);
202 private native void nativeDialogContinue(int nativeAutofillDialogControllerAndroid,
204 boolean lastUsedChoiceIsAutofill, String lastUsedAccountName,
205 String guidLastUsedBilling, String guidLastUsedShipping, String guidLastUsedCard);