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