2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "V8DOMWrapper.h"
34 #include "ArrayBufferView.h"
35 #include "CSSMutableStyleDeclaration.h"
36 #include "DOMDataStore.h"
37 #include "DocumentLoader.h"
38 #include "EventTargetHeaders.h"
39 #include "EventTargetInterfaces.h"
40 #include "FrameLoaderClient.h"
41 #include "V8AbstractEventListener.h"
42 #include "V8Binding.h"
43 #include "V8Collection.h"
45 #include "V8EventListener.h"
46 #include "V8EventListenerList.h"
47 #include "V8HTMLCollection.h"
48 #include "V8HTMLDocument.h"
49 #include "V8HiddenPropertyName.h"
50 #include "V8IsolatedContext.h"
51 #include "V8Location.h"
52 #include "V8NamedNodeMap.h"
53 #include "V8NodeFilterCondition.h"
54 #include "V8NodeList.h"
56 #include "V8StyleSheet.h"
57 #include "V8WorkerContextEventListener.h"
58 #include "WebGLContextAttributes.h"
59 #include "WebGLUniformLocation.h"
60 #include "WorkerContextExecutionProxy.h"
61 #include "WrapperTypeInfo.h"
65 #include <wtf/Assertions.h>
66 #include <wtf/OwnArrayPtr.h>
67 #include <wtf/StdLibExtras.h>
68 #include <wtf/UnusedParam.h>
72 typedef HashMap<Node*, v8::Object*> DOMNodeMap;
73 typedef HashMap<void*, v8::Object*> DOMObjectMap;
75 // The caller must have increased obj's ref count.
76 void V8DOMWrapper::setJSWrapperForDOMObject(void* object, v8::Persistent<v8::Object> wrapper)
78 ASSERT(V8DOMWrapper::maybeDOMWrapper(wrapper));
79 ASSERT(!domWrapperType(wrapper)->toActiveDOMObjectFunction);
80 getDOMObjectMap().set(object, wrapper);
83 // The caller must have increased obj's ref count.
84 void V8DOMWrapper::setJSWrapperForActiveDOMObject(void* object, v8::Persistent<v8::Object> wrapper)
86 ASSERT(V8DOMWrapper::maybeDOMWrapper(wrapper));
87 ASSERT(domWrapperType(wrapper)->toActiveDOMObjectFunction);
88 getActiveDOMObjectMap().set(object, wrapper);
91 // The caller must have increased node's ref count.
92 void V8DOMWrapper::setJSWrapperForDOMNode(Node* node, v8::Persistent<v8::Object> wrapper)
94 ASSERT(V8DOMWrapper::maybeDOMWrapper(wrapper));
95 if (node->isActiveNode())
96 getActiveDOMNodeMap().set(node, wrapper);
98 getDOMNodeMap().set(node, wrapper);
101 v8::Local<v8::Function> V8DOMWrapper::getConstructor(WrapperTypeInfo* type, v8::Handle<v8::Value> objectPrototype)
103 // A DOM constructor is a function instance created from a DOM constructor
104 // template. There is one instance per context. A DOM constructor is
105 // different from a normal function in two ways:
106 // 1) it cannot be called as constructor (aka, used to create a DOM object)
107 // 2) its __proto__ points to Object.prototype rather than
108 // Function.prototype.
109 // The reason for 2) is that, in Safari, a DOM constructor is a normal JS
110 // object, but not a function. Hotmail relies on the fact that, in Safari,
111 // HTMLElement.__proto__ == Object.prototype.
112 v8::Handle<v8::FunctionTemplate> functionTemplate = type->getTemplate();
113 // Getting the function might fail if we're running out of
115 v8::TryCatch tryCatch;
116 v8::Local<v8::Function> value = functionTemplate->GetFunction();
118 return v8::Local<v8::Function>();
119 // Hotmail fix, see comments above.
120 if (!objectPrototype.IsEmpty())
121 value->SetPrototype(objectPrototype);
125 v8::Local<v8::Function> V8DOMWrapper::getConstructorForContext(WrapperTypeInfo* type, v8::Handle<v8::Context> context)
127 // Enter the scope for this context to get the correct constructor.
128 v8::Context::Scope scope(context);
130 return getConstructor(type, V8DOMWindowShell::getHiddenObjectPrototype(context));
133 v8::Local<v8::Function> V8DOMWrapper::getConstructor(WrapperTypeInfo* type, DOMWindow* window)
135 Frame* frame = window->frame();
137 return v8::Local<v8::Function>();
139 v8::Handle<v8::Context> context = V8Proxy::context(frame);
140 if (context.IsEmpty())
141 return v8::Local<v8::Function>();
143 return getConstructorForContext(type, context);
147 v8::Local<v8::Function> V8DOMWrapper::getConstructor(WrapperTypeInfo* type, WorkerContext*)
149 WorkerScriptController* controller = WorkerScriptController::controllerForContext();
150 WorkerContextExecutionProxy* proxy = controller ? controller->proxy() : 0;
152 return v8::Local<v8::Function>();
154 v8::Handle<v8::Context> context = proxy->context();
155 if (context.IsEmpty())
156 return v8::Local<v8::Function>();
158 return getConstructorForContext(type, context);
163 void V8DOMWrapper::setNamedHiddenReference(v8::Handle<v8::Object> parent, const char* name, v8::Handle<v8::Value> child)
165 parent->SetHiddenValue(V8HiddenPropertyName::hiddenReferenceName(name), child);
168 void V8DOMWrapper::setNamedHiddenWindowReference(Frame* frame, const char* name, v8::Handle<v8::Value> jsObject)
172 return; // Object might be detached from window
173 v8::Handle<v8::Context> context = V8Proxy::context(frame);
174 if (context.IsEmpty())
177 v8::Handle<v8::Object> global = context->Global();
178 // Look for real DOM wrapper.
179 global = V8DOMWrapper::lookupDOMWrapper(V8DOMWindow::GetTemplate(), global);
180 ASSERT(!global.IsEmpty());
182 setNamedHiddenReference(global, name, jsObject);
185 WrapperTypeInfo* V8DOMWrapper::domWrapperType(v8::Handle<v8::Object> object)
187 ASSERT(V8DOMWrapper::maybeDOMWrapper(object));
188 return static_cast<WrapperTypeInfo*>(object->GetPointerFromInternalField(v8DOMWrapperTypeIndex));
191 PassRefPtr<NodeFilter> V8DOMWrapper::wrapNativeNodeFilter(v8::Handle<v8::Value> filter)
193 // A NodeFilter is used when walking through a DOM tree or iterating tree
195 // FIXME: we may want to cache NodeFilterCondition and NodeFilter
196 // object, but it is minor.
197 // NodeFilter is passed to NodeIterator that has a ref counted pointer
198 // to NodeFilter. NodeFilter has a ref counted pointer to NodeFilterCondition.
199 // In NodeFilterCondition, filter object is persisted in its constructor,
200 // and disposed in its destructor.
201 return NodeFilter::create(V8NodeFilterCondition::create(filter));
204 v8::Local<v8::Object> V8DOMWrapper::instantiateV8Object(V8Proxy* proxy, WrapperTypeInfo* type, void* impl)
207 WorkerContext* workerContext = 0;
209 if (V8IsolatedContext::getEntered()) {
210 // This effectively disables the wrapper cache for isolated worlds.
212 // FIXME: Do we need a wrapper cache for the isolated world? We should
213 // see if the performance gains are worth while.
214 // We'll get one once we give the isolated context a proper window shell.
216 v8::Handle<v8::Context> context = v8::Context::GetCurrent();
217 if (!context.IsEmpty()) {
218 v8::Handle<v8::Object> globalPrototype = v8::Handle<v8::Object>::Cast(context->Global()->GetPrototype());
219 if (isWrapperOfType(globalPrototype, &V8DOMWindow::info))
220 proxy = V8Proxy::retrieve(V8DOMWindow::toNative(globalPrototype)->frame());
223 workerContext = V8WorkerContext::toNative(lookupDOMWrapper(V8WorkerContext::GetTemplate(), context->Global()));
228 v8::Local<v8::Object> instance;
230 // FIXME: Fix this to work properly with isolated worlds (see above).
231 instance = proxy->windowShell()->createWrapperFromCache(type);
233 v8::Local<v8::Function> function;
236 function = getConstructor(type, workerContext);
239 function = type->getTemplate()->GetFunction();
240 instance = SafeAllocation::newInstance(function);
242 if (!instance.IsEmpty()) {
243 // Avoid setting the DOM wrapper for failed allocations.
244 setDOMWrapper(instance, type, impl);
245 if (type == &V8HTMLDocument::info)
246 instance = V8HTMLDocument::WrapInShadowObject(instance, static_cast<Node*>(impl));
252 bool V8DOMWrapper::maybeDOMWrapper(v8::Handle<v8::Value> value)
254 if (value.IsEmpty() || !value->IsObject())
257 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
258 if (!object->InternalFieldCount())
261 ASSERT(object->InternalFieldCount() >= v8DefaultWrapperInternalFieldCount);
263 v8::Handle<v8::Value> wrapper = object->GetInternalField(v8DOMWrapperObjectIndex);
264 ASSERT(wrapper->IsNumber() || wrapper->IsExternal());
270 bool V8DOMWrapper::isValidDOMObject(v8::Handle<v8::Value> value)
272 if (value.IsEmpty() || !value->IsObject())
274 return v8::Handle<v8::Object>::Cast(value)->InternalFieldCount();
277 bool V8DOMWrapper::isWrapperOfType(v8::Handle<v8::Value> value, WrapperTypeInfo* type)
279 if (!isValidDOMObject(value))
282 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
283 ASSERT(object->InternalFieldCount() >= v8DefaultWrapperInternalFieldCount);
285 v8::Handle<v8::Value> wrapper = object->GetInternalField(v8DOMWrapperObjectIndex);
286 ASSERT_UNUSED(wrapper, wrapper->IsNumber() || wrapper->IsExternal());
288 WrapperTypeInfo* typeInfo = static_cast<WrapperTypeInfo*>(object->GetPointerFromInternalField(v8DOMWrapperTypeIndex));
289 return typeInfo == type;
292 v8::Handle<v8::Object> V8DOMWrapper::getWrapperSlow(Node* node)
294 V8IsolatedContext* context = V8IsolatedContext::getEntered();
295 if (LIKELY(!context)) {
296 v8::Persistent<v8::Object>* wrapper = node->wrapper();
298 return v8::Handle<v8::Object>();
301 DOMDataStore* store = context->world()->domDataStore();
302 DOMNodeMapping& domNodeMap = node->isActiveNode() ? store->activeDomNodeMap() : store->domNodeMap();
303 return domNodeMap.get(node);
306 #define TRY_TO_WRAP_WITH_INTERFACE(interfaceName) \
307 if (eventNames().interfaceFor##interfaceName == desiredInterface) \
308 return toV8(static_cast<interfaceName*>(target));
310 // A JS object of type EventTarget is limited to a small number of possible classes.
311 v8::Handle<v8::Value> V8DOMWrapper::convertEventTargetToV8Object(EventTarget* target)
316 AtomicString desiredInterface = target->interfaceName();
317 DOM_EVENT_TARGET_INTERFACES_FOR_EACH(TRY_TO_WRAP_WITH_INTERFACE)
319 ASSERT_NOT_REACHED();
320 return notHandledByInterceptor();
323 PassRefPtr<EventListener> V8DOMWrapper::getEventListener(v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup)
325 v8::Handle<v8::Context> context = v8::Context::GetCurrent();
326 if (context.IsEmpty())
328 if (lookup == ListenerFindOnly)
329 return V8EventListenerList::findWrapper(value, isAttribute);
330 v8::Handle<v8::Object> globalPrototype = v8::Handle<v8::Object>::Cast(context->Global()->GetPrototype());
331 if (isWrapperOfType(globalPrototype, &V8DOMWindow::info))
332 return V8EventListenerList::findOrCreateWrapper<V8EventListener>(value, isAttribute);
334 return V8EventListenerList::findOrCreateWrapper<V8WorkerContextEventListener>(value, isAttribute);
340 // XPath-related utilities
341 RefPtr<XPathNSResolver> V8DOMWrapper::getXPathNSResolver(v8::Handle<v8::Value> value, V8Proxy* proxy)
343 RefPtr<XPathNSResolver> resolver;
344 if (V8XPathNSResolver::HasInstance(value))
345 resolver = V8XPathNSResolver::toNative(v8::Handle<v8::Object>::Cast(value));
346 else if (value->IsObject())
347 resolver = V8CustomXPathNSResolver::create(value->ToObject());
351 } // namespace WebCore