Index: Source/bindings/core/v8/ScriptPromisePropertyBase.cpp |
diff --git a/Source/bindings/core/v8/ScriptPromisePropertyBase.cpp b/Source/bindings/core/v8/ScriptPromisePropertyBase.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7d62d9dfd10dc560cf6145faff9d69e188f04da3 |
--- /dev/null |
+++ b/Source/bindings/core/v8/ScriptPromisePropertyBase.cpp |
@@ -0,0 +1,143 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "bindings/core/v8/ScriptPromisePropertyBase.h" |
+ |
+#include "bindings/core/v8/V8Binding.h" |
+#include "bindings/core/v8/V8HiddenValue.h" |
+#include "core/dom/ExecutionContext.h" |
+#include "wtf/text/StringBuilder.h" |
+ |
+namespace WebCore { |
+ |
+static String makeName(const char* name, const char* suffix) |
+{ |
+ StringBuilder builder; |
+ builder.append(name); |
+ builder.append(suffix); |
+ return builder.toString(); |
+} |
+ |
+ScriptPromisePropertyBase::ScriptPromisePropertyBase(ExecutionContext* executionContext, const char* name) |
+ : ContextLifecycleObserver(executionContext) |
+ , m_isolate(toIsolate(executionContext)) |
+ , m_state(Pending) |
+{ |
+ v8::HandleScope handleScope(m_isolate); |
haraken
2014/07/02 09:04:31
The HandleScope is not needed.
|
+ m_resolverName = makeName(name, "/Resolver"); |
+ m_promiseName = makeName(name, "/Promise"); |
haraken
2014/07/02 09:04:32
It's a bit fragile that there is no mechanism to p
|
+} |
+ |
+ScriptPromisePropertyBase::~ScriptPromisePropertyBase() |
+{ |
+ v8::HandleScope handleScope(m_isolate); |
+ v8::Handle<v8::Object> wrapper = m_mainWorldWrapper.newLocal(m_isolate); |
+ if (!wrapper.IsEmpty()) { |
+ v8::Handle<v8::String> resolverName = v8String(m_isolate, m_resolverName); |
+ wrapper->DeleteHiddenValue(resolverName); |
+ v8::Handle<v8::String> promiseName = v8String(m_isolate, m_promiseName); |
+ wrapper->DeleteHiddenValue(promiseName); |
+ } |
+} |
+ |
+static void clearHandle(const v8::WeakCallbackData<v8::Object, ScopedPersistent<v8::Object> >& data) |
+{ |
+ data.GetParameter()->clear(); |
+} |
+ |
+ScriptPromise ScriptPromisePropertyBase::promise(DOMWrapperWorld& world) |
+{ |
+ if (!executionContext()) |
+ return ScriptPromise(); |
+ |
+ if (!world.isMainWorld()) { |
+ // FIXME: Support isolated worlds. |
+ return ScriptPromise(); |
+ } |
+ |
+ v8::HandleScope handleScope(m_isolate); |
+ v8::Handle<v8::Context> context = toV8Context(executionContext(), world); |
+ if (context.IsEmpty()) |
+ return ScriptPromise(); |
+ ScriptState* scriptState = ScriptState::from(context); |
+ ScriptState::Scope scope(scriptState); |
+ |
+ v8::Handle<v8::String> resolverName = v8String(m_isolate, m_resolverName); |
+ v8::Handle<v8::String> promiseName = v8String(m_isolate, m_promiseName); |
+ |
+ v8::Handle<v8::Object> wrapper = m_mainWorldWrapper.newLocal(m_isolate); |
+ if (wrapper.IsEmpty()) { |
+ wrapper = holder(context->Global(), m_isolate); |
+ ASSERT(!wrapper.IsEmpty()); |
+ ASSERT(V8HiddenValue::getHiddenValue(m_isolate, wrapper, resolverName).IsEmpty()); |
+ ASSERT(V8HiddenValue::getHiddenValue(m_isolate, wrapper, promiseName).IsEmpty()); |
+ m_mainWorldWrapper.set(m_isolate, wrapper); |
+ m_mainWorldWrapper.setWeak(&m_mainWorldWrapper, &clearHandle); |
+ } |
+ ASSERT(wrapper->CreationContext() == context); |
+ |
+ v8::Handle<v8::Promise> promise = V8HiddenValue::getHiddenValue(m_isolate, wrapper, promiseName).As<v8::Promise>(); |
+ if (!promise.IsEmpty()) { |
+ // Return cached Promise |
+ return ScriptPromise(scriptState, promise); |
+ } |
+ |
+ // Create and cache the Promise |
+ v8::Handle<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(m_isolate); |
+ promise = resolver->GetPromise(); |
+ V8HiddenValue::setHiddenValue(m_isolate, wrapper, promiseName, promise); |
+ V8HiddenValue::setHiddenValue(m_isolate, promise, promiseName, wrapper); |
+ |
+ switch (m_state) { |
+ case Pending: |
+ // Cache the resolver too |
+ V8HiddenValue::setHiddenValue(m_isolate, wrapper, resolverName, resolver); |
+ break; |
+ case Resolved: |
+ case Rejected: |
+ resolveOrReject(resolver); |
+ break; |
+ } |
+ |
+ return ScriptPromise(scriptState, promise); |
+} |
+ |
+void ScriptPromisePropertyBase::settle(State targetState) |
+{ |
+ ASSERT(executionContext()); |
+ ASSERT(m_state == Pending); |
+ ASSERT(targetState == Resolved || targetState == Rejected); |
+ |
+ m_state = targetState; |
+ |
+ v8::HandleScope handleScope(m_isolate); |
+ v8::Handle<v8::Object> wrapper = m_mainWorldWrapper.newLocal(m_isolate); |
+ if (wrapper.IsEmpty()) |
+ return; // wrapper has died or was never populated |
haraken
2014/07/02 09:04:32
Is this a valid state? In order to provide determi
dominicc (has gone to gerrit)
2014/07/03 02:13:39
We don't want to prevent garbage collection! So if
|
+ ScriptState::Scope scope(ScriptState::from(wrapper->CreationContext())); |
+ |
+ v8::Handle<v8::String> resolverName = v8String(m_isolate, m_resolverName); |
+ v8::Handle<v8::Promise::Resolver> resolver = V8HiddenValue::getHiddenValue(m_isolate, wrapper, resolverName).As<v8::Promise::Resolver>(); |
+ |
+ V8HiddenValue::deleteHiddenValue(m_isolate, wrapper, resolverName); |
haraken
2014/07/02 09:04:31
Can we delete the hidden value for the Promise as
dominicc (has gone to gerrit)
2014/07/03 02:13:39
No. We need to keep the reference to the Promise b
|
+ resolveOrReject(resolver); |
+} |
+ |
+void ScriptPromisePropertyBase::resolveOrReject(v8::Handle<v8::Promise::Resolver> resolver) |
haraken
2014/07/02 09:04:32
Shall we rename settle to resolveOrReject, and res
|
+{ |
+ switch (m_state) { |
+ case Pending: |
+ ASSERT_NOT_REACHED(); |
+ break; |
+ case Resolved: |
+ resolver->Resolve(resolvedValue(resolver->CreationContext()->Global(), m_isolate)); |
+ break; |
+ case Rejected: |
+ resolver->Reject(rejectedValue(resolver->CreationContext()->Global(), m_isolate)); |
+ break; |
+ } |
+} |
+ |
+} // namespace WebCore |