blob: 2cc645f189121f621636186a3c7f0213e6d0f42d [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"
rouslan6e3cf7c62017-04-17 21:23:2811#include "components/payments/content/origin_security_checker.h"
rouslan908248c2017-02-27 21:30:2412#include "components/payments/content/payment_details_validation.h"
13#include "components/payments/content/payment_request_web_contents_manager.h"
mathpf709499d2017-01-09 20:48:3614#include "content/public/browser/browser_thread.h"
15#include "content/public/browser/web_contents.h"
16
17namespace payments {
18
19PaymentRequest::PaymentRequest(
20 content::WebContents* web_contents,
21 std::unique_ptr<PaymentRequestDelegate> delegate,
22 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2823 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3724 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3625 : web_contents_(web_contents),
26 delegate_(std::move(delegate)),
27 manager_(manager),
mathp300fa542017-03-27 19:29:3728 binding_(this, std::move(request)),
29 observer_for_testing_(observer_for_testing) {
mathpf4bc50e2017-01-24 05:17:5030 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
31 // will happen as a result of many renderer-side events (both successful and
32 // erroneous in nature).
33 // TODO(crbug.com/683636): Investigate using
34 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
35 binding_.set_connection_error_handler(base::Bind(
36 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
mathpf709499d2017-01-09 20:48:3637}
38
39PaymentRequest::~PaymentRequest() {}
40
rouslan6e3cf7c62017-04-17 21:23:2841void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
42 std::vector<mojom::PaymentMethodDataPtr> method_data,
43 mojom::PaymentDetailsPtr details,
44 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3645 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2846 client_ = std::move(client);
47
48 if (!OriginSecurityChecker::IsOriginSecure(
49 delegate_->GetLastCommittedURL())) {
50 LOG(ERROR) << "Not in a secure origin";
51 OnConnectionTerminated();
52 return;
53 }
54
55 if (OriginSecurityChecker::IsSchemeCryptographic(
56 delegate_->GetLastCommittedURL()) &&
57 !delegate_->IsSslCertificateValid()) {
58 LOG(ERROR) << "SSL certificate is not valid";
59 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
60 // with "NotSupportedError".
61 spec_ = base::MakeUnique<PaymentRequestSpec>(
62 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
63 std::vector<mojom::PaymentMethodDataPtr>(), this,
64 delegate_->GetApplicationLocale());
65 state_ = base::MakeUnique<PaymentRequestState>(
66 spec_.get(), this, delegate_->GetApplicationLocale(),
67 delegate_->GetPersonalDataManager(), delegate_.get());
68 return;
69 }
70
mathpf709499d2017-01-09 20:48:3671 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2872 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3673 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5074 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3675 return;
76 }
rouslan6e3cf7c62017-04-17 21:23:2877
jinho.bangfcb5ec92017-03-29 08:08:0278 if (!details->total) {
79 LOG(ERROR) << "Missing total";
80 OnConnectionTerminated();
81 return;
82 }
rouslan6e3cf7c62017-04-17 21:23:2883
mathpf1a7a3752017-03-15 11:23:3784 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:3385 std::move(options), std::move(details), std::move(method_data), this,
86 delegate_->GetApplicationLocale());
87 state_ = base::MakeUnique<PaymentRequestState>(
88 spec_.get(), this, delegate_->GetApplicationLocale(),
anthonyvdd23ed702017-04-05 15:29:0089 delegate_->GetPersonalDataManager(), delegate_.get());
mathpf709499d2017-01-09 20:48:3690}
91
92void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:1093 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:5094 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
95 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:1096 return;
97 }
rouslan6e3cf7c62017-04-17 21:23:2898
99 if (!state_->AreRequestedMethodsSupported()) {
100 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
101 if (observer_for_testing_)
102 observer_for_testing_->OnNotSupportedError();
103 OnConnectionTerminated();
104 return;
105 }
106
mathpf4bc50e2017-01-24 05:17:50107 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36108}
109
mathp151bd31e2017-04-03 21:07:24110void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
111 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28112 if (!validatePaymentDetails(details, &error)) {
mathp151bd31e2017-04-03 21:07:24113 LOG(ERROR) << error;
114 OnConnectionTerminated();
115 return;
116 }
117 spec_->UpdateWith(std::move(details));
118}
119
mathpf4bc50e2017-01-24 05:17:50120void PaymentRequest::Abort() {
121 // The API user has decided to abort. We return a successful abort message to
122 // the renderer, which closes the Mojo message pipe, which triggers
123 // PaymentRequest::OnConnectionTerminated, which destroys this object.
124 if (client_.is_bound())
125 client_->OnAbort(true /* aborted_successfully */);
126}
127
mathp218795892017-03-29 15:15:34128void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16129 if (!client_.is_bound())
130 return;
131
mathp218795892017-03-29 15:15:34132 if (result != mojom::PaymentComplete::SUCCESS) {
133 delegate_->ShowErrorMessage();
134 } else {
135 // When the renderer closes the connection,
136 // PaymentRequest::OnConnectionTerminated will be called.
137 client_->OnComplete();
138 }
mathp4b85b582017-03-08 21:07:16139}
140
141void PaymentRequest::CanMakePayment() {
mathp1a5be4f2017-03-24 18:09:19142 // TODO(crbug.com/704676): Implement a quota policy for this method.
mathpf39f46d2017-03-24 22:03:47143 // PaymentRequest.canMakePayments() never returns false in incognito mode.
mathp1a5be4f2017-03-24 18:09:19144 client_->OnCanMakePayment(
mathpf39f46d2017-03-24 22:03:47145 delegate_->IsIncognito() || state()->CanMakePayment()
mathp1a5be4f2017-03-24 18:09:19146 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
147 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
mathp300fa542017-03-27 19:29:37148 if (observer_for_testing_)
149 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16150}
151
mathpf1a7a3752017-03-15 11:23:37152void PaymentRequest::OnPaymentResponseAvailable(
153 mojom::PaymentResponsePtr response) {
154 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16155}
156
mathp151bd31e2017-04-03 21:07:24157void PaymentRequest::OnShippingOptionIdSelected(
158 std::string shipping_option_id) {
159 client_->OnShippingOptionChange(shipping_option_id);
160}
161
162void PaymentRequest::OnShippingAddressSelected(
163 mojom::PaymentAddressPtr address) {
164 client_->OnShippingAddressChange(std::move(address));
165}
166
mathpf4bc50e2017-01-24 05:17:50167void PaymentRequest::UserCancelled() {
168 // If |client_| is not bound, then the object is already being destroyed as
169 // a result of a renderer event.
170 if (!client_.is_bound())
171 return;
172
173 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28174 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50175
176 // We close all bindings and ask to be destroyed.
177 client_.reset();
178 binding_.Close();
179 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36180}
181
mathpf4bc50e2017-01-24 05:17:50182void PaymentRequest::OnConnectionTerminated() {
183 // We are here because of a browser-side error, or likely as a result of the
184 // connection_error_handler on |binding_|, which can mean that the renderer
185 // has decided to close the pipe for various reasons (see all uses of
186 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
187 // the binding and the dialog, and ask to be deleted.
188 client_.reset();
mathpf709499d2017-01-09 20:48:36189 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50190 delegate_->CloseDialog();
mathpf709499d2017-01-09 20:48:36191 manager_->DestroyRequest(this);
192}
193
mathpd4be8de82017-03-01 00:51:48194void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37195 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48196}
197
mathpf709499d2017-01-09 20:48:36198} // namespace payments