4505525c7a85d5852b8ee9227da40c7199684508
[WebKit-https.git] / Source / WebCore / 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 "DOMDataStore.h"
35 #include "Event.h"
36 #include "IsolatedWorld.h"
37 #include "Node.h"
38 #include "NodeFilter.h"
39 #include "PlatformString.h"
40 #include "V8CustomXPathNSResolver.h"
41 #include "V8DOMMap.h"
42 #include "V8Event.h"
43 #include "V8IsolatedContext.h"
44 #include "V8Utilities.h"
45 #include "V8XPathNSResolver.h"
46 #include "WrapperTypeInfo.h"
47 #include "XPathNSResolver.h"
48 #include <v8.h>
49 #include <wtf/MainThread.h>
50 #include <wtf/PassRefPtr.h>
51
52 namespace WebCore {
53
54     class DOMWindow;
55     class EventTarget;
56     class Frame;
57     class SVGElementInstance;
58     class V8Proxy;
59     class WorkerContext;
60
61     enum ListenerLookupType {
62         ListenerFindOnly,
63         ListenerFindOrCreate,
64     };
65
66     class V8DOMWrapper {
67     public:
68 #ifndef NDEBUG
69         // Checks if a v8 value can be a DOM wrapper
70         static bool maybeDOMWrapper(v8::Handle<v8::Value>);
71 #endif
72
73         // Sets contents of a DOM wrapper.
74         static void setDOMWrapper(v8::Handle<v8::Object> object, WrapperTypeInfo* type, void* cptr)
75         {
76             ASSERT(object->InternalFieldCount() >= 2);
77             object->SetPointerInInternalField(v8DOMWrapperObjectIndex, cptr);
78             object->SetPointerInInternalField(v8DOMWrapperTypeIndex, type);
79         }
80
81         static v8::Handle<v8::Object> lookupDOMWrapper(v8::Handle<v8::FunctionTemplate> functionTemplate, v8::Handle<v8::Object> object)
82         {
83             return object.IsEmpty() ? object : object->FindInstanceInPrototypeChain(functionTemplate);
84         }
85
86         static WrapperTypeInfo* domWrapperType(v8::Handle<v8::Object>);
87
88         static v8::Handle<v8::Value> convertEventTargetToV8Object(PassRefPtr<EventTarget> eventTarget)
89         {
90             return convertEventTargetToV8Object(eventTarget.get());
91         }
92
93         static v8::Handle<v8::Value> convertEventTargetToV8Object(EventTarget*);
94
95         static PassRefPtr<EventListener> getEventListener(v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup);
96
97         // XPath-related utilities
98         static RefPtr<XPathNSResolver> getXPathNSResolver(v8::Handle<v8::Value> value, V8Proxy* proxy = 0);
99
100         // Wrap JS node filter in C++.
101         static PassRefPtr<NodeFilter> wrapNativeNodeFilter(v8::Handle<v8::Value>);
102
103         static v8::Local<v8::Function> getConstructorForContext(WrapperTypeInfo*, v8::Handle<v8::Context>);
104         static v8::Local<v8::Function> getConstructor(WrapperTypeInfo*, v8::Handle<v8::Value> objectPrototype);
105         static v8::Local<v8::Function> getConstructor(WrapperTypeInfo*, DOMWindow*);
106 #if ENABLE(WORKERS)
107         static v8::Local<v8::Function> getConstructor(WrapperTypeInfo*, WorkerContext*);
108 #endif
109
110         template<typename T> static void setJSWrapperForDOMObject(PassRefPtr<T>, v8::Persistent<v8::Object>);
111         template<typename T> static void setJSWrapperForActiveDOMObject(PassRefPtr<T>, v8::Persistent<v8::Object>);
112         static void setJSWrapperForDOMNode(PassRefPtr<Node>, v8::Persistent<v8::Object>);
113         static void setJSWrapperForActiveDOMNode(PassRefPtr<Node>, v8::Persistent<v8::Object>);
114 #if ENABLE(SVG)
115         static void setJSWrapperForDOMSVGElementInstance(PassRefPtr<SVGElementInstance>, v8::Persistent<v8::Object>);
116 #endif
117
118         static bool isValidDOMObject(v8::Handle<v8::Value>);
119
120         // Check whether a V8 value is a wrapper of type |classType|.
121         static bool isWrapperOfType(v8::Handle<v8::Value>, WrapperTypeInfo*);
122
123         // Proper object lifetime support.
124         //
125         // Helper functions to make sure the child object stays alive
126         // while the parent is alive. Using the name more than once
127         // overwrites previous references making it possible to free
128         // old children.
129         static void setNamedHiddenReference(v8::Handle<v8::Object> parent, const char* name, v8::Handle<v8::Value> child);
130         static void setNamedHiddenWindowReference(Frame*, const char*, v8::Handle<v8::Value>);
131
132         static v8::Local<v8::Object> instantiateV8Object(V8Proxy* proxy, WrapperTypeInfo*, void* impl);
133
134         static v8::Handle<v8::Object> getCachedWrapper(Node* node)
135         {
136             ASSERT(isMainThread());
137             if (LIKELY(!IsolatedWorld::count())) {
138                 v8::Persistent<v8::Object>* wrapper = node->wrapper();
139                 if (LIKELY(!!wrapper))
140                     return *wrapper;
141             }
142
143             V8IsolatedContext* context = V8IsolatedContext::getEntered();
144             if (LIKELY(!context)) {
145                 v8::Persistent<v8::Object>* wrapper = node->wrapper();
146                 if (!wrapper)
147                     return v8::Handle<v8::Object>();
148                 return *wrapper;
149             }
150             DOMDataStore* store = context->world()->domDataStore();
151             DOMNodeMapping& domNodeMap = node->isActiveNode() ? store->activeDomNodeMap() : store->domNodeMap();
152             return domNodeMap.get(node);
153         }
154     };
155
156     template<typename T>
157     inline void V8DOMWrapper::setJSWrapperForDOMObject(PassRefPtr<T> object, v8::Persistent<v8::Object> wrapper)
158     {
159         ASSERT(maybeDOMWrapper(wrapper));
160         ASSERT(!domWrapperType(wrapper)->toActiveDOMObjectFunction);
161         getDOMObjectMap().set(object.leakRef(), wrapper);
162     }
163
164     template<typename T>
165     inline void V8DOMWrapper::setJSWrapperForActiveDOMObject(PassRefPtr<T> object, v8::Persistent<v8::Object> wrapper)
166     {
167         ASSERT(maybeDOMWrapper(wrapper));
168         ASSERT(domWrapperType(wrapper)->toActiveDOMObjectFunction);
169         getActiveDOMObjectMap().set(object.leakRef(), wrapper);
170     }
171
172     inline void V8DOMWrapper::setJSWrapperForDOMNode(PassRefPtr<Node> node, v8::Persistent<v8::Object> wrapper)
173     {
174         ASSERT(maybeDOMWrapper(wrapper));
175         ASSERT(!domWrapperType(wrapper)->toActiveDOMObjectFunction);
176         ASSERT(!node->isActiveNode());
177         getDOMNodeMap().set(node.leakRef(), wrapper);
178     }
179
180     inline void V8DOMWrapper::setJSWrapperForActiveDOMNode(PassRefPtr<Node> node, v8::Persistent<v8::Object> wrapper)
181     {
182         ASSERT(maybeDOMWrapper(wrapper));
183         ASSERT(domWrapperType(wrapper)->toActiveDOMObjectFunction);
184         ASSERT(node->isActiveNode());
185         getActiveDOMNodeMap().set(node.leakRef(), wrapper);
186     }
187
188 #if ENABLE(SVG)
189     inline void V8DOMWrapper::setJSWrapperForDOMSVGElementInstance(PassRefPtr<SVGElementInstance> element, v8::Persistent<v8::Object> wrapper)
190     {
191         ASSERT(maybeDOMWrapper(wrapper));
192         getDOMSVGElementInstanceMap().set(element.leakRef(), wrapper);
193     }
194 #endif
195
196 } // namespace WebCore
197
198 #endif // V8DOMWrapper_h