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