blob: 54fcb97346ddbc3126daa1887cfb281f305028f2 [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
Rouslan Solomakhin1804ee42017-10-03 14:27:4310#include "base/feature_list.h"
Mathieu Perreault627b97c2017-08-12 00:44:2211#include "base/stl_util.h"
rouslan690997682017-05-09 18:07:3912#include "components/payments/content/can_make_payment_query_factory.h"
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5113#include "components/payments/content/content_payment_request_delegate.h"
rouslan6e3cf7c62017-04-17 21:23:2814#include "components/payments/content/origin_security_checker.h"
Mohamad Ahmadif5544bb2017-09-01 21:48:2215#include "components/payments/content/payment_request_converter.h"
rouslan908248c2017-02-27 21:30:2416#include "components/payments/content/payment_request_web_contents_manager.h"
rouslan690997682017-05-09 18:07:3917#include "components/payments/core/can_make_payment_query.h"
Anthony Vallee-Dubois968ae4d2018-03-15 16:56:3618#include "components/payments/core/features.h"
Mohamad Ahmadif5544bb2017-09-01 21:48:2219#include "components/payments/core/payment_details.h"
20#include "components/payments/core/payment_details_validation.h"
Mathieu Perreault23d25bfb82018-05-11 14:45:3721#include "components/payments/core/payment_instrument.h"
anthonyvd6a43b932017-05-11 18:39:2722#include "components/payments/core/payment_prefs.h"
Jinho Bangbe463a22018-08-02 10:26:5023#include "components/payments/core/payments_validators.h"
anthonyvd6a43b932017-05-11 18:39:2724#include "components/prefs/pref_service.h"
Steven Holte2083e8bc2018-07-16 23:50:3625#include "components/ukm/content/source_url_recorder.h"
Rouslan Solomakhin6e979ab2017-08-30 17:30:3926#include "components/url_formatter/elide_url.h"
mathpf709499d2017-01-09 20:48:3627#include "content/public/browser/browser_thread.h"
rouslan690997682017-05-09 18:07:3928#include "content/public/browser/render_frame_host.h"
mathpf709499d2017-01-09 20:48:3629#include "content/public/browser/web_contents.h"
Rouslan Solomakhin1804ee42017-10-03 14:27:4330#include "content/public/common/content_features.h"
mathpf709499d2017-01-09 20:48:3631
32namespace payments {
33
34PaymentRequest::PaymentRequest(
rouslan690997682017-05-09 18:07:3935 content::RenderFrameHost* render_frame_host,
mathpf709499d2017-01-09 20:48:3636 content::WebContents* web_contents,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5137 std::unique_ptr<ContentPaymentRequestDelegate> delegate,
mathpf709499d2017-01-09 20:48:3638 PaymentRequestWebContentsManager* manager,
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0739 PaymentRequestDisplayManager* display_manager,
rouslan6e3cf7c62017-04-17 21:23:2840 mojo::InterfaceRequest<mojom::PaymentRequest> request,
mathp300fa542017-03-27 19:29:3741 ObserverForTest* observer_for_testing)
mathpf709499d2017-01-09 20:48:3642 : web_contents_(web_contents),
43 delegate_(std::move(delegate)),
44 manager_(manager),
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:0745 display_manager_(display_manager),
46 display_handle_(nullptr),
mathp300fa542017-03-27 19:29:3747 binding_(this, std::move(request)),
Rouslan Solomakhin6e979ab2017-08-30 17:30:3948 top_level_origin_(url_formatter::FormatUrlForSecurityDisplay(
49 web_contents_->GetLastCommittedURL())),
50 frame_origin_(url_formatter::FormatUrlForSecurityDisplay(
51 render_frame_host->GetLastCommittedURL())),
sebsg20b49d7b2017-05-04 20:23:1752 observer_for_testing_(observer_for_testing),
53 journey_logger_(delegate_->IsIncognito(),
Steven Holte2083e8bc2018-07-16 23:50:3654 ukm::GetSourceIdForWebContentsDocument(web_contents)),
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1355 weak_ptr_factory_(this) {
mathpf4bc50e2017-01-24 05:17:5056 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
57 // will happen as a result of many renderer-side events (both successful and
58 // erroneous in nature).
59 // TODO(crbug.com/683636): Investigate using
60 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
tzik2bcf8e42018-07-31 11:22:1561 binding_.set_connection_error_handler(base::BindOnce(
Anthony Vallee-Duboisdc1dbf1a2017-07-17 15:01:1362 &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr()));
mathpf709499d2017-01-09 20:48:3663}
64
65PaymentRequest::~PaymentRequest() {}
66
rouslan6e3cf7c62017-04-17 21:23:2867void PaymentRequest::Init(mojom::PaymentRequestClientPtr client,
68 std::vector<mojom::PaymentMethodDataPtr> method_data,
69 mojom::PaymentDetailsPtr details,
70 mojom::PaymentOptionsPtr options) {
mathpf709499d2017-01-09 20:48:3671 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
rouslan6e3cf7c62017-04-17 21:23:2872 client_ = std::move(client);
73
rouslanb28f4532017-05-08 15:41:4774 const GURL last_committed_url = delegate_->GetLastCommittedURL();
75 if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) {
rouslan6e3cf7c62017-04-17 21:23:2876 LOG(ERROR) << "Not in a secure origin";
77 OnConnectionTerminated();
78 return;
79 }
80
rouslanb28f4532017-05-08 15:41:4781 bool allowed_origin =
82 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) ||
83 OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url);
84 if (!allowed_origin) {
85 LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins "
86 "allowed";
87 }
88
89 bool invalid_ssl =
90 OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) &&
91 !delegate_->IsSslCertificateValid();
92 if (invalid_ssl)
rouslan6e3cf7c62017-04-17 21:23:2893 LOG(ERROR) << "SSL certificate is not valid";
rouslanb28f4532017-05-08 15:41:4794
95 if (!allowed_origin || invalid_ssl) {
Rouslan Solomakhind2cae95a2018-08-09 00:16:1096 // Intentionally don't set |spec_| and |state_|. Don't show UI. Resolve
97 // .canMakepayment() with "false". Reject .show() with "NotSupportedError".
rouslan6e3cf7c62017-04-17 21:23:2898 return;
99 }
100
mathpf709499d2017-01-09 20:48:36101 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22102 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathpf709499d2017-01-09 20:48:36103 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:50104 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:36105 return;
106 }
rouslan6e3cf7c62017-04-17 21:23:28107
jinho.bangfcb5ec92017-03-29 08:08:02108 if (!details->total) {
109 LOG(ERROR) << "Missing total";
110 OnConnectionTerminated();
111 return;
112 }
rouslan6e3cf7c62017-04-17 21:23:28113
sebsga70a6da2017-12-21 22:27:02114 spec_ = std::make_unique<PaymentRequestSpec>(
mathpc0d616a2017-03-15 14:09:33115 std::move(options), std::move(details), std::move(method_data), this,
116 delegate_->GetApplicationLocale());
sebsga70a6da2017-12-21 22:27:02117 state_ = std::make_unique<PaymentRequestState>(
Rouslan Solomakhindbf593d92017-11-21 19:20:57118 web_contents_, top_level_origin_, frame_origin_, spec_.get(), this,
119 delegate_->GetApplicationLocale(), delegate_->GetPersonalDataManager(),
120 delegate_.get(), &journey_logger_);
Mathieu Perreault627b97c2017-08-12 00:44:22121
122 journey_logger_.SetRequestedInformation(
123 spec_->request_shipping(), spec_->request_payer_email(),
124 spec_->request_payer_phone(), spec_->request_payer_name());
125
126 // Log metrics around which payment methods are requested by the merchant.
127 GURL google_pay_url(kGooglePayMethodName);
128 GURL android_pay_url(kAndroidPayMethodName);
129 // Looking for payment methods that are NOT google-related payment methods.
130 auto non_google_it =
131 std::find_if(spec_->url_payment_method_identifiers().begin(),
132 spec_->url_payment_method_identifiers().end(),
133 [google_pay_url, android_pay_url](const GURL& url) {
134 return url != google_pay_url && url != android_pay_url;
135 });
136 journey_logger_.SetRequestedPaymentMethodTypes(
137 /*requested_basic_card=*/!spec_->supported_card_networks().empty(),
138 /*requested_method_google=*/
139 base::ContainsValue(spec_->url_payment_method_identifiers(),
140 google_pay_url) ||
141 base::ContainsValue(spec_->url_payment_method_identifiers(),
142 android_pay_url),
143 /*requested_method_other=*/non_google_it !=
144 spec_->url_payment_method_identifiers().end());
mathpf709499d2017-01-09 20:48:36145}
146
Rouslan Solomakhin833f8512018-04-03 23:19:25147void PaymentRequest::Show(bool is_user_gesture) {
tmartino8ce922852017-01-09 22:23:10148 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:50149 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
150 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:10151 return;
152 }
rouslan6e3cf7c62017-04-17 21:23:28153
rouslan7d433cc22017-05-08 15:18:07154 // A tab can display only one PaymentRequest UI at a time.
Anthony Vallee-Dubois8f5e7e12018-01-12 16:14:06155 display_handle_ = display_manager_->TryShow(delegate_.get());
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07156 if (!display_handle_) {
rouslan7d433cc22017-05-08 15:18:07157 LOG(ERROR) << "A PaymentRequest UI is already showing";
sebsg828269bc2017-06-09 19:11:12158 journey_logger_.SetNotShown(
159 JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS);
rouslan7d433cc22017-05-08 15:18:07160 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
161 OnConnectionTerminated();
162 return;
163 }
164
Rouslan Solomakhin5b510432017-09-26 16:59:32165 if (!delegate_->IsBrowserWindowActive()) {
166 LOG(ERROR) << "Cannot show PaymentRequest UI in a background tab";
167 journey_logger_.SetNotShown(JourneyLogger::NOT_SHOWN_REASON_OTHER);
168 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
169 OnConnectionTerminated();
170 return;
171 }
172
Rouslan Solomakhind2cae95a2018-08-09 00:16:10173 if (!state_) {
174 // SSL is not valid.
175 AreRequestedMethodsSupportedCallback(false);
176 return;
177 }
178
Rouslan Solomakhin833f8512018-04-03 23:19:25179 is_show_user_gesture_ = is_user_gesture;
180
Rouslan Solomakhind2cae95a2018-08-09 00:16:10181 display_handle_->Show(this);
182
gogerald0a7ee6c2017-11-13 18:23:19183 state_->AreRequestedMethodsSupported(
184 base::BindOnce(&PaymentRequest::AreRequestedMethodsSupportedCallback,
185 weak_ptr_factory_.GetWeakPtr()));
186}
187
Jinho Bangbe463a22018-08-02 10:26:50188void PaymentRequest::Retry(mojom::PaymentValidationErrorsPtr errors) {
189 std::string error;
190 if (!PaymentsValidators::IsValidPaymentValidationErrorsFormat(errors,
191 &error)) {
192 DLOG(ERROR) << error;
193 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
194 OnConnectionTerminated();
195 return;
196 }
197
198 // TODO(zino): Should implement this method (including updating UI part).
199 // Please see https://crbug.com/861704
200 NOTIMPLEMENTED();
201}
202
gogerald0a7ee6c2017-11-13 18:23:19203void PaymentRequest::AreRequestedMethodsSupportedCallback(
204 bool methods_supported) {
205 if (methods_supported) {
gogerald2ddfc64c2018-08-23 16:20:21206 if (SatisfiesSkipUIConstraints()) {
207 skipped_payment_request_ui_ = true;
Rouslan Solomakhind2cae95a2018-08-09 00:16:10208 Pay();
gogerald2ddfc64c2018-08-23 16:20:21209 }
gogerald0a7ee6c2017-11-13 18:23:19210 } else {
sebsg828269bc2017-06-09 19:11:12211 journey_logger_.SetNotShown(
212 JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD);
rouslan6e3cf7c62017-04-17 21:23:28213 client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED);
214 if (observer_for_testing_)
215 observer_for_testing_->OnNotSupportedError();
216 OnConnectionTerminated();
rouslan6e3cf7c62017-04-17 21:23:28217 }
mathpf709499d2017-01-09 20:48:36218}
219
gogerald2ddfc64c2018-08-23 16:20:21220bool PaymentRequest::SatisfiesSkipUIConstraints() const {
221 return base::FeatureList::IsEnabled(features::kWebPaymentsSingleAppUiSkip) &&
222 base::FeatureList::IsEnabled(::features::kServiceWorkerPaymentApps) &&
223 is_show_user_gesture_ && state()->is_get_all_instruments_finished() &&
224 state()->available_instruments().size() == 1 &&
225 spec()->stringified_method_data().size() == 1 &&
226 !spec()->request_shipping() && !spec()->request_payer_name() &&
227 !spec()->request_payer_phone() &&
228 !spec()->request_payer_email()
229 // Only allowing URL base payment apps to skip the payment sheet.
230 && spec()->url_payment_method_identifiers().size() == 1;
231}
232
mathp151bd312017-04-03 21:07:24233void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) {
234 std::string error;
Mohamad Ahmadif5544bb2017-09-01 21:48:22235 if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) {
mathp151bd312017-04-03 21:07:24236 LOG(ERROR) << error;
237 OnConnectionTerminated();
238 return;
239 }
Rouslan Solomakhin4cbda822017-08-23 18:50:39240
241 if (!details->total) {
242 LOG(ERROR) << "Missing total";
243 OnConnectionTerminated();
244 return;
245 }
246
mathp151bd312017-04-03 21:07:24247 spec_->UpdateWith(std::move(details));
248}
249
Rouslan Solomakhina9ff9282017-10-31 21:58:05250void PaymentRequest::NoUpdatedPaymentDetails() {
251 spec_->RecomputeSpecForDetails();
252}
253
mathpf4bc50e2017-01-24 05:17:50254void PaymentRequest::Abort() {
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56255 // The API user has decided to abort. If a successful abort message is
256 // returned to the renderer, the Mojo message pipe is closed, which triggers
mathpf4bc50e2017-01-24 05:17:50257 // PaymentRequest::OnConnectionTerminated, which destroys this object.
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56258 // Otherwise, the abort promise is rejected and the pipe is not closed.
259 // The abort is only successful if the payment app wasn't yet invoked.
260 // TODO(crbug.com/716546): Add a merchant abort metric
261
262 bool accepting_abort = !state_->IsPaymentAppInvoked();
sebsgfcdd13c2017-06-08 15:49:33263 if (accepting_abort)
264 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT);
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56265
mathpf4bc50e2017-01-24 05:17:50266 if (client_.is_bound())
Anthony Vallee-Dubois6813c1442017-05-17 19:32:56267 client_->OnAbort(accepting_abort);
268
269 if (observer_for_testing_)
270 observer_for_testing_->OnAbortCalled();
mathpf4bc50e2017-01-24 05:17:50271}
272
mathp218795892017-03-29 15:15:34273void PaymentRequest::Complete(mojom::PaymentComplete result) {
mathp4b85b582017-03-08 21:07:16274 if (!client_.is_bound())
275 return;
276
Rouslan Solomakhine3473192017-06-16 14:54:57277 // Failed transactions show an error. Successful and unknown-state
278 // transactions don't show an error.
279 if (result == mojom::PaymentComplete::FAIL) {
mathp218795892017-03-29 15:15:34280 delegate_->ShowErrorMessage();
281 } else {
sebsgfcdd13c2017-06-08 15:49:33282 DCHECK(!has_recorded_completion_);
sebsgf8272a22017-05-26 14:32:58283 journey_logger_.SetCompleted();
sebsgfcdd13c2017-06-08 15:49:33284 has_recorded_completion_ = true;
285
anthonyvd6a43b932017-05-11 18:39:27286 delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted,
287 true);
mathp218795892017-03-29 15:15:34288 // When the renderer closes the connection,
289 // PaymentRequest::OnConnectionTerminated will be called.
290 client_->OnComplete();
sebsg8a93b272017-05-11 19:30:22291 state_->RecordUseStats();
mathp218795892017-03-29 15:15:34292 }
mathp4b85b582017-03-08 21:07:16293}
294
295void PaymentRequest::CanMakePayment() {
gogerald8189d522017-09-15 17:52:18296 if (observer_for_testing_)
297 observer_for_testing_->OnCanMakePaymentCalled();
Mathieu Perreaultcacb85e2018-06-06 20:40:13298
Rouslan Solomakhind2cae95a2018-08-09 00:16:10299 if (!delegate_->GetPrefService()->GetBoolean(kCanMakePaymentEnabled) ||
300 !state_) {
Mathieu Perreaultcacb85e2018-06-06 20:40:13301 CanMakePaymentCallback(/*can_make_payment=*/false);
302 } else {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10303 state_->CanMakePayment(
Mathieu Perreaultcacb85e2018-06-06 20:40:13304 base::BindOnce(&PaymentRequest::CanMakePaymentCallback,
305 weak_ptr_factory_.GetWeakPtr()));
306 }
gogerald8189d522017-09-15 17:52:18307}
308
mathpf1a7a3752017-03-15 11:23:37309void PaymentRequest::OnPaymentResponseAvailable(
310 mojom::PaymentResponsePtr response) {
mathp57c8c862017-06-16 20:15:45311 journey_logger_.SetEventOccurred(
312 JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS);
gogerald7a2b761e2017-11-09 18:30:19313
314 // Do not send invalid response to client.
315 if (response->method_name.empty() || response->stringified_details.empty()) {
316 RecordFirstAbortReason(
317 JourneyLogger::ABORT_REASON_INSTRUMENT_DETAILS_ERROR);
318 delegate_->ShowErrorMessage();
319 return;
320 }
321
mathpf1a7a3752017-03-15 11:23:37322 client_->OnPaymentResponse(std::move(response));
mathp4b85b582017-03-08 21:07:16323}
324
mathp151bd312017-04-03 21:07:24325void PaymentRequest::OnShippingOptionIdSelected(
326 std::string shipping_option_id) {
327 client_->OnShippingOptionChange(shipping_option_id);
328}
329
330void PaymentRequest::OnShippingAddressSelected(
331 mojom::PaymentAddressPtr address) {
332 client_->OnShippingAddressChange(std::move(address));
333}
334
mathpf4bc50e2017-01-24 05:17:50335void PaymentRequest::UserCancelled() {
336 // If |client_| is not bound, then the object is already being destroyed as
337 // a result of a renderer event.
338 if (!client_.is_bound())
339 return;
340
sebsgfcdd13c2017-06-08 15:49:33341 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER);
sebsg20b49d7b2017-05-04 20:23:17342
mathpf4bc50e2017-01-24 05:17:50343 // This sends an error to the renderer, which informs the API user.
rouslan6e3cf7c62017-04-17 21:23:28344 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:50345
346 // We close all bindings and ask to be destroyed.
347 client_.reset();
348 binding_.Close();
rouslanb28f4532017-05-08 15:41:47349 if (observer_for_testing_)
350 observer_for_testing_->OnConnectionTerminated();
mathpf4bc50e2017-01-24 05:17:50351 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:36352}
353
sebsgd56b3e422017-10-20 18:08:08354void PaymentRequest::DidStartMainFrameNavigationToDifferentDocument(
355 bool is_user_initiated) {
sebsgfcdd13c2017-06-08 15:49:33356 RecordFirstAbortReason(is_user_initiated
357 ? JourneyLogger::ABORT_REASON_USER_NAVIGATION
358 : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION);
sebsg2c8558a2017-05-17 18:54:10359}
360
mathpf4bc50e2017-01-24 05:17:50361void PaymentRequest::OnConnectionTerminated() {
362 // We are here because of a browser-side error, or likely as a result of the
363 // connection_error_handler on |binding_|, which can mean that the renderer
364 // has decided to close the pipe for various reasons (see all uses of
365 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
366 // the binding and the dialog, and ask to be deleted.
367 client_.reset();
mathpf709499d2017-01-09 20:48:36368 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50369 delegate_->CloseDialog();
rouslanb28f4532017-05-08 15:41:47370 if (observer_for_testing_)
371 observer_for_testing_->OnConnectionTerminated();
sebsgfcdd13c2017-06-08 15:49:33372
373 RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR);
mathpf709499d2017-01-09 20:48:36374 manager_->DestroyRequest(this);
375}
376
mathpd4be8de82017-03-01 00:51:48377void PaymentRequest::Pay() {
mathp57c8c862017-06-16 20:15:45378 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED);
Mathieu Perreault23d25bfb82018-05-11 14:45:37379
380 // Log the correct "selected instrument" metric according to type.
381 DCHECK(state_->selected_instrument());
382 JourneyLogger::Event selected_event =
383 JourneyLogger::Event::EVENT_SELECTED_OTHER;
384 switch (state_->selected_instrument()->type()) {
385 case PaymentInstrument::Type::AUTOFILL:
386 selected_event = JourneyLogger::Event::EVENT_SELECTED_CREDIT_CARD;
387 break;
388 case PaymentInstrument::Type::SERVICE_WORKER_APP:
389 selected_event = JourneyLogger::Event::EVENT_SELECTED_OTHER;
390 break;
391 case PaymentInstrument::Type::NATIVE_MOBILE_APP:
392 NOTREACHED();
393 break;
394 }
395 journey_logger_.SetEventOccurred(selected_event);
396
mathpf1a7a3752017-03-15 11:23:37397 state_->GeneratePaymentResponse();
mathpd4be8de82017-03-01 00:51:48398}
399
Anthony Vallee-Duboisc7ae7332017-12-19 20:44:07400void PaymentRequest::HideIfNecessary() {
401 display_handle_.reset();
402}
403
Rouslan Solomakhind2cae95a2018-08-09 00:16:10404void PaymentRequest::RecordDialogShownEventInJourneyLogger() {
405 journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN);
406}
407
Anthony Vallee-Dubois10d131a2018-02-22 15:41:04408bool PaymentRequest::IsIncognito() const {
409 return delegate_->IsIncognito();
410}
411
sebsgfcdd13c2017-06-08 15:49:33412void PaymentRequest::RecordFirstAbortReason(
413 JourneyLogger::AbortReason abort_reason) {
414 if (!has_recorded_completion_) {
415 has_recorded_completion_ = true;
416 journey_logger_.SetAborted(abort_reason);
sebsg2c8558a2017-05-17 18:54:10417 }
418}
419
Rouslan Solomakhin1804ee42017-10-03 14:27:43420void PaymentRequest::CanMakePaymentCallback(bool can_make_payment) {
Rouslan Solomakhind2cae95a2018-08-09 00:16:10421 if (!spec_ || CanMakePaymentQueryFactory::GetInstance()
422 ->GetForContext(web_contents_->GetBrowserContext())
423 ->CanQuery(top_level_origin_, frame_origin_,
424 spec_->stringified_method_data())) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42425 RespondToCanMakePaymentQuery(can_make_payment, false);
Rouslan Solomakhin1804ee42017-10-03 14:27:43426 } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42427 RespondToCanMakePaymentQuery(can_make_payment, true);
Rouslan Solomakhin1804ee42017-10-03 14:27:43428 } else {
429 client_->OnCanMakePayment(
430 mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED);
431 }
432
433 if (observer_for_testing_)
434 observer_for_testing_->OnCanMakePaymentReturned();
435}
436
Rouslan Solomakhin16ee55202017-10-03 19:04:42437void PaymentRequest::RespondToCanMakePaymentQuery(bool can_make_payment,
438 bool warn_localhost_or_file) {
Rouslan Solomakhin16ee55202017-10-03 19:04:42439 mojom::CanMakePaymentQueryResult positive =
440 warn_localhost_or_file
441 ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT
442 : mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT;
443 mojom::CanMakePaymentQueryResult negative =
444 warn_localhost_or_file
445 ? mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT
446 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT;
447
448 client_->OnCanMakePayment(can_make_payment ? positive : negative);
Rouslan Solomakhin1804ee42017-10-03 14:27:43449 journey_logger_.SetCanMakePaymentValue(can_make_payment);
450}
451
mathpf709499d2017-01-09 20:48:36452} // namespace payments