Commit cbdeef95 authored by Rouslan Solomakhin's avatar Rouslan Solomakhin Committed by Commit Bot

[Web Payment] TWA package name in PaymentAppFactory::Delegate.

Before this patch, the TWA package name was available only on desktop
through ChromePaymentRequestDelegate object, which prevented TWA payment
app logic from being cross-platform.

This patch adds the TWA package name to PaymentAppFactory::Delegate,
which is implemented on both desktop and Android.

After this patch, the logic for handling TWA payment apps can be
cross-platform.

Bug: 1061503
Change-Id: I01ac3c2e3a01e31f17a9e8e963671302f92c091a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2300002
Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarSahel Sharify <sahel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#798442}
parent 6b4f9da3
......@@ -43,7 +43,8 @@ public class PaymentAppServiceBridge implements PaymentAppFactoryInterface {
PaymentAppServiceBridgeJni.get().create(delegate.getParams().getRenderFrameHost(),
delegate.getParams().getTopLevelOrigin(), delegate.getParams().getSpec(),
delegate.getParams().getMayCrawl(), callback);
delegate.getParams().getTwaPackageName(), delegate.getParams().getMayCrawl(),
callback);
}
/** Handles callbacks from native PaymentAppService. */
......@@ -89,8 +90,21 @@ public class PaymentAppServiceBridge implements PaymentAppFactoryInterface {
@NativeMethods
/* package */ interface Natives {
/**
* Creates a native payment app service.
* @param initiatorRenderFrameHost The host of the render frame where PaymentRequest API was
* invoked.
* @param topOrigin The (scheme, host, port) tuple of top level context where
* PaymentRequest API was invoked.
* @param spec The parameters passed into the PaymentRequest API.
* @param twaPackageName The Android package name of the Trusted Web Activity that invoked
* Chrome. If not running in TWA mode, then this string is null or empty.
* @param mayCrawlForInstallablePaymentApps Whether crawling for just-in-time installable
* payment apps is allowed.
* @param callback The callback that receives the discovered payment apps.
*/
void create(RenderFrameHost initiatorRenderFrameHost, String topOrigin,
PaymentRequestSpec spec, boolean mayCrawlForInstallablePaymentApps,
PaymentAppServiceCallback callback);
PaymentRequestSpec spec, String twaPackageName,
boolean mayCrawlForInstallablePaymentApps, PaymentAppServiceCallback callback);
}
}
......@@ -87,6 +87,7 @@ void JNI_PaymentAppServiceBridge_Create(
const JavaParamRef<jobject>& jrender_frame_host,
const JavaParamRef<jstring>& jtop_origin,
const JavaParamRef<jobject>& jpayment_request_spec,
const JavaParamRef<jstring>& jtwa_package_name,
jboolean jmay_crawl_for_installable_payment_apps,
const JavaParamRef<jobject>& jcallback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
......@@ -108,6 +109,7 @@ void JNI_PaymentAppServiceBridge_Create(
service->GetNumberOfFactories(), render_frame_host, GURL(top_origin),
payments::android::PaymentRequestSpec::FromJavaPaymentRequestSpec(
env, jpayment_request_spec),
jtwa_package_name ? ConvertJavaStringToUTF8(env, jtwa_package_name) : "",
web_data_service, jmay_crawl_for_installable_payment_apps,
base::BindOnce(&OnCanMakePaymentCalculated,
ScopedJavaGlobalRef<jobject>(env, jcallback)),
......@@ -161,15 +163,18 @@ PaymentAppServiceBridge* PaymentAppServiceBridge::Create(
content::RenderFrameHost* render_frame_host,
const GURL& top_origin,
PaymentRequestSpec* spec,
const std::string& twa_package_name,
scoped_refptr<PaymentManifestWebDataService> web_data_service,
bool may_crawl_for_installable_payment_apps,
CanMakePaymentCalculatedCallback can_make_payment_calculated_callback,
PaymentAppCreatedCallback payment_app_created_callback,
PaymentAppCreationErrorCallback payment_app_creation_error_callback,
base::OnceClosure done_creating_payment_apps_callback) {
// Not using std::make_unique, because that requires a public constructor.
std::unique_ptr<PaymentAppServiceBridge> bridge(new PaymentAppServiceBridge(
number_of_factories, render_frame_host, top_origin, spec,
std::move(web_data_service), may_crawl_for_installable_payment_apps,
twa_package_name, std::move(web_data_service),
may_crawl_for_installable_payment_apps,
std::move(can_make_payment_calculated_callback),
std::move(payment_app_created_callback),
std::move(payment_app_creation_error_callback),
......@@ -182,6 +187,7 @@ PaymentAppServiceBridge::PaymentAppServiceBridge(
content::RenderFrameHost* render_frame_host,
const GURL& top_origin,
PaymentRequestSpec* spec,
const std::string& twa_package_name,
scoped_refptr<PaymentManifestWebDataService> web_data_service,
bool may_crawl_for_installable_payment_apps,
CanMakePaymentCalculatedCallback can_make_payment_calculated_callback,
......@@ -197,6 +203,7 @@ PaymentAppServiceBridge::PaymentAppServiceBridge(
render_frame_host->GetLastCommittedURL())),
frame_security_origin_(render_frame_host->GetLastCommittedOrigin()),
spec_(spec),
twa_package_name_(twa_package_name),
payment_manifest_web_data_service_(web_data_service),
may_crawl_for_installable_payment_apps_(
may_crawl_for_installable_payment_apps),
......@@ -282,6 +289,10 @@ PaymentRequestSpec* PaymentAppServiceBridge::GetSpec() const {
return spec_;
}
std::string PaymentAppServiceBridge::GetTwaPackageName() const {
return twa_package_name_;
}
void PaymentAppServiceBridge::OnPaymentAppCreated(
std::unique_ptr<PaymentApp> app) {
if (can_make_payment_calculated_callback_)
......
......@@ -44,6 +44,7 @@ class PaymentAppServiceBridge : public PaymentAppFactory::Delegate {
content::RenderFrameHost* render_frame_host,
const GURL& top_origin,
PaymentRequestSpec* spec,
const std::string& twa_package_name,
scoped_refptr<PaymentManifestWebDataService> web_data_service,
bool may_crawl_for_installable_payment_apps,
CanMakePaymentCalculatedCallback can_make_payment_calculated_callback,
......@@ -76,6 +77,7 @@ class PaymentAppServiceBridge : public PaymentAppFactory::Delegate {
ContentPaymentRequestDelegate* GetPaymentRequestDelegate() const override;
void ShowProcessingSpinner() override;
PaymentRequestSpec* GetSpec() const override;
std::string GetTwaPackageName() const override;
void OnPaymentAppCreated(std::unique_ptr<PaymentApp> app) override;
void OnPaymentAppCreationError(const std::string& error_message) override;
bool SkipCreatingNativePaymentApps() const override;
......@@ -88,6 +90,7 @@ class PaymentAppServiceBridge : public PaymentAppFactory::Delegate {
content::RenderFrameHost* render_frame_host,
const GURL& top_origin,
PaymentRequestSpec* spec,
const std::string& twa_package_name,
scoped_refptr<PaymentManifestWebDataService> web_data_service,
bool may_crawl_for_installable_payment_apps,
CanMakePaymentCalculatedCallback can_make_payment_calculated_callback,
......@@ -102,6 +105,7 @@ class PaymentAppServiceBridge : public PaymentAppFactory::Delegate {
const GURL frame_origin_;
const url::Origin frame_security_origin_;
PaymentRequestSpec* spec_;
const std::string twa_package_name_;
scoped_refptr<PaymentManifestWebDataService>
payment_manifest_web_data_service_;
bool may_crawl_for_installable_payment_apps_;
......
......@@ -62,7 +62,8 @@ class MockApp : public PaymentApp {
MOCK_CONST_METHOD0(HandlesPayerPhone, bool());
};
class PaymentAppServiceBridgeUnitTest : public ::testing::Test {
class PaymentAppServiceBridgeUnitTest
: public ::testing::TestWithParam<std::string> {
public:
PaymentAppServiceBridgeUnitTest()
: top_origin_("https://shop.example"),
......@@ -90,7 +91,7 @@ class PaymentAppServiceBridgeUnitTest : public ::testing::Test {
scoped_refptr<PaymentManifestWebDataService> web_data_service_;
};
TEST_F(PaymentAppServiceBridgeUnitTest, Smoke) {
TEST_P(PaymentAppServiceBridgeUnitTest, Smoke) {
std::vector<mojom::PaymentMethodDataPtr> method_data;
method_data.push_back(MakePaymentMethodData("basic-card"));
method_data.push_back(MakePaymentMethodData("https://ph.example"));
......@@ -102,7 +103,7 @@ TEST_F(PaymentAppServiceBridgeUnitTest, Smoke) {
base::WeakPtr<PaymentAppServiceBridge> bridge =
PaymentAppServiceBridge::Create(
/*number_of_factories=*/3, web_contents_->GetMainFrame(), top_origin_,
&spec, web_data_service_,
&spec, /*twa_package_name=*/GetParam(), web_data_service_,
/*may_crawl_for_installable_payment_apps=*/true,
base::BindRepeating(&MockCallback::NotifyCanMakePaymentCalculated,
base::Unretained(&mock_callback)),
......@@ -147,4 +148,10 @@ TEST_F(PaymentAppServiceBridgeUnitTest, Smoke) {
CHECK_EQ(nullptr, bridge.get());
}
// An empty string indicates running outside of a TWA. A non-empty string is the
// package name of the TWA when running in a TWA mode.
INSTANTIATE_TEST_SUITE_P(WithAndWithoutTwaPackageName,
PaymentAppServiceBridgeUnitTest,
::testing::Values("", "com.example.twa.app"));
} // namespace payments
......@@ -77,8 +77,8 @@ public interface PaymentAppFactoryParams extends PaymentRequestParams {
}
/**
* @return The Android package name of the Trusted Web Activity that invoked Chrome, if any.
* Otherwise null or empty string.
* @return The Android package name of the Trusted Web Activity that invoked Chrome, if running
* in TWA mode. Otherwise null or empty string.
*/
@Nullable
default String getTwaPackageName() {
......
......@@ -57,6 +57,10 @@ class PaymentAppFactory {
virtual bool IsOffTheRecord() const = 0;
virtual PaymentRequestSpec* GetSpec() const = 0;
// Returns the Android package name of the Trusted Web Activity that invoked
// this browser, if any. Otherwise, an empty string.
virtual std::string GetTwaPackageName() const = 0;
// Tells the UI to show the processing spinner. Only desktop UI needs this
// notification.
virtual void ShowProcessingSpinner() = 0;
......
......@@ -111,6 +111,10 @@ PaymentRequestSpec* PaymentRequestState::GetSpec() const {
return spec_;
}
std::string PaymentRequestState::GetTwaPackageName() const {
return GetPaymentRequestDelegate()->GetTwaPackageName();
}
const GURL& PaymentRequestState::GetTopOrigin() {
return top_origin_;
}
......
......@@ -132,6 +132,7 @@ class PaymentRequestState : public PaymentAppFactory::Delegate,
ContentPaymentRequestDelegate* GetPaymentRequestDelegate() const override;
void ShowProcessingSpinner() override;
PaymentRequestSpec* GetSpec() const override;
std::string GetTwaPackageName() const override;
const GURL& GetTopOrigin() override;
const GURL& GetFrameOrigin() override;
const url::Origin& GetFrameSecurityOrigin() override;
......
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