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

[PRImpl] Encapsulate PRClient in CPRImpl

Change:
* Encapsulate PRClient in CPRImpl. Before the change, PRImpl had to
manage the lifespan of PRClient and CPRImpl. After the change, PRImpl
only need to manage the lifespan of CPRImpl, as PRClient is no longer
visible to PRImpl.
* Clarify that PR#close can only be called by the renderer
* Create CPRImpl#teardown to play the role of closing (should have
called #close if not for the collision with the PR#close interface).
* Clarify the usage of CPRImpl#teardown, BPR#close, PRImpl#close. Add
mHasClosed/mHasTorndown for CPRImpl#teardown and PRImpl#close to make
sure these methods can be called within themself without worrying
deadloop. For example, while PRImpl#close calls CPRImpl#teardown,
CPRImpl#teardown can call PRImpl#close back without causing deadloop.

Acronym:
PRClient = PaymentRequestClient
CPRImpl = ComponentPaymentRequestImpl
BPR = BrowserPaymentRequest
PR = PaymentRequest

Bug: 1102522

Change-Id: Ic6495f02593f9a1cfad9747e52e9dcf3a05b1018
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2343631
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796532}
parent e69fc19a
...@@ -84,7 +84,6 @@ import org.chromium.components.url_formatter.UrlFormatter; ...@@ -84,7 +84,6 @@ import org.chromium.components.url_formatter.UrlFormatter;
import org.chromium.content_public.browser.RenderFrameHost; import org.chromium.content_public.browser.RenderFrameHost;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsStatics; import org.chromium.content_public.browser.WebContentsStatics;
import org.chromium.mojo.system.MojoException;
import org.chromium.payments.mojom.AddressErrors; import org.chromium.payments.mojom.AddressErrors;
import org.chromium.payments.mojom.CanMakePaymentQueryResult; import org.chromium.payments.mojom.CanMakePaymentQueryResult;
import org.chromium.payments.mojom.HasEnrolledInstrumentQueryResult; import org.chromium.payments.mojom.HasEnrolledInstrumentQueryResult;
...@@ -99,7 +98,6 @@ import org.chromium.payments.mojom.PaymentItem; ...@@ -99,7 +98,6 @@ import org.chromium.payments.mojom.PaymentItem;
import org.chromium.payments.mojom.PaymentMethodData; import org.chromium.payments.mojom.PaymentMethodData;
import org.chromium.payments.mojom.PaymentOptions; import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentRequest; import org.chromium.payments.mojom.PaymentRequest;
import org.chromium.payments.mojom.PaymentRequestClient;
import org.chromium.payments.mojom.PaymentResponse; 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;
...@@ -172,7 +170,7 @@ public class PaymentRequestImpl ...@@ -172,7 +170,7 @@ public class PaymentRequestImpl
*/ */
private static PaymentRequestImpl sShowingPaymentRequest; private static PaymentRequestImpl sShowingPaymentRequest;
private final ComponentPaymentRequestImpl mComponentPaymentRequestImpl; private ComponentPaymentRequestImpl mComponentPaymentRequestImpl;
/** Monitors changes in the TabModelSelector. */ /** Monitors changes in the TabModelSelector. */
private final TabModelSelectorObserver mSelectorObserver = new EmptyTabModelSelectorObserver() { private final TabModelSelectorObserver mSelectorObserver = new EmptyTabModelSelectorObserver() {
...@@ -230,6 +228,8 @@ public class PaymentRequestImpl ...@@ -230,6 +228,8 @@ public class PaymentRequestImpl
private boolean mIsCurrentPaymentRequestShowing; private boolean mIsCurrentPaymentRequestShowing;
private boolean mWasRetryCalled; private boolean mWasRetryCalled;
private boolean mHasClosed;
/** /**
* The raw total amount being charged, as it was received from the website. This data is passed * The raw total amount being charged, as it was received from the website. This data is passed
* to the payment app. * to the payment app.
...@@ -376,7 +376,7 @@ public class PaymentRequestImpl ...@@ -376,7 +376,7 @@ public class PaymentRequestImpl
@Override @Override
public void init(PaymentMethodData[] methodData, PaymentDetails details, public void init(PaymentMethodData[] methodData, PaymentDetails details,
@Nullable PaymentOptions options, boolean googlePayBridgeEligible) { @Nullable PaymentOptions options, boolean googlePayBridgeEligible) {
assert getClient() != null; assert mComponentPaymentRequestImpl != null;
mMethodData = new HashMap<>(); mMethodData = new HashMap<>();
mComponentPaymentRequestImpl.registerPaymentRequestLifecycleObserver(mPaymentUIsManager); mComponentPaymentRequestImpl.registerPaymentRequestLifecycleObserver(mPaymentUIsManager);
...@@ -607,9 +607,8 @@ public class PaymentRequestImpl ...@@ -607,9 +607,8 @@ public class PaymentRequestImpl
mWebContents.getLastCommittedUrl(), mWebContents.getLastCommittedUrl(),
activity.getResources().getDimensionPixelSize(R.dimen.payments_favicon_size), activity.getResources().getDimensionPixelSize(R.dimen.payments_favicon_size),
(bitmap, iconUrl) -> { (bitmap, iconUrl) -> {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl != null && bitmap == null) {
if (client != null && bitmap == null) { mComponentPaymentRequestImpl.warnNoFavicon();
client.warnNoFavicon();
} }
if (mPaymentUIsManager.getPaymentRequestUI() != null && bitmap != null) { if (mPaymentUIsManager.getPaymentRequestUI() != null && bitmap != null) {
mPaymentUIsManager.getPaymentRequestUI().setTitleBitmap(bitmap); mPaymentUIsManager.getPaymentRequestUI().setTitleBitmap(bitmap);
...@@ -640,7 +639,7 @@ public class PaymentRequestImpl ...@@ -640,7 +639,7 @@ public class PaymentRequestImpl
*/ */
@Override @Override
public void show(boolean isUserGesture, boolean waitForUpdatedDetails) { public void show(boolean isUserGesture, boolean waitForUpdatedDetails) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (mPaymentUIsManager.getPaymentRequestUI() != null || mMinimalUi != null) { if (mPaymentUIsManager.getPaymentRequestUI() != null || mMinimalUi != null) {
// Can be triggered only by a compromised renderer. In normal operation, calling show() // Can be triggered only by a compromised renderer. In normal operation, calling show()
...@@ -806,17 +805,16 @@ public class PaymentRequestImpl ...@@ -806,17 +805,16 @@ public class PaymentRequestImpl
} }
private void onMinimalUiErroredAndClosed() { private void onMinimalUiErroredAndClosed() {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
closeClient(); close();
closeUIAndDestroyNativeObjects(); closeUIAndDestroyNativeObjects();
} }
private void onMinimalUiCompletedAndClosed() { private void onMinimalUiCompletedAndClosed() {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl != null) {
if (client != null) { mComponentPaymentRequestImpl.onComplete();
client.onComplete();
} }
closeClient(); close();
closeUIAndDestroyNativeObjects(); closeUIAndDestroyNativeObjects();
} }
...@@ -852,14 +850,13 @@ public class PaymentRequestImpl ...@@ -852,14 +850,13 @@ public class PaymentRequestImpl
/** 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) {
PaymentRequestClient client = getClient(); if (TextUtils.isEmpty(methodName) || stringifiedDetails == null
if (TextUtils.isEmpty(methodName) || stringifiedDetails == null || client == null || mComponentPaymentRequestImpl == null || mInvokedPaymentApp == null
|| mInvokedPaymentApp == null
|| mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) { || mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
return false; return false;
} }
client.onPaymentMethodChange(methodName, stringifiedDetails); mComponentPaymentRequestImpl.onPaymentMethodChange(methodName, stringifiedDetails);
return true; return true;
} }
...@@ -868,8 +865,8 @@ public class PaymentRequestImpl ...@@ -868,8 +865,8 @@ public class PaymentRequestImpl
*/ */
@Override @Override
public boolean changeShippingOptionFromInvokedApp(String shippingOptionId) { public boolean changeShippingOptionFromInvokedApp(String shippingOptionId) {
PaymentRequestClient client = getClient(); if (TextUtils.isEmpty(shippingOptionId) || mComponentPaymentRequestImpl == null
if (TextUtils.isEmpty(shippingOptionId) || client == null || mInvokedPaymentApp == null || mInvokedPaymentApp == null
|| mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping || mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping
|| mRawShippingOptions == null) { || mRawShippingOptions == null) {
return false; return false;
...@@ -885,7 +882,7 @@ public class PaymentRequestImpl ...@@ -885,7 +882,7 @@ public class PaymentRequestImpl
if (!isValidId) return false; if (!isValidId) return false;
client.onShippingOptionChange(shippingOptionId); mComponentPaymentRequestImpl.onShippingOptionChange(shippingOptionId);
return true; return true;
} }
...@@ -894,14 +891,14 @@ public class PaymentRequestImpl ...@@ -894,14 +891,14 @@ public class PaymentRequestImpl
*/ */
@Override @Override
public boolean changeShippingAddressFromInvokedApp(PaymentAddress shippingAddress) { public boolean changeShippingAddressFromInvokedApp(PaymentAddress shippingAddress) {
PaymentRequestClient client = getClient(); if (shippingAddress == null || mComponentPaymentRequestImpl == null
if (shippingAddress == null || client == null || mInvokedPaymentApp == null || mInvokedPaymentApp == null
|| mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping) { || mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate() || !mRequestShipping) {
return false; return false;
} }
redactShippingAddress(shippingAddress); redactShippingAddress(shippingAddress);
client.onShippingAddressChange(shippingAddress); mComponentPaymentRequestImpl.onShippingAddressChange(shippingAddress);
return true; return true;
} }
...@@ -1024,7 +1021,7 @@ public class PaymentRequestImpl ...@@ -1024,7 +1021,7 @@ public class PaymentRequestImpl
*/ */
@Override @Override
public void updateWith(PaymentDetails details) { public void updateWith(PaymentDetails details) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (mWaitForUpdatedDetails) { if (mWaitForUpdatedDetails) {
initializeWithUpdatedDetails(details); initializeWithUpdatedDetails(details);
...@@ -1128,7 +1125,7 @@ public class PaymentRequestImpl ...@@ -1128,7 +1125,7 @@ public class PaymentRequestImpl
*/ */
@Override @Override
public void onPaymentDetailsNotUpdated() { public void onPaymentDetailsNotUpdated() {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (mPaymentUIsManager.getPaymentRequestUI() == null) { if (mPaymentUIsManager.getPaymentRequestUI() == null) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER); mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
...@@ -1270,8 +1267,7 @@ public class PaymentRequestImpl ...@@ -1270,8 +1267,7 @@ public class PaymentRequestImpl
@PaymentRequestUI.SelectionResult @PaymentRequestUI.SelectionResult
public int onSectionOptionSelected(@PaymentRequestUI.DataType int optionType, public int onSectionOptionSelected(@PaymentRequestUI.DataType int optionType,
EditableOption option, Callback<PaymentInformation> callback) { EditableOption option, Callback<PaymentInformation> callback) {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl == null) return SelectionResult.NONE;
if (client == null) return SelectionResult.NONE;
if (optionType == PaymentRequestUI.DataType.SHIPPING_ADDRESSES) { if (optionType == PaymentRequestUI.DataType.SHIPPING_ADDRESSES) {
// Log the change of shipping address. // Log the change of shipping address.
mJourneyLogger.incrementSelectionChanges(Section.SHIPPING_ADDRESS); mJourneyLogger.incrementSelectionChanges(Section.SHIPPING_ADDRESS);
...@@ -1289,7 +1285,7 @@ public class PaymentRequestImpl ...@@ -1289,7 +1285,7 @@ public class PaymentRequestImpl
} else if (optionType == PaymentRequestUI.DataType.SHIPPING_OPTIONS) { } else if (optionType == PaymentRequestUI.DataType.SHIPPING_OPTIONS) {
// This may update the line items. // This may update the line items.
mPaymentUIsManager.getUiShippingOptions().setSelectedItem(option); mPaymentUIsManager.getUiShippingOptions().setSelectedItem(option);
client.onShippingOptionChange(option.getIdentifier()); mComponentPaymentRequestImpl.onShippingOptionChange(option.getIdentifier());
mPaymentUIsManager.setPaymentInformationCallback(callback); mPaymentUIsManager.setPaymentInformationCallback(callback);
return PaymentRequestUI.SelectionResult.ASYNCHRONOUS_VALIDATION; return PaymentRequestUI.SelectionResult.ASYNCHRONOUS_VALIDATION;
} else if (optionType == PaymentRequestUI.DataType.CONTACT_DETAILS) { } else if (optionType == PaymentRequestUI.DataType.CONTACT_DETAILS) {
...@@ -1367,7 +1363,7 @@ public class PaymentRequestImpl ...@@ -1367,7 +1363,7 @@ public class PaymentRequestImpl
@Override @Override
public void onInstrumentDetailsLoadingWithoutUI() { public void onInstrumentDetailsLoadingWithoutUI() {
if (getClient() == null || mPaymentUIsManager.getPaymentRequestUI() == null if (mComponentPaymentRequestImpl == null || mPaymentUIsManager.getPaymentRequestUI() == null
|| mPaymentResponseHelper == null) { || mPaymentResponseHelper == null) {
return; return;
} }
...@@ -1483,9 +1479,10 @@ public class PaymentRequestImpl ...@@ -1483,9 +1479,10 @@ public class PaymentRequestImpl
private void disconnectFromClientWithDebugMessage(String debugMessage, int reason) { private void disconnectFromClientWithDebugMessage(String debugMessage, int reason) {
Log.d(TAG, debugMessage); Log.d(TAG, debugMessage);
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl != null) {
if (client != null) client.onError(reason, debugMessage); mComponentPaymentRequestImpl.onError(reason, debugMessage);
closeClient(); }
close();
closeUIAndDestroyNativeObjects(); closeUIAndDestroyNativeObjects();
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) { if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onConnectionTerminated(); ComponentPaymentRequestImpl.getNativeObserverForTest().onConnectionTerminated();
...@@ -1498,7 +1495,7 @@ public class PaymentRequestImpl ...@@ -1498,7 +1495,7 @@ public class PaymentRequestImpl
*/ */
@Override @Override
public void abort() { public void abort() {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (mInvokedPaymentApp != null) { if (mInvokedPaymentApp != null) {
mInvokedPaymentApp.abortPaymentApp(/*callback=*/this); mInvokedPaymentApp.abortPaymentApp(/*callback=*/this);
...@@ -1510,13 +1507,11 @@ public class PaymentRequestImpl ...@@ -1510,13 +1507,11 @@ public class PaymentRequestImpl
/** Called by the payment app in response to an abort request. */ /** Called by the payment app in response to an abort request. */
@Override @Override
public void onInstrumentAbortResult(boolean abortSucceeded) { public void onInstrumentAbortResult(boolean abortSucceeded) {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl == null) return;
if (client == null) return; mComponentPaymentRequestImpl.onAbort(abortSucceeded);
client.onAbort(abortSucceeded);
if (abortSucceeded) { if (abortSucceeded) {
closeClient();
mJourneyLogger.setAborted(AbortReason.ABORTED_BY_MERCHANT); mJourneyLogger.setAborted(AbortReason.ABORTED_BY_MERCHANT);
closeUIAndDestroyNativeObjects(); close();
} else { } else {
if (ComponentPaymentRequestImpl.getObserverForTest() != null) { if (ComponentPaymentRequestImpl.getObserverForTest() != null) {
ComponentPaymentRequestImpl.getObserverForTest() ComponentPaymentRequestImpl.getObserverForTest()
...@@ -1534,7 +1529,7 @@ public class PaymentRequestImpl ...@@ -1534,7 +1529,7 @@ public class PaymentRequestImpl
*/ */
@Override @Override
public void complete(int result) { public void complete(int result) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (result != PaymentComplete.FAIL) { if (result != PaymentComplete.FAIL) {
mJourneyLogger.setCompleted(); mJourneyLogger.setCompleted();
...@@ -1573,7 +1568,7 @@ public class PaymentRequestImpl ...@@ -1573,7 +1568,7 @@ public class PaymentRequestImpl
// Implement BrowserPaymentRequest: // Implement BrowserPaymentRequest:
@Override @Override
public void retry(PaymentValidationErrors errors) { public void retry(PaymentValidationErrors errors) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (!PaymentValidator.validatePaymentValidationErrors(errors)) { if (!PaymentValidator.validatePaymentValidationErrors(errors)) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER); mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
...@@ -1666,7 +1661,7 @@ public class PaymentRequestImpl ...@@ -1666,7 +1661,7 @@ public class PaymentRequestImpl
/** Called by the merchant website to check if the user has complete payment apps. */ /** Called by the merchant website to check if the user has complete payment apps. */
@Override @Override
public void canMakePayment() { public void canMakePayment() {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) { if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onCanMakePaymentCalled(); ComponentPaymentRequestImpl.getNativeObserverForTest().onCanMakePaymentCalled();
...@@ -1680,14 +1675,14 @@ public class PaymentRequestImpl ...@@ -1680,14 +1675,14 @@ public class PaymentRequestImpl
} }
private void respondCanMakePaymentQuery() { private void respondCanMakePaymentQuery() {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl == null) return;
if (client == null) return;
mIsCanMakePaymentResponsePending = false; mIsCanMakePaymentResponsePending = false;
boolean response = mCanMakePayment && mDelegate.prefsCanMakePayment(); boolean response = mCanMakePayment && mDelegate.prefsCanMakePayment();
client.onCanMakePayment(response ? CanMakePaymentQueryResult.CAN_MAKE_PAYMENT mComponentPaymentRequestImpl.onCanMakePayment(response
: CanMakePaymentQueryResult.CANNOT_MAKE_PAYMENT); ? CanMakePaymentQueryResult.CAN_MAKE_PAYMENT
: CanMakePaymentQueryResult.CANNOT_MAKE_PAYMENT);
mJourneyLogger.setCanMakePaymentValue(response || mIsOffTheRecord); mJourneyLogger.setCanMakePaymentValue(response || mIsOffTheRecord);
...@@ -1704,7 +1699,7 @@ public class PaymentRequestImpl ...@@ -1704,7 +1699,7 @@ public class PaymentRequestImpl
/** Called by the merchant website to check if the user has complete payment instruments. */ /** Called by the merchant website to check if the user has complete payment instruments. */
@Override @Override
public void hasEnrolledInstrument(boolean perMethodQuota) { public void hasEnrolledInstrument(boolean perMethodQuota) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) { if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onHasEnrolledInstrumentCalled(); ComponentPaymentRequestImpl.getNativeObserverForTest().onHasEnrolledInstrumentCalled();
...@@ -1720,20 +1715,20 @@ public class PaymentRequestImpl ...@@ -1720,20 +1715,20 @@ public class PaymentRequestImpl
} }
private void respondHasEnrolledInstrumentQuery(boolean response) { private void respondHasEnrolledInstrumentQuery(boolean response) {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl == null) return;
if (client == null) return;
mIsHasEnrolledInstrumentResponsePending = false; mIsHasEnrolledInstrumentResponsePending = false;
if (CanMakePaymentQuery.canQuery(mWebContents, mTopLevelOrigin, mPaymentRequestOrigin, if (CanMakePaymentQuery.canQuery(mWebContents, mTopLevelOrigin, mPaymentRequestOrigin,
mQueryForQuota, mHasEnrolledInstrumentUsesPerMethodQuota)) { mQueryForQuota, mHasEnrolledInstrumentUsesPerMethodQuota)) {
client.onHasEnrolledInstrument(response mComponentPaymentRequestImpl.onHasEnrolledInstrument(response
? HasEnrolledInstrumentQueryResult.HAS_ENROLLED_INSTRUMENT ? HasEnrolledInstrumentQueryResult.HAS_ENROLLED_INSTRUMENT
: HasEnrolledInstrumentQueryResult.HAS_NO_ENROLLED_INSTRUMENT); : HasEnrolledInstrumentQueryResult.HAS_NO_ENROLLED_INSTRUMENT);
} else if (shouldEnforceCanMakePaymentQueryQuota()) { } else if (shouldEnforceCanMakePaymentQueryQuota()) {
client.onHasEnrolledInstrument(HasEnrolledInstrumentQueryResult.QUERY_QUOTA_EXCEEDED); mComponentPaymentRequestImpl.onHasEnrolledInstrument(
HasEnrolledInstrumentQueryResult.QUERY_QUOTA_EXCEEDED);
} else { } else {
client.onHasEnrolledInstrument(response mComponentPaymentRequestImpl.onHasEnrolledInstrument(response
? HasEnrolledInstrumentQueryResult.WARNING_HAS_ENROLLED_INSTRUMENT ? HasEnrolledInstrumentQueryResult.WARNING_HAS_ENROLLED_INSTRUMENT
: HasEnrolledInstrumentQueryResult.WARNING_HAS_NO_ENROLLED_INSTRUMENT); : HasEnrolledInstrumentQueryResult.WARNING_HAS_NO_ENROLLED_INSTRUMENT);
} }
...@@ -1764,36 +1759,16 @@ public class PaymentRequestImpl ...@@ -1764,36 +1759,16 @@ public class PaymentRequestImpl
} }
// Implement BrowserPaymentRequest: // Implement BrowserPaymentRequest:
/**
* Called when the renderer closes the Mojo connection.
*/
@Override @Override
public void close() { public void close() {
if (getClient() == null) return; if (mHasClosed) return;
closeClient(); mHasClosed = true;
mJourneyLogger.setAborted(AbortReason.MOJO_RENDERER_CLOSING);
if (ComponentPaymentRequestImpl.getObserverForTest() != null) { assert mComponentPaymentRequestImpl != null;
ComponentPaymentRequestImpl.getObserverForTest().onRendererClosedMojoConnection(); mComponentPaymentRequestImpl.teardown();
} mComponentPaymentRequestImpl = null;
closeUIAndDestroyNativeObjects();
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onConnectionTerminated();
}
}
// Implement BrowserPaymentRequest:
/**
* Called when the Mojo connection encounters an error.
*/
@Override
public void onConnectionError(MojoException e) {
if (getClient() == null) return;
closeClient();
mJourneyLogger.setAborted(AbortReason.MOJO_CONNECTION_ERROR);
closeUIAndDestroyNativeObjects(); closeUIAndDestroyNativeObjects();
if (ComponentPaymentRequestImpl.getNativeObserverForTest() != null) {
ComponentPaymentRequestImpl.getNativeObserverForTest().onConnectionTerminated();
}
} }
// PaymentAppFactoryParams implementation. // PaymentAppFactoryParams implementation.
...@@ -1892,7 +1867,7 @@ public class PaymentRequestImpl ...@@ -1892,7 +1867,7 @@ public class PaymentRequestImpl
// PaymentAppFactoryDelegate implementation. // PaymentAppFactoryDelegate implementation.
@Override @Override
public void onCanMakePaymentCalculated(boolean canMakePayment) { public void onCanMakePaymentCalculated(boolean canMakePayment) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
mCanMakePayment = canMakePayment; mCanMakePayment = canMakePayment;
...@@ -1912,7 +1887,7 @@ public class PaymentRequestImpl ...@@ -1912,7 +1887,7 @@ public class PaymentRequestImpl
// PaymentAppFactoryDelegate implementation. // PaymentAppFactoryDelegate implementation.
@Override @Override
public void onPaymentAppCreated(PaymentApp paymentApp) { public void onPaymentAppCreated(PaymentApp paymentApp) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
mHideServerAutofillCards |= paymentApp.isServerAutofillInstrumentReplacement(); mHideServerAutofillCards |= paymentApp.isServerAutofillInstrumentReplacement();
paymentApp.setHaveRequestedAutofillData(mPaymentUIsManager.haveRequestedAutofillData()); paymentApp.setHaveRequestedAutofillData(mPaymentUIsManager.haveRequestedAutofillData());
...@@ -1942,7 +1917,7 @@ public class PaymentRequestImpl ...@@ -1942,7 +1917,7 @@ public class PaymentRequestImpl
public void onDoneCreatingPaymentApps(PaymentAppFactoryInterface factory /* Unused */) { public void onDoneCreatingPaymentApps(PaymentAppFactoryInterface factory /* Unused */) {
mIsFinishedQueryingPaymentApps = true; mIsFinishedQueryingPaymentApps = true;
if (getClient() == null || disconnectIfNoPaymentMethodsSupported()) { if (mComponentPaymentRequestImpl == null || disconnectIfNoPaymentMethodsSupported()) {
return; return;
} }
...@@ -2146,7 +2121,7 @@ public class PaymentRequestImpl ...@@ -2146,7 +2121,7 @@ public class PaymentRequestImpl
assert methodName != null; assert methodName != null;
assert stringifiedDetails != null; assert stringifiedDetails != null;
if (getClient() == null || mPaymentResponseHelper == null) { if (mComponentPaymentRequestImpl == null || mPaymentResponseHelper == null) {
return; return;
} }
...@@ -2178,9 +2153,8 @@ public class PaymentRequestImpl ...@@ -2178,9 +2153,8 @@ public class PaymentRequestImpl
disconnectFromClientWithDebugMessage( disconnectFromClientWithDebugMessage(
ErrorStrings.PAYMENT_APP_INVALID_RESPONSE, PaymentErrorReason.NOT_SUPPORTED); ErrorStrings.PAYMENT_APP_INVALID_RESPONSE, PaymentErrorReason.NOT_SUPPORTED);
} }
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl == null) return;
if (client == null) return; mComponentPaymentRequestImpl.onPaymentResponse(response);
client.onPaymentResponse(response);
mPaymentResponseHelper = null; mPaymentResponseHelper = null;
if (ComponentPaymentRequestImpl.getObserverForTest() != null) { if (ComponentPaymentRequestImpl.getObserverForTest() != null) {
ComponentPaymentRequestImpl.getObserverForTest().onPaymentResponseReady(); ComponentPaymentRequestImpl.getObserverForTest().onPaymentResponseReady();
...@@ -2190,7 +2164,7 @@ public class PaymentRequestImpl ...@@ -2190,7 +2164,7 @@ public class PaymentRequestImpl
/** Called if unable to retrieve payment details. */ /** Called if unable to retrieve payment details. */
@Override @Override
public void onInstrumentDetailsError(String errorMessage) { public void onInstrumentDetailsError(String errorMessage) {
if (getClient() == null) return; if (mComponentPaymentRequestImpl == null) return;
mInvokedPaymentApp = null; mInvokedPaymentApp = null;
if (mMinimalUi != null) { if (mMinimalUi != null) {
mJourneyLogger.setAborted(AbortReason.ABORTED_BY_USER); mJourneyLogger.setAborted(AbortReason.ABORTED_BY_USER);
...@@ -2212,8 +2186,7 @@ public class PaymentRequestImpl ...@@ -2212,8 +2186,7 @@ public class PaymentRequestImpl
@Override @Override
public void onAddressNormalized(AutofillProfile profile) { public void onAddressNormalized(AutofillProfile profile) {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl == null) return;
if (client == null) return;
ChromeActivity chromeActivity = ChromeActivity.fromWebContents(mWebContents); ChromeActivity chromeActivity = ChromeActivity.fromWebContents(mWebContents);
// Can happen if the tab is closed during the normalization process. // Can happen if the tab is closed during the normalization process.
...@@ -2234,7 +2207,7 @@ public class PaymentRequestImpl ...@@ -2234,7 +2207,7 @@ public class PaymentRequestImpl
redactShippingAddress(redactedAddress); redactShippingAddress(redactedAddress);
// This updates the line items and the shipping options asynchronously. // This updates the line items and the shipping options asynchronously.
client.onShippingAddressChange(redactedAddress); mComponentPaymentRequestImpl.onShippingAddressChange(redactedAddress);
} }
@Override @Override
...@@ -2265,13 +2238,12 @@ public class PaymentRequestImpl ...@@ -2265,13 +2238,12 @@ public class PaymentRequestImpl
if (mPaymentUIsManager.getPaymentRequestUI() != null) { if (mPaymentUIsManager.getPaymentRequestUI() != null) {
mPaymentUIsManager.getPaymentRequestUI().close(); mPaymentUIsManager.getPaymentRequestUI().close();
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl != null) {
if (client != null) {
if (ComponentPaymentRequestImpl.getObserverForTest() != null) { if (ComponentPaymentRequestImpl.getObserverForTest() != null) {
ComponentPaymentRequestImpl.getObserverForTest().onCompleteReplied(); ComponentPaymentRequestImpl.getObserverForTest().onCompleteReplied();
} }
client.onComplete(); mComponentPaymentRequestImpl.onComplete();
closeClient(); close();
} }
ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents); ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents);
if (activity != null) { if (activity != null) {
...@@ -2329,9 +2301,8 @@ public class PaymentRequestImpl ...@@ -2329,9 +2301,8 @@ public class PaymentRequestImpl
// Implement PaymentUIsManager.Delegate: // Implement PaymentUIsManager.Delegate:
@Override @Override
public void dispatchPayerDetailChangeEventIfNeeded(PayerDetail detail) { public void dispatchPayerDetailChangeEventIfNeeded(PayerDetail detail) {
PaymentRequestClient client = getClient(); if (mComponentPaymentRequestImpl == null || !mWasRetryCalled) return;
if (client == null || !mWasRetryCalled) return; mComponentPaymentRequestImpl.onPayerDetailChange(detail);
client.onPayerDetailChange(detail);
} }
/** /**
...@@ -2368,16 +2339,4 @@ public class PaymentRequestImpl ...@@ -2368,16 +2339,4 @@ public class PaymentRequestImpl
public static void setIsLocalCanMakePaymentQueryQuotaEnforcedForTest() { public static void setIsLocalCanMakePaymentQueryQuotaEnforcedForTest() {
sIsLocalCanMakePaymentQueryQuotaEnforcedForTest = true; sIsLocalCanMakePaymentQueryQuotaEnforcedForTest = true;
} }
@Nullable
private PaymentRequestClient getClient() {
return mComponentPaymentRequestImpl == null ? null
: mComponentPaymentRequestImpl.getClient();
}
// Pre-condition: the client is not null.
private void closeClient() {
assert getClient() != null;
mComponentPaymentRequestImpl.closeClient();
}
} }
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
package org.chromium.components.payments; package org.chromium.components.payments;
import org.chromium.mojo.system.MojoException;
import org.chromium.payments.mojom.PaymentDetails; import org.chromium.payments.mojom.PaymentDetails;
import org.chromium.payments.mojom.PaymentMethodData; import org.chromium.payments.mojom.PaymentMethodData;
import org.chromium.payments.mojom.PaymentOptions; import org.chromium.payments.mojom.PaymentOptions;
...@@ -68,18 +67,15 @@ public interface BrowserPaymentRequest { ...@@ -68,18 +67,15 @@ public interface BrowserPaymentRequest {
/** The browser part of the {@link PaymentRequest#canMakePayment} implementation. */ /** The browser part of the {@link PaymentRequest#canMakePayment} implementation. */
void canMakePayment(); void canMakePayment();
/** The browser part of the {@link PaymentRequest#close} implementation. */
void close();
/**
* The browser part of the {@link PaymentRequest#onConnectionError} implementation.
* @param e The detail of the error.
*/
void onConnectionError(MojoException e);
/** @return The JourneyLogger of PaymentRequestImpl. */ /** @return The JourneyLogger of PaymentRequestImpl. */
JourneyLogger getJourneyLogger(); JourneyLogger getJourneyLogger();
/** Delegate to the same method of PaymentRequestImpl. */ /** Delegate to the same method of PaymentRequestImpl. */
void disconnectFromClientWithDebugMessage(String debugMessage); void disconnectFromClientWithDebugMessage(String debugMessage);
/**
* Close this instance. The callers of this method should stop referencing this instance upon
* calling it. This method can be called within itself without causing infinite loop.
*/
void close();
} }
...@@ -11,12 +11,15 @@ import org.chromium.components.autofill.EditableOption; ...@@ -11,12 +11,15 @@ import org.chromium.components.autofill.EditableOption;
import org.chromium.content_public.browser.RenderFrameHost; import org.chromium.content_public.browser.RenderFrameHost;
import org.chromium.content_public.browser.WebContentsStatics; import org.chromium.content_public.browser.WebContentsStatics;
import org.chromium.mojo.system.MojoException; import org.chromium.mojo.system.MojoException;
import org.chromium.payments.mojom.PayerDetail;
import org.chromium.payments.mojom.PaymentAddress;
import org.chromium.payments.mojom.PaymentDetails; import org.chromium.payments.mojom.PaymentDetails;
import org.chromium.payments.mojom.PaymentItem; import org.chromium.payments.mojom.PaymentItem;
import org.chromium.payments.mojom.PaymentMethodData; import org.chromium.payments.mojom.PaymentMethodData;
import org.chromium.payments.mojom.PaymentOptions; import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentRequest; import org.chromium.payments.mojom.PaymentRequest;
import org.chromium.payments.mojom.PaymentRequestClient; import org.chromium.payments.mojom.PaymentRequestClient;
import org.chromium.payments.mojom.PaymentResponse;
import org.chromium.payments.mojom.PaymentValidationErrors; import org.chromium.payments.mojom.PaymentValidationErrors;
import java.util.List; import java.util.List;
...@@ -33,10 +36,12 @@ public class ComponentPaymentRequestImpl implements PaymentRequest { ...@@ -33,10 +36,12 @@ public class ComponentPaymentRequestImpl implements PaymentRequest {
private final BrowserPaymentRequestFactory mBrowserPaymentRequestFactory; private final BrowserPaymentRequestFactory mBrowserPaymentRequestFactory;
private final RenderFrameHost mRenderFrameHost; private final RenderFrameHost mRenderFrameHost;
private boolean mSkipUiForNonUrlPaymentMethodIdentifiers; private boolean mSkipUiForNonUrlPaymentMethodIdentifiers;
private BrowserPaymentRequest mBrowserPaymentRequest;
private PaymentRequestClient mClient; private PaymentRequestClient mClient;
private boolean mIsOffTheRecord; private boolean mIsOffTheRecord;
private PaymentRequestLifecycleObserver mPaymentRequestLifecycleObserver; private PaymentRequestLifecycleObserver mPaymentRequestLifecycleObserver;
private boolean mHasTorndown;
@Nullable
private BrowserPaymentRequest mBrowserPaymentRequest;
/** The factory that creates an instance of {@link BrowserPaymentRequest}. */ /** The factory that creates an instance of {@link BrowserPaymentRequest}. */
public interface BrowserPaymentRequestFactory { public interface BrowserPaymentRequestFactory {
...@@ -216,82 +221,108 @@ public class ComponentPaymentRequestImpl implements PaymentRequest { ...@@ -216,82 +221,108 @@ public class ComponentPaymentRequestImpl implements PaymentRequest {
@Override @Override
public void show(boolean isUserGesture, boolean waitForUpdatedDetails) { public void show(boolean isUserGesture, boolean waitForUpdatedDetails) {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.show(isUserGesture, waitForUpdatedDetails); mBrowserPaymentRequest.show(isUserGesture, waitForUpdatedDetails);
} }
@Override @Override
public void updateWith(PaymentDetails details) { public void updateWith(PaymentDetails details) {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.updateWith(details); mBrowserPaymentRequest.updateWith(details);
} }
@Override @Override
public void onPaymentDetailsNotUpdated() { public void onPaymentDetailsNotUpdated() {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.onPaymentDetailsNotUpdated(); mBrowserPaymentRequest.onPaymentDetailsNotUpdated();
} }
@Override @Override
public void abort() { public void abort() {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.abort(); mBrowserPaymentRequest.abort();
} }
@Override @Override
public void complete(int result) { public void complete(int result) {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.complete(result); mBrowserPaymentRequest.complete(result);
} }
@Override @Override
public void retry(PaymentValidationErrors errors) { public void retry(PaymentValidationErrors errors) {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.retry(errors); mBrowserPaymentRequest.retry(errors);
} }
@Override @Override
public void canMakePayment() { public void canMakePayment() {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.canMakePayment(); mBrowserPaymentRequest.canMakePayment();
} }
@Override @Override
public void hasEnrolledInstrument(boolean perMethodQuota) { public void hasEnrolledInstrument(boolean perMethodQuota) {
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.hasEnrolledInstrument(perMethodQuota); mBrowserPaymentRequest.hasEnrolledInstrument(perMethodQuota);
} }
// This should be called by the renderer only. The closing triggered by other classes should
// call {@link #teardown} instead.
@Override @Override
public void close() { public void close() {
mBrowserPaymentRequest.close(); if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.getJourneyLogger().setAborted(
org.chromium.components.payments.AbortReason.MOJO_RENDERER_CLOSING);
teardown();
if (sObserverForTest != null) {
sObserverForTest.onRendererClosedMojoConnection();
}
if (sNativeObserverForTest != null) {
sNativeObserverForTest.onConnectionTerminated();
}
} }
@Override @Override
public void onConnectionError(MojoException e) { public void onConnectionError(MojoException e) {
mBrowserPaymentRequest.onConnectionError(e); if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.getJourneyLogger().setAborted(
org.chromium.components.payments.AbortReason.MOJO_CONNECTION_ERROR);
teardown();
if (sNativeObserverForTest != null) {
sNativeObserverForTest.onConnectionTerminated();
}
} }
/** /**
* Get the PaymentRequestClient. To close the client, the caller should call {@link * Close this instance and release all of the retained resources. The external callers of this
* #closeClient()} instead of calling {@link PaymentRequest#close()} directly. * method should stop referencing this instance upon calling. This method can be called within
* @return The client. * itself without causing dead loops.
*/ */
@Nullable public void teardown() {
public PaymentRequestClient getClient() { if (mHasTorndown) return;
return mClient; mHasTorndown = true;
}
if (mBrowserPaymentRequest == null) return;
mBrowserPaymentRequest.close();
mBrowserPaymentRequest = null;
/**
* Close the PaymentRequestClient.
*/
public void closeClient() {
if (mClient != null) mClient.close(); if (mClient != null) mClient.close();
mClient = null; mClient = null;
} }
/** /**
* Register an observer for the PaymentRequest lifecycle. * Register an observer for the PaymentRequest lifecycle.
* @param paymentRequestLifecycleObserver The observer. * @param paymentRequestLifecycleObserver The observer, cannot be null.
*/ */
public void registerPaymentRequestLifecycleObserver( public void registerPaymentRequestLifecycleObserver(
PaymentRequestLifecycleObserver paymentRequestLifecycleObserver) { PaymentRequestLifecycleObserver paymentRequestLifecycleObserver) {
assert paymentRequestLifecycleObserver != null;
mPaymentRequestLifecycleObserver = paymentRequestLifecycleObserver; mPaymentRequestLifecycleObserver = paymentRequestLifecycleObserver;
} }
/** @return The observer for the PaymentRequest lifecycle. */ /** @return The observer for the PaymentRequest lifecycle, can be null. */
@Nullable
public PaymentRequestLifecycleObserver getPaymentRequestLifecycleObserver() { public PaymentRequestLifecycleObserver getPaymentRequestLifecycleObserver() {
return mPaymentRequestLifecycleObserver; return mPaymentRequestLifecycleObserver;
} }
...@@ -321,4 +352,92 @@ public class ComponentPaymentRequestImpl implements PaymentRequest { ...@@ -321,4 +352,92 @@ public class ComponentPaymentRequestImpl implements PaymentRequest {
public void setSkipUiForNonUrlPaymentMethodIdentifiersForTest() { public void setSkipUiForNonUrlPaymentMethodIdentifiersForTest() {
mSkipUiForNonUrlPaymentMethodIdentifiers = true; mSkipUiForNonUrlPaymentMethodIdentifiers = true;
} }
/** Invokes {@link PaymentRequest.onPaymentMethodChange}. */
public void onPaymentMethodChange(String methodName, String stringifiedDetails) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onPaymentMethodChange(methodName, stringifiedDetails);
}
/** Invokes {@link PaymentRequest.onShippingAddressChange}. */
public void onShippingAddressChange(PaymentAddress address) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onShippingAddressChange(address);
}
/** Invokes {@link PaymentRequest.onShippingOptionChange}. */
public void onShippingOptionChange(String shippingOptionId) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onShippingOptionChange(shippingOptionId);
}
/** Invokes {@link PaymentRequest.onPayerDetailChange}. */
public void onPayerDetailChange(PayerDetail detail) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onPayerDetailChange(detail);
}
/** Invokes {@link PaymentRequest.onPaymentResponse}. */
public void onPaymentResponse(PaymentResponse response) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onPaymentResponse(response);
}
/** Invokes {@link PaymentRequest.onError}. */
public void onError(int error, String errorMessage) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onError(error, errorMessage);
}
/** Invokes {@link PaymentRequest.onComplete}. */
public void onComplete() {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onComplete();
}
/** Invokes {@link PaymentRequest.onAbort}. */
public void onAbort(boolean abortedSuccessfully) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onAbort(abortedSuccessfully);
}
/** Invokes {@link PaymentRequest.onCanMakePayment}. */
public void onCanMakePayment(int result) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onCanMakePayment(result);
}
/** Invokes {@link PaymentRequest.onHasEnrolledInstrument}. */
public void onHasEnrolledInstrument(int result) {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.onHasEnrolledInstrument(result);
}
/** Invokes {@link PaymentRequest.warnNoFavicon}. */
public void warnNoFavicon() {
// Every caller should stop referencing this class once teardown() is called.
assert mClient != null;
mClient.warnNoFavicon();
}
} }
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