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

[WebLayer] Move mInvokedPaymentApp into PRService

Changes:
* Moved CPRService#mInvokedPaymentApp into PRService.
* Refactored isInvokedInstrumentValidForPaymentMethodIdentifier() so
  as to be explicit about its dependency on mInvokedPaymentApp.

Bug: 1144527

Change-Id: I605dc470b41dd191cd362986963022c0f39dbddd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2520097
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824687}
parent e726ec7d
......@@ -105,7 +105,6 @@ public class ChromePaymentRequestService
private boolean mHasClosed;
private PaymentRequestSpec mSpec;
private PaymentApp mInvokedPaymentApp;
private boolean mHideServerAutofillCards;
private boolean mWaitForUpdatedDetails;
private PaymentHandlerHost mPaymentHandlerHost;
......@@ -512,9 +511,10 @@ public class ChromePaymentRequestService
/** Called by the payment app to get updated total based on the billing address, for example. */
@Override
public boolean changePaymentMethodFromInvokedApp(String methodName, String stringifiedDetails) {
if (TextUtils.isEmpty(methodName) || stringifiedDetails == null
|| mPaymentRequestService == null || mInvokedPaymentApp == null
|| mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
if (mPaymentRequestService == null) return false;
PaymentApp invokedPaymentApp = mPaymentRequestService.getInvokedPaymentApp();
if (TextUtils.isEmpty(methodName) || stringifiedDetails == null || invokedPaymentApp == null
|| invokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
return false;
}
......@@ -527,9 +527,10 @@ public class ChromePaymentRequestService
*/
@Override
public boolean changeShippingOptionFromInvokedApp(String shippingOptionId) {
if (TextUtils.isEmpty(shippingOptionId) || mPaymentRequestService == null
|| mInvokedPaymentApp == null
|| mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping
if (mPaymentRequestService == null) return false;
PaymentApp invokedPaymentApp = mPaymentRequestService.getInvokedPaymentApp();
if (TextUtils.isEmpty(shippingOptionId) || invokedPaymentApp == null
|| invokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping
|| mSpec.getRawShippingOptions() == null) {
return false;
}
......@@ -553,8 +554,9 @@ public class ChromePaymentRequestService
*/
@Override
public boolean changeShippingAddressFromInvokedApp(PaymentAddress shippingAddress) {
if (shippingAddress == null || mPaymentRequestService == null || mInvokedPaymentApp == null
|| mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping) {
PaymentApp invokedPaymentApp = mPaymentRequestService.getInvokedPaymentApp();
if (shippingAddress == null || mPaymentRequestService == null || invokedPaymentApp == null
|| invokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping) {
return false;
}
......@@ -665,10 +667,10 @@ public class ChromePaymentRequestService
*/
@Nullable
private WebContents openPaymentHandlerWindowInternal(GURL url) {
assert mInvokedPaymentApp != null;
assert mInvokedPaymentApp.getPaymentAppType() == PaymentAppType.SERVICE_WORKER_APP;
if (mPaymentRequestService == null) return null;
PaymentApp invokedPaymentApp = mPaymentRequestService.getInvokedPaymentApp();
assert invokedPaymentApp != null;
assert invokedPaymentApp.getPaymentAppType() == PaymentAppType.SERVICE_WORKER_APP;
@Nullable
WebContents paymentHandlerWebContents = mPaymentUiService.showPaymentHandlerUI(
......@@ -680,15 +682,16 @@ public class ChromePaymentRequestService
// UKM for payment app origin should get recorded only when the origin of the invoked
// payment app is shown to the user.
mJourneyLogger.setPaymentAppUkmSourceId(mInvokedPaymentApp.getUkmSourceId());
mJourneyLogger.setPaymentAppUkmSourceId(invokedPaymentApp.getUkmSourceId());
}
return paymentHandlerWebContents;
}
@Override
public boolean isInvokedInstrumentValidForPaymentMethodIdentifier(String methodName) {
return mInvokedPaymentApp != null
&& mInvokedPaymentApp.isValidForPaymentMethodData(methodName, null);
public boolean isInvokedInstrumentValidForPaymentMethodIdentifier(
String methodName, PaymentApp invokedPaymentApp) {
return invokedPaymentApp != null
&& invokedPaymentApp.isValidForPaymentMethodData(methodName, null);
}
// Implement BrowserPaymentRequest:
......@@ -713,9 +716,10 @@ public class ChromePaymentRequestService
return;
}
PaymentApp invokedPaymentApp = mPaymentRequestService.getInvokedPaymentApp();
if (!PaymentOptionsUtils.requestAnyInformation(mPaymentOptions)
&& (mInvokedPaymentApp == null
|| !mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate())) {
&& (invokedPaymentApp == null
|| !invokedPaymentApp.isWaitingForPaymentDetailsUpdate())) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_STATE);
return;
......@@ -731,14 +735,13 @@ public class ChromePaymentRequestService
mPaymentUiService.updateDetailsOnPaymentRequestUI(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems());
if (mInvokedPaymentApp != null && mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
if (invokedPaymentApp != null && invokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
// After a payment app has been invoked, all of the merchant's calls to update the price
// via updateWith() should be forwarded to the invoked app, so it can reflect the
// updated price in its UI.
mInvokedPaymentApp.updateWith(
invokedPaymentApp.updateWith(
PaymentDetailsConverter.convertToPaymentRequestDetailsUpdate(details,
mInvokedPaymentApp.handlesShippingAddress() /* handlesShipping */,
this /* methodChecker */));
/*methodChecker=*/this, invokedPaymentApp));
return;
}
......@@ -835,8 +838,9 @@ public class ChromePaymentRequestService
mSpec.recomputeSpecForDetails();
if (mInvokedPaymentApp != null && mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
mInvokedPaymentApp.onPaymentDetailsNotUpdated();
PaymentApp invokedPaymentApp = mPaymentRequestService.getInvokedPaymentApp();
if (invokedPaymentApp != null && invokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
invokedPaymentApp.onPaymentDetailsNotUpdated();
return;
}
......@@ -905,24 +909,22 @@ public class ChromePaymentRequestService
@Override
public boolean invokePaymentApp(EditableOption selectedShippingAddress,
EditableOption selectedShippingOption, PaymentApp selectedPaymentApp) {
mInvokedPaymentApp = selectedPaymentApp;
EditableOption selectedContact = mPaymentUiService.getContactSection() != null
? mPaymentUiService.getContactSection().getSelectedItem()
: null;
mPaymentResponseHelper = new PaymentResponseHelper(selectedShippingAddress,
selectedShippingOption, selectedContact, mInvokedPaymentApp, mPaymentOptions,
selectedShippingOption, selectedContact, selectedPaymentApp, mPaymentOptions,
mSkipToGPayHelper != null, this);
mInvokedPaymentApp.setPaymentHandlerHost(getPaymentHandlerHost());
selectedPaymentApp.setPaymentHandlerHost(getPaymentHandlerHost());
// Only native apps can use PaymentDetailsUpdateService.
if (mInvokedPaymentApp.getPaymentAppType() == PaymentAppType.NATIVE_MOBILE_APP) {
if (selectedPaymentApp.getPaymentAppType() == PaymentAppType.NATIVE_MOBILE_APP) {
PaymentDetailsUpdateServiceHelper.getInstance().initialize(new PackageManagerDelegate(),
((AndroidPaymentApp) mInvokedPaymentApp).packageName(),
((AndroidPaymentApp) selectedPaymentApp).packageName(),
this /* PaymentApp.PaymentRequestUpdateEventListener */);
}
mPaymentRequestService.invokePaymentApp(mInvokedPaymentApp, /*callback=*/this);
return !mInvokedPaymentApp.isAutofillInstrument();
mPaymentRequestService.invokePaymentApp(selectedPaymentApp, /*callback=*/this);
return !selectedPaymentApp.isAutofillInstrument();
}
private PaymentHandlerHost getPaymentHandlerHost() {
......@@ -971,9 +973,9 @@ public class ChromePaymentRequestService
@Override
public void abort() {
if (mPaymentRequestService == null) return;
if (mInvokedPaymentApp != null) {
mInvokedPaymentApp.abortPaymentApp(/*callback=*/this);
PaymentApp invokedPaymentApp = mPaymentRequestService.getInvokedPaymentApp();
if (invokedPaymentApp != null) {
invokedPaymentApp.abortPaymentApp(/*callback=*/this);
return;
}
onInstrumentAbortResult(true);
......@@ -1195,7 +1197,7 @@ public class ChromePaymentRequestService
@Override
public void onInstrumentDetailsError(String errorMessage) {
if (mPaymentRequestService == null) return;
mInvokedPaymentApp = null;
mPaymentRequestService.resetInvokedPaymentApp();
if (mPaymentUiService.getMinimalUI() != null) {
mJourneyLogger.setAborted(AbortReason.ABORTED_BY_USER);
mPaymentUiService.getMinimalUI().showErrorAndClose(
......
......@@ -26,9 +26,11 @@ public class PaymentDetailsConverter {
* Checks whether the invoked payment instrument is valid for the given payment method
* identifier.
* @param methodName Payment method identifier.
* @param invokedPaymentApp The invoked payment instrument (app).
* @return Whether the invoked instrument is valid for the given payment method identifier.
*/
boolean isInvokedInstrumentValidForPaymentMethodIdentifier(String methodName);
boolean isInvokedInstrumentValidForPaymentMethodIdentifier(
String methodName, PaymentApp invokedPaymentApp);
}
/**
......@@ -41,18 +43,18 @@ public class PaymentDetailsConverter {
* sent to the payment handler.
* @param details The pre-validated payment details update from the merchant. Should not
* be null.
* @param handlesShipping The shipping related information should get redacted when
* handlesShipping is false.
* @param methodChecker The object that can check whether the invoked payment instrument is
* valid for the given payment method identifier. Should not be null.
* @param invokedPaymentApp The invoked payment app.
* @return The data structure that can be sent to the invoked payment handler.
*/
public static PaymentRequestDetailsUpdate convertToPaymentRequestDetailsUpdate(
PaymentDetails details, boolean handlesShipping, MethodChecker methodChecker) {
PaymentDetails details, MethodChecker methodChecker, PaymentApp invokedPaymentApp) {
// Keep in sync with components/payments/content/payment_details_converter.cc.
assert details != null;
assert methodChecker != null;
boolean handlesShipping = invokedPaymentApp.handlesShippingAddress();
PaymentRequestDetailsUpdate response = new PaymentRequestDetailsUpdate();
response.error = details.error;
response.stringifiedPaymentMethodErrors = details.stringifiedPaymentMethodErrors;
......@@ -65,7 +67,7 @@ public class PaymentDetailsConverter {
for (int i = 0; i < details.modifiers.length; i++) {
if (!methodChecker.isInvokedInstrumentValidForPaymentMethodIdentifier(
details.modifiers[i].methodData.supportedMethod)) {
details.modifiers[i].methodData.supportedMethod, invokedPaymentApp)) {
continue;
}
......
......@@ -125,6 +125,8 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment
private boolean mIsCanMakePaymentResponsePending;
private boolean mIsHasEnrolledInstrumentResponsePending;
@Nullable
private PaymentApp mInvokedPaymentApp;
/**
* An observer interface injected when running tests to allow them to observe events.
......@@ -512,6 +514,7 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment
mSpec.getRawTotal(), mSpec.getRawLineItems(),
Collections.unmodifiableMap(modifiers), paymentOptions, redactedShippingOptions,
callback);
mInvokedPaymentApp = paymentApp;
mJourneyLogger.setEventOccurred(Event.PAY_CLICKED);
boolean isAutofillCard = paymentApp.isAutofillInstrument();
// Record what type of app was selected when "Pay" was clicked.
......@@ -1187,4 +1190,15 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment
public String getTwaPackageName() {
return mDelegate.getTwaPackageName();
}
/** @return The invoked payment app, can be null. */
@Nullable
public PaymentApp getInvokedPaymentApp() {
return mInvokedPaymentApp;
}
/** Sets no payment app is invoked. */
public void resetInvokedPaymentApp() {
mInvokedPaymentApp = null;
}
}
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