Commit 57bf3ef2 authored by Zarah Tesfai's avatar Zarah Tesfai Committed by Commit Bot

[Componentize PRImpl] Refactor buildUI method

Purpose: Move payment request UI logic of buildUI to PaymentUIsManager

Change:
* buildPaymentRequestUI() implemented in PaymentUIsManager
* added PaymentUIsObserver interface
* getShippingType() implemented in PaymentOptionsUtils

Bug: 1112974
Change-Id: I7ce7e9193bdd3f368ee44bb7457a92275b340beb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2343155
Commit-Queue: Zarah Tesfai <ztesfai@google.com>
Auto-Submit: Zarah Tesfai <ztesfai@google.com>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarLiquan (Max) Gu <maxlg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800768}
parent ecb9d657
......@@ -37,7 +37,6 @@ import org.chromium.chrome.browser.payments.ui.PaymentRequestUI.SelectionResult;
import org.chromium.chrome.browser.payments.ui.PaymentUIsManager;
import org.chromium.chrome.browser.payments.ui.SectionInformation;
import org.chromium.chrome.browser.payments.ui.ShoppingCart;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.SettingsLauncher;
import org.chromium.chrome.browser.settings.SettingsLauncherImpl;
import org.chromium.chrome.browser.tab.Tab;
......@@ -47,7 +46,6 @@ import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.tabmodel.TabModelObserver;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper;
import org.chromium.components.autofill.EditableOption;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetControllerProvider;
import org.chromium.components.embedder_support.util.UrlConstants;
......@@ -78,7 +76,6 @@ import org.chromium.components.payments.PaymentRequestUpdateEventListener;
import org.chromium.components.payments.PaymentValidator;
import org.chromium.components.payments.Section;
import org.chromium.components.payments.UrlUtil;
import org.chromium.components.security_state.SecurityStateModel;
import org.chromium.content_public.browser.RenderFrameHost;
import org.chromium.content_public.browser.WebContents;
import org.chromium.payments.mojom.CanMakePaymentQueryResult;
......@@ -335,10 +332,11 @@ public class PaymentRequestImpl
mDelegate = delegate;
mWebContents = componentPaymentRequestImpl.getWebContents();
mJourneyLogger = componentPaymentRequestImpl.getJourneyLogger();
mPaymentUIsManager = new PaymentUIsManager(/*delegate=*/this,
/*params=*/this, mWebContents, componentPaymentRequestImpl.isOffTheRecord(),
mJourneyLogger);
mComponentPaymentRequestImpl = componentPaymentRequestImpl;
mPaymentUIsManager = new PaymentUIsManager(/*delegate=*/this,
/*params=*/this, mWebContents, mComponentPaymentRequestImpl.isOffTheRecord(),
mJourneyLogger, mComponentPaymentRequestImpl.getTopLevelOrigin(),
mComponentPaymentRequestImpl);
mComponentPaymentRequestImpl.registerPaymentRequestLifecycleObserver(mPaymentUIsManager);
}
......@@ -355,7 +353,7 @@ public class PaymentRequestImpl
mRequestPayerName = options != null && options.requestPayerName;
mRequestPayerPhone = options != null && options.requestPayerPhone;
mRequestPayerEmail = options != null && options.requestPayerEmail;
mShippingType = options == null ? PaymentShippingType.SHIPPING : options.shippingType;
mShippingType = PaymentOptionsUtils.getShippingType(options);
// TODO(crbug.com/978471): Improve architecture for handling prohibited origins and invalid
// SSL certificates.
......@@ -556,47 +554,7 @@ public class PaymentRequestImpl
mPaymentUIsManager.getContactEditor(), mJourneyLogger));
}
mPaymentUIsManager.setPaymentRequestUI(new PaymentRequestUI(activity, this,
mPaymentUIsManager.merchantSupportsAutofillCards(),
!PaymentPreferencesUtil.isPaymentCompleteOnce(),
mComponentPaymentRequestImpl.getMerchantName(),
mComponentPaymentRequestImpl.getTopLevelOrigin(),
SecurityStateModel.getSecurityLevelForWebContents(mWebContents),
new ShippingStrings(mShippingType),
mPaymentUIsManager.getPaymentUisShowStateReconciler(),
Profile.fromWebContents(mWebContents)));
activity.getLifecycleDispatcher().register(
mPaymentUIsManager
.getPaymentRequestUI()); // registered as a PauseResumeWithNativeObserver
final FaviconHelper faviconHelper = new FaviconHelper();
faviconHelper.getLocalFaviconImageForURL(Profile.fromWebContents(mWebContents),
mWebContents.getLastCommittedUrl(),
activity.getResources().getDimensionPixelSize(R.dimen.payments_favicon_size),
(bitmap, iconUrl) -> {
if (mComponentPaymentRequestImpl != null && bitmap == null) {
mComponentPaymentRequestImpl.warnNoFavicon();
}
if (mPaymentUIsManager.getPaymentRequestUI() != null && bitmap != null) {
mPaymentUIsManager.getPaymentRequestUI().setTitleBitmap(bitmap);
}
faviconHelper.destroy();
});
// Add the callback to change the label of shipping addresses depending on the focus.
if (mRequestShipping) {
mPaymentUIsManager.setShippingAddressSectionFocusChangedObserverForPaymentRequestUI();
}
mPaymentUIsManager.getAddressEditor().setEditorDialog(
mPaymentUIsManager.getPaymentRequestUI().getEditorDialog());
mPaymentUIsManager.getCardEditor().setEditorDialog(
mPaymentUIsManager.getPaymentRequestUI().getCardEditorDialog());
if (mPaymentUIsManager.getContactEditor() != null) {
mPaymentUIsManager.getContactEditor().setEditorDialog(
mPaymentUIsManager.getPaymentRequestUI().getEditorDialog());
}
mPaymentUIsManager.buildPaymentRequestUI(activity);
return true;
}
......@@ -1206,6 +1164,7 @@ public class PaymentRequestImpl
});
}
// Implement PaymentUIsManager.Delegate:
@Override
public void recordShowEventAndTransactionAmount() {
if (mDidRecordShowEvent) return;
......@@ -2129,6 +2088,7 @@ public class PaymentRequestImpl
onAddressNormalized(profile);
}
// Implement PaymentUIsManager.Delegate:
@Override
public void startShippingAddressChangeNormalization(AutofillAddress address) {
// Will call back into either onAddressNormalized or onCouldNotNormalize which will send the
......@@ -2137,6 +2097,12 @@ public class PaymentRequestImpl
address.getProfile(), /* delegate= */ this);
}
// Implement PaymentUIsManager.Delegate:
@Override
public PaymentRequestUI.Client getPaymentRequestUIClient() {
return this;
}
/**
* Closes the UI and destroys native objects. If the client is still connected, then it's
* notified of UI hiding. This PaymentRequestImpl object can't be reused after this function is
......
......@@ -26,12 +26,16 @@ import org.chromium.chrome.browser.payments.AutofillPaymentAppFactory;
import org.chromium.chrome.browser.payments.AutofillPaymentInstrument;
import org.chromium.chrome.browser.payments.CardEditor;
import org.chromium.chrome.browser.payments.ContactEditor;
import org.chromium.chrome.browser.payments.PaymentPreferencesUtil;
import org.chromium.chrome.browser.payments.PaymentRequestImpl;
import org.chromium.chrome.browser.payments.SettingsAutofillAndPaymentsObserver;
import org.chromium.chrome.browser.payments.ShippingStrings;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator.PaymentHandlerUiObserver;
import org.chromium.chrome.browser.payments.handler.PaymentHandlerCoordinator.PaymentHandlerWebContentsObserver;
import org.chromium.chrome.browser.payments.ui.PaymentRequestSection.OptionSection.FocusChangedObserver;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper;
import org.chromium.components.autofill.Completable;
import org.chromium.components.autofill.EditableOption;
import org.chromium.components.payments.CurrencyFormatter;
......@@ -43,7 +47,9 @@ 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;
import org.chromium.components.security_state.SecurityStateModel;
import org.chromium.content_public.browser.WebContents;
import org.chromium.payments.mojom.AddressErrors;
import org.chromium.payments.mojom.PayerDetail;
......@@ -94,6 +100,8 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
private SectionInformation mUiShippingOptions;
private final Delegate mDelegate;
private final WebContents mWebContents;
private final String mTopLevelOriginFormattedForDisplay;
private final String mMerchantName;
private final Map<String, CurrencyFormatter> mCurrencyFormatterMap;
private final AddressEditor mAddressEditor;
private final CardEditor mCardEditor;
......@@ -112,6 +120,7 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
private List<AutofillProfile> mAutofillProfiles;
private Boolean mCanUserAddCreditCard;
private final JourneyLogger mJourneyLogger;
private PaymentUIsObserver mObserver;
/** The delegate of this class. */
public interface Delegate {
......@@ -121,6 +130,10 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
void recordShowEventAndTransactionAmount();
/** Start the normalization of the new shipping address. */
void startShippingAddressChangeNormalization(AutofillAddress editedAddress);
/** Gets the PaymentRequestUI Client from PaymentRequestImpl. */
// TODO(crbug.com/1114791): This class will implement the PaymentRequestUI Client interface
// once its current implementation has no dependency on PRImpl.
PaymentRequestUI.Client getPaymentRequestUIClient();
}
/**
......@@ -184,9 +197,12 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
* @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.
* @param topLevelOrigin The last committed url of webContents.
* @param observer The payment UIs observer.
*/
public PaymentUIsManager(Delegate delegate, PaymentRequestParams params,
WebContents webContents, boolean isOffTheRecord, JourneyLogger journeyLogger) {
WebContents webContents, boolean isOffTheRecord, JourneyLogger journeyLogger,
String topLevelOrigin, PaymentUIsObserver observer) {
mDelegate = delegate;
mParams = params;
......@@ -198,11 +214,14 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
mCardEditor = new CardEditor(webContents, mAddressEditor, /*includeOrgLabel=*/false);
mJourneyLogger = journeyLogger;
mWebContents = webContents;
mTopLevelOriginFormattedForDisplay = topLevelOrigin;
mMerchantName = webContents.getTitle();
mPaymentUisShowStateReconciler = new PaymentUisShowStateReconciler();
mCurrencyFormatterMap = new HashMap<>();
mIsOffTheRecord = isOffTheRecord;
mPaymentAppComparator = new PaymentAppComparator(/*params=*/mParams);
mObserver = observer;
}
/** @return The PaymentRequestUI. */
......@@ -935,6 +954,47 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
});
}
/**
* Build the PaymentRequest UI.
* @param activity The ChromeActivity for the payment request.
*/
public void buildPaymentRequestUI(ChromeActivity activity) {
mPaymentRequestUI = new PaymentRequestUI(activity, mDelegate.getPaymentRequestUIClient(),
merchantSupportsAutofillCards(), !PaymentPreferencesUtil.isPaymentCompleteOnce(),
mMerchantName, mTopLevelOriginFormattedForDisplay,
SecurityStateModel.getSecurityLevelForWebContents(mWebContents),
new ShippingStrings(
PaymentOptionsUtils.getShippingType(mParams.getPaymentOptions())),
mPaymentUisShowStateReconciler, Profile.fromWebContents(mWebContents));
activity.getLifecycleDispatcher().register(
mPaymentRequestUI); // registered as a PauseResumeWithNativeObserver
final FaviconHelper faviconHelper = new FaviconHelper();
faviconHelper.getLocalFaviconImageForURL(Profile.fromWebContents(mWebContents),
mWebContents.getLastCommittedUrl(),
activity.getResources().getDimensionPixelSize(R.dimen.payments_favicon_size),
(bitmap, iconUrl) -> {
if (bitmap == null) {
mObserver.onPaymentRequestUIFaviconNotAvailable();
}
if (mPaymentRequestUI != null && bitmap != null) {
mPaymentRequestUI.setTitleBitmap(bitmap);
}
faviconHelper.destroy();
});
// Add the callback to change the label of shipping addresses depending on the focus.
if (PaymentOptionsUtils.requestShipping(mParams.getPaymentOptions())) {
setShippingAddressSectionFocusChangedObserverForPaymentRequestUI();
}
mAddressEditor.setEditorDialog(mPaymentRequestUI.getEditorDialog());
mCardEditor.setEditorDialog(mPaymentRequestUI.getCardEditorDialog());
if (mContactEditor != null) {
mContactEditor.setEditorDialog(mPaymentRequestUI.getEditorDialog());
}
}
/** Create a shipping section for PaymentRequest UI. */
public void createShippingSectionForPaymentRequestUI(Context context) {
List<AutofillAddress> addresses = new ArrayList<>();
......
......@@ -106,6 +106,7 @@ android_library("java") {
"java/src/org/chromium/components/payments/PaymentRequestParams.java",
"java/src/org/chromium/components/payments/PaymentRequestSpec.java",
"java/src/org/chromium/components/payments/PaymentRequestUpdateEventListener.java",
"java/src/org/chromium/components/payments/PaymentUIsObserver.java",
"java/src/org/chromium/components/payments/PaymentValidator.java",
"java/src/org/chromium/components/payments/SslValidityChecker.java",
"java/src/org/chromium/components/payments/SupportedDelegations.java",
......
......@@ -40,7 +40,7 @@ import java.util.List;
* PaymentRequestImpl. Note that the callers of the instances of this class need to close them with
* {@link ComponentPaymentRequestImpl#close()}, after which no usage is allowed.
*/
public class ComponentPaymentRequestImpl {
public class ComponentPaymentRequestImpl implements PaymentUIsObserver {
private static final String TAG = "CompPaymentRequest";
private static PaymentRequestServiceObserverForTest sObserverForTest;
private static NativeObserverForTest sNativeObserverForTest;
......@@ -567,8 +567,10 @@ public class ComponentPaymentRequestImpl {
mClient.onHasEnrolledInstrument(result);
}
// PaymentUIsObserver implementation.
@Override
/** Invokes {@link PaymentRequest.warnNoFavicon}. */
public void warnNoFavicon() {
public void onPaymentRequestUIFaviconNotAvailable() {
// Every caller should stop referencing this class once close() is called.
assert mClient != null;
......
......@@ -7,6 +7,7 @@ package org.chromium.components.payments;
import androidx.annotation.Nullable;
import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentShippingType;
/**
* A collection of utility methods for PaymentOptions.
......@@ -56,8 +57,7 @@ public class PaymentOptionsUtils {
* @param paymentOptions The PaymentOptions of the payment request.
* @return Whether requestShipping is specified in the payment request.
*/
public static boolean requestShipping(
org.chromium.payments.mojom.PaymentOptions paymentOptions) {
public static boolean requestShipping(PaymentOptions paymentOptions) {
return paymentOptions != null && paymentOptions.requestShipping;
}
......@@ -65,8 +65,7 @@ public class PaymentOptionsUtils {
* @param paymentOptions The PaymentOptions of the payment request.
* @return Whether requestPayerName is specified in the payment request.
*/
public static boolean requestPayerName(
org.chromium.payments.mojom.PaymentOptions paymentOptions) {
public static boolean requestPayerName(PaymentOptions paymentOptions) {
return paymentOptions != null && paymentOptions.requestPayerName;
}
......@@ -74,8 +73,7 @@ public class PaymentOptionsUtils {
* @param paymentOptions The PaymentOptions of the payment request.
* @return Whether requestPayerPhone is specified in the payment request.
*/
public static boolean requestPayerPhone(
org.chromium.payments.mojom.PaymentOptions paymentOptions) {
public static boolean requestPayerPhone(PaymentOptions paymentOptions) {
return paymentOptions != null && paymentOptions.requestPayerPhone;
}
......@@ -83,8 +81,17 @@ public class PaymentOptionsUtils {
* @param paymentOptions The PaymentOptions of the payment request.
* @return Whether requestPayerEmail is specified in the payment request.
*/
public static boolean requestPayerEmail(
org.chromium.payments.mojom.PaymentOptions paymentOptions) {
public static boolean requestPayerEmail(PaymentOptions paymentOptions) {
return paymentOptions != null && paymentOptions.requestPayerEmail;
}
/**
* @param options The PaymentOptions of the payment request.
* @return The shippingType of the payment request.
*/
public static int getShippingType(PaymentOptions options) {
// SHIPPING chosen as default according to w3c spec
// (https://w3c.github.io/payment-request/#paymentoptions-dictionary).
return options == null ? PaymentShippingType.SHIPPING : options.shippingType;
}
}
\ No newline at end of file
// 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;
/** Interface for observing payment UIs. */
public interface PaymentUIsObserver {
/** Called when favicon not available for payment request UI. */
void onPaymentRequestUIFaviconNotAvailable();
}
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