2 * Copyright (C) 2010 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.
31 #ifndef ScriptWrappable_h
32 #define ScriptWrappable_h
34 #include "bindings/v8/UnsafePersistent.h"
35 #include "bindings/v8/V8Utilities.h"
36 #include "bindings/v8/WrapperTypeInfo.h"
37 #include "heap/Handle.h"
40 // Helper to call webCoreInitializeScriptWrappableForInterface in the global namespace.
41 template <class C> inline void initializeScriptWrappableHelper(C* object)
43 void webCoreInitializeScriptWrappableForInterface(C*);
44 webCoreInitializeScriptWrappableForInterface(object);
49 class ScriptWrappable {
51 ScriptWrappable() : m_wrapperOrTypeInfo(0) { }
53 // Wrappables need to be initialized with their most derrived type for which
54 // bindings exist, in much the same way that certain other types need to be
55 // adopted and so forth. The overloaded initializeScriptWrappableForInterface()
56 // functions are implemented by the generated V8 bindings code. Declaring the
57 // extern function in the template avoids making a centralized header of all
58 // the bindings in the universe. C++11's extern template feature may provide
59 // a cleaner solution someday.
60 template <class C> static void init(C* object)
62 initializeScriptWrappableHelper(object);
65 void setWrapper(v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
67 ASSERT(!containsWrapper());
69 m_wrapperOrTypeInfo = 0;
72 v8::Persistent<v8::Object> persistent(isolate, wrapper);
73 configuration.configureWrapper(&persistent);
74 persistent.SetWeak(this, &setWeakCallback);
75 m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(persistent.ClearAndLeak()) | 1;
76 ASSERT(containsWrapper());
79 v8::Local<v8::Object> newLocalWrapper(v8::Isolate* isolate) const
81 return unsafePersistent().newLocal(isolate);
84 const WrapperTypeInfo* typeInfo()
86 if (containsTypeInfo())
87 return reinterpret_cast<const WrapperTypeInfo*>(m_wrapperOrTypeInfo);
89 if (containsWrapper())
90 return toWrapperTypeInfo(*(unsafePersistent().persistent()));
95 void setTypeInfo(const WrapperTypeInfo* typeInfo)
97 m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(typeInfo);
98 ASSERT(containsTypeInfo());
101 static bool wrapperCanBeStoredInObject(const void*) { return false; }
102 static bool wrapperCanBeStoredInObject(const ScriptWrappable*) { return true; }
104 static void setWrapperInObject(void*, v8::Handle<v8::Object>, v8::Isolate*, const WrapperConfiguration&)
106 ASSERT_NOT_REACHED();
109 static void setWrapperInObject(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
111 object->setWrapper(wrapper, isolate, configuration);
114 static const WrapperTypeInfo* getTypeInfoFromObject(void* object)
116 ASSERT_NOT_REACHED();
120 static const WrapperTypeInfo* getTypeInfoFromObject(ScriptWrappable* object)
122 return object->typeInfo();
125 static void setTypeInfoInObject(void* object, const WrapperTypeInfo*)
127 ASSERT_NOT_REACHED();
130 static void setTypeInfoInObject(ScriptWrappable* object, const WrapperTypeInfo* typeInfo)
132 object->setTypeInfo(typeInfo);
135 template<typename V8T, typename T>
136 static bool setReturnValueWithSecurityCheck(v8::ReturnValue<v8::Value> returnValue, T* object)
138 return ScriptWrappable::getUnsafeWrapperFromObject(object).template setReturnValueWithSecurityCheck<V8T>(returnValue, object);
142 static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, T* object)
144 return ScriptWrappable::getUnsafeWrapperFromObject(object).setReturnValue(returnValue);
150 ASSERT(m_wrapperOrTypeInfo); // Assert initialization via init() even if not subsequently wrapped.
151 m_wrapperOrTypeInfo = 0; // Break UAF attempts to wrap.
155 // For calling unsafePersistent and getWrapperFromObject.
156 friend class MinorGCWrapperVisitor;
157 friend class DOMDataStore;
159 UnsafePersistent<v8::Object> unsafePersistent() const
161 v8::Object* object = containsWrapper() ? reinterpret_cast<v8::Object*>(m_wrapperOrTypeInfo & ~1) : 0;
162 return UnsafePersistent<v8::Object>(object);
165 static UnsafePersistent<v8::Object> getUnsafeWrapperFromObject(void*)
167 ASSERT_NOT_REACHED();
168 return UnsafePersistent<v8::Object>();
171 static UnsafePersistent<v8::Object> getUnsafeWrapperFromObject(ScriptWrappable* object)
173 return object->unsafePersistent();
176 inline bool containsWrapper() const { return (m_wrapperOrTypeInfo & 1) == 1; }
177 inline bool containsTypeInfo() const { return m_wrapperOrTypeInfo && (m_wrapperOrTypeInfo & 1) == 0; }
179 inline void disposeWrapper(v8::Local<v8::Object> wrapper)
181 ASSERT(containsWrapper());
182 ASSERT(wrapper == *unsafePersistent().persistent());
183 unsafePersistent().dispose();
184 setTypeInfo(toWrapperTypeInfo(wrapper));
187 // If zero, then this contains nothing, otherwise:
188 // If the bottom bit it set, then this contains a pointer to a wrapper object in the remainging bits.
189 // If the bottom bit is clear, then this contains a pointer to the wrapper type info in the remaining bits.
190 uintptr_t m_wrapperOrTypeInfo;
192 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, ScriptWrappable>& data)
194 ASSERT(*data.GetParameter()->unsafePersistent().persistent() == data.GetValue());
195 data.GetParameter()->disposeWrapper(data.GetValue());
197 // FIXME: I noticed that 50%~ of minor GC cycle times can be consumed
198 // inside data.GetParameter()->deref(), which causes Node destructions. We should
199 // make Node destructions incremental.
200 releaseObject(data.GetValue());
204 } // namespace WebCore
206 #endif // ScriptWrappable_h