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