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" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 12 | #include "components/payments/content/payment_request_spec.h" |
| 13 | #include "components/payments/content/payment_request_state.h" |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 14 | #include "components/payments/core/payment_request_delegate.h" |
tobiasjs | c66efdd2 | 2017-04-17 13:38:59 | [diff] [blame] | 15 | #include "components/payments/mojom/payment_request.mojom.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" |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 18 | |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 19 | namespace content { |
| 20 | class WebContents; |
| 21 | } |
| 22 | |
| 23 | namespace payments { |
| 24 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 25 | class PaymentRequestWebContentsManager; |
| 26 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 27 | // This class manages the interaction between the renderer (through the |
| 28 | // PaymentRequestClient and Mojo stub implementation) and the UI (through the |
| 29 | // PaymentRequestDelegate). The API user (merchant) specification (supported |
| 30 | // payment methods, required information, order details) is stored in |
| 31 | // PaymentRequestSpec, and the current user selection state (and related data) |
| 32 | // is stored in PaymentRequestSpec. |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 33 | class PaymentRequest : public mojom::PaymentRequest, |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 34 | public PaymentRequestSpec::Observer, |
| 35 | public PaymentRequestState::Delegate { |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 36 | public: |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 37 | class ObserverForTest { |
| 38 | public: |
| 39 | virtual void OnCanMakePaymentCalled() = 0; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame^] | 40 | virtual void OnNotSupportedError() = 0; |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | virtual ~ObserverForTest() {} |
| 44 | }; |
| 45 | |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 46 | PaymentRequest(content::WebContents* web_contents, |
| 47 | std::unique_ptr<PaymentRequestDelegate> delegate, |
| 48 | PaymentRequestWebContentsManager* manager, |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 49 | mojo::InterfaceRequest<mojom::PaymentRequest> request, |
| 50 | ObserverForTest* observer_for_testing); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 51 | ~PaymentRequest() override; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 52 | |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 53 | // mojom::PaymentRequest |
| 54 | void Init(mojom::PaymentRequestClientPtr client, |
| 55 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 56 | mojom::PaymentDetailsPtr details, |
| 57 | mojom::PaymentOptionsPtr options) override; |
sanjoy.pal | a1f17e8 | 2016-12-15 03:39:12 | [diff] [blame] | 58 | void Show() override; |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 59 | void UpdateWith(mojom::PaymentDetailsPtr details) override; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 60 | void Abort() override; |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 61 | void Complete(mojom::PaymentComplete result) override; |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 62 | void CanMakePayment() override; |
| 63 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 64 | // PaymentRequestSpec::Observer: |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 65 | void OnSpecUpdated() override {} |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 66 | |
| 67 | // PaymentRequestState::Delegate: |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 68 | void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override; |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 69 | void OnShippingOptionIdSelected(std::string shipping_option_id) override; |
| 70 | void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override; |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 71 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 72 | // Called when the user explicitely cancelled the flow. Will send a message |
| 73 | // to the renderer which will indirectly destroy this object (through |
| 74 | // OnConnectionTerminated). |
| 75 | void UserCancelled(); |
| 76 | |
| 77 | // As a result of a browser-side error or renderer-initiated mojo channel |
| 78 | // closure (e.g. there was an error on the renderer side, or payment was |
| 79 | // successful), this method is called. It is responsible for cleaning up, |
| 80 | // such as possibly closing the dialog. |
| 81 | void OnConnectionTerminated(); |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 82 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 83 | // Called when the user clicks on the "Pay" button. |
| 84 | void Pay(); |
| 85 | |
anthonyvd | 3d7f972 | 2016-12-07 18:43:54 | [diff] [blame] | 86 | content::WebContents* web_contents() { return web_contents_; } |
| 87 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 88 | PaymentRequestSpec* spec() { return spec_.get(); } |
| 89 | PaymentRequestState* state() { return state_.get(); } |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 90 | |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 91 | private: |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 92 | content::WebContents* web_contents_; |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 93 | std::unique_ptr<PaymentRequestDelegate> delegate_; |
| 94 | // |manager_| owns this PaymentRequest. |
| 95 | PaymentRequestWebContentsManager* manager_; |
rouslan | 8fdbfb24 | 2017-03-15 01:12:49 | [diff] [blame] | 96 | mojo::Binding<mojom::PaymentRequest> binding_; |
| 97 | mojom::PaymentRequestClientPtr client_; |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 98 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 99 | std::unique_ptr<PaymentRequestSpec> spec_; |
| 100 | std::unique_ptr<PaymentRequestState> state_; |
anthonyvd | 3d7f972 | 2016-12-07 18:43:54 | [diff] [blame] | 101 | |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 102 | // May be null, must outlive this object. |
| 103 | ObserverForTest* observer_for_testing_; |
| 104 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 105 | DISALLOW_COPY_AND_ASSIGN(PaymentRequest); |
krb | 7f6421b | 2016-11-18 17:46:21 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace payments |
| 109 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 110 | #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_ |