Commit 47f109e4 authored by Rouslan Solomakhin's avatar Rouslan Solomakhin Committed by Commit Bot

[Web Payment] PaymentHandlerHost for all payment apps.

Before this patch, PaymentHandlerHost was passed only to
ServiceWorkerPaymentApps, which are based on payment handlers.
This prevented introducing more types of payment apps which may be based
on payment handler, such as a JNI wrapper on top of C++ payment app
interface.

This patch sends the PaymentHandlerHost to all payment apps, so it's up
to the payment app implementation to determine whether the Host should
be used.

After this patch, it's easier to add a JNI wrapper on top of a C++
payment app, regardless of whether that app is backed by a payment
handler.

Design: https://bit.ly/cross-platform-pay-app-factory

Bug: 1083242
Change-Id: Ibf785fd665691a402500705c5b6d39061c904f73
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2207290
Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarDanyao Wang <danyao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#770605}
parent 84743c7b
...@@ -2104,14 +2104,7 @@ public class PaymentRequestImpl ...@@ -2104,14 +2104,7 @@ public class PaymentRequestImpl
} }
} }
if (mInvokedPaymentApp instanceof ServiceWorkerPaymentApp) { mInvokedPaymentApp.setPaymentHandlerHost(getPaymentHandlerHost());
if (mPaymentHandlerHost == null) {
mPaymentHandlerHost = new PaymentHandlerHost(mWebContents, this /* delegate */);
}
((ServiceWorkerPaymentApp) mInvokedPaymentApp)
.setPaymentHandlerHost(mPaymentHandlerHost);
}
// Create payment options for the invoked payment app. // Create payment options for the invoked payment app.
PaymentOptions paymentOptions = new PaymentOptions(); PaymentOptions paymentOptions = new PaymentOptions();
...@@ -2151,6 +2144,13 @@ public class PaymentRequestImpl ...@@ -2151,6 +2144,13 @@ public class PaymentRequestImpl
return !isAutofillCard; return !isAutofillCard;
} }
private PaymentHandlerHost getPaymentHandlerHost() {
if (mPaymentHandlerHost == null) {
mPaymentHandlerHost = new PaymentHandlerHost(mWebContents, /*delegate=*/this);
}
return mPaymentHandlerHost;
}
@Override @Override
public void onDismiss() { public void onDismiss() {
mJourneyLogger.setAborted(AbortReason.ABORTED_BY_USER); mJourneyLogger.setAborted(AbortReason.ABORTED_BY_USER);
...@@ -2984,7 +2984,10 @@ public class PaymentRequestImpl ...@@ -2984,7 +2984,10 @@ public class PaymentRequestImpl
} }
mJourneyLogger.destroy(); mJourneyLogger.destroy();
if (mPaymentHandlerHost != null) mPaymentHandlerHost.destroy(); if (mPaymentHandlerHost != null) {
mPaymentHandlerHost.destroy();
mPaymentHandlerHost = null;
}
} }
private void closeClient() { private void closeClient() {
......
...@@ -199,12 +199,8 @@ public class ServiceWorkerPaymentApp extends PaymentApp { ...@@ -199,12 +199,8 @@ public class ServiceWorkerPaymentApp extends PaymentApp {
mUkmSourceId = 0; mUkmSourceId = 0;
} }
/** @Override
* Sets the endpoint for payment handler communication. Must be called before invoking this public void setPaymentHandlerHost(PaymentHandlerHost host) {
* payment handler.
* @param host The endpoint for payment handler communication. Should not be null.
*/
/* package */ void setPaymentHandlerHost(PaymentHandlerHost host) {
assert host != null; assert host != null;
mPaymentHandlerHost = host; mPaymentHandlerHost = host;
} }
......
...@@ -305,4 +305,11 @@ public abstract class PaymentApp extends EditableOption { ...@@ -305,4 +305,11 @@ public abstract class PaymentApp extends EditableOption {
public long getUkmSourceId() { public long getUkmSourceId() {
return 0; return 0;
} }
/**
* Sets the endpoint for payment handler communication. Must be called before invoking this
* payment app. Used only by payment apps that are backed by a payment handler.
* @param host The endpoint for payment handler communication. Should not be null.
*/
public void setPaymentHandlerHost(PaymentHandlerHost host) {}
} }
...@@ -765,10 +765,8 @@ void PaymentRequest::OnConnectionTerminated() { ...@@ -765,10 +765,8 @@ void PaymentRequest::OnConnectionTerminated() {
void PaymentRequest::Pay() { void PaymentRequest::Pay() {
journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED); journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
DCHECK(state_->selected_app()); DCHECK(state_->selected_app());
if (state_->selected_app()->type() == PaymentApp::Type::SERVICE_WORKER_APP) { state_->selected_app()->SetPaymentHandlerHost(
static_cast<ServiceWorkerPaymentApp*>(state_->selected_app()) payment_handler_host_.AsWeakPtr());
->set_payment_handler_host(payment_handler_host_.Bind());
}
state_->GeneratePaymentResponse(); state_->GeneratePaymentResponse();
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "components/payments/content/payment_event_response_util.h" #include "components/payments/content/payment_event_response_util.h"
#include "components/payments/content/payment_handler_host.h"
#include "components/payments/content/payment_request_converter.h" #include "components/payments/content/payment_request_converter.h"
#include "components/payments/core/features.h" #include "components/payments/core/features.h"
#include "components/payments/core/method_strings.h" #include "components/payments/core/method_strings.h"
...@@ -549,4 +550,9 @@ ukm::SourceId ServiceWorkerPaymentApp::UkmSourceId() { ...@@ -549,4 +550,9 @@ ukm::SourceId ServiceWorkerPaymentApp::UkmSourceId() {
return ukm_source_id_; return ukm_source_id_;
} }
void ServiceWorkerPaymentApp::SetPaymentHandlerHost(
base::WeakPtr<PaymentHandlerHost> payment_handler_host) {
payment_handler_host_ = payment_handler_host->Bind();
}
} // namespace payments } // namespace payments
...@@ -98,11 +98,8 @@ class ServiceWorkerPaymentApp : public PaymentApp { ...@@ -98,11 +98,8 @@ class ServiceWorkerPaymentApp : public PaymentApp {
bool HandlesPayerEmail() const override; bool HandlesPayerEmail() const override;
bool HandlesPayerPhone() const override; bool HandlesPayerPhone() const override;
ukm::SourceId UkmSourceId() override; ukm::SourceId UkmSourceId() override;
void SetPaymentHandlerHost(
void set_payment_handler_host( base::WeakPtr<PaymentHandlerHost> payment_handler_host) override;
mojo::PendingRemote<mojom::PaymentHandlerHost> payment_handler_host) {
payment_handler_host_ = std::move(payment_handler_host);
}
private: private:
friend class ServiceWorkerPaymentAppTest; friend class ServiceWorkerPaymentAppTest;
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
namespace payments { namespace payments {
class PaymentHandlerHost;
// Base class which represents a payment app in Payment Request. // Base class which represents a payment app in Payment Request.
class PaymentApp { class PaymentApp {
public: public:
...@@ -113,6 +115,11 @@ class PaymentApp { ...@@ -113,6 +115,11 @@ class PaymentApp {
virtual ukm::SourceId UkmSourceId(); virtual ukm::SourceId UkmSourceId();
// Optionally bind to the Mojo pipe for receiving events generated by the
// invoked payment handler.
virtual void SetPaymentHandlerHost(
base::WeakPtr<PaymentHandlerHost> payment_handler_host) {}
protected: protected:
PaymentApp(int icon_resource_id, Type type); PaymentApp(int icon_resource_id, Type type);
......
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