blob: 138b6d15946b618802d2b60ab1b93241e67bc24f [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
rouslanb28f4532017-05-08 15:41:4751 const GURL last_committed_url = delegate_->GetLastCommittedURL();
52 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2853 LOG(ERROR) << "Not in a secure origin";
54 OnConnectionTerminated();
55 return;
56 }
57
rouslanb28f4532017-05-08 15:41:4758 bool allowed_origin =
59 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
60 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
61 if (!allowed_origin) {
62 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
63 "allowed";
64 }
65
66 bool invalid_ssl =
67 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
68 !delegate_->IsSslCertificateValid();
69 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2870 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4771
72 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2873 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
74 // with "NotSupportedError".
75 spec_ = base::MakeUnique<PaymentRequestSpec>(
76 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
77 std::vector<mojom::PaymentMethodDataPtr>(), this,
78 delegate_->GetApplicationLocale());
79 state_ = base::MakeUnique<PaymentRequestState>(
80 spec_.get(), this, delegate_->GetApplicationLocale(),
81 delegate_->GetPersonalDataManager(), delegate_.get());
82 return;
83 }
84
mathpf709499d2017-01-09 20:48:3685 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2886 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3687 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5088 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3689 return;
90 }
rouslan6e3cf7c62017-04-17 21:23:2891
jinho.bangfcb5ec92017-03-29 08:08:0292 if (!details->total) {
93 LOG(ERROR) << "Missing total";
94 OnConnectionTerminated();
95 return;
96 }
rouslan6e3cf7c62017-04-17 21:23:2897
mathpf1a7a3752017-03-15 11:23:3798 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:3399 std::move(options), std::move(details), std::move(method_data), this,
100 delegate_->GetApplicationLocale());
101 state_ = base::MakeUnique<PaymentRequestState>(
102 spec_.get(), this, delegate_->GetApplicationLocale(),
anthonyvdd23ed702017-04-05 15:29:00103 delegate_->GetPersonalDataManager(), delegate_.get());
mathpf709499d2017-01-09 20:48:36104}
105
106void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10107 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50108 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
109 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10110 return;
111 }
rouslan6e3cf7c62017-04-17 21:23:28112
rouslan7d433cc22017-05-08 15:18:07113 // A tab can display only one PaymentRequest UI at a time.
114 if (!manager_->CanShow(this)) {
115 LOG(ERROR) << "A PaymentRequest UI is already showing";
116 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
117 OnConnectionTerminated();
118 return;
119 }
120
rouslan6e3cf7c62017-04-17 21:23:28121 if (!state_->AreRequestedMethodsSupported()) {
122 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
123 if (observer_for_testing_)
124 observer_for_testing_->OnNotSupportedError();
125 OnConnectionTerminated();
126 return;
127 }
128
sebsg20b49d7b2017-05-04 20:23:17129 journey_logger_.SetShowCalled();
mathpf4bc50e2017-01-24 05:17:50130 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36131}
132
mathp151bd312017-04-03 21:07:24133void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
134 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28135 if (!validatePaymentDetails(details, &error)) {
mathp151bd312017-04-03 21:07:24136 LOG(ERROR) << error;
137 OnConnectionTerminated();
138 return;
139 }
140 spec_->UpdateWith(std::move(details));
141}
142
mathpf4bc50e2017-01-24 05:17:50143void PaymentRequest::Abort() {
144 // The API user has decided to abort. We return a successful abort message to
145 // the renderer, which closes the Mojo message pipe, which triggers
146 // PaymentRequest::OnConnectionTerminated, which destroys this object.
sebsg20b49d7b2017-05-04 20:23:17147 // TODO(crbug.com/716546): Add a merchant abort metric,
148 journey_logger_.RecordJourneyStatsHistograms(
149 JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED);
mathpf4bc50e2017-01-24 05:17:50150 if (client_.is_bound())
151 client_->OnAbort(true /* aborted_successfully */);
152}
153
mathp218795892017-03-29 15:15:34154void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16155 if (!client_.is_bound())
156 return;
157
mathp218795892017-03-29 15:15:34158 if (result != mojom::PaymentComplete::SUCCESS) {
159 delegate_->ShowErrorMessage();
160 } else {
sebsg20b49d7b2017-05-04 20:23:17161 journey_logger_.RecordJourneyStatsHistograms(
162 JourneyLogger::COMPLETION_STATUS_COMPLETED);
mathp218795892017-03-29 15:15:34163 // When the renderer closes the connection,
164 // PaymentRequest::OnConnectionTerminated will be called.
165 client_->OnComplete();
166 }
mathp4b85b582017-03-08 21:07:16167}
168
169void PaymentRequest::CanMakePayment() {
mathp1a5be4f2017-03-24 18:09:19170 // TODO(crbug.com/704676): Implement a quota policy for this method.
mathpf39f46d2017-03-24 22:03:47171 // PaymentRequest.canMakePayments() never returns false in incognito mode.
mathp1a5be4f2017-03-24 18:09:19172 client_->OnCanMakePayment(
mathpf39f46d2017-03-24 22:03:47173 delegate_->IsIncognito() || state()->CanMakePayment()
mathp1a5be4f2017-03-24 18:09:19174 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
175 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
sebsg20b49d7b2017-05-04 20:23:17176 journey_logger_.SetCanMakePaymentValue(delegate_->IsIncognito() ||
177 state()->CanMakePayment());
mathp300fa542017-03-27 19:29:37178 if (observer_for_testing_)
179 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16180}
181
mathpf1a7a3752017-03-15 11:23:37182void PaymentRequest::OnPaymentResponseAvailable(
183 mojom::PaymentResponsePtr response) {
184 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16185}
186
mathp151bd312017-04-03 21:07:24187void PaymentRequest::OnShippingOptionIdSelected(
188 std::string shipping_option_id) {
189 client_->OnShippingOptionChange(shipping_option_id);
190}
191
192void PaymentRequest::OnShippingAddressSelected(
193 mojom::PaymentAddressPtr address) {
194 client_->OnShippingAddressChange(std::move(address));
195}
196
mathpf4bc50e2017-01-24 05:17:50197void PaymentRequest::UserCancelled() {
198 // If |client_| is not bound, then the object is already being destroyed as
199 // a result of a renderer event.
200 if (!client_.is_bound())
201 return;
202
sebsg20b49d7b2017-05-04 20:23:17203 journey_logger_.RecordJourneyStatsHistograms(
204 JourneyLogger::COMPLETION_STATUS_USER_ABORTED);
205
mathpf4bc50e2017-01-24 05:17:50206 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28207 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50208
209 // We close all bindings and ask to be destroyed.
210 client_.reset();
211 binding_.Close();
rouslanb28f4532017-05-08 15:41:47212 if (observer_for_testing_)
213 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50214 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36215}
216
mathpf4bc50e2017-01-24 05:17:50217void PaymentRequest::OnConnectionTerminated() {
218 // We are here because of a browser-side error, or likely as a result of the
219 // connection_error_handler on |binding_|, which can mean that the renderer
220 // has decided to close the pipe for various reasons (see all uses of
221 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
222 // the binding and the dialog, and ask to be deleted.
223 client_.reset();
mathpf709499d2017-01-09 20:48:36224 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50225 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47226 if (observer_for_testing_)
227 observer_for_testing_->OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36228 manager_->DestroyRequest(this);
229}
230
mathpd4be8de82017-03-01 00:51:48231void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37232 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48233}
234
mathpf709499d2017-01-09 20:48:36235} // namespace payments