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