File: payment_handler_host.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (157 lines) | stat: -rw-r--r-- 5,831 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_HANDLER_HOST_H_
#define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_HANDLER_HOST_H_

#include <stdint.h>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "third_party/blink/public/mojom/payments/payment_handler_host.mojom.h"
#include "url/origin.h"

namespace content {
class WebContents;
}  // namespace content

namespace payments {

using ChangePaymentRequestDetailsCallback =
    base::OnceCallback<void(mojom::PaymentRequestDetailsUpdatePtr)>;

// Handles the communication from the payment handler renderer process to the
// merchant renderer process.
class PaymentHandlerHost : public mojom::PaymentHandlerHost {
 public:
  // The interface to be implemented by the object that can communicate to the
  // merchant's renderer process.
  class Delegate {
   public:
    virtual ~Delegate() = default;

    // Notifies the merchant that the payment method has changed. Returns
    // "false" if the state is invalid.
    virtual bool ChangePaymentMethod(const std::string& method_name,
                                     const std::string& stringified_data) = 0;

    // Notifies the merchant that the shipping option has changed. Returns
    // "false" if the state is invalid.
    virtual bool ChangeShippingOption(
        const std::string& shipping_option_id) = 0;

    // Notifies the merchant that the shipping address has changed after
    // redacting the address whenever needed. Returns "false" if the state is
    // invalid.
    virtual bool ChangeShippingAddress(
        mojom::PaymentAddressPtr shipping_address) = 0;
  };

  // The |delegate| cannot be null and must outlive this object. Typically this
  // is accomplished by the |delegate| owning this object. The |web_contents| is
  // used for developer tools logging and should be from the same browser
  // context as the payment handler.
  PaymentHandlerHost(content::WebContents* web_contents,
                     base::WeakPtr<Delegate> delegate);

  PaymentHandlerHost(const PaymentHandlerHost&) = delete;
  PaymentHandlerHost& operator=(const PaymentHandlerHost&) = delete;

  ~PaymentHandlerHost() override;

  // Sets the origin of the payment handler / service worker registration scope.
  // Used for developer tools logging.
  void set_sw_origin_for_logs(const url::Origin& origin) {
    sw_origin_for_logs_ = origin;
  }

  // Sets the registration identifier of the payment handler / service worker.
  // Used for developer tools logging.
  void set_registration_id_for_logs(int64_t id) {
    registration_id_for_logs_ = id;
  }

  // Sets the identifier for the Payment Request object. Used for developer
  // tools logging.
  void set_payment_request_id_for_logs(const std::string& id) {
    payment_request_id_for_logs_ = id;
  }

  // Returns "true" when the payment handler has changed any of the payment
  // method, shipping address or shipping option, but has not received the
  // response from the merchant yet.
  bool is_waiting_for_payment_details_update() const {
    return !!change_payment_request_details_callback_;
  }

  // Binds to an IPC endpoint and returns it.
  mojo::PendingRemote<mojom::PaymentHandlerHost> Bind();

  // Notifies the payment handler of the updated details, such as updated total,
  // in response to the change of any of the following: payment method, shipping
  // address, or shipping option.
  void UpdateWith(mojom::PaymentRequestDetailsUpdatePtr response);

  // Notifies the payment handler that the merchant did not handle the payment
  // method, shipping option, or shipping address change events, so the payment
  // details are unchanged.
  void OnPaymentDetailsNotUpdated();

  // Disconnects from the payment handler.
  void Disconnect();

  base::WeakPtr<PaymentHandlerHost> AsWeakPtr();

 private:
  // mojom::PaymentHandlerHost
  void ChangePaymentMethod(
      mojom::PaymentHandlerMethodDataPtr method_data,
      ChangePaymentRequestDetailsCallback callback) override;

  // mojom::PaymentHandlerHost
  void ChangeShippingOption(
      const std::string& shipping_option_id,
      ChangePaymentRequestDetailsCallback callback) override;

  // mojom::PaymentHandlerHost
  void ChangeShippingAddress(
      mojom::PaymentAddressPtr shipping_address,
      ChangePaymentRequestDetailsCallback callback) override;

  // Payment handler's callback to invoke after merchant responds to any of the
  // "payment method change", "shipping option change", or "shipping address
  // change" events.
  ChangePaymentRequestDetailsCallback change_payment_request_details_callback_;

  // The end-point for the payment handler renderer process to call into the
  // browser process.
  mojo::Receiver<mojom::PaymentHandlerHost> receiver_{this};

  // Not null and outlives this object. Either owns this object or is owned by
  // the owner of this object.
  base::WeakPtr<Delegate> delegate_;

  // The origin of the payment handler / service worker registration scope. Used
  // for developer tools logging.
  url::Origin sw_origin_for_logs_;

  // The registration identifier for the payment handler / service worker. Used
  // for developer tools logging.
  int64_t registration_id_for_logs_ = -1;

  // The identifier for the Payment Request object. Used for developer tools
  // logging.
  std::string payment_request_id_for_logs_;

  base::WeakPtr<content::WebContents> web_contents_;

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

}  // namespace payments

#endif  // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_HANDLER_HOST_H_