blob: 68da7ff714a86963414c53c99fe4d04bb5fcbb54 [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)),
sebsg20b49d7b2017-05-04 20:23:1729 observer_for_testing_(observer_for_testing),
30 journey_logger_(delegate_->IsIncognito(),
31 web_contents_->GetLastCommittedURL(),
32 delegate_->GetUkmService()) {
mathpf4bc50e2017-01-24 05:17:5033 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
34 // will happen as a result of many renderer-side events (both successful and
35 // erroneous in nature).
36 // TODO(crbug.com/683636): Investigate using
37 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
38 binding_.set_connection_error_handler(base::Bind(
39 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
mathpf709499d2017-01-09 20:48:3640}
41
42PaymentRequest::~PaymentRequest() {}
43
rouslan6e3cf7c62017-04-17 21:23:2844void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
45 std::vector<mojom::PaymentMethodDataPtr> method_data,
46 mojom::PaymentDetailsPtr details,
47 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3648 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2849 client_ = std::move(client);
50
51 if (!OriginSecurityChecker::IsOriginSecure(
52 delegate_->GetLastCommittedURL())) {
53 LOG(ERROR) << "Not in a secure origin";
54 OnConnectionTerminated();
55 return;
56 }
57
58 if (OriginSecurityChecker::IsSchemeCryptographic(
59 delegate_->GetLastCommittedURL()) &&
60 !delegate_->IsSslCertificateValid()) {
61 LOG(ERROR) << "SSL certificate is not valid";
62 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
63 // with "NotSupportedError".
64 spec_ = base::MakeUnique<PaymentRequestSpec>(
65 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
66 std::vector<mojom::PaymentMethodDataPtr>(), this,
67 delegate_->GetApplicationLocale());
68 state_ = base::MakeUnique<PaymentRequestState>(
69 spec_.get(), this, delegate_->GetApplicationLocale(),
70 delegate_->GetPersonalDataManager(), delegate_.get());
71 return;
72 }
73
mathpf709499d2017-01-09 20:48:3674 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2875 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3676 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5077 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3678 return;
79 }
rouslan6e3cf7c62017-04-17 21:23:2880
jinho.bangfcb5ec92017-03-29 08:08:0281 if (!details->total) {
82 LOG(ERROR) << "Missing total";
83 OnConnectionTerminated();
84 return;
85 }
rouslan6e3cf7c62017-04-17 21:23:2886
mathpf1a7a3752017-03-15 11:23:3787 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:3388 std::move(options), std::move(details), std::move(method_data), this,
89 delegate_->GetApplicationLocale());
90 state_ = base::MakeUnique<PaymentRequestState>(
91 spec_.get(), this, delegate_->GetApplicationLocale(),
anthonyvdd23ed702017-04-05 15:29:0092 delegate_->GetPersonalDataManager(), delegate_.get());
mathpf709499d2017-01-09 20:48:3693}
94
95void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:1096 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:5097 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
98 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:1099 return;
100 }
rouslan6e3cf7c62017-04-17 21:23:28101
102 if (!state_->AreRequestedMethodsSupported()) {
103 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
104 if (observer_for_testing_)
105 observer_for_testing_->OnNotSupportedError();
106 OnConnectionTerminated();
107 return;
108 }
109
sebsg20b49d7b2017-05-04 20:23:17110 journey_logger_.SetShowCalled();
mathpf4bc50e2017-01-24 05:17:50111 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36112}
113
mathp151bd31e2017-04-03 21:07:24114void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
115 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28116 if (!validatePaymentDetails(details, &error)) {
mathp151bd31e2017-04-03 21:07:24117 LOG(ERROR) << error;
118 OnConnectionTerminated();
119 return;
120 }
121 spec_->UpdateWith(std::move(details));
122}
123
mathpf4bc50e2017-01-24 05:17:50124void PaymentRequest::Abort() {
125 // The API user has decided to abort. We return a successful abort message to
126 // the renderer, which closes the Mojo message pipe, which triggers
127 // PaymentRequest::OnConnectionTerminated, which destroys this object.
sebsg20b49d7b2017-05-04 20:23:17128 // TODO(crbug.com/716546): Add a merchant abort metric,
129 journey_logger_.RecordJourneyStatsHistograms(
130 JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED);
mathpf4bc50e2017-01-24 05:17:50131 if (client_.is_bound())
132 client_->OnAbort(true /* aborted_successfully */);
133}
134
mathp218795892017-03-29 15:15:34135void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16136 if (!client_.is_bound())
137 return;
138
mathp218795892017-03-29 15:15:34139 if (result != mojom::PaymentComplete::SUCCESS) {
140 delegate_->ShowErrorMessage();
141 } else {
sebsg20b49d7b2017-05-04 20:23:17142 journey_logger_.RecordJourneyStatsHistograms(
143 JourneyLogger::COMPLETION_STATUS_COMPLETED);
mathp218795892017-03-29 15:15:34144 // When the renderer closes the connection,
145 // PaymentRequest::OnConnectionTerminated will be called.
146 client_->OnComplete();
147 }
mathp4b85b582017-03-08 21:07:16148}
149
150void PaymentRequest::CanMakePayment() {
mathp1a5be4f2017-03-24 18:09:19151 // TODO(crbug.com/704676): Implement a quota policy for this method.
mathpf39f46d2017-03-24 22:03:47152 // PaymentRequest.canMakePayments() never returns false in incognito mode.
mathp1a5be4f2017-03-24 18:09:19153 client_->OnCanMakePayment(
mathpf39f46d2017-03-24 22:03:47154 delegate_->IsIncognito() || state()->CanMakePayment()
mathp1a5be4f2017-03-24 18:09:19155 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
156 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
sebsg20b49d7b2017-05-04 20:23:17157 journey_logger_.SetCanMakePaymentValue(delegate_->IsIncognito() ||
158 state()->CanMakePayment());
mathp300fa542017-03-27 19:29:37159 if (observer_for_testing_)
160 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16161}
162
mathpf1a7a3752017-03-15 11:23:37163void PaymentRequest::OnPaymentResponseAvailable(
164 mojom::PaymentResponsePtr response) {
165 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16166}
167
mathp151bd31e2017-04-03 21:07:24168void PaymentRequest::OnShippingOptionIdSelected(
169 std::string shipping_option_id) {
170 client_->OnShippingOptionChange(shipping_option_id);
171}
172
173void PaymentRequest::OnShippingAddressSelected(
174 mojom::PaymentAddressPtr address) {
175 client_->OnShippingAddressChange(std::move(address));
176}
177
mathpf4bc50e2017-01-24 05:17:50178void PaymentRequest::UserCancelled() {
179 // If |client_| is not bound, then the object is already being destroyed as
180 // a result of a renderer event.
181 if (!client_.is_bound())
182 return;
183
sebsg20b49d7b2017-05-04 20:23:17184 journey_logger_.RecordJourneyStatsHistograms(
185 JourneyLogger::COMPLETION_STATUS_USER_ABORTED);
186
mathpf4bc50e2017-01-24 05:17:50187 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28188 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50189
190 // We close all bindings and ask to be destroyed.
191 client_.reset();
192 binding_.Close();
193 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36194}
195
mathpf4bc50e2017-01-24 05:17:50196void PaymentRequest::OnConnectionTerminated() {
197 // We are here because of a browser-side error, or likely as a result of the
198 // connection_error_handler on |binding_|, which can mean that the renderer
199 // has decided to close the pipe for various reasons (see all uses of
200 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
201 // the binding and the dialog, and ask to be deleted.
202 client_.reset();
mathpf709499d2017-01-09 20:48:36203 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50204 delegate_->CloseDialog();
mathpf709499d2017-01-09 20:48:36205 manager_->DestroyRequest(this);
206}
207
mathpd4be8de82017-03-01 00:51:48208void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37209 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48210}
211
mathpf709499d2017-01-09 20:48:36212} // namespace payments