Commit 2f1a893e authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Chromium LUCI CQ

[WebLayer] Creates PaymentNotShownError to ease error handling

Changes:
* Creates PaymentNotShownError that can be used to return more
  error details. Going forwards, this is used to replace the error
  string return in multiple methods, so that the methods callers can
  handle errors more flexibly.
* Renames disconnectIfNoPaymentMethodsSupported,
  disconnectForStrictShow.

Behavioural changes:
* After the change, when disconnectIfNoPaymentMethodsSupported fails,
  sObserverForTest.onPaymentRequestServiceShowFailed() is invoked. This
  makes sense because the disconnect method is invoked only after
  show().

Bug: 1153353
Change-Id: I9ad81fb26b9ffd89aec06eb68072960955626ce5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2562750
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#832224}
parent 1699f871
......@@ -128,6 +128,7 @@ android_library("java") {
"java/src/org/chromium/components/payments/PaymentHandlerHost.java",
"java/src/org/chromium/components/payments/PaymentManifestDownloader.java",
"java/src/org/chromium/components/payments/PaymentManifestParser.java",
"java/src/org/chromium/components/payments/PaymentNotShownError.java",
"java/src/org/chromium/components/payments/PaymentOptionsUtils.java",
"java/src/org/chromium/components/payments/PaymentRequestParams.java",
"java/src/org/chromium/components/payments/PaymentRequestService.java",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.components.payments;
import org.chromium.payments.mojom.PaymentErrorReason;
/** The error of payment UIs not being shown. */
public class PaymentNotShownError {
private final int mNotShownReason;
private final String mErrorMessage;
private final int mReason;
/**
* Creates an instance with the error details.
* @param notShownReason The reason of not showing UI, defined in {@link NotShownReason}.
* @param errorMessage The error message for informing the web developer.
* @param paymentErrorReason The reason of the payment error, defined in {@link
* PaymentErrorReason}.
*/
/* package */ PaymentNotShownError(
int notShownReason, String errorMessage, int paymentErrorReason) {
assert notShownReason <= NotShownReason.MAX;
assert paymentErrorReason >= PaymentErrorReason.MIN_VALUE;
assert paymentErrorReason <= PaymentErrorReason.MAX_VALUE;
mNotShownReason = notShownReason;
mErrorMessage = errorMessage;
mReason = paymentErrorReason;
}
/** @return The reason of not showing UI, defined in {@link NotShownReason}. */
public int getNotShownReason() {
return mNotShownReason;
}
/** @return The error message for informing the web developer. */
public String getErrorMessage() {
return mErrorMessage;
}
/** @return The reason of the error, defined in {@link PaymentErrorReason}.*/
public int getPaymentErrorReason() {
return mReason;
}
}
......@@ -749,7 +749,11 @@ public class PaymentRequestService
mBrowserPaymentRequest.notifyPaymentUiOfPendingApps(mPendingApps);
mPendingApps.clear();
if (mIsShowCalled) {
if (disconnectIfNoPaymentMethodsSupported()) return;
PaymentNotShownError notShownError = ensureHasSupportedPaymentMethods();
if (notShownError != null) {
onShowFailed(notShownError);
return;
}
String error = mBrowserPaymentRequest.showAppSelector(mIsShowWaitingForUpdatedDetails,
mSpec.getRawTotal(), mSpec.getPaymentOptions(), mIsUserGestureShow);
if (error != null) {
......@@ -777,6 +781,11 @@ public class PaymentRequestService
onShowFailed(NotShownReason.OTHER, error, PaymentErrorReason.USER_CANCEL);
}
private void onShowFailed(PaymentNotShownError error) {
onShowFailed(
error.getNotShownReason(), error.getErrorMessage(), error.getPaymentErrorReason());
}
// notShowReason is defined in NotShownReason.
// paymentErrorReason is defined in PaymentErrorReason.
private void onShowFailed(int notShowReason, String error, int paymentErrorReason) {
......@@ -786,10 +795,11 @@ public class PaymentRequestService
}
/**
* If no payment methods are supported, disconnect from the client and return true.
* @return Whether client has been disconnected.
* Ensures the available payment apps can make payment.
* @return The error if the payment cannot be made; null otherwise.
*/
private boolean disconnectIfNoPaymentMethodsSupported() {
@Nullable
private PaymentNotShownError ensureHasSupportedPaymentMethods() {
assert mIsShowCalled;
assert mIsFinishedQueryingPaymentApps;
if (!mCanMakePayment || !mBrowserPaymentRequest.hasAvailableApps()) {
......@@ -821,31 +831,32 @@ public class PaymentRequestService
: " " + mRejectShowErrorMessage);
paymentErrorReason = PaymentErrorReason.NOT_SUPPORTED;
}
onShowFailed(notShowReason, debugMessage, paymentErrorReason);
return true;
return new PaymentNotShownError(notShowReason, debugMessage, paymentErrorReason);
}
return disconnectForStrictShow(mIsUserGestureShow);
return ensureHasSupportedPaymentMethodsForStrictShow(mIsUserGestureShow);
}
/**
* If strict show() conditions are not satisfied, disconnect from client and return true.
* Ensures the available payment apps can make payment under the strict show() conditions.
* @param isUserGestureShow Whether the PaymentRequest.show() is triggered by user gesture.
* @return Whether client has been disconnected.
* @return The error if the payment cannot be made; null otherwise.
*/
private boolean disconnectForStrictShow(boolean isUserGestureShow) {
@Nullable
private PaymentNotShownError ensureHasSupportedPaymentMethodsForStrictShow(
boolean isUserGestureShow) {
if (!isUserGestureShow || !mSpec.getMethodData().containsKey(MethodStrings.BASIC_CARD)
|| mHasEnrolledInstrument || mHasNonAutofillApp
|| !PaymentFeatureList.isEnabledOrExperimentalFeaturesEnabled(
PaymentFeatureList.STRICT_HAS_ENROLLED_AUTOFILL_INSTRUMENT)) {
return false;
return null;
}
mRejectShowErrorMessage = ErrorStrings.STRICT_BASIC_CARD_SHOW_REJECT;
String debugMessage =
ErrorMessageUtil.getNotSupportedErrorMessage(mSpec.getMethodData().keySet()) + " "
+ mRejectShowErrorMessage;
onShowFailed(NotShownReason.OTHER, debugMessage, PaymentErrorReason.NOT_SUPPORTED);
return true;
return new PaymentNotShownError(
NotShownReason.OTHER, debugMessage, PaymentErrorReason.NOT_SUPPORTED);
}
private boolean isInTwa() {
......@@ -1024,7 +1035,11 @@ public class PaymentRequestService
mJourneyLogger.setTriggerTime();
if (mIsFinishedQueryingPaymentApps) {
if (disconnectIfNoPaymentMethodsSupported()) return;
PaymentNotShownError notShownError = ensureHasSupportedPaymentMethods();
if (notShownError != null) {
onShowFailed(notShownError);
return;
}
String error = mBrowserPaymentRequest.showAppSelector(mIsShowWaitingForUpdatedDetails,
mSpec.getRawTotal(), mSpec.getPaymentOptions(), mIsUserGestureShow);
if (error != 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