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