blob: 3d7fe46ee00096810760cb5dd13c6208577f4436 [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"
Rouslan Solomakhin2c88e232017-06-14 20:28:5219#include "third_party/WebKit/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;
mathp151bd312017-04-03 21:07:2469 void UpdateWith(mojom::PaymentDetailsPtr details) override;
Rouslan Solomakhina9ff9282017-10-31 21:58:0570 void NoUpdatedPaymentDetails() override;
mathpf4bc50e2017-01-24 05:17:5071 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4972 void Complete(mojom::PaymentComplete result) override;
mathp4b85b582017-03-08 21:07:1673 void CanMakePayment() override;
74
mathpf1a7a3752017-03-15 11:23:3775 // PaymentRequestSpec::Observer:
mathp151bd312017-04-03 21:07:2476 void OnSpecUpdated() override {}
mathpf1a7a3752017-03-15 11:23:3777
78 // PaymentRequestState::Delegate:
mathpf1a7a3752017-03-15 11:23:3779 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
mathp151bd312017-04-03 21:07:2480 void OnShippingOptionIdSelected(std::string shipping_option_id) override;
81 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override;
krb7f6421b2016-11-18 17:46:2182
Rouslan Solomakhin16ee55202017-10-03 19:04:4283 // Called when the user explicitly cancelled the flow. Will send a message
mathpf4bc50e2017-01-24 05:17:5084 // to the renderer which will indirectly destroy this object (through
85 // OnConnectionTerminated).
86 void UserCancelled();
87
sebsgd56b3e422017-10-20 18:08:0888 // Called when the main frame attached to this PaymentRequest is navigating to
89 // another document, but before the PaymentRequest is destroyed.
90 void DidStartMainFrameNavigationToDifferentDocument(bool is_user_initiated);
sebsg2c8558a2017-05-17 18:54:1091
mathpf4bc50e2017-01-24 05:17:5092 // As a result of a browser-side error or renderer-initiated mojo channel
93 // closure (e.g. there was an error on the renderer side, or payment was
94 // successful), this method is called. It is responsible for cleaning up,
95 // such as possibly closing the dialog.
96 void OnConnectionTerminated();
mathp6758be032017-01-13 04:49:5097
mathpd4be8de82017-03-01 00:51:4898 // Called when the user clicks on the "Pay" button.
99 void Pay();
100
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07101 // Hide this Payment Request if it's already showing.
102 void HideIfNecessary();
103
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04104 bool IsIncognito() const;
105
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:36106 // Returns true if this payment request supports skipping the Payment Sheet.
107 // Typically, this means only one payment method is supported, it's a URL
108 // based method, and no other info is requested from the user.
109 bool SatisfiesSkipUIConstraints() const;
110
anthonyvd3d7f9722016-12-07 18:43:54111 content::WebContents* web_contents() { return web_contents_; }
112
mathpf1a7a3752017-03-15 11:23:37113 PaymentRequestSpec* spec() { return spec_.get(); }
114 PaymentRequestState* state() { return state_.get(); }
mathpd4be8de82017-03-01 00:51:48115
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:36116 PaymentRequestSpec* spec() const { return spec_.get(); }
117 PaymentRequestState* state() const { return state_.get(); }
118
krb7f6421b2016-11-18 17:46:21119 private:
sebsgfcdd13c2017-06-08 15:49:33120 // Only records the abort reason if it's the first completion for this Payment
121 // Request. This is necessary since the aborts cascade into one another with
122 // the first one being the most precise.
123 void RecordFirstAbortReason(JourneyLogger::AbortReason completion_status);
sebsg2c8558a2017-05-17 18:54:10124
gogerald0a7ee6c2017-11-13 18:23:19125 // The callback for PaymentRequestState::CanMakePayment. Checks for query
126 // quota and may send QUERY_QUOTA_EXCEEDED.
gogerald8189d522017-09-15 17:52:18127 void CanMakePaymentCallback(bool can_make_payment);
128
gogerald0a7ee6c2017-11-13 18:23:19129 // The callback for PaymentRequestState::AreRequestedMethodsSupported.
130 void AreRequestedMethodsSupportedCallback(bool methods_supported);
131
Rouslan Solomakhin1804ee42017-10-03 14:27:43132 // Sends either CAN_MAKE_PAYMENT or CANNOT_MAKE_PAYMENT to the renderer,
133 // depending on |can_make_payment| value. Never sends QUERY_QUOTA_EXCEEDED.
Rouslan Solomakhin16ee55202017-10-03 19:04:42134 // Does not check query quota, but does check for incognito mode. If
135 // |warn_localhost_or_file| is true, then sends WARNING_CAN_MAKE_PAYMENT or
136 // WARNING_CANNOT_MAKE_PAYMENT version of the values instead.
137 void RespondToCanMakePaymentQuery(bool can_make_payment,
138 bool warn_localhost_or_file);
Rouslan Solomakhin1804ee42017-10-03 14:27:43139
krb7f6421b2016-11-18 17:46:21140 content::WebContents* web_contents_;
Rouslan Solomakhin4eea9bc22017-10-10 15:18:51141 std::unique_ptr<ContentPaymentRequestDelegate> delegate_;
mathpf709499d2017-01-09 20:48:36142 // |manager_| owns this PaymentRequest.
143 PaymentRequestWebContentsManager* manager_;
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07144 PaymentRequestDisplayManager* display_manager_;
145 std::unique_ptr<PaymentRequestDisplayManager::DisplayHandle> display_handle_;
rouslan8fdbfb242017-03-15 01:12:49146 mojo::Binding<mojom::PaymentRequest> binding_;
147 mojom::PaymentRequestClientPtr client_;
mathpd4be8de82017-03-01 00:51:48148
mathpf1a7a3752017-03-15 11:23:37149 std::unique_ptr<PaymentRequestSpec> spec_;
150 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54151
Rouslan Solomakhin115f7232017-08-01 15:24:38152 // The RFC 6454 origin of the top level frame that has invoked PaymentRequest
153 // API. This is what the user sees in the address bar.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39154 const GURL top_level_origin_;
Rouslan Solomakhin115f7232017-08-01 15:24:38155
rouslan690997682017-05-09 18:07:39156 // The RFC 6454 origin of the frame that has invoked PaymentRequest API. This
157 // can be either the main frame or an iframe.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39158 const GURL frame_origin_;
rouslan690997682017-05-09 18:07:39159
mathp300fa542017-03-27 19:29:37160 // May be null, must outlive this object.
161 ObserverForTest* observer_for_testing_;
162
sebsg20b49d7b2017-05-04 20:23:17163 JourneyLogger journey_logger_;
164
sebsgfcdd13c2017-06-08 15:49:33165 // Whether a completion was already recorded for this Payment Request.
166 bool has_recorded_completion_ = false;
sebsg2c8558a2017-05-17 18:54:10167
Rouslan Solomakhin833f8512018-04-03 23:19:25168 // Whether PaymentRequest.show() was invoked with a user gesture.
169 bool is_show_user_gesture_ = false;
170
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:13171 base::WeakPtrFactory<PaymentRequest> weak_ptr_factory_;
172
mathpf709499d2017-01-09 20:48:36173 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21174};
175
176} // namespace payments
177
rouslan908248c2017-02-27 21:30:24178#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_