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 "Document.h" 35 #include "Event.h" 36 #include "Node.h" 37 #include "NodeFilter.h" 38 #include "PlatformString.h" // for WebCore::String 39 #include "V8CustomXPathNSResolver.h" 40 #include "V8DOMMap.h" 41 #include "V8Event.h" 42 #include "V8Index.h" 43 #include "V8Utilities.h" 44 #include "V8XPathNSResolver.h" 45 #include "XPathNSResolver.h" 46 #include <v8.h> 47 48 namespace WebCore { 49 50 // FIXME: This probably aren't all needed. 51 class CSSRule; 52 class CSSRuleList; 53 class CSSStyleDeclaration; 54 class CSSValue; 55 class CSSValueList; 56 class ClientRectList; 57 class DOMImplementation; 58 class DOMWindow; 59 class Document; 60 class Element; 61 class Event; 62 class EventListener; 63 class EventTarget; 64 class Frame; 65 class HTMLCollection; 66 class HTMLDocument; 67 class HTMLElement; 68 class HTMLOptionsCollection; 69 class MediaList; 70 class MimeType; 71 class MimeTypeArray; 72 class NamedNodeMap; 73 class Navigator; 74 class Node; 75 class NodeFilter; 76 class NodeList; 77 class Plugin; 78 class PluginArray; 79 class SVGElement; 80 #if ENABLE(SVG) 81 class SVGElementInstance; 82 #endif 83 class Screen; 84 class ScriptExecutionContext; 85 #if ENABLE(DOM_STORAGE) 86 class Storage; 87 class StorageEvent; 88 #endif 89 class String; 90 class StyleSheet; 91 class StyleSheetList; 92 class V8EventListener; 93 class V8ObjectEventListener; 94 class V8Proxy; 95 #if ENABLE(WEB_SOCKETS) 96 class WebSocket; 97 #endif 98 class WorkerContext; 99 100 enum ListenerLookupType { 101 ListenerFindOnly, 102 ListenerFindOrCreate, 103 }; 104 105 class V8DOMWrapper { 106 public: 107 #ifndef NDEBUG 108 // Checks if a v8 value can be a DOM wrapper 109 static bool maybeDOMWrapper(v8::Handle<v8::Value>); 110 #endif 111 112 // Sets contents of a DOM wrapper. 113 static void setDOMWrapper(v8::Handle<v8::Object> object, int type, void* cptr) 114 { 115 ASSERT(object->InternalFieldCount() >= 2); 116 object->SetPointerInInternalField(v8DOMWrapperObjectIndex, cptr); 117 object->SetInternalField(v8DOMWrapperTypeIndex, v8::Integer::New(type)); 118 } 119 120 static v8::Handle<v8::Object> lookupDOMWrapper(v8::Handle<v8::FunctionTemplate> functionTemplate, v8::Handle<v8::Object> object) 121 { 122 return object.IsEmpty() ? object : object->FindInstanceInPrototypeChain(functionTemplate); 123 } 124 125 static V8ClassIndex::V8WrapperType domWrapperType(v8::Handle<v8::Object>); 126 127 static v8::Handle<v8::Value> convertEventTargetToV8Object(PassRefPtr<EventTarget> eventTarget) 128 { 129 return convertEventTargetToV8Object(eventTarget.get()); 130 } 131 132 static v8::Handle<v8::Value> convertEventTargetToV8Object(EventTarget*); 133 134 static PassRefPtr<EventListener> getEventListener(Node* node, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 135 136 static PassRefPtr<EventListener> getEventListener(SVGElementInstance* element, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 137 138 static PassRefPtr<EventListener> getEventListener(AbstractWorker* worker, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 139 140 #if ENABLE(NOTIFICATIONS) 141 static PassRefPtr<EventListener> getEventListener(Notification* notification, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 142 #endif 143 144 static PassRefPtr<EventListener> getEventListener(WorkerContext* workerContext, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 145 146 static PassRefPtr<EventListener> getEventListener(XMLHttpRequestUpload* upload, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 147 148 #if ENABLE(EVENTSOURCE) 149 static PassRefPtr<EventListener> getEventListener(EventSource* eventTarget, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 150 #endif 151 152 static PassRefPtr<EventListener> getEventListener(EventTarget* eventTarget, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 153 154 static PassRefPtr<EventListener> getEventListener(V8Proxy* proxy, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); 155 156 #if ENABLE(XPATH) 157 // XPath-related utilities 158 static RefPtr<XPathNSResolver> getXPathNSResolver(v8::Handle<v8::Value> value, V8Proxy* proxy = 0) 159 { 160 RefPtr<XPathNSResolver> resolver; 161 if (V8XPathNSResolver::HasInstance(value)) 162 resolver = V8XPathNSResolver::toNative(v8::Handle<v8::Object>::Cast(value)); 163 else if (value->IsObject()) 164 resolver = V8CustomXPathNSResolver::create(proxy, value->ToObject()); 165 return resolver; 166 } 167 #endif 168 169 // Wrap JS node filter in C++. 170 static PassRefPtr<NodeFilter> wrapNativeNodeFilter(v8::Handle<v8::Value>); 171 172 static v8::Local<v8::Function> getConstructorForContext(V8ClassIndex::V8WrapperType, v8::Handle<v8::Context>); 173 static v8::Local<v8::Function> getConstructor(V8ClassIndex::V8WrapperType, v8::Handle<v8::Value> objectPrototype); 174 static v8::Local<v8::Function> getConstructor(V8ClassIndex::V8WrapperType, DOMWindow*); 175 static v8::Local<v8::Function> getConstructor(V8ClassIndex::V8WrapperType, WorkerContext*); 176 177 // Set JS wrapper of a DOM object, the caller in charge of increase ref. 178 static void setJSWrapperForDOMObject(void*, v8::Persistent<v8::Object>); 179 static void setJSWrapperForActiveDOMObject(void*, v8::Persistent<v8::Object>); 180 static void setJSWrapperForDOMNode(Node*, v8::Persistent<v8::Object>); 181 182 // Check whether a V8 value is a wrapper of type |classType|. 183 static bool isWrapperOfType(v8::Handle<v8::Value>, V8ClassIndex::V8WrapperType); 184 185 #if ENABLE(3D_CANVAS) 186 static void setIndexedPropertiesToExternalArray(v8::Handle<v8::Object>, 187 int, 188 void*, 189 int); 190 #endif 191 // Set hidden references in a DOMWindow object of a frame. 192 static void setHiddenWindowReference(Frame*, const int internalIndex, v8::Handle<v8::Object>); 193 194 static v8::Local<v8::Object> instantiateV8Object(V8Proxy* proxy, V8ClassIndex::V8WrapperType type, void* impl); 195 196 static v8::Handle<v8::Object> getWrapper(Node*); 197 }; 198 199 } 200 201 #endif // V8DOMWrapper_h 202