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 | |
| 51 | if (!OriginSecurityChecker::IsOriginSecure( |
| 52 | delegate_->GetLastCommittedURL())) { |
| 53 | LOG(ERROR) << "Not in a secure origin"; |
| 54 | OnConnectionTerminated(); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (OriginSecurityChecker::IsSchemeCryptographic( |
| 59 | delegate_->GetLastCommittedURL()) && |
| 60 | !delegate_->IsSslCertificateValid()) { |
| 61 | LOG(ERROR) << "SSL certificate is not valid"; |
| 62 | // Don't show UI. Resolve .canMakepayment() with "false". Reject .show() |
| 63 | // with "NotSupportedError". |
| 64 | spec_ = base::MakeUnique<PaymentRequestSpec>( |
| 65 | mojom::PaymentOptions::New(), mojom::PaymentDetails::New(), |
| 66 | std::vector<mojom::PaymentMethodDataPtr>(), this, |
| 67 | delegate_->GetApplicationLocale()); |
| 68 | state_ = base::MakeUnique<PaymentRequestState>( |
| 69 | spec_.get(), this, delegate_->GetApplicationLocale(), |
| 70 | delegate_->GetPersonalDataManager(), delegate_.get()); |
| 71 | return; |
| 72 | } |
| 73 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 74 | std::string error; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 75 | if (!validatePaymentDetails(details, &error)) { |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 76 | LOG(ERROR) << error; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 77 | OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 78 | return; |
| 79 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 80 | |
jinho.bang | fcb5ec9 | 2017-03-29 08:08:02 | [diff] [blame] | 81 | if (!details->total) { |
| 82 | LOG(ERROR) << "Missing total"; |
| 83 | OnConnectionTerminated(); |
| 84 | return; |
| 85 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 86 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 87 | spec_ = base::MakeUnique<PaymentRequestSpec>( |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 88 | std::move(options), std::move(details), std::move(method_data), this, |
| 89 | delegate_->GetApplicationLocale()); |
| 90 | state_ = base::MakeUnique<PaymentRequestState>( |
| 91 | spec_.get(), this, delegate_->GetApplicationLocale(), |
anthonyvd | d23ed70 | 2017-04-05 15:29:00 | [diff] [blame] | 92 | delegate_->GetPersonalDataManager(), delegate_.get()); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void PaymentRequest::Show() { |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 96 | if (!client_.is_bound() || !binding_.is_bound()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 97 | LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 98 | OnConnectionTerminated(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 99 | return; |
| 100 | } |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 101 | |
| 102 | if (!state_->AreRequestedMethodsSupported()) { |
| 103 | client_->OnError(mojom::PaymentErrorReason::NOT_SUPPORTED); |
| 104 | if (observer_for_testing_) |
| 105 | observer_for_testing_->OnNotSupportedError(); |
| 106 | OnConnectionTerminated(); |
| 107 | return; |
| 108 | } |
| 109 | |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 110 | journey_logger_.SetShowCalled(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 111 | delegate_->ShowDialog(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 112 | } |
| 113 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 114 | void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 115 | std::string error; |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 116 | if (!validatePaymentDetails(details, &error)) { |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 117 | LOG(ERROR) << error; |
| 118 | OnConnectionTerminated(); |
| 119 | return; |
| 120 | } |
| 121 | spec_->UpdateWith(std::move(details)); |
| 122 | } |
| 123 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 124 | void PaymentRequest::Abort() { |
| 125 | // The API user has decided to abort. We return a successful abort message to |
| 126 | // the renderer, which closes the Mojo message pipe, which triggers |
| 127 | // PaymentRequest::OnConnectionTerminated, which destroys this object. |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 128 | // TODO(crbug.com/716546): Add a merchant abort metric, |
| 129 | journey_logger_.RecordJourneyStatsHistograms( |
| 130 | JourneyLogger::COMPLETION_STATUS_OTHER_ABORTED); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 131 | if (client_.is_bound()) |
| 132 | client_->OnAbort(true /* aborted_successfully */); |
| 133 | } |
| 134 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 135 | void PaymentRequest::Complete(mojom::PaymentComplete result) { |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 136 | if (!client_.is_bound()) |
| 137 | return; |
| 138 | |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 139 | if (result != mojom::PaymentComplete::SUCCESS) { |
| 140 | delegate_->ShowErrorMessage(); |
| 141 | } else { |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 142 | journey_logger_.RecordJourneyStatsHistograms( |
| 143 | JourneyLogger::COMPLETION_STATUS_COMPLETED); |
mathp | 21879589 | 2017-03-29 15:15:34 | [diff] [blame] | 144 | // When the renderer closes the connection, |
| 145 | // PaymentRequest::OnConnectionTerminated will be called. |
| 146 | client_->OnComplete(); |
| 147 | } |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void PaymentRequest::CanMakePayment() { |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 151 | // TODO(crbug.com/704676): Implement a quota policy for this method. |
mathp | f39f46d | 2017-03-24 22:03:47 | [diff] [blame] | 152 | // PaymentRequest.canMakePayments() never returns false in incognito mode. |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 153 | client_->OnCanMakePayment( |
mathp | f39f46d | 2017-03-24 22:03:47 | [diff] [blame] | 154 | delegate_->IsIncognito() || state()->CanMakePayment() |
mathp | 1a5be4f | 2017-03-24 18:09:19 | [diff] [blame] | 155 | ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT |
| 156 | : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT); |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 157 | journey_logger_.SetCanMakePaymentValue(delegate_->IsIncognito() || |
| 158 | state()->CanMakePayment()); |
mathp | 300fa54 | 2017-03-27 19:29:37 | [diff] [blame] | 159 | if (observer_for_testing_) |
| 160 | observer_for_testing_->OnCanMakePaymentCalled(); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 161 | } |
| 162 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 163 | void PaymentRequest::OnPaymentResponseAvailable( |
| 164 | mojom::PaymentResponsePtr response) { |
| 165 | client_->OnPaymentResponse(std::move(response)); |
mathp | 4b85b58 | 2017-03-08 21:07:16 | [diff] [blame] | 166 | } |
| 167 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 168 | void PaymentRequest::OnShippingOptionIdSelected( |
| 169 | std::string shipping_option_id) { |
| 170 | client_->OnShippingOptionChange(shipping_option_id); |
| 171 | } |
| 172 | |
| 173 | void PaymentRequest::OnShippingAddressSelected( |
| 174 | mojom::PaymentAddressPtr address) { |
| 175 | client_->OnShippingAddressChange(std::move(address)); |
| 176 | } |
| 177 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 178 | void PaymentRequest::UserCancelled() { |
| 179 | // If |client_| is not bound, then the object is already being destroyed as |
| 180 | // a result of a renderer event. |
| 181 | if (!client_.is_bound()) |
| 182 | return; |
| 183 | |
sebsg | 20b49d7b | 2017-05-04 20:23:17 | [diff] [blame] | 184 | journey_logger_.RecordJourneyStatsHistograms( |
| 185 | JourneyLogger::COMPLETION_STATUS_USER_ABORTED); |
| 186 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 187 | // This sends an error to the renderer, which informs the API user. |
rouslan | 6e3cf7c6 | 2017-04-17 21:23:28 | [diff] [blame] | 188 | client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 189 | |
| 190 | // We close all bindings and ask to be destroyed. |
| 191 | client_.reset(); |
| 192 | binding_.Close(); |
| 193 | manager_->DestroyRequest(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 194 | } |
| 195 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 196 | void PaymentRequest::OnConnectionTerminated() { |
| 197 | // We are here because of a browser-side error, or likely as a result of the |
| 198 | // connection_error_handler on |binding_|, which can mean that the renderer |
| 199 | // has decided to close the pipe for various reasons (see all uses of |
| 200 | // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close |
| 201 | // the binding and the dialog, and ask to be deleted. |
| 202 | client_.reset(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 203 | binding_.Close(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 204 | delegate_->CloseDialog(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 205 | manager_->DestroyRequest(this); |
| 206 | } |
| 207 | |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 208 | void PaymentRequest::Pay() { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 209 | state_->GeneratePaymentResponse(); |
mathp | d4be8de8 | 2017-03-01 00:51:48 | [diff] [blame] | 210 | } |
| 211 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 212 | } // namespace payments |