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