blob: 15aee2c679179aa5b8350baa43e647edde7dd14a [file] [log] [blame]
mathpf709499d2017-01-09 20:48:361// 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#include "components/payments/content/payment_request.h"
6
anthonyvdd23ed702017-04-05 15:29:007#include <string>
rouslan908248c2017-02-27 21:30:248#include <utility>
mathpf709499d2017-01-09 20:48:369
tmartino68c0a272017-01-19 17:44:0810#include "base/memory/ptr_util.h"
rouslan908248c2017-02-27 21:30:2411#include "components/payments/content/payment_details_validation.h"
12#include "components/payments/content/payment_request_web_contents_manager.h"
mathpf709499d2017-01-09 20:48:3613#include "content/public/browser/browser_thread.h"
14#include "content/public/browser/web_contents.h"
15
16namespace payments {
17
18PaymentRequest::PaymentRequest(
19 content::WebContents* web_contents,
20 std::unique_ptr<PaymentRequestDelegate> delegate,
21 PaymentRequestWebContentsManager* manager,
mathp300fa542017-03-27 19:29:3722 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request,
23 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3624 : web_contents_(web_contents),
25 delegate_(std::move(delegate)),
26 manager_(manager),
mathp300fa542017-03-27 19:29:3727 binding_(this, std::move(request)),
28 observer_for_testing_(observer_for_testing) {
mathpf4bc50e2017-01-24 05:17:5029 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
30 // will happen as a result of many renderer-side events (both successful and
31 // erroneous in nature).
32 // TODO(crbug.com/683636): Investigate using
33 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
34 binding_.set_connection_error_handler(base::Bind(
35 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
mathpf709499d2017-01-09 20:48:3636}
37
38PaymentRequest::~PaymentRequest() {}
39
40void PaymentRequest::Init(
41 payments::mojom::PaymentRequestClientPtr client,
mathpc2d07f962017-02-17 18:33:5142 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
mathpf709499d2017-01-09 20:48:3643 payments::mojom::PaymentDetailsPtr details,
44 payments::mojom::PaymentOptionsPtr options) {
45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
46 std::string error;
47 if (!payments::validatePaymentDetails(details, &error)) {
48 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5049 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3650 return;
51 }
jinho.bangfcb5ec92017-03-29 08:08:0252 if (!details->total) {
53 LOG(ERROR) << "Missing total";
54 OnConnectionTerminated();
55 return;
56 }
mathpf709499d2017-01-09 20:48:3657 client_ = std::move(client);
mathpf1a7a3752017-03-15 11:23:3758 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:3359 std::move(options), std::move(details), std::move(method_data), this,
60 delegate_->GetApplicationLocale());
61 state_ = base::MakeUnique<PaymentRequestState>(
62 spec_.get(), this, delegate_->GetApplicationLocale(),
anthonyvdd23ed702017-04-05 15:29:0063 delegate_->GetPersonalDataManager(), delegate_.get());
mathpf709499d2017-01-09 20:48:3664}
65
66void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:1067 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:5068 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
69 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:1070 return;
71 }
mathpf4bc50e2017-01-24 05:17:5072 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:3673}
74
mathp151bd312017-04-03 21:07:2475void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
76 std::string error;
77 if (!payments::validatePaymentDetails(details, &error)) {
78 LOG(ERROR) << error;
79 OnConnectionTerminated();
80 return;
81 }
82 spec_->UpdateWith(std::move(details));
83}
84
mathpf4bc50e2017-01-24 05:17:5085void PaymentRequest::Abort() {
86 // The API user has decided to abort. We return a successful abort message to
87 // the renderer, which closes the Mojo message pipe, which triggers
88 // PaymentRequest::OnConnectionTerminated, which destroys this object.
89 if (client_.is_bound())
90 client_->OnAbort(true /* aborted_successfully */);
91}
92
mathp218795892017-03-29 15:15:3493void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:1694 if (!client_.is_bound())
95 return;
96
mathp218795892017-03-29 15:15:3497 if (result != mojom::PaymentComplete::SUCCESS) {
98 delegate_->ShowErrorMessage();
99 } else {
100 // When the renderer closes the connection,
101 // PaymentRequest::OnConnectionTerminated will be called.
102 client_->OnComplete();
103 }
mathp4b85b582017-03-08 21:07:16104}
105
106void PaymentRequest::CanMakePayment() {
mathp1a5be4f2017-03-24 18:09:19107 // TODO(crbug.com/704676): Implement a quota policy for this method.
mathpf39f46d2017-03-24 22:03:47108 // PaymentRequest.canMakePayments() never returns false in incognito mode.
mathp1a5be4f2017-03-24 18:09:19109 client_->OnCanMakePayment(
mathpf39f46d2017-03-24 22:03:47110 delegate_->IsIncognito() || state()->CanMakePayment()
mathp1a5be4f2017-03-24 18:09:19111 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
112 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
mathp300fa542017-03-27 19:29:37113 if (observer_for_testing_)
114 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16115}
116
mathpf1a7a3752017-03-15 11:23:37117void PaymentRequest::OnInvalidSpecProvided() {
118 OnConnectionTerminated();
119}
120
mathpf1a7a3752017-03-15 11:23:37121void PaymentRequest::OnPaymentResponseAvailable(
122 mojom::PaymentResponsePtr response) {
123 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16124}
125
mathp151bd312017-04-03 21:07:24126void PaymentRequest::OnShippingOptionIdSelected(
127 std::string shipping_option_id) {
128 client_->OnShippingOptionChange(shipping_option_id);
129}
130
131void PaymentRequest::OnShippingAddressSelected(
132 mojom::PaymentAddressPtr address) {
133 client_->OnShippingAddressChange(std::move(address));
134}
135
mathpf4bc50e2017-01-24 05:17:50136void PaymentRequest::UserCancelled() {
137 // If |client_| is not bound, then the object is already being destroyed as
138 // a result of a renderer event.
139 if (!client_.is_bound())
140 return;
141
142 // This sends an error to the renderer, which informs the API user.
mathpf709499d2017-01-09 20:48:36143 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50144
145 // We close all bindings and ask to be destroyed.
146 client_.reset();
147 binding_.Close();
148 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36149}
150
mathpf4bc50e2017-01-24 05:17:50151void PaymentRequest::OnConnectionTerminated() {
152 // We are here because of a browser-side error, or likely as a result of the
153 // connection_error_handler on |binding_|, which can mean that the renderer
154 // has decided to close the pipe for various reasons (see all uses of
155 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
156 // the binding and the dialog, and ask to be deleted.
157 client_.reset();
mathpf709499d2017-01-09 20:48:36158 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50159 delegate_->CloseDialog();
mathpf709499d2017-01-09 20:48:36160 manager_->DestroyRequest(this);
161}
162
mathpd4be8de82017-03-01 00:51:48163void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37164 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48165}
166
mathpf709499d2017-01-09 20:48:36167} // namespace payments