1/*
2 * Copyright (C) 2013 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#ifndef JSDOMPromise_h
27#define JSDOMPromise_h
28
29#if ENABLE(PROMISES)
30
31#include "JSCryptoKey.h"
32#include "JSCryptoKeyPair.h"
33#include "JSDOMBinding.h"
34#include <heap/StrongInlines.h>
35#include <runtime/JSPromiseDeferred.h>
36
37namespace WebCore {
38
39class DeferredWrapper {
40public:
41    DeferredWrapper(JSC::ExecState*, JSDOMGlobalObject*);
42
43    template<class ResolveResultType>
44    void resolve(const ResolveResultType&);
45
46    template<class RejectResultType>
47    void reject(const RejectResultType&);
48
49    JSC::JSObject* promise() const;
50
51private:
52    void resolve(JSC::ExecState*, JSC::JSValue);
53    void reject(JSC::ExecState*, JSC::JSValue);
54
55    JSC::Strong<JSDOMGlobalObject> m_globalObject;
56    JSC::Strong<JSC::JSPromiseDeferred> m_deferred;
57};
58
59template<class ResolveResultType>
60inline void DeferredWrapper::resolve(const ResolveResultType& result)
61{
62    JSC::ExecState* exec = m_globalObject->globalExec();
63    JSC::JSLockHolder locker(exec);
64    resolve(exec, toJS(exec, m_globalObject.get(), result));
65}
66
67template<class RejectResultType>
68inline void DeferredWrapper::reject(const RejectResultType& result)
69{
70    JSC::ExecState* exec = m_globalObject->globalExec();
71    JSC::JSLockHolder locker(exec);
72    reject(exec, toJS(exec, m_globalObject.get(), result));
73}
74
75template<>
76inline void DeferredWrapper::reject(const std::nullptr_t&)
77{
78    JSC::ExecState* exec = m_globalObject->globalExec();
79    JSC::JSLockHolder locker(exec);
80    reject(exec, JSC::jsNull());
81}
82
83template<>
84inline void DeferredWrapper::resolve<String>(const String& result)
85{
86    JSC::ExecState* exec = m_globalObject->globalExec();
87    JSC::JSLockHolder locker(exec);
88    resolve(exec, jsString(exec, result));
89}
90
91template<>
92inline void DeferredWrapper::resolve<bool>(const bool& result)
93{
94    JSC::ExecState* exec = m_globalObject->globalExec();
95    JSC::JSLockHolder locker(exec);
96    resolve(exec, JSC::jsBoolean(result));
97}
98
99template<>
100inline void DeferredWrapper::resolve<Vector<unsigned char>>(const Vector<unsigned char>& result)
101{
102    JSC::ExecState* exec = m_globalObject->globalExec();
103    JSC::JSLockHolder locker(exec);
104    RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(result.data(), result.size());
105    resolve(exec, toJS(exec, m_globalObject.get(), buffer.get()));
106}
107
108template<>
109inline void DeferredWrapper::reject<String>(const String& result)
110{
111    JSC::ExecState* exec = m_globalObject->globalExec();
112    JSC::JSLockHolder locker(exec);
113    reject(exec, jsString(exec, result));
114}
115
116}
117
118#endif // ENABLE(PROMISES)
119
120#endif // JSDOMPromise_h
121