| 1 | /* |
| 2 | * Copyright (C) 2017-2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(PAYMENT_REQUEST) |
| 29 | |
| 30 | #include "ActiveDOMObject.h" |
| 31 | #include "EventTarget.h" |
| 32 | #include "ExceptionOr.h" |
| 33 | #include "JSDOMPromiseDeferred.h" |
| 34 | #include "PaymentDetailsInit.h" |
| 35 | #include "PaymentMethodChangeEvent.h" |
| 36 | #include "PaymentOptions.h" |
| 37 | #include "PaymentResponse.h" |
| 38 | #include <wtf/URL.h> |
| 39 | #include <wtf/Variant.h> |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | class Document; |
| 44 | class Event; |
| 45 | class PaymentAddress; |
| 46 | class PaymentHandler; |
| 47 | class PaymentResponse; |
| 48 | enum class PaymentComplete; |
| 49 | enum class PaymentShippingType; |
| 50 | struct PaymentDetailsUpdate; |
| 51 | struct PaymentMethodData; |
| 52 | |
| 53 | class PaymentRequest final : public ActiveDOMObject, public CanMakeWeakPtr<PaymentRequest>, public EventTargetWithInlineData, public RefCounted<PaymentRequest> { |
| 54 | WTF_MAKE_ISO_ALLOCATED(PaymentRequest); |
| 55 | public: |
| 56 | using AbortPromise = DOMPromiseDeferred<void>; |
| 57 | using CanMakePaymentPromise = DOMPromiseDeferred<IDLBoolean>; |
| 58 | using ShowPromise = DOMPromiseDeferred<IDLInterface<PaymentResponse>>; |
| 59 | |
| 60 | static ExceptionOr<Ref<PaymentRequest>> create(Document&, Vector<PaymentMethodData>&&, PaymentDetailsInit&&, PaymentOptions&&); |
| 61 | static bool enabledForContext(ScriptExecutionContext&); |
| 62 | ~PaymentRequest(); |
| 63 | |
| 64 | void show(Document&, RefPtr<DOMPromise>&& detailsPromise, ShowPromise&&); |
| 65 | void abort(AbortPromise&&); |
| 66 | void canMakePayment(Document&, CanMakePaymentPromise&&); |
| 67 | |
| 68 | const String& id() const; |
| 69 | PaymentAddress* shippingAddress() const { return m_shippingAddress.get(); } |
| 70 | const String& shippingOption() const { return m_shippingOption; } |
| 71 | Optional<PaymentShippingType> shippingType() const; |
| 72 | |
| 73 | enum class State { |
| 74 | Created, |
| 75 | Interactive, |
| 76 | Closed, |
| 77 | }; |
| 78 | |
| 79 | enum class UpdateReason { |
| 80 | ShowDetailsResolved, |
| 81 | ShippingAddressChanged, |
| 82 | ShippingOptionChanged, |
| 83 | PaymentMethodChanged, |
| 84 | }; |
| 85 | |
| 86 | State state() const { return m_state; } |
| 87 | |
| 88 | const PaymentOptions& paymentOptions() const { return m_options; } |
| 89 | const PaymentDetailsInit& paymentDetails() const { return m_details; } |
| 90 | const Vector<String>& serializedModifierData() const { return m_serializedModifierData; } |
| 91 | |
| 92 | void shippingAddressChanged(Ref<PaymentAddress>&&); |
| 93 | void shippingOptionChanged(const String& shippingOption); |
| 94 | void paymentMethodChanged(const String& methodName, PaymentMethodChangeEvent::MethodDetailsFunction&&); |
| 95 | ExceptionOr<void> updateWith(UpdateReason, Ref<DOMPromise>&&); |
| 96 | ExceptionOr<void> completeMerchantValidation(Event&, Ref<DOMPromise>&&); |
| 97 | void accept(const String& methodName, PaymentResponse::DetailsFunction&&, Ref<PaymentAddress>&& shippingAddress, const String& payerName, const String& payerEmail, const String& payerPhone); |
| 98 | ExceptionOr<void> complete(Optional<PaymentComplete>&&); |
| 99 | ExceptionOr<void> retry(PaymentValidationErrors&&); |
| 100 | void cancel(); |
| 101 | |
| 102 | using MethodIdentifier = Variant<String, URL>; |
| 103 | using RefCounted<PaymentRequest>::ref; |
| 104 | using RefCounted<PaymentRequest>::deref; |
| 105 | |
| 106 | private: |
| 107 | struct Method { |
| 108 | MethodIdentifier identifier; |
| 109 | String serializedData; |
| 110 | }; |
| 111 | |
| 112 | struct PaymentHandlerWithPendingActivity { |
| 113 | Ref<PaymentHandler> paymentHandler; |
| 114 | Ref<PendingActivity<PaymentRequest>> pendingActivity; |
| 115 | }; |
| 116 | |
| 117 | PaymentRequest(Document&, PaymentOptions&&, PaymentDetailsInit&&, Vector<String>&& serializedModifierData, Vector<Method>&& serializedMethodData, String&& selectedShippingOption); |
| 118 | |
| 119 | void settleDetailsPromise(UpdateReason); |
| 120 | void whenDetailsSettled(std::function<void()>&&); |
| 121 | void abortWithException(Exception&&); |
| 122 | PaymentHandler* activePaymentHandler() { return m_activePaymentHandler ? m_activePaymentHandler->paymentHandler.ptr() : nullptr; } |
| 123 | void settleShowPromise(ExceptionOr<PaymentResponse&>&&); |
| 124 | void closeActivePaymentHandler(); |
| 125 | |
| 126 | // ActiveDOMObject |
| 127 | const char* activeDOMObjectName() const final { return "PaymentRequest" ; } |
| 128 | bool canSuspendForDocumentSuspension() const final; |
| 129 | void stop() final; |
| 130 | |
| 131 | // EventTarget |
| 132 | EventTargetInterface eventTargetInterface() const final { return PaymentRequestEventTargetInterfaceType; } |
| 133 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
| 134 | bool isPaymentRequest() const final { return true; } |
| 135 | void refEventTarget() final { ref(); } |
| 136 | void derefEventTarget() final { deref(); } |
| 137 | |
| 138 | PaymentOptions m_options; |
| 139 | PaymentDetailsInit m_details; |
| 140 | Vector<String> m_serializedModifierData; |
| 141 | Vector<Method> m_serializedMethodData; |
| 142 | String m_shippingOption; |
| 143 | RefPtr<PaymentAddress> m_shippingAddress; |
| 144 | State m_state { State::Created }; |
| 145 | Optional<ShowPromise> m_showPromise; |
| 146 | Optional<PaymentHandlerWithPendingActivity> m_activePaymentHandler; |
| 147 | RefPtr<DOMPromise> m_detailsPromise; |
| 148 | RefPtr<DOMPromise> m_merchantSessionPromise; |
| 149 | RefPtr<PaymentResponse> m_response; |
| 150 | bool m_isUpdating { false }; |
| 151 | bool m_isCancelPending { false }; |
| 152 | }; |
| 153 | |
| 154 | Optional<PaymentRequest::MethodIdentifier> convertAndValidatePaymentMethodIdentifier(const String& identifier); |
| 155 | |
| 156 | } // namespace WebCore |
| 157 | |
| 158 | SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::PaymentRequest) |
| 159 | static bool isType(const WebCore::EventTarget& eventTarget) { return eventTarget.isPaymentRequest(); } |
| 160 | SPECIALIZE_TYPE_TRAITS_END() |
| 161 | |
| 162 | #endif // ENABLE(PAYMENT_REQUEST) |
| 163 | |