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

[PRImpl] Bring UI logic together in PRImpl#initAndValidate()

Change:
* In PRImpl#initAndValidate(), refactors the mSpec parsing block to
unwind the UI logic and the business logic. This way,
mPaymentUIsManager.updateDetailsOnPaymentRequestUI() have a chance to
be combined into onPaymentRequestParamsInitiated().
* Do the same unwinding for other cases in the code

Bug: 1102522

Change-Id: I6939e934a554a2a252f1f0cb71208c245a738486
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2405617
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarSahel Sharify <sahel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807500}
parent 4d04c664
......@@ -254,8 +254,8 @@ public class PaymentRequestImpl
boolean googlePayBridgeActivated = googlePayBridgeEligible
&& SkipToGPayHelper.canActivateExperiment(mWebContents, rawMethodData);
Map<String, PaymentMethodData> methodData = getValidatedMethodData(
rawMethodData, googlePayBridgeActivated, mPaymentUIsManager.getCardEditor());
Map<String, PaymentMethodData> methodData =
getValidatedMethodData(rawMethodData, googlePayBridgeActivated);
if (methodData == null) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
......@@ -280,10 +280,8 @@ public class PaymentRequestImpl
mSpec = PaymentRequestSpec.createAndValidate(
details, mPaymentOptions, methodData, LocaleUtils.getDefaultLocaleString());
if (mSpec != null && parseAndValidateDetailsForSkipToGPayHelper(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
details, mSpec.getRawTotal(), mSpec.getRawLineItems());
} else {
if (mSpec == null
|| !parseAndValidateDetailsForSkipToGPayHelper(mSpec.getPaymentDetails())) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return false;
......@@ -295,6 +293,9 @@ public class PaymentRequestImpl
return false;
}
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems());
// The first time initializations and validation of all of the parameters of {@link
// PaymentRequestParams} should be done before {@link
// PaymentRequestLifeCycleObserver#onPaymentRequestParamsInitiated}.
......@@ -532,8 +533,7 @@ public class PaymentRequestImpl
}
private static Map<String, PaymentMethodData> getValidatedMethodData(
PaymentMethodData[] methodData, boolean googlePayBridgeEligible,
CardEditor paymentMethodsCollector) {
PaymentMethodData[] methodData, boolean googlePayBridgeEligible) {
// Payment methodData are required.
assert methodData != null;
if (methodData.length == 0) return null;
......@@ -554,8 +554,6 @@ public class PaymentRequestImpl
}
}
result.put(method, methodData[i]);
paymentMethodsCollector.addAcceptedPaymentMethodIfRecognized(methodData[i]);
}
return Collections.unmodifiableMap(result);
......@@ -755,16 +753,15 @@ public class PaymentRequestImpl
return;
}
if (mSpec.parseAndValidateDetails(details)
&& parseAndValidateDetailsForSkipToGPayHelper(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
details, mSpec.getRawTotal(), mSpec.getRawLineItems());
} else {
if (!mSpec.parseAndValidateDetails(details)
|| !parseAndValidateDetailsForSkipToGPayHelper(details)) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return;
}
mSpec.updateWith(details);
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems());
if (mInvokedPaymentApp != null && mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
// After a payment app has been invoked, all of the merchant's calls to update the price
......@@ -804,16 +801,15 @@ public class PaymentRequestImpl
return;
}
if (mSpec.parseAndValidateDetails(details)
&& parseAndValidateDetailsForSkipToGPayHelper(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
details, mSpec.getRawTotal(), mSpec.getRawLineItems());
} else {
if (!mSpec.parseAndValidateDetails(details)
|| !parseAndValidateDetailsForSkipToGPayHelper(details)) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return;
}
mSpec.updateWith(details);
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems());
if (!TextUtils.isEmpty(details.error)) {
mJourneyLogger.setNotShown(NotShownReason.OTHER);
......
......@@ -73,6 +73,7 @@ import org.chromium.payments.mojom.PaymentCurrencyAmount;
import org.chromium.payments.mojom.PaymentDetails;
import org.chromium.payments.mojom.PaymentDetailsModifier;
import org.chromium.payments.mojom.PaymentItem;
import org.chromium.payments.mojom.PaymentMethodData;
import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentShippingOption;
import org.chromium.payments.mojom.PaymentValidationErrors;
......@@ -559,6 +560,9 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
// Implement PaymentRequestLifecycleObserver:
@Override
public void onPaymentRequestParamsInitiated(PaymentRequestParams params) {
for (PaymentMethodData method : params.getMethodData().values()) {
mCardEditor.addAcceptedPaymentMethodIfRecognized(method);
}
// Checks whether the merchant supports autofill cards before show is called.
mMerchantSupportsAutofillCards =
BasicCardUtils.merchantSupportsBasicCard(params.getMethodData());
......
......@@ -41,9 +41,11 @@ public class PaymentRequestSpec {
private List<PaymentItem> mRawLineItems;
private PaymentItem mRawTotal;
private long mNativePointer;
private PaymentDetails mPaymentDetails;
/**
* Creates a valid instance of PaymentRequestSpec.
* Creates a valid instance of PaymentRequestSpec with payment parameters, and saves the
* parameters in the instance.
* @param details The payment details, e.g., the total amount.
* @param options The payment options, e.g., whether shipping is requested.
* @param methodData The map of supported payment method identifiers and corresponding payment
......@@ -53,21 +55,22 @@ public class PaymentRequestSpec {
@Nullable
public static PaymentRequestSpec createAndValidate(PaymentDetails details,
PaymentOptions options, Map<String, PaymentMethodData> methodData, String appLocale) {
PaymentRequestSpec spec = new PaymentRequestSpec(details.id, methodData);
PaymentRequestSpec spec = new PaymentRequestSpec(details, methodData);
if (!spec.parseAndValidateDetails(details)) return null;
spec.createNative(details, options, appLocale);
spec.createNative(options, appLocale);
return spec;
}
private PaymentRequestSpec(String id, Map<String, PaymentMethodData> methodData) {
mId = id;
private PaymentRequestSpec(PaymentDetails details, Map<String, PaymentMethodData> methodData) {
mPaymentDetails = details;
mId = mPaymentDetails.id;
mMethodData = methodData;
}
private void createNative(PaymentDetails details, PaymentOptions options, String appLocale) {
private void createNative(PaymentOptions options, String appLocale) {
assert mNativePointer == 0;
mNativePointer =
PaymentRequestSpecJni.get().create(options.serialize(), details.serialize(),
PaymentRequestSpecJni.get().create(options.serialize(), mPaymentDetails.serialize(),
MojoStructCollection.serialize(mMethodData.values()), appLocale);
}
......@@ -127,6 +130,11 @@ public class PaymentRequestSpec {
return mRawTotal;
}
/** @return The payment details specified in the payment request. */
public PaymentDetails getPaymentDetails() {
return mPaymentDetails;
}
/**
* Sets the total, display line items, and shipping options based on input and returns the
* status boolean. That status is true for valid data, false for invalid data. If the input is
......@@ -178,6 +186,7 @@ public class PaymentRequestSpec {
* @param details The updated payment details, e.g., the updated total amount.
*/
public void updateWith(PaymentDetails details) {
mPaymentDetails = details;
PaymentRequestSpecJni.get().updateWith(mNativePointer, details.serialize());
}
......
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