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