Commit 207b12b0 authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Commit Bot

[WebLayer] Removes PaymentRequestLifecycleObserver

Context:
Before, we planned to remove ChromePaymentRequestService completely, so
we created PaymentRequestLifecycleObserver in order for
PaymentRequestService to call into PaymentUiService directly. Now that
we believe that ChromePaymentRequestService will keep existing as a
glue between the UI service and the PRService, the observer becomes
unnecessary.

Changes:
* Removed PaymentRequestLifecycleObserver as a middle man between
PaymentRequestService and PaymentUiService; changed to use
ChromePaymentRequestService as a middle man instead.
* Created PaymentUiService#close() to encapsulate the closing logic.

Bug: 1144527

Change-Id: Ib87234e6d2ed98d68586983da9779ba74a62abaf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2514481
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823375}
parent a63c03aa
...@@ -254,7 +254,6 @@ public class ChromePaymentRequestService ...@@ -254,7 +254,6 @@ public class ChromePaymentRequestService
mPaymentUiService = new PaymentUiService(/*delegate=*/this, mPaymentUiService = new PaymentUiService(/*delegate=*/this,
/*params=*/this, mWebContents, mIsOffTheRecord, mJourneyLogger, mTopLevelOrigin, /*params=*/this, mWebContents, mIsOffTheRecord, mJourneyLogger, mTopLevelOrigin,
/*observer=*/this); /*observer=*/this);
mPaymentRequestService.registerPaymentRequestLifecycleObserver(mPaymentUiService);
} }
// Implement BrowserPaymentRequest: // Implement BrowserPaymentRequest:
...@@ -307,15 +306,9 @@ public class ChromePaymentRequestService ...@@ -307,15 +306,9 @@ public class ChromePaymentRequestService
return false; return false;
} }
mPaymentUiService.updateDetailsOnPaymentRequestUI( mPaymentUiService.initialize(
mSpec.getPaymentDetails(), mSpec.getRawTotal(), mSpec.getRawLineItems()); 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}.
mPaymentRequestService.getPaymentRequestLifecycleObserver().onPaymentRequestParamsInitiated(
/*params=*/this);
PaymentAppService service = PaymentAppService.getInstance(); PaymentAppService service = PaymentAppService.getInstance();
addUniqueFactoriesToPaymentAppService(service); addUniqueFactoriesToPaymentAppService(service);
service.create(/*delegate=*/this); service.create(/*delegate=*/this);
...@@ -1181,7 +1174,7 @@ public class ChromePaymentRequestService ...@@ -1181,7 +1174,7 @@ public class ChromePaymentRequestService
mWasRetryCalled = true; mWasRetryCalled = true;
mPaymentRequestService.getPaymentRequestLifecycleObserver().onRetry(errors); mPaymentUiService.onRetry(errors);
} }
// Implement BrowserPaymentRequest: // Implement BrowserPaymentRequest:
...@@ -1722,39 +1715,14 @@ public class ChromePaymentRequestService ...@@ -1722,39 +1715,14 @@ public class ChromePaymentRequestService
* function is called. * function is called.
*/ */
private void closeUIAndDestroyNativeObjects() { private void closeUIAndDestroyNativeObjects() {
mPaymentUiService.ensureHideAndResetPaymentHandlerUi(); mPaymentUiService.close();
mPaymentUiService.hideMinimalUI(); SettingsAutofillAndPaymentsObserver.getInstance().unregisterObserver(mPaymentUiService);
if (mPaymentUiService.getPaymentRequestUI() != null) {
mPaymentUiService.getPaymentRequestUI().close();
ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents);
if (activity != null) {
activity.getLifecycleDispatcher().unregister(
mPaymentUiService.getPaymentRequestUI());
}
mPaymentUiService.setPaymentRequestUI(null);
mPaymentUiService.getPaymentUisShowStateReconciler().onPaymentRequestUiClosed();
}
setShowingPaymentRequest(null); setShowingPaymentRequest(null);
mIsCurrentPaymentRequestShowing = false; mIsCurrentPaymentRequestShowing = false;
if (mPaymentUiService.getPaymentMethodsSection() != null) {
for (int i = 0; i < mPaymentUiService.getPaymentMethodsSection().getSize(); i++) {
EditableOption option = mPaymentUiService.getPaymentMethodsSection().getItem(i);
((PaymentApp) option).dismissInstrument();
}
mPaymentUiService.setPaymentMethodsSection(null);
}
mPaymentUiService.removeLeavingTabObservers();
SettingsAutofillAndPaymentsObserver.getInstance().unregisterObserver(mPaymentUiService);
// Destroy native objects. // Destroy native objects.
mPaymentUiService.destroyCurrencyFormatters();
mJourneyLogger.destroy(); mJourneyLogger.destroy();
if (mPaymentHandlerHost != null) { if (mPaymentHandlerHost != null) {
mPaymentHandlerHost.destroy(); mPaymentHandlerHost.destroy();
mPaymentHandlerHost = null; mPaymentHandlerHost = null;
......
...@@ -62,7 +62,6 @@ import org.chromium.components.payments.PaymentAppType; ...@@ -62,7 +62,6 @@ import org.chromium.components.payments.PaymentAppType;
import org.chromium.components.payments.PaymentDetailsUpdateServiceHelper; import org.chromium.components.payments.PaymentDetailsUpdateServiceHelper;
import org.chromium.components.payments.PaymentFeatureList; import org.chromium.components.payments.PaymentFeatureList;
import org.chromium.components.payments.PaymentOptionsUtils; import org.chromium.components.payments.PaymentOptionsUtils;
import org.chromium.components.payments.PaymentRequestLifecycleObserver;
import org.chromium.components.payments.PaymentRequestParams; import org.chromium.components.payments.PaymentRequestParams;
import org.chromium.components.payments.PaymentUIsObserver; import org.chromium.components.payments.PaymentUIsObserver;
import org.chromium.components.payments.Section; import org.chromium.components.payments.Section;
...@@ -99,9 +98,8 @@ import java.util.Set; ...@@ -99,9 +98,8 @@ import java.util.Set;
* ChromePaymentRequestService} should be moved into this class. * ChromePaymentRequestService} should be moved into this class.
*/ */
public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Observer, public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Observer,
PaymentRequestLifecycleObserver, PaymentHandlerUiObserver, PaymentHandlerUiObserver, FocusChangedObserver,
FocusChangedObserver, NormalizedAddressRequestDelegate, NormalizedAddressRequestDelegate, PaymentRequestUI.Client {
PaymentRequestUI.Client {
/** Limit in the number of suggested items in a section. */ /** Limit in the number of suggested items in a section. */
/* package */ static final int SUGGESTIONS_LIMIT = 4; /* package */ static final int SUGGESTIONS_LIMIT = 4;
...@@ -136,7 +134,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -136,7 +134,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
private ShoppingCart mUiShoppingCart; private ShoppingCart mUiShoppingCart;
private boolean mMerchantSupportsAutofillCards; private boolean mMerchantSupportsAutofillCards;
private boolean mIsPaymentRequestParamsInitiated; private boolean mHasInitialized;
private boolean mHasClosed;
private SectionInformation mPaymentMethodsSection; private SectionInformation mPaymentMethodsSection;
private SectionInformation mShippingAddressesSection; private SectionInformation mShippingAddressesSection;
private ContactDetailsSection mContactSection; private ContactDetailsSection mContactSection;
...@@ -254,7 +253,6 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -254,7 +253,6 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/** /**
* Create PaymentUiService. * Create PaymentUiService.
* @param delegate The delegate of this instance. * @param delegate The delegate of this instance.
* @param params The parameters of the payment request specified by the merchant.
* @param webContents The WebContents of the merchant page. * @param webContents The WebContents of the merchant page.
* @param isOffTheRecord Whether merchant page is in an isOffTheRecord tab. * @param isOffTheRecord Whether merchant page is in an isOffTheRecord tab.
* @param journeyLogger The logger of the user journey. * @param journeyLogger The logger of the user journey.
...@@ -331,10 +329,10 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -331,10 +329,10 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/** /**
* @return Whether the merchant supports autofill cards. It can be used only after * @return Whether the merchant supports autofill cards. It can be used only after
* onPaymentRequestParamsInitiated() is invoked. * initialize() is invoked.
*/ */
public boolean merchantSupportsAutofillCards() { public boolean merchantSupportsAutofillCards() {
assert mIsPaymentRequestParamsInitiated; assert mHasInitialized;
return mMerchantSupportsAutofillCards; return mMerchantSupportsAutofillCards;
} }
...@@ -370,10 +368,10 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -370,10 +368,10 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/** /**
* @return Whether user can add credit card. It can be used only after * @return Whether user can add credit card. It can be used only after
* onPaymentRequestParamsInitiated() is invoked. * initialize() is invoked.
*/ */
public boolean canUserAddCreditCard() { public boolean canUserAddCreditCard() {
assert mIsPaymentRequestParamsInitiated; assert mHasInitialized;
return mCanUserAddCreditCard; return mCanUserAddCreditCard;
} }
...@@ -553,7 +551,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -553,7 +551,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
// Implement SettingsAutofillAndPaymentsObserver.Observer: // Implement SettingsAutofillAndPaymentsObserver.Observer:
@Override @Override
public void onCreditCardUpdated(CreditCard card) { public void onCreditCardUpdated(CreditCard card) {
assert mIsPaymentRequestParamsInitiated; assert mHasInitialized;
if (!mMerchantSupportsAutofillCards || mPaymentMethodsSection == null if (!mMerchantSupportsAutofillCards || mPaymentMethodsSection == null
|| mAutofillPaymentAppCreator == null) { || mAutofillPaymentAppCreator == null) {
return; return;
...@@ -579,7 +577,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -579,7 +577,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
// Implement SettingsAutofillAndPaymentsObserver.Observer: // Implement SettingsAutofillAndPaymentsObserver.Observer:
@Override @Override
public void onCreditCardDeleted(String guid) { public void onCreditCardDeleted(String guid) {
assert mIsPaymentRequestParamsInitiated; assert mHasInitialized;
if (!mMerchantSupportsAutofillCards || mPaymentMethodsSection == null) return; if (!mMerchantSupportsAutofillCards || mPaymentMethodsSection == null) return;
mPaymentMethodsSection.removeAndUnselectItem(guid); mPaymentMethodsSection.removeAndUnselectItem(guid);
...@@ -592,16 +590,23 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -592,16 +590,23 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
} }
} }
// Implement PaymentRequestLifecycleObserver: /**
@Override * Initializes the payment UI service.
public void onPaymentRequestParamsInitiated(PaymentRequestParams params) { * @param details The PaymentDetails provided by the merchant.
assert !params.hasClosed(); * @param rawTotal The raw total amount being charged.
for (PaymentMethodData method : params.getMethodData().values()) { * @param rawLineItems The raw items in the shopping cart, as they were received from the
* merchant page.
*/
public void initialize(
PaymentDetails details, PaymentItem rawTotal, List<PaymentItem> rawLineItems) {
assert !mParams.hasClosed();
updateDetailsOnPaymentRequestUI(details, rawTotal, rawLineItems);
for (PaymentMethodData method : mParams.getMethodData().values()) {
mCardEditor.addAcceptedPaymentMethodIfRecognized(method); 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(mParams.getMethodData());
// If in strict mode, don't give user an option to add an autofill card during the checkout // If in strict mode, don't give user an option to add an autofill card during the checkout
// to avoid the "unhappy" basic-card flow. // to avoid the "unhappy" basic-card flow.
...@@ -646,11 +651,12 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -646,11 +651,12 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
} }
mHaveRequestedAutofillData &= haveCompleteContactInfo; mHaveRequestedAutofillData &= haveCompleteContactInfo;
} }
mIsPaymentRequestParamsInitiated = true; mHasInitialized = true;
} }
/**
// Implement PaymentRequestLifecycleObserver: * Called after {@link PaymentRequest#retry} is invoked.
@Override * @param errors The payment validation errors.
*/
public void onRetry(PaymentValidationErrors errors) { public void onRetry(PaymentValidationErrors errors) {
// Remove all payment apps except the selected one. // Remove all payment apps except the selected one.
assert mPaymentMethodsSection != null; assert mPaymentMethodsSection != null;
...@@ -1677,4 +1683,40 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs ...@@ -1677,4 +1683,40 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
// result to the merchant. // result to the merchant.
PersonalDataManager.getInstance().normalizeAddress(address.getProfile(), /*delegate=*/this); PersonalDataManager.getInstance().normalizeAddress(address.getProfile(), /*delegate=*/this);
} }
/** Close the instance. Do not use this instance any more after calling this method. */
public void close() {
assert !mHasClosed;
mHasClosed = true;
if (mPaymentHandlerUi != null) {
mPaymentHandlerUi.hide();
mPaymentHandlerUi = null;
}
if (mMinimalUi != null) {
mMinimalUi.hide();
mMinimalUi = null;
}
if (mPaymentRequestUI != null) {
mPaymentRequestUI.close();
ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents);
if (activity != null) {
activity.getLifecycleDispatcher().unregister(mPaymentRequestUI);
}
mPaymentRequestUI = null;
mPaymentUisShowStateReconciler.onPaymentRequestUiClosed();
}
if (mPaymentMethodsSection != null) {
for (int i = 0; i < mPaymentMethodsSection.getSize(); i++) {
EditableOption option = mPaymentMethodsSection.getItem(i);
((PaymentApp) option).dismissInstrument();
}
mPaymentMethodsSection = null;
}
removeLeavingTabObservers();
destroyCurrencyFormatters();
}
} }
...@@ -129,7 +129,6 @@ android_library("all_java") { ...@@ -129,7 +129,6 @@ android_library("all_java") {
"java/src/org/chromium/components/payments/PaymentManifestDownloader.java", "java/src/org/chromium/components/payments/PaymentManifestDownloader.java",
"java/src/org/chromium/components/payments/PaymentManifestParser.java", "java/src/org/chromium/components/payments/PaymentManifestParser.java",
"java/src/org/chromium/components/payments/PaymentOptionsUtils.java", "java/src/org/chromium/components/payments/PaymentOptionsUtils.java",
"java/src/org/chromium/components/payments/PaymentRequestLifecycleObserver.java",
"java/src/org/chromium/components/payments/PaymentRequestParams.java", "java/src/org/chromium/components/payments/PaymentRequestParams.java",
"java/src/org/chromium/components/payments/PaymentRequestService.java", "java/src/org/chromium/components/payments/PaymentRequestService.java",
"java/src/org/chromium/components/payments/PaymentRequestServiceUtil.java", "java/src/org/chromium/components/payments/PaymentRequestServiceUtil.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.PaymentRequest;
import org.chromium.payments.mojom.PaymentValidationErrors;
/** Observe the lifecycle of the PaymentRequest. */
public interface PaymentRequestLifecycleObserver {
/**
* Called when all of the PaymentRequest parameters have been initiated and validated.
* @param params The parameters.
*/
void onPaymentRequestParamsInitiated(PaymentRequestParams params);
/**
* Called after {@link PaymentRequest#retry} is invoked.
* @param errors The payment validation errors.
*/
void onRetry(PaymentValidationErrors errors);
}
...@@ -66,7 +66,6 @@ public class PaymentRequestService { ...@@ -66,7 +66,6 @@ public class PaymentRequestService {
private final boolean mRequestPayerPhone; private final boolean mRequestPayerPhone;
private final boolean mRequestPayerEmail; private final boolean mRequestPayerEmail;
private final Delegate mDelegate; private final Delegate mDelegate;
private PaymentRequestLifecycleObserver mPaymentRequestLifecycleObserver;
private boolean mHasClosed; private boolean mHasClosed;
// mClient is null only when it has closed. // mClient is null only when it has closed.
...@@ -513,22 +512,6 @@ public class PaymentRequestService { ...@@ -513,22 +512,6 @@ public class PaymentRequestService {
mOnClosedListener.run(); mOnClosedListener.run();
} }
/**
* Register an observer for the PaymentRequest lifecycle.
* @param paymentRequestLifecycleObserver The observer, cannot be null.
*/
public void registerPaymentRequestLifecycleObserver(
PaymentRequestLifecycleObserver paymentRequestLifecycleObserver) {
assert paymentRequestLifecycleObserver != null;
mPaymentRequestLifecycleObserver = paymentRequestLifecycleObserver;
}
/** @return The observer for the PaymentRequest lifecycle, can be null. */
@Nullable
public PaymentRequestLifecycleObserver getPaymentRequestLifecycleObserver() {
return mPaymentRequestLifecycleObserver;
}
/** @return An observer for the payment request service, if any; otherwise, null. */ /** @return An observer for the payment request service, if any; otherwise, null. */
@Nullable @Nullable
public static PaymentRequestServiceObserverForTest getObserverForTest() { public static PaymentRequestServiceObserverForTest getObserverForTest() {
......
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