blob: 4196a71cd6d13a5f1bfe7438fcd13f0c8a16dcb7 [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"
mathpf1a7a3752017-03-15 11:23:3713#include "components/payments/content/payment_request_spec.h"
14#include "components/payments/content/payment_request_state.h"
sebsg20b49d7b2017-05-04 20:23:1715#include "components/payments/core/journey_logger.h"
anthonyvdd23ed702017-04-05 15:29:0016#include "components/payments/core/payment_request_delegate.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
mathpf709499d2017-01-09 20:48:3629class PaymentRequestWebContentsManager;
30
mathpf1a7a3752017-03-15 11:23:3731// 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.
mathp4b85b582017-03-08 21:07:1637class PaymentRequest : public mojom::PaymentRequest,
mathpf1a7a3752017-03-15 11:23:3738 public PaymentRequestSpec::Observer,
39 public PaymentRequestState::Delegate {
krb7f6421b2016-11-18 17:46:2140 public:
mathp300fa542017-03-27 19:29:3741 class ObserverForTest {
42 public:
43 virtual void OnCanMakePaymentCalled() = 0;
gogerald8189d522017-09-15 17:52:1844 virtual void OnCanMakePaymentReturned() = 0;
rouslan6e3cf7c62017-04-17 21:23:2845 virtual void OnNotSupportedError() = 0;
rouslanb28f4532017-05-08 15:41:4746 virtual void OnConnectionTerminated() = 0;
Anthony Vallee-Dubois6813c1442017-05-17 19:32:5647 virtual void OnAbortCalled() = 0;
mathp300fa542017-03-27 19:29:3748
49 protected:
50 virtual ~ObserverForTest() {}
51 };
52
rouslan690997682017-05-09 18:07:3953 PaymentRequest(content::RenderFrameHost* render_frame_host,
54 content::WebContents* web_contents,
rouslan8fdbfb242017-03-15 01:12:4955 std::unique_ptr<PaymentRequestDelegate> delegate,
56 PaymentRequestWebContentsManager* manager,
mathp300fa542017-03-27 19:29:3757 mojo::InterfaceRequest<mojom::PaymentRequest> request,
58 ObserverForTest* observer_for_testing);
mathpf709499d2017-01-09 20:48:3659 ~PaymentRequest() override;
krb7f6421b2016-11-18 17:46:2160
rouslan8fdbfb242017-03-15 01:12:4961 // 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.pala1f17e82016-12-15 03:39:1266 void Show() override;
mathp151bd312017-04-03 21:07:2467 void UpdateWith(mojom::PaymentDetailsPtr details) override;
mathpf4bc50e2017-01-24 05:17:5068 void Abort() override;
rouslan8fdbfb242017-03-15 01:12:4969 void Complete(mojom::PaymentComplete result) override;
mathp4b85b582017-03-08 21:07:1670 void CanMakePayment() override;
71
mathpf1a7a3752017-03-15 11:23:3772 // PaymentRequestSpec::Observer:
mathp151bd312017-04-03 21:07:2473 void OnSpecUpdated() override {}
mathpf1a7a3752017-03-15 11:23:3774
75 // PaymentRequestState::Delegate:
mathpf1a7a3752017-03-15 11:23:3776 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override;
mathp151bd312017-04-03 21:07:2477 void OnShippingOptionIdSelected(std::string shipping_option_id) override;
78 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override;
krb7f6421b2016-11-18 17:46:2179
Rouslan Solomakhin16ee55202017-10-03 19:04:4280 // Called when the user explicitly cancelled the flow. Will send a message
mathpf4bc50e2017-01-24 05:17:5081 // to the renderer which will indirectly destroy this object (through
82 // OnConnectionTerminated).
83 void UserCancelled();
84
sebsg2c8558a2017-05-17 18:54:1085 // Called when the frame attached to this PaymentRequest is navigating away,
86 // but before the PaymentRequest is destroyed.
87 void DidStartNavigation(bool is_user_initiated);
88
mathpf4bc50e2017-01-24 05:17:5089 // 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();
mathp6758be032017-01-13 04:49:5094
mathpd4be8de82017-03-01 00:51:4895 // Called when the user clicks on the "Pay" button.
96 void Pay();
97
anthonyvd3d7f9722016-12-07 18:43:5498 content::WebContents* web_contents() { return web_contents_; }
99
mathpf1a7a3752017-03-15 11:23:37100 PaymentRequestSpec* spec() { return spec_.get(); }
101 PaymentRequestState* state() { return state_.get(); }
mathpd4be8de82017-03-01 00:51:48102
krb7f6421b2016-11-18 17:46:21103 private:
sebsgfcdd13c2017-06-08 15:49:33104 // 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);
sebsg2c8558a2017-05-17 18:54:10108
Rouslan Solomakhin16ee55202017-10-03 19:04:42109 // The PaymentRequestState::CanMakePaymentCallback. Checks for query quota and
110 // may send QUERY_QUOTA_EXCEEDED.
gogerald8189d522017-09-15 17:52:18111 void CanMakePaymentCallback(bool can_make_payment);
112
Rouslan Solomakhin1804ee42017-10-03 14:27:43113 // 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 Solomakhin16ee55202017-10-03 19:04:42115 // 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 Solomakhin1804ee42017-10-03 14:27:43120
krb7f6421b2016-11-18 17:46:21121 content::WebContents* web_contents_;
mathpf709499d2017-01-09 20:48:36122 std::unique_ptr<PaymentRequestDelegate> delegate_;
123 // |manager_| owns this PaymentRequest.
124 PaymentRequestWebContentsManager* manager_;
rouslan8fdbfb242017-03-15 01:12:49125 mojo::Binding<mojom::PaymentRequest> binding_;
126 mojom::PaymentRequestClientPtr client_;
mathpd4be8de82017-03-01 00:51:48127
mathpf1a7a3752017-03-15 11:23:37128 std::unique_ptr<PaymentRequestSpec> spec_;
129 std::unique_ptr<PaymentRequestState> state_;
anthonyvd3d7f9722016-12-07 18:43:54130
Rouslan Solomakhin115f7232017-08-01 15:24:38131 // 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 Solomakhin6e979ab2017-08-30 17:30:39133 const GURL top_level_origin_;
Rouslan Solomakhin115f7232017-08-01 15:24:38134
rouslan690997682017-05-09 18:07:39135 // The RFC 6454 origin of the frame that has invoked PaymentRequest API. This
136 // can be either the main frame or an iframe.
Rouslan Solomakhin6e979ab2017-08-30 17:30:39137 const GURL frame_origin_;
rouslan690997682017-05-09 18:07:39138
mathp300fa542017-03-27 19:29:37139 // May be null, must outlive this object.
140 ObserverForTest* observer_for_testing_;
141
sebsg20b49d7b2017-05-04 20:23:17142 JourneyLogger journey_logger_;
143
sebsgfcdd13c2017-06-08 15:49:33144 // Whether a completion was already recorded for this Payment Request.
145 bool has_recorded_completion_ = false;
sebsg2c8558a2017-05-17 18:54:10146
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:13147 base::WeakPtrFactory<PaymentRequest> weak_ptr_factory_;
148
mathpf709499d2017-01-09 20:48:36149 DISALLOW_COPY_AND_ASSIGN(PaymentRequest);
krb7f6421b2016-11-18 17:46:21150};
151
152} // namespace payments
153
rouslan908248c2017-02-27 21:30:24154#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_H_