blob: 697f7196d1fb3d2160861709e8d36d4c281895ac [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 Solomakhin8e9f149b22019-05-10 17:43:0214#include "components/payments/content/payment_handler_host.h"
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0715#include "components/payments/content/payment_request_display_manager.h"
mathpf1a7a3752017-03-15 11:23:3716#include "components/payments/content/payment_request_spec.h"
17#include "components/payments/content/payment_request_state.h"
Rouslan Solomakhin5d15cb1f2019-11-11 18:11:3918#include "components/payments/content/service_worker_payment_app.h"
sebsg20b49d7b2017-05-04 20:23:1719#include "components/payments/core/journey_logger.h"
Danyao Wang15840792020-05-07 23:52:1720#include "content/public/browser/global_routing_id.h"
Gyuyoung Kim9cfbda32019-08-27 02:15:1821#include "mojo/public/cpp/bindings/receiver.h"
22#include "mojo/public/cpp/bindings/remote.h"
Han Leonabbe7c902018-08-30 02:08:1023#include "third_party/blink/public/mojom/payments/payment_request.mojom.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3924#include "url/gurl.h"
Rouslan Solomakhinc71cca622020-01-31 22:15:3525#include "url/origin.h"
krb7f6421b2016-11-18 17:46:2126
27namespace content {
rouslan690997682017-05-09 18:07:3928class RenderFrameHost;
krb7f6421b2016-11-18 17:46:2129class WebContents;
Rouslan Solomakhin115f7232017-08-01 15:24:3830} // namespace content
krb7f6421b2016-11-18 17:46:2131
32namespace payments {
33
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5134class ContentPaymentRequestDelegate;
mathpf709499d2017-01-09 20:48:3635class PaymentRequestWebContentsManager;
36
mathpf1a7a3752017-03-15 11:23:3737// This class manages the interaction between the renderer (through the
38// PaymentRequestClient and Mojo stub implementation) and the UI (through the
39// PaymentRequestDelegate). The API user (merchant) specification (supported
40// payment methods, required information, order details) is stored in
41// PaymentRequestSpec, and the current user selection state (and related data)
42// is stored in PaymentRequestSpec.
mathp4b85b582017-03-08 21:07:1643class PaymentRequest : public mojom::PaymentRequest,
Rouslan Solomakhin8e9f149b22019-05-10 17:43:0244 public PaymentHandlerHost::Delegate,
mathpf1a7a3752017-03-15 11:23:3745 public PaymentRequestSpec::Observer,
Rouslan Solomakhin6d4c1c272019-11-22 17:09:2746 public PaymentRequestState::Delegate {
krb7f6421b2016-11-18 17:46:2147 public:
mathp300fa542017-03-27 19:29:3748 class ObserverForTest {
49 public:
50 virtual void OnCanMakePaymentCalled() = 0;
gogerald8189d522017-09-15 17:52:1851 virtual void OnCanMakePaymentReturned() = 0;
Danyao Wangce175bf2018-12-21 22:35:5852 virtual void OnHasEnrolledInstrumentCalled() = 0;
53 virtual void OnHasEnrolledInstrumentReturned() = 0;
Rouslan Solomakhin5d15cb1f2019-11-11 18:11:3954 virtual void OnShowAppsReady() {}
rouslan6e3cf7c62017-04-17 21:23:2855 virtual void OnNotSupportedError() = 0;
rouslanb28f4532017-05-08 15:41:4756 virtual void OnConnectionTerminated() = 0;
Anthony Vallee-Dubois6813c1442017-05-17 19:32:5657 virtual void OnAbortCalled() = 0;
Sahel Sharify2a56b5f2019-11-28 19:27:1958 virtual void OnCompleteCalled() {}
mathp300fa542017-03-27 19:29:3759
60 protected:
61 virtual ~ObserverForTest() {}
62 };
63
rouslan690997682017-05-09 18:07:3964 PaymentRequest(content::RenderFrameHost* render_frame_host,
65 content::WebContents* web_contents,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5166 std::unique_ptr<ContentPaymentRequestDelegate> delegate,
rouslan8fdbfb242017-03-15 01:12:4967 PaymentRequestWebContentsManager* manager,
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0768 PaymentRequestDisplayManager* display_manager,
Gyuyoung Kim9cfbda32019-08-27 02:15:1869 mojo::PendingReceiver<mojom::PaymentRequest> receiver,
mathp300fa542017-03-27 19:29:3770 ObserverForTest* observer_for_testing);
mathpf709499d2017-01-09 20:48:3671 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2172
rouslan8fdbfb242017-03-15 01:12:4973 // mojom::PaymentRequest
Gyuyoung Kim9cfbda32019-08-27 02:15:1874 void Init(mojo::PendingRemote<mojom::PaymentRequestClient> client,
rouslan8fdbfb242017-03-15 01:12:4975 std::vector<mojom::PaymentMethodDataPtr> method_data,
76 mojom::PaymentDetailsPtr details,
77 mojom::PaymentOptionsPtr options) override;
Rouslan Solomakhin9788d4b2019-04-09 13:10:2378 void Show(bool is_user_gesture, bool wait_for_updated_details) override;
Jinho Bangbe463a22018-08-02 10:26:5079 void Retry(mojom::PaymentValidationErrorsPtr errors) override;
mathp151bd312017-04-03 21:07:2480 void UpdateWith(mojom::PaymentDetailsPtr details) override;
Sahel Sharifye2f889542019-12-02 22:17:4881 void OnPaymentDetailsNotUpdated() override;
mathpf4bc50e2017-01-24 05:17:5082 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4983 void Complete(mojom::PaymentComplete result) override;
Danyao Wang03a4cbd2019-08-15 23:47:1184 void CanMakePayment() override;
Rouslan Solomakhin5683eb282019-01-29 18:06:0385 void HasEnrolledInstrument(bool per_method_quota) override;
mathp4b85b582017-03-08 21:07:1686
Rouslan Solomakhin8e9f149b22019-05-10 17:43:0287 // PaymentHandlerHost::Delegate
88 bool ChangePaymentMethod(const std::string& method_name,
89 const std::string& stringified_data) override;
Sahel Sharify9d98a502019-09-30 19:58:3990 bool ChangeShippingOption(const std::string& shipping_option_id) override;
91 bool ChangeShippingAddress(
92 mojom::PaymentAddressPtr shipping_address) override;
Rouslan Solomakhina480efa2019-05-06 15:37:2293
mathpf1a7a3752017-03-15 11:23:3794 // PaymentRequestSpec::Observer:
mathp151bd312017-04-03 21:07:2495 void OnSpecUpdated() override {}
mathpf1a7a3752017-03-15 11:23:3796
97 // PaymentRequestState::Delegate:
mathpf1a7a3752017-03-15 11:23:3798 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
Rouslan Solomakhin68429b72019-06-27 15:12:3999 void OnPaymentResponseError(const std::string& error_message) override;
mathp151bd312017-04-03 21:07:24100 void OnShippingOptionIdSelected(std::string shipping_option_id) override;
101 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override;
Jinho Bangbb178152018-09-13 09:44:43102 void OnPayerInfoSelected(mojom::PayerDetailPtr payer_info) override;
krb7f6421b2016-11-18 17:46:21103
Rouslan Solomakhin16ee55202017-10-03 19:04:42104 // Called when the user explicitly cancelled the flow. Will send a message
mathpf4bc50e2017-01-24 05:17:50105 // to the renderer which will indirectly destroy this object (through
106 // OnConnectionTerminated).
107 void UserCancelled();
108
sebsgd56b3e422017-10-20 18:08:08109 // Called when the main frame attached to this PaymentRequest is navigating to
110 // another document, but before the PaymentRequest is destroyed.
111 void DidStartMainFrameNavigationToDifferentDocument(bool is_user_initiated);
sebsg2c8558a2017-05-17 18:54:10112
mathpf4bc50e2017-01-24 05:17:50113 // As a result of a browser-side error or renderer-initiated mojo channel
114 // closure (e.g. there was an error on the renderer side, or payment was
115 // successful), this method is called. It is responsible for cleaning up,
116 // such as possibly closing the dialog.
117 void OnConnectionTerminated();
mathp6758be032017-01-13 04:49:50118
mathpd4be8de82017-03-01 00:51:48119 // Called when the user clicks on the "Pay" button.
120 void Pay();
121
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07122 // Hide this Payment Request if it's already showing.
123 void HideIfNecessary();
124
Ramin Halavatifbfcef672020-05-21 16:33:34125 bool IsOffTheRecord() const;
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04126
Sahel Sharify54f5e862020-03-26 20:02:41127 // Called when the payment handler requests to open a payment handler window.
128 void OnPaymentHandlerOpenWindowCalled();
129
anthonyvd3d7f9722016-12-07 18:43:54130 content::WebContents* web_contents() { return web_contents_; }
131
gogerald2ddfc64c2018-08-23 16:20:21132 bool skipped_payment_request_ui() { return skipped_payment_request_ui_; }
Rouslan Solomakhin1dca2a922019-09-06 22:25:07133 bool is_show_user_gesture() const { return is_show_user_gesture_; }
gogerald2ddfc64c2018-08-23 16:20:21134
mathpf1a7a3752017-03-15 11:23:37135 PaymentRequestSpec* spec() { return spec_.get(); }
136 PaymentRequestState* state() { return state_.get(); }
mathpd4be8de82017-03-01 00:51:48137
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:36138 PaymentRequestSpec* spec() const { return spec_.get(); }
139 PaymentRequestState* state() const { return state_.get(); }
140
krb7f6421b2016-11-18 17:46:21141 private:
Rouslan Solomakhin27064702018-12-14 21:15:33142 // Returns true after init() has been called and the mojo connection has been
143 // established. If the mojo connection gets later disconnected, this will
144 // returns false.
145 bool IsInitialized() const;
146
147 // Returns true after show() has been called and the payment sheet is showing.
148 // If the payment sheet is later hidden, this will return false.
149 bool IsThisPaymentRequestShowing() const;
150
Sahel Sharify9120fd32019-12-05 17:16:11151 // Returns true when there is exactly one available payment app which can
152 // provide all requested information including shipping address and payer's
153 // contact information whenever needed.
154 bool OnlySingleAppCanProvideAllRequiredInformation() const;
155
gogerald2ddfc64c2018-08-23 16:20:21156 // Returns true if this payment request supports skipping the Payment Sheet.
Sahel Sharify9120fd32019-12-05 17:16:11157 // Typically, this means that exactly one payment app can provide requested
158 // information.
Sahel Sharifyd3f1bc82019-05-21 18:48:46159 bool SatisfiesSkipUIConstraints();
gogerald2ddfc64c2018-08-23 16:20:21160
sebsgfcdd13c2017-06-08 15:49:33161 // Only records the abort reason if it's the first completion for this Payment
162 // Request. This is necessary since the aborts cascade into one another with
163 // the first one being the most precise.
164 void RecordFirstAbortReason(JourneyLogger::AbortReason completion_status);
sebsg2c8558a2017-05-17 18:54:10165
Danyao Wang03a4cbd2019-08-15 23:47:11166 // The callback for PaymentRequestState::CanMakePayment.
167 void CanMakePaymentCallback(bool can_make_payment);
gogerald8189d522017-09-15 17:52:18168
Danyao Wangce175bf2018-12-21 22:35:58169 // The callback for PaymentRequestState::HasEnrolledInstrument. Checks for
170 // query quota and may send QUERY_QUOTA_EXCEEDED.
Rouslan Solomakhin5683eb282019-01-29 18:06:03171 void HasEnrolledInstrumentCallback(bool per_method_quota,
172 bool has_enrolled_instrument);
Danyao Wangce175bf2018-12-21 22:35:58173
gogerald0a7ee6c2017-11-13 18:23:19174 // The callback for PaymentRequestState::AreRequestedMethodsSupported.
Rouslan Solomakhind5dcc322019-07-11 21:47:20175 void AreRequestedMethodsSupportedCallback(bool methods_supported,
176 const std::string& error_message);
gogerald0a7ee6c2017-11-13 18:23:19177
Danyao Wangce175bf2018-12-21 22:35:58178 // Sends either HAS_ENROLLED_INSTRUMENT or HAS_NO_ENROLLED_INSTRUMENT to the
Danyao Wang4bc0606a2018-12-27 16:54:53179 // renderer, depending on |has_enrolled_instrument| value. Does not check
180 // query quota so never sends QUERY_QUOTA_EXCEEDED. If
181 // |warn_localhost_or_file| is true, then sends
Danyao Wangce175bf2018-12-21 22:35:58182 // WARNING_HAS_ENROLLED_INSTRUMENT or WARNING_HAS_NO_ENROLLED_INSTRUMENT
183 // version of the values instead.
184 void RespondToHasEnrolledInstrumentQuery(bool has_enrolled_instrument,
185 bool warn_localhost_or_file);
186
Rouslan Solomakhin13947ae32020-05-22 15:34:29187 void OnAbortResult(bool aborted);
188
krb7f6421b2016-11-18 17:46:21189 content::WebContents* web_contents_;
Danyao Wang15840792020-05-07 23:52:17190 const content::GlobalFrameRoutingId initiator_frame_routing_id_;
Rouslan Solomakhin27064702018-12-14 21:15:33191 DeveloperConsoleLogger log_;
Rouslan Solomakhin4eea9bc22017-10-10 15:18:51192 std::unique_ptr<ContentPaymentRequestDelegate> delegate_;
mathpf709499d2017-01-09 20:48:36193 // |manager_| owns this PaymentRequest.
194 PaymentRequestWebContentsManager* manager_;
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07195 PaymentRequestDisplayManager* display_manager_;
196 std::unique_ptr<PaymentRequestDisplayManager::DisplayHandle> display_handle_;
Gyuyoung Kim9cfbda32019-08-27 02:15:18197 mojo::Receiver<mojom::PaymentRequest> receiver_{this};
198 mojo::Remote<mojom::PaymentRequestClient> client_;
mathpd4be8de82017-03-01 00:51:48199
mathpf1a7a3752017-03-15 11:23:37200 std::unique_ptr<PaymentRequestSpec> spec_;
201 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54202
Rouslan Solomakhina480efa2019-05-06 15:37:22203 // The end-point for the payment handler renderer process to call into the
204 // browser process.
Rouslan Solomakhin8e9f149b22019-05-10 17:43:02205 PaymentHandlerHost payment_handler_host_;
Rouslan Solomakhina480efa2019-05-06 15:37:22206
Rouslan Solomakhinc71cca622020-01-31 22:15:35207 // The scheme, host, and port of the top level frame that has invoked
208 // PaymentRequest API as formatted by
209 // url_formatter::FormatUrlForSecurityDisplay(). This is what the user sees in
210 // the address bar.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39211 const GURL top_level_origin_;
Rouslan Solomakhin115f7232017-08-01 15:24:38212
Rouslan Solomakhinc71cca622020-01-31 22:15:35213 // The scheme, host, and port of the frame that has invoked PaymentRequest API
214 // as formatted by url_formatter::FormatUrlForSecurityDisplay(). This can be
215 // either the main frame or an iframe.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39216 const GURL frame_origin_;
rouslan690997682017-05-09 18:07:39217
Rouslan Solomakhinc71cca622020-01-31 22:15:35218 // The security origin of the frame that has invoked PaymentRequest API. This
219 // can be opaque. Used by security features like 'Sec-Fetch-Site' and
220 // 'Cross-Origin-Resource-Policy'.
221 const url::Origin frame_security_origin_;
222
mathp300fa542017-03-27 19:29:37223 // May be null, must outlive this object.
224 ObserverForTest* observer_for_testing_;
225
sebsg20b49d7b2017-05-04 20:23:17226 JourneyLogger journey_logger_;
227
sebsgfcdd13c2017-06-08 15:49:33228 // Whether a completion was already recorded for this Payment Request.
229 bool has_recorded_completion_ = false;
sebsg2c8558a2017-05-17 18:54:10230
Rouslan Solomakhin833f8512018-04-03 23:19:25231 // Whether PaymentRequest.show() was invoked with a user gesture.
232 bool is_show_user_gesture_ = false;
233
gogerald2ddfc64c2018-08-23 16:20:21234 // Whether PaymentRequest.show() was invoked by skipping payment request UI.
235 bool skipped_payment_request_ui_ = false;
236
Rouslan Solomakhin27064702018-12-14 21:15:33237 // Whether PaymentRequest mojo connection has been initialized from the
238 // renderer.
239 bool is_initialized_ = false;
240
241 // Whether PaymentRequest.show() has been called.
242 bool is_show_called_ = false;
243
Rouslan Solomakhin55db8272019-06-25 18:16:28244 // If not empty, use this error message for rejecting PaymentRequest.show().
Rouslan Solomakhin68429b72019-06-27 15:12:39245 std::string reject_show_error_message_;
Rouslan Solomakhin55db8272019-06-25 18:16:28246
Jeremy Roman5c341f6d2019-07-15 15:56:10247 base::WeakPtrFactory<PaymentRequest> weak_ptr_factory_{this};
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:13248
mathpf709499d2017-01-09 20:48:36249 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21250};
251
252} // namespace payments
253
rouslan908248c2017-02-27 21:30:24254#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_