Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: third_party/WebKit/Source/modules/payments/PaymentRequest.cpp

Issue 2477883002: [Web Payments] Mojom namespace blink -> payments (Closed)
Patch Set: Fix WebKit tests Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/ScriptPromiseResolver.h" 8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "bindings/core/v8/ScriptState.h" 9 #include "bindings/core/v8/ScriptState.h"
10 #include "bindings/core/v8/V8StringResource.h" 10 #include "bindings/core/v8/V8StringResource.h"
(...skipping 17 matching lines...) Expand all
28 #include "mojo/public/cpp/bindings/wtf_array.h" 28 #include "mojo/public/cpp/bindings/wtf_array.h"
29 #include "platform/mojo/MojoHelper.h" 29 #include "platform/mojo/MojoHelper.h"
30 #include "public/platform/InterfaceProvider.h" 30 #include "public/platform/InterfaceProvider.h"
31 #include "public/platform/Platform.h" 31 #include "public/platform/Platform.h"
32 #include "public/platform/WebTraceLocation.h" 32 #include "public/platform/WebTraceLocation.h"
33 #include "wtf/HashSet.h" 33 #include "wtf/HashSet.h"
34 #include <utility> 34 #include <utility>
35 35
36 namespace mojo { 36 namespace mojo {
37 37
38 using blink::mojom::blink::PaymentCurrencyAmount; 38 using payments::mojom::blink::PaymentCurrencyAmount;
39 using blink::mojom::blink::PaymentCurrencyAmountPtr; 39 using payments::mojom::blink::PaymentCurrencyAmountPtr;
40 using blink::mojom::blink::PaymentDetails; 40 using payments::mojom::blink::PaymentDetails;
41 using blink::mojom::blink::PaymentDetailsModifier; 41 using payments::mojom::blink::PaymentDetailsModifier;
42 using blink::mojom::blink::PaymentDetailsModifierPtr; 42 using payments::mojom::blink::PaymentDetailsModifierPtr;
43 using blink::mojom::blink::PaymentDetailsPtr; 43 using payments::mojom::blink::PaymentDetailsPtr;
44 using blink::mojom::blink::PaymentErrorReason; 44 using payments::mojom::blink::PaymentErrorReason;
45 using blink::mojom::blink::PaymentItem; 45 using payments::mojom::blink::PaymentItem;
46 using blink::mojom::blink::PaymentItemPtr; 46 using payments::mojom::blink::PaymentItemPtr;
47 using blink::mojom::blink::PaymentMethodData; 47 using payments::mojom::blink::PaymentMethodData;
48 using blink::mojom::blink::PaymentMethodDataPtr; 48 using payments::mojom::blink::PaymentMethodDataPtr;
49 using blink::mojom::blink::PaymentOptions; 49 using payments::mojom::blink::PaymentOptions;
50 using blink::mojom::blink::PaymentOptionsPtr; 50 using payments::mojom::blink::PaymentOptionsPtr;
51 using blink::mojom::blink::PaymentShippingOption; 51 using payments::mojom::blink::PaymentShippingOption;
52 using blink::mojom::blink::PaymentShippingOptionPtr; 52 using payments::mojom::blink::PaymentShippingOptionPtr;
53 using blink::mojom::blink::PaymentShippingType; 53 using payments::mojom::blink::PaymentShippingType;
54 54
55 template <> 55 template <>
56 struct TypeConverter<PaymentCurrencyAmountPtr, blink::PaymentCurrencyAmount> { 56 struct TypeConverter<PaymentCurrencyAmountPtr, blink::PaymentCurrencyAmount> {
57 static PaymentCurrencyAmountPtr Convert( 57 static PaymentCurrencyAmountPtr Convert(
58 const blink::PaymentCurrencyAmount& input) { 58 const blink::PaymentCurrencyAmount& input) {
59 PaymentCurrencyAmountPtr output = PaymentCurrencyAmount::New(); 59 PaymentCurrencyAmountPtr output = PaymentCurrencyAmount::New();
60 output->currency = input.currency(); 60 output->currency = input.currency();
61 output->value = input.value(); 61 output->value = input.value();
62 if (input.hasCurrencySystem()) 62 if (input.hasCurrencySystem())
63 output->currencySystem = input.currencySystem(); 63 output->currencySystem = input.currencySystem();
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 static const char* const validValues[] = { 411 static const char* const validValues[] = {
412 "shipping", "delivery", "pickup", 412 "shipping", "delivery", "pickup",
413 }; 413 };
414 for (size_t i = 0; i < WTF_ARRAY_LENGTH(validValues); i++) { 414 for (size_t i = 0; i < WTF_ARRAY_LENGTH(validValues); i++) {
415 if (shippingType == validValues[i]) 415 if (shippingType == validValues[i])
416 return shippingType; 416 return shippingType;
417 } 417 }
418 return validValues[0]; 418 return validValues[0];
419 } 419 }
420 420
421 mojom::blink::PaymentDetailsPtr maybeKeepShippingOptions( 421 payments::mojom::blink::PaymentDetailsPtr maybeKeepShippingOptions(
422 mojom::blink::PaymentDetailsPtr details, 422 payments::mojom::blink::PaymentDetailsPtr details,
423 bool keep) { 423 bool keep) {
424 if (!keep) 424 if (!keep)
425 details->shipping_options.resize(0); 425 details->shipping_options.resize(0);
426 426
427 return details; 427 return details;
428 } 428 }
429 429
430 bool allowedToUsePaymentRequest(const Frame* frame) { 430 bool allowedToUsePaymentRequest(const Frame* frame) {
431 // To determine whether a Document object |document| is allowed to use the 431 // To determine whether a Document object |document| is allowed to use the
432 // feature indicated by attribute name |allowpaymentrequest|, run these steps: 432 // feature indicated by attribute name |allowpaymentrequest|, run these steps:
(...skipping 15 matching lines...) Expand all
448 if (ownerElement && isHTMLIFrameElement(ownerElement)) { 448 if (ownerElement && isHTMLIFrameElement(ownerElement)) {
449 HTMLIFrameElement* iframe = toHTMLIFrameElement(ownerElement); 449 HTMLIFrameElement* iframe = toHTMLIFrameElement(ownerElement);
450 if (HTMLIFrameElementPayments::from(*iframe).allowPaymentRequest(*iframe)) 450 if (HTMLIFrameElementPayments::from(*iframe).allowPaymentRequest(*iframe))
451 return allowedToUsePaymentRequest(frame->tree().parent()); 451 return allowedToUsePaymentRequest(frame->tree().parent());
452 } 452 }
453 453
454 // 4. Return false. 454 // 4. Return false.
455 return false; 455 return false;
456 } 456 }
457 457
458 WTF::Vector<mojom::blink::PaymentMethodDataPtr> ConvertPaymentMethodData( 458 WTF::Vector<payments::mojom::blink::PaymentMethodDataPtr>
459 ConvertPaymentMethodData(
459 const Vector<PaymentRequest::MethodData>& blinkMethods) { 460 const Vector<PaymentRequest::MethodData>& blinkMethods) {
460 WTF::Vector<mojom::blink::PaymentMethodDataPtr> mojoMethods( 461 WTF::Vector<payments::mojom::blink::PaymentMethodDataPtr> mojoMethods(
461 blinkMethods.size()); 462 blinkMethods.size());
462 for (size_t i = 0; i < blinkMethods.size(); ++i) { 463 for (size_t i = 0; i < blinkMethods.size(); ++i) {
463 mojoMethods[i] = mojom::blink::PaymentMethodData::New(); 464 mojoMethods[i] = payments::mojom::blink::PaymentMethodData::New();
464 mojoMethods[i]->supported_methods = 465 mojoMethods[i]->supported_methods =
465 WTF::Vector<WTF::String>(blinkMethods[i].supportedMethods); 466 WTF::Vector<WTF::String>(blinkMethods[i].supportedMethods);
466 mojoMethods[i]->stringified_data = blinkMethods[i].stringifiedData; 467 mojoMethods[i]->stringified_data = blinkMethods[i].stringifiedData;
467 } 468 }
468 return mojoMethods; 469 return mojoMethods;
469 } 470 }
470 471
471 } // namespace 472 } // namespace
472 473
473 PaymentRequest* PaymentRequest::create( 474 PaymentRequest* PaymentRequest::create(
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 556
556 // User has cancelled the transaction while the website was processing it. 557 // User has cancelled the transaction while the website was processing it.
557 if (!m_paymentProvider) 558 if (!m_paymentProvider)
558 return ScriptPromise::rejectWithDOMException( 559 return ScriptPromise::rejectWithDOMException(
559 scriptState, 560 scriptState,
560 DOMException::create(InvalidStateError, "Request cancelled")); 561 DOMException::create(InvalidStateError, "Request cancelled"));
561 562
562 m_completeTimer.stop(); 563 m_completeTimer.stop();
563 564
564 // The payment provider should respond in PaymentRequest::OnComplete(). 565 // The payment provider should respond in PaymentRequest::OnComplete().
565 m_paymentProvider->Complete(mojom::blink::PaymentComplete(result)); 566 m_paymentProvider->Complete(payments::mojom::blink::PaymentComplete(result));
566 567
567 m_completeResolver = ScriptPromiseResolver::create(scriptState); 568 m_completeResolver = ScriptPromiseResolver::create(scriptState);
568 return m_completeResolver->promise(); 569 return m_completeResolver->promise();
569 } 570 }
570 571
571 void PaymentRequest::onUpdatePaymentDetails( 572 void PaymentRequest::onUpdatePaymentDetails(
572 const ScriptValue& detailsScriptValue) { 573 const ScriptValue& detailsScriptValue) {
573 if (!m_showResolver || !m_paymentProvider) 574 if (!m_showResolver || !m_paymentProvider)
574 return; 575 return;
575 576
(...skipping 18 matching lines...) Expand all
594 } 595 }
595 596
596 if (m_options.requestShipping()) { 597 if (m_options.requestShipping()) {
597 if (keepShippingOptions) 598 if (keepShippingOptions)
598 m_shippingOption = getSelectedShippingOption(details); 599 m_shippingOption = getSelectedShippingOption(details);
599 else 600 else
600 m_shippingOption = String(); 601 m_shippingOption = String();
601 } 602 }
602 603
603 m_paymentProvider->UpdateWith(maybeKeepShippingOptions( 604 m_paymentProvider->UpdateWith(maybeKeepShippingOptions(
604 mojom::blink::PaymentDetails::From(details), keepShippingOptions)); 605 payments::mojom::blink::PaymentDetails::From(details),
606 keepShippingOptions));
605 } 607 }
606 608
607 void PaymentRequest::onUpdatePaymentDetailsFailure(const String& error) { 609 void PaymentRequest::onUpdatePaymentDetailsFailure(const String& error) {
608 if (m_showResolver) 610 if (m_showResolver)
609 m_showResolver->reject(DOMException::create(AbortError, error)); 611 m_showResolver->reject(DOMException::create(AbortError, error));
610 if (m_completeResolver) 612 if (m_completeResolver)
611 m_completeResolver->reject(DOMException::create(AbortError, error)); 613 m_completeResolver->reject(DOMException::create(AbortError, error));
612 clearResolversAndCloseMojoConnection(); 614 clearResolversAndCloseMojoConnection();
613 } 615 }
614 616
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 if (m_options.requestShipping()) { 669 if (m_options.requestShipping()) {
668 if (keepShippingOptions) 670 if (keepShippingOptions)
669 m_shippingOption = getSelectedShippingOption(details); 671 m_shippingOption = getSelectedShippingOption(details);
670 m_shippingType = getValidShippingType(m_options.shippingType()); 672 m_shippingType = getValidShippingType(m_options.shippingType());
671 } 673 }
672 674
673 scriptState->domWindow()->frame()->interfaceProvider()->getInterface( 675 scriptState->domWindow()->frame()->interfaceProvider()->getInterface(
674 mojo::GetProxy(&m_paymentProvider)); 676 mojo::GetProxy(&m_paymentProvider));
675 m_paymentProvider.set_connection_error_handler(convertToBaseCallback( 677 m_paymentProvider.set_connection_error_handler(convertToBaseCallback(
676 WTF::bind(&PaymentRequest::OnError, wrapWeakPersistent(this), 678 WTF::bind(&PaymentRequest::OnError, wrapWeakPersistent(this),
677 mojom::blink::PaymentErrorReason::UNKNOWN))); 679 payments::mojom::blink::PaymentErrorReason::UNKNOWN)));
678 m_paymentProvider->Init( 680 m_paymentProvider->Init(
679 m_clientBinding.CreateInterfacePtrAndBind(), 681 m_clientBinding.CreateInterfacePtrAndBind(),
680 ConvertPaymentMethodData(validatedMethodData), 682 ConvertPaymentMethodData(validatedMethodData),
681 maybeKeepShippingOptions( 683 maybeKeepShippingOptions(
682 mojom::blink::PaymentDetails::From(details), 684 payments::mojom::blink::PaymentDetails::From(details),
683 keepShippingOptions && m_options.requestShipping()), 685 keepShippingOptions && m_options.requestShipping()),
684 mojom::blink::PaymentOptions::From(m_options)); 686 payments::mojom::blink::PaymentOptions::From(m_options));
685 } 687 }
686 688
687 void PaymentRequest::contextDestroyed() { 689 void PaymentRequest::contextDestroyed() {
688 clearResolversAndCloseMojoConnection(); 690 clearResolversAndCloseMojoConnection();
689 } 691 }
690 692
691 void PaymentRequest::OnShippingAddressChange( 693 void PaymentRequest::OnShippingAddressChange(
692 mojom::blink::PaymentAddressPtr address) { 694 payments::mojom::blink::PaymentAddressPtr address) {
693 DCHECK(m_showResolver); 695 DCHECK(m_showResolver);
694 DCHECK(!m_completeResolver); 696 DCHECK(!m_completeResolver);
695 697
696 String errorMessage; 698 String errorMessage;
697 if (!PaymentsValidators::isValidShippingAddress(address, &errorMessage)) { 699 if (!PaymentsValidators::isValidShippingAddress(address, &errorMessage)) {
698 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); 700 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage));
699 clearResolversAndCloseMojoConnection(); 701 clearResolversAndCloseMojoConnection();
700 return; 702 return;
701 } 703 }
702 704
(...skipping 14 matching lines...) Expand all
717 PaymentRequestUpdateEvent* event = 719 PaymentRequestUpdateEvent* event =
718 PaymentRequestUpdateEvent::create(EventTypeNames::shippingoptionchange); 720 PaymentRequestUpdateEvent::create(EventTypeNames::shippingoptionchange);
719 event->setTarget(this); 721 event->setTarget(this);
720 event->setPaymentDetailsUpdater(this); 722 event->setPaymentDetailsUpdater(this);
721 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event); 723 bool success = getExecutionContext()->getEventQueue()->enqueueEvent(event);
722 DCHECK(success); 724 DCHECK(success);
723 ALLOW_UNUSED_LOCAL(success); 725 ALLOW_UNUSED_LOCAL(success);
724 } 726 }
725 727
726 void PaymentRequest::OnPaymentResponse( 728 void PaymentRequest::OnPaymentResponse(
727 mojom::blink::PaymentResponsePtr response) { 729 payments::mojom::blink::PaymentResponsePtr response) {
728 DCHECK(m_showResolver); 730 DCHECK(m_showResolver);
729 DCHECK(!m_completeResolver); 731 DCHECK(!m_completeResolver);
730 DCHECK(!m_completeTimer.isActive()); 732 DCHECK(!m_completeTimer.isActive());
731 733
732 if (m_options.requestShipping()) { 734 if (m_options.requestShipping()) {
733 if (!response->shipping_address || response->shipping_option.isEmpty()) { 735 if (!response->shipping_address || response->shipping_option.isEmpty()) {
734 m_showResolver->reject(DOMException::create(SyntaxError)); 736 m_showResolver->reject(DOMException::create(SyntaxError));
735 clearResolversAndCloseMojoConnection(); 737 clearResolversAndCloseMojoConnection();
736 return; 738 return;
737 } 739 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 if (!Platform::current()) { 781 if (!Platform::current()) {
780 // TODO(rockot): Clean this up once renderer shutdown sequence is fixed. 782 // TODO(rockot): Clean this up once renderer shutdown sequence is fixed.
781 return; 783 return;
782 } 784 }
783 785
784 bool isError = false; 786 bool isError = false;
785 ExceptionCode ec = UnknownError; 787 ExceptionCode ec = UnknownError;
786 String message; 788 String message;
787 789
788 switch (error) { 790 switch (error) {
789 case mojom::blink::PaymentErrorReason::USER_CANCEL: 791 case payments::mojom::blink::PaymentErrorReason::USER_CANCEL:
790 message = "Request cancelled"; 792 message = "Request cancelled";
791 break; 793 break;
792 case mojom::blink::PaymentErrorReason::NOT_SUPPORTED: 794 case payments::mojom::blink::PaymentErrorReason::NOT_SUPPORTED:
793 isError = true; 795 isError = true;
794 ec = NotSupportedError; 796 ec = NotSupportedError;
795 message = "The payment method is not supported"; 797 message = "The payment method is not supported";
796 break; 798 break;
797 case mojom::blink::PaymentErrorReason::UNKNOWN: 799 case payments::mojom::blink::PaymentErrorReason::UNKNOWN:
798 isError = true; 800 isError = true;
799 ec = UnknownError; 801 ec = UnknownError;
800 message = "Request failed"; 802 message = "Request failed";
801 break; 803 break;
802 } 804 }
803 805
804 DCHECK(!message.isEmpty()); 806 DCHECK(!message.isEmpty());
805 807
806 if (isError) { 808 if (isError) {
807 if (m_completeResolver) 809 if (m_completeResolver)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 m_abortResolver.clear(); 843 m_abortResolver.clear();
842 return; 844 return;
843 } 845 }
844 846
845 m_showResolver->reject(DOMException::create(AbortError)); 847 m_showResolver->reject(DOMException::create(AbortError));
846 m_abortResolver->resolve(); 848 m_abortResolver->resolve();
847 clearResolversAndCloseMojoConnection(); 849 clearResolversAndCloseMojoConnection();
848 } 850 }
849 851
850 void PaymentRequest::onCompleteTimeout(TimerBase*) { 852 void PaymentRequest::onCompleteTimeout(TimerBase*) {
851 m_paymentProvider->Complete(mojom::blink::PaymentComplete(Fail)); 853 m_paymentProvider->Complete(payments::mojom::blink::PaymentComplete(Fail));
852 clearResolversAndCloseMojoConnection(); 854 clearResolversAndCloseMojoConnection();
853 } 855 }
854 856
855 void PaymentRequest::clearResolversAndCloseMojoConnection() { 857 void PaymentRequest::clearResolversAndCloseMojoConnection() {
856 m_completeTimer.stop(); 858 m_completeTimer.stop();
857 m_completeResolver.clear(); 859 m_completeResolver.clear();
858 m_showResolver.clear(); 860 m_showResolver.clear();
859 m_abortResolver.clear(); 861 m_abortResolver.clear();
860 if (m_clientBinding.is_bound()) 862 if (m_clientBinding.is_bound())
861 m_clientBinding.Close(); 863 m_clientBinding.Close();
862 m_paymentProvider.reset(); 864 m_paymentProvider.reset();
863 } 865 }
864 866
865 } // namespace blink 867 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698