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; ...@@ -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.handler.PaymentHandlerCoordinator.PaymentHandlerWebContentsObserver;
import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator; import org.chromium.chrome.browser.payments.minimal.MinimalUICoordinator;
import org.chromium.chrome.browser.payments.ui.ContactDetailsSection; 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.PaymentInformation;
import org.chromium.chrome.browser.payments.ui.PaymentRequestUI; import org.chromium.chrome.browser.payments.ui.PaymentRequestUI;
import org.chromium.chrome.browser.payments.ui.PaymentRequestUI.SelectionResult; import org.chromium.chrome.browser.payments.ui.PaymentRequestUI.SelectionResult;
...@@ -54,7 +53,6 @@ import org.chromium.components.payments.BrowserPaymentRequest; ...@@ -54,7 +53,6 @@ import org.chromium.components.payments.BrowserPaymentRequest;
import org.chromium.components.payments.CanMakePaymentQuery; import org.chromium.components.payments.CanMakePaymentQuery;
import org.chromium.components.payments.CheckoutFunnelStep; import org.chromium.components.payments.CheckoutFunnelStep;
import org.chromium.components.payments.ComponentPaymentRequestImpl; import org.chromium.components.payments.ComponentPaymentRequestImpl;
import org.chromium.components.payments.CurrencyFormatter;
import org.chromium.components.payments.ErrorMessageUtil; import org.chromium.components.payments.ErrorMessageUtil;
import org.chromium.components.payments.ErrorStrings; import org.chromium.components.payments.ErrorStrings;
import org.chromium.components.payments.Event; import org.chromium.components.payments.Event;
...@@ -403,7 +401,13 @@ public class PaymentRequestImpl ...@@ -403,7 +401,13 @@ public class PaymentRequestImpl
mQueryForQuota.put("basic-card-payment-options", paymentMethodData); 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(), mSpec = new PaymentRequestSpec(mPaymentOptions, details, mMethodData.values(),
LocaleUtils.getDefaultLocaleString()); LocaleUtils.getDefaultLocaleString());
...@@ -932,7 +936,13 @@ public class PaymentRequestImpl ...@@ -932,7 +936,13 @@ public class PaymentRequestImpl
return; 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); mSpec.updateWith(details);
if (mInvokedPaymentApp != null && mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) { if (mInvokedPaymentApp != null && mInvokedPaymentApp.isWaitingForPaymentDetailsUpdate()) {
...@@ -971,7 +981,13 @@ public class PaymentRequestImpl ...@@ -971,7 +981,13 @@ public class PaymentRequestImpl
return; 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); mSpec.updateWith(details);
if (!TextUtils.isEmpty(details.error)) { if (!TextUtils.isEmpty(details.error)) {
...@@ -1056,12 +1072,8 @@ public class PaymentRequestImpl ...@@ -1056,12 +1072,8 @@ public class PaymentRequestImpl
* member variables. * member variables.
* @return True if the data is valid. False if the data is invalid. * @return True if the data is valid. False if the data is invalid.
*/ */
private boolean parseAndValidateDetailsOrDisconnectFromClient(PaymentDetails details) { private boolean parseAndValidateDetails(PaymentDetails details) {
if (!PaymentValidator.validatePaymentDetails(details)) { if (!PaymentValidator.validatePaymentDetails(details)) return false;
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return false;
}
if (details.total != null) { if (details.total != null) {
mRawTotal = details.total; mRawTotal = details.total;
...@@ -1073,26 +1085,7 @@ public class PaymentRequestImpl ...@@ -1073,26 +1085,7 @@ public class PaymentRequestImpl
: new ArrayList<>()); : 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)) { if (mSkipToGPayHelper != null && !mSkipToGPayHelper.setShippingOptionIfValid(details)) {
mJourneyLogger.setAborted(AbortReason.INVALID_DATA_FROM_RENDERER);
disconnectFromClientWithDebugMessage(ErrorStrings.INVALID_PAYMENT_DETAILS);
return false; return false;
} }
...@@ -1113,8 +1106,6 @@ public class PaymentRequestImpl ...@@ -1113,8 +1106,6 @@ public class PaymentRequestImpl
mRawShippingOptions = Collections.unmodifiableList(new ArrayList<>()); mRawShippingOptions = Collections.unmodifiableList(new ArrayList<>());
} }
mPaymentUIsManager.updateAppModifiedTotals();
assert mRawTotal != null; assert mRawTotal != null;
assert mRawLineItems != null; assert mRawLineItems != null;
......
...@@ -250,11 +250,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -250,11 +250,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mPaymentUisShowStateReconciler; return mPaymentUisShowStateReconciler;
} }
/** @return Get the AddressEditor of the PaymentRequest UI. */
public AddressEditor getAddressEditor() {
return mAddressEditor;
}
/** @return Get the CardEditor of the PaymentRequest UI. */ /** @return Get the CardEditor of the PaymentRequest UI. */
public CardEditor getCardEditor() { public CardEditor getCardEditor() {
return mCardEditor; return mCardEditor;
...@@ -283,11 +278,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -283,11 +278,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mShippingAddressesSection; return mShippingAddressesSection;
} }
/** Set the ShippingAddressesSection of the PaymentRequest UI. */
public void setShippingAddressesSection(SectionInformation shippingAddressesSection) {
mShippingAddressesSection = shippingAddressesSection;
}
/** Get the ContactSection of the PaymentRequest UI. */ /** Get the ContactSection of the PaymentRequest UI. */
public ContactDetailsSection getContactSection() { public ContactDetailsSection getContactSection() {
return mContactSection; return mContactSection;
...@@ -317,11 +307,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -317,11 +307,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mUiShoppingCart; 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. */ /** @return Get a map of currency code to CurrencyFormatter. */
public Map<String, CurrencyFormatter> getCurrencyFormatterMap() { public Map<String, CurrencyFormatter> getCurrencyFormatterMap() {
return mCurrencyFormatterMap; return mCurrencyFormatterMap;
...@@ -335,14 +320,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -335,14 +320,6 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mUiShippingOptions; 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 * Set the call back of PaymentInformation. This callback would be invoked when the payment
* information is retrieved. * information is retrieved.
...@@ -670,7 +647,7 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -670,7 +647,7 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
* *
* @param details The given payment details. * @param details The given payment details.
*/ */
public void loadCurrencyFormattersForPaymentDetails(PaymentDetails details) { private void loadCurrencyFormattersForPaymentDetails(PaymentDetails details) {
if (details.total != null) { if (details.total != null) {
getOrCreateCurrencyFormatter(details.total.amount); getOrCreateCurrencyFormatter(details.total.amount);
} }
...@@ -1251,6 +1228,31 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -1251,6 +1228,31 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return anAppCanProvideAllInfo; 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. * 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. * @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