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