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