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

[PRImpl] Separate parseAndValidateDetailsOrDisconnectFromClient()

Change:
* break down parseAndValidateDetailsOrDisconnectFromClient into
business logic (parsing details) and UI logic (updating UI with
details).
* move the client-disconnection logic out of
parseAndValidateDetailsOrDisconnectFromClient.

Bug: 1102522

Change-Id: I3636884ba883d38eb75e3584b3205b2c13d26abd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2382406
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803137}
parent 5b35b285
......@@ -30,7 +30,6 @@ import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator.PaymentHandlerWebContentsObserver;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator;
import org.chromium.chrome.browser.payments.ui.ContactDetailsSection;
import org.chromium.chrome.browser.payments.ui.LineItem;
import org.chromium.chrome.browser.payments.ui.PaymentInformation;
import org.chromium.chrome.browser.payments.ui.PaymentRequestUI;
import org.chromium.chrome.browser.payments.ui.PaymentRequestUI.SelectionResult;
......@@ -54,7 +53,6 @@ import org.chromium.components.payments.BrowserPaymentRequest;
import org.chromium.components.payments.CanMakePaymentQuery;
import org.chromium.components.payments.CheckoutFunnelStep;
import org.chromium.components.payments.ComponentPaymentRequestImpl;
import org.chromium.components.payments.CurrencyFormatter;
import org.chromium.components.payments.ErrorMessageUtil;
import org.chromium.components.payments.ErrorStrings;
import org.chromium.components.payments.Event;
......@@ -403,7 +401,13 @@ public class PaymentRequestImpl
mQueryForQuota.put("basic-card-payment-options", paymentMethodData);
}
if (!parseAndValidateDetailsOrDisconnectFromClient(details)) return false;
if (parseAndValidateDetails(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(details, mRawTotal, mRawLineItems);
} else {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return false;
}
mSpec = new PaymentRequestSpec(mPaymentOptions, details, mMethodData.values(),
LocaleUtils.getDefaultLocaleString());
......@@ -932,7 +936,13 @@ public class PaymentRequestImpl
return;
}
if (!parseAndValidateDetailsOrDisconnectFromClient(details)) return;
if (parseAndValidateDetails(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(details, mRawTotal, mRawLineItems);
} else {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return;
}
mSpec.updateWith(details);
if (mInvokedPaymentApp != null && mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
......@@ -971,7 +981,13 @@ public class PaymentRequestImpl
return;
}
if (!parseAndValidateDetailsOrDisconnectFromClient(details)) return;
if (parseAndValidateDetails(details)) {
mPaymentUIsManager.updateDetailsOnPaymentRequestUI(details, mRawTotal, mRawLineItems);
} else {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return;
}
mSpec.updateWith(details);
if (!TextUtils.isEmpty(details.error)) {
......@@ -1056,12 +1072,8 @@ public class PaymentRequestImpl
* member variables.
* @return True if the data is valid. False if the data is invalid.
*/
private boolean parseAndValidateDetailsOrDisconnectFromClient(PaymentDetails details) {
if (!PaymentValidator.validatePaymentDetails(details)) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return false;
}
private boolean parseAndValidateDetails(PaymentDetails details) {
if (!PaymentValidator.validatePaymentDetails(details)) return false;
if (details.total != null) {
mRawTotal = details.total;
......@@ -1073,26 +1085,7 @@ public class PaymentRequestImpl
: new ArrayList<>());
}
mPaymentUIsManager.loadCurrencyFormattersForPaymentDetails(details);
// Total is never pending.
CurrencyFormatter formatter =
mPaymentUIsManager.getOrCreateCurrencyFormatter(mRawTotal.amount);
LineItem uiTotal = new LineItem(mRawTotal.label, formatter.getFormattedCurrencyCode(),
formatter.format(mRawTotal.amount.value), /* isPending */ false);
List<LineItem> uiLineItems = mPaymentUIsManager.getLineItems(mRawLineItems);
mPaymentUIsManager.setUiShoppingCart(new ShoppingCart(uiTotal, uiLineItems));
if (mPaymentUIsManager.getUiShippingOptions() == null || details.shippingOptions != null) {
mPaymentUIsManager.setUiShippingOptions(
mPaymentUIsManager.getShippingOptions(details.shippingOptions));
}
if (mSkipToGPayHelper != null && !mSkipToGPayHelper.setShippingOptionIfValid(details)) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return false;
}
......@@ -1113,8 +1106,6 @@ public class PaymentRequestImpl
mRawShippingOptions = Collections.unmodifiableList(new ArrayList<>());
}
mPaymentUIsManager.updateAppModifiedTotals();
assert mRawTotal != null;
assert mRawLineItems != null;
......
......@@ -250,11 +250,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mPaymentUisShowStateReconciler;
}
/** @return Get the AddressEditor of the PaymentRequest UI. */
public AddressEditor getAddressEditor() {
return mAddressEditor;
}
/** @return Get the CardEditor of the PaymentRequest UI. */
public CardEditor getCardEditor() {
return mCardEditor;
......@@ -283,11 +278,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mShippingAddressesSection;
}
/** Set the ShippingAddressesSection of the PaymentRequest UI. */
public void setShippingAddressesSection(SectionInformation shippingAddressesSection) {
mShippingAddressesSection = shippingAddressesSection;
}
/** Get the ContactSection of the PaymentRequest UI. */
public ContactDetailsSection getContactSection() {
return mContactSection;
......@@ -317,11 +307,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mUiShoppingCart;
}
/** Set the shopping cart on the PaymentRequest UI. */
public void setUiShoppingCart(ShoppingCart uiShoppingCart) {
mUiShoppingCart = uiShoppingCart;
}
/** @return Get a map of currency code to CurrencyFormatter. */
public Map<String, CurrencyFormatter> getCurrencyFormatterMap() {
return mCurrencyFormatterMap;
......@@ -335,14 +320,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mUiShippingOptions;
}
/**
* Set the shipping options for the Payment Request UI.
* @param uiShippingOptions A shipping options to be displayed on the Payment Request UI.
*/
public void setUiShippingOptions(SectionInformation uiShippingOptions) {
mUiShippingOptions = uiShippingOptions;
}
/**
* Set the call back of PaymentInformation. This callback would be invoked when the payment
* information is retrieved.
......@@ -670,7 +647,7 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
*
* @param details The given payment details.
*/
public void loadCurrencyFormattersForPaymentDetails(PaymentDetails details) {
private void loadCurrencyFormattersForPaymentDetails(PaymentDetails details) {
if (details.total != null) {
getOrCreateCurrencyFormatter(details.total.amount);
}
......@@ -1251,6 +1228,31 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return anAppCanProvideAllInfo;
}
/**
* Update the details related fields on the PaymentRequest UI.
* @param details The details whose information is used for the update.
* @param rawTotal The raw total parsed from the details to be used for the update.
* @param rawLineItems The raw line items parsed from the details to be used for the update.
*/
public void updateDetailsOnPaymentRequestUI(
PaymentDetails details, PaymentItem rawTotal, List<PaymentItem> rawLineItems) {
loadCurrencyFormattersForPaymentDetails(details);
// Total is never pending.
CurrencyFormatter formatter = getOrCreateCurrencyFormatter(rawTotal.amount);
LineItem uiTotal = new LineItem(rawTotal.label, formatter.getFormattedCurrencyCode(),
formatter.format(rawTotal.amount.value), /*isPending=*/false);
List<LineItem> uiLineItems = getLineItems(rawLineItems);
mUiShoppingCart = new ShoppingCart(uiTotal, uiLineItems);
if (mUiShippingOptions == null || details.shippingOptions != null) {
mUiShippingOptions = getShippingOptions(details.shippingOptions);
}
updateAppModifiedTotals();
}
/**
* Calculate whether the browser payment sheet should be skipped directly into the payment app.
* @param isUserGestureShow Whether the PaymentRequest.show() is triggered by user gesture.
......
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