Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / ScriptWrappable.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #ifndef ScriptWrappable_h
32 #define ScriptWrappable_h
33
34 #include "bindings/core/v8/WrapperTypeInfo.h"
35 #include "platform/ScriptForbiddenScope.h"
36 #include "platform/heap/Handle.h"
37 #include <v8.h>
38
39 // Helper to call webCoreInitializeScriptWrappableForInterface in the global namespace.
40 template <class C> inline void initializeScriptWrappableHelper(C* object)
41 {
42     void webCoreInitializeScriptWrappableForInterface(C*);
43     webCoreInitializeScriptWrappableForInterface(object);
44 }
45
46 namespace blink {
47
48 /**
49  * The base class of all wrappable objects.
50  *
51  * This class provides the internal pointer to be stored in the wrapper objects,
52  * and its conversions from / to the DOM instances.
53  *
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.
57  */
58 class ScriptWrappableBase {
59 public:
60     template <class T> static T* fromInternalPointer(ScriptWrappableBase* internalPointer)
61     {
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);
68     }
69     ScriptWrappableBase* toInternalPointer() { return this; }
70 };
71
72 /**
73  * ScriptWrappable wraps a V8 object and its WrapperTypeInfo.
74  *
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).
78  *
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.
84  *
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.
88  *
89  * typeInfo() and newLocalWrapper will return appropriate values (possibly
90  * 0/empty) in all physical states.
91  *
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.
100  */
101 class ScriptWrappable : public ScriptWrappableBase {
102 public:
103     ScriptWrappable() : m_wrapperOrTypeInfo(0) { }
104
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)
113     {
114         initializeScriptWrappableHelper(object);
115     }
116
117     void setWrapper(v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
118     {
119         ASSERT(!containsWrapper());
120         if (!*wrapper) {
121             m_wrapperOrTypeInfo = 0;
122             return;
123         }
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());
129     }
130
131     v8::Local<v8::Object> newLocalWrapper(v8::Isolate* isolate) const
132     {
133         v8::Persistent<v8::Object> persistent;
134         getPersistent(&persistent);
135         return v8::Local<v8::Object>::New(isolate, persistent);
136     }
137
138     const WrapperTypeInfo* typeInfo()
139     {
140         if (containsTypeInfo())
141             return reinterpret_cast<const WrapperTypeInfo*>(m_wrapperOrTypeInfo);
142
143         if (containsWrapper()) {
144             v8::Persistent<v8::Object> persistent;
145             getPersistent(&persistent);
146             return toWrapperTypeInfo(persistent);
147         }
148
149         return 0;
150     }
151
152     void setTypeInfo(const WrapperTypeInfo* typeInfo)
153     {
154         m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(typeInfo);
155         ASSERT(containsTypeInfo());
156     }
157
158     bool isEqualTo(const v8::Local<v8::Object>& other) const
159     {
160         v8::Persistent<v8::Object> persistent;
161         getPersistent(&persistent);
162         return persistent == other;
163     }
164
165     static bool wrapperCanBeStoredInObject(const void*) { return false; }
166     static bool wrapperCanBeStoredInObject(const ScriptWrappable*) { return true; }
167
168     static ScriptWrappable* fromObject(const void*)
169     {
170         ASSERT_NOT_REACHED();
171         return 0;
172     }
173
174     static ScriptWrappable* fromObject(ScriptWrappable* object)
175     {
176         return object;
177     }
178
179     bool setReturnValue(v8::ReturnValue<v8::Value> returnValue)
180     {
181         v8::Persistent<v8::Object> persistent;
182         getPersistent(&persistent);
183         returnValue.Set(persistent);
184         return containsWrapper();
185     }
186
187     void markAsDependentGroup(ScriptWrappable* groupRoot, v8::Isolate* isolate)
188     {
189         ASSERT(containsWrapper());
190         ASSERT(groupRoot && groupRoot->containsWrapper());
191
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);
197     }
198
199     void setReference(const v8::Persistent<v8::Object>& parent, v8::Isolate* isolate)
200     {
201         v8::Persistent<v8::Object> persistent;
202         getPersistent(&persistent);
203         isolate->SetReference(parent, persistent);
204     }
205
206     template<typename V8T, typename T>
207     static void assertWrapperSanity(v8::Local<v8::Object> object, T* objectAsT)
208     {
209         ASSERT(objectAsT);
210         RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(object.IsEmpty()
211             || object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(objectAsT));
212     }
213
214     template<typename V8T, typename T>
215     static void assertWrapperSanity(void* object, T* objectAsT)
216     {
217         ASSERT_NOT_REACHED();
218     }
219
220     template<typename V8T, typename T>
221     static void assertWrapperSanity(ScriptWrappable* object, T* objectAsT)
222     {
223         ASSERT(object);
224         ASSERT(objectAsT);
225         v8::Object* value = object->getRawValue();
226         RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(value == 0
227             || value->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex) == V8T::toInternalPointer(objectAsT));
228     }
229
230     inline bool containsWrapper() const { return (m_wrapperOrTypeInfo & 1); }
231     inline bool containsTypeInfo() const { return m_wrapperOrTypeInfo && !(m_wrapperOrTypeInfo & 1); }
232
233 #if !ENABLE(OILPAN)
234 protected:
235     virtual ~ScriptWrappable()
236     {
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.
242     }
243 #endif
244     // With Oilpan we don't need a ScriptWrappable destructor.
245     //
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.
252     //
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.
255
256 private:
257     void getPersistent(v8::Persistent<v8::Object>* persistent) const
258     {
259         ASSERT(persistent);
260
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();
266     }
267
268     inline v8::Object* getRawValue() const
269     {
270         v8::Object* object = containsWrapper() ? reinterpret_cast<v8::Object*>(m_wrapperOrTypeInfo & ~1) : 0;
271         return object;
272     }
273
274     inline void disposeWrapper(v8::Local<v8::Object> wrapper)
275     {
276         ASSERT(containsWrapper());
277
278         v8::Persistent<v8::Object> persistent;
279         getPersistent(&persistent);
280
281         ASSERT(wrapper == persistent);
282         persistent.Reset();
283         setTypeInfo(toWrapperTypeInfo(wrapper));
284     }
285
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;
290
291     static void setWeakCallback(const v8::WeakCallbackData<v8::Object, ScriptWrappable>& data)
292     {
293         v8::Persistent<v8::Object> persistent;
294         data.GetParameter()->getPersistent(&persistent);
295         ASSERT(persistent == data.GetValue());
296         data.GetParameter()->disposeWrapper(data.GetValue());
297
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());
302     }
303 };
304
305 } // namespace blink
306
307 #endif // ScriptWrappable_h