[V8] We can merge the wrapper maps for DOM objects and active DOM objects
[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 "DOMWrapperWorld.h"
36 #include "Event.h"
37 #include "Node.h"
38 #include "NodeFilter.h"
39 #include "V8CustomXPathNSResolver.h"
40 #include "V8DOMMap.h"
41 #include "V8DOMWindowShell.h"
42 #include "V8Utilities.h"
43 #include "WrapperTypeInfo.h"
44 #include <v8.h>
45 #include <wtf/MainThread.h>
46 #include <wtf/PassRefPtr.h>
47 #include <wtf/text/WTFString.h>
48
49 namespace WebCore {
50
51     class DOMWindow;
52     class EventTarget;
53     class Frame;
54     class Node;
55     class V8PerContextData;
56     class WorkerContext;
57
58     enum ListenerLookupType {
59         ListenerFindOnly,
60         ListenerFindOrCreate,
61     };
62
63     class V8DOMWrapper {
64     public:
65 #ifndef NDEBUG
66         // Checks if a v8 value can be a DOM wrapper
67         static bool maybeDOMWrapper(v8::Handle<v8::Value>);
68 #endif
69
70         // Sets contents of a DOM wrapper.
71         static void setDOMWrapper(v8::Handle<v8::Object> object, WrapperTypeInfo* type, void* cptr)
72         {
73             ASSERT(object->InternalFieldCount() >= 2);
74             object->SetPointerInInternalField(v8DOMWrapperObjectIndex, cptr);
75             object->SetPointerInInternalField(v8DOMWrapperTypeIndex, type);
76         }
77
78         static v8::Handle<v8::Object> lookupDOMWrapper(v8::Handle<v8::FunctionTemplate> functionTemplate, v8::Handle<v8::Object> object)
79         {
80             return object.IsEmpty() ? object : object->FindInstanceInPrototypeChain(functionTemplate);
81         }
82
83         static WrapperTypeInfo* domWrapperType(v8::Handle<v8::Object>);
84
85         static v8::Handle<v8::Value> convertEventTargetToV8Object(PassRefPtr<EventTarget> eventTarget, v8::Handle<v8::Object> creationContext = v8::Handle<v8::Object>(), v8::Isolate* isolate = 0)
86         {
87             return convertEventTargetToV8Object(eventTarget.get(), creationContext, isolate);
88         }
89
90         static v8::Handle<v8::Value> convertEventTargetToV8Object(EventTarget*, v8::Handle<v8::Object> creationContext = v8::Handle<v8::Object>(), v8::Isolate* = 0);
91
92         static PassRefPtr<EventListener> getEventListener(v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup);
93
94         // Wrap JS node filter in C++.
95         static PassRefPtr<NodeFilter> wrapNativeNodeFilter(v8::Handle<v8::Value>);
96
97         template<typename T>
98         static v8::Persistent<v8::Object> setJSWrapperForDOMObject(PassRefPtr<T>, v8::Handle<v8::Object>, v8::Isolate* = 0);
99         static v8::Persistent<v8::Object> setJSWrapperForDOMNode(PassRefPtr<Node>, v8::Handle<v8::Object>, v8::Isolate* = 0);
100
101         static bool isValidDOMObject(v8::Handle<v8::Value>);
102
103         // Check whether a V8 value is a wrapper of type |classType|.
104         static bool isWrapperOfType(v8::Handle<v8::Value>, WrapperTypeInfo*);
105
106         static void setNamedHiddenReference(v8::Handle<v8::Object> parent, const char* name, v8::Handle<v8::Value> child);
107
108         static v8::Local<v8::Object> instantiateV8Object(Document*, WrapperTypeInfo*, void*);
109
110         static v8::Handle<v8::Object> getCachedWrapper(Node* node)
111         {
112             ASSERT(isMainThread());
113             if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) {
114                 v8::Persistent<v8::Object> wrapper = node->wrapper();
115                 if (LIKELY(!wrapper.IsEmpty()))
116                     return wrapper;
117             }
118
119             V8DOMWindowShell* context = V8DOMWindowShell::getEntered();
120             if (LIKELY(!context))
121                 return node->wrapper();
122
123             DOMDataStore* store = context->world()->domDataStore();
124             DOMWrapperMap<Node>& domNodeMap = store->domNodeMap();
125             return domNodeMap.get(node);
126         }
127     };
128
129     template<typename T>
130     v8::Persistent<v8::Object> V8DOMWrapper::setJSWrapperForDOMObject(PassRefPtr<T> object, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate)
131     {
132         v8::Persistent<v8::Object> wrapperHandle = v8::Persistent<v8::Object>::New(wrapper);
133         ASSERT(maybeDOMWrapper(wrapperHandle));
134         ASSERT(!domWrapperType(wrapperHandle)->toActiveDOMObjectFunction);
135         getDOMObjectMap(isolate).set(object.leakRef(), wrapperHandle);
136         return wrapperHandle;
137     }
138
139 }
140
141 #endif // V8DOMWrapper_h