blob: 9e8950018cae22684c9d4d5bfb21a303f2d2b177 [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 {
sebsg20b49d7b2017-05-04 20:23:17177 journey_logger_.RecordJourneyStatsHistograms(
178 JourneyLogger::COMPLETION_STATUS_COMPLETED);
anthonyvd6a43b932017-05-11 18:39:27179 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
180 true);
mathp218795892017-03-29 15:15:34181 // When the renderer closes the connection,
182 // PaymentRequest::OnConnectionTerminated will be called.
183 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22184 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34185 }
mathp4b85b582017-03-08 21:07:16186}
187
188void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39189 bool can_make_payment = state()->CanMakePayment();
190 if (delegate_->IsIncognito()) {
191 client_->OnCanMakePayment(
192 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
193 journey_logger_.SetCanMakePaymentValue(true);
194 } else if (CanMakePaymentQueryFactory::GetInstance()
195 ->GetForContext(web_contents_->GetBrowserContext())
196 ->CanQuery(frame_origin_, spec()->stringified_method_data())) {
197 client_->OnCanMakePayment(
198 can_make_payment
199 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
200 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
201 journey_logger_.SetCanMakePaymentValue(can_make_payment);
202 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
203 client_->OnCanMakePayment(
204 can_make_payment
205 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
206 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
207 journey_logger_.SetCanMakePaymentValue(can_make_payment);
208 } else {
209 client_->OnCanMakePayment(
210 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
211 }
212
mathp300fa542017-03-27 19:29:37213 if (observer_for_testing_)
214 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16215}
216
mathpf1a7a3752017-03-15 11:23:37217void PaymentRequest::OnPaymentResponseAvailable(
218 mojom::PaymentResponsePtr response) {
219 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16220}
221
mathp151bd312017-04-03 21:07:24222void PaymentRequest::OnShippingOptionIdSelected(
223 std::string shipping_option_id) {
224 client_->OnShippingOptionChange(shipping_option_id);
225}
226
227void PaymentRequest::OnShippingAddressSelected(
228 mojom::PaymentAddressPtr address) {
229 client_->OnShippingAddressChange(std::move(address));
230}
231
mathpf4bc50e2017-01-24 05:17:50232void PaymentRequest::UserCancelled() {
233 // If |client_| is not bound, then the object is already being destroyed as
234 // a result of a renderer event.
235 if (!client_.is_bound())
236 return;
237
sebsg2c8558a2017-05-17 18:54:10238 RecordFirstCompletionStatus(JourneyLogger::COMPLETION_STATUS_USER_ABORTED);
sebsg20b49d7b2017-05-04 20:23:17239
mathpf4bc50e2017-01-24 05:17:50240 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28241 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50242
243 // We close all bindings and ask to be destroyed.
244 client_.reset();
245 binding_.Close();
rouslanb28f4532017-05-08 15:41:47246 if (observer_for_testing_)
247 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50248 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36249}
250
sebsg2c8558a2017-05-17 18:54:10251void PaymentRequest::DidStartNavigation(bool is_user_initiated) {
252 RecordFirstCompletionStatus(
253 is_user_initiated ? JourneyLogger::COMPLETION_STATUS_USER_ABORTED
254 : JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED);
255}
256
mathpf4bc50e2017-01-24 05:17:50257void PaymentRequest::OnConnectionTerminated() {
258 // We are here because of a browser-side error, or likely as a result of the
259 // connection_error_handler on |binding_|, which can mean that the renderer
260 // has decided to close the pipe for various reasons (see all uses of
261 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
262 // the binding and the dialog, and ask to be deleted.
263 client_.reset();
mathpf709499d2017-01-09 20:48:36264 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50265 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47266 if (observer_for_testing_)
267 observer_for_testing_->OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36268 manager_->DestroyRequest(this);
269}
270
mathpd4be8de82017-03-01 00:51:48271void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37272 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48273}
274
sebsg2c8558a2017-05-17 18:54:10275void PaymentRequest::RecordFirstCompletionStatus(
276 JourneyLogger::CompletionStatus completion_status) {
277 if (!has_recorded_abort_reason_) {
278 has_recorded_abort_reason_ = true;
279 journey_logger_.RecordJourneyStatsHistograms(completion_status);
280 }
281}
282
mathpf709499d2017-01-09 20:48:36283} // namespace payments