blob: 69a13a702eb5ee0749eedbd3a8a0fdc4cb1bedee [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"
rouslan690997682017-05-09 18:07:3911#include "components/payments/content/can_make_payment_query_factory.h"
rouslan6e3cf7c62017-04-17 21:23:2812#include "components/payments/content/origin_security_checker.h"
rouslan908248c2017-02-27 21:30:2413#include "components/payments/content/payment_details_validation.h"
14#include "components/payments/content/payment_request_web_contents_manager.h"
rouslan690997682017-05-09 18:07:3915#include "components/payments/core/can_make_payment_query.h"
mathpf709499d2017-01-09 20:48:3616#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3917#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3618#include "content/public/browser/web_contents.h"
19
20namespace payments {
21
22PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3923 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3624 content::WebContents* web_contents,
25 std::unique_ptr<PaymentRequestDelegate> delegate,
26 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2827 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3728 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3629 : web_contents_(web_contents),
30 delegate_(std::move(delegate)),
31 manager_(manager),
mathp300fa542017-03-27 19:29:3732 binding_(this, std::move(request)),
rouslan690997682017-05-09 18:07:3933 frame_origin_(GURL(render_frame_host->GetLastCommittedURL()).GetOrigin()),
sebsg20b49d7b2017-05-04 20:23:1734 observer_for_testing_(observer_for_testing),
35 journey_logger_(delegate_->IsIncognito(),
36 web_contents_->GetLastCommittedURL(),
37 delegate_->GetUkmService()) {
mathpf4bc50e2017-01-24 05:17:5038 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
39 // will happen as a result of many renderer-side events (both successful and
40 // erroneous in nature).
41 // TODO(crbug.com/683636): Investigate using
42 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
43 binding_.set_connection_error_handler(base::Bind(
44 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
mathpf709499d2017-01-09 20:48:3645}
46
47PaymentRequest::~PaymentRequest() {}
48
rouslan6e3cf7c62017-04-17 21:23:2849void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
50 std::vector<mojom::PaymentMethodDataPtr> method_data,
51 mojom::PaymentDetailsPtr details,
52 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3653 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2854 client_ = std::move(client);
55
rouslanb28f4532017-05-08 15:41:4756 const GURL last_committed_url = delegate_->GetLastCommittedURL();
57 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2858 LOG(ERROR) << "Not in a secure origin";
59 OnConnectionTerminated();
60 return;
61 }
62
rouslanb28f4532017-05-08 15:41:4763 bool allowed_origin =
64 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
65 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
66 if (!allowed_origin) {
67 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
68 "allowed";
69 }
70
71 bool invalid_ssl =
72 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
73 !delegate_->IsSslCertificateValid();
74 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2875 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4776
77 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2878 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
79 // with "NotSupportedError".
80 spec_ = base::MakeUnique<PaymentRequestSpec>(
81 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
82 std::vector<mojom::PaymentMethodDataPtr>(), this,
83 delegate_->GetApplicationLocale());
84 state_ = base::MakeUnique<PaymentRequestState>(
85 spec_.get(), this, delegate_->GetApplicationLocale(),
86 delegate_->GetPersonalDataManager(), delegate_.get());
87 return;
88 }
89
mathpf709499d2017-01-09 20:48:3690 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2891 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3692 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5093 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3694 return;
95 }
rouslan6e3cf7c62017-04-17 21:23:2896
jinho.bangfcb5ec92017-03-29 08:08:0297 if (!details->total) {
98 LOG(ERROR) << "Missing total";
99 OnConnectionTerminated();
100 return;
101 }
rouslan6e3cf7c62017-04-17 21:23:28102
mathpf1a7a3752017-03-15 11:23:37103 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33104 std::move(options), std::move(details), std::move(method_data), this,
105 delegate_->GetApplicationLocale());
106 state_ = base::MakeUnique<PaymentRequestState>(
107 spec_.get(), this, delegate_->GetApplicationLocale(),
anthonyvdd23ed702017-04-05 15:29:00108 delegate_->GetPersonalDataManager(), delegate_.get());
mathpf709499d2017-01-09 20:48:36109}
110
111void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10112 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50113 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
114 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10115 return;
116 }
rouslan6e3cf7c62017-04-17 21:23:28117
rouslan7d433cc22017-05-08 15:18:07118 // A tab can display only one PaymentRequest UI at a time.
119 if (!manager_->CanShow(this)) {
120 LOG(ERROR) << "A PaymentRequest UI is already showing";
121 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
122 OnConnectionTerminated();
123 return;
124 }
125
rouslan6e3cf7c62017-04-17 21:23:28126 if (!state_->AreRequestedMethodsSupported()) {
127 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
128 if (observer_for_testing_)
129 observer_for_testing_->OnNotSupportedError();
130 OnConnectionTerminated();
131 return;
132 }
133
sebsg20b49d7b2017-05-04 20:23:17134 journey_logger_.SetShowCalled();
mathpf4bc50e2017-01-24 05:17:50135 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36136}
137
mathp151bd312017-04-03 21:07:24138void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
139 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28140 if (!validatePaymentDetails(details, &error)) {
mathp151bd312017-04-03 21:07:24141 LOG(ERROR) << error;
142 OnConnectionTerminated();
143 return;
144 }
145 spec_->UpdateWith(std::move(details));
146}
147
mathpf4bc50e2017-01-24 05:17:50148void PaymentRequest::Abort() {
149 // The API user has decided to abort. We return a successful abort message to
150 // the renderer, which closes the Mojo message pipe, which triggers
151 // PaymentRequest::OnConnectionTerminated, which destroys this object.
sebsg20b49d7b2017-05-04 20:23:17152 // TODO(crbug.com/716546): Add a merchant abort metric,
153 journey_logger_.RecordJourneyStatsHistograms(
154 JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED);
mathpf4bc50e2017-01-24 05:17:50155 if (client_.is_bound())
156 client_->OnAbort(true /* aborted_successfully */);
157}
158
mathp218795892017-03-29 15:15:34159void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16160 if (!client_.is_bound())
161 return;
162
mathp218795892017-03-29 15:15:34163 if (result != mojom::PaymentComplete::SUCCESS) {
164 delegate_->ShowErrorMessage();
165 } else {
sebsg20b49d7b2017-05-04 20:23:17166 journey_logger_.RecordJourneyStatsHistograms(
167 JourneyLogger::COMPLETION_STATUS_COMPLETED);
mathp218795892017-03-29 15:15:34168 // When the renderer closes the connection,
169 // PaymentRequest::OnConnectionTerminated will be called.
170 client_->OnComplete();
171 }
mathp4b85b582017-03-08 21:07:16172}
173
174void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39175 bool can_make_payment = state()->CanMakePayment();
176 if (delegate_->IsIncognito()) {
177 client_->OnCanMakePayment(
178 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
179 journey_logger_.SetCanMakePaymentValue(true);
180 } else if (CanMakePaymentQueryFactory::GetInstance()
181 ->GetForContext(web_contents_->GetBrowserContext())
182 ->CanQuery(frame_origin_, spec()->stringified_method_data())) {
183 client_->OnCanMakePayment(
184 can_make_payment
185 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
186 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
187 journey_logger_.SetCanMakePaymentValue(can_make_payment);
188 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
189 client_->OnCanMakePayment(
190 can_make_payment
191 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
192 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
193 journey_logger_.SetCanMakePaymentValue(can_make_payment);
194 } else {
195 client_->OnCanMakePayment(
196 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
197 }
198
mathp300fa542017-03-27 19:29:37199 if (observer_for_testing_)
200 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16201}
202
mathpf1a7a3752017-03-15 11:23:37203void PaymentRequest::OnPaymentResponseAvailable(
204 mojom::PaymentResponsePtr response) {
205 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16206}
207
mathp151bd312017-04-03 21:07:24208void PaymentRequest::OnShippingOptionIdSelected(
209 std::string shipping_option_id) {
210 client_->OnShippingOptionChange(shipping_option_id);
211}
212
213void PaymentRequest::OnShippingAddressSelected(
214 mojom::PaymentAddressPtr address) {
215 client_->OnShippingAddressChange(std::move(address));
216}
217
mathpf4bc50e2017-01-24 05:17:50218void PaymentRequest::UserCancelled() {
219 // If |client_| is not bound, then the object is already being destroyed as
220 // a result of a renderer event.
221 if (!client_.is_bound())
222 return;
223
sebsg20b49d7b2017-05-04 20:23:17224 journey_logger_.RecordJourneyStatsHistograms(
225 JourneyLogger::COMPLETION_STATUS_USER_ABORTED);
226
mathpf4bc50e2017-01-24 05:17:50227 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28228 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50229
230 // We close all bindings and ask to be destroyed.
231 client_.reset();
232 binding_.Close();
rouslanb28f4532017-05-08 15:41:47233 if (observer_for_testing_)
234 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50235 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36236}
237
mathpf4bc50e2017-01-24 05:17:50238void PaymentRequest::OnConnectionTerminated() {
239 // We are here because of a browser-side error, or likely as a result of the
240 // connection_error_handler on |binding_|, which can mean that the renderer
241 // has decided to close the pipe for various reasons (see all uses of
242 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
243 // the binding and the dialog, and ask to be deleted.
244 client_.reset();
mathpf709499d2017-01-09 20:48:36245 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50246 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47247 if (observer_for_testing_)
248 observer_for_testing_->OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36249 manager_->DestroyRequest(this);
250}
251
mathpd4be8de82017-03-01 00:51:48252void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37253 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48254}
255
mathpf709499d2017-01-09 20:48:36256} // namespace payments