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