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 "modules/payments/PaymentRequest.h" | 5 #include "modules/payments/PaymentRequest.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/JSONValuesForV8.h" | 8 #include "bindings/core/v8/JSONValuesForV8.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
11 #include "core/EventTypeNames.h" | 11 #include "core/EventTypeNames.h" |
12 #include "core/dom/DOMException.h" | 12 #include "core/dom/DOMException.h" |
13 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
14 #include "core/events/Event.h" | 14 #include "core/events/Event.h" |
15 #include "core/events/EventQueue.h" | 15 #include "core/events/EventQueue.h" |
16 #include "modules/EventTargetModulesNames.h" | 16 #include "modules/EventTargetModulesNames.h" |
17 #include "modules/payments/PaymentItem.h" | 17 #include "modules/payments/PaymentItem.h" |
18 #include "modules/payments/PaymentResponse.h" | 18 #include "modules/payments/PaymentResponse.h" |
19 #include "modules/payments/PaymentsValidators.h" | 19 #include "modules/payments/PaymentsValidators.h" |
20 #include "modules/payments/ShippingAddress.h" | 20 #include "modules/payments/ShippingAddress.h" |
21 #include "modules/payments/ShippingOption.h" | 21 #include "modules/payments/ShippingOption.h" |
22 #include "mojo/public/cpp/bindings/interface_request.h" | 22 #include "mojo/public/cpp/bindings/interface_request.h" |
23 #include "mojo/public/cpp/bindings/wtf_array.h" | 23 #include "mojo/public/cpp/bindings/wtf_array.h" |
24 #include "platform/mojo/MojoHelper.h" | 24 #include "platform/mojo/MojoHelper.h" |
25 #include "public/platform/ServiceRegistry.h" | 25 #include "public/platform/ServiceRegistry.h" |
26 #include <utility> | 26 #include <utility> |
27 | 27 |
28 namespace mojo { | 28 namespace mojo { |
29 | 29 |
30 using blink::mojom::wtf::CurrencyAmount; | 30 using blink::mojom::blink::CurrencyAmount; |
31 using blink::mojom::wtf::CurrencyAmountPtr; | 31 using blink::mojom::blink::CurrencyAmountPtr; |
32 using blink::mojom::wtf::PaymentDetails; | 32 using blink::mojom::blink::PaymentDetails; |
33 using blink::mojom::wtf::PaymentDetailsPtr; | 33 using blink::mojom::blink::PaymentDetailsPtr; |
34 using blink::mojom::wtf::PaymentItem; | 34 using blink::mojom::blink::PaymentItem; |
35 using blink::mojom::wtf::PaymentItemPtr; | 35 using blink::mojom::blink::PaymentItemPtr; |
36 using blink::mojom::wtf::PaymentOptions; | 36 using blink::mojom::blink::PaymentOptions; |
37 using blink::mojom::wtf::PaymentOptionsPtr; | 37 using blink::mojom::blink::PaymentOptionsPtr; |
38 using blink::mojom::wtf::ShippingOption; | 38 using blink::mojom::blink::ShippingOption; |
39 using blink::mojom::wtf::ShippingOptionPtr; | 39 using blink::mojom::blink::ShippingOptionPtr; |
40 | 40 |
41 template <> | 41 template <> |
42 struct TypeConverter<CurrencyAmountPtr, blink::CurrencyAmount> { | 42 struct TypeConverter<CurrencyAmountPtr, blink::CurrencyAmount> { |
43 static CurrencyAmountPtr Convert(const blink::CurrencyAmount& input) | 43 static CurrencyAmountPtr Convert(const blink::CurrencyAmount& input) |
44 { | 44 { |
45 CurrencyAmountPtr output = CurrencyAmount::New(); | 45 CurrencyAmountPtr output = CurrencyAmount::New(); |
46 output->currency_code = input.currencyCode(); | 46 output->currency_code = input.currencyCode(); |
47 output->value = input.value(); | 47 output->value = input.value(); |
48 return output; | 48 return output; |
49 } | 49 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 if (m_showResolver) | 171 if (m_showResolver) |
172 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Already called show() once")); | 172 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Already called show() once")); |
173 | 173 |
174 if (!scriptState->domWindow() || !scriptState->domWindow()->frame()) | 174 if (!scriptState->domWindow() || !scriptState->domWindow()->frame()) |
175 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Cannot show the payment request")); | 175 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Cannot show the payment request")); |
176 | 176 |
177 DCHECK(!m_paymentProvider.is_bound()); | 177 DCHECK(!m_paymentProvider.is_bound()); |
178 scriptState->domWindow()->frame()->serviceRegistry()->connectToRemoteService
(mojo::GetProxy(&m_paymentProvider)); | 178 scriptState->domWindow()->frame()->serviceRegistry()->connectToRemoteService
(mojo::GetProxy(&m_paymentProvider)); |
179 m_paymentProvider.set_connection_error_handler(sameThreadBindForMojo(&Paymen
tRequest::OnError, this)); | 179 m_paymentProvider.set_connection_error_handler(sameThreadBindForMojo(&Paymen
tRequest::OnError, this)); |
180 m_paymentProvider->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); | 180 m_paymentProvider->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); |
181 m_paymentProvider->Show(std::move(m_supportedMethods), mojom::wtf::PaymentDe
tails::From(m_details), mojom::wtf::PaymentOptions::From(m_options), m_stringifi
edData.isNull() ? "" : m_stringifiedData); | 181 m_paymentProvider->Show(std::move(m_supportedMethods), mojom::blink::Payment
Details::From(m_details), mojom::blink::PaymentOptions::From(m_options), m_strin
gifiedData.isNull() ? "" : m_stringifiedData); |
182 | 182 |
183 m_showResolver = ScriptPromiseResolver::create(scriptState); | 183 m_showResolver = ScriptPromiseResolver::create(scriptState); |
184 return m_showResolver->promise(); | 184 return m_showResolver->promise(); |
185 } | 185 } |
186 | 186 |
187 void PaymentRequest::abort(ExceptionState& exceptionState) | 187 void PaymentRequest::abort(ExceptionState& exceptionState) |
188 { | 188 { |
189 if (!m_showResolver) { | 189 if (!m_showResolver) { |
190 exceptionState.throwDOMException(InvalidStateError, "Never called show()
, so nothing to abort"); | 190 exceptionState.throwDOMException(InvalidStateError, "Never called show()
, so nothing to abort"); |
191 return; | 191 return; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 // Set the currently selected option if only one option was passed. | 296 // Set the currently selected option if only one option was passed. |
297 if (details.hasShippingOptions() && details.shippingOptions().size() == 1) | 297 if (details.hasShippingOptions() && details.shippingOptions().size() == 1) |
298 m_shippingOption = details.shippingOptions().begin()->id(); | 298 m_shippingOption = details.shippingOptions().begin()->id(); |
299 } | 299 } |
300 | 300 |
301 void PaymentRequest::contextDestroyed() | 301 void PaymentRequest::contextDestroyed() |
302 { | 302 { |
303 cleanUp(); | 303 cleanUp(); |
304 } | 304 } |
305 | 305 |
306 void PaymentRequest::OnShippingAddressChange(mojom::wtf::ShippingAddressPtr addr
ess) | 306 void PaymentRequest::OnShippingAddressChange(mojom::blink::ShippingAddressPtr ad
dress) |
307 { | 307 { |
308 DCHECK(m_showResolver); | 308 DCHECK(m_showResolver); |
309 DCHECK(!m_completeResolver); | 309 DCHECK(!m_completeResolver); |
310 | 310 |
311 String errorMessage; | 311 String errorMessage; |
312 if (!PaymentsValidators::isValidRegionCodeFormat(address->region_code, &erro
rMessage) | 312 if (!PaymentsValidators::isValidRegionCodeFormat(address->region_code, &erro
rMessage) |
313 || !PaymentsValidators::isValidLanguageCodeFormat(address->language_code
, &errorMessage) | 313 || !PaymentsValidators::isValidLanguageCodeFormat(address->language_code
, &errorMessage) |
314 || !PaymentsValidators::isValidScriptCodeFormat(address->script_code, &e
rrorMessage)) { | 314 || !PaymentsValidators::isValidScriptCodeFormat(address->script_code, &e
rrorMessage)) { |
315 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); | 315 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); |
316 cleanUp(); | 316 cleanUp(); |
(...skipping 15 matching lines...) Expand all Loading... |
332 void PaymentRequest::OnShippingOptionChange(const String& shippingOptionId) | 332 void PaymentRequest::OnShippingOptionChange(const String& shippingOptionId) |
333 { | 333 { |
334 DCHECK(m_showResolver); | 334 DCHECK(m_showResolver); |
335 DCHECK(!m_completeResolver); | 335 DCHECK(!m_completeResolver); |
336 m_shippingOption = shippingOptionId; | 336 m_shippingOption = shippingOptionId; |
337 Event* event = Event::create(EventTypeNames::shippingoptionchange); | 337 Event* event = Event::create(EventTypeNames::shippingoptionchange); |
338 event->setTarget(this); | 338 event->setTarget(this); |
339 getExecutionContext()->getEventQueue()->enqueueEvent(event); | 339 getExecutionContext()->getEventQueue()->enqueueEvent(event); |
340 } | 340 } |
341 | 341 |
342 void PaymentRequest::OnPaymentResponse(mojom::wtf::PaymentResponsePtr response) | 342 void PaymentRequest::OnPaymentResponse(mojom::blink::PaymentResponsePtr response
) |
343 { | 343 { |
344 DCHECK(m_showResolver); | 344 DCHECK(m_showResolver); |
345 DCHECK(!m_completeResolver); | 345 DCHECK(!m_completeResolver); |
346 m_showResolver->resolve(new PaymentResponse(std::move(response), this)); | 346 m_showResolver->resolve(new PaymentResponse(std::move(response), this)); |
347 } | 347 } |
348 | 348 |
349 void PaymentRequest::OnError() | 349 void PaymentRequest::OnError() |
350 { | 350 { |
351 if (m_completeResolver) | 351 if (m_completeResolver) |
352 m_completeResolver->reject(DOMException::create(SyntaxError, "Request ca
ncelled")); | 352 m_completeResolver->reject(DOMException::create(SyntaxError, "Request ca
ncelled")); |
(...skipping 12 matching lines...) Expand all Loading... |
365 void PaymentRequest::cleanUp() | 365 void PaymentRequest::cleanUp() |
366 { | 366 { |
367 m_completeResolver.clear(); | 367 m_completeResolver.clear(); |
368 m_showResolver.clear(); | 368 m_showResolver.clear(); |
369 if (m_clientBinding.is_bound()) | 369 if (m_clientBinding.is_bound()) |
370 m_clientBinding.Close(); | 370 m_clientBinding.Close(); |
371 m_paymentProvider.reset(); | 371 m_paymentProvider.reset(); |
372 } | 372 } |
373 | 373 |
374 } // namespace blink | 374 } // namespace blink |
OLD | NEW |