blob: 9753d08e9b6f2e305d2b9b58be6c061a3aa51681 [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"
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0713#include "components/payments/content/payment_request_display_manager.h"
mathpf1a7a3752017-03-15 11:23:3714#include "components/payments/content/payment_request_spec.h"
15#include "components/payments/content/payment_request_state.h"
sebsg20b49d7b2017-05-04 20:23:1716#include "components/payments/core/journey_logger.h"
krb7f6421b2016-11-18 17:46:2117#include "mojo/public/cpp/bindings/binding.h"
rouslan8fdbfb242017-03-15 01:12:4918#include "mojo/public/cpp/bindings/interface_request.h"
Blink Reformata30d4232018-04-07 15:31:0619#include "third_party/blink/public/platform/modules/payments/payment_request.mojom.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3920#include "url/gurl.h"
krb7f6421b2016-11-18 17:46:2121
22namespace content {
rouslan690997682017-05-09 18:07:3923class RenderFrameHost;
krb7f6421b2016-11-18 17:46:2124class WebContents;
Rouslan Solomakhin115f7232017-08-01 15:24:3825} // namespace content
krb7f6421b2016-11-18 17:46:2126
27namespace payments {
28
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5129class ContentPaymentRequestDelegate;
mathpf709499d2017-01-09 20:48:3630class PaymentRequestWebContentsManager;
31
mathpf1a7a3752017-03-15 11:23:3732// This class manages the interaction between the renderer (through the
33// PaymentRequestClient and Mojo stub implementation) and the UI (through the
34// PaymentRequestDelegate). The API user (merchant) specification (supported
35// payment methods, required information, order details) is stored in
36// PaymentRequestSpec, and the current user selection state (and related data)
37// is stored in PaymentRequestSpec.
mathp4b85b582017-03-08 21:07:1638class PaymentRequest : public mojom::PaymentRequest,
mathpf1a7a3752017-03-15 11:23:3739 public PaymentRequestSpec::Observer,
40 public PaymentRequestState::Delegate {
krb7f6421b2016-11-18 17:46:2141 public:
mathp300fa542017-03-27 19:29:3742 class ObserverForTest {
43 public:
44 virtual void OnCanMakePaymentCalled() = 0;
gogerald8189d522017-09-15 17:52:1845 virtual void OnCanMakePaymentReturned() = 0;
rouslan6e3cf7c62017-04-17 21:23:2846 virtual void OnNotSupportedError() = 0;
rouslanb28f4532017-05-08 15:41:4747 virtual void OnConnectionTerminated() = 0;
Anthony Vallee-Dubois6813c1442017-05-17 19:32:5648 virtual void OnAbortCalled() = 0;
mathp300fa542017-03-27 19:29:3749
50 protected:
51 virtual ~ObserverForTest() {}
52 };
53
rouslan690997682017-05-09 18:07:3954 PaymentRequest(content::RenderFrameHost* render_frame_host,
55 content::WebContents* web_contents,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5156 std::unique_ptr<ContentPaymentRequestDelegate> delegate,
rouslan8fdbfb242017-03-15 01:12:4957 PaymentRequestWebContentsManager* manager,
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0758 PaymentRequestDisplayManager* display_manager,
mathp300fa542017-03-27 19:29:3759 mojo::InterfaceRequest<mojom::PaymentRequest> request,
60 ObserverForTest* observer_for_testing);
mathpf709499d2017-01-09 20:48:3661 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2162
rouslan8fdbfb242017-03-15 01:12:4963 // mojom::PaymentRequest
64 void Init(mojom::PaymentRequestClientPtr client,
65 std::vector<mojom::PaymentMethodDataPtr> method_data,
66 mojom::PaymentDetailsPtr details,
67 mojom::PaymentOptionsPtr options) override;
Rouslan Solomakhin833f8512018-04-03 23:19:2568 void Show(bool is_user_gesture) override;
Jinho Bangbe463a22018-08-02 10:26:5069 void Retry(mojom::PaymentValidationErrorsPtr errors) override;
mathp151bd312017-04-03 21:07:2470 void UpdateWith(mojom::PaymentDetailsPtr details) override;
Rouslan Solomakhina9ff9282017-10-31 21:58:0571 void NoUpdatedPaymentDetails() override;
mathpf4bc50e2017-01-24 05:17:5072 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4973 void Complete(mojom::PaymentComplete result) override;
mathp4b85b582017-03-08 21:07:1674 void CanMakePayment() override;
75
mathpf1a7a3752017-03-15 11:23:3776 // PaymentRequestSpec::Observer:
mathp151bd312017-04-03 21:07:2477 void OnSpecUpdated() override {}
mathpf1a7a3752017-03-15 11:23:3778
79 // PaymentRequestState::Delegate:
mathpf1a7a3752017-03-15 11:23:3780 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
mathp151bd312017-04-03 21:07:2481 void OnShippingOptionIdSelected(std::string shipping_option_id) override;
82 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override;
krb7f6421b2016-11-18 17:46:2183
Rouslan Solomakhin16ee55202017-10-03 19:04:4284 // Called when the user explicitly cancelled the flow. Will send a message
mathpf4bc50e2017-01-24 05:17:5085 // to the renderer which will indirectly destroy this object (through
86 // OnConnectionTerminated).
87 void UserCancelled();
88
sebsgd56b3e422017-10-20 18:08:0889 // Called when the main frame attached to this PaymentRequest is navigating to
90 // another document, but before the PaymentRequest is destroyed.
91 void DidStartMainFrameNavigationToDifferentDocument(bool is_user_initiated);
sebsg2c8558a2017-05-17 18:54:1092
mathpf4bc50e2017-01-24 05:17:5093 // As a result of a browser-side error or renderer-initiated mojo channel
94 // closure (e.g. there was an error on the renderer side, or payment was
95 // successful), this method is called. It is responsible for cleaning up,
96 // such as possibly closing the dialog.
97 void OnConnectionTerminated();
mathp6758be032017-01-13 04:49:5098
mathpd4be8de82017-03-01 00:51:4899 // Called when the user clicks on the "Pay" button.
100 void Pay();
101
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07102 // Hide this Payment Request if it's already showing.
103 void HideIfNecessary();
104
Rouslan Solomakhind2cae95a2018-08-09 00:16:10105 // Record the "dialog shown" event in the journey logger.
106 void RecordDialogShownEventInJourneyLogger();
107
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04108 bool IsIncognito() const;
109
anthonyvd3d7f9722016-12-07 18:43:54110 content::WebContents* web_contents() { return web_contents_; }
111
gogerald2ddfc64c2018-08-23 16:20:21112 bool skipped_payment_request_ui() { return skipped_payment_request_ui_; }
113
mathpf1a7a3752017-03-15 11:23:37114 PaymentRequestSpec* spec() { return spec_.get(); }
115 PaymentRequestState* state() { return state_.get(); }
mathpd4be8de82017-03-01 00:51:48116
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:36117 PaymentRequestSpec* spec() const { return spec_.get(); }
118 PaymentRequestState* state() const { return state_.get(); }
119
krb7f6421b2016-11-18 17:46:21120 private:
gogerald2ddfc64c2018-08-23 16:20:21121 // Returns true if this payment request supports skipping the Payment Sheet.
122 // Typically, this means only one payment method is supported, it's a URL
123 // based method, and no other info is requested from the user.
124 bool SatisfiesSkipUIConstraints() const;
125
sebsgfcdd13c2017-06-08 15:49:33126 // Only records the abort reason if it's the first completion for this Payment
127 // Request. This is necessary since the aborts cascade into one another with
128 // the first one being the most precise.
129 void RecordFirstAbortReason(JourneyLogger::AbortReason completion_status);
sebsg2c8558a2017-05-17 18:54:10130
gogerald0a7ee6c2017-11-13 18:23:19131 // The callback for PaymentRequestState::CanMakePayment. Checks for query
132 // quota and may send QUERY_QUOTA_EXCEEDED.
gogerald8189d522017-09-15 17:52:18133 void CanMakePaymentCallback(bool can_make_payment);
134
gogerald0a7ee6c2017-11-13 18:23:19135 // The callback for PaymentRequestState::AreRequestedMethodsSupported.
136 void AreRequestedMethodsSupportedCallback(bool methods_supported);
137
Rouslan Solomakhin1804ee42017-10-03 14:27:43138 // Sends either CAN_MAKE_PAYMENT or CANNOT_MAKE_PAYMENT to the renderer,
139 // depending on |can_make_payment| value. Never sends QUERY_QUOTA_EXCEEDED.
Rouslan Solomakhin16ee55202017-10-03 19:04:42140 // Does not check query quota, but does check for incognito mode. If
141 // |warn_localhost_or_file| is true, then sends WARNING_CAN_MAKE_PAYMENT or
142 // WARNING_CANNOT_MAKE_PAYMENT version of the values instead.
143 void RespondToCanMakePaymentQuery(bool can_make_payment,
144 bool warn_localhost_or_file);
Rouslan Solomakhin1804ee42017-10-03 14:27:43145
krb7f6421b2016-11-18 17:46:21146 content::WebContents* web_contents_;
Rouslan Solomakhin4eea9bc22017-10-10 15:18:51147 std::unique_ptr<ContentPaymentRequestDelegate> delegate_;
mathpf709499d2017-01-09 20:48:36148 // |manager_| owns this PaymentRequest.
149 PaymentRequestWebContentsManager* manager_;
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07150 PaymentRequestDisplayManager* display_manager_;
151 std::unique_ptr<PaymentRequestDisplayManager::DisplayHandle> display_handle_;
rouslan8fdbfb242017-03-15 01:12:49152 mojo::Binding<mojom::PaymentRequest> binding_;
153 mojom::PaymentRequestClientPtr client_;
mathpd4be8de82017-03-01 00:51:48154
mathpf1a7a3752017-03-15 11:23:37155 std::unique_ptr<PaymentRequestSpec> spec_;
156 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54157
Rouslan Solomakhin115f7232017-08-01 15:24:38158 // The RFC 6454 origin of the top level frame that has invoked PaymentRequest
159 // API. This is what the user sees in the address bar.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39160 const GURL top_level_origin_;
Rouslan Solomakhin115f7232017-08-01 15:24:38161
rouslan690997682017-05-09 18:07:39162 // The RFC 6454 origin of the frame that has invoked PaymentRequest API. This
163 // can be either the main frame or an iframe.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39164 const GURL frame_origin_;
rouslan690997682017-05-09 18:07:39165
mathp300fa542017-03-27 19:29:37166 // May be null, must outlive this object.
167 ObserverForTest* observer_for_testing_;
168
sebsg20b49d7b2017-05-04 20:23:17169 JourneyLogger journey_logger_;
170
sebsgfcdd13c2017-06-08 15:49:33171 // Whether a completion was already recorded for this Payment Request.
172 bool has_recorded_completion_ = false;
sebsg2c8558a2017-05-17 18:54:10173
Rouslan Solomakhin833f8512018-04-03 23:19:25174 // Whether PaymentRequest.show() was invoked with a user gesture.
175 bool is_show_user_gesture_ = false;
176
gogerald2ddfc64c2018-08-23 16:20:21177 // Whether PaymentRequest.show() was invoked by skipping payment request UI.
178 bool skipped_payment_request_ui_ = false;
179
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:13180 base::WeakPtrFactory<PaymentRequest> weak_ptr_factory_;
181
mathpf709499d2017-01-09 20:48:36182 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21183};
184
185} // namespace payments
186
rouslan908248c2017-02-27 21:30:24187#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_