Commit e1304936 authored by Zarah Tesfai's avatar Zarah Tesfai Committed by Commit Bot

Move mMinimalUI to PaymentUIManager

Purpose: Move Minimal UI related logic to PaymentUIsManager

Changes:
* Added get/setMinimalUI() methods in PaymentUIsManager
* Moved testing related methods
* Added hideMinimalUI() method


Bug: 1108099
Change-Id: Ibb35f92f8f61c68056b37ee5f9019e7a7f05cb7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2381063
Commit-Queue: Zarah Tesfai <ztesfai@google.com>
Reviewed-by: default avatarLiquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805865}
parent edfd32d9
......@@ -24,7 +24,6 @@ import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.autofill.PersonalDataManager.NormalizedAddressRequestDelegate;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator.PaymentHandlerWebContentsObserver;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator;
import org.chromium.chrome.browser.payments.ui.ContactDetailsSection;
import org.chromium.chrome.browser.payments.ui.PaymentInformation;
import org.chromium.chrome.browser.payments.ui.PaymentRequestUI;
......@@ -35,7 +34,6 @@ import org.chromium.chrome.browser.payments.ui.ShoppingCart;
import org.chromium.chrome.browser.settings.SettingsLauncher;
import org.chromium.chrome.browser.settings.SettingsLauncherImpl;
import org.chromium.components.autofill.EditableOption;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetControllerProvider;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.payments.AbortReason;
import org.chromium.components.payments.BrowserPaymentRequest;
......@@ -187,7 +185,6 @@ public class PaymentRequestImpl
private int mShippingType;
private boolean mIsFinishedQueryingPaymentApps;
private List<PaymentApp> mPendingApps = new ArrayList<>();
private MinimalUICoordinator mMinimalUi;
private PaymentApp mInvokedPaymentApp;
private boolean mHideServerAutofillCards;
private boolean mWaitForUpdatedDetails;
......@@ -425,7 +422,7 @@ public class PaymentRequestImpl
public void show(boolean isUserGesture, boolean waitForUpdatedDetails) {
if (mComponentPaymentRequestImpl == null) return;
if (mPaymentUIsManager.getPaymentRequestUI() != null || mMinimalUi != null) {
if (mPaymentUIsManager.isShowingUI()) {
// Can be triggered only by a compromised renderer. In normal operation, calling show()
// twice on the same instance of PaymentRequest in JavaScript is rejected at the
// renderer level.
......@@ -516,7 +513,14 @@ public class PaymentRequestImpl
assert mPaymentUIsManager.getPaymentRequestUI() != null;
if (isMinimalUiApplicable()) {
triggerMinimalUi(chromeActivity);
if (mPaymentUIsManager.triggerMinimalUI(chromeActivity, mSpec.getRawTotal(),
this::onMinimalUIReady, this::onMinimalUiConfirmed,
this::onMinimalUiDismissed)) {
mDidRecordShowEvent = true;
mJourneyLogger.setEventOccurred(Event.SHOWN);
} else {
disconnectFromClientWithDebugMessage(ErrorStrings.MINIMAL_UI_SUPPRESSED);
}
return;
}
......@@ -553,30 +557,6 @@ public class PaymentRequestImpl
return PaymentFeatureList.isEnabled(PaymentFeatureList.WEB_PAYMENTS_MINIMAL_UI);
}
/**
* Triggers the minimal UI.
* @param chromeActivity The Android activity for the Chrome UI that will host the minimal UI.
*/
private void triggerMinimalUi(ChromeActivity chromeActivity) {
// Do not show the Payment Request UI dialog even if the minimal UI is suppressed.
mPaymentUIsManager.getPaymentUisShowStateReconciler().onBottomSheetShown();
mMinimalUi = new MinimalUICoordinator();
if (mMinimalUi.show(chromeActivity,
BottomSheetControllerProvider.from(chromeActivity.getWindowAndroid()),
(PaymentApp) mPaymentUIsManager.getPaymentMethodsSection().getSelectedItem(),
mPaymentUIsManager.getCurrencyFormatterMap().get(
mSpec.getRawTotal().amount.currency),
mPaymentUIsManager.getUiShoppingCart().getTotal(), this::onMinimalUIReady,
this::onMinimalUiConfirmed, this::onMinimalUiDismissed)) {
mDidRecordShowEvent = true;
mJourneyLogger.setEventOccurred(Event.SHOWN);
return;
}
disconnectFromClientWithDebugMessage(ErrorStrings.MINIMAL_UI_SUPPRESSED);
}
private void onMinimalUIReady() {
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onMinimalUIReady();
......@@ -608,6 +588,14 @@ public class PaymentRequestImpl
closeUIAndDestroyNativeObjects();
}
private void onPaymentRequestCompleteForNonMinimalUI() {
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onCompleteCalled();
}
closeUIAndDestroyNativeObjects();
}
private static Map<String, PaymentMethodData> getValidatedMethodData(
PaymentMethodData[] methodData, boolean googlePayBridgeEligible,
CardEditor paymentMethodsCollector) {
......@@ -740,9 +728,7 @@ public class PaymentRequestImpl
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
private boolean confirmMinimalUIForTestInternal() {
if (mMinimalUi == null) return false;
mMinimalUi.confirmForTest();
return true;
return mPaymentUIsManager.confirmMinimalUIForTest();
}
/**
......@@ -758,9 +744,7 @@ public class PaymentRequestImpl
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
private boolean dismissMinimalUIForTestInternal() {
if (mMinimalUi == null) return false;
mMinimalUi.dismissForTest();
return true;
return mPaymentUIsManager.dismissMinimalUIForTest();
}
/**
......@@ -1307,28 +1291,8 @@ public class PaymentRequestImpl
mSpec.getRawTotal().amount.value, true /*completed*/);
}
/** Update records of the used payment app for sorting payment apps next time. */
EditableOption selectedPaymentMethod =
mPaymentUIsManager.getPaymentMethodsSection().getSelectedItem();
PaymentPreferencesUtil.increasePaymentAppUseCount(selectedPaymentMethod.getIdentifier());
PaymentPreferencesUtil.setPaymentAppLastUseDate(
selectedPaymentMethod.getIdentifier(), System.currentTimeMillis());
if (mMinimalUi != null) {
if (result == PaymentComplete.FAIL) {
mMinimalUi.showErrorAndClose(
this::onMinimalUiErroredAndClosed, R.string.payments_error_message);
} else {
mMinimalUi.showCompleteAndClose(this::onMinimalUiCompletedAndClosed);
}
return;
}
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onCompleteCalled();
}
closeUIAndDestroyNativeObjects();
mPaymentUIsManager.onPaymentRequestComplete(result, this::onMinimalUiErroredAndClosed,
this::onMinimalUiCompletedAndClosed, this::onPaymentRequestCompleteForNonMinimalUI);
}
// Implement BrowserPaymentRequest:
......@@ -1872,9 +1836,9 @@ public class PaymentRequestImpl
public void onInstrumentDetailsError(String errorMessage) {
if (mComponentPaymentRequestImpl == null) return;
mInvokedPaymentApp = null;
if (mMinimalUi != null) {
if (mPaymentUIsManager.getMinimalUI() != null) {
mJourneyLogger.setAborted(AbortReason.ABORTED_BY_USER);
mMinimalUi.showErrorAndClose(
mPaymentUIsManager.getMinimalUI().showErrorAndClose(
this::onMinimalUiErroredAndClosed, R.string.payments_error_message);
return;
}
......@@ -1944,10 +1908,7 @@ public class PaymentRequestImpl
*/
private void closeUIAndDestroyNativeObjects() {
mPaymentUIsManager.ensureHideAndResetPaymentHandlerUi();
if (mMinimalUi != null) {
mMinimalUi.hide();
mMinimalUi = null;
}
mPaymentUIsManager.hideMinimalUI();
if (mPaymentUIsManager.getPaymentRequestUI() != null) {
mPaymentUIsManager.getPaymentRequestUI().close();
......
......@@ -8,10 +8,12 @@ import android.content.Context;
import androidx.annotation.VisibleForTesting;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.payments.ui.LineItem;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController;
import org.chromium.components.payments.CurrencyFormatter;
import org.chromium.components.payments.PaymentApp;
import org.chromium.payments.mojom.PaymentComplete;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.modelutil.PropertyModelChangeProcessor;
......@@ -150,6 +152,22 @@ public class MinimalUICoordinator {
mMediator.showErrorAndClose(observer, null, errorMessageResourceId);
}
/**
* Runs when the payment request is closed.
* @param result The status of PaymentComplete.
* @param onMinimalUiErroredAndClosed The function called when MinimalUI errors and closes.
* @param onMinimalUiCompletedAndClosed The function called when MinimalUI completes and closes.
*/
public void onPaymentRequestComplete(int result,
ErrorAndCloseObserver onMinimalUiErroredAndClosed,
CompleteAndCloseObserver onMinimalUiCompletedAndClosed) {
if (result == PaymentComplete.FAIL) {
showErrorAndClose(onMinimalUiErroredAndClosed, R.string.payments_error_message);
} else {
showCompleteAndClose(onMinimalUiCompletedAndClosed);
}
}
/** Confirms payment in minimal UI. Used only in tests. */
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public void confirmForTest() {
......
......@@ -37,6 +37,7 @@ import org.chromium.chrome.browser.payments.ShippingStrings;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator.PaymentHandlerUiObserver;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator.PaymentHandlerWebContentsObserver;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator;
import org.chromium.chrome.browser.payments.ui.PaymentRequestSection.OptionSection.FocusChangedObserver;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
......@@ -49,7 +50,7 @@ import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper;
import org.chromium.components.autofill.Completable;
import org.chromium.components.autofill.EditableOption;
import org.chromium.components.payments.BrowserPaymentRequest;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetControllerProvider;
import org.chromium.components.payments.CurrencyFormatter;
import org.chromium.components.payments.ErrorStrings;
import org.chromium.components.payments.JourneyLogger;
......@@ -141,6 +142,7 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
private TabModelSelector mObservedTabModelSelector;
private TabModel mObservedTabModel;
private OverviewModeBehavior mOverviewModeBehavior;
private MinimalUICoordinator mMinimalUi;
/**
* True if we should skip showing PaymentRequest UI.
......@@ -388,6 +390,99 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mHaveRequestedAutofillData;
}
/** @return The minimal UI coordinator. */
public MinimalUICoordinator getMinimalUI() { return mMinimalUi; }
/** @return Whether the Payment Request or Minimal UIs are showing. */
public boolean isShowingUI() {
return mPaymentRequestUI != null || mMinimalUi != null;
}
/**
* Confirms payment in minimal UI. Used only in test.
*
* @return Whether the payment was confirmed successfully.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public boolean confirmMinimalUIForTest() {
if (mMinimalUi == null) return false;
mMinimalUi.confirmForTest();
return true;
}
/**
* Dismisses the minimal UI. Used only in test.
*
* @return Whether the dismissal was successful.
*/
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
public boolean dismissMinimalUIForTest() {
if (mMinimalUi == null) return false;
mMinimalUi.dismissForTest();
return true;
}
/** Hides the minimal UI for when objects are closed and destroyed. */
public void hideMinimalUI() {
if (mMinimalUi != null) {
mMinimalUi.hide();
mMinimalUi = null;
}
}
/**
* Triggers the minimal UI.
* @param chromeActivity The Android activity for the Chrome UI that will host the minimal UI.
* @param mRawTotal The raw total of the payment item.
* @param readyObserver The onMinimalUIReady function.
* @param confirmObserver The onMinimalUiConfirmed function.
* @param dismissObserver The onMinimalUiDismissed function.
*/
public boolean triggerMinimalUI(ChromeActivity chromeActivity, PaymentItem mRawTotal,
MinimalUICoordinator.ReadyObserver readyObserver,
MinimalUICoordinator.ConfirmObserver confirmObserver,
MinimalUICoordinator.DismissObserver dismissObserver) {
// Do not show the Payment Request UI dialog even if the minimal UI is suppressed.
mPaymentUisShowStateReconciler.onBottomSheetShown();
mMinimalUi = new MinimalUICoordinator();
if (mMinimalUi.show(chromeActivity,
BottomSheetControllerProvider.from(chromeActivity.getWindowAndroid()),
(PaymentApp) mPaymentMethodsSection.getSelectedItem(),
mCurrencyFormatterMap.get(mRawTotal.amount.currency),
mUiShoppingCart.getTotal(), readyObserver,
confirmObserver, dismissObserver)) {
return true;
}
return false;
}
/**
* Called when the payment request is complete.
* @param result The status of PaymentComplete.
* @param onMinimalUiErroredAndClosed The function called when MinimalUI errors and closes.
* @param onMinimalUiCompletedAndClosed The function called when MinimalUI completes and closes.
* @param onPaymentRequestCompleteForNonMinimalUI The function called when PaymentRequest
* completes for non-minimal UI.
*/
public void onPaymentRequestComplete(int result,
MinimalUICoordinator.ErrorAndCloseObserver onMinimalUiErroredAndClosed,
MinimalUICoordinator.CompleteAndCloseObserver onMinimalUiCompletedAndClosed,
Runnable onPaymentRequestCompleteForNonMinimalUI) {
// Update records of the used payment app for sorting payment apps next time.
EditableOption selectedPaymentMethod = mPaymentMethodsSection.getSelectedItem();
PaymentPreferencesUtil.increasePaymentAppUseCount(selectedPaymentMethod.getIdentifier());
PaymentPreferencesUtil.setPaymentAppLastUseDate(
selectedPaymentMethod.getIdentifier(), System.currentTimeMillis());
if (mMinimalUi != null) {
mMinimalUi.onPaymentRequestComplete(result, onMinimalUiErroredAndClosed,
onMinimalUiCompletedAndClosed);
return;
}
onPaymentRequestCompleteForNonMinimalUI.run();
}
/** @return Whether PaymentRequestUI should be skipped. */
public boolean shouldSkipShowingPaymentRequestUi() {
return mShouldSkipShowingPaymentRequestUi;
......
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