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