blob: 30a7c5564dd4528809669465d6ab7aebdac6475b [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;
sanjoy.pala1f17e82016-12-15 03:39:1268 void Show() 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
anthonyvd3d7f9722016-12-07 18:43:54106 content::WebContents* web_contents() { return web_contents_; }
107
mathpf1a7a3752017-03-15 11:23:37108 PaymentRequestSpec* spec() { return spec_.get(); }
109 PaymentRequestState* state() { return state_.get(); }
mathpd4be8de82017-03-01 00:51:48110
krb7f6421b2016-11-18 17:46:21111 private:
sebsgfcdd13c2017-06-08 15:49:33112 // Only records the abort reason if it's the first completion for this Payment
113 // Request. This is necessary since the aborts cascade into one another with
114 // the first one being the most precise.
115 void RecordFirstAbortReason(JourneyLogger::AbortReason completion_status);
sebsg2c8558a2017-05-17 18:54:10116
gogerald0a7ee6c2017-11-13 18:23:19117 // The callback for PaymentRequestState::CanMakePayment. Checks for query
118 // quota and may send QUERY_QUOTA_EXCEEDED.
gogerald8189d522017-09-15 17:52:18119 void CanMakePaymentCallback(bool can_make_payment);
120
gogerald0a7ee6c2017-11-13 18:23:19121 // The callback for PaymentRequestState::AreRequestedMethodsSupported.
122 void AreRequestedMethodsSupportedCallback(bool methods_supported);
123
Rouslan Solomakhin1804ee42017-10-03 14:27:43124 // Sends either CAN_MAKE_PAYMENT or CANNOT_MAKE_PAYMENT to the renderer,
125 // depending on |can_make_payment| value. Never sends QUERY_QUOTA_EXCEEDED.
Rouslan Solomakhin16ee55202017-10-03 19:04:42126 // Does not check query quota, but does check for incognito mode. If
127 // |warn_localhost_or_file| is true, then sends WARNING_CAN_MAKE_PAYMENT or
128 // WARNING_CANNOT_MAKE_PAYMENT version of the values instead.
129 void RespondToCanMakePaymentQuery(bool can_make_payment,
130 bool warn_localhost_or_file);
Rouslan Solomakhin1804ee42017-10-03 14:27:43131
krb7f6421b2016-11-18 17:46:21132 content::WebContents* web_contents_;
Rouslan Solomakhin4eea9bc22017-10-10 15:18:51133 std::unique_ptr<ContentPaymentRequestDelegate> delegate_;
mathpf709499d2017-01-09 20:48:36134 // |manager_| owns this PaymentRequest.
135 PaymentRequestWebContentsManager* manager_;
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07136 PaymentRequestDisplayManager* display_manager_;
137 std::unique_ptr<PaymentRequestDisplayManager::DisplayHandle> display_handle_;
rouslan8fdbfb242017-03-15 01:12:49138 mojo::Binding<mojom::PaymentRequest> binding_;
139 mojom::PaymentRequestClientPtr client_;
mathpd4be8de82017-03-01 00:51:48140
mathpf1a7a3752017-03-15 11:23:37141 std::unique_ptr<PaymentRequestSpec> spec_;
142 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54143
Rouslan Solomakhin115f7232017-08-01 15:24:38144 // The RFC 6454 origin of the top level frame that has invoked PaymentRequest
145 // API. This is what the user sees in the address bar.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39146 const GURL top_level_origin_;
Rouslan Solomakhin115f7232017-08-01 15:24:38147
rouslan690997682017-05-09 18:07:39148 // The RFC 6454 origin of the frame that has invoked PaymentRequest API. This
149 // can be either the main frame or an iframe.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39150 const GURL frame_origin_;
rouslan690997682017-05-09 18:07:39151
mathp300fa542017-03-27 19:29:37152 // May be null, must outlive this object.
153 ObserverForTest* observer_for_testing_;
154
sebsg20b49d7b2017-05-04 20:23:17155 JourneyLogger journey_logger_;
156
sebsgfcdd13c2017-06-08 15:49:33157 // Whether a completion was already recorded for this Payment Request.
158 bool has_recorded_completion_ = false;
sebsg2c8558a2017-05-17 18:54:10159
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:13160 base::WeakPtrFactory<PaymentRequest> weak_ptr_factory_;
161
mathpf709499d2017-01-09 20:48:36162 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21163};
164
165} // namespace payments
166
rouslan908248c2017-02-27 21:30:24167#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_