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 | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 11 | #include "components/payments/content/origin_security_checker.h" |
rouslan | 908248c | 2017-02-27 21:30:24 | [diff] [blame] | 12 | #include "components/payments/content/payment_details_validation.h" |
| 13 | #include "components/payments/content/payment_request_web_contents_manager.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
| 15 | #include "content/public/browser/web_contents.h" |
| 16 | |
| 17 | namespace payments { |
| 18 | |
| 19 | PaymentRequest::PaymentRequest( |
| 20 | content::WebContents* web_contents, |
| 21 | std::unique_ptr<PaymentRequestDelegate> delegate, |
| 22 | PaymentRequestWebContentsManager* manager, |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 23 | mojo::InterfaceRequest<mojom::PaymentRequest> request, |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 24 | ObserverForTest* observer_for_testing) |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 25 | : web_contents_(web_contents), |
| 26 | delegate_(std::move(delegate)), |
| 27 | manager_(manager), |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 28 | binding_(this, std::move(request)), |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 29 | observer_for_testing_(observer_for_testing), |
| 30 | journey_logger_(delegate_->IsIncognito(), |
| 31 | web_contents_->GetLastCommittedURL(), |
| 32 | delegate_->GetUkmService()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 33 | // OnConnectionTerminated will be called when the Mojo pipe is closed. This |
| 34 | // will happen as a result of many renderer-side events (both successful and |
| 35 | // erroneous in nature). |
| 36 | // TODO(crbug.com/683636): Investigate using |
| 37 | // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
| 38 | binding_.set_connection_error_handler(base::Bind( |
| 39 | &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | PaymentRequest::~PaymentRequest() {} |
| 43 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 44 | void PaymentRequest::Init(mojom::PaymentRequestClientPtr client, |
| 45 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 46 | mojom::PaymentDetailsPtr details, |
| 47 | mojom::PaymentOptionsPtr options) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 48 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 49 | client_ = std::move(client); |
| 50 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame^] | 51 | const GURL last_committed_url = delegate_->GetLastCommittedURL(); |
| 52 | if (!OriginSecurityChecker::IsOriginSecure(last_committed_url)) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 53 | LOG(ERROR) << "Not in a secure origin"; |
| 54 | OnConnectionTerminated(); |
| 55 | return; |
| 56 | } |
| 57 | |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame^] | 58 | bool allowed_origin = |
| 59 | OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) || |
| 60 | OriginSecurityChecker::IsOriginLocalhostOrFile(last_committed_url); |
| 61 | if (!allowed_origin) { |
| 62 | LOG(ERROR) << "Only localhost, file://, and cryptographic scheme origins " |
| 63 | "allowed"; |
| 64 | } |
| 65 | |
| 66 | bool invalid_ssl = |
| 67 | OriginSecurityChecker::IsSchemeCryptographic(last_committed_url) && |
| 68 | !delegate_->IsSslCertificateValid(); |
| 69 | if (invalid_ssl) |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 70 | LOG(ERROR) << "SSL certificate is not valid"; |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame^] | 71 | |
| 72 | if (!allowed_origin || invalid_ssl) { |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 73 | // Don't show UI. Resolve .canMakepayment() with "false". Reject .show() |
| 74 | // with "NotSupportedError". |
| 75 | spec_ = base::MakeUnique<PaymentRequestSpec>( |
| 76 | mojom::PaymentOptions::New(), mojom::PaymentDetails::New(), |
| 77 | std::vector<mojom::PaymentMethodDataPtr>(), this, |
| 78 | delegate_->GetApplicationLocale()); |
| 79 | state_ = base::MakeUnique<PaymentRequestState>( |
| 80 | spec_.get(), this, delegate_->GetApplicationLocale(), |
| 81 | delegate_->GetPersonalDataManager(), delegate_.get()); |
| 82 | return; |
| 83 | } |
| 84 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 85 | std::string error; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 86 | if (!validatePaymentDetails(details, &error)) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 87 | LOG(ERROR) << error; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 88 | OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 89 | return; |
| 90 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 91 | |
jinho.bang | fcb5ec9 | 2017-03-29 08:08:02 | [diff] [blame] | 92 | if (!details->total) { |
| 93 | LOG(ERROR) << "Missing total"; |
| 94 | OnConnectionTerminated(); |
| 95 | return; |
| 96 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 97 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 98 | spec_ = base::MakeUnique<PaymentRequestSpec>( |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 99 | std::move(options), std::move(details), std::move(method_data), this, |
| 100 | delegate_->GetApplicationLocale()); |
| 101 | state_ = base::MakeUnique<PaymentRequestState>( |
| 102 | spec_.get(), this, delegate_->GetApplicationLocale(), |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 103 | delegate_->GetPersonalDataManager(), delegate_.get()); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void PaymentRequest::Show() { |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 107 | if (!client_.is_bound() || !binding_.is_bound()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 108 | LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 109 | OnConnectionTerminated(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 110 | return; |
| 111 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 112 | |
rouslan | 7d433cc2 | 2017-05-08 15:18:07 | [diff] [blame] | 113 | // A tab can display only one PaymentRequest UI at a time. |
| 114 | if (!manager_->CanShow(this)) { |
| 115 | LOG(ERROR) << "A PaymentRequest UI is already showing"; |
| 116 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
| 117 | OnConnectionTerminated(); |
| 118 | return; |
| 119 | } |
| 120 | |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 121 | if (!state_->AreRequestedMethodsSupported()) { |
| 122 | client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED); |
| 123 | if (observer_for_testing_) |
| 124 | observer_for_testing_->OnNotSupportedError(); |
| 125 | OnConnectionTerminated(); |
| 126 | return; |
| 127 | } |
| 128 | |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 129 | journey_logger_.SetShowCalled(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 130 | delegate_->ShowDialog(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 131 | } |
| 132 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 133 | void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 134 | std::string error; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 135 | if (!validatePaymentDetails(details, &error)) { |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 136 | LOG(ERROR) << error; |
| 137 | OnConnectionTerminated(); |
| 138 | return; |
| 139 | } |
| 140 | spec_->UpdateWith(std::move(details)); |
| 141 | } |
| 142 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 143 | void PaymentRequest::Abort() { |
| 144 | // The API user has decided to abort. We return a successful abort message to |
| 145 | // the renderer, which closes the Mojo message pipe, which triggers |
| 146 | // PaymentRequest::OnConnectionTerminated, which destroys this object. |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 147 | // TODO(crbug.com/716546): Add a merchant abort metric, |
| 148 | journey_logger_.RecordJourneyStatsHistograms( |
| 149 | JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 150 | if (client_.is_bound()) |
| 151 | client_->OnAbort(true /* aborted_successfully */); |
| 152 | } |
| 153 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 154 | void PaymentRequest::Complete(mojom::PaymentComplete result) { |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 155 | if (!client_.is_bound()) |
| 156 | return; |
| 157 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 158 | if (result != mojom::PaymentComplete::SUCCESS) { |
| 159 | delegate_->ShowErrorMessage(); |
| 160 | } else { |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 161 | journey_logger_.RecordJourneyStatsHistograms( |
| 162 | JourneyLogger::COMPLETION_STATUS_COMPLETED); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 163 | // When the renderer closes the connection, |
| 164 | // PaymentRequest::OnConnectionTerminated will be called. |
| 165 | client_->OnComplete(); |
| 166 | } |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void PaymentRequest::CanMakePayment() { |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 170 | // TODO(crbug.com/704676): Implement a quota policy for this method. |
mathp | f39f46d | 2017-03-24 22:03:47 | [diff] [blame] | 171 | // PaymentRequest.canMakePayments() never returns false in incognito mode. |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 172 | client_->OnCanMakePayment( |
mathp | f39f46d | 2017-03-24 22:03:47 | [diff] [blame] | 173 | delegate_->IsIncognito() || state()->CanMakePayment() |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 174 | ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT |
| 175 | : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT); |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 176 | journey_logger_.SetCanMakePaymentValue(delegate_->IsIncognito() || |
| 177 | state()->CanMakePayment()); |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 178 | if (observer_for_testing_) |
| 179 | observer_for_testing_->OnCanMakePaymentCalled(); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 180 | } |
| 181 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 182 | void PaymentRequest::OnPaymentResponseAvailable( |
| 183 | mojom::PaymentResponsePtr response) { |
| 184 | client_->OnPaymentResponse(std::move(response)); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 185 | } |
| 186 | |
mathp | 151bd31 | 2017-04-03 21:07:24 | [diff] [blame] | 187 | void PaymentRequest::OnShippingOptionIdSelected( |
| 188 | std::string shipping_option_id) { |
| 189 | client_->OnShippingOptionChange(shipping_option_id); |
| 190 | } |
| 191 | |
| 192 | void PaymentRequest::OnShippingAddressSelected( |
| 193 | mojom::PaymentAddressPtr address) { |
| 194 | client_->OnShippingAddressChange(std::move(address)); |
| 195 | } |
| 196 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 197 | void PaymentRequest::UserCancelled() { |
| 198 | // If |client_| is not bound, then the object is already being destroyed as |
| 199 | // a result of a renderer event. |
| 200 | if (!client_.is_bound()) |
| 201 | return; |
| 202 | |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 203 | journey_logger_.RecordJourneyStatsHistograms( |
| 204 | JourneyLogger::COMPLETION_STATUS_USER_ABORTED); |
| 205 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 206 | // This sends an error to the renderer, which informs the API user. |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 207 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 208 | |
| 209 | // We close all bindings and ask to be destroyed. |
| 210 | client_.reset(); |
| 211 | binding_.Close(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame^] | 212 | if (observer_for_testing_) |
| 213 | observer_for_testing_->OnConnectionTerminated(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 214 | manager_->DestroyRequest(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 215 | } |
| 216 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 217 | void PaymentRequest::OnConnectionTerminated() { |
| 218 | // We are here because of a browser-side error, or likely as a result of the |
| 219 | // connection_error_handler on |binding_|, which can mean that the renderer |
| 220 | // has decided to close the pipe for various reasons (see all uses of |
| 221 | // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close |
| 222 | // the binding and the dialog, and ask to be deleted. |
| 223 | client_.reset(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 224 | binding_.Close(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 225 | delegate_->CloseDialog(); |
rouslan | b28f453 | 2017-05-08 15:41:47 | [diff] [blame^] | 226 | if (observer_for_testing_) |
| 227 | observer_for_testing_->OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 228 | manager_->DestroyRequest(this); |
| 229 | } |
| 230 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 231 | void PaymentRequest::Pay() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 232 | state_->GeneratePaymentResponse(); |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 233 | } |
| 234 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 235 | } // namespace payments |