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 "ContextDestructionObserver.h"
32#include "EventTarget.h"
33#include "JSDOMPromiseDeferred.h"
34#include "JSValueInWrappedObject.h"
35#include "PaymentAddress.h"
36#include "PaymentComplete.h"
37#include <wtf/WeakPtr.h>
38
39namespace WebCore {
40
41class Document;
42class PaymentRequest;
43struct PaymentValidationErrors;
44
45class PaymentResponse final : public ActiveDOMObject, public EventTargetWithInlineData, public RefCounted<PaymentResponse> {
46 WTF_MAKE_ISO_ALLOCATED(PaymentResponse);
47public:
48 using DetailsFunction = Function<JSC::Strong<JSC::JSObject>(JSC::ExecState&)>;
49
50 static Ref<PaymentResponse> create(ScriptExecutionContext* context, PaymentRequest& request)
51 {
52 auto response = adoptRef(*new PaymentResponse(context, request));
53 response->finishConstruction();
54 return response;
55 }
56
57 ~PaymentResponse();
58
59 const String& requestId() const { return m_requestId; }
60 void setRequestId(const String& requestId) { m_requestId = requestId; }
61
62 const String& methodName() const { return m_methodName; }
63 void setMethodName(const String& methodName) { m_methodName = methodName; }
64
65 const DetailsFunction& detailsFunction() const { return m_detailsFunction; }
66 void setDetailsFunction(DetailsFunction&&);
67
68 JSValueInWrappedObject& cachedDetails() { return m_cachedDetails; }
69
70 PaymentAddress* shippingAddress() const { return m_shippingAddress.get(); }
71 void setShippingAddress(PaymentAddress* shippingAddress) { m_shippingAddress = shippingAddress; }
72
73 const String& shippingOption() const { return m_shippingOption; }
74 void setShippingOption(const String& shippingOption) { m_shippingOption = shippingOption; }
75
76 const String& payerName() const { return m_payerName; }
77 void setPayerName(const String& payerName) { m_payerName = payerName; }
78
79 const String& payerEmail() const { return m_payerEmail; }
80 void setPayerEmail(const String& payerEmail) { m_payerEmail = payerEmail; }
81
82 const String& payerPhone() const { return m_payerPhone; }
83 void setPayerPhone(const String& payerPhone) { m_payerPhone = payerPhone; }
84
85 void complete(Optional<PaymentComplete>&&, DOMPromiseDeferred<void>&&);
86 void retry(PaymentValidationErrors&&, DOMPromiseDeferred<void>&&);
87 void abortWithException(Exception&&);
88 bool hasRetryPromise() const { return !!m_retryPromise; }
89 void settleRetryPromise(ExceptionOr<void>&& = { });
90
91 using RefCounted<PaymentResponse>::ref;
92 using RefCounted<PaymentResponse>::deref;
93
94private:
95 PaymentResponse(ScriptExecutionContext*, PaymentRequest&);
96 void finishConstruction();
97
98 // ActiveDOMObject
99 const char* activeDOMObjectName() const final { return "PaymentResponse"; }
100 bool canSuspendForDocumentSuspension() const final;
101 void stop() final;
102
103 // EventTarget
104 EventTargetInterface eventTargetInterface() const final { return PaymentResponseEventTargetInterfaceType; }
105 ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
106 void refEventTarget() final { ref(); }
107 void derefEventTarget() final { deref(); }
108
109 enum class State {
110 Created,
111 Completed,
112 Stopped,
113 };
114
115 WeakPtr<PaymentRequest> m_request;
116 String m_requestId;
117 String m_methodName;
118 DetailsFunction m_detailsFunction;
119 JSValueInWrappedObject m_cachedDetails;
120 RefPtr<PaymentAddress> m_shippingAddress;
121 String m_shippingOption;
122 String m_payerName;
123 String m_payerEmail;
124 String m_payerPhone;
125 State m_state { State::Created };
126 Optional<DOMPromiseDeferred<void>> m_retryPromise;
127 RefPtr<PendingActivity<PaymentResponse>> m_pendingActivity;
128};
129
130} // namespace WebCore
131
132#endif // ENABLE(PAYMENT_REQUEST)
133