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

[WebLayer] Refactor two responders

This CL moved respondCanMakePaymentQuery() and
respondHasEnrolledInstrumentQuery() into PaymentRequestService.

Bug: 1144527

Change-Id: Idd33310232c6ca46865bd4ff6e0c4100b29f4fc8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2515133Reviewed-by: default avatarLiquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824067}
parent 4bb0db09
...@@ -24,7 +24,6 @@ import org.chromium.components.autofill.EditableOption; ...@@ -24,7 +24,6 @@ import org.chromium.components.autofill.EditableOption;
import org.chromium.components.embedder_support.util.UrlConstants; import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.payments.AbortReason; import org.chromium.components.payments.AbortReason;
import org.chromium.components.payments.BrowserPaymentRequest; import org.chromium.components.payments.BrowserPaymentRequest;
import org.chromium.components.payments.CanMakePaymentQuery;
import org.chromium.components.payments.CheckoutFunnelStep; import org.chromium.components.payments.CheckoutFunnelStep;
import org.chromium.components.payments.ErrorMessageUtil; import org.chromium.components.payments.ErrorMessageUtil;
import org.chromium.components.payments.ErrorStrings; import org.chromium.components.payments.ErrorStrings;
...@@ -56,8 +55,6 @@ import org.chromium.components.payments.SkipToGPayHelper; ...@@ -56,8 +55,6 @@ import org.chromium.components.payments.SkipToGPayHelper;
import org.chromium.components.payments.UrlUtil; import org.chromium.components.payments.UrlUtil;
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.payments.mojom.CanMakePaymentQueryResult;
import org.chromium.payments.mojom.HasEnrolledInstrumentQueryResult;
import org.chromium.payments.mojom.PayerDetail; import org.chromium.payments.mojom.PayerDetail;
import org.chromium.payments.mojom.PaymentAddress; import org.chromium.payments.mojom.PaymentAddress;
import org.chromium.payments.mojom.PaymentComplete; import org.chromium.payments.mojom.PaymentComplete;
...@@ -131,7 +128,6 @@ public class ChromePaymentRequestService ...@@ -131,7 +128,6 @@ public class ChromePaymentRequestService
private final boolean mRequestPayerEmail; private final boolean mRequestPayerEmail;
private final int mShippingType; private final int mShippingType;
private boolean mIsCanMakePaymentResponsePending;
private boolean mIsHasEnrolledInstrumentResponsePending; private boolean mIsHasEnrolledInstrumentResponsePending;
private boolean mIsCurrentPaymentRequestShowing; private boolean mIsCurrentPaymentRequestShowing;
private boolean mWasRetryCalled; private boolean mWasRetryCalled;
...@@ -168,9 +164,6 @@ public class ChromePaymentRequestService ...@@ -168,9 +164,6 @@ public class ChromePaymentRequestService
*/ */
private boolean mDidRecordShowEvent; private boolean mDidRecordShowEvent;
/** True if any of the requested payment methods are supported. */
private boolean mCanMakePayment;
/** Whether PaymentRequest.show() was invoked with a user gesture. */ /** Whether PaymentRequest.show() was invoked with a user gesture. */
private boolean mIsUserGestureShow; private boolean mIsUserGestureShow;
...@@ -1123,30 +1116,9 @@ public class ChromePaymentRequestService ...@@ -1123,30 +1116,9 @@ public class ChromePaymentRequestService
} }
if (mIsFinishedQueryingPaymentApps) { if (mIsFinishedQueryingPaymentApps) {
respondCanMakePaymentQuery(); mPaymentRequestService.respondCanMakePaymentQuery();
} else { } else {
mIsCanMakePaymentResponsePending = true; mPaymentRequestService.setCanMakePaymentResponsePending(true);
}
}
private void respondCanMakePaymentQuery() {
if (mPaymentRequestService == null) return;
mIsCanMakePaymentResponsePending = false;
boolean response = mCanMakePayment && mDelegate.prefsCanMakePayment();
mPaymentRequestService.onCanMakePayment(response
? CanMakePaymentQueryResult.CAN_MAKE_PAYMENT
: CanMakePaymentQueryResult.CANNOT_MAKE_PAYMENT);
mJourneyLogger.setCanMakePaymentValue(response || mPaymentRequestService.isOffTheRecord());
if (PaymentRequestService.getObserverForTest() != null) {
PaymentRequestService.getObserverForTest()
.onPaymentRequestServiceCanMakePaymentQueryResponded();
}
if (PaymentRequestService.getNativeObserverForTest() != null) {
PaymentRequestService.getNativeObserverForTest().onCanMakePaymentReturned();
} }
} }
...@@ -1161,39 +1133,9 @@ public class ChromePaymentRequestService ...@@ -1161,39 +1133,9 @@ public class ChromePaymentRequestService
} }
if (mIsFinishedQueryingPaymentApps) { if (mIsFinishedQueryingPaymentApps) {
respondHasEnrolledInstrumentQuery(mPaymentRequestService.getHasEnrolledInstrument()); mPaymentRequestService.respondHasEnrolledInstrumentQuery();
} else { } else {
mIsHasEnrolledInstrumentResponsePending = true; mPaymentRequestService.setIsHasEnrolledInstrumentResponsePending(true);
}
}
private void respondHasEnrolledInstrumentQuery(boolean response) {
if (mPaymentRequestService == null) return;
mIsHasEnrolledInstrumentResponsePending = false;
if (CanMakePaymentQuery.canQuery(mWebContents, mTopLevelOrigin, mPaymentRequestOrigin,
mPaymentRequestService.getQueryForQuota())) {
mPaymentRequestService.onHasEnrolledInstrument(response
? HasEnrolledInstrumentQueryResult.HAS_ENROLLED_INSTRUMENT
: HasEnrolledInstrumentQueryResult.HAS_NO_ENROLLED_INSTRUMENT);
} else if (shouldEnforceCanMakePaymentQueryQuota()) {
mPaymentRequestService.onHasEnrolledInstrument(
HasEnrolledInstrumentQueryResult.QUERY_QUOTA_EXCEEDED);
} else {
mPaymentRequestService.onHasEnrolledInstrument(response
? HasEnrolledInstrumentQueryResult.WARNING_HAS_ENROLLED_INSTRUMENT
: HasEnrolledInstrumentQueryResult.WARNING_HAS_NO_ENROLLED_INSTRUMENT);
}
mJourneyLogger.setHasEnrolledInstrumentValue(response || mIsOffTheRecord);
if (PaymentRequestService.getObserverForTest() != null) {
PaymentRequestService.getObserverForTest()
.onPaymentRequestServiceHasEnrolledInstrumentQueryResponded();
}
if (PaymentRequestService.getNativeObserverForTest() != null) {
PaymentRequestService.getNativeObserverForTest().onHasEnrolledInstrumentReturned();
} }
} }
...@@ -1202,7 +1144,8 @@ public class ChromePaymentRequestService ...@@ -1202,7 +1144,8 @@ public class ChromePaymentRequestService
* enforced only on https:// scheme origins. However, the tests also enable the quota on * enforced only on https:// scheme origins. However, the tests also enable the quota on
* localhost and file:// scheme origins to verify its behavior. * localhost and file:// scheme origins to verify its behavior.
*/ */
private boolean shouldEnforceCanMakePaymentQueryQuota() { @Override
public boolean shouldEnforceCanMakePaymentQueryQuota() {
// If |mWebContents| is destroyed, don't bother checking the localhost or file:// scheme // If |mWebContents| is destroyed, don't bother checking the localhost or file:// scheme
// exemption. It doesn't really matter anyways. // exemption. It doesn't really matter anyways.
return mWebContents.isDestroyed() return mWebContents.isDestroyed()
...@@ -1343,13 +1286,13 @@ public class ChromePaymentRequestService ...@@ -1343,13 +1286,13 @@ public class ChromePaymentRequestService
public void onCanMakePaymentCalculated(boolean canMakePayment) { public void onCanMakePaymentCalculated(boolean canMakePayment) {
if (mPaymentRequestService == null) return; if (mPaymentRequestService == null) return;
mCanMakePayment = canMakePayment; mPaymentRequestService.setCanMakePayment(canMakePayment);
if (!mIsCanMakePaymentResponsePending) return; if (!mPaymentRequestService.isCanMakePaymentResponsePending()) return;
// canMakePayment doesn't need to wait for all apps to be queried because it only needs to // canMakePayment doesn't need to wait for all apps to be queried because it only needs to
// test the existence of a payment handler. // test the existence of a payment handler.
respondCanMakePaymentQuery(); mPaymentRequestService.respondCanMakePaymentQuery();
} }
// PaymentAppFactoryDelegate implementation. // PaymentAppFactoryDelegate implementation.
...@@ -1383,12 +1326,12 @@ public class ChromePaymentRequestService ...@@ -1383,12 +1326,12 @@ public class ChromePaymentRequestService
mPaymentRequestService.getHasEnrolledInstrument() mPaymentRequestService.getHasEnrolledInstrument()
&& mDelegate.prefsCanMakePayment()); && mDelegate.prefsCanMakePayment());
if (mIsCanMakePaymentResponsePending) { if (mPaymentRequestService.isCanMakePaymentResponsePending()) {
respondCanMakePaymentQuery(); mPaymentRequestService.respondCanMakePaymentQuery();
} }
if (mIsHasEnrolledInstrumentResponsePending) { if (mPaymentRequestService.isHasEnrolledInstrumentResponsePending()) {
respondHasEnrolledInstrumentQuery(mPaymentRequestService.getHasEnrolledInstrument()); mPaymentRequestService.respondHasEnrolledInstrumentQuery();
} }
notifyPaymentUiOfPendingApps(mPendingApps); notifyPaymentUiOfPendingApps(mPendingApps);
...@@ -1468,12 +1411,14 @@ public class ChromePaymentRequestService ...@@ -1468,12 +1411,14 @@ public class ChromePaymentRequestService
* @return Whether client has been disconnected. * @return Whether client has been disconnected.
*/ */
private boolean disconnectIfNoPaymentMethodsSupported(boolean hasAvailableApps) { private boolean disconnectIfNoPaymentMethodsSupported(boolean hasAvailableApps) {
assert mPaymentRequestService != null;
if (!mIsFinishedQueryingPaymentApps || !mIsCurrentPaymentRequestShowing) return false; if (!mIsFinishedQueryingPaymentApps || !mIsCurrentPaymentRequestShowing) return false;
if (!mCanMakePayment || (mPendingApps.isEmpty() && !hasAvailableApps)) { if (!mPaymentRequestService.getCanMakePayment()
|| (mPendingApps.isEmpty() && !hasAvailableApps)) {
// All factories have responded, but none of them have apps. It's possible to add credit // All factories have responded, but none of them have apps. It's possible to add credit
// cards, but the merchant does not support them either. The payment request must be // cards, but the merchant does not support them either. The payment request must be
// rejected. // rejected.
mJourneyLogger.setNotShown(mCanMakePayment mJourneyLogger.setNotShown(mPaymentRequestService.getCanMakePayment()
? NotShownReason.NO_MATCHING_PAYMENT_METHOD ? NotShownReason.NO_MATCHING_PAYMENT_METHOD
: NotShownReason.NO_SUPPORTED_PAYMENT_METHOD); : NotShownReason.NO_SUPPORTED_PAYMENT_METHOD);
if (mDelegate.isOffTheRecord()) { if (mDelegate.isOffTheRecord()) {
......
...@@ -70,6 +70,11 @@ public interface BrowserPaymentRequest { ...@@ -70,6 +70,11 @@ public interface BrowserPaymentRequest {
*/ */
void hasEnrolledInstrument(); void hasEnrolledInstrument();
/** @return Whether CanMakePayment query quota should be enfored. */
default boolean shouldEnforceCanMakePaymentQueryQuota() {
return false;
}
/** The browser part of the {@link PaymentRequest#canMakePayment} implementation. */ /** The browser part of the {@link PaymentRequest#canMakePayment} implementation. */
void canMakePayment(); void canMakePayment();
......
...@@ -21,6 +21,8 @@ import org.chromium.content_public.browser.RenderFrameHost; ...@@ -21,6 +21,8 @@ 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.mojo.system.MojoException;
import org.chromium.payments.mojom.CanMakePaymentQueryResult;
import org.chromium.payments.mojom.HasEnrolledInstrumentQueryResult;
import org.chromium.payments.mojom.PayerDetail; import org.chromium.payments.mojom.PayerDetail;
import org.chromium.payments.mojom.PaymentAddress; import org.chromium.payments.mojom.PaymentAddress;
import org.chromium.payments.mojom.PaymentDetails; import org.chromium.payments.mojom.PaymentDetails;
...@@ -102,12 +104,17 @@ public class PaymentRequestService { ...@@ -102,12 +104,17 @@ public class PaymentRequestService {
* after all payment apps have been queried. * after all payment apps have been queried.
*/ */
private boolean mHasEnrolledInstrument; private boolean mHasEnrolledInstrument;
/** True if any of the requested payment methods are supported. */
private boolean mCanMakePayment;
/** /**
* Whether there's at least one app that is not an autofill card. Should be read only after all * Whether there's at least one app that is not an autofill card. Should be read only after all
* payment apps have been queried. * payment apps have been queried.
*/ */
private boolean mHasNonAutofillApp; private boolean mHasNonAutofillApp;
private boolean mIsCanMakePaymentResponsePending;
private boolean mIsHasEnrolledInstrumentResponsePending;
/** /**
* 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.
* This interface holds events that should be passed back to the native C++ test * This interface holds events that should be passed back to the native C++ test
...@@ -527,6 +534,75 @@ public class PaymentRequestService { ...@@ -527,6 +534,75 @@ public class PaymentRequestService {
pendingApps.add(paymentApp); pendingApps.add(paymentApp);
} }
/** @return Whether the response of CanMakePayment is pending. */
public boolean isCanMakePaymentResponsePending() {
return mIsCanMakePaymentResponsePending;
}
/** Sets pending for the response of CanMakePayment. */
public void setCanMakePaymentResponsePending(boolean isPending) {
mIsCanMakePaymentResponsePending = isPending;
}
/** @return Whether the response of HasEnrolledInstrument is pending. */
public boolean isHasEnrolledInstrumentResponsePending() {
return mIsHasEnrolledInstrumentResponsePending;
}
/** Sets pending for HasEnrolledInstrument. */
public void setIsHasEnrolledInstrumentResponsePending(boolean isPending) {
mIsHasEnrolledInstrumentResponsePending = isPending;
}
/** Responds to the CanMakePayment query from the merchant page. */
public void respondCanMakePaymentQuery() {
// Every caller should stop referencing this class once close() is called.
assert mClient != null;
mIsCanMakePaymentResponsePending = false;
boolean response = mCanMakePayment && mDelegate.prefsCanMakePayment();
mClient.onCanMakePayment(response ? CanMakePaymentQueryResult.CAN_MAKE_PAYMENT
: CanMakePaymentQueryResult.CANNOT_MAKE_PAYMENT);
mJourneyLogger.setCanMakePaymentValue(response || mIsOffTheRecord);
if (sObserverForTest != null) {
sObserverForTest.onPaymentRequestServiceCanMakePaymentQueryResponded();
}
if (sNativeObserverForTest != null) {
sNativeObserverForTest.onCanMakePaymentReturned();
}
}
/** Responds to the HasEnrolledInstrument query from the merchant page. */
public void respondHasEnrolledInstrumentQuery() {
boolean response = mHasEnrolledInstrument;
mIsHasEnrolledInstrumentResponsePending = false;
if (CanMakePaymentQuery.canQuery(
mWebContents, mTopLevelOrigin, mPaymentRequestOrigin, mQueryForQuota)) {
onHasEnrolledInstrument(response
? HasEnrolledInstrumentQueryResult.HAS_ENROLLED_INSTRUMENT
: HasEnrolledInstrumentQueryResult.HAS_NO_ENROLLED_INSTRUMENT);
} else if (mBrowserPaymentRequest.shouldEnforceCanMakePaymentQueryQuota()) {
onHasEnrolledInstrument(HasEnrolledInstrumentQueryResult.QUERY_QUOTA_EXCEEDED);
} else {
onHasEnrolledInstrument(response
? HasEnrolledInstrumentQueryResult.WARNING_HAS_ENROLLED_INSTRUMENT
: HasEnrolledInstrumentQueryResult.WARNING_HAS_NO_ENROLLED_INSTRUMENT);
}
mJourneyLogger.setHasEnrolledInstrumentValue(response || mIsOffTheRecord);
if (sObserverForTest != null) {
sObserverForTest.onPaymentRequestServiceHasEnrolledInstrumentQueryResponded();
}
if (sNativeObserverForTest != null) {
sNativeObserverForTest.onHasEnrolledInstrumentReturned();
}
}
/** @return Whether the instrument has been enrolled. */ /** @return Whether the instrument has been enrolled. */
public boolean getHasEnrolledInstrument() { public boolean getHasEnrolledInstrument() {
return mHasEnrolledInstrument; return mHasEnrolledInstrument;
...@@ -537,6 +613,20 @@ public class PaymentRequestService { ...@@ -537,6 +613,20 @@ public class PaymentRequestService {
mHasEnrolledInstrument = hasEnrolledInstrument; mHasEnrolledInstrument = hasEnrolledInstrument;
} }
/**
* Sets the result of CanMakePayment request.
* @param canMakePayment Whether the user can make a payment with the merchant specified
* request.
*/
public void setCanMakePayment(boolean canMakePayment) {
mCanMakePayment = canMakePayment;
}
/** @return The result of the CanMakePayment request. */
public boolean getCanMakePayment() {
return mCanMakePayment;
}
/** @return Whether the created payment apps includes any autofill payment app. */ /** @return Whether the created payment apps includes any autofill payment app. */
public boolean getHasNonAutofillApp() { public boolean getHasNonAutofillApp() {
return mHasNonAutofillApp; return mHasNonAutofillApp;
......
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