mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 1 | // 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 | |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 5 | #include "components/payments/content/payment_request.h" |
| 6 | |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 7 | #include <string> |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 8 | #include <utility> |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 9 | |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
Mathieu Perreault | 627b97c | 2017-08-12 00:44:22 | [diff] [blame] | 11 | #include "base/stl_util.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 12 | #include "components/payments/content/can_make_payment_query_factory.h" |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 13 | #include "components/payments/content/origin_security_checker.h" |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame^] | 14 | #include "components/payments/content/payment_request_converter.h" |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 15 | #include "components/payments/content/payment_request_web_contents_manager.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 16 | #include "components/payments/core/can_make_payment_query.h" |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame^] | 17 | #include "components/payments/core/payment_details.h" |
| 18 | #include "components/payments/core/payment_details_validation.h" |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 19 | #include "components/payments/core/payment_prefs.h" |
| 20 | #include "components/prefs/pref_service.h" |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 21 | #include "components/url_formatter/elide_url.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 23 | #include "content/public/browser/render_frame_host.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 24 | #include "content/public/browser/web_contents.h" |
| 25 | |
| 26 | namespace payments { |
| 27 | |
| 28 | PaymentRequest::PaymentRequest( |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 29 | content::RenderFrameHost* render_frame_host, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 30 | content::WebContents* web_contents, |
| 31 | std::unique_ptr<PaymentRequestDelegate> delegate, |
| 32 | PaymentRequestWebContentsManager* manager, |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 33 | mojo::InterfaceRequest<mojom::PaymentRequest> request, |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 34 | ObserverForTest* observer_for_testing) |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 35 | : web_contents_(web_contents), |
| 36 | delegate_(std::move(delegate)), |
| 37 | manager_(manager), |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 38 | binding_(this, std::move(request)), |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 39 | top_level_origin_(url_formatter::FormatUrlForSecurityDisplay( |
| 40 | web_contents_->GetLastCommittedURL())), |
| 41 | frame_origin_(url_formatter::FormatUrlForSecurityDisplay( |
| 42 | render_frame_host->GetLastCommittedURL())), |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 43 | observer_for_testing_(observer_for_testing), |
| 44 | journey_logger_(delegate_->IsIncognito(), |
| 45 | web_contents_->GetLastCommittedURL(), |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 46 | delegate_->GetUkmRecorder()), |
| 47 | weak_ptr_factory_(this) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 48 | // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
| 49 | // will happen as a result of many renderer-side events (both successful and |
| 50 | // erroneous in nature). |
| 51 | // TODO(crbug.com/683636): Investigate using |
| 52 | // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
| 53 | binding_.set_connection_error_handler(base::Bind( |
Anthony Vallee-Dubois | dc1dbf1a | 2017-07-17 15:01:13 | [diff] [blame] | 54 | &PaymentRequest::OnConnectionTerminated, weak_ptr_factory_.GetWeakPtr())); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | PaymentRequest::~PaymentRequest() {} |
| 58 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 59 | void PaymentRequest::Init(mojom::PaymentRequestClientPtr client, |
| 60 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 61 | mojom::PaymentDetailsPtr details, |
| 62 | mojom::PaymentOptionsPtr options) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 63 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 64 | client_ = std::move(client); |
| 65 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 66 | const GURL last_committed_url = delegate_->GetLastCommittedURL(); |
| 67 | if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 68 | LOG(ERROR) << "Not in a secure origin"; |
| 69 | OnConnectionTerminated(); |
| 70 | return; |
| 71 | } |
| 72 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 73 | bool allowed_origin = |
| 74 | OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) || |
| 75 | OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url); |
| 76 | if (!allowed_origin) { |
| 77 | LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins " |
| 78 | "allowed"; |
| 79 | } |
| 80 | |
| 81 | bool invalid_ssl = |
| 82 | OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) && |
| 83 | !delegate_->IsSslCertificateValid(); |
| 84 | if (invalid_ssl) |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 85 | LOG(ERROR) << "SSL certificate is not valid"; |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 86 | |
| 87 | if (!allowed_origin || invalid_ssl) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 88 | // Don't show UI. Resolve .canMakepayment() with "false". Reject .show() |
| 89 | // with "NotSupportedError". |
| 90 | spec_ = base::MakeUnique<PaymentRequestSpec>( |
| 91 | mojom::PaymentOptions::New(), mojom::PaymentDetails::New(), |
| 92 | std::vector<mojom::PaymentMethodDataPtr>(), this, |
| 93 | delegate_->GetApplicationLocale()); |
| 94 | state_ = base::MakeUnique<PaymentRequestState>( |
| 95 | spec_.get(), this, delegate_->GetApplicationLocale(), |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 96 | delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 100 | std::string error; |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame^] | 101 | if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 102 | LOG(ERROR) << error; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 103 | OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 104 | return; |
| 105 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 106 | |
jinho.bang | fcb5ec9 | 2017-03-29 08:08:02 | [diff] [blame] | 107 | if (!details->total) { |
| 108 | LOG(ERROR) << "Missing total"; |
| 109 | OnConnectionTerminated(); |
| 110 | return; |
| 111 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 112 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 113 | spec_ = base::MakeUnique<PaymentRequestSpec>( |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 114 | std::move(options), std::move(details), std::move(method_data), this, |
| 115 | delegate_->GetApplicationLocale()); |
| 116 | state_ = base::MakeUnique<PaymentRequestState>( |
| 117 | spec_.get(), this, delegate_->GetApplicationLocale(), |
sebsg | c6719b3 | 2017-07-05 19:54:47 | [diff] [blame] | 118 | delegate_->GetPersonalDataManager(), delegate_.get(), &journey_logger_); |
Mathieu Perreault | 627b97c | 2017-08-12 00:44:22 | [diff] [blame] | 119 | |
| 120 | journey_logger_.SetRequestedInformation( |
| 121 | spec_->request_shipping(), spec_->request_payer_email(), |
| 122 | spec_->request_payer_phone(), spec_->request_payer_name()); |
| 123 | |
| 124 | // Log metrics around which payment methods are requested by the merchant. |
| 125 | GURL google_pay_url(kGooglePayMethodName); |
| 126 | GURL android_pay_url(kAndroidPayMethodName); |
| 127 | // Looking for payment methods that are NOT google-related payment methods. |
| 128 | auto non_google_it = |
| 129 | std::find_if(spec_->url_payment_method_identifiers().begin(), |
| 130 | spec_->url_payment_method_identifiers().end(), |
| 131 | [google_pay_url, android_pay_url](const GURL& url) { |
| 132 | return url != google_pay_url && url != android_pay_url; |
| 133 | }); |
| 134 | journey_logger_.SetRequestedPaymentMethodTypes( |
| 135 | /*requested_basic_card=*/!spec_->supported_card_networks().empty(), |
| 136 | /*requested_method_google=*/ |
| 137 | base::ContainsValue(spec_->url_payment_method_identifiers(), |
| 138 | google_pay_url) || |
| 139 | base::ContainsValue(spec_->url_payment_method_identifiers(), |
| 140 | android_pay_url), |
| 141 | /*requested_method_other=*/non_google_it != |
| 142 | spec_->url_payment_method_identifiers().end()); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void PaymentRequest::Show() { |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 146 | if (!client_.is_bound() || !binding_.is_bound()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 147 | LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 148 | OnConnectionTerminated(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 149 | return; |
| 150 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 151 | |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 152 | // A tab can display only one PaymentRequest UI at a time. |
| 153 | if (!manager_->CanShow(this)) { |
| 154 | LOG(ERROR) << "A PaymentRequest UI is already showing"; |
sebsg | 828269bc | 2017-06-09 19:11:12 | [diff] [blame] | 155 | journey_logger_.SetNotShown( |
| 156 | JourneyLogger::NOT_SHOWN_REASON_CONCURRENT_REQUESTS); |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 157 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
| 158 | OnConnectionTerminated(); |
| 159 | return; |
| 160 | } |
| 161 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 162 | if (!state_->AreRequestedMethodsSupported()) { |
sebsg | 828269bc | 2017-06-09 19:11:12 | [diff] [blame] | 163 | journey_logger_.SetNotShown( |
| 164 | JourneyLogger::NOT_SHOWN_REASON_NO_SUPPORTED_PAYMENT_METHOD); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 165 | client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED); |
| 166 | if (observer_for_testing_) |
| 167 | observer_for_testing_->OnNotSupportedError(); |
| 168 | OnConnectionTerminated(); |
| 169 | return; |
| 170 | } |
| 171 | |
mathp | 57c8c86 | 2017-06-16 20:15:45 | [diff] [blame] | 172 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SHOWN); |
sebsg | 8f4fa4d | 2017-06-13 15:25:45 | [diff] [blame] | 173 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 174 | delegate_->ShowDialog(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 175 | } |
| 176 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 177 | void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 178 | std::string error; |
Mohamad Ahmadi | f5544bb | 2017-09-01 21:48:22 | [diff] [blame^] | 179 | if (!ValidatePaymentDetails(ConvertPaymentDetails(details), &error)) { |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 180 | LOG(ERROR) << error; |
| 181 | OnConnectionTerminated(); |
| 182 | return; |
| 183 | } |
Rouslan Solomakhin | 4cbda82 | 2017-08-23 18:50:39 | [diff] [blame] | 184 | |
| 185 | if (!details->total) { |
| 186 | LOG(ERROR) << "Missing total"; |
| 187 | OnConnectionTerminated(); |
| 188 | return; |
| 189 | } |
| 190 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 191 | spec_->UpdateWith(std::move(details)); |
| 192 | } |
| 193 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 194 | void PaymentRequest::Abort() { |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 195 | // The API user has decided to abort. If a successful abort message is |
| 196 | // returned to the renderer, the Mojo message pipe is closed, which triggers |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 197 | // PaymentRequest::OnConnectionTerminated, which destroys this object. |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 198 | // Otherwise, the abort promise is rejected and the pipe is not closed. |
| 199 | // The abort is only successful if the payment app wasn't yet invoked. |
| 200 | // TODO(crbug.com/716546): Add a merchant abort metric |
| 201 | |
| 202 | bool accepting_abort = !state_->IsPaymentAppInvoked(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 203 | if (accepting_abort) |
| 204 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_MERCHANT); |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 205 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 206 | if (client_.is_bound()) |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 207 | client_->OnAbort(accepting_abort); |
| 208 | |
| 209 | if (observer_for_testing_) |
| 210 | observer_for_testing_->OnAbortCalled(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 211 | } |
| 212 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 213 | void PaymentRequest::Complete(mojom::PaymentComplete result) { |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 214 | if (!client_.is_bound()) |
| 215 | return; |
| 216 | |
Rouslan Solomakhin | e347319 | 2017-06-16 14:54:57 | [diff] [blame] | 217 | // Failed transactions show an error. Successful and unknown-state |
| 218 | // transactions don't show an error. |
| 219 | if (result == mojom::PaymentComplete::FAIL) { |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 220 | delegate_->ShowErrorMessage(); |
| 221 | } else { |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 222 | DCHECK(!has_recorded_completion_); |
sebsg | f8272a2 | 2017-05-26 14:32:58 | [diff] [blame] | 223 | journey_logger_.SetCompleted(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 224 | has_recorded_completion_ = true; |
| 225 | |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 226 | delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted, |
| 227 | true); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 228 | // When the renderer closes the connection, |
| 229 | // PaymentRequest::OnConnectionTerminated will be called. |
| 230 | client_->OnComplete(); |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 231 | state_->RecordUseStats(); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 232 | } |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void PaymentRequest::CanMakePayment() { |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 236 | bool can_make_payment = state()->CanMakePayment(); |
| 237 | if (delegate_->IsIncognito()) { |
| 238 | client_->OnCanMakePayment( |
| 239 | mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT); |
| 240 | journey_logger_.SetCanMakePaymentValue(true); |
| 241 | } else if (CanMakePaymentQueryFactory::GetInstance() |
| 242 | ->GetForContext(web_contents_->GetBrowserContext()) |
Rouslan Solomakhin | 115f723 | 2017-08-01 15:24:38 | [diff] [blame] | 243 | ->CanQuery(top_level_origin_, frame_origin_, |
| 244 | spec()->stringified_method_data())) { |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 245 | client_->OnCanMakePayment( |
| 246 | can_make_payment |
| 247 | ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT |
| 248 | : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT); |
| 249 | journey_logger_.SetCanMakePaymentValue(can_make_payment); |
Rouslan Solomakhin | 6e979ab | 2017-08-30 17:30:39 | [diff] [blame] | 250 | } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) { |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 251 | client_->OnCanMakePayment( |
| 252 | can_make_payment |
| 253 | ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT |
| 254 | : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT); |
| 255 | journey_logger_.SetCanMakePaymentValue(can_make_payment); |
| 256 | } else { |
| 257 | client_->OnCanMakePayment( |
| 258 | mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED); |
| 259 | } |
| 260 | |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 261 | if (observer_for_testing_) |
| 262 | observer_for_testing_->OnCanMakePaymentCalled(); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 263 | } |
| 264 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 265 | void PaymentRequest::OnPaymentResponseAvailable( |
| 266 | mojom::PaymentResponsePtr response) { |
mathp | 57c8c86 | 2017-06-16 20:15:45 | [diff] [blame] | 267 | journey_logger_.SetEventOccurred( |
| 268 | JourneyLogger::EVENT_RECEIVED_INSTRUMENT_DETAILS); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 269 | client_->OnPaymentResponse(std::move(response)); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 270 | } |
| 271 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 272 | void PaymentRequest::OnShippingOptionIdSelected( |
| 273 | std::string shipping_option_id) { |
| 274 | client_->OnShippingOptionChange(shipping_option_id); |
| 275 | } |
| 276 | |
| 277 | void PaymentRequest::OnShippingAddressSelected( |
| 278 | mojom::PaymentAddressPtr address) { |
| 279 | client_->OnShippingAddressChange(std::move(address)); |
| 280 | } |
| 281 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 282 | void PaymentRequest::UserCancelled() { |
| 283 | // If |client_| is not bound, then the object is already being destroyed as |
| 284 | // a result of a renderer event. |
| 285 | if (!client_.is_bound()) |
| 286 | return; |
| 287 | |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 288 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_ABORTED_BY_USER); |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 289 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 290 | // This sends an error to the renderer, which informs the API user. |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 291 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 292 | |
| 293 | // We close all bindings and ask to be destroyed. |
| 294 | client_.reset(); |
| 295 | binding_.Close(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 296 | if (observer_for_testing_) |
| 297 | observer_for_testing_->OnConnectionTerminated(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 298 | manager_->DestroyRequest(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 299 | } |
| 300 | |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 301 | void PaymentRequest::DidStartNavigation(bool is_user_initiated) { |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 302 | RecordFirstAbortReason(is_user_initiated |
| 303 | ? JourneyLogger::ABORT_REASON_USER_NAVIGATION |
| 304 | : JourneyLogger::ABORT_REASON_MERCHANT_NAVIGATION); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 305 | } |
| 306 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 307 | void PaymentRequest::OnConnectionTerminated() { |
| 308 | // We are here because of a browser-side error, or likely as a result of the |
| 309 | // connection_error_handler on |binding_|, which can mean that the renderer |
| 310 | // has decided to close the pipe for various reasons (see all uses of |
| 311 | // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close |
| 312 | // the binding and the dialog, and ask to be deleted. |
| 313 | client_.reset(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 314 | binding_.Close(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 315 | delegate_->CloseDialog(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 316 | if (observer_for_testing_) |
| 317 | observer_for_testing_->OnConnectionTerminated(); |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 318 | |
| 319 | RecordFirstAbortReason(JourneyLogger::ABORT_REASON_MOJO_CONNECTION_ERROR); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 320 | manager_->DestroyRequest(this); |
| 321 | } |
| 322 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 323 | void PaymentRequest::Pay() { |
mathp | 57c8c86 | 2017-06-16 20:15:45 | [diff] [blame] | 324 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_PAY_CLICKED); |
Mathieu Perreault | 49a97e5 | 2017-08-15 01:51:36 | [diff] [blame] | 325 | journey_logger_.SetEventOccurred(JourneyLogger::EVENT_SELECTED_CREDIT_CARD); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 326 | state_->GeneratePaymentResponse(); |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 327 | } |
| 328 | |
sebsg | fcdd13c | 2017-06-08 15:49:33 | [diff] [blame] | 329 | void PaymentRequest::RecordFirstAbortReason( |
| 330 | JourneyLogger::AbortReason abort_reason) { |
| 331 | if (!has_recorded_completion_) { |
| 332 | has_recorded_completion_ = true; |
| 333 | journey_logger_.SetAborted(abort_reason); |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 337 | } // namespace payments |