Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / WrapperTypeInfo.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 WrapperTypeInfo_h
32 #define WrapperTypeInfo_h
33
34 #include "gin/public/wrapper_info.h"
35 #include "heap/Handle.h"
36 #include "wtf/Assertions.h"
37 #include <v8.h>
38
39 namespace WebCore {
40
41     class ActiveDOMObject;
42     class EventTarget;
43     class Node;
44
45     static const int v8DOMWrapperTypeIndex = static_cast<int>(gin::kWrapperInfoIndex);
46     static const int v8DOMWrapperObjectIndex = static_cast<int>(gin::kEncodedValueIndex);
47     static const int v8DefaultWrapperInternalFieldCount = static_cast<int>(gin::kNumberOfInternalFields);
48     static const int v8PrototypeTypeIndex = 0;
49     static const int v8PrototypeInternalFieldcount = 1;
50
51     static const uint16_t v8DOMNodeClassId = 1;
52     static const uint16_t v8DOMObjectClassId = 2;
53
54     enum WrapperWorldType {
55         MainWorld,
56         IsolatedWorld,
57         WorkerWorld
58     };
59
60     typedef v8::Handle<v8::FunctionTemplate> (*DomTemplateFunction)(v8::Isolate*, WrapperWorldType);
61     typedef void (*DerefObjectFunction)(void*);
62     typedef ActiveDOMObject* (*ToActiveDOMObjectFunction)(v8::Handle<v8::Object>);
63     typedef EventTarget* (*ToEventTargetFunction)(v8::Handle<v8::Object>);
64     typedef void (*ResolveWrapperReachabilityFunction)(void*, const v8::Persistent<v8::Object>&, v8::Isolate*);
65     typedef void (*InstallPerContextEnabledPrototypePropertiesFunction)(v8::Handle<v8::Object>, v8::Isolate*);
66
67     enum WrapperTypePrototype {
68         WrapperTypeObjectPrototype,
69         WrapperTypeExceptionPrototype
70     };
71
72     inline void setObjectGroup(void* object, const v8::Persistent<v8::Object>& wrapper, v8::Isolate* isolate)
73     {
74         isolate->SetObjectGroupId(wrapper, v8::UniqueId(reinterpret_cast<intptr_t>(object)));
75     }
76
77     // This struct provides a way to store a bunch of information that is helpful when unwrapping
78     // v8 objects. Each v8 bindings class has exactly one static WrapperTypeInfo member, so
79     // comparing pointers is a safe way to determine if types match.
80     struct WrapperTypeInfo {
81
82         static const WrapperTypeInfo* unwrap(v8::Handle<v8::Value> typeInfoWrapper)
83         {
84             return reinterpret_cast<const WrapperTypeInfo*>(v8::External::Cast(*typeInfoWrapper)->Value());
85         }
86
87
88         bool equals(const WrapperTypeInfo* that) const
89         {
90             return this == that;
91         }
92
93         bool isSubclass(const WrapperTypeInfo* that) const
94         {
95             for (const WrapperTypeInfo* current = this; current; current = current->parentClass) {
96                 if (current == that)
97                     return true;
98             }
99
100             return false;
101         }
102
103         v8::Handle<v8::FunctionTemplate> domTemplate(v8::Isolate* isolate, WrapperWorldType worldType) const { return domTemplateFunction(isolate, worldType); }
104
105         void installPerContextEnabledMethods(v8::Handle<v8::Object> prototypeTemplate, v8::Isolate* isolate) const
106         {
107             if (installPerContextEnabledMethodsFunction)
108                 installPerContextEnabledMethodsFunction(prototypeTemplate, isolate);
109         }
110
111         ActiveDOMObject* toActiveDOMObject(v8::Handle<v8::Object> object) const
112         {
113             if (!toActiveDOMObjectFunction)
114                 return 0;
115             return toActiveDOMObjectFunction(object);
116         }
117
118         EventTarget* toEventTarget(v8::Handle<v8::Object> object) const
119         {
120             if (!toEventTargetFunction)
121                 return 0;
122             return toEventTargetFunction(object);
123         }
124
125         void visitDOMWrapper(void* object, const v8::Persistent<v8::Object>& wrapper, v8::Isolate* isolate) const
126         {
127             if (!visitDOMWrapperFunction)
128                 setObjectGroup(object, wrapper, isolate);
129             else
130                 visitDOMWrapperFunction(object, wrapper, isolate);
131         }
132
133         // This field must be the first member of the struct WrapperTypeInfo. This is also checked by a COMPILE_ASSERT() below.
134         const gin::GinEmbedder ginEmbedder;
135
136         const DomTemplateFunction domTemplateFunction;
137         const DerefObjectFunction derefObjectFunction;
138         const ToActiveDOMObjectFunction toActiveDOMObjectFunction;
139         const ToEventTargetFunction toEventTargetFunction;
140         const ResolveWrapperReachabilityFunction visitDOMWrapperFunction;
141         const InstallPerContextEnabledPrototypePropertiesFunction installPerContextEnabledMethodsFunction;
142         const WrapperTypeInfo* parentClass;
143         const WrapperTypePrototype wrapperTypePrototype;
144         const bool isGarbageCollected;
145     };
146
147
148     COMPILE_ASSERT(offsetof(struct WrapperTypeInfo, ginEmbedder) == offsetof(struct gin::WrapperInfo, embedder), wrapper_type_info_compatible_to_gin);
149
150     template<typename T, int offset>
151     inline T* getInternalField(const v8::Persistent<v8::Object>& persistent)
152     {
153         // This would be unsafe, but InternalFieldCount and GetAlignedPointerFromInternalField are guaranteed not to allocate
154         const v8::Handle<v8::Object>& object = reinterpret_cast<const v8::Handle<v8::Object>&>(persistent);
155         ASSERT(offset < object->InternalFieldCount());
156         return static_cast<T*>(object->GetAlignedPointerFromInternalField(offset));
157     }
158
159     template<typename T, int offset>
160     inline T* getInternalField(v8::Handle<v8::Object> wrapper)
161     {
162         ASSERT(offset < wrapper->InternalFieldCount());
163         return static_cast<T*>(wrapper->GetAlignedPointerFromInternalField(offset));
164     }
165
166     inline void* toNative(const v8::Persistent<v8::Object>& wrapper)
167     {
168         return getInternalField<void, v8DOMWrapperObjectIndex>(wrapper);
169     }
170
171     inline void* toNative(v8::Handle<v8::Object> wrapper)
172     {
173         return getInternalField<void, v8DOMWrapperObjectIndex>(wrapper);
174     }
175
176     inline const WrapperTypeInfo* toWrapperTypeInfo(const v8::Persistent<v8::Object>& wrapper)
177     {
178         return getInternalField<WrapperTypeInfo, v8DOMWrapperTypeIndex>(wrapper);
179     }
180
181     inline const WrapperTypeInfo* toWrapperTypeInfo(v8::Handle<v8::Object> wrapper)
182     {
183         return getInternalField<WrapperTypeInfo, v8DOMWrapperTypeIndex>(wrapper);
184     }
185
186     inline const PersistentNode* toPersistentHandle(const v8::Handle<v8::Object>& wrapper)
187     {
188         // Persistent handle is stored in the last internal field.
189         return static_cast<PersistentNode*>(wrapper->GetAlignedPointerFromInternalField(wrapper->InternalFieldCount() - 1));
190     }
191
192     inline void releaseObject(v8::Handle<v8::Object> wrapper)
193     {
194         const WrapperTypeInfo* typeInfo = toWrapperTypeInfo(wrapper);
195 #if ENABLE(OILPAN)
196         if (typeInfo->isGarbageCollected) {
197             const PersistentNode* handle = toPersistentHandle(wrapper);
198             ASSERT(handle);
199             delete handle;
200         } else {
201             ASSERT(typeInfo->derefObjectFunction);
202             typeInfo->derefObjectFunction(toNative(wrapper));
203         }
204 #else
205         ASSERT(typeInfo->derefObjectFunction);
206         typeInfo->derefObjectFunction(toNative(wrapper));
207 #endif
208     }
209
210     struct WrapperConfiguration {
211
212         enum Lifetime {
213             Dependent, Independent
214         };
215
216         void configureWrapper(v8::Persistent<v8::Object>* wrapper) const
217         {
218             wrapper->SetWrapperClassId(classId);
219             if (lifetime == Independent)
220                 wrapper->MarkIndependent();
221         }
222
223         const uint16_t classId;
224         const Lifetime lifetime;
225     };
226
227     inline WrapperConfiguration buildWrapperConfiguration(void*, WrapperConfiguration::Lifetime lifetime)
228     {
229         WrapperConfiguration configuration = {v8DOMObjectClassId, lifetime};
230         return configuration;
231     }
232
233     inline WrapperConfiguration buildWrapperConfiguration(Node*, WrapperConfiguration::Lifetime lifetime)
234     {
235         WrapperConfiguration configuration = {v8DOMNodeClassId, lifetime};
236         return configuration;
237     }
238
239     template<class ElementType>
240     class WrapperTypeTraits {
241         // specialized classes have thier own functions, which are generated by binding generator.
242     };
243 }
244
245 #endif // WrapperTypeInfo_h