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 ...@@ -254,8 +254,8 @@ public class PaymentRequestImpl
boolean googlePayBridgeActivated = googlePayBridgeEligible boolean googlePayBridgeActivated = googlePayBridgeEligible
&& SkipToGPayHelper.canActivateExperiment(mWebContents, rawMethodData); && SkipToGPayHelper.canActivateExperiment(mWebContents, rawMethodData);
Map<String, PaymentMethodData> methodData = getValidatedMethodData( Map<String, PaymentMethodData> methodData =
rawMethodData, googlePayBridgeActivated, mPaymentUIsManager.getCardEditor()); getValidatedMethodData(rawMethodData, googlePayBridgeActivated);
if (methodData == null) { if (methodData == null) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER); mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
...@@ -280,10 +280,8 @@ public class PaymentRequestImpl ...@@ -280,10 +280,8 @@ public class PaymentRequestImpl
mSpec = PaymentRequestSpec.createAndValidate( mSpec = PaymentRequestSpec.createAndValidate(
details, mPaymentOptions, methodData, LocaleUtils.getDefaultLocaleString()); details, mPaymentOptions, methodData, LocaleUtils.getDefaultLocaleString());
if (mSpec != null && parseAndValidateDetailsForSkipToGPayHelper(details)) { if (mSpec == null
mPaymentUIsManager.updateDetailsOnPaymentRequestUI( || !parseAndValidateDetailsForSkipToGPayHelper(mSpec.getPaymentDetails())) {
details, mSpec.getRawTotal(), mSpec.getRawLineItems());
} else {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER); mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS); disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return false; return false;
...@@ -295,6 +293,9 @@ public class PaymentRequestImpl ...@@ -295,6 +293,9 @@ public class PaymentRequestImpl
return false; return false;
} }
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems());
// The first time initializations and validation of all of the parameters of {@link // The first time initializations and validation of all of the parameters of {@link
// PaymentRequestParams} should be done before {@link // PaymentRequestParams} should be done before {@link
// PaymentRequestLifeCycleObserver#onPaymentRequestParamsInitiated}. // PaymentRequestLifeCycleObserver#onPaymentRequestParamsInitiated}.
...@@ -532,8 +533,7 @@ public class PaymentRequestImpl ...@@ -532,8 +533,7 @@ public class PaymentRequestImpl
} }
private static Map<String, PaymentMethodData> getValidatedMethodData( private static Map<String, PaymentMethodData> getValidatedMethodData(
PaymentMethodData[] methodData, boolean googlePayBridgeEligible, PaymentMethodData[] methodData, boolean googlePayBridgeEligible) {
CardEditor paymentMethodsCollector) {
// Payment methodData are required. // Payment methodData are required.
assert methodData != null; assert methodData != null;
if (methodData.length == 0) return null; if (methodData.length == 0) return null;
...@@ -554,8 +554,6 @@ public class PaymentRequestImpl ...@@ -554,8 +554,6 @@ public class PaymentRequestImpl
} }
} }
result.put(method, methodData[i]); result.put(method, methodData[i]);
paymentMethodsCollector.addAcceptedPaymentMethodIfRecognized(methodData[i]);
} }
return Collections.unmodifiableMap(result); return Collections.unmodifiableMap(result);
...@@ -755,16 +753,15 @@ public class PaymentRequestImpl ...@@ -755,16 +753,15 @@ public class PaymentRequestImpl
return; return;
} }
if (mSpec.parseAndValidateDetails(details) if (!mSpec.parseAndValidateDetails(details)
&& parseAndValidateDetailsForSkipToGPayHelper(details)) { || !parseAndValidateDetailsForSkipToGPayHelper(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
details, mSpec.getRawTotal(), mSpec.getRawLineItems());
} else {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER); mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS); disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return; return;
} }
mSpec.updateWith(details); mSpec.updateWith(details);
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems());
if (mInvokedPaymentApp != null && mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) { if (mInvokedPaymentApp != null && mInvokedPaymentApp.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
...@@ -804,16 +801,15 @@ public class PaymentRequestImpl ...@@ -804,16 +801,15 @@ public class PaymentRequestImpl
return; return;
} }
if (mSpec.parseAndValidateDetails(details) if (!mSpec.parseAndValidateDetails(details)
&& parseAndValidateDetailsForSkipToGPayHelper(details)) { || !parseAndValidateDetailsForSkipToGPayHelper(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
details, mSpec.getRawTotal(), mSpec.getRawLineItems());
} else {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER); mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS); disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return; return;
} }
mSpec.updateWith(details); mSpec.updateWith(details);
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems());
if (!TextUtils.isEmpty(details.error)) { if (!TextUtils.isEmpty(details.error)) {
mJourneyLogger.setNotShown(NotShownReason.OTHER); mJourneyLogger.setNotShown(NotShownReason.OTHER);
......
...@@ -73,6 +73,7 @@ import org.chromium.payments.mojom.PaymentCurrencyAmount; ...@@ -73,6 +73,7 @@ import org.chromium.payments.mojom.PaymentCurrencyAmount;
import org.chromium.payments.mojom.PaymentDetails; import org.chromium.payments.mojom.PaymentDetails;
import org.chromium.payments.mojom.PaymentDetailsModifier; import org.chromium.payments.mojom.PaymentDetailsModifier;
import org.chromium.payments.mojom.PaymentItem; import org.chromium.payments.mojom.PaymentItem;
import org.chromium.payments.mojom.PaymentMethodData;
import org.chromium.payments.mojom.PaymentOptions; import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentShippingOption; import org.chromium.payments.mojom.PaymentShippingOption;
import org.chromium.payments.mojom.PaymentValidationErrors; import org.chromium.payments.mojom.PaymentValidationErrors;
...@@ -559,6 +560,9 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -559,6 +560,9 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
// Implement PaymentRequestLifecycleObserver: // Implement PaymentRequestLifecycleObserver:
@Override @Override
public void onPaymentRequestParamsInitiated(PaymentRequestParams params) { 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. // Checks whether the merchant supports autofill cards before show is called.
mMerchantSupportsAutofillCards = mMerchantSupportsAutofillCards =
BasicCardUtils.merchantSupportsBasicCard(params.getMethodData()); BasicCardUtils.merchantSupportsBasicCard(params.getMethodData());
......
...@@ -41,9 +41,11 @@ public class PaymentRequestSpec { ...@@ -41,9 +41,11 @@ public class PaymentRequestSpec {
private List<PaymentItem> mRawLineItems; private List<PaymentItem> mRawLineItems;
private PaymentItem mRawTotal; private PaymentItem mRawTotal;
private long mNativePointer; 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 details The payment details, e.g., the total amount.
* @param options The payment options, e.g., whether shipping is requested. * @param options The payment options, e.g., whether shipping is requested.
* @param methodData The map of supported payment method identifiers and corresponding payment * @param methodData The map of supported payment method identifiers and corresponding payment
...@@ -53,21 +55,22 @@ public class PaymentRequestSpec { ...@@ -53,21 +55,22 @@ public class PaymentRequestSpec {
@Nullable @Nullable
public static PaymentRequestSpec createAndValidate(PaymentDetails details, public static PaymentRequestSpec createAndValidate(PaymentDetails details,
PaymentOptions options, Map<String, PaymentMethodData> methodData, String appLocale) { 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; if (!spec.parseAndValidateDetails(details)) return null;
spec.createNative(details, options, appLocale); spec.createNative(options, appLocale);
return spec; return spec;
} }
private PaymentRequestSpec(String id, Map<String, PaymentMethodData> methodData) { private PaymentRequestSpec(PaymentDetails details, Map<String, PaymentMethodData> methodData) {
mId = id; mPaymentDetails = details;
mId = mPaymentDetails.id;
mMethodData = methodData; mMethodData = methodData;
} }
private void createNative(PaymentDetails details, PaymentOptions options, String appLocale) { private void createNative(PaymentOptions options, String appLocale) {
assert mNativePointer == 0; assert mNativePointer == 0;
mNativePointer = mNativePointer =
PaymentRequestSpecJni.get().create(options.serialize(), details.serialize(), PaymentRequestSpecJni.get().create(options.serialize(), mPaymentDetails.serialize(),
MojoStructCollection.serialize(mMethodData.values()), appLocale); MojoStructCollection.serialize(mMethodData.values()), appLocale);
} }
...@@ -127,6 +130,11 @@ public class PaymentRequestSpec { ...@@ -127,6 +130,11 @@ public class PaymentRequestSpec {
return mRawTotal; 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 * 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 * status boolean. That status is true for valid data, false for invalid data. If the input is
...@@ -178,6 +186,7 @@ public class PaymentRequestSpec { ...@@ -178,6 +186,7 @@ public class PaymentRequestSpec {
* @param details The updated payment details, e.g., the updated total amount. * @param details The updated payment details, e.g., the updated total amount.
*/ */
public void updateWith(PaymentDetails details) { public void updateWith(PaymentDetails details) {
mPaymentDetails = details;
PaymentRequestSpecJni.get().updateWith(mNativePointer, details.serialize()); 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