Upstream version 5.34.104.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>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
52         template<typename V8T, typename T>
53         static inline v8::Handle<v8::Object> associateObjectWithWrapper(T*, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
54         static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*);
55         static inline void setNativeInfoWithPersistentHandle(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*, PersistentNode*);
56         static inline void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*);
57
58         static bool isDOMWrapper(v8::Handle<v8::Value>);
59     };
60
61     inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type, void* object)
62     {
63         ASSERT(wrapper->InternalFieldCount() >= 2);
64         ASSERT(object);
65         ASSERT(type);
66 #if ENABLE(OILPAN)
67         ASSERT(!type->isGarbageCollected);
68 #endif
69         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
70         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
71     }
72
73     inline void V8DOMWrapper::setNativeInfoWithPersistentHandle(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type, void* object, PersistentNode* handle)
74     {
75         ASSERT(wrapper->InternalFieldCount() >= 3);
76         ASSERT(object);
77         ASSERT(type);
78         ASSERT(type->isGarbageCollected);
79         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
80         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
81         // Persistent handle is stored in the last internal field.
82         wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, handle);
83     }
84
85     inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type)
86     {
87         ASSERT(wrapper->InternalFieldCount() >= 2);
88         ASSERT(type);
89         // clearNativeInfo() is used only by NP objects, which are not garbage collected.
90         ASSERT(!type->isGarbageCollected);
91         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
92         wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
93     }
94
95     template<typename V8T, typename T>
96     inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, const WrapperTypeInfo* type, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
97     {
98         setNativeInfo(wrapper, type, V8T::toInternalPointer(object.get()));
99         ASSERT(isDOMWrapper(wrapper));
100         WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
101         DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, configuration);
102         return wrapper;
103     }
104
105     template<typename V8T, typename T>
106     inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(RawPtr<T> object, const WrapperTypeInfo* type, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
107     {
108         setNativeInfoWithPersistentHandle(wrapper, type, V8T::toInternalPointer(object.get()), new Persistent<T>(object));
109         ASSERT(isDOMWrapper(wrapper));
110         WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
111         DOMDataStore::setWrapper<V8T>(object.get(), wrapper, isolate, configuration);
112         return wrapper;
113     }
114
115     class V8WrapperInstantiationScope {
116     public:
117         V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
118             : m_didEnterContext(false)
119             , m_context(isolate->GetCurrentContext())
120         {
121             // FIXME: Remove all empty creationContexts from caller sites.
122             // If a creationContext is empty, we will end up creating a new object
123             // in the context currently entered. This is wrong.
124             if (creationContext.IsEmpty())
125                 return;
126             v8::Handle<v8::Context> contextForWrapper = creationContext->CreationContext();
127             // For performance, we enter the context only if the currently running context
128             // is different from the context that we are about to enter.
129             if (contextForWrapper == m_context)
130                 return;
131             m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper);
132             m_didEnterContext = true;
133             m_context->Enter();
134         }
135
136         ~V8WrapperInstantiationScope()
137         {
138             if (!m_didEnterContext)
139                 return;
140             m_context->Exit();
141         }
142
143         v8::Handle<v8::Context> context() const { return m_context; }
144
145     private:
146         bool m_didEnterContext;
147         v8::Handle<v8::Context> m_context;
148     };
149
150 }
151
152 #endif // V8DOMWrapper_h