Commit 3c6ead95 authored by Rouslan Solomakhin's avatar Rouslan Solomakhin Committed by Commit Bot

[Web Payment] TWA package name in PaymentAppFactoryParams.

Before this patch, Android payment app finder would determine the TWA
package name directly through the package manager, which prevented the
C++ browser tests from mocking the TWA package name through the test
delegate.

This patch changes Android payment app finder to look up the TWA package
name through PaymentAppFactoryParams. The PaymentRequestImpl implements
the Params interface through the test delegate, which the C++ tests can
modify.

After this patch, android browser tests can simulate running inside of a
TWA.

Bug: 1061503
Change-Id: I4d5df8d10f80fbb81678e877496285bc8cedf8b9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2300179
Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarLiquan (Max) Gu <maxlg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798347}
parent a30a0f05
...@@ -190,11 +190,9 @@ public class AndroidPaymentAppFinder implements ManifestVerifyCallback { ...@@ -190,11 +190,9 @@ public class AndroidPaymentAppFinder implements ManifestVerifyCallback {
mIsIncognito = activity != null && activity.getCurrentTabModel().isIncognito(); mIsIncognito = activity != null && activity.getCurrentTabModel().isIncognito();
} }
private void findAppStoreBillingApp( private void findAppStoreBillingApp(List<ResolveInfo> allInstalledPaymentApps) {
ChromeActivity activity, List<ResolveInfo> allInstalledPaymentApps) { String twaPackageName = mFactoryDelegate.getParams().getTwaPackageName();
assert activity != null; if (TextUtils.isEmpty(twaPackageName)) return;
String twaPackageName = mTwaPackageManagerDelegate.getTwaPackageName(activity);
if (twaPackageName == null) return;
ResolveInfo twaApp = findAppWithPackageName(allInstalledPaymentApps, twaPackageName); ResolveInfo twaApp = findAppWithPackageName(allInstalledPaymentApps, twaPackageName);
if (twaApp == null) return; if (twaApp == null) return;
...@@ -298,20 +296,11 @@ public class AndroidPaymentAppFinder implements ManifestVerifyCallback { ...@@ -298,20 +296,11 @@ public class AndroidPaymentAppFinder implements ManifestVerifyCallback {
} }
} }
// WebContents is possible to attach to different activities on {@link PaymentRequest}
// created and shown. Ideally {@link #findAppStoreBillingApp} should have based on the
// activity that is used when PaymentRequest is shown. But we intentionally not do that for
// the sake of simple design and better performance. Plus, for app store billing case in
// particular, it's unusual for a TWA to switch to CCT without destroying JavaScript context
// and, consequently, the {@link PaymentRequest} object.
ChromeActivity activity =
ChromeActivity.fromWebContents(mFactoryDelegate.getParams().getWebContents());
if (!PaymentOptionsUtils.requestAnyInformation( if (!PaymentOptionsUtils.requestAnyInformation(
mFactoryDelegate.getParams().getPaymentOptions()) mFactoryDelegate.getParams().getPaymentOptions())
&& activity != null
&& PaymentFeatureList.isEnabled( && PaymentFeatureList.isEnabled(
PaymentFeatureList.WEB_PAYMENTS_APP_STORE_BILLING)) { PaymentFeatureList.WEB_PAYMENTS_APP_STORE_BILLING)) {
findAppStoreBillingApp(activity, allInstalledPaymentApps); findAppStoreBillingApp(allInstalledPaymentApps);
} }
// All URL methods for which manifests should be downloaded. For example, if merchant // All URL methods for which manifests should be downloaded. For example, if merchant
......
...@@ -1794,6 +1794,13 @@ public class PaymentRequestImpl ...@@ -1794,6 +1794,13 @@ public class PaymentRequestImpl
return mSpec; return mSpec;
} }
// PaymentAppFactoryParams implementation.
@Override
@Nullable
public String getTwaPackageName() {
return mDelegate.getTwaPackageName(ChromeActivity.fromWebContents(mWebContents));
}
// PaymentAppFactoryDelegate implementation. // PaymentAppFactoryDelegate implementation.
@Override @Override
public PaymentAppFactoryParams getParams() { public PaymentAppFactoryParams getParams() {
...@@ -2022,8 +2029,7 @@ public class PaymentRequestImpl ...@@ -2022,8 +2029,7 @@ public class PaymentRequestImpl
} }
private boolean isInTwa() { private boolean isInTwa() {
return !TextUtils.isEmpty( return !TextUtils.isEmpty(getTwaPackageName());
mDelegate.getTwaPackageName(ChromeActivity.fromWebContents(mWebContents)));
} }
/** /**
......
...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.payments; ...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.payments;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import androidx.annotation.Nullable;
import androidx.test.filters.MediumTest; import androidx.test.filters.MediumTest;
import org.junit.After; import org.junit.After;
...@@ -192,6 +193,13 @@ public class AndroidPaymentAppFinderTest ...@@ -192,6 +193,13 @@ public class AndroidPaymentAppFinderTest
mPaymentOptions.requestShipping = requestShipping; mPaymentOptions.requestShipping = requestShipping;
} }
// PaymentAppFactoryParams implementation.
@Override
@Nullable
public String getTwaPackageName() {
return mTwaPackageManager.getTwaPackageName(mRule.getActivity());
}
@Before @Before
public void setUp() throws Throwable { public void setUp() throws Throwable {
mRule.startMainActivityOnBlankPage(); mRule.startMainActivityOnBlankPage();
......
...@@ -75,4 +75,13 @@ public interface PaymentAppFactoryParams extends PaymentRequestParams { ...@@ -75,4 +75,13 @@ public interface PaymentAppFactoryParams extends PaymentRequestParams {
default PaymentRequestSpec getSpec() { default PaymentRequestSpec getSpec() {
return null; return null;
} }
/**
* @return The Android package name of the Trusted Web Activity that invoked Chrome, if any.
* Otherwise null or empty string.
*/
@Nullable
default String getTwaPackageName() {
return null;
}
} }
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