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/origin_security_checker.h" |
11 #include "components/payments/content/payment_details_validation.h" | 12 #include "components/payments/content/payment_details_validation.h" |
12 #include "components/payments/content/payment_request_web_contents_manager.h" | 13 #include "components/payments/content/payment_request_web_contents_manager.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
15 | 16 |
16 namespace payments { | 17 namespace payments { |
17 | 18 |
18 PaymentRequest::PaymentRequest( | 19 PaymentRequest::PaymentRequest( |
19 content::WebContents* web_contents, | 20 content::WebContents* web_contents, |
20 std::unique_ptr<PaymentRequestDelegate> delegate, | 21 std::unique_ptr<PaymentRequestDelegate> delegate, |
21 PaymentRequestWebContentsManager* manager, | 22 PaymentRequestWebContentsManager* manager, |
22 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request, | 23 mojo::InterfaceRequest<mojom::PaymentRequest> request, |
23 ObserverForTest* observer_for_testing) | 24 ObserverForTest* observer_for_testing) |
24 : web_contents_(web_contents), | 25 : web_contents_(web_contents), |
25 delegate_(std::move(delegate)), | 26 delegate_(std::move(delegate)), |
26 manager_(manager), | 27 manager_(manager), |
27 binding_(this, std::move(request)), | 28 binding_(this, std::move(request)), |
28 observer_for_testing_(observer_for_testing) { | 29 observer_for_testing_(observer_for_testing) { |
29 // OnConnectionTerminated will be called when the Mojo pipe is closed. This | 30 // 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 // will happen as a result of many renderer-side events (both successful and |
31 // erroneous in nature). | 32 // erroneous in nature). |
32 // TODO(crbug.com/683636): Investigate using | 33 // TODO(crbug.com/683636): Investigate using |
33 // set_connection_error_with_reason_handler with Binding::CloseWithReason. | 34 // set_connection_error_with_reason_handler with Binding::CloseWithReason. |
34 binding_.set_connection_error_handler(base::Bind( | 35 binding_.set_connection_error_handler(base::Bind( |
35 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); | 36 &PaymentRequest::OnConnectionTerminated, base::Unretained(this))); |
36 } | 37 } |
37 | 38 |
38 PaymentRequest::~PaymentRequest() {} | 39 PaymentRequest::~PaymentRequest() {} |
39 | 40 |
40 void PaymentRequest::Init( | 41 void PaymentRequest::Init(mojom::PaymentRequestClientPtr client, |
41 payments::mojom::PaymentRequestClientPtr client, | 42 std::vector<mojom::PaymentMethodDataPtr> method_data, |
42 std::vector<payments::mojom::PaymentMethodDataPtr> method_data, | 43 mojom::PaymentDetailsPtr details, |
43 payments::mojom::PaymentDetailsPtr details, | 44 mojom::PaymentOptionsPtr options) { |
44 payments::mojom::PaymentOptionsPtr options) { | |
45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 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 |
46 std::string error; | 71 std::string error; |
47 if (!payments::validatePaymentDetails(details, &error)) { | 72 if (!validatePaymentDetails(details, &error)) { |
48 LOG(ERROR) << error; | 73 LOG(ERROR) << error; |
49 OnConnectionTerminated(); | 74 OnConnectionTerminated(); |
50 return; | 75 return; |
51 } | 76 } |
| 77 |
52 if (!details->total) { | 78 if (!details->total) { |
53 LOG(ERROR) << "Missing total"; | 79 LOG(ERROR) << "Missing total"; |
54 OnConnectionTerminated(); | 80 OnConnectionTerminated(); |
55 return; | 81 return; |
56 } | 82 } |
57 client_ = std::move(client); | 83 |
58 spec_ = base::MakeUnique<PaymentRequestSpec>( | 84 spec_ = base::MakeUnique<PaymentRequestSpec>( |
59 std::move(options), std::move(details), std::move(method_data), this, | 85 std::move(options), std::move(details), std::move(method_data), this, |
60 delegate_->GetApplicationLocale()); | 86 delegate_->GetApplicationLocale()); |
61 state_ = base::MakeUnique<PaymentRequestState>( | 87 state_ = base::MakeUnique<PaymentRequestState>( |
62 spec_.get(), this, delegate_->GetApplicationLocale(), | 88 spec_.get(), this, delegate_->GetApplicationLocale(), |
63 delegate_->GetPersonalDataManager(), delegate_.get()); | 89 delegate_->GetPersonalDataManager(), delegate_.get()); |
64 } | 90 } |
65 | 91 |
66 void PaymentRequest::Show() { | 92 void PaymentRequest::Show() { |
67 if (!client_.is_bound() || !binding_.is_bound()) { | 93 if (!client_.is_bound() || !binding_.is_bound()) { |
68 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; | 94 LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
69 OnConnectionTerminated(); | 95 OnConnectionTerminated(); |
70 return; | 96 return; |
71 } | 97 } |
| 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 |
72 delegate_->ShowDialog(this); | 107 delegate_->ShowDialog(this); |
73 } | 108 } |
74 | 109 |
75 void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { | 110 void PaymentRequest::UpdateWith(mojom::PaymentDetailsPtr details) { |
76 std::string error; | 111 std::string error; |
77 if (!payments::validatePaymentDetails(details, &error)) { | 112 if (!validatePaymentDetails(details, &error)) { |
78 LOG(ERROR) << error; | 113 LOG(ERROR) << error; |
79 OnConnectionTerminated(); | 114 OnConnectionTerminated(); |
80 return; | 115 return; |
81 } | 116 } |
82 spec_->UpdateWith(std::move(details)); | 117 spec_->UpdateWith(std::move(details)); |
83 } | 118 } |
84 | 119 |
85 void PaymentRequest::Abort() { | 120 void PaymentRequest::Abort() { |
86 // The API user has decided to abort. We return a successful abort message to | 121 // 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 | 122 // the renderer, which closes the Mojo message pipe, which triggers |
(...skipping 19 matching lines...) Expand all Loading... |
107 // TODO(crbug.com/704676): Implement a quota policy for this method. | 142 // TODO(crbug.com/704676): Implement a quota policy for this method. |
108 // PaymentRequest.canMakePayments() never returns false in incognito mode. | 143 // PaymentRequest.canMakePayments() never returns false in incognito mode. |
109 client_->OnCanMakePayment( | 144 client_->OnCanMakePayment( |
110 delegate_->IsIncognito() || state()->CanMakePayment() | 145 delegate_->IsIncognito() || state()->CanMakePayment() |
111 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT | 146 ? mojom::CanMakePaymentQueryResult::CAN_MAKE_PAYMENT |
112 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT); | 147 : mojom::CanMakePaymentQueryResult::CANNOT_MAKE_PAYMENT); |
113 if (observer_for_testing_) | 148 if (observer_for_testing_) |
114 observer_for_testing_->OnCanMakePaymentCalled(); | 149 observer_for_testing_->OnCanMakePaymentCalled(); |
115 } | 150 } |
116 | 151 |
117 void PaymentRequest::OnInvalidSpecProvided() { | |
118 OnConnectionTerminated(); | |
119 } | |
120 | |
121 void PaymentRequest::OnPaymentResponseAvailable( | 152 void PaymentRequest::OnPaymentResponseAvailable( |
122 mojom::PaymentResponsePtr response) { | 153 mojom::PaymentResponsePtr response) { |
123 client_->OnPaymentResponse(std::move(response)); | 154 client_->OnPaymentResponse(std::move(response)); |
124 } | 155 } |
125 | 156 |
126 void PaymentRequest::OnShippingOptionIdSelected( | 157 void PaymentRequest::OnShippingOptionIdSelected( |
127 std::string shipping_option_id) { | 158 std::string shipping_option_id) { |
128 client_->OnShippingOptionChange(shipping_option_id); | 159 client_->OnShippingOptionChange(shipping_option_id); |
129 } | 160 } |
130 | 161 |
131 void PaymentRequest::OnShippingAddressSelected( | 162 void PaymentRequest::OnShippingAddressSelected( |
132 mojom::PaymentAddressPtr address) { | 163 mojom::PaymentAddressPtr address) { |
133 client_->OnShippingAddressChange(std::move(address)); | 164 client_->OnShippingAddressChange(std::move(address)); |
134 } | 165 } |
135 | 166 |
136 void PaymentRequest::UserCancelled() { | 167 void PaymentRequest::UserCancelled() { |
137 // If |client_| is not bound, then the object is already being destroyed as | 168 // If |client_| is not bound, then the object is already being destroyed as |
138 // a result of a renderer event. | 169 // a result of a renderer event. |
139 if (!client_.is_bound()) | 170 if (!client_.is_bound()) |
140 return; | 171 return; |
141 | 172 |
142 // This sends an error to the renderer, which informs the API user. | 173 // This sends an error to the renderer, which informs the API user. |
143 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); | 174 client_->OnError(mojom::PaymentErrorReason::USER_CANCEL); |
144 | 175 |
145 // We close all bindings and ask to be destroyed. | 176 // We close all bindings and ask to be destroyed. |
146 client_.reset(); | 177 client_.reset(); |
147 binding_.Close(); | 178 binding_.Close(); |
148 manager_->DestroyRequest(this); | 179 manager_->DestroyRequest(this); |
149 } | 180 } |
150 | 181 |
151 void PaymentRequest::OnConnectionTerminated() { | 182 void PaymentRequest::OnConnectionTerminated() { |
152 // We are here because of a browser-side error, or likely as a result of the | 183 // 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 | 184 // 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 | 185 // has decided to close the pipe for various reasons (see all uses of |
155 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close | 186 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close |
156 // the binding and the dialog, and ask to be deleted. | 187 // the binding and the dialog, and ask to be deleted. |
157 client_.reset(); | 188 client_.reset(); |
158 binding_.Close(); | 189 binding_.Close(); |
159 delegate_->CloseDialog(); | 190 delegate_->CloseDialog(); |
160 manager_->DestroyRequest(this); | 191 manager_->DestroyRequest(this); |
161 } | 192 } |
162 | 193 |
163 void PaymentRequest::Pay() { | 194 void PaymentRequest::Pay() { |
164 state_->GeneratePaymentResponse(); | 195 state_->GeneratePaymentResponse(); |
165 } | 196 } |
166 | 197 |
167 } // namespace payments | 198 } // namespace payments |
OLD | NEW |