| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/payments/content/payment_request.h" | 5 #include "components/payments/content/payment_request.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "components/payments/content/payment_details_validation.h" | 11 #include "components/payments/content/payment_details_validation.h" |
| 12 #include "components/payments/content/payment_request_web_contents_manager.h" | 12 #include "components/payments/content/payment_request_web_contents_manager.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 | 15 |
| 16 namespace payments { | 16 namespace payments { |
| 17 namespace { |
| 18 // When waiting for an UpdateWith call, timeout after 60 seconds. |
| 19 constexpr int kTimeoutDuration = 60; |
| 20 } // namespace |
| 17 | 21 |
| 18 PaymentRequest::PaymentRequest( | 22 PaymentRequest::PaymentRequest( |
| 19 content::WebContents* web_contents, | 23 content::WebContents* web_contents, |
| 20 std::unique_ptr<PaymentRequestDelegate> delegate, | 24 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 21 PaymentRequestWebContentsManager* manager, | 25 PaymentRequestWebContentsManager* manager, |
| 22 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request, | 26 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request, |
| 23 ObserverForTest* observer_for_testing) | 27 ObserverForTest* observer_for_testing) |
| 24 : web_contents_(web_contents), | 28 : web_contents_(web_contents), |
| 25 delegate_(std::move(delegate)), | 29 delegate_(std::move(delegate)), |
| 26 manager_(manager), | 30 manager_(manager), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 delegate_->ShowDialog(this); | 76 delegate_->ShowDialog(this); |
| 73 } | 77 } |
| 74 | 78 |
| 75 void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { | 79 void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 76 std::string error; | 80 std::string error; |
| 77 if (!payments::validatePaymentDetails(details, &error)) { | 81 if (!payments::validatePaymentDetails(details, &error)) { |
| 78 LOG(ERROR) << error; | 82 LOG(ERROR) << error; |
| 79 OnConnectionTerminated(); | 83 OnConnectionTerminated(); |
| 80 return; | 84 return; |
| 81 } | 85 } |
| 86 wait_timer_.Stop(); |
| 82 spec_->UpdateWith(std::move(details)); | 87 spec_->UpdateWith(std::move(details)); |
| 83 } | 88 } |
| 84 | 89 |
| 85 void PaymentRequest::Abort() { | 90 void PaymentRequest::Abort() { |
| 86 // The API user has decided to abort. We return a successful abort message to | 91 // 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 | 92 // the renderer, which closes the Mojo message pipe, which triggers |
| 88 // PaymentRequest::OnConnectionTerminated, which destroys this object. | 93 // PaymentRequest::OnConnectionTerminated, which destroys this object. |
| 89 if (client_.is_bound()) | 94 if (client_.is_bound()) |
| 90 client_->OnAbort(true /* aborted_successfully */); | 95 client_->OnAbort(true /* aborted_successfully */); |
| 91 } | 96 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 118 OnConnectionTerminated(); | 123 OnConnectionTerminated(); |
| 119 } | 124 } |
| 120 | 125 |
| 121 void PaymentRequest::OnPaymentResponseAvailable( | 126 void PaymentRequest::OnPaymentResponseAvailable( |
| 122 mojom::PaymentResponsePtr response) { | 127 mojom::PaymentResponsePtr response) { |
| 123 client_->OnPaymentResponse(std::move(response)); | 128 client_->OnPaymentResponse(std::move(response)); |
| 124 } | 129 } |
| 125 | 130 |
| 126 void PaymentRequest::OnShippingOptionIdSelected( | 131 void PaymentRequest::OnShippingOptionIdSelected( |
| 127 std::string shipping_option_id) { | 132 std::string shipping_option_id) { |
| 133 // TODO(anthonyvd): display an error before terminating the connection. |
| 134 wait_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeoutDuration), |
| 135 this, &PaymentRequest::OnConnectionTerminated); |
| 136 spec_->StartWaitingForUpdateWith( |
| 137 PaymentRequestSpec::UpdateReason::SHIPPING_OPTION); |
| 128 client_->OnShippingOptionChange(shipping_option_id); | 138 client_->OnShippingOptionChange(shipping_option_id); |
| 129 } | 139 } |
| 130 | 140 |
| 131 void PaymentRequest::OnShippingAddressSelected( | 141 void PaymentRequest::OnShippingAddressSelected( |
| 132 mojom::PaymentAddressPtr address) { | 142 mojom::PaymentAddressPtr address) { |
| 143 // TODO(anthonyvd): display an error before terminating the connection. |
| 144 wait_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeoutDuration), |
| 145 this, &PaymentRequest::OnConnectionTerminated); |
| 146 spec_->StartWaitingForUpdateWith( |
| 147 PaymentRequestSpec::UpdateReason::SHIPPING_ADDRESS); |
| 133 client_->OnShippingAddressChange(std::move(address)); | 148 client_->OnShippingAddressChange(std::move(address)); |
| 134 } | 149 } |
| 135 | 150 |
| 136 void PaymentRequest::UserCancelled() { | 151 void PaymentRequest::UserCancelled() { |
| 137 // If |client_| is not bound, then the object is already being destroyed as | 152 // If |client_| is not bound, then the object is already being destroyed as |
| 138 // a result of a renderer event. | 153 // a result of a renderer event. |
| 139 if (!client_.is_bound()) | 154 if (!client_.is_bound()) |
| 140 return; | 155 return; |
| 141 | 156 |
| 142 // This sends an error to the renderer, which informs the API user. | 157 // This sends an error to the renderer, which informs the API user. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 158 binding_.Close(); | 173 binding_.Close(); |
| 159 delegate_->CloseDialog(); | 174 delegate_->CloseDialog(); |
| 160 manager_->DestroyRequest(this); | 175 manager_->DestroyRequest(this); |
| 161 } | 176 } |
| 162 | 177 |
| 163 void PaymentRequest::Pay() { | 178 void PaymentRequest::Pay() { |
| 164 state_->GeneratePaymentResponse(); | 179 state_->GeneratePaymentResponse(); |
| 165 } | 180 } |
| 166 | 181 |
| 167 } // namespace payments | 182 } // namespace payments |
| OLD | NEW |