krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 1 | // 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 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 5 | #ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |
| 6 | #define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 9 | #include <vector> |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 10 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 11 | #include "base/macros.h" |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 13 | #include "components/payments/content/payment_request_spec.h" |
| 14 | #include "components/payments/content/payment_request_state.h" |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 15 | #include "components/payments/core/journey_logger.h" |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 16 | #include "mojo/public/cpp/bindings/binding.h" |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 17 | #include "mojo/public/cpp/bindings/interface_request.h" |
Rouslan Solomakhin | 2c88e23 | 2017-06-14 20:28:52 | [diff] [blame] | 18 | #include "third_party/WebKit/public/platform/modules/payments/payment_request.mojom.h" |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 19 | #include "url/gurl.h" |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 20 | |
| 21 | namespace content { |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 22 | class RenderFrameHost; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 23 | class WebContents; |
Rouslan Solomakhin | 115f723 | 2017-08-01 15:24:38 | [diff] [blame] | 24 | } // namespace content |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 25 | |
| 26 | namespace payments { |
| 27 | |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 28 | class ContentPaymentRequestDelegate; |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 29 | class PaymentRequestWebContentsManager; |
| 30 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 31 | // This class manages the interaction between the renderer (through the |
| 32 | // PaymentRequestClient and Mojo stub implementation) and the UI (through the |
| 33 | // PaymentRequestDelegate). The API user (merchant) specification (supported |
| 34 | // payment methods, required information, order details) is stored in |
| 35 | // PaymentRequestSpec, and the current user selection state (and related data) |
| 36 | // is stored in PaymentRequestSpec. |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 37 | class PaymentRequest : public mojom::PaymentRequest, |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 38 | public PaymentRequestSpec::Observer, |
| 39 | public PaymentRequestState::Delegate { |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 40 | public: |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 41 | class ObserverForTest { |
| 42 | public: |
| 43 | virtual void OnCanMakePaymentCalled() = 0; |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 44 | virtual void OnCanMakePaymentReturned() = 0; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 45 | virtual void OnNotSupportedError() = 0; |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 46 | virtual void OnConnectionTerminated() = 0; |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 47 | virtual void OnAbortCalled() = 0; |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 48 | |
| 49 | protected: |
| 50 | virtual ~ObserverForTest() {} |
| 51 | }; |
| 52 | |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 53 | PaymentRequest(content::RenderFrameHost* render_frame_host, |
| 54 | content::WebContents* web_contents, |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 55 | std::unique_ptr<ContentPaymentRequestDelegate> delegate, |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 56 | PaymentRequestWebContentsManager* manager, |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 57 | mojo::InterfaceRequest<mojom::PaymentRequest> request, |
| 58 | ObserverForTest* observer_for_testing); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 59 | ~PaymentRequest() override; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 60 | |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 61 | // mojom::PaymentRequest |
| 62 | void Init(mojom::PaymentRequestClientPtr client, |
| 63 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 64 | mojom::PaymentDetailsPtr details, |
| 65 | mojom::PaymentOptionsPtr options) override; |
sanjoy.pal | a1f17e8 | 2016-12-15 03:39:12 | [diff] [blame] | 66 | void Show() override; |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 67 | void UpdateWith(mojom::PaymentDetailsPtr details) override; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 68 | void Abort() override; |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 69 | void Complete(mojom::PaymentComplete result) override; |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 70 | void CanMakePayment() override; |
| 71 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 72 | // PaymentRequestSpec::Observer: |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 73 | void OnSpecUpdated() override {} |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 74 | |
| 75 | // PaymentRequestState::Delegate: |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 76 | void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override; |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 77 | void OnShippingOptionIdSelected(std::string shipping_option_id) override; |
| 78 | void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 79 | |
Rouslan Solomakhin | 16ee5520 | 2017-10-03 19:04:42 | [diff] [blame] | 80 | // Called when the user explicitly cancelled the flow. Will send a message |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 81 | // to the renderer which will indirectly destroy this object (through |
| 82 | // OnConnectionTerminated). |
| 83 | void UserCancelled(); |
| 84 | |
sebsg | d56b3e42 | 2017-10-20 18:08:08 | [diff] [blame^] | 85 | // Called when the main frame attached to this PaymentRequest is navigating to |
| 86 | // another document, but before the PaymentRequest is destroyed. |
| 87 | void DidStartMainFrameNavigationToDifferentDocument(bool is_user_initiated); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 88 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 89 | // As a result of a browser-side error or renderer-initiated mojo channel |
| 90 | // closure (e.g. there was an error on the renderer side, or payment was |
| 91 | // successful), this method is called. It is responsible for cleaning up, |
| 92 | // such as possibly closing the dialog. |
| 93 | void OnConnectionTerminated(); |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 94 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 95 | // Called when the user clicks on the "Pay" button. |
| 96 | void Pay(); |
| 97 | |
anthonyvd | 3d7f972 | 2016-12-07 18:43:54 | [diff] [blame] | 98 | content::WebContents* web_contents() { return web_contents_; } |
| 99 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 100 | PaymentRequestSpec* spec() { return spec_.get(); } |
| 101 | PaymentRequestState* state() { return state_.get(); } |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 102 | |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 103 | private: |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 104 | // Only records the abort reason if it's the first completion for this Payment |
| 105 | // Request. This is necessary since the aborts cascade into one another with |
| 106 | // the first one being the most precise. |
| 107 | void RecordFirstAbortReason(JourneyLogger::AbortReason completion_status); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 108 | |
Rouslan Solomakhin | 16ee5520 | 2017-10-03 19:04:42 | [diff] [blame] | 109 | // The PaymentRequestState::CanMakePaymentCallback. Checks for query quota and |
| 110 | // may send QUERY_QUOTA_EXCEEDED. |
gogerald | 8189d52 | 2017-09-15 17:52:18 | [diff] [blame] | 111 | void CanMakePaymentCallback(bool can_make_payment); |
| 112 | |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 113 | // Sends either CAN_MAKE_PAYMENT or CANNOT_MAKE_PAYMENT to the renderer, |
| 114 | // depending on |can_make_payment| value. Never sends QUERY_QUOTA_EXCEEDED. |
Rouslan Solomakhin | 16ee5520 | 2017-10-03 19:04:42 | [diff] [blame] | 115 | // Does not check query quota, but does check for incognito mode. If |
| 116 | // |warn_localhost_or_file| is true, then sends WARNING_CAN_MAKE_PAYMENT or |
| 117 | // WARNING_CANNOT_MAKE_PAYMENT version of the values instead. |
| 118 | void RespondToCanMakePaymentQuery(bool can_make_payment, |
| 119 | bool warn_localhost_or_file); |
Rouslan Solomakhin | 1804ee4 | 2017-10-03 14:27:43 | [diff] [blame] | 120 | |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 121 | content::WebContents* web_contents_; |
Rouslan Solomakhin | 4eea9bc2 | 2017-10-10 15:18:51 | [diff] [blame] | 122 | std::unique_ptr<ContentPaymentRequestDelegate> delegate_; |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 123 | // |manager_| owns this PaymentRequest. |
| 124 | PaymentRequestWebContentsManager* manager_; |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 125 | mojo::Binding<mojom::PaymentRequest> binding_; |
| 126 | mojom::PaymentRequestClientPtr client_; |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 127 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 128 | std::unique_ptr<PaymentRequestSpec> spec_; |
| 129 | std::unique_ptr<PaymentRequestState> state_; |
anthonyvd | 3d7f972 | 2016-12-07 18:43:54 | [diff] [blame] | 130 | |
Rouslan Solomakhin | 115f723 | 2017-08-01 15:24:38 | [diff] [blame] | 131 | // The RFC 6454 origin of the top level frame that has invoked PaymentRequest |
| 132 | // API. This is what the user sees in the address bar. |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 133 | const GURL top_level_origin_; |
Rouslan Solomakhin | 115f723 | 2017-08-01 15:24:38 | [diff] [blame] | 134 | |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 135 | // The RFC 6454 origin of the frame that has invoked PaymentRequest API. This |
| 136 | // can be either the main frame or an iframe. |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 137 | const GURL frame_origin_; |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 138 | |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 139 | // May be null, must outlive this object. |
| 140 | ObserverForTest* observer_for_testing_; |
| 141 | |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 142 | JourneyLogger journey_logger_; |
| 143 | |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 144 | // Whether a completion was already recorded for this Payment Request. |
| 145 | bool has_recorded_completion_ = false; |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 146 | |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 147 | base::WeakPtrFactory<PaymentRequest> weak_ptr_factory_; |
| 148 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 149 | DISALLOW_COPY_AND_ASSIGN(PaymentRequest); |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | } // namespace payments |
| 153 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 154 | #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |