blob: 852d877f889932c288f0aa0fff98927d9600cc65 [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"
anthonyvd6a43b932017-05-11 18:39:2716#include "components/payments/core/payment_prefs.h"
17#include "components/prefs/pref_service.h"
mathpf709499d2017-01-09 20:48:3618#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3919#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3620#include "content/public/browser/web_contents.h"
21
22namespace payments {
23
24PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3925 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3626 content::WebContents* web_contents,
27 std::unique_ptr<PaymentRequestDelegate> delegate,
28 PaymentRequestWebContentsManager* manager,
rouslan6e3cf7c62017-04-17 21:23:2829 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3730 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3631 : web_contents_(web_contents),
32 delegate_(std::move(delegate)),
33 manager_(manager),
mathp300fa542017-03-27 19:29:3734 binding_(this, std::move(request)),
rouslan690997682017-05-09 18:07:3935 frame_origin_(GURL(render_frame_host->GetLastCommittedURL()).GetOrigin()),
sebsg20b49d7b2017-05-04 20:23:1736 observer_for_testing_(observer_for_testing),
37 journey_logger_(delegate_->IsIncognito(),
38 web_contents_->GetLastCommittedURL(),
oysteineb068f272017-05-23 00:14:0139 delegate_->GetUkmRecorder()) {
mathpf4bc50e2017-01-24 05:17:5040 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
41 // will happen as a result of many renderer-side events (both successful and
42 // erroneous in nature).
43 // TODO(crbug.com/683636): Investigate using
44 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
45 binding_.set_connection_error_handler(base::Bind(
46 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
mathpf709499d2017-01-09 20:48:3647}
48
49PaymentRequest::~PaymentRequest() {}
50
rouslan6e3cf7c62017-04-17 21:23:2851void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
52 std::vector<mojom::PaymentMethodDataPtr> method_data,
53 mojom::PaymentDetailsPtr details,
54 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3655 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2856 client_ = std::move(client);
57
rouslanb28f4532017-05-08 15:41:4758 const GURL last_committed_url = delegate_->GetLastCommittedURL();
59 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2860 LOG(ERROR) << "Not in a secure origin";
61 OnConnectionTerminated();
62 return;
63 }
64
rouslanb28f4532017-05-08 15:41:4765 bool allowed_origin =
66 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
67 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
68 if (!allowed_origin) {
69 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
70 "allowed";
71 }
72
73 bool invalid_ssl =
74 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
75 !delegate_->IsSslCertificateValid();
76 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2877 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4778
79 if (!allowed_origin || invalid_ssl) {
rouslan6e3cf7c62017-04-17 21:23:2880 // Don't show UI. Resolve .canMakepayment() with "false". Reject .show()
81 // with "NotSupportedError".
82 spec_ = base::MakeUnique<PaymentRequestSpec>(
83 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(),
84 std::vector<mojom::PaymentMethodDataPtr>(), this,
85 delegate_->GetApplicationLocale());
86 state_ = base::MakeUnique<PaymentRequestState>(
87 spec_.get(), this, delegate_->GetApplicationLocale(),
88 delegate_->GetPersonalDataManager(), delegate_.get());
89 return;
90 }
91
mathpf709499d2017-01-09 20:48:3692 std::string error;
rouslan6e3cf7c62017-04-17 21:23:2893 if (!validatePaymentDetails(details, &error)) {
mathpf709499d2017-01-09 20:48:3694 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5095 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3696 return;
97 }
rouslan6e3cf7c62017-04-17 21:23:2898
jinho.bangfcb5ec92017-03-29 08:08:0299 if (!details->total) {
100 LOG(ERROR) << "Missing total";
101 OnConnectionTerminated();
102 return;
103 }
rouslan6e3cf7c62017-04-17 21:23:28104
mathpf1a7a3752017-03-15 11:23:37105 spec_ = base::MakeUnique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33106 std::move(options), std::move(details), std::move(method_data), this,
107 delegate_->GetApplicationLocale());
108 state_ = base::MakeUnique<PaymentRequestState>(
109 spec_.get(), this, delegate_->GetApplicationLocale(),
anthonyvdd23ed702017-04-05 15:29:00110 delegate_->GetPersonalDataManager(), delegate_.get());
mathpf709499d2017-01-09 20:48:36111}
112
113void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:10114 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50115 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
116 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10117 return;
118 }
rouslan6e3cf7c62017-04-17 21:23:28119
rouslan7d433cc22017-05-08 15:18:07120 // A tab can display only one PaymentRequest UI at a time.
121 if (!manager_->CanShow(this)) {
122 LOG(ERROR) << "A PaymentRequest UI is already showing";
123 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
124 OnConnectionTerminated();
125 return;
126 }
127
rouslan6e3cf7c62017-04-17 21:23:28128 if (!state_->AreRequestedMethodsSupported()) {
129 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
130 if (observer_for_testing_)
131 observer_for_testing_->OnNotSupportedError();
132 OnConnectionTerminated();
133 return;
134 }
135
sebsg20b49d7b2017-05-04 20:23:17136 journey_logger_.SetShowCalled();
mathpf4bc50e2017-01-24 05:17:50137 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:36138}
139
mathp151bd312017-04-03 21:07:24140void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
141 std::string error;
rouslan6e3cf7c62017-04-17 21:23:28142 if (!validatePaymentDetails(details, &error)) {
mathp151bd312017-04-03 21:07:24143 LOG(ERROR) << error;
144 OnConnectionTerminated();
145 return;
146 }
147 spec_->UpdateWith(std::move(details));
148}
149
mathpf4bc50e2017-01-24 05:17:50150void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56151 // The API user has decided to abort. If a successful abort message is
152 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50153 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56154 // Otherwise, the abort promise is rejected and the pipe is not closed.
155 // The abort is only successful if the payment app wasn't yet invoked.
156 // TODO(crbug.com/716546): Add a merchant abort metric
157
158 bool accepting_abort = !state_->IsPaymentAppInvoked();
159 if (accepting_abort) {
160 RecordFirstCompletionStatus(JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED);
161 }
162
mathpf4bc50e2017-01-24 05:17:50163 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56164 client_->OnAbort(accepting_abort);
165
166 if (observer_for_testing_)
167 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50168}
169
mathp218795892017-03-29 15:15:34170void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16171 if (!client_.is_bound())
172 return;
173
mathp218795892017-03-29 15:15:34174 if (result != mojom::PaymentComplete::SUCCESS) {
175 delegate_->ShowErrorMessage();
176 } else {
sebsgf8272a22017-05-26 14:32:58177 journey_logger_.SetCompleted();
anthonyvd6a43b932017-05-11 18:39:27178 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
179 true);
mathp218795892017-03-29 15:15:34180 // When the renderer closes the connection,
181 // PaymentRequest::OnConnectionTerminated will be called.
182 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22183 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34184 }
mathp4b85b582017-03-08 21:07:16185}
186
187void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39188 bool can_make_payment = state()->CanMakePayment();
189 if (delegate_->IsIncognito()) {
190 client_->OnCanMakePayment(
191 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
192 journey_logger_.SetCanMakePaymentValue(true);
193 } else if (CanMakePaymentQueryFactory::GetInstance()
194 ->GetForContext(web_contents_->GetBrowserContext())
195 ->CanQuery(frame_origin_, spec()->stringified_method_data())) {
196 client_->OnCanMakePayment(
197 can_make_payment
198 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
199 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
200 journey_logger_.SetCanMakePaymentValue(can_make_payment);
201 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
202 client_->OnCanMakePayment(
203 can_make_payment
204 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
205 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
206 journey_logger_.SetCanMakePaymentValue(can_make_payment);
207 } else {
208 client_->OnCanMakePayment(
209 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
210 }
211
mathp300fa542017-03-27 19:29:37212 if (observer_for_testing_)
213 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16214}
215
mathpf1a7a3752017-03-15 11:23:37216void PaymentRequest::OnPaymentResponseAvailable(
217 mojom::PaymentResponsePtr response) {
218 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16219}
220
mathp151bd312017-04-03 21:07:24221void PaymentRequest::OnShippingOptionIdSelected(
222 std::string shipping_option_id) {
223 client_->OnShippingOptionChange(shipping_option_id);
224}
225
226void PaymentRequest::OnShippingAddressSelected(
227 mojom::PaymentAddressPtr address) {
228 client_->OnShippingAddressChange(std::move(address));
229}
230
mathpf4bc50e2017-01-24 05:17:50231void PaymentRequest::UserCancelled() {
232 // If |client_| is not bound, then the object is already being destroyed as
233 // a result of a renderer event.
234 if (!client_.is_bound())
235 return;
236
sebsg2c8558a2017-05-17 18:54:10237 RecordFirstCompletionStatus(JourneyLogger::COMPLETION_STATUS_USER_ABORTED);
sebsg20b49d7b2017-05-04 20:23:17238
mathpf4bc50e2017-01-24 05:17:50239 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28240 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50241
242 // We close all bindings and ask to be destroyed.
243 client_.reset();
244 binding_.Close();
rouslanb28f4532017-05-08 15:41:47245 if (observer_for_testing_)
246 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50247 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36248}
249
sebsg2c8558a2017-05-17 18:54:10250void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
251 RecordFirstCompletionStatus(
252 is_user_initiated ? JourneyLogger::COMPLETION_STATUS_USER_ABORTED
253 : JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED);
254}
255
mathpf4bc50e2017-01-24 05:17:50256void PaymentRequest::OnConnectionTerminated() {
257 // We are here because of a browser-side error, or likely as a result of the
258 // connection_error_handler on |binding_|, which can mean that the renderer
259 // has decided to close the pipe for various reasons (see all uses of
260 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
261 // the binding and the dialog, and ask to be deleted.
262 client_.reset();
mathpf709499d2017-01-09 20:48:36263 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50264 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47265 if (observer_for_testing_)
266 observer_for_testing_->OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36267 manager_->DestroyRequest(this);
268}
269
mathpd4be8de82017-03-01 00:51:48270void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37271 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48272}
273
sebsg2c8558a2017-05-17 18:54:10274void PaymentRequest::RecordFirstCompletionStatus(
275 JourneyLogger::CompletionStatus completion_status) {
276 if (!has_recorded_abort_reason_) {
277 has_recorded_abort_reason_ = true;
sebsgf8272a22017-05-26 14:32:58278 // TODO(crbug.com/716546): Record more abort reasons.
279 if (completion_status == JourneyLogger::COMPLETION_STATUS_USER_ABORTED) {
280 journey_logger_.SetAborted(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
281 } else {
282 journey_logger_.SetAborted(JourneyLogger::ABORT_REASON_OTHER);
283 }
sebsg2c8558a2017-05-17 18:54:10284 }
285}
286
mathpf709499d2017-01-09 20:48:36287} // namespace payments