Clean up some includes to make the build a bit faster: DOMPromise
[WebKit-https.git] / Source / WebCore / Modules / paymentrequest / PaymentRequest.h
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 "IDLTypes.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 template<typename IDLType> class DOMPromiseDeferred;
53
54 class PaymentRequest final : public ActiveDOMObject, public CanMakeWeakPtr<PaymentRequest>, public EventTargetWithInlineData, public RefCounted<PaymentRequest> {
55     WTF_MAKE_ISO_ALLOCATED(PaymentRequest);
56 public:
57     using AbortPromise = DOMPromiseDeferred<void>;
58     using CanMakePaymentPromise = DOMPromiseDeferred<IDLBoolean>;
59     using ShowPromise = DOMPromiseDeferred<IDLInterface<PaymentResponse>>;
60
61     static ExceptionOr<Ref<PaymentRequest>> create(Document&, Vector<PaymentMethodData>&&, PaymentDetailsInit&&, PaymentOptions&&);
62     static bool enabledForContext(ScriptExecutionContext&);
63     ~PaymentRequest();
64
65     void show(Document&, RefPtr<DOMPromise>&& detailsPromise, ShowPromise&&);
66     void abort(AbortPromise&&);
67     void canMakePayment(Document&, CanMakePaymentPromise&&);
68
69     const String& id() const;
70     PaymentAddress* shippingAddress() const { return m_shippingAddress.get(); }
71     const String& shippingOption() const { return m_shippingOption; }
72     Optional<PaymentShippingType> shippingType() const;
73
74     enum class State {
75         Created,
76         Interactive,
77         Closed,
78     };
79
80     enum class UpdateReason {
81         ShowDetailsResolved,
82         ShippingAddressChanged,
83         ShippingOptionChanged,
84         PaymentMethodChanged,
85     };
86
87     State state() const { return m_state; }
88
89     const PaymentOptions& paymentOptions() const { return m_options; }
90     const PaymentDetailsInit& paymentDetails() const { return m_details; }
91     const Vector<String>& serializedModifierData() const { return m_serializedModifierData; }
92
93     void shippingAddressChanged(Ref<PaymentAddress>&&);
94     void shippingOptionChanged(const String& shippingOption);
95     void paymentMethodChanged(const String& methodName, PaymentMethodChangeEvent::MethodDetailsFunction&&);
96     ExceptionOr<void> updateWith(UpdateReason, Ref<DOMPromise>&&);
97     ExceptionOr<void> completeMerchantValidation(Event&, Ref<DOMPromise>&&);
98     void accept(const String& methodName, PaymentResponse::DetailsFunction&&, Ref<PaymentAddress>&& shippingAddress, const String& payerName, const String& payerEmail, const String& payerPhone);
99     ExceptionOr<void> complete(Optional<PaymentComplete>&&);
100     ExceptionOr<void> retry(PaymentValidationErrors&&);
101     void cancel();
102
103     using MethodIdentifier = Variant<String, URL>;
104     using RefCounted<PaymentRequest>::ref;
105     using RefCounted<PaymentRequest>::deref;
106
107 private:
108     struct Method {
109         MethodIdentifier identifier;
110         String serializedData;
111     };
112
113     struct PaymentHandlerWithPendingActivity {
114         Ref<PaymentHandler> paymentHandler;
115         Ref<PendingActivity<PaymentRequest>> pendingActivity;
116     };
117
118     PaymentRequest(Document&, PaymentOptions&&, PaymentDetailsInit&&, Vector<String>&& serializedModifierData, Vector<Method>&& serializedMethodData, String&& selectedShippingOption);
119
120     void settleDetailsPromise(UpdateReason);
121     void whenDetailsSettled(std::function<void()>&&);
122     void abortWithException(Exception&&);
123     PaymentHandler* activePaymentHandler() { return m_activePaymentHandler ? m_activePaymentHandler->paymentHandler.ptr() : nullptr; }
124     void settleShowPromise(ExceptionOr<PaymentResponse&>&&);
125     void closeActivePaymentHandler();
126
127     // ActiveDOMObject
128     const char* activeDOMObjectName() const final { return "PaymentRequest"; }
129     bool canSuspendForDocumentSuspension() const final;
130     void stop() final;
131
132     // EventTarget
133     EventTargetInterface eventTargetInterface() const final { return PaymentRequestEventTargetInterfaceType; }
134     ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
135     bool isPaymentRequest() const final { return true; }
136     void refEventTarget() final { ref(); }
137     void derefEventTarget() final { deref(); }
138
139     PaymentOptions m_options;
140     PaymentDetailsInit m_details;
141     Vector<String> m_serializedModifierData;
142     Vector<Method> m_serializedMethodData;
143     String m_shippingOption;
144     RefPtr<PaymentAddress> m_shippingAddress;
145     State m_state { State::Created };
146     std::unique_ptr<ShowPromise> m_showPromise;
147     Optional<PaymentHandlerWithPendingActivity> m_activePaymentHandler;
148     RefPtr<DOMPromise> m_detailsPromise;
149     RefPtr<DOMPromise> m_merchantSessionPromise;
150     RefPtr<PaymentResponse> m_response;
151     bool m_isUpdating { false };
152     bool m_isCancelPending { false };
153 };
154
155 Optional<PaymentRequest::MethodIdentifier> convertAndValidatePaymentMethodIdentifier(const String& identifier);
156
157 } // namespace WebCore
158
159 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::PaymentRequest)
160     static bool isType(const WebCore::EventTarget& eventTarget) { return eventTarget.isPaymentRequest(); }
161 SPECIALIZE_TYPE_TRAITS_END()
162
163 #endif // ENABLE(PAYMENT_REQUEST)