Commit 7463fe9b authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Chromium LUCI CQ

[Payment] CPRService reduces ChromeActivity usages

Context:
In some cases, CPRService passes ChromeActivity to PaymentUiService when
PUiService really needs Context and WindowAndroid. So this CL changes
to provides Context and WindowAndroid to these cases. Doing so can
reduce Chrome features' dependencies on ChromeActivity.

Changes:
* CPRService provides Context and WindowAndroid instead of
  ChromeActivity to methods when applicable.

Bug: 1155788, 1140340, 1140344
Change-Id: Ib934dbbdba25bfc278ce13232d59ef3c4acf2131
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2577956
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#834987}
parent 4cebdd48
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.payments; package org.chromium.chrome.browser.payments;
import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -49,6 +50,7 @@ import org.chromium.payments.mojom.PaymentOptions; ...@@ -49,6 +50,7 @@ import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentRequest; import org.chromium.payments.mojom.PaymentRequest;
import org.chromium.payments.mojom.PaymentResponse; import org.chromium.payments.mojom.PaymentResponse;
import org.chromium.payments.mojom.PaymentValidationErrors; import org.chromium.payments.mojom.PaymentValidationErrors;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.url.GURL; import org.chromium.url.GURL;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -283,9 +285,9 @@ public class ChromePaymentRequestService ...@@ -283,9 +285,9 @@ public class ChromePaymentRequestService
assert mPaymentUiService.isPaymentRequestUiAlive(); assert mPaymentUiService.isPaymentRequestUiAlive();
if (isMinimalUiApplicable(isUserGestureShow)) { if (isMinimalUiApplicable(isUserGestureShow)) {
ChromeActivity chromeActivity = ChromeActivity.fromWebContents(mWebContents); WindowAndroid windowAndroid = mDelegate.getWindowAndroid(mRenderFrameHost);
if (chromeActivity == null) return ErrorStrings.ACTIVITY_NOT_FOUND; if (windowAndroid == null) return ErrorStrings.WINDOW_NOT_FOUND;
if (mPaymentUiService.triggerMinimalUI(chromeActivity, mSpec.getRawTotal(), if (mPaymentUiService.triggerMinimalUI(windowAndroid, mSpec.getRawTotal(),
this::onMinimalUIReady, this::onMinimalUiConfirmed, this::onMinimalUIReady, this::onMinimalUiConfirmed,
/*dismissObserver=*/ /*dismissObserver=*/
() ()
...@@ -407,8 +409,8 @@ public class ChromePaymentRequestService ...@@ -407,8 +409,8 @@ public class ChromePaymentRequestService
// Implements BrowserPaymentRequest: // Implements BrowserPaymentRequest:
@Override @Override
public String continueShow(boolean isFinishedQueryingPaymentApps) { public String continueShow(boolean isFinishedQueryingPaymentApps) {
ChromeActivity chromeActivity = ChromeActivity.fromWebContents(mWebContents); Context context = mDelegate.getContext(mRenderFrameHost);
if (chromeActivity == null) return ErrorStrings.ACTIVITY_NOT_FOUND; if (context == null) return ErrorStrings.CONTEXT_NOT_FOUND;
mPaymentUiService.updateDetailsOnPaymentRequestUI(mSpec.getPaymentDetails()); mPaymentUiService.updateDetailsOnPaymentRequestUI(mSpec.getPaymentDetails());
...@@ -416,7 +418,7 @@ public class ChromePaymentRequestService ...@@ -416,7 +418,7 @@ public class ChromePaymentRequestService
// promise gets resolved before all apps are ready. // promise gets resolved before all apps are ready.
if (mPaymentUiService.isPaymentRequestUiAlive() if (mPaymentUiService.isPaymentRequestUiAlive()
&& mPaymentUiService.shouldShowShippingSection()) { && mPaymentUiService.shouldShowShippingSection()) {
mPaymentUiService.createShippingSectionForPaymentRequestUI(chromeActivity); mPaymentUiService.createShippingSectionForPaymentRequestUI(context);
} }
// Triggered transaction amount gets recorded when both of the following conditions are met: // Triggered transaction amount gets recorded when both of the following conditions are met:
......
...@@ -37,6 +37,9 @@ import org.chromium.chrome.browser.payments.ShippingStrings; ...@@ -37,6 +37,9 @@ import org.chromium.chrome.browser.payments.ShippingStrings;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator; 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.PaymentHandlerUiObserver;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator; import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator.ConfirmObserver;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator.DismissObserver;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator.ReadyObserver;
import org.chromium.chrome.browser.payments.ui.PaymentRequestSection.OptionSection.FocusChangedObserver; import org.chromium.chrome.browser.payments.ui.PaymentRequestSection.OptionSection.FocusChangedObserver;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.SettingsLauncher; import org.chromium.chrome.browser.settings.SettingsLauncher;
...@@ -79,6 +82,7 @@ import org.chromium.payments.mojom.PaymentMethodData; ...@@ -79,6 +82,7 @@ import org.chromium.payments.mojom.PaymentMethodData;
import org.chromium.payments.mojom.PaymentOptions; import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentShippingOption; import org.chromium.payments.mojom.PaymentShippingOption;
import org.chromium.payments.mojom.PaymentValidationErrors; import org.chromium.payments.mojom.PaymentValidationErrors;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.url.GURL; import org.chromium.url.GURL;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -467,24 +471,25 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -467,24 +471,25 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/** /**
* Triggers the minimal UI. * Triggers the minimal UI.
* @param chromeActivity The Android activity for the Chrome UI that will host the minimal UI. * @param windowAndroid The window of the main activity.
* @param mRawTotal The raw total of the payment item. * @param mRawTotal The raw total of the payment item.
* @param readyObserver The onMinimalUIReady function. * @param readyObserver The onMinimalUIReady function.
* @param confirmObserver The onMinimalUiConfirmed function. * @param confirmObserver The onMinimalUiConfirmed function.
* @param dismissObserver The onMinimalUiDismissed function. * @param dismissObserver The onMinimalUiDismissed function.
* @return Whether the triggering is successful.
*/ */
public boolean triggerMinimalUI(ChromeActivity chromeActivity, PaymentItem mRawTotal, public boolean triggerMinimalUI(WindowAndroid windowAndroid, PaymentItem mRawTotal,
MinimalUICoordinator.ReadyObserver readyObserver, ReadyObserver readyObserver, ConfirmObserver confirmObserver,
MinimalUICoordinator.ConfirmObserver confirmObserver, DismissObserver dismissObserver) {
MinimalUICoordinator.DismissObserver dismissObserver) { Context context = windowAndroid.getContext().get();
if (context == null) return false;
// Do not show the Payment Request UI dialog even if the minimal UI is suppressed. // Do not show the Payment Request UI dialog even if the minimal UI is suppressed.
mPaymentUisShowStateReconciler.onBottomSheetShown(); mPaymentUisShowStateReconciler.onBottomSheetShown();
mMinimalUi = new MinimalUICoordinator(); mMinimalUi = new MinimalUICoordinator();
PaymentApp selectedApp = getSelectedPaymentApp(); PaymentApp selectedApp = getSelectedPaymentApp();
return mMinimalUi.show(chromeActivity, return mMinimalUi.show(context, BottomSheetControllerProvider.from(windowAndroid),
BottomSheetControllerProvider.from(chromeActivity.getWindowAndroid()), selectedApp, selectedApp, mCurrencyFormatterMap.get(mRawTotal.amount.currency),
mCurrencyFormatterMap.get(mRawTotal.amount.currency), mUiShoppingCart.getTotal(), mUiShoppingCart.getTotal(), readyObserver, confirmObserver, dismissObserver);
readyObserver, confirmObserver, dismissObserver);
} }
/** /**
...@@ -985,9 +990,6 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -985,9 +990,6 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
@Nullable @Nullable
public WebContents showPaymentHandlerUI(GURL url, boolean isOffTheRecord) { public WebContents showPaymentHandlerUI(GURL url, boolean isOffTheRecord) {
if (mPaymentHandlerUi != null) return null; if (mPaymentHandlerUi != null) return null;
ChromeActivity chromeActivity = ChromeActivity.fromWebContents(mWebContents);
if (chromeActivity == null) return null;
PaymentHandlerCoordinator paymentHandlerUi = new PaymentHandlerCoordinator(); PaymentHandlerCoordinator paymentHandlerUi = new PaymentHandlerCoordinator();
WebContents paymentHandlerWebContents = paymentHandlerUi.show( WebContents paymentHandlerWebContents = paymentHandlerUi.show(
/*paymentRequestWebContents=*/mWebContents, url, isOffTheRecord, /*paymentRequestWebContents=*/mWebContents, url, isOffTheRecord,
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package org.chromium.components.payments; package org.chromium.components.payments;
import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
...@@ -36,6 +37,7 @@ import org.chromium.payments.mojom.PaymentResponse; ...@@ -36,6 +37,7 @@ import org.chromium.payments.mojom.PaymentResponse;
import org.chromium.payments.mojom.PaymentShippingOption; import org.chromium.payments.mojom.PaymentShippingOption;
import org.chromium.payments.mojom.PaymentShippingType; import org.chromium.payments.mojom.PaymentShippingType;
import org.chromium.payments.mojom.PaymentValidationErrors; import org.chromium.payments.mojom.PaymentValidationErrors;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.url.GURL; import org.chromium.url.GURL;
import org.chromium.url.Origin; import org.chromium.url.Origin;
...@@ -286,6 +288,28 @@ public class PaymentRequestService ...@@ -286,6 +288,28 @@ public class PaymentRequestService
default PaymentAppService getPaymentAppService() { default PaymentAppService getPaymentAppService() {
return PaymentAppService.getInstance(); return PaymentAppService.getInstance();
} }
/**
* @return The context of the current activity, can be null when WebContents has been
* destroyed, the activity is gone, the window is closed, etc.
*/
@Nullable
default Context getContext(RenderFrameHost renderFrameHost) {
WindowAndroid window = getWindowAndroid(renderFrameHost);
if (window == null) return null;
return window.getContext().get();
}
/**
* @return The WindowAndroid of the current activity, can be null when WebContents has
* been destroyed, the activity is gone, etc.
*/
@Nullable
default WindowAndroid getWindowAndroid(RenderFrameHost renderFrameHost) {
WebContents webContents = PaymentRequestServiceUtil.getLiveWebContents(renderFrameHost);
if (webContents == null) return null;
return webContents.getTopLevelNativeWindow();
}
} }
/** /**
......
...@@ -28,6 +28,10 @@ public abstract class ErrorStrings {{ ...@@ -28,6 +28,10 @@ public abstract class ErrorStrings {{
public static final String ACTIVITY_NOT_FOUND = "Unable to find Chrome activity."; public static final String ACTIVITY_NOT_FOUND = "Unable to find Chrome activity.";
public static final String CONTEXT_NOT_FOUND = "Unable to find Chrome context.";
public static final String WINDOW_NOT_FOUND = "Unable to find Chrome window.";
public static final String PAYMENT_APP_INVALID_RESPONSE = "Payment app response is not valid."; public static final String PAYMENT_APP_INVALID_RESPONSE = "Payment app response is not valid.";
public static final String PAYMENT_APP_LAUNCH_FAIL = "Unable to invoke the payment app."; public static final String PAYMENT_APP_LAUNCH_FAIL = "Unable to invoke the payment app.";
......
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