blob: 7b84a8c0472da9c865f3d7ca5b5b10e6482c5c7f [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(),
39 delegate_->GetUkmService()) {
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() {
151 // The API user has decided to abort. We return a successful abort message to
152 // the renderer, which closes the Mojo message pipe, which triggers
153 // PaymentRequest::OnConnectionTerminated, which destroys this object.
sebsg20b49d7b2017-05-04 20:23:17154 // TODO(crbug.com/716546): Add a merchant abort metric,
155 journey_logger_.RecordJourneyStatsHistograms(
156 JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED);
mathpf4bc50e2017-01-24 05:17:50157 if (client_.is_bound())
158 client_->OnAbort(true /* aborted_successfully */);
159}
160
mathp218795892017-03-29 15:15:34161void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16162 if (!client_.is_bound())
163 return;
164
mathp218795892017-03-29 15:15:34165 if (result != mojom::PaymentComplete::SUCCESS) {
166 delegate_->ShowErrorMessage();
167 } else {
sebsg20b49d7b2017-05-04 20:23:17168 journey_logger_.RecordJourneyStatsHistograms(
169 JourneyLogger::COMPLETION_STATUS_COMPLETED);
anthonyvd6a43b932017-05-11 18:39:27170 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
171 true);
mathp218795892017-03-29 15:15:34172 // When the renderer closes the connection,
173 // PaymentRequest::OnConnectionTerminated will be called.
174 client_->OnComplete();
175 }
mathp4b85b582017-03-08 21:07:16176}
177
178void PaymentRequest::CanMakePayment() {
rouslan690997682017-05-09 18:07:39179 bool can_make_payment = state()->CanMakePayment();
180 if (delegate_->IsIncognito()) {
181 client_->OnCanMakePayment(
182 mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT);
183 journey_logger_.SetCanMakePaymentValue(true);
184 } else if (CanMakePaymentQueryFactory::GetInstance()
185 ->GetForContext(web_contents_->GetBrowserContext())
186 ->CanQuery(frame_origin_, spec()->stringified_method_data())) {
187 client_->OnCanMakePayment(
188 can_make_payment
189 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT
190 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT);
191 journey_logger_.SetCanMakePaymentValue(can_make_payment);
192 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
193 client_->OnCanMakePayment(
194 can_make_payment
195 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
196 : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT);
197 journey_logger_.SetCanMakePaymentValue(can_make_payment);
198 } else {
199 client_->OnCanMakePayment(
200 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
201 }
202
mathp300fa542017-03-27 19:29:37203 if (observer_for_testing_)
204 observer_for_testing_->OnCanMakePaymentCalled();
mathp4b85b582017-03-08 21:07:16205}
206
mathpf1a7a3752017-03-15 11:23:37207void PaymentRequest::OnPaymentResponseAvailable(
208 mojom::PaymentResponsePtr response) {
209 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16210}
211
mathp151bd312017-04-03 21:07:24212void PaymentRequest::OnShippingOptionIdSelected(
213 std::string shipping_option_id) {
214 client_->OnShippingOptionChange(shipping_option_id);
215}
216
217void PaymentRequest::OnShippingAddressSelected(
218 mojom::PaymentAddressPtr address) {
219 client_->OnShippingAddressChange(std::move(address));
220}
221
mathpf4bc50e2017-01-24 05:17:50222void PaymentRequest::UserCancelled() {
223 // If |client_| is not bound, then the object is already being destroyed as
224 // a result of a renderer event.
225 if (!client_.is_bound())
226 return;
227
sebsg20b49d7b2017-05-04 20:23:17228 journey_logger_.RecordJourneyStatsHistograms(
229 JourneyLogger::COMPLETION_STATUS_USER_ABORTED);
230
mathpf4bc50e2017-01-24 05:17:50231 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28232 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50233
234 // We close all bindings and ask to be destroyed.
235 client_.reset();
236 binding_.Close();
rouslanb28f4532017-05-08 15:41:47237 if (observer_for_testing_)
238 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50239 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36240}
241
mathpf4bc50e2017-01-24 05:17:50242void PaymentRequest::OnConnectionTerminated() {
243 // We are here because of a browser-side error, or likely as a result of the
244 // connection_error_handler on |binding_|, which can mean that the renderer
245 // has decided to close the pipe for various reasons (see all uses of
246 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
247 // the binding and the dialog, and ask to be deleted.
248 client_.reset();
mathpf709499d2017-01-09 20:48:36249 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50250 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47251 if (observer_for_testing_)
252 observer_for_testing_->OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36253 manager_->DestroyRequest(this);
254}
255
mathpd4be8de82017-03-01 00:51:48256void PaymentRequest::Pay() {
mathpf1a7a3752017-03-15 11:23:37257 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48258}
259
mathpf709499d2017-01-09 20:48:36260} // namespace payments