Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / V8DOMWrapper.h
1 /*
2  * Copyright (C) 2009 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 V8DOMWrapper_h
32 #define V8DOMWrapper_h
33
34 #include "bindings/v8/DOMDataStore.h"
35 #include <v8.h>
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/RawPtr.h"
38 #include "wtf/text/AtomicString.h"
39
40 namespace WebCore {
41
42 struct WrapperTypeInfo;
43
44     class V8DOMWrapper {
45     public:
46         static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationContext, const WrapperTypeInfo*, void*, v8::Isolate*);
47
48         template<typename V8T, typename T>
49         static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
50         template<typename V8T, typename T>
51         static inline v8::Handle<v8::Object> associateObjectWithWrapper(RawPtr<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
52         {
53             return associateObjectWithWrapper<V8T, T>(object.get(), wrapperTypeInfo, wrapper, isolate, lifetime);
54         }
55         template<typename V8T, typename T>
56         static inline v8::Handle<v8::Object> associateObjectWithWrapper(T*, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
57         static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*);
58         static inline void setNativeInfoForHiddenWrapper(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*);
59         static inline void setNativeInfoWithPersistentHandle(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*, PersistentNode*);
60         static inline void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*);
61
62         static bool isDOMWrapper(v8::Handle<v8::Value>);
63     };
64
65     inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, void* object)
66     {
67         ASSERT(wrapper->InternalFieldCount() >= 2);
68         ASSERT(object);
69         ASSERT(wrapperTypeInfo);
70 #if ENABLE(OILPAN)
71         ASSERT(wrapperTypeInfo->gcType == RefCountedObject);
72 #else
73         ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcType == WillBeGarbageCollectedObject);
74 #endif
75         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
76         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
77     }
78
79     inline void V8DOMWrapper::setNativeInfoForHiddenWrapper(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, void* object)
80     {
81         // see V8WindowShell::installDOMWindow() comment for why this version is needed and safe.
82         ASSERT(wrapper->InternalFieldCount() >= 2);
83         ASSERT(object);
84         ASSERT(wrapperTypeInfo);
85 #if ENABLE(OILPAN)
86         ASSERT(wrapperTypeInfo->gcType != RefCountedObject);
87 #else
88         ASSERT(wrapperTypeInfo->gcType == RefCountedObject || wrapperTypeInfo->gcType == WillBeGarbageCollectedObject);
89 #endif
90         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
91         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
92         // Clear out the last internal field, which is assumed to contain a valid persistent pointer value.
93         wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, 0);
94     }
95
96     inline void V8DOMWrapper::setNativeInfoWithPersistentHandle(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo, void* object, PersistentNode* handle)
97     {
98         ASSERT(wrapper->InternalFieldCount() >= 3);
99         ASSERT(object);
100         ASSERT(wrapperTypeInfo);
101         ASSERT(wrapperTypeInfo->gcType != RefCountedObject);
102         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
103         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
104         // Persistent handle is stored in the last internal field.
105         wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, handle);
106     }
107
108     inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo)
109     {
110         ASSERT(wrapper->InternalFieldCount() >= 2);
111         ASSERT(wrapperTypeInfo);
112         // clearNativeInfo() is used only by NP objects, which are not garbage collected.
113         ASSERT(wrapperTypeInfo->gcType == RefCountedObject);
114         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(wrapperTypeInfo));
115         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
116     }
117
118     template<typename V8T, typename T>
119     inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
120     {
121         setNativeInfo(wrapper, wrapperTypeInfo, V8T::toInternalPointer(object.get()));
122         ASSERT(isDOMWrapper(wrapper));
123         WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
124         DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, configuration);
125         return wrapper;
126     }
127
128     template<typename V8T, typename T>
129     inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(T* object, const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
130     {
131         setNativeInfoWithPersistentHandle(wrapper, wrapperTypeInfo, V8T::toInternalPointer(object), new Persistent<T>(object));
132         ASSERT(isDOMWrapper(wrapper));
133         WrapperConfiguration configuration = buildWrapperConfiguration(object, lifetime);
134         DOMDataStore::setWrapper<V8T>(object, wrapper, isolate, configuration);
135         return wrapper;
136     }
137
138     class V8WrapperInstantiationScope {
139     public:
140         V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
141             : m_didEnterContext(false)
142             , m_context(isolate->GetCurrentContext())
143         {
144             // FIXME: Remove all empty creationContexts from caller sites.
145             // If a creationContext is empty, we will end up creating a new object
146             // in the context currently entered. This is wrong.
147             if (creationContext.IsEmpty())
148                 return;
149             v8::Handle<v8::Context> contextForWrapper = creationContext->CreationContext();
150             // For performance, we enter the context only if the currently running context
151             // is different from the context that we are about to enter.
152             if (contextForWrapper == m_context)
153                 return;
154             m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper);
155             m_didEnterContext = true;
156             m_context->Enter();
157         }
158
159         ~V8WrapperInstantiationScope()
160         {
161             if (!m_didEnterContext)
162                 return;
163             m_context->Exit();
164         }
165
166         v8::Handle<v8::Context> context() const { return m_context; }
167
168     private:
169         bool m_didEnterContext;
170         v8::Handle<v8::Context> m_context;
171     };
172
173 }
174
175 #endif // V8DOMWrapper_h