1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161

content / browser / payments / respond_with_callback.h [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_PAYMENTS_RESPOND_WITH_CALLBACK_H_
#define CONTENT_BROWSER_PAYMENTS_RESPOND_WITH_CALLBACK_H_

#include "base/functional/callback_forward.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/payment_app_provider.h"
#include "content/public/browser/payment_app_provider_util.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "third_party/blink/public/mojom/payments/payment_app.mojom.h"

namespace content {

class PaymentEventDispatcher;
enum class RespondWithCallbackType { kInvoke, kAbort, kCanMakePayment };

// Abstract base class for event callbacks that are invoked when the payment
// handler resolves the promise passed in to TheEvent.respondWith() method.
class RespondWithCallback
    : public payments::mojom::PaymentHandlerResponseCallback {
 public:
  // Disallow copy and assign.
  RespondWithCallback(const RespondWithCallback& other) = delete;
  RespondWithCallback& operator=(const RespondWithCallback& other) = delete;

  mojo::PendingRemote<payments::mojom::PaymentHandlerResponseCallback>
  BindNewPipeAndPassRemote();

 protected:
  RespondWithCallback(
      ServiceWorkerMetrics::EventType event_type,
      scoped_refptr<ServiceWorkerVersion> service_worker_version,
      base::WeakPtr<PaymentEventDispatcher> event_dispatcher);

  ~RespondWithCallback() override;

  // payments::mojom::PaymentHandlerResponseCallback implementation.
  void OnResponseForCanMakePayment(
      payments::mojom::CanMakePaymentResponsePtr response) override {}

  // payments::mojom::PaymentHandlerResponseCallback implementation.
  void OnResponseForPaymentRequest(
      payments::mojom::PaymentHandlerResponsePtr response) override {}

  // payments::mojom::PaymentHandlerResponseCallback implementation.
  void OnResponseForAbortPayment(bool payment_aborted) override {}

  virtual void OnServiceWorkerError(
      blink::ServiceWorkerStatusCode service_worker_status) = 0;

  void FinishServiceWorkerRequest();
  void ClearRespondWithCallbackAndCloseWindow();

 private:
  int request_id_;
  scoped_refptr<ServiceWorkerVersion> service_worker_version_;
  base::WeakPtr<PaymentEventDispatcher> event_dispatcher_;
  mojo::Receiver<payments::mojom::PaymentHandlerResponseCallback> receiver_{
      this};

  base::WeakPtrFactory<RespondWithCallback> weak_ptr_factory_{this};
};

// Self-deleting callback for "canmakepayment" event. Invoked when the payment
// handler resolves the promise passed into CanMakePaymentEvent.respondWith()
// method.
class CanMakePaymentRespondWithCallback : public RespondWithCallback {
 public:
  CanMakePaymentRespondWithCallback(
      scoped_refptr<ServiceWorkerVersion> service_worker_version,
      base::WeakPtr<PaymentEventDispatcher> event_dispatcher,
      PaymentAppProvider::CanMakePaymentCallback callback);
  ~CanMakePaymentRespondWithCallback() override;

  // Disallow copy and assign.
  CanMakePaymentRespondWithCallback(
      const CanMakePaymentRespondWithCallback& other) = delete;
  CanMakePaymentRespondWithCallback& operator=(
      const CanMakePaymentRespondWithCallback& other) = delete;

 private:
  // payments::mojom::PaymentHandlerResponseCallback implementation.
  void OnResponseForCanMakePayment(
      payments::mojom::CanMakePaymentResponsePtr response) override;

  // RespondWithCallback implementation.
  void OnServiceWorkerError(
      blink::ServiceWorkerStatusCode service_worker_status) override;

  PaymentAppProvider::CanMakePaymentCallback callback_;
};

// Self-deleting callback for "paymentrequest" event. Invoked when the payment
// handler resolves the promise passed into PaymentRequestEvent.respondWith()
// method.
class InvokeRespondWithCallback : public RespondWithCallback {
 public:
  InvokeRespondWithCallback(
      scoped_refptr<ServiceWorkerVersion> service_worker_version,
      base::WeakPtr<PaymentEventDispatcher> event_dispatcher,
      PaymentAppProvider::InvokePaymentAppCallback callback);
  ~InvokeRespondWithCallback() override;

  // Disallow copy and assign.
  InvokeRespondWithCallback(const InvokeRespondWithCallback& other) = delete;
  InvokeRespondWithCallback& operator=(const InvokeRespondWithCallback& other) =
      delete;

  // Called only for "paymentrequest" event.
  void AbortPaymentSinceOpennedWindowClosing(
      payments::mojom::PaymentEventResponseType reason);

 private:
  // payments::mojom::PaymentHandlerResponseCallback implementation.
  void OnResponseForPaymentRequest(
      payments::mojom::PaymentHandlerResponsePtr response) override;

  // RespondWithCallback implementation.
  void OnServiceWorkerError(
      blink::ServiceWorkerStatusCode service_worker_status) override;

  void RespondToPaymentRequestWithErrorAndDeleteSelf(
      payments::mojom::PaymentEventResponseType response_type);

  PaymentAppProvider::InvokePaymentAppCallback callback_;
};

// Self-deleting callback for "abortpayment" event. Invoked when the payment
// handler resolves the promise passed into AbortPayment.respondWith() method.
class AbortRespondWithCallback : public RespondWithCallback {
 public:
  AbortRespondWithCallback(
      scoped_refptr<ServiceWorkerVersion> service_worker_version,
      base::WeakPtr<PaymentEventDispatcher> event_dispatcher,
      PaymentAppProvider::AbortCallback callback);
  ~AbortRespondWithCallback() override;

  // Disallow copy and assign.
  AbortRespondWithCallback(const AbortRespondWithCallback& other) = delete;
  AbortRespondWithCallback& operator=(const AbortRespondWithCallback& other) =
      delete;

 private:
  // payments::mojom::PaymentHandlerResponseCallback implementation.
  void OnResponseForAbortPayment(bool payment_aborted) override;

  // RespondWithCallback implementation.
  void OnServiceWorkerError(
      blink::ServiceWorkerStatusCode service_worker_status) override;

  PaymentAppProvider::AbortCallback callback_;
};

}  // namespace content.

#endif  // CONTENT_BROWSER_PAYMENTS_RESPOND_WITH_CALLBACK_H_