blob: 0569efcc79278e2be094adddec8fcf9076507e9b [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
rouslan908248c2017-02-27 21:30:247#include <utility>
mathpf709499d2017-01-09 20:48:368
tmartino68c0a272017-01-19 17:44:089#include "base/memory/ptr_util.h"
rouslan908248c2017-02-27 21:30:2410#include "components/payments/content/payment_details_validation.h"
11#include "components/payments/content/payment_request_web_contents_manager.h"
mathpf709499d2017-01-09 20:48:3612#include "content/public/browser/browser_thread.h"
13#include "content/public/browser/web_contents.h"
14
15namespace payments {
16
17PaymentRequest::PaymentRequest(
18 content::WebContents* web_contents,
19 std::unique_ptr<PaymentRequestDelegate> delegate,
20 PaymentRequestWebContentsManager* manager,
mathp300fa542017-03-27 19:29:3721 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request,
22 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3623 : web_contents_(web_contents),
24 delegate_(std::move(delegate)),
25 manager_(manager),
mathp300fa542017-03-27 19:29:3726 binding_(this, std::move(request)),
27 observer_for_testing_(observer_for_testing) {
mathpf4bc50e2017-01-24 05:17:5028 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
29 // will happen as a result of many renderer-side events (both successful and
30 // erroneous in nature).
31 // TODO(crbug.com/683636): Investigate using
32 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
33 binding_.set_connection_error_handler(base::Bind(
34 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
mathpf709499d2017-01-09 20:48:3635}
36
37PaymentRequest::~PaymentRequest() {}
38
39void PaymentRequest::Init(
40 payments::mojom::PaymentRequestClientPtr client,
mathpc2d07f962017-02-17 18:33:5141 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
mathpf709499d2017-01-09 20:48:3642 payments::mojom::PaymentDetailsPtr details,
43 payments::mojom::PaymentOptionsPtr options) {
44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
45 std::string error;
46 if (!payments::validatePaymentDetails(details, &error)) {
47 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5048 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3649 return;
50 }
jinho.bangfcb5ec92017-03-29 08:08:0251 if (!details->total) {
52 LOG(ERROR) << "Missing total";
53 OnConnectionTerminated();
54 return;
55 }
mathpf709499d2017-01-09 20:48:3656 client_ = std::move(client);
mathpf1a7a3752017-03-15 11:23:3757 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:3358 std::move(options), std::move(details), std::move(method_data), this,
59 delegate_->GetApplicationLocale());
60 state_ = base::MakeUnique<PaymentRequestState>(
61 spec_.get(), this, delegate_->GetApplicationLocale(),
62 delegate_->GetPersonalDataManager());
mathpf709499d2017-01-09 20:48:3663}
64
65void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:1066 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:5067 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
68 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:1069 return;
70 }
mathpf4bc50e2017-01-24 05:17:5071 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:3672}
73
mathp151bd312017-04-03 21:07:2474void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
75 std::string error;
76 if (!payments::validatePaymentDetails(details, &error)) {
77 LOG(ERROR) << error;
78 OnConnectionTerminated();
79 return;
80 }
81 spec_->UpdateWith(std::move(details));
82}
83
mathpf4bc50e2017-01-24 05:17:5084void PaymentRequest::Abort() {
85 // The API user has decided to abort. We return a successful abort message to
86 // the renderer, which closes the Mojo message pipe, which triggers
87 // PaymentRequest::OnConnectionTerminated, which destroys this object.
88 if (client_.is_bound())
89 client_->OnAbort(true /* aborted_successfully */);
90}
91
mathp218795892017-03-29 15:15:3492void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:1693 if (!client_.is_bound())
94 return;
95
mathp218795892017-03-29 15:15:3496 if (result != mojom::PaymentComplete::SUCCESS) {
97 delegate_->ShowErrorMessage();
98 } else {
99 // When the renderer closes the connection,
100 // PaymentRequest::OnConnectionTerminated will be called.
101 client_->OnComplete();
102 }
mathp4b85b582017-03-08 21:07:16103}
104
105void PaymentRequest::CanMakePayment() {
mathp1a5be4f2017-03-24 18:09:19106 // TODO(crbug.com/704676): Implement a quota policy for this method.
mathpf39f46d2017-03-24 22:03:47107 // PaymentRequest.canMakePayments() never returns false in incognito mode.
mathp1a5be4f2017-03-24 18:09:19108 client_->OnCanMakePayment(
mathpf39f46d2017-03-24 22:03:47109 delegate_->IsIncognito() || state()->CanMakePayment()
mathp1a5be4f2017-03-24 18:09:19110 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
111 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
mathp300fa542017-03-27 19:29:37112 if (observer_for_testing_)
113 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16114}
115
mathpf1a7a3752017-03-15 11:23:37116void PaymentRequest::OnInvalidSpecProvided() {
117 OnConnectionTerminated();
118}
119
mathpf1a7a3752017-03-15 11:23:37120void PaymentRequest::OnPaymentResponseAvailable(
121 mojom::PaymentResponsePtr response) {
122 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16123}
124
mathp151bd312017-04-03 21:07:24125void PaymentRequest::OnShippingOptionIdSelected(
126 std::string shipping_option_id) {
127 client_->OnShippingOptionChange(shipping_option_id);
128}
129
130void PaymentRequest::OnShippingAddressSelected(
131 mojom::PaymentAddressPtr address) {
132 client_->OnShippingAddressChange(std::move(address));
133}
134
mathpf4bc50e2017-01-24 05:17:50135void PaymentRequest::UserCancelled() {
136 // If |client_| is not bound, then the object is already being destroyed as
137 // a result of a renderer event.
138 if (!client_.is_bound())
139 return;
140
141 // This sends an error to the renderer, which informs the API user.
mathpf709499d2017-01-09 20:48:36142 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50143
144 // We close all bindings and ask to be destroyed.
145 client_.reset();
146 binding_.Close();
147 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36148}
149
mathpf4bc50e2017-01-24 05:17:50150void PaymentRequest::OnConnectionTerminated() {
151 // We are here because of a browser-side error, or likely as a result of the
152 // connection_error_handler on |binding_|, which can mean that the renderer
153 // has decided to close the pipe for various reasons (see all uses of
154 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
155 // the binding and the dialog, and ask to be deleted.
156 client_.reset();
mathpf709499d2017-01-09 20:48:36157 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50158 delegate_->CloseDialog();
mathpf709499d2017-01-09 20:48:36159 manager_->DestroyRequest(this);
160}
161
mathpd4be8de82017-03-01 00:51:48162void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37163 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48164}
165
mathpf709499d2017-01-09 20:48:36166} // namespace payments