blob: ff8c2474371a2967067f0e349fe8eb5a0409631b [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"
krb7f6421b2016-11-18 17:46:2116#include "mojo/public/cpp/bindings/binding.h"
rouslan8fdbfb242017-03-15 01:12:4917#include "mojo/public/cpp/bindings/interface_request.h"
Rouslan Solomakhin2c88e232017-06-14 20:28:5218#include "third_party/WebKit/public/platform/modules/payments/payment_request.mojom.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;
Anthony Vallee-Dubois6813c1442017-05-17 19:32:5645 virtual void OnAbortCalled() = 0;
mathp300fa542017-03-27 19:29:3746
47 protected:
48 virtual ~ObserverForTest() {}
49 };
50
rouslan690997682017-05-09 18:07:3951 PaymentRequest(content::RenderFrameHost* render_frame_host,
52 content::WebContents* web_contents,
rouslan8fdbfb242017-03-15 01:12:4953 std::unique_ptr<PaymentRequestDelegate> delegate,
54 PaymentRequestWebContentsManager* manager,
mathp300fa542017-03-27 19:29:3755 mojo::InterfaceRequest<mojom::PaymentRequest> request,
56 ObserverForTest* observer_for_testing);
mathpf709499d2017-01-09 20:48:3657 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2158
rouslan8fdbfb242017-03-15 01:12:4959 // mojom::PaymentRequest
60 void Init(mojom::PaymentRequestClientPtr client,
61 std::vector<mojom::PaymentMethodDataPtr> method_data,
62 mojom::PaymentDetailsPtr details,
63 mojom::PaymentOptionsPtr options) override;
sanjoy.pala1f17e82016-12-15 03:39:1264 void Show() override;
mathp151bd312017-04-03 21:07:2465 void UpdateWith(mojom::PaymentDetailsPtr details) override;
mathpf4bc50e2017-01-24 05:17:5066 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4967 void Complete(mojom::PaymentComplete result) override;
mathp4b85b582017-03-08 21:07:1668 void CanMakePayment() override;
69
mathpf1a7a3752017-03-15 11:23:3770 // PaymentRequestSpec::Observer:
mathp151bd312017-04-03 21:07:2471 void OnSpecUpdated() override {}
mathpf1a7a3752017-03-15 11:23:3772
73 // PaymentRequestState::Delegate:
mathpf1a7a3752017-03-15 11:23:3774 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
mathp151bd312017-04-03 21:07:2475 void OnShippingOptionIdSelected(std::string shipping_option_id) override;
76 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override;
krb7f6421b2016-11-18 17:46:2177
mathpf4bc50e2017-01-24 05:17:5078 // Called when the user explicitely cancelled the flow. Will send a message
79 // to the renderer which will indirectly destroy this object (through
80 // OnConnectionTerminated).
81 void UserCancelled();
82
sebsg2c8558a2017-05-17 18:54:1083 // Called when the frame attached to this PaymentRequest is navigating away,
84 // but before the PaymentRequest is destroyed.
85 void DidStartNavigation(bool is_user_initiated);
86
mathpf4bc50e2017-01-24 05:17:5087 // As a result of a browser-side error or renderer-initiated mojo channel
88 // closure (e.g. there was an error on the renderer side, or payment was
89 // successful), this method is called. It is responsible for cleaning up,
90 // such as possibly closing the dialog.
91 void OnConnectionTerminated();
mathp6758be032017-01-13 04:49:5092
mathpd4be8de82017-03-01 00:51:4893 // Called when the user clicks on the "Pay" button.
94 void Pay();
95
anthonyvd3d7f9722016-12-07 18:43:5496 content::WebContents* web_contents() { return web_contents_; }
97
mathpf1a7a3752017-03-15 11:23:3798 PaymentRequestSpec* spec() { return spec_.get(); }
99 PaymentRequestState* state() { return state_.get(); }
mathpd4be8de82017-03-01 00:51:48100
krb7f6421b2016-11-18 17:46:21101 private:
sebsgfcdd13c2017-06-08 15:49:33102 // Only records the abort reason if it's the first completion for this Payment
103 // Request. This is necessary since the aborts cascade into one another with
104 // the first one being the most precise.
105 void RecordFirstAbortReason(JourneyLogger::AbortReason completion_status);
sebsg2c8558a2017-05-17 18:54:10106
krb7f6421b2016-11-18 17:46:21107 content::WebContents* web_contents_;
mathpf709499d2017-01-09 20:48:36108 std::unique_ptr<PaymentRequestDelegate> delegate_;
109 // |manager_| owns this PaymentRequest.
110 PaymentRequestWebContentsManager* manager_;
rouslan8fdbfb242017-03-15 01:12:49111 mojo::Binding<mojom::PaymentRequest> binding_;
112 mojom::PaymentRequestClientPtr client_;
mathpd4be8de82017-03-01 00:51:48113
mathpf1a7a3752017-03-15 11:23:37114 std::unique_ptr<PaymentRequestSpec> spec_;
115 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54116
rouslan690997682017-05-09 18:07:39117 // The RFC 6454 origin of the frame that has invoked PaymentRequest API. This
118 // can be either the main frame or an iframe.
119 const GURL frame_origin_;
120
mathp300fa542017-03-27 19:29:37121 // May be null, must outlive this object.
122 ObserverForTest* observer_for_testing_;
123
sebsg20b49d7b2017-05-04 20:23:17124 JourneyLogger journey_logger_;
125
sebsgfcdd13c2017-06-08 15:49:33126 // Whether a completion was already recorded for this Payment Request.
127 bool has_recorded_completion_ = false;
sebsg2c8558a2017-05-17 18:54:10128
mathpf709499d2017-01-09 20:48:36129 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21130};
131
132} // namespace payments
133
rouslan908248c2017-02-27 21:30:24134#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_