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" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 11 | #include "components/payments/content/can_make_payment_query_factory.h" |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 12 | #include "components/payments/content/origin_security_checker.h" |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 13 | #include "components/payments/content/payment_details_validation.h" |
| 14 | #include "components/payments/content/payment_request_web_contents_manager.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 15 | #include "components/payments/core/can_make_payment_query.h" |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 16 | #include "components/payments/core/payment_prefs.h" |
| 17 | #include "components/prefs/pref_service.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 19 | #include "content/public/browser/render_frame_host.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 20 | #include "content/public/browser/web_contents.h" |
| 21 | |
| 22 | namespace payments { |
| 23 | |
| 24 | PaymentRequest::PaymentRequest( |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 25 | content::RenderFrameHost* render_frame_host, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 26 | content::WebContents* web_contents, |
| 27 | std::unique_ptr<PaymentRequestDelegate> delegate, |
| 28 | PaymentRequestWebContentsManager* manager, |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 29 | mojo::InterfaceRequest<mojom::PaymentRequest> request, |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 30 | ObserverForTest* observer_for_testing) |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 31 | : web_contents_(web_contents), |
| 32 | delegate_(std::move(delegate)), |
| 33 | manager_(manager), |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 34 | binding_(this, std::move(request)), |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 35 | frame_origin_(GURL(render_frame_host->GetLastCommittedURL()).GetOrigin()), |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 36 | observer_for_testing_(observer_for_testing), |
| 37 | journey_logger_(delegate_->IsIncognito(), |
| 38 | web_contents_->GetLastCommittedURL(), |
oysteine | b068f27 | 2017-05-23 00:14:01 | [diff] [blame] | 39 | delegate_->GetUkmRecorder()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 40 | // 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))); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | PaymentRequest::~PaymentRequest() {} |
| 50 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 51 | void PaymentRequest::Init(mojom::PaymentRequestClientPtr client, |
| 52 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 53 | mojom::PaymentDetailsPtr details, |
| 54 | mojom::PaymentOptionsPtr options) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 55 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 56 | client_ = std::move(client); |
| 57 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 58 | const GURL last_committed_url = delegate_->GetLastCommittedURL(); |
| 59 | if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 60 | LOG(ERROR) << "Not in a secure origin"; |
| 61 | OnConnectionTerminated(); |
| 62 | return; |
| 63 | } |
| 64 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 65 | 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) |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 77 | LOG(ERROR) << "SSL certificate is not valid"; |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 78 | |
| 79 | if (!allowed_origin || invalid_ssl) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 80 | // 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 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 92 | std::string error; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 93 | if (!validatePaymentDetails(details, &error)) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 94 | LOG(ERROR) << error; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 95 | OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 96 | return; |
| 97 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 98 | |
jinho.bang | fcb5ec9 | 2017-03-29 08:08:02 | [diff] [blame] | 99 | if (!details->total) { |
| 100 | LOG(ERROR) << "Missing total"; |
| 101 | OnConnectionTerminated(); |
| 102 | return; |
| 103 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 104 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 105 | spec_ = base::MakeUnique<PaymentRequestSpec>( |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 106 | 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(), |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 110 | delegate_->GetPersonalDataManager(), delegate_.get()); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void PaymentRequest::Show() { |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 114 | if (!client_.is_bound() || !binding_.is_bound()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 115 | LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 116 | OnConnectionTerminated(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 117 | return; |
| 118 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 119 | |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 120 | // 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 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 128 | 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 | |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 136 | journey_logger_.SetShowCalled(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 137 | delegate_->ShowDialog(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 138 | } |
| 139 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 140 | void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 141 | std::string error; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 142 | if (!validatePaymentDetails(details, &error)) { |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 143 | LOG(ERROR) << error; |
| 144 | OnConnectionTerminated(); |
| 145 | return; |
| 146 | } |
| 147 | spec_->UpdateWith(std::move(details)); |
| 148 | } |
| 149 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 150 | void PaymentRequest::Abort() { |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 151 | // 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 |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 153 | // PaymentRequest::OnConnectionTerminated, which destroys this object. |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 154 | // 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 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 163 | if (client_.is_bound()) |
Anthony Vallee-Dubois | 6813c144 | 2017-05-17 19:32:56 | [diff] [blame] | 164 | client_->OnAbort(accepting_abort); |
| 165 | |
| 166 | if (observer_for_testing_) |
| 167 | observer_for_testing_->OnAbortCalled(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 168 | } |
| 169 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 170 | void PaymentRequest::Complete(mojom::PaymentComplete result) { |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 171 | if (!client_.is_bound()) |
| 172 | return; |
| 173 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 174 | if (result != mojom::PaymentComplete::SUCCESS) { |
| 175 | delegate_->ShowErrorMessage(); |
| 176 | } else { |
sebsg | f8272a2 | 2017-05-26 14:32:58 | [diff] [blame^] | 177 | journey_logger_.SetCompleted(); |
anthonyvd | 6a43b93 | 2017-05-11 18:39:27 | [diff] [blame] | 178 | delegate_->GetPrefService()->SetBoolean(kPaymentsFirstTransactionCompleted, |
| 179 | true); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 180 | // When the renderer closes the connection, |
| 181 | // PaymentRequest::OnConnectionTerminated will be called. |
| 182 | client_->OnComplete(); |
sebsg | 8a93b27 | 2017-05-11 19:30:22 | [diff] [blame] | 183 | state_->RecordUseStats(); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 184 | } |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void PaymentRequest::CanMakePayment() { |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 188 | bool can_make_payment = state()->CanMakePayment(); |
| 189 | if (delegate_->IsIncognito()) { |
| 190 | client_->OnCanMakePayment( |
| 191 | mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT); |
| 192 | journey_logger_.SetCanMakePaymentValue(true); |
| 193 | } else if (CanMakePaymentQueryFactory::GetInstance() |
| 194 | ->GetForContext(web_contents_->GetBrowserContext()) |
| 195 | ->CanQuery(frame_origin_, spec()->stringified_method_data())) { |
| 196 | client_->OnCanMakePayment( |
| 197 | can_make_payment |
| 198 | ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT |
| 199 | : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT); |
| 200 | journey_logger_.SetCanMakePaymentValue(can_make_payment); |
| 201 | } else if (OriginSecurityChecker::IsOriginLocalhostOrFile(frame_origin_)) { |
| 202 | client_->OnCanMakePayment( |
| 203 | can_make_payment |
| 204 | ? mojom::CanMakePaymentQueryResult::WARNING_CAN_MAKE_PAYMENT |
| 205 | : mojom::CanMakePaymentQueryResult::WARNING_CANNOT_MAKE_PAYMENT); |
| 206 | journey_logger_.SetCanMakePaymentValue(can_make_payment); |
| 207 | } else { |
| 208 | client_->OnCanMakePayment( |
| 209 | mojom::CanMakePaymentQueryResult::QUERY_QUOTA_EXCEEDED); |
| 210 | } |
| 211 | |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 212 | if (observer_for_testing_) |
| 213 | observer_for_testing_->OnCanMakePaymentCalled(); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 214 | } |
| 215 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 216 | void PaymentRequest::OnPaymentResponseAvailable( |
| 217 | mojom::PaymentResponsePtr response) { |
| 218 | client_->OnPaymentResponse(std::move(response)); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 219 | } |
| 220 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 221 | void PaymentRequest::OnShippingOptionIdSelected( |
| 222 | std::string shipping_option_id) { |
| 223 | client_->OnShippingOptionChange(shipping_option_id); |
| 224 | } |
| 225 | |
| 226 | void PaymentRequest::OnShippingAddressSelected( |
| 227 | mojom::PaymentAddressPtr address) { |
| 228 | client_->OnShippingAddressChange(std::move(address)); |
| 229 | } |
| 230 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 231 | void PaymentRequest::UserCancelled() { |
| 232 | // If |client_| is not bound, then the object is already being destroyed as |
| 233 | // a result of a renderer event. |
| 234 | if (!client_.is_bound()) |
| 235 | return; |
| 236 | |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 237 | RecordFirstCompletionStatus(JourneyLogger::COMPLETION_STATUS_USER_ABORTED); |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 238 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 239 | // This sends an error to the renderer, which informs the API user. |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 240 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 241 | |
| 242 | // We close all bindings and ask to be destroyed. |
| 243 | client_.reset(); |
| 244 | binding_.Close(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 245 | if (observer_for_testing_) |
| 246 | observer_for_testing_->OnConnectionTerminated(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 247 | manager_->DestroyRequest(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 248 | } |
| 249 | |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 250 | void PaymentRequest::DidStartNavigation(bool is_user_initiated) { |
| 251 | RecordFirstCompletionStatus( |
| 252 | is_user_initiated ? JourneyLogger::COMPLETION_STATUS_USER_ABORTED |
| 253 | : JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED); |
| 254 | } |
| 255 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 256 | void PaymentRequest::OnConnectionTerminated() { |
| 257 | // We are here because of a browser-side error, or likely as a result of the |
| 258 | // connection_error_handler on |binding_|, which can mean that the renderer |
| 259 | // has decided to close the pipe for various reasons (see all uses of |
| 260 | // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close |
| 261 | // the binding and the dialog, and ask to be deleted. |
| 262 | client_.reset(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 263 | binding_.Close(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 264 | delegate_->CloseDialog(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame] | 265 | if (observer_for_testing_) |
| 266 | observer_for_testing_->OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 267 | manager_->DestroyRequest(this); |
| 268 | } |
| 269 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 270 | void PaymentRequest::Pay() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 271 | state_->GeneratePaymentResponse(); |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 272 | } |
| 273 | |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 274 | void PaymentRequest::RecordFirstCompletionStatus( |
| 275 | JourneyLogger::CompletionStatus completion_status) { |
| 276 | if (!has_recorded_abort_reason_) { |
| 277 | has_recorded_abort_reason_ = true; |
sebsg | f8272a2 | 2017-05-26 14:32:58 | [diff] [blame^] | 278 | // TODO(crbug.com/716546): Record more abort reasons. |
| 279 | if (completion_status == JourneyLogger::COMPLETION_STATUS_USER_ABORTED) { |
| 280 | journey_logger_.SetAborted(JourneyLogger::ABORT_REASON_ABORTED_BY_USER); |
| 281 | } else { |
| 282 | journey_logger_.SetAborted(JourneyLogger::ABORT_REASON_OTHER); |
| 283 | } |
sebsg | 2c8558a | 2017-05-17 18:54:10 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 287 | } // namespace payments |