Index: Source/bindings/v8/ScriptPromiseProperty.h |
diff --git a/Source/bindings/v8/ScriptPromiseProperty.h b/Source/bindings/v8/ScriptPromiseProperty.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9b331acda1301ce76d3c5299efbf60030667e7ac |
--- /dev/null |
+++ b/Source/bindings/v8/ScriptPromiseProperty.h |
@@ -0,0 +1,141 @@ |
+// 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. |
+ |
+#ifndef ScriptPromiseProperty_h |
+#define ScriptPromiseProperty_h |
+ |
+#include "bindings/v8/ScriptPromise.h" |
+#include "bindings/v8/ScriptPromisePropertyBase.h" |
+#include "bindings/v8/V8Binding.h" |
+#include "platform/heap/Visitor.h" |
haraken
2014/07/01 06:29:12
We normally include platform/heap/Handle.h.
|
+#include "wtf/Noncopyable.h" |
+#include "wtf/PassRefPtr.h" |
+#include "wtf/RefPtr.h" |
+ |
+namespace WebCore { |
+ |
+class ExecutionContext; |
+ |
+// ScriptPromiseProperty is a helper for implementing a DOM method or |
+// attribute whose value is a Promise, and the same Promise must be |
+// returned each time. |
+// |
+// ScriptPromiseProperty does not keep Promises or worlds alive to |
+// deliver Promise resolution/rejection to them; the Promise |
+// resolution/rejections are delivered if the wrapper for the object |
+// with the property is alive. To avoid exposing the action of the |
+// garbage collector to script, you should keep the wrapper alive as |
+// long as a promise may be settled. |
+// |
+// ScriptPromiseProperty is an ActiveDOMObject and defers resolving or |
+// rejecting Promises while active DOM objects are suspended. Do not |
+// call promise, resolve or reject when active DOM objects are |
+// stopped. |
+// |
+// ScriptPromiseProperty only supports the main world. |
+// FIXME: Implement support for isolated worlds. |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+class ScriptPromiseProperty : public ScriptPromisePropertyBase { |
+ WTF_MAKE_NONCOPYABLE(ScriptPromiseProperty); |
+public: |
+ // Creates a ScriptPromiseProperty that will create Promises in |
+ // the specified ExecutionContext for a property of 'holder' |
+ // (typically ScriptPromiseProperty should be a member of the |
+ // property holder). 'name' must be a static string that is a |
+ // unique name for the hidden value backing the property. |
+ template<typename PassHolderType> |
+ static PassRefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType, RejectedType> > create(ExecutionContext*, PassHolderType, const char* name); |
+ |
+ virtual ~ScriptPromiseProperty() { } |
+ |
+ virtual void trace(Visitor*); |
+ |
+ template<typename PassResolvedType> |
+ void resolve(PassResolvedType); |
+ |
+ template<typename PassRejectedType> |
+ void reject(PassRejectedType); |
+ |
+private: |
+ template<typename PassHolderType> |
+ ScriptPromiseProperty(ExecutionContext*, PassHolderType, const char* name); |
+ |
+ virtual v8::Handle<v8::Object> holder(v8::Handle<v8::Object> creationContext, v8::Isolate*) OVERRIDE; |
+ virtual v8::Handle<v8::Value> resolvedValue(v8::Handle<v8::Object> creationContext, v8::Isolate*) OVERRIDE; |
+ virtual v8::Handle<v8::Value> rejectedValue(v8::Handle<v8::Object> creationContext, v8::Isolate*) OVERRIDE; |
+ |
+ HolderType m_holder; |
+ ResolvedType m_resolved; |
+ RejectedType m_rejected; |
+}; |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+template<typename PassHolderType> |
+PassRefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType, RejectedType> > ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::create(ExecutionContext* executionContext, PassHolderType holder, const char* name) |
+{ |
+ RefPtrWillBeRawPtr<ScriptPromiseProperty<HolderType, ResolvedType, RejectedType> > property = adoptRef(new ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>(executionContext, holder, name)); |
+ property->suspendIfNeeded(); |
+ return property.release(); |
+} |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+template<typename PassHolderType> |
+ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::ScriptPromiseProperty(ExecutionContext* executionContext, PassHolderType holder, const char* name) |
+ : ScriptPromisePropertyBase(executionContext, name) |
+ , m_holder(holder) |
+{ |
+} |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::trace(Visitor* visitor) |
+{ |
+ visitor->trace(m_holder); |
+ visitor->trace(m_resolved); |
+ visitor->trace(m_rejected); |
haraken
2014/07/01 06:29:12
What are types of m_holder, m_resolved and m_rejec
|
+} |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+template<typename PassResolvedType> |
+void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::resolve(PassResolvedType value) |
+{ |
+ if (state() != Pending) |
+ return; |
+ m_resolved = value; |
+ settle(Resolved); |
+} |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+template<typename PassRejectedType> |
+void ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::reject(PassRejectedType value) |
+{ |
+ if (state() != Pending) |
+ return; |
+ m_rejected = value; |
+ settle(Rejected); |
+} |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+v8::Handle<v8::Object> ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::holder(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) |
+{ |
+ v8::Handle<v8::Value> value = V8ValueTraits<HolderType>::toV8Value(m_holder, creationContext, isolate); |
+ return value.As<v8::Object>(); |
+} |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+v8::Handle<v8::Value> ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::resolvedValue(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) |
+{ |
+ ASSERT(state() == Resolved); |
+ return V8ValueTraits<ResolvedType>::toV8Value(m_resolved, creationContext, isolate); |
+} |
+ |
+template<typename HolderType, typename ResolvedType, typename RejectedType> |
+v8::Handle<v8::Value> ScriptPromiseProperty<HolderType, ResolvedType, RejectedType>::rejectedValue(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) |
+{ |
+ ASSERT(state() == Rejected); |
+ return V8ValueTraits<RejectedType>::toV8Value(m_rejected, creationContext, isolate); |
+} |
+ |
+} // namespace WebCore |
+ |
+#endif // ScriptPromiseProperty_h |