2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "bindings/v8/ScriptPromiseResolver.h"
34 #include "RuntimeEnabledFeatures.h"
35 #include "bindings/v8/ScriptValue.h"
36 #include "bindings/v8/V8Binding.h"
37 #include "bindings/v8/V8DOMWrapper.h"
38 #include "bindings/v8/custom/V8PromiseCustom.h"
44 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState)
45 : m_scriptState(scriptState)
47 v8::Isolate* isolate = m_scriptState->isolate();
48 ASSERT(!m_scriptState->contextIsEmpty());
49 ASSERT(isolate->InContext());
50 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
51 m_resolver = ScriptValue(scriptState, v8::Promise::Resolver::New(isolate));
53 m_promise = ScriptPromise(scriptState, V8PromiseCustom::createPromise(m_scriptState->context()->Global(), isolate));
57 ScriptPromiseResolver::~ScriptPromiseResolver()
59 // We don't call "reject" here because it requires a caller
60 // to be in a v8 context.
66 ScriptPromise ScriptPromiseResolver::promise()
68 ASSERT(!m_scriptState->contextIsEmpty());
69 ASSERT(m_scriptState->isolate()->InContext());
70 if (!m_resolver.isEmpty()) {
71 v8::Local<v8::Promise::Resolver> v8Resolver = m_resolver.v8Value().As<v8::Promise::Resolver>();
72 return ScriptPromise(m_scriptState.get(), v8Resolver->GetPromise());
77 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptState* scriptState)
79 ASSERT(scriptState->isolate()->InContext());
80 return adoptRef(new ScriptPromiseResolver(scriptState));
83 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value)
85 ASSERT(m_scriptState->isolate()->InContext());
86 if (!m_resolver.isEmpty()) {
87 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value);
88 } else if (!m_promise.isEmpty()) {
89 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
90 ASSERT(V8PromiseCustom::isPromise(promise, m_scriptState->isolate()));
91 V8PromiseCustom::resolve(promise, value, m_scriptState->isolate());
97 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value)
99 ASSERT(m_scriptState->isolate()->InContext());
100 if (!m_resolver.isEmpty()) {
101 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value);
102 } else if (!m_promise.isEmpty()) {
103 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
104 ASSERT(V8PromiseCustom::isPromise(promise, m_scriptState->isolate()));
105 V8PromiseCustom::reject(promise, value, m_scriptState->isolate());
111 } // namespace WebCore