blob: 4613add27328af0b17270804ab8cdfe4fca24a78 [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"
mathpf1a7a3752017-03-15 11:23:3712#include "components/payments/content/payment_request_spec.h"
13#include "components/payments/content/payment_request_state.h"
sebsg20b49d7b2017-05-04 20:23:1714#include "components/payments/core/journey_logger.h"
anthonyvdd23ed702017-04-05 15:29:0015#include "components/payments/core/payment_request_delegate.h"
tobiasjsc66efdd22017-04-17 13:38:5916#include "components/payments/mojom/payment_request.mojom.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"
rouslan690997682017-05-09 18:07:3919#include "url/gurl.h"
krb7f6421b2016-11-18 17:46:2120
krb7f6421b2016-11-18 17:46:2121namespace content {
rouslan690997682017-05-09 18:07:3922class RenderFrameHost;
krb7f6421b2016-11-18 17:46:2123class WebContents;
24}
25
26namespace payments {
27
mathpf709499d2017-01-09 20:48:3628class PaymentRequestWebContentsManager;
29
mathpf1a7a3752017-03-15 11:23:3730// This class manages the interaction between the renderer (through the
31// PaymentRequestClient and Mojo stub implementation) and the UI (through the
32// PaymentRequestDelegate). The API user (merchant) specification (supported
33// payment methods, required information, order details) is stored in
34// PaymentRequestSpec, and the current user selection state (and related data)
35// is stored in PaymentRequestSpec.
mathp4b85b582017-03-08 21:07:1636class PaymentRequest : public mojom::PaymentRequest,
mathpf1a7a3752017-03-15 11:23:3737 public PaymentRequestSpec::Observer,
38 public PaymentRequestState::Delegate {
krb7f6421b2016-11-18 17:46:2139 public:
mathp300fa542017-03-27 19:29:3740 class ObserverForTest {
41 public:
42 virtual void OnCanMakePaymentCalled() = 0;
rouslan6e3cf7c62017-04-17 21:23:2843 virtual void OnNotSupportedError() = 0;
rouslanb28f4532017-05-08 15:41:4744 virtual void OnConnectionTerminated() = 0;
mathp300fa542017-03-27 19:29:3745
46 protected:
47 virtual ~ObserverForTest() {}
48 };
49
rouslan690997682017-05-09 18:07:3950 PaymentRequest(content::RenderFrameHost* render_frame_host,
51 content::WebContents* web_contents,
rouslan8fdbfb242017-03-15 01:12:4952 std::unique_ptr<PaymentRequestDelegate> delegate,
53 PaymentRequestWebContentsManager* manager,
mathp300fa542017-03-27 19:29:3754 mojo::InterfaceRequest<mojom::PaymentRequest> request,
55 ObserverForTest* observer_for_testing);
mathpf709499d2017-01-09 20:48:3656 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2157
rouslan8fdbfb242017-03-15 01:12:4958 // mojom::PaymentRequest
59 void Init(mojom::PaymentRequestClientPtr client,
60 std::vector<mojom::PaymentMethodDataPtr> method_data,
61 mojom::PaymentDetailsPtr details,
62 mojom::PaymentOptionsPtr options) override;
sanjoy.pala1f17e82016-12-15 03:39:1263 void Show() override;
mathp151bd312017-04-03 21:07:2464 void UpdateWith(mojom::PaymentDetailsPtr details) override;
mathpf4bc50e2017-01-24 05:17:5065 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4966 void Complete(mojom::PaymentComplete result) override;
mathp4b85b582017-03-08 21:07:1667 void CanMakePayment() override;
68
mathpf1a7a3752017-03-15 11:23:3769 // PaymentRequestSpec::Observer:
mathp151bd312017-04-03 21:07:2470 void OnSpecUpdated() override {}
mathpf1a7a3752017-03-15 11:23:3771
72 // PaymentRequestState::Delegate:
mathpf1a7a3752017-03-15 11:23:3773 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
mathp151bd312017-04-03 21:07:2474 void OnShippingOptionIdSelected(std::string shipping_option_id) override;
75 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override;
krb7f6421b2016-11-18 17:46:2176
mathpf4bc50e2017-01-24 05:17:5077 // Called when the user explicitely cancelled the flow. Will send a message
78 // to the renderer which will indirectly destroy this object (through
79 // OnConnectionTerminated).
80 void UserCancelled();
81
82 // As a result of a browser-side error or renderer-initiated mojo channel
83 // closure (e.g. there was an error on the renderer side, or payment was
84 // successful), this method is called. It is responsible for cleaning up,
85 // such as possibly closing the dialog.
86 void OnConnectionTerminated();
mathp6758be032017-01-13 04:49:5087
mathpd4be8de82017-03-01 00:51:4888 // Called when the user clicks on the "Pay" button.
89 void Pay();
90
anthonyvd3d7f9722016-12-07 18:43:5491 content::WebContents* web_contents() { return web_contents_; }
92
mathpf1a7a3752017-03-15 11:23:3793 PaymentRequestSpec* spec() { return spec_.get(); }
94 PaymentRequestState* state() { return state_.get(); }
mathpd4be8de82017-03-01 00:51:4895
krb7f6421b2016-11-18 17:46:2196 private:
krb7f6421b2016-11-18 17:46:2197 content::WebContents* web_contents_;
mathpf709499d2017-01-09 20:48:3698 std::unique_ptr<PaymentRequestDelegate> delegate_;
99 // |manager_| owns this PaymentRequest.
100 PaymentRequestWebContentsManager* manager_;
rouslan8fdbfb242017-03-15 01:12:49101 mojo::Binding<mojom::PaymentRequest> binding_;
102 mojom::PaymentRequestClientPtr client_;
mathpd4be8de82017-03-01 00:51:48103
mathpf1a7a3752017-03-15 11:23:37104 std::unique_ptr<PaymentRequestSpec> spec_;
105 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54106
rouslan690997682017-05-09 18:07:39107 // The RFC 6454 origin of the frame that has invoked PaymentRequest API. This
108 // can be either the main frame or an iframe.
109 const GURL frame_origin_;
110
mathp300fa542017-03-27 19:29:37111 // May be null, must outlive this object.
112 ObserverForTest* observer_for_testing_;
113
sebsg20b49d7b2017-05-04 20:23:17114 JourneyLogger journey_logger_;
115
mathpf709499d2017-01-09 20:48:36116 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21117};
118
119} // namespace payments
120
rouslan908248c2017-02-27 21:30:24121#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_