Commit 8fe2301a authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Commit Bot

[PRImpl] Move EnableUserInterfaceAfterPaymentRequestUpdateEvent...

Change:
* Move EnableUserInterfaceAfterPaymentRequestUpdateEvent,
shouldShowShippingSection into PaymentUIsManager.

Bug: 1102522

Change-Id: Ie56768af521987c0a181db550720296f02bfe583
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2317565
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarSahel Sharify <sahel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792460}
parent c997b5e8
......@@ -134,7 +134,7 @@ public class PaymentRequestImpl
PaymentApp.InstrumentDetailsCallback,
PaymentResponseHelper.PaymentResponseRequesterDelegate, FocusChangedObserver,
NormalizedAddressRequestDelegate, PaymentDetailsConverter.MethodChecker,
PaymentHandlerUiObserver {
PaymentHandlerUiObserver, PaymentUIsManager.Delegate {
/**
* A delegate to ask questions about the system, that allows tests to inject behaviour without
* having to modify the entire system. This partially mirrors a similar C++
......@@ -349,7 +349,6 @@ public class PaymentRequestImpl
private List<PaymentApp> mPendingApps = new ArrayList<>();
private final PaymentUIsManager mPaymentUIsManager;
private MinimalUICoordinator mMinimalUi;
private Callback<PaymentInformation> mPaymentInformationCallback;
private PaymentApp mInvokedPaymentApp;
private PaymentHandlerCoordinator mPaymentHandlerUi;
private boolean mHideServerAutofillCards;
......@@ -468,7 +467,7 @@ public class PaymentRequestImpl
mSkipUiForNonUrlPaymentMethodIdentifiers = mDelegate.skipUiForBasicCard();
if (sObserverForTest != null) sObserverForTest.onPaymentRequestCreated(this);
mPaymentUIsManager = new PaymentUIsManager(
mPaymentUIsManager = new PaymentUIsManager(/*delegate=*/this,
/*params=*/this, addressEditor, cardEditor);
mPaymentAppComparator = new PaymentAppComparator(/*params=*/this);
}
......@@ -1324,7 +1323,7 @@ public class PaymentRequestImpl
mPaymentUIsManager.getShippingAddressesSection().setErrorMessage(details.error);
}
enableUserInterfaceAfterPaymentRequestUpdateEvent();
mPaymentUIsManager.enableUserInterfaceAfterPaymentRequestUpdateEvent();
}
private void initializeWithUpdatedDetails(PaymentDetails details) {
......@@ -1368,7 +1367,7 @@ public class PaymentRequestImpl
triggerPaymentAppUiSkipIfApplicable(chromeActivity);
if (mIsFinishedQueryingPaymentApps && !mShouldSkipShowingPaymentRequestUi) {
enableUserInterfaceAfterPaymentRequestUpdateEvent();
mPaymentUIsManager.enableUserInterfaceAfterPaymentRequestUpdateEvent();
}
}
......@@ -1392,22 +1391,7 @@ public class PaymentRequestImpl
return;
}
enableUserInterfaceAfterPaymentRequestUpdateEvent();
}
private void enableUserInterfaceAfterPaymentRequestUpdateEvent() {
if (mPaymentInformationCallback != null
&& mPaymentUIsManager.getPaymentMethodsSection() != null) {
providePaymentInformation();
} else {
mPaymentUIsManager.getPaymentRequestUI().updateOrderSummarySection(
mPaymentUIsManager.getUiShoppingCart());
if (shouldShowShippingSection()) {
mPaymentUIsManager.getPaymentRequestUI().updateSection(
PaymentRequestUI.DataType.SHIPPING_OPTIONS,
mPaymentUIsManager.getUiShippingOptions());
}
}
mPaymentUIsManager.enableUserInterfaceAfterPaymentRequestUpdateEvent();
}
/**
......@@ -1487,7 +1471,7 @@ public class PaymentRequestImpl
*/
@Override
public void getDefaultPaymentInformation(Callback<PaymentInformation> callback) {
mPaymentInformationCallback = callback;
mPaymentUIsManager.setPaymentInformationCallback(callback);
// mPaymentUIsManager.getPaymentRequestUI().show() is called only after request.show() is
// called and all payment apps are ready.
......@@ -1501,19 +1485,21 @@ public class PaymentRequestImpl
});
}
private void providePaymentInformation() {
// Implement PaymentUIsManager.Delegate:
@Override
public void providePaymentInformation() {
// Do not display service worker payment apps summary in single line so as to display its
// origin completely.
mPaymentUIsManager.getPaymentMethodsSection()
.setDisplaySelectedItemSummaryInSingleLineInNormalMode(
mPaymentUIsManager.getSelectedPaymentAppType()
!= PaymentAppType.SERVICE_WORKER_APP);
mPaymentInformationCallback.onResult(new PaymentInformation(
mPaymentUIsManager.getPaymentInformationCallback().onResult(new PaymentInformation(
mPaymentUIsManager.getUiShoppingCart(),
mPaymentUIsManager.getShippingAddressesSection(),
mPaymentUIsManager.getUiShippingOptions(), mPaymentUIsManager.getContactSection(),
mPaymentUIsManager.getPaymentMethodsSection()));
mPaymentInformationCallback = null;
mPaymentUIsManager.setPaymentInformationCallback(null);
if (!mDidRecordShowEvent) {
mDidRecordShowEvent = true;
......@@ -1573,13 +1559,13 @@ public class PaymentRequestImpl
} else {
editAddress(address);
}
mPaymentInformationCallback = callback;
mPaymentUIsManager.setPaymentInformationCallback(callback);
return PaymentRequestUI.SelectionResult.ASYNCHRONOUS_VALIDATION;
} else if (optionType == PaymentRequestUI.DataType.SHIPPING_OPTIONS) {
// This may update the line items.
mPaymentUIsManager.getUiShippingOptions().setSelectedItem(option);
client.onShippingOptionChange(option.getIdentifier());
mPaymentInformationCallback = callback;
mPaymentUIsManager.setPaymentInformationCallback(callback);
return PaymentRequestUI.SelectionResult.ASYNCHRONOUS_VALIDATION;
} else if (optionType == PaymentRequestUI.DataType.CONTACT_DETAILS) {
// Log the change of contact info.
......@@ -1593,7 +1579,7 @@ public class PaymentRequestImpl
editContact(contact);
if (!mWasRetryCalled) return PaymentRequestUI.SelectionResult.EDITOR_LAUNCH;
}
mPaymentInformationCallback = callback;
mPaymentUIsManager.setPaymentInformationCallback(callback);
return PaymentRequestUI.SelectionResult.ASYNCHRONOUS_VALIDATION;
} else if (optionType == PaymentRequestUI.DataType.PAYMENT_METHODS) {
if (shouldShowShippingSection()
......@@ -1634,7 +1620,7 @@ public class PaymentRequestImpl
Callback<PaymentInformation> callback) {
if (optionType == PaymentRequestUI.DataType.SHIPPING_ADDRESSES) {
editAddress((AutofillAddress) option);
mPaymentInformationCallback = callback;
mPaymentUIsManager.setPaymentInformationCallback(callback);
return PaymentRequestUI.SelectionResult.ASYNCHRONOUS_VALIDATION;
}
......@@ -1659,7 +1645,7 @@ public class PaymentRequestImpl
@PaymentRequestUI.DataType int optionType, Callback<PaymentInformation> callback) {
if (optionType == PaymentRequestUI.DataType.SHIPPING_ADDRESSES) {
editAddress(null);
mPaymentInformationCallback = callback;
mPaymentUIsManager.setPaymentInformationCallback(callback);
// Log the add of shipping address.
mJourneyLogger.incrementSelectionAdds(Section.SHIPPING_ADDRESS);
return PaymentRequestUI.SelectionResult.ASYNCHRONOUS_VALIDATION;
......@@ -1680,13 +1666,7 @@ public class PaymentRequestImpl
@Override
public boolean shouldShowShippingSection() {
if (!mRequestShipping) return false;
if (mPaymentUIsManager.getPaymentMethodsSection() == null) return true;
PaymentApp selectedApp =
(PaymentApp) mPaymentUIsManager.getPaymentMethodsSection().getSelectedItem();
return selectedApp == null || !selectedApp.handlesShippingAddress();
return mPaymentUIsManager.shouldShowShippingSection();
}
@Override
......
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.payments.ui;
import androidx.annotation.Nullable;
import org.chromium.base.Callback;
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
import org.chromium.chrome.browser.payments.AddressEditor;
import org.chromium.chrome.browser.payments.AutofillAddress;
......@@ -19,6 +20,7 @@ import org.chromium.components.payments.CurrencyFormatter;
import org.chromium.components.payments.PaymentApp;
import org.chromium.components.payments.PaymentAppType;
import org.chromium.components.payments.PaymentFeatureList;
import org.chromium.components.payments.PaymentOptionsUtils;
import org.chromium.components.payments.PaymentRequestLifecycleObserver;
import org.chromium.components.payments.PaymentRequestParams;
import org.chromium.payments.mojom.PaymentCurrencyAmount;
......@@ -43,7 +45,9 @@ import java.util.Set;
*/
public class PaymentUIsManager
implements SettingsAutofillAndPaymentsObserver.Observer, PaymentRequestLifecycleObserver {
private Callback<PaymentInformation> mPaymentInformationCallback;
private SectionInformation mUiShippingOptions;
private final Delegate mDelegate;
private final Map<String, CurrencyFormatter> mCurrencyFormatterMap;
private final AddressEditor mAddressEditor;
private final CardEditor mCardEditor;
......@@ -61,6 +65,12 @@ public class PaymentUIsManager
private Boolean mCanUserAddCreditCard;
/** The delegate of this class. */
public interface Delegate {
/** Provide payment information to the Payment Request UI. */
void providePaymentInformation();
}
/**
* This class is to coordinate the show state of a bottom sheet UI (either expandable payment
* handler or minimal UI) and the Payment Request UI so that these visibility rules are
......@@ -117,6 +127,7 @@ public class PaymentUIsManager
/**
* Create PaymentUIsManager.
* @param delegate The delegate of this instance.
* @param params The parameters of the payment request specified by the merchant.
* @param addressEditor The AddressEditor of the PaymentRequest UI.
* @param cardEditor The CardEditor of the PaymentRequest UI.
......@@ -124,8 +135,9 @@ public class PaymentUIsManager
// TODO(crbug.com/1107102): AddressEditor and CardEditor should be initialized in this
// constructor instead of the caller of the constructor, once CardEditor's "ForTest" symbols
// have been removed from the production code.
public PaymentUIsManager(
PaymentRequestParams params, AddressEditor addressEditor, CardEditor cardEditor) {
public PaymentUIsManager(Delegate delegate, PaymentRequestParams params,
AddressEditor addressEditor, CardEditor cardEditor) {
mDelegate = delegate;
mParams = params;
mAddressEditor = addressEditor;
mCardEditor = cardEditor;
......@@ -244,6 +256,20 @@ public class PaymentUIsManager
mUiShippingOptions = uiShippingOptions;
}
/** Get the PaymentInformation callback. */
public Callback<PaymentInformation> getPaymentInformationCallback() {
return mPaymentInformationCallback;
}
/**
* Set the call back of PaymentInformation. This callback would be invoked when the payment
* information is retrieved.
*/
public void setPaymentInformationCallback(
Callback<PaymentInformation> paymentInformationCallback) {
mPaymentInformationCallback = paymentInformationCallback;
}
// Implement SettingsAutofillAndPaymentsObserver.Observer:
@Override
public void onAddressUpdated(AutofillAddress address) {
......@@ -515,4 +541,31 @@ public class PaymentUIsManager
new PaymentInformation(mUiShoppingCart, mShippingAddressesSection,
mUiShippingOptions, mContactSection, mPaymentMethodsSection));
}
/**
* Update Payment Request UI with the update event's information and enable the UI (The user
* interface is disabled with a "↻" spinner being displayed and the user is unable to interact
* with the user interface until this "enableUserInterface" method is called).
*/
public void enableUserInterfaceAfterPaymentRequestUpdateEvent() {
if (mPaymentInformationCallback != null && mPaymentMethodsSection != null) {
mDelegate.providePaymentInformation();
} else {
mPaymentRequestUI.updateOrderSummarySection(mUiShoppingCart);
if (shouldShowShippingSection()) {
mPaymentRequestUI.updateSection(
PaymentRequestUI.DataType.SHIPPING_OPTIONS, mUiShippingOptions);
}
}
}
/** @return Whether PaymentRequest UI should show the shipping section. */
public boolean shouldShowShippingSection() {
if (!PaymentOptionsUtils.requestShipping(mParams.getPaymentOptions())) return false;
if (mPaymentMethodsSection == null) return true;
PaymentApp selectedApp = (PaymentApp) mPaymentMethodsSection.getSelectedItem();
return selectedApp == null || !selectedApp.handlesShippingAddress();
}
}
......@@ -42,4 +42,13 @@ public class PaymentOptionsUtils {
return String.format("{payerEmail:%s,payerName:%s,payerPhone:%s,shipping:%s}",
requestPayerEmail, requestPayerName, requestPayerPhone, requestShipping);
}
/**
* @param paymentOptions The PaymentOptions of the payment request.
* @return Whether requestShipping is specified in the payment request.
*/
public static boolean requestShipping(
org.chromium.payments.mojom.PaymentOptions paymentOptions) {
return paymentOptions != null && paymentOptions.requestShipping;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment