blob: aa9b1fb0dce703b91abeb398db3201e9e0ac8296 [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
mathpf4bc50e2017-01-24 05:17:5074void PaymentRequest::Abort() {
75 // The API user has decided to abort. We return a successful abort message to
76 // the renderer, which closes the Mojo message pipe, which triggers
77 // PaymentRequest::OnConnectionTerminated, which destroys this object.
78 if (client_.is_bound())
79 client_->OnAbort(true /* aborted_successfully */);
80}
81
mathp218795892017-03-29 15:15:3482void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:1683 if (!client_.is_bound())
84 return;
85
mathp218795892017-03-29 15:15:3486 if (result != mojom::PaymentComplete::SUCCESS) {
87 delegate_->ShowErrorMessage();
88 } else {
89 // When the renderer closes the connection,
90 // PaymentRequest::OnConnectionTerminated will be called.
91 client_->OnComplete();
92 }
mathp4b85b582017-03-08 21:07:1693}
94
95void PaymentRequest::CanMakePayment() {
mathp1a5be4f2017-03-24 18:09:1996 // TODO(crbug.com/704676): Implement a quota policy for this method.
mathpf39f46d2017-03-24 22:03:4797 // PaymentRequest.canMakePayments() never returns false in incognito mode.
mathp1a5be4f2017-03-24 18:09:1998 client_->OnCanMakePayment(
mathpf39f46d2017-03-24 22:03:4799 delegate_->IsIncognito() || state()->CanMakePayment()
mathp1a5be4f2017-03-24 18:09:19100 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
101 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
mathp300fa542017-03-27 19:29:37102 if (observer_for_testing_)
103 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16104}
105
mathpf1a7a3752017-03-15 11:23:37106void PaymentRequest::OnInvalidSpecProvided() {
107 OnConnectionTerminated();
108}
109
mathpf1a7a3752017-03-15 11:23:37110void PaymentRequest::OnPaymentResponseAvailable(
111 mojom::PaymentResponsePtr response) {
112 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16113}
114
mathpf4bc50e2017-01-24 05:17:50115void PaymentRequest::UserCancelled() {
116 // If |client_| is not bound, then the object is already being destroyed as
117 // a result of a renderer event.
118 if (!client_.is_bound())
119 return;
120
121 // This sends an error to the renderer, which informs the API user.
mathpf709499d2017-01-09 20:48:36122 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50123
124 // We close all bindings and ask to be destroyed.
125 client_.reset();
126 binding_.Close();
127 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36128}
129
mathpf4bc50e2017-01-24 05:17:50130void PaymentRequest::OnConnectionTerminated() {
131 // We are here because of a browser-side error, or likely as a result of the
132 // connection_error_handler on |binding_|, which can mean that the renderer
133 // has decided to close the pipe for various reasons (see all uses of
134 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
135 // the binding and the dialog, and ask to be deleted.
136 client_.reset();
mathpf709499d2017-01-09 20:48:36137 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50138 delegate_->CloseDialog();
mathpf709499d2017-01-09 20:48:36139 manager_->DestroyRequest(this);
140}
141
mathpd4be8de82017-03-01 00:51:48142void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37143 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48144}
145
mathpf709499d2017-01-09 20:48:36146} // namespace payments