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/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"
45 ScriptPromiseResolver::ScriptPromiseResolver(ExecutionContext* context)
46 : m_isolate(toIsolate(context))
49 v8::Isolate* isolate = toIsolate(context);
50 ASSERT(isolate->InContext());
51 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
52 m_resolver = ScriptValue(v8::Promise::Resolver::New(isolate), isolate);
54 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current(isolate));
55 v8::Handle<v8::Object> creationContext = v8Context.IsEmpty() ? v8::Object::New(isolate) : v8Context->Global();
56 m_promise = ScriptPromise(V8PromiseCustom::createPromise(creationContext, isolate), isolate);
60 ScriptPromiseResolver::ScriptPromiseResolver(v8::Isolate* isolate)
63 ASSERT(isolate->InContext());
64 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
65 m_resolver = ScriptValue(v8::Promise::Resolver::New(isolate), isolate);
67 m_promise = ScriptPromise(V8PromiseCustom::createPromise(v8::Object::New(isolate), isolate), isolate);
71 ScriptPromiseResolver::~ScriptPromiseResolver()
73 // We don't call "reject" here because it requires a caller
74 // to be in a v8 context.
80 ScriptPromise ScriptPromiseResolver::promise()
82 ASSERT(m_isolate->InContext());
83 if (!m_resolver.hasNoValue()) {
84 v8::Local<v8::Promise::Resolver> v8Resolver = m_resolver.v8Value().As<v8::Promise::Resolver>();
85 return ScriptPromise(v8Resolver->GetPromise(), m_isolate);
90 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ExecutionContext* context)
93 ASSERT(toIsolate(context)->InContext());
94 return adoptRef(new ScriptPromiseResolver(context));
97 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(v8::Isolate* isolate)
99 ASSERT(isolate->InContext());
100 return adoptRef(new ScriptPromiseResolver(isolate));
103 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value)
105 ASSERT(m_isolate->InContext());
106 if (!m_resolver.hasNoValue()) {
107 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value);
108 } else if (!m_promise.hasNoValue()) {
109 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
110 ASSERT(V8PromiseCustom::isPromise(promise, m_isolate));
111 V8PromiseCustom::resolve(promise, value, m_isolate);
117 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value)
119 ASSERT(m_isolate->InContext());
120 if (!m_resolver.hasNoValue()) {
121 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value);
122 } else if (!m_promise.hasNoValue()) {
123 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
124 ASSERT(V8PromiseCustom::isPromise(promise, m_isolate));
125 V8PromiseCustom::reject(promise, value, m_isolate);
131 void ScriptPromiseResolver::resolve(ScriptValue value)
133 ASSERT(m_isolate->InContext());
134 resolve(value.v8Value());
137 void ScriptPromiseResolver::reject(ScriptValue value)
139 ASSERT(m_isolate->InContext());
140 reject(value.v8Value());
143 } // namespace WebCore