1/*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "bindings/v8/ScriptPromiseResolver.h"
33
34#include "RuntimeEnabledFeatures.h"
35#include "bindings/v8/ScriptState.h"
36#include "bindings/v8/ScriptValue.h"
37#include "bindings/v8/V8Binding.h"
38#include "bindings/v8/V8DOMWrapper.h"
39#include "bindings/v8/custom/V8PromiseCustom.h"
40
41#include <v8.h>
42
43namespace WebCore {
44
45ScriptPromiseResolver::ScriptPromiseResolver(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
46    : m_isolate(isolate)
47{
48    ASSERT(RuntimeEnabledFeatures::promiseEnabled());
49    v8::Local<v8::Object> promise, resolver;
50    V8PromiseCustom::createPromise(creationContext, &promise, &resolver, isolate);
51    m_promise.set(isolate, promise);
52    m_resolver.set(isolate, resolver);
53}
54
55ScriptPromiseResolver::~ScriptPromiseResolver()
56{
57    // We don't call "detach" here because it requires a caller
58    // to be in a v8 context.
59
60    detachPromise();
61    m_resolver.clear();
62}
63
64PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptExecutionContext* context)
65{
66    ASSERT(v8::Context::InContext());
67    ASSERT(context);
68    v8::Isolate* isolate = v8::Isolate::GetCurrent();
69    return adoptRef(new ScriptPromiseResolver(toV8Context(context, DOMWrapperWorld::current())->Global(), isolate));
70}
71
72PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create()
73{
74    ASSERT(v8::Context::InContext());
75    v8::Isolate* isolate = v8::Isolate::GetCurrent();
76    return adoptRef(new ScriptPromiseResolver(v8::Object::New(), isolate));
77}
78
79bool ScriptPromiseResolver::isPending() const
80{
81    ASSERT(v8::Context::InContext());
82    return isPendingInternal();
83}
84
85void ScriptPromiseResolver::detach()
86{
87    ASSERT(v8::Context::InContext());
88    detachPromise();
89    reject(v8::Undefined(m_isolate));
90    m_resolver.clear();
91}
92
93void ScriptPromiseResolver::fulfill(v8::Handle<v8::Value> value)
94{
95    ASSERT(v8::Context::InContext());
96    if (!isPendingInternal())
97        return;
98    V8PromiseCustom::fulfillResolver(m_resolver.newLocal(m_isolate), value, V8PromiseCustom::Asynchronous, m_isolate);
99    m_resolver.clear();
100}
101
102void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value)
103{
104    ASSERT(v8::Context::InContext());
105    if (!isPendingInternal())
106        return;
107    V8PromiseCustom::resolveResolver(m_resolver.newLocal(m_isolate), value, V8PromiseCustom::Asynchronous, m_isolate);
108    m_resolver.clear();
109}
110
111void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value)
112{
113    ASSERT(v8::Context::InContext());
114    if (!isPendingInternal())
115        return;
116    V8PromiseCustom::rejectResolver(m_resolver.newLocal(m_isolate), value, V8PromiseCustom::Asynchronous, m_isolate);
117    m_resolver.clear();
118}
119
120void ScriptPromiseResolver::fulfill(ScriptValue value)
121{
122    ASSERT(v8::Context::InContext());
123    fulfill(value.v8Value());
124}
125
126void ScriptPromiseResolver::resolve(ScriptValue value)
127{
128    ASSERT(v8::Context::InContext());
129    resolve(value.v8Value());
130}
131
132void ScriptPromiseResolver::reject(ScriptValue value)
133{
134    ASSERT(v8::Context::InContext());
135    reject(value.v8Value());
136}
137
138bool ScriptPromiseResolver::isPendingInternal() const
139{
140    ASSERT(v8::Context::InContext());
141    if (m_resolver.isEmpty())
142        return false;
143    v8::Local<v8::Object> resolver = m_resolver.newLocal(m_isolate);
144    if (V8PromiseCustom::isInternalDetached(resolver))
145        return false;
146    v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(resolver);
147    V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal);
148    return state == V8PromiseCustom::Pending;
149}
150
151} // namespace WebCore
152