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