Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / 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/v8/UnsafePersistent.h"
35 #include "bindings/v8/V8Utilities.h"
36 #include "bindings/v8/WrapperTypeInfo.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 WebCore {
47
48 class ScriptWrappable {
49 public:
50     ScriptWrappable() : m_wrapperOrTypeInfo(0) { }
51
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)
60     {
61         initializeScriptWrappableHelper(object);
62     }
63
64     void setWrapper(v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
65     {
66         ASSERT(!containsWrapper());
67         if (!*wrapper) {
68             m_wrapperOrTypeInfo = 0;
69             return;
70         }
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());
76     }
77
78     v8::Local<v8::Object> newLocalWrapper(v8::Isolate* isolate) const
79     {
80         return unsafePersistent().newLocal(isolate);
81     }
82
83     const WrapperTypeInfo* typeInfo()
84     {
85         if (containsTypeInfo())
86             return reinterpret_cast<const WrapperTypeInfo*>(m_wrapperOrTypeInfo);
87
88         if (containsWrapper())
89             return toWrapperTypeInfo(*(unsafePersistent().persistent()));
90
91         return 0;
92     }
93
94     void setTypeInfo(const WrapperTypeInfo* info)
95     {
96         m_wrapperOrTypeInfo = reinterpret_cast<uintptr_t>(info);
97         ASSERT(containsTypeInfo());
98     }
99
100     static bool wrapperCanBeStoredInObject(const void*) { return false; }
101     static bool wrapperCanBeStoredInObject(const ScriptWrappable*) { return true; }
102
103     static void setWrapperInObject(void*, v8::Handle<v8::Object>, v8::Isolate*, const WrapperConfiguration&)
104     {
105         ASSERT_NOT_REACHED();
106     }
107
108     static void setWrapperInObject(ScriptWrappable* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration)
109     {
110         object->setWrapper(wrapper, isolate, configuration);
111     }
112
113     static const WrapperTypeInfo* getTypeInfoFromObject(void* object)
114     {
115         ASSERT_NOT_REACHED();
116         return 0;
117     }
118
119     static const WrapperTypeInfo* getTypeInfoFromObject(ScriptWrappable* object)
120     {
121         return object->typeInfo();
122     }
123
124     static void setTypeInfoInObject(void* object, const WrapperTypeInfo* info)
125     {
126         ASSERT_NOT_REACHED();
127     }
128
129     static void setTypeInfoInObject(ScriptWrappable* object, const WrapperTypeInfo* info)
130     {
131         object->setTypeInfo(info);
132     }
133
134     template<typename V8T, typename T>
135     static bool setReturnValueWithSecurityCheck(v8::ReturnValue<v8::Value> returnValue, T* object)
136     {
137         return ScriptWrappable::getUnsafeWrapperFromObject(object).template setReturnValueWithSecurityCheck<V8T>(returnValue, object);
138     }
139
140     template<typename T>
141     static bool setReturnValue(v8::ReturnValue<v8::Value> returnValue, T* object)
142     {
143         return ScriptWrappable::getUnsafeWrapperFromObject(object).setReturnValue(returnValue);
144     }
145
146 protected:
147     ~ScriptWrappable()
148     {
149         ASSERT(m_wrapperOrTypeInfo);  // Assert initialization via init() even if not subsequently wrapped.
150         m_wrapperOrTypeInfo = 0;      // Break UAF attempts to wrap.
151     }
152
153 private:
154     // For calling unsafePersistent and getWrapperFromObject.
155     friend class MinorGCWrapperVisitor;
156     friend class DOMDataStore;
157
158     UnsafePersistent<v8::Object> unsafePersistent() const
159     {
160         v8::Object* object = containsWrapper() ? reinterpret_cast<v8::Object*>(m_wrapperOrTypeInfo & ~1) : 0;
161         return UnsafePersistent<v8::Object>(object);
162     }
163
164     static UnsafePersistent<v8::Object> getUnsafeWrapperFromObject(void*)
165     {
166         ASSERT_NOT_REACHED();
167         return UnsafePersistent<v8::Object>();
168     }
169
170     static UnsafePersistent<v8::Object> getUnsafeWrapperFromObject(ScriptWrappable* object)
171     {
172         return object->unsafePersistent();
173     }
174
175     inline bool containsWrapper() const { return (m_wrapperOrTypeInfo & 1) == 1; }
176     inline bool containsTypeInfo() const { return m_wrapperOrTypeInfo && (m_wrapperOrTypeInfo & 1) == 0; }
177
178     inline void disposeWrapper(v8::Local<v8::Object> value, const WrapperTypeInfo* info)
179     {
180         ASSERT(containsWrapper());
181         ASSERT(value == *unsafePersistent().persistent());
182         unsafePersistent().dispose();
183         setTypeInfo(info);
184     }
185
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;
190
191     static void setWeakCallback(const v8::WeakCallbackData<v8::Object, ScriptWrappable>& data)
192     {
193         ASSERT(*data.GetParameter()->unsafePersistent().persistent() == data.GetValue());
194
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);
199
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);
205     }
206 };
207
208 } // namespace WebCore
209
210 #endif // ScriptWrappable_h