blob: ee8443a43d6c24869ed212d38b0447c6b7e1c1ad [file] [log] [blame]
krb7f6421b2016-11-18 17:46:211// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rouslan908248c2017-02-27 21:30:245#ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
6#define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_
mathpf709499d2017-01-09 20:48:367
8#include <memory>
tmartino68c0a272017-01-19 17:44:089#include <vector>
krb7f6421b2016-11-18 17:46:2110
rouslan908248c2017-02-27 21:30:2411#include "base/macros.h"
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1312#include "base/memory/weak_ptr.h"
Rouslan Solomakhin27064702018-12-14 21:15:3313#include "components/payments/content/developer_console_logger.h"
Rouslan Solomakhin6b115fb2020-10-07 21:05:1814#include "components/payments/content/initialization_task.h"
Rouslan Solomakhin8e9f149b22019-05-10 17:43:0215#include "components/payments/content/payment_handler_host.h"
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0716#include "components/payments/content/payment_request_display_manager.h"
mathpf1a7a3752017-03-15 11:23:3717#include "components/payments/content/payment_request_spec.h"
18#include "components/payments/content/payment_request_state.h"
Rouslan Solomakhin5d15cb1f2019-11-11 18:11:3919#include "components/payments/content/service_worker_payment_app.h"
sebsg20b49d7b2017-05-04 20:23:1720#include "components/payments/core/journey_logger.h"
Danyao Wang15840792020-05-07 23:52:1721#include "content/public/browser/global_routing_id.h"
Gyuyoung Kim9cfbda32019-08-27 02:15:1822#include "mojo/public/cpp/bindings/receiver.h"
23#include "mojo/public/cpp/bindings/remote.h"
Han Leonabbe7c902018-08-30 02:08:1024#include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3925#include "url/gurl.h"
Rouslan Solomakhinc71cca622020-01-31 22:15:3526#include "url/origin.h"
krb7f6421b2016-11-18 17:46:2127
28namespace content {
rouslan690997682017-05-09 18:07:3929class RenderFrameHost;
krb7f6421b2016-11-18 17:46:2130class WebContents;
Rouslan Solomakhin115f7232017-08-01 15:24:3831} // namespace content
krb7f6421b2016-11-18 17:46:2132
33namespace payments {
34
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5135class ContentPaymentRequestDelegate;
mathpf709499d2017-01-09 20:48:3636class PaymentRequestWebContentsManager;
37
mathpf1a7a3752017-03-15 11:23:3738// This class manages the interaction between the renderer (through the
Liquan (Max) Gub7fdf17e2020-06-19 21:38:2239// PaymentRequestClient and Mojo stub implementation) and the desktop Payment UI
40// (through the PaymentRequestDelegate). The API user (merchant) specification
41// (supported payment methods, required information, order details) is stored in
mathpf1a7a3752017-03-15 11:23:3742// PaymentRequestSpec, and the current user selection state (and related data)
Liquan (Max) Gub7fdf17e2020-06-19 21:38:2243// is stored in PaymentRequestState.
mathp4b85b582017-03-08 21:07:1644class PaymentRequest : public mojom::PaymentRequest,
Rouslan Solomakhin8e9f149b22019-05-10 17:43:0245 public PaymentHandlerHost::Delegate,
mathpf1a7a3752017-03-15 11:23:3746 public PaymentRequestSpec::Observer,
Rouslan Solomakhin6b115fb2020-10-07 21:05:1847 public PaymentRequestState::Delegate,
48 public InitializationTask::Observer {
krb7f6421b2016-11-18 17:46:2149 public:
mathp300fa542017-03-27 19:29:3750 class ObserverForTest {
51 public:
52 virtual void OnCanMakePaymentCalled() = 0;
gogerald8189d522017-09-15 17:52:1853 virtual void OnCanMakePaymentReturned() = 0;
Danyao Wangce175bf2018-12-21 22:35:5854 virtual void OnHasEnrolledInstrumentCalled() = 0;
55 virtual void OnHasEnrolledInstrumentReturned() = 0;
Sahel Sharifyfd677ff2020-07-08 19:01:5256 virtual void OnAppListReady(base::WeakPtr<PaymentRequest> payment_request) {
57 }
rouslan6e3cf7c62017-04-17 21:23:2858 virtual void OnNotSupportedError() = 0;
rouslanb28f4532017-05-08 15:41:4759 virtual void OnConnectionTerminated() = 0;
Anthony Vallee-Dubois6813c1442017-05-17 19:32:5660 virtual void OnAbortCalled() = 0;
Sahel Sharify2a56b5f2019-11-28 19:27:1961 virtual void OnCompleteCalled() {}
mathp300fa542017-03-27 19:29:3762
63 protected:
64 virtual ~ObserverForTest() {}
65 };
66
rouslan690997682017-05-09 18:07:3967 PaymentRequest(content::RenderFrameHost* render_frame_host,
68 content::WebContents* web_contents,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5169 std::unique_ptr<ContentPaymentRequestDelegate> delegate,
rouslan8fdbfb242017-03-15 01:12:4970 PaymentRequestWebContentsManager* manager,
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0771 PaymentRequestDisplayManager* display_manager,
Gyuyoung Kim9cfbda32019-08-27 02:15:1872 mojo::PendingReceiver<mojom::PaymentRequest> receiver,
mathp300fa542017-03-27 19:29:3773 ObserverForTest* observer_for_testing);
mathpf709499d2017-01-09 20:48:3674 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2175
rouslan8fdbfb242017-03-15 01:12:4976 // mojom::PaymentRequest
Gyuyoung Kim9cfbda32019-08-27 02:15:1877 void Init(mojo::PendingRemote<mojom::PaymentRequestClient> client,
rouslan8fdbfb242017-03-15 01:12:4978 std::vector<mojom::PaymentMethodDataPtr> method_data,
79 mojom::PaymentDetailsPtr details,
80 mojom::PaymentOptionsPtr options) override;
Rouslan Solomakhin9788d4b2019-04-09 13:10:2381 void Show(bool is_user_gesture, bool wait_for_updated_details) override;
Jinho Bangbe463a22018-08-02 10:26:5082 void Retry(mojom::PaymentValidationErrorsPtr errors) override;
mathp151bd312017-04-03 21:07:2483 void UpdateWith(mojom::PaymentDetailsPtr details) override;
Sahel Sharifye2f889542019-12-02 22:17:4884 void OnPaymentDetailsNotUpdated() override;
mathpf4bc50e2017-01-24 05:17:5085 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4986 void Complete(mojom::PaymentComplete result) override;
Danyao Wang03a4cbd2019-08-15 23:47:1187 void CanMakePayment() override;
Dongjun Kim0a7565b2020-08-18 09:39:4688 void HasEnrolledInstrument() override;
mathp4b85b582017-03-08 21:07:1689
Rouslan Solomakhin8e9f149b22019-05-10 17:43:0290 // PaymentHandlerHost::Delegate
91 bool ChangePaymentMethod(const std::string& method_name,
92 const std::string& stringified_data) override;
Sahel Sharify9d98a502019-09-30 19:58:3993 bool ChangeShippingOption(const std::string& shipping_option_id) override;
94 bool ChangeShippingAddress(
95 mojom::PaymentAddressPtr shipping_address) override;
Rouslan Solomakhina480efa2019-05-06 15:37:2296
mathpf1a7a3752017-03-15 11:23:3797 // PaymentRequestSpec::Observer:
mathp151bd312017-04-03 21:07:2498 void OnSpecUpdated() override {}
mathpf1a7a3752017-03-15 11:23:3799
100 // PaymentRequestState::Delegate:
mathpf1a7a3752017-03-15 11:23:37101 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
Rouslan Solomakhin68429b72019-06-27 15:12:39102 void OnPaymentResponseError(const std::string& error_message) override;
mathp151bd312017-04-03 21:07:24103 void OnShippingOptionIdSelected(std::string shipping_option_id) override;
104 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override;
Jinho Bangbb178152018-09-13 09:44:43105 void OnPayerInfoSelected(mojom::PayerDetailPtr payer_info) override;
krb7f6421b2016-11-18 17:46:21106
Rouslan Solomakhin16ee55202017-10-03 19:04:42107 // Called when the user explicitly cancelled the flow. Will send a message
mathpf4bc50e2017-01-24 05:17:50108 // to the renderer which will indirectly destroy this object (through
109 // OnConnectionTerminated).
110 void UserCancelled();
111
sebsgd56b3e422017-10-20 18:08:08112 // Called when the main frame attached to this PaymentRequest is navigating to
113 // another document, but before the PaymentRequest is destroyed.
114 void DidStartMainFrameNavigationToDifferentDocument(bool is_user_initiated);
sebsg2c8558a2017-05-17 18:54:10115
Danyao Wang5c38b302020-08-26 12:08:01116 // Called when the frame attached to this PaymentRequest is about to be
117 // destroyed. This is used to clean up before the RenderFrameHost is actually
118 // destroyed because some objects held by the PaymentRequest (e.g.
119 // InternalAuthenticator) must be out-lived by the RenderFrameHost.
120 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host);
121
mathpf4bc50e2017-01-24 05:17:50122 // As a result of a browser-side error or renderer-initiated mojo channel
123 // closure (e.g. there was an error on the renderer side, or payment was
124 // successful), this method is called. It is responsible for cleaning up,
125 // such as possibly closing the dialog.
126 void OnConnectionTerminated();
mathp6758be032017-01-13 04:49:50127
mathpd4be8de82017-03-01 00:51:48128 // Called when the user clicks on the "Pay" button.
129 void Pay();
130
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07131 // Hide this Payment Request if it's already showing.
132 void HideIfNecessary();
133
Ramin Halavatifbfcef672020-05-21 16:33:34134 bool IsOffTheRecord() const;
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04135
Sahel Sharify54f5e862020-03-26 20:02:41136 // Called when the payment handler requests to open a payment handler window.
137 void OnPaymentHandlerOpenWindowCalled();
138
anthonyvd3d7f9722016-12-07 18:43:54139 content::WebContents* web_contents() { return web_contents_; }
140
Danyao Wang5c38b302020-08-26 12:08:01141 const content::GlobalFrameRoutingId& initiator_frame_routing_id() {
142 return initiator_frame_routing_id_;
143 }
144
gogerald2ddfc64c2018-08-23 16:20:21145 bool skipped_payment_request_ui() { return skipped_payment_request_ui_; }
Rouslan Solomakhin1dca2a922019-09-06 22:25:07146 bool is_show_user_gesture() const { return is_show_user_gesture_; }
gogerald2ddfc64c2018-08-23 16:20:21147
DongJun Kim54690d52020-09-26 12:07:54148 base::WeakPtr<PaymentRequestSpec> spec() { return spec_->AsWeakPtr(); }
149 base::WeakPtr<PaymentRequestState> state() { return state_->AsWeakPtr(); }
mathpd4be8de82017-03-01 00:51:48150
DongJun Kim54690d52020-09-26 12:07:54151 base::WeakPtr<PaymentRequestSpec> spec() const { return spec_->AsWeakPtr(); }
152 base::WeakPtr<PaymentRequestState> state() const {
153 return state_->AsWeakPtr();
154 }
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:36155
Rouslan Solomakhine37ecbd02020-07-29 20:35:42156 base::WeakPtr<PaymentRequest> GetWeakPtr();
157
krb7f6421b2016-11-18 17:46:21158 private:
Rouslan Solomakhin6b115fb2020-10-07 21:05:18159 // InitializationTask::Observer.
160 void OnInitialized(InitializationTask* initialization_task) override;
161
Rouslan Solomakhin27064702018-12-14 21:15:33162 // Returns true after init() has been called and the mojo connection has been
163 // established. If the mojo connection gets later disconnected, this will
164 // returns false.
165 bool IsInitialized() const;
166
167 // Returns true after show() has been called and the payment sheet is showing.
168 // If the payment sheet is later hidden, this will return false.
169 bool IsThisPaymentRequestShowing() const;
170
Sahel Sharify9120fd32019-12-05 17:16:11171 // Returns true when there is exactly one available payment app which can
172 // provide all requested information including shipping address and payer's
173 // contact information whenever needed.
174 bool OnlySingleAppCanProvideAllRequiredInformation() const;
175
gogerald2ddfc64c2018-08-23 16:20:21176 // Returns true if this payment request supports skipping the Payment Sheet.
Sahel Sharify9120fd32019-12-05 17:16:11177 // Typically, this means that exactly one payment app can provide requested
178 // information.
Sahel Sharifyd3f1bc82019-05-21 18:48:46179 bool SatisfiesSkipUIConstraints();
gogerald2ddfc64c2018-08-23 16:20:21180
sebsgfcdd13c2017-06-08 15:49:33181 // Only records the abort reason if it's the first completion for this Payment
182 // Request. This is necessary since the aborts cascade into one another with
183 // the first one being the most precise.
184 void RecordFirstAbortReason(JourneyLogger::AbortReason completion_status);
sebsg2c8558a2017-05-17 18:54:10185
Danyao Wang03a4cbd2019-08-15 23:47:11186 // The callback for PaymentRequestState::CanMakePayment.
187 void CanMakePaymentCallback(bool can_make_payment);
gogerald8189d522017-09-15 17:52:18188
Danyao Wangce175bf2018-12-21 22:35:58189 // The callback for PaymentRequestState::HasEnrolledInstrument. Checks for
190 // query quota and may send QUERY_QUOTA_EXCEEDED.
Dongjun Kim0a7565b2020-08-18 09:39:46191 void HasEnrolledInstrumentCallback(bool has_enrolled_instrument);
Danyao Wangce175bf2018-12-21 22:35:58192
gogerald0a7ee6c2017-11-13 18:23:19193 // The callback for PaymentRequestState::AreRequestedMethodsSupported.
Rouslan Solomakhind5dcc322019-07-11 21:47:20194 void AreRequestedMethodsSupportedCallback(bool methods_supported,
195 const std::string& error_message);
gogerald0a7ee6c2017-11-13 18:23:19196
Danyao Wangce175bf2018-12-21 22:35:58197 // Sends either HAS_ENROLLED_INSTRUMENT or HAS_NO_ENROLLED_INSTRUMENT to the
Danyao Wang4bc0606a2018-12-27 16:54:53198 // renderer, depending on |has_enrolled_instrument| value. Does not check
199 // query quota so never sends QUERY_QUOTA_EXCEEDED. If
200 // |warn_localhost_or_file| is true, then sends
Danyao Wangce175bf2018-12-21 22:35:58201 // WARNING_HAS_ENROLLED_INSTRUMENT or WARNING_HAS_NO_ENROLLED_INSTRUMENT
202 // version of the values instead.
203 void RespondToHasEnrolledInstrumentQuery(bool has_enrolled_instrument,
204 bool warn_localhost_or_file);
205
Rouslan Solomakhin13947ae32020-05-22 15:34:29206 void OnAbortResult(bool aborted);
207
krb7f6421b2016-11-18 17:46:21208 content::WebContents* web_contents_;
Danyao Wang15840792020-05-07 23:52:17209 const content::GlobalFrameRoutingId initiator_frame_routing_id_;
Rouslan Solomakhin27064702018-12-14 21:15:33210 DeveloperConsoleLogger log_;
Rouslan Solomakhin4eea9bc22017-10-10 15:18:51211 std::unique_ptr<ContentPaymentRequestDelegate> delegate_;
mathpf709499d2017-01-09 20:48:36212 // |manager_| owns this PaymentRequest.
213 PaymentRequestWebContentsManager* manager_;
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07214 PaymentRequestDisplayManager* display_manager_;
215 std::unique_ptr<PaymentRequestDisplayManager::DisplayHandle> display_handle_;
Gyuyoung Kim9cfbda32019-08-27 02:15:18216 mojo::Receiver<mojom::PaymentRequest> receiver_{this};
217 mojo::Remote<mojom::PaymentRequestClient> client_;
mathpd4be8de82017-03-01 00:51:48218
mathpf1a7a3752017-03-15 11:23:37219 std::unique_ptr<PaymentRequestSpec> spec_;
220 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54221
Rouslan Solomakhina480efa2019-05-06 15:37:22222 // The end-point for the payment handler renderer process to call into the
223 // browser process.
DongJun Kim54690d52020-09-26 12:07:54224 std::unique_ptr<PaymentHandlerHost> payment_handler_host_;
Rouslan Solomakhina480efa2019-05-06 15:37:22225
Rouslan Solomakhinc71cca622020-01-31 22:15:35226 // The scheme, host, and port of the top level frame that has invoked
227 // PaymentRequest API as formatted by
228 // url_formatter::FormatUrlForSecurityDisplay(). This is what the user sees in
229 // the address bar.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39230 const GURL top_level_origin_;
Rouslan Solomakhin115f7232017-08-01 15:24:38231
Rouslan Solomakhinc71cca622020-01-31 22:15:35232 // The scheme, host, and port of the frame that has invoked PaymentRequest API
233 // as formatted by url_formatter::FormatUrlForSecurityDisplay(). This can be
234 // either the main frame or an iframe.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39235 const GURL frame_origin_;
rouslan690997682017-05-09 18:07:39236
Rouslan Solomakhinc71cca622020-01-31 22:15:35237 // The security origin of the frame that has invoked PaymentRequest API. This
238 // can be opaque. Used by security features like 'Sec-Fetch-Site' and
239 // 'Cross-Origin-Resource-Policy'.
240 const url::Origin frame_security_origin_;
241
mathp300fa542017-03-27 19:29:37242 // May be null, must outlive this object.
243 ObserverForTest* observer_for_testing_;
244
sebsg20b49d7b2017-05-04 20:23:17245 JourneyLogger journey_logger_;
246
sebsgfcdd13c2017-06-08 15:49:33247 // Whether a completion was already recorded for this Payment Request.
248 bool has_recorded_completion_ = false;
sebsg2c8558a2017-05-17 18:54:10249
Rouslan Solomakhin833f8512018-04-03 23:19:25250 // Whether PaymentRequest.show() was invoked with a user gesture.
251 bool is_show_user_gesture_ = false;
252
gogerald2ddfc64c2018-08-23 16:20:21253 // Whether PaymentRequest.show() was invoked by skipping payment request UI.
254 bool skipped_payment_request_ui_ = false;
255
Rouslan Solomakhin27064702018-12-14 21:15:33256 // Whether PaymentRequest mojo connection has been initialized from the
257 // renderer.
258 bool is_initialized_ = false;
259
260 // Whether PaymentRequest.show() has been called.
261 bool is_show_called_ = false;
262
Rouslan Solomakhin55db8272019-06-25 18:16:28263 // If not empty, use this error message for rejecting PaymentRequest.show().
Rouslan Solomakhin68429b72019-06-27 15:12:39264 std::string reject_show_error_message_;
Rouslan Solomakhin55db8272019-06-25 18:16:28265
Jeremy Roman5c341f6d2019-07-15 15:56:10266 base::WeakPtrFactory<PaymentRequest> weak_ptr_factory_{this};
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:13267
mathpf709499d2017-01-09 20:48:36268 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21269};
270
271} // namespace payments
272
rouslan908248c2017-02-27 21:30:24273#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_