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"
39 // Helper to call webCoreInitializeScriptWrappableForInterface in the global namespace.
40 template <class C> inline void initializeScriptWrappableHelper(C* object)
42 void webCoreInitializeScriptWrappableForInterface(C*);
43 webCoreInitializeScriptWrappableForInterface(object);
48 class ScriptWrappable {
50 ScriptWrappable() : m_wrapperOrTypeInfo(0) { }
52 // Wrappables need to be initialized with their most derrived type for which
53 // bindings exist, in much the same way that certain other types need to be
54 // adopted and so forth. The overloaded initializeScriptWrappableForInterface()
55 // functions are implemented by the generated V8 bindings code. Declaring the
56 // extern function in the template avoids making a centralized header of all
57 // the bindings in the universe. C++11's extern template feature may provide
58 // a cleaner solution someday.
59 template <class C> static void init(C* object)
61 initializeScriptWrappableHelper(object);
64 void setWrapper(v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
66 ASSERT(!containsWrapper());
68 m_wrapperOrTypeInfo = 0;
71 v8::Persistent<v8::Object> persistent(isolate, wrapper);
72 configuration.configureWrapper(&persistent);
73 persistent.SetWeak(this, &setWeakCallback);
74 m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(persistent.ClearAndLeak()) | 1;
75 ASSERT(containsWrapper());
78 v8::Local<v8::Object> newLocalWrapper(v8::Isolate* isolate) const
80 return unsafePersistent().newLocal(isolate);
83 const WrapperTypeInfo* typeInfo()
85 if (containsTypeInfo())
86 return reinterpret_cast<const WrapperTypeInfo*>(m_wrapperOrTypeInfo);
88 if (containsWrapper())
89 return toWrapperTypeInfo(*(unsafePersistent().persistent()));
94 void setTypeInfo(const WrapperTypeInfo* info)
96 m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(info);
97 ASSERT(containsTypeInfo());
100 static bool wrapperCanBeStoredInObject(const void*) { return false; }
101 static bool wrapperCanBeStoredInObject(const ScriptWrappable*) { return true; }
103 static void setWrapperInObject(void*, v8::Handle<v8::Object>, v8::Isolate*, const WrapperConfiguration&)
105 ASSERT_NOT_REACHED();
108 static void setWrapperInObject(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
110 object->setWrapper(wrapper, isolate, configuration);
113 static const WrapperTypeInfo* getTypeInfoFromObject(void* object)
115 ASSERT_NOT_REACHED();
119 static const WrapperTypeInfo* getTypeInfoFromObject(ScriptWrappable* object)
121 return object->typeInfo();
124 static void setTypeInfoInObject(void* object, const WrapperTypeInfo* info)
126 ASSERT_NOT_REACHED();
129 static void setTypeInfoInObject(ScriptWrappable* object, const WrapperTypeInfo* info)
131 object->setTypeInfo(info);
134 template<typename V8T, typename T>
135 static bool setReturnValueWithSecurityCheck(v8::ReturnValue<v8::Value> returnValue, T* object)
137 return ScriptWrappable::getUnsafeWrapperFromObject(object).template setReturnValueWithSecurityCheck<V8T>(returnValue, object);
141 static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, T* object)
143 return ScriptWrappable::getUnsafeWrapperFromObject(object).setReturnValue(returnValue);
149 ASSERT(m_wrapperOrTypeInfo); // Assert initialization via init() even if not subsequently wrapped.
150 m_wrapperOrTypeInfo = 0; // Break UAF attempts to wrap.
154 // For calling unsafePersistent and getWrapperFromObject.
155 friend class MinorGCWrapperVisitor;
156 friend class DOMDataStore;
158 UnsafePersistent<v8::Object> unsafePersistent() const
160 v8::Object* object = containsWrapper() ? reinterpret_cast<v8::Object*>(m_wrapperOrTypeInfo & ~1) : 0;
161 return UnsafePersistent<v8::Object>(object);
164 static UnsafePersistent<v8::Object> getUnsafeWrapperFromObject(void*)
166 ASSERT_NOT_REACHED();
167 return UnsafePersistent<v8::Object>();
170 static UnsafePersistent<v8::Object> getUnsafeWrapperFromObject(ScriptWrappable* object)
172 return object->unsafePersistent();
175 inline bool containsWrapper() const { return (m_wrapperOrTypeInfo & 1) == 1; }
176 inline bool containsTypeInfo() const { return m_wrapperOrTypeInfo && (m_wrapperOrTypeInfo & 1) == 0; }
178 inline void disposeWrapper(v8::Local<v8::Object> value, const WrapperTypeInfo* info)
180 ASSERT(containsWrapper());
181 ASSERT(value == *unsafePersistent().persistent());
182 unsafePersistent().dispose();
186 // If zero, then this contains nothing, otherwise:
187 // If the bottom bit it set, then this contains a pointer to a wrapper object in the remainging bits.
188 // If the bottom bit is clear, then this contains a pointer to the wrapper type info in the remaining bits.
189 uintptr_t m_wrapperOrTypeInfo;
191 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, ScriptWrappable>& data)
193 ASSERT(*data.GetParameter()->unsafePersistent().persistent() == data.GetValue());
195 // Note: |object| might not be equal to |data|.GetParameter(), e.g., if ScriptWrappable isn't a left-most base class.
196 void* object = toNative(data.GetValue());
197 const WrapperTypeInfo* info = toWrapperTypeInfo(data.GetValue());
198 ASSERT(info->derefObjectFunction);
200 data.GetParameter()->disposeWrapper(data.GetValue(), info);
201 // FIXME: I noticed that 50%~ of minor GC cycle times can be consumed
202 // inside data.GetParameter()->deref(), which causes Node destructions. We should
203 // make Node destructions incremental.
204 info->derefObject(object);
208 } // namespace WebCore
210 #endif // ScriptWrappable_h