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/core/v8/WrapperTypeInfo.h"
35 #include "platform/ScriptForbiddenScope.h"
36 #include "platform/heap/Handle.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);
49 * The base class of all wrappable objects.
51 * This class provides the internal pointer to be stored in the wrapper objects,
52 * and its conversions from / to the DOM instances.
54 * Note that this class must not have vtbl (any virtual function) or any member
55 * variable which increase the size of instances. Some of the classes sensitive
56 * to the size inherit from this class. So this class must be zero size.
58 class ScriptWrappableBase {
60 template <class T> static T* fromInternalPointer(ScriptWrappableBase* internalPointer)
62 // Check if T* is castable to ScriptWrappableBase*, which means T
63 // doesn't have two or more ScriptWrappableBase as superclasses.
64 // If T has two ScriptWrappableBase as superclasses, conversions
65 // from T* to ScriptWrappableBase* are ambiguous.
66 ASSERT(static_cast<ScriptWrappableBase*>(static_cast<T*>(internalPointer)));
67 return static_cast<T*>(internalPointer);
69 ScriptWrappableBase* toInternalPointer() { return this; }
73 * ScriptWrappable wraps a V8 object and its WrapperTypeInfo.
75 * ScriptWrappable acts much like a v8::Persistent<> in that it keeps a
76 * V8 object alive. Under the hood, however, it keeps either a TypeInfo
77 * object or an actual v8 persistent (or is empty).
79 * The physical state space of ScriptWrappable is:
80 * - uintptr_t m_wrapperOrTypeInfo;
81 * - if 0: the ScriptWrappable is uninitialized/empty.
82 * - if even: a pointer to blink::TypeInfo
83 * - if odd: a pointer to v8::Persistent<v8::Object> + 1.
85 * In other words, one integer represents one of two object pointers,
86 * depending on its least signficiant bit, plus an uninitialized state.
87 * This class is meant to mask the logistics behind this.
89 * typeInfo() and newLocalWrapper will return appropriate values (possibly
90 * 0/empty) in all physical states.
92 * The state transitions are:
93 * - new: an empty and invalid ScriptWrappable.
94 * - init (to be called by all subclasses in their constructor):
95 * needs to call setTypeInfo
96 * - setTypeInfo: install a WrapperTypeInfo
97 * - setWrapper: install a v8::Persistent (or empty)
98 * - disposeWrapper (via setWeakCallback, triggered by V8 garbage collecter):
99 * remove v8::Persistent and install a TypeInfo of the previous value.
101 class ScriptWrappable : public ScriptWrappableBase {
103 ScriptWrappable() : m_wrapperOrTypeInfo(0) { }
105 // Wrappables need to be initialized with their most derrived type for which
106 // bindings exist, in much the same way that certain other types need to be
107 // adopted and so forth. The overloaded initializeScriptWrappableForInterface()
108 // functions are implemented by the generated V8 bindings code. Declaring the
109 // extern function in the template avoids making a centralized header of all
110 // the bindings in the universe. C++11's extern template feature may provide
111 // a cleaner solution someday.
112 template <class C> static void init(C* object)
114 initializeScriptWrappableHelper(object);
117 void setWrapper(v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
119 ASSERT(!containsWrapper());
121 m_wrapperOrTypeInfo = 0;
124 v8::Persistent<v8::Object> persistent(isolate, wrapper);
125 configuration.configureWrapper(&persistent);
126 persistent.SetWeak(this, &setWeakCallback);
127 m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(persistent.ClearAndLeak()) | 1;
128 ASSERT(containsWrapper());
131 v8::Local<v8::Object> newLocalWrapper(v8::Isolate* isolate) const
133 v8::Persistent<v8::Object> persistent;
134 getPersistent(&persistent);
135 return v8::Local<v8::Object>::New(isolate, persistent);
138 const WrapperTypeInfo* typeInfo()
140 if (containsTypeInfo())
141 return reinterpret_cast<const WrapperTypeInfo*>(m_wrapperOrTypeInfo);
143 if (containsWrapper()) {
144 v8::Persistent<v8::Object> persistent;
145 getPersistent(&persistent);
146 return toWrapperTypeInfo(persistent);
152 void setTypeInfo(const WrapperTypeInfo* typeInfo)
154 m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(typeInfo);
155 ASSERT(containsTypeInfo());
158 bool isEqualTo(const v8::Local<v8::Object>& other) const
160 v8::Persistent<v8::Object> persistent;
161 getPersistent(&persistent);
162 return persistent == other;
165 static bool wrapperCanBeStoredInObject(const void*) { return false; }
166 static bool wrapperCanBeStoredInObject(const ScriptWrappable*) { return true; }
168 static ScriptWrappable* fromObject(const void*)
170 ASSERT_NOT_REACHED();
174 static ScriptWrappable* fromObject(ScriptWrappable* object)
179 bool setReturnValue(v8::ReturnValue<v8::Value> returnValue)
181 v8::Persistent<v8::Object> persistent;
182 getPersistent(&persistent);
183 returnValue.Set(persistent);
184 return containsWrapper();
187 void markAsDependentGroup(ScriptWrappable* groupRoot, v8::Isolate* isolate)
189 ASSERT(containsWrapper());
190 ASSERT(groupRoot && groupRoot->containsWrapper());
192 v8::UniqueId groupId(groupRoot->m_wrapperOrTypeInfo);
193 v8::Persistent<v8::Object> wrapper;
194 getPersistent(&wrapper);
195 wrapper.MarkPartiallyDependent();
196 isolate->SetObjectGroupId(v8::Persistent<v8::Value>::Cast(wrapper), groupId);
199 void setReference(const v8::Persistent<v8::Object>& parent, v8::Isolate* isolate)
201 v8::Persistent<v8::Object> persistent;
202 getPersistent(&persistent);
203 isolate->SetReference(parent, persistent);
206 template<typename V8T, typename T>
207 static void assertWrapperSanity(v8::Local<v8::Object> object, T* objectAsT)
210 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(object.IsEmpty()
211 || object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(objectAsT));
214 template<typename V8T, typename T>
215 static void assertWrapperSanity(void* object, T* objectAsT)
217 ASSERT_NOT_REACHED();
220 template<typename V8T, typename T>
221 static void assertWrapperSanity(ScriptWrappable* object, T* objectAsT)
225 v8::Object* value = object->getRawValue();
226 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(value == 0
227 || value->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(objectAsT));
230 inline bool containsWrapper() const { return (m_wrapperOrTypeInfo & 1); }
231 inline bool containsTypeInfo() const { return m_wrapperOrTypeInfo && !(m_wrapperOrTypeInfo & 1); }
235 virtual ~ScriptWrappable()
237 // We must not get deleted as long as we contain a wrapper. If this happens, we screwed up ref
238 // counting somewhere. Crash here instead of crashing during a later gc cycle.
239 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!containsWrapper());
240 ASSERT(m_wrapperOrTypeInfo); // Assert initialization via init() even if not subsequently wrapped.
241 m_wrapperOrTypeInfo = 0; // Break UAF attempts to wrap.
244 // With Oilpan we don't need a ScriptWrappable destructor.
246 // - 'RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!containsWrapper())' is not needed
247 // because Oilpan is not using reference counting at all. If containsWrapper() is true,
248 // it means that ScriptWrappable still has a wrapper. In this case, the destructor
249 // must not be called since the wrapper has a persistent handle back to this ScriptWrappable object.
250 // Assuming that Oilpan's GC is correct (If we cannot assume this, a lot of more things are
251 // already broken), we must not hit the RELEASE_ASSERT.
253 // - 'm_wrapperOrTypeInfo = 0' is not needed because Oilpan's GC zeroes out memory when
254 // the memory is collected and added to a free list.
257 void getPersistent(v8::Persistent<v8::Object>* persistent) const
261 // Horrible and super unsafe: Cast the Persistent to an Object*, so
262 // that we can inject the wrapped value. This only works because
263 // we previously 'stole' the object pointer from a Persistent in
264 // the setWrapper() method.
265 *reinterpret_cast<v8::Object**>(persistent) = getRawValue();
268 inline v8::Object* getRawValue() const
270 v8::Object* object = containsWrapper() ? reinterpret_cast<v8::Object*>(m_wrapperOrTypeInfo & ~1) : 0;
274 inline void disposeWrapper(v8::Local<v8::Object> wrapper)
276 ASSERT(containsWrapper());
278 v8::Persistent<v8::Object> persistent;
279 getPersistent(&persistent);
281 ASSERT(wrapper == persistent);
283 setTypeInfo(toWrapperTypeInfo(wrapper));
286 // If zero, then this contains nothing, otherwise:
287 // If the bottom bit it set, then this contains a pointer to a wrapper object in the remainging bits.
288 // If the bottom bit is clear, then this contains a pointer to the wrapper type info in the remaining bits.
289 uintptr_t m_wrapperOrTypeInfo;
291 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, ScriptWrappable>& data)
293 v8::Persistent<v8::Object> persistent;
294 data.GetParameter()->getPersistent(&persistent);
295 ASSERT(persistent == data.GetValue());
296 data.GetParameter()->disposeWrapper(data.GetValue());
298 // FIXME: I noticed that 50%~ of minor GC cycle times can be consumed
299 // inside data.GetParameter()->deref(), which causes Node destructions. We should
300 // make Node destructions incremental.
301 releaseObject(data.GetValue());
307 #endif // ScriptWrappable_h