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

[PRImpl] Move PRImpl#init UI logic into PaymentUIsManager

Change:
* Move the UI logic that's inlined in PRImpl#init into
PaymentUIsManager.
* Add PaymentOptionsUtils#requestAnyPayerInformation to simplify the
code.

Bug: 1102522

Change-Id: I25f3d06c2d107be38cba545c85ce513482220099
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2329943
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarSahel Sharify <sahel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794296}
parent baed8513
...@@ -304,8 +304,6 @@ public class PaymentRequestImpl ...@@ -304,8 +304,6 @@ public class PaymentRequestImpl
private final JourneyLogger mJourneyLogger; private final JourneyLogger mJourneyLogger;
private final boolean mIsOffTheRecord; private final boolean mIsOffTheRecord;
private List<AutofillProfile> mAutofillProfiles;
private boolean mHaveRequestedAutofillData = true;
private boolean mIsCanMakePaymentResponsePending; private boolean mIsCanMakePaymentResponsePending;
private boolean mIsHasEnrolledInstrumentResponsePending; private boolean mIsHasEnrolledInstrumentResponsePending;
private boolean mHasEnrolledInstrumentUsesPerMethodQuota; private boolean mHasEnrolledInstrumentUsesPerMethodQuota;
...@@ -571,43 +569,6 @@ public class PaymentRequestImpl ...@@ -571,43 +569,6 @@ public class PaymentRequestImpl
.onPaymentRequestParamsInitiated( .onPaymentRequestParamsInitiated(
/*params=*/this); /*params=*/this);
if (PaymentOptionsUtils.requestAnyInformation(mPaymentOptions)) {
mAutofillProfiles = Collections.unmodifiableList(
PersonalDataManager.getInstance().getProfilesToSuggest(
false /* includeNameInLabel */));
}
if (mRequestShipping) {
boolean haveCompleteShippingAddress = false;
for (int i = 0; i < mAutofillProfiles.size(); i++) {
if (AutofillAddress.checkAddressCompletionStatus(
mAutofillProfiles.get(i), AutofillAddress.CompletenessCheckType.NORMAL)
== AutofillAddress.CompletionStatus.COMPLETE) {
haveCompleteShippingAddress = true;
break;
}
}
mHaveRequestedAutofillData &= haveCompleteShippingAddress;
}
if (mRequestPayerName || mRequestPayerPhone || mRequestPayerEmail) {
// Do not persist changes on disk in OffTheRecord mode.
mPaymentUIsManager.setContactEditor(new ContactEditor(mRequestPayerName,
mRequestPayerPhone, mRequestPayerEmail, /*saveToDisk=*/!mIsOffTheRecord));
boolean haveCompleteContactInfo = false;
for (int i = 0; i < mAutofillProfiles.size(); i++) {
AutofillProfile profile = mAutofillProfiles.get(i);
if (mPaymentUIsManager.getContactEditor().checkContactCompletionStatus(
profile.getFullName(), profile.getPhoneNumber(),
profile.getEmailAddress())
== ContactEditor.COMPLETE) {
haveCompleteContactInfo = true;
break;
}
}
mHaveRequestedAutofillData &= haveCompleteContactInfo;
}
PaymentAppService.getInstance().create(/*delegate=*/this); PaymentAppService.getInstance().create(/*delegate=*/this);
// Log the various types of payment methods that were requested by the merchant. // Log the various types of payment methods that were requested by the merchant.
...@@ -750,12 +711,13 @@ public class PaymentRequestImpl ...@@ -750,12 +711,13 @@ public class PaymentRequestImpl
} }
if (shouldShowShippingSection() && !mWaitForUpdatedDetails) { if (shouldShowShippingSection() && !mWaitForUpdatedDetails) {
createShippingSection(activity, mAutofillProfiles); createShippingSection(activity, mPaymentUIsManager.getAutofillProfiles());
} }
if (shouldShowContactSection()) { if (shouldShowContactSection()) {
mPaymentUIsManager.setContactSection(new ContactDetailsSection(activity, mPaymentUIsManager.setContactSection(
mAutofillProfiles, mPaymentUIsManager.getContactEditor(), mJourneyLogger)); new ContactDetailsSection(activity, mPaymentUIsManager.getAutofillProfiles(),
mPaymentUIsManager.getContactEditor(), mJourneyLogger));
} }
mPaymentUIsManager.setPaymentRequestUI(new PaymentRequestUI(activity, this, mPaymentUIsManager.setPaymentRequestUI(new PaymentRequestUI(activity, this,
...@@ -1323,7 +1285,7 @@ public class PaymentRequestImpl ...@@ -1323,7 +1285,7 @@ public class PaymentRequestImpl
// Do not create shipping section When UI is not built yet. This happens when the show // Do not create shipping section When UI is not built yet. This happens when the show
// promise gets resolved before all apps are ready. // promise gets resolved before all apps are ready.
if (mPaymentUIsManager.getPaymentRequestUI() != null && shouldShowShippingSection()) { if (mPaymentUIsManager.getPaymentRequestUI() != null && shouldShowShippingSection()) {
createShippingSection(chromeActivity, mAutofillProfiles); createShippingSection(chromeActivity, mPaymentUIsManager.getAutofillProfiles());
} }
mWaitForUpdatedDetails = false; mWaitForUpdatedDetails = false;
...@@ -1553,13 +1515,14 @@ public class PaymentRequestImpl ...@@ -1553,13 +1515,14 @@ public class PaymentRequestImpl
&& mPaymentUIsManager.getShippingAddressesSection() == null) { && mPaymentUIsManager.getShippingAddressesSection() == null) {
ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents); ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents);
assert activity != null; assert activity != null;
createShippingSection(activity, mAutofillProfiles); createShippingSection(activity, mPaymentUIsManager.getAutofillProfiles());
} }
if (shouldShowContactSection() && mPaymentUIsManager.getContactSection() == null) { if (shouldShowContactSection() && mPaymentUIsManager.getContactSection() == null) {
ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents); ChromeActivity activity = ChromeActivity.fromWebContents(mWebContents);
assert activity != null; assert activity != null;
mPaymentUIsManager.setContactSection(new ContactDetailsSection(activity, mPaymentUIsManager.setContactSection(new ContactDetailsSection(activity,
mAutofillProfiles, mPaymentUIsManager.getContactEditor(), mJourneyLogger)); mPaymentUIsManager.getAutofillProfiles(),
mPaymentUIsManager.getContactEditor(), mJourneyLogger));
} }
mPaymentUIsManager.onSelectedPaymentMethodUpdated(); mPaymentUIsManager.onSelectedPaymentMethodUpdated();
PaymentApp paymentApp = (PaymentApp) option; PaymentApp paymentApp = (PaymentApp) option;
...@@ -2266,7 +2229,7 @@ public class PaymentRequestImpl ...@@ -2266,7 +2229,7 @@ public class PaymentRequestImpl
if (getClient() == null) return; if (getClient() == null) return;
mHideServerAutofillCards |= paymentApp.isServerAutofillInstrumentReplacement(); mHideServerAutofillCards |= paymentApp.isServerAutofillInstrumentReplacement();
paymentApp.setHaveRequestedAutofillData(mHaveRequestedAutofillData); paymentApp.setHaveRequestedAutofillData(mPaymentUIsManager.haveRequestedAutofillData());
mHasEnrolledInstrument |= paymentApp.canMakePayment(); mHasEnrolledInstrument |= paymentApp.canMakePayment();
mHasNonAutofillApp |= !paymentApp.isAutofillInstrument(); mHasNonAutofillApp |= !paymentApp.isAutofillInstrument();
......
...@@ -11,6 +11,8 @@ import androidx.annotation.VisibleForTesting; ...@@ -11,6 +11,8 @@ import androidx.annotation.VisibleForTesting;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.chrome.browser.ChromeActivity; import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.autofill.PersonalDataManager;
import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard; import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
import org.chromium.chrome.browser.payments.AddressEditor; import org.chromium.chrome.browser.payments.AddressEditor;
import org.chromium.chrome.browser.payments.AutofillAddress; import org.chromium.chrome.browser.payments.AutofillAddress;
...@@ -39,6 +41,7 @@ import org.chromium.payments.mojom.PaymentCurrencyAmount; ...@@ -39,6 +41,7 @@ import org.chromium.payments.mojom.PaymentCurrencyAmount;
import org.chromium.payments.mojom.PaymentDetails; import org.chromium.payments.mojom.PaymentDetails;
import org.chromium.payments.mojom.PaymentDetailsModifier; import org.chromium.payments.mojom.PaymentDetailsModifier;
import org.chromium.payments.mojom.PaymentItem; import org.chromium.payments.mojom.PaymentItem;
import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentShippingOption; import org.chromium.payments.mojom.PaymentShippingOption;
import org.chromium.url.GURL; import org.chromium.url.GURL;
...@@ -61,6 +64,7 @@ import java.util.Set; ...@@ -61,6 +64,7 @@ import java.util.Set;
public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Observer, public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Observer,
PaymentRequestLifecycleObserver, PaymentRequestLifecycleObserver,
PaymentHandlerUiObserver { PaymentHandlerUiObserver {
private final boolean mIsOffTheRecord;
private final Handler mHandler = new Handler(); private final Handler mHandler = new Handler();
private final Queue<Runnable> mRetryQueue = new LinkedList<>(); private final Queue<Runnable> mRetryQueue = new LinkedList<>();
private ContactEditor mContactEditor; private ContactEditor mContactEditor;
...@@ -82,7 +86,8 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -82,7 +86,8 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
private SectionInformation mShippingAddressesSection; private SectionInformation mShippingAddressesSection;
private ContactDetailsSection mContactSection; private ContactDetailsSection mContactSection;
private AutofillPaymentAppCreator mAutofillPaymentAppCreator; private AutofillPaymentAppCreator mAutofillPaymentAppCreator;
private boolean mHaveRequestedAutofillData = true;
private List<AutofillProfile> mAutofillProfiles;
private Boolean mCanUserAddCreditCard; private Boolean mCanUserAddCreditCard;
/** The delegate of this class. */ /** The delegate of this class. */
...@@ -166,6 +171,7 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -166,6 +171,7 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
mPaymentUisShowStateReconciler = new PaymentUisShowStateReconciler(); mPaymentUisShowStateReconciler = new PaymentUisShowStateReconciler();
mCurrencyFormatterMap = new HashMap<>(); mCurrencyFormatterMap = new HashMap<>();
mIsOffTheRecord = isOffTheRecord;
} }
/** /**
...@@ -302,16 +308,21 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -302,16 +308,21 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
return mContactEditor; return mContactEditor;
} }
/** Set the contact editor for PaymentRequest UI. */
public void setContactEditor(ContactEditor contactEditor) {
mContactEditor = contactEditor;
}
/** Get the retry queue. */ /** Get the retry queue. */
public Queue<Runnable> getRetryQueue() { public Queue<Runnable> getRetryQueue() {
return mRetryQueue; return mRetryQueue;
} }
/** @return The autofill profiles. */
public List<AutofillProfile> getAutofillProfiles() {
return mAutofillProfiles;
}
/** @return Whether PaymentRequestUI has requested autofill data. */
public boolean haveRequestedAutofillData() {
return mHaveRequestedAutofillData;
}
// Implement SettingsAutofillAndPaymentsObserver.Observer: // Implement SettingsAutofillAndPaymentsObserver.Observer:
@Override @Override
public void onAddressUpdated(AutofillAddress address) { public void onAddressUpdated(AutofillAddress address) {
...@@ -393,6 +404,45 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob ...@@ -393,6 +404,45 @@ public class PaymentUIsManager implements SettingsAutofillAndPaymentsObserver.Ob
mCanUserAddCreditCard = mMerchantSupportsAutofillCards mCanUserAddCreditCard = mMerchantSupportsAutofillCards
&& !PaymentFeatureList.isEnabledOrExperimentalFeaturesEnabled( && !PaymentFeatureList.isEnabledOrExperimentalFeaturesEnabled(
PaymentFeatureList.STRICT_HAS_ENROLLED_AUTOFILL_INSTRUMENT); PaymentFeatureList.STRICT_HAS_ENROLLED_AUTOFILL_INSTRUMENT);
if (PaymentOptionsUtils.requestAnyInformation(mParams.getPaymentOptions())) {
mAutofillProfiles = Collections.unmodifiableList(
PersonalDataManager.getInstance().getProfilesToSuggest(
false /* includeNameInLabel */));
}
if (PaymentOptionsUtils.requestShipping(mParams.getPaymentOptions())) {
boolean haveCompleteShippingAddress = false;
for (int i = 0; i < mAutofillProfiles.size(); i++) {
if (AutofillAddress.checkAddressCompletionStatus(
mAutofillProfiles.get(i), AutofillAddress.CompletenessCheckType.NORMAL)
== AutofillAddress.CompletionStatus.COMPLETE) {
haveCompleteShippingAddress = true;
break;
}
}
mHaveRequestedAutofillData &= haveCompleteShippingAddress;
}
PaymentOptions options = mParams.getPaymentOptions();
if (PaymentOptionsUtils.requestAnyContactInformation(mParams.getPaymentOptions())) {
// Do not persist changes on disk in OffTheRecord mode.
mContactEditor = new ContactEditor(PaymentOptionsUtils.requestPayerName(options),
PaymentOptionsUtils.requestPayerPhone(options),
PaymentOptionsUtils.requestPayerEmail(options),
/*saveToDisk=*/!mIsOffTheRecord);
boolean haveCompleteContactInfo = false;
for (int i = 0; i < getAutofillProfiles().size(); i++) {
AutofillProfile profile = getAutofillProfiles().get(i);
if (getContactEditor().checkContactCompletionStatus(profile.getFullName(),
profile.getPhoneNumber(), profile.getEmailAddress())
== ContactEditor.COMPLETE) {
haveCompleteContactInfo = true;
break;
}
}
mHaveRequestedAutofillData &= haveCompleteContactInfo;
}
} }
/** @return The selected payment app type. */ /** @return The selected payment app type. */
......
...@@ -23,6 +23,15 @@ public class PaymentOptionsUtils { ...@@ -23,6 +23,15 @@ public class PaymentOptionsUtils {
|| options.requestPayerName; || options.requestPayerName;
} }
/**
* @param options Any PaymentOption, can be null.
* @return Whether a PaymentOptions has requested any payer's information (email, phone, name).
*/
public static boolean requestAnyContactInformation(@Nullable PaymentOptions options) {
if (options == null) return false;
return options.requestPayerEmail || options.requestPayerPhone || options.requestPayerName;
}
/** /**
* @param options Any PaymentOptions, can be null. * @param options Any PaymentOptions, can be null.
* @return Return a JSON string indicating whether each information is requested in the * @return Return a JSON string indicating whether each information is requested in the
......
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