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
mPaymentUiService = new PaymentUiService(/*delegate=*/this,
/*params=*/this, mWebContents, mIsOffTheRecord, mJourneyLogger, mTopLevelOrigin,
/*observer=*/this);
mPaymentRequestService.registerPaymentRequestLifecycleObserver(mPaymentUiService);
}
// Implement BrowserPaymentRequest:
......@@ -307,15 +306,9 @@ public class ChromePaymentRequestService
return false;
}
mPaymentUiService.updateDetailsOnPaymentRequestUI(
mPaymentUiService.initialize(
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();
addUniqueFactoriesToPaymentAppService(service);
service.create(/*delegate=*/this);
......@@ -1181,7 +1174,7 @@ public class ChromePaymentRequestService
mWasRetryCalled = true;
mPaymentRequestService.getPaymentRequestLifecycleObserver().onRetry(errors);
mPaymentUiService.onRetry(errors);
}
// Implement BrowserPaymentRequest:
......@@ -1722,39 +1715,14 @@ public class ChromePaymentRequestService
* function is called.
*/
private void closeUIAndDestroyNativeObjects() {
mPaymentUiService.ensureHideAndResetPaymentHandlerUi();
mPaymentUiService.hideMinimalUI();
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();
}
mPaymentUiService.close();
SettingsAutofillAndPaymentsObserver.getInstance().unregisterObserver(mPaymentUiService);
setShowingPaymentRequest(null);
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.
mPaymentUiService.destroyCurrencyFormatters();
mJourneyLogger.destroy();
if (mPaymentHandlerHost != null) {
mPaymentHandlerHost.destroy();
mPaymentHandlerHost = null;
......
......@@ -62,7 +62,6 @@ import org.chromium.components.payments.PaymentAppType;
import org.chromium.components.payments.PaymentDetailsUpdateServiceHelper;
import org.chromium.components.payments.PaymentFeatureList;
import org.chromium.components.payments.PaymentOptionsUtils;
import org.chromium.components.payments.PaymentRequestLifecycleObserver;
import org.chromium.components.payments.PaymentRequestParams;
import org.chromium.components.payments.PaymentUIsObserver;
import org.chromium.components.payments.Section;
......@@ -99,9 +98,8 @@ import java.util.Set;
* ChromePaymentRequestService} should be moved into this class.
*/
public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Observer,
PaymentRequestLifecycleObserver, PaymentHandlerUiObserver,
FocusChangedObserver, NormalizedAddressRequestDelegate,
PaymentRequestUI.Client {
PaymentHandlerUiObserver, FocusChangedObserver,
NormalizedAddressRequestDelegate, PaymentRequestUI.Client {
/** Limit in the number of suggested items in a section. */
/* package */ static final int SUGGESTIONS_LIMIT = 4;
......@@ -136,7 +134,8 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
private ShoppingCart mUiShoppingCart;
private boolean mMerchantSupportsAutofillCards;
private boolean mIsPaymentRequestParamsInitiated;
private boolean mHasInitialized;
private boolean mHasClosed;
private SectionInformation mPaymentMethodsSection;
private SectionInformation mShippingAddressesSection;
private ContactDetailsSection mContactSection;
......@@ -254,7 +253,6 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/**
* Create PaymentUiService.
* @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 isOffTheRecord Whether merchant page is in an isOffTheRecord tab.
* @param journeyLogger The logger of the user journey.
......@@ -331,10 +329,10 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/**
* @return Whether the merchant supports autofill cards. It can be used only after
* onPaymentRequestParamsInitiated() is invoked.
* initialize() is invoked.
*/
public boolean merchantSupportsAutofillCards() {
assert mIsPaymentRequestParamsInitiated;
assert mHasInitialized;
return mMerchantSupportsAutofillCards;
}
......@@ -370,10 +368,10 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
/**
* @return Whether user can add credit card. It can be used only after
* onPaymentRequestParamsInitiated() is invoked.
* initialize() is invoked.
*/
public boolean canUserAddCreditCard() {
assert mIsPaymentRequestParamsInitiated;
assert mHasInitialized;
return mCanUserAddCreditCard;
}
......@@ -553,7 +551,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
// Implement SettingsAutofillAndPaymentsObserver.Observer:
@Override
public void onCreditCardUpdated(CreditCard card) {
assert mIsPaymentRequestParamsInitiated;
assert mHasInitialized;
if (!mMerchantSupportsAutofillCards || mPaymentMethodsSection == null
|| mAutofillPaymentAppCreator == null) {
return;
......@@ -579,7 +577,7 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
// Implement SettingsAutofillAndPaymentsObserver.Observer:
@Override
public void onCreditCardDeleted(String guid) {
assert mIsPaymentRequestParamsInitiated;
assert mHasInitialized;
if (!mMerchantSupportsAutofillCards || mPaymentMethodsSection == null) return;
mPaymentMethodsSection.removeAndUnselectItem(guid);
......@@ -592,16 +590,23 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
}
}
// Implement PaymentRequestLifecycleObserver:
@Override
public void onPaymentRequestParamsInitiated(PaymentRequestParams params) {
assert !params.hasClosed();
for (PaymentMethodData method : params.getMethodData().values()) {
/**
* Initializes the payment UI service.
* @param details The PaymentDetails provided by the merchant.
* @param rawTotal The raw total amount being charged.
* @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);
}
// Checks whether the merchant supports autofill cards before show is called.
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
// to avoid the "unhappy" basic-card flow.
......@@ -646,11 +651,12 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
}
mHaveRequestedAutofillData &= haveCompleteContactInfo;
}
mIsPaymentRequestParamsInitiated = true;
mHasInitialized = true;
}
// Implement PaymentRequestLifecycleObserver:
@Override
/**
* Called after {@link PaymentRequest#retry} is invoked.
* @param errors The payment validation errors.
*/
public void onRetry(PaymentValidationErrors errors) {
// Remove all payment apps except the selected one.
assert mPaymentMethodsSection != null;
......@@ -1677,4 +1683,40 @@ public class PaymentUiService implements SettingsAutofillAndPaymentsObserver.Obs
// result to the merchant.
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") {
"java/src/org/chromium/components/payments/PaymentManifestDownloader.java",
"java/src/org/chromium/components/payments/PaymentManifestParser.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/PaymentRequestService.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 {
private final boolean mRequestPayerPhone;
private final boolean mRequestPayerEmail;
private final Delegate mDelegate;
private PaymentRequestLifecycleObserver mPaymentRequestLifecycleObserver;
private boolean mHasClosed;
// mClient is null only when it has closed.
......@@ -513,22 +512,6 @@ public class PaymentRequestService {
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. */
@Nullable
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