1 /* 2 * Copyright (C) 2008 Apple 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "JSEventTarget.h" 28 29 #include "DOMWindow.h" 30 #include "Document.h" 31 #include "JSDOMWindow.h" 32 #include "JSDOMWindowShell.h" 33 #include "JSEventListener.h" 34 #include "JSMessagePort.h" 35 #include "JSNode.h" 36 #if ENABLE(SHARED_WORKERS) 37 38 #include "JSSharedWorker.h" 39 #include "JSSharedWorkerContext.h" 40 #endif 41 42 #include "JSXMLHttpRequest.h" 43 #include "JSXMLHttpRequestUpload.h" 44 #include "MessagePort.h" 45 46 #if ENABLE(SHARED_WORKERS) 47 #include "SharedWorker.h" 48 #include "SharedWorkerContext.h" 49 #endif 50 51 #include "XMLHttpRequest.h" 52 #include "XMLHttpRequestUpload.h" 53 54 #if ENABLE(EVENTSOURCE) 55 #include "EventSource.h" 56 #include "JSEventSource.h" 57 #endif 58 59 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 60 #include "DOMApplicationCache.h" 61 #include "JSDOMApplicationCache.h" 62 #endif 63 64 #if ENABLE(SVG) 65 #include "SVGElementInstance.h" 66 #include "JSSVGElementInstance.h" 67 #endif 68 69 #if ENABLE(WORKERS) 70 #include "DedicatedWorkerContext.h" 71 #include "JSDedicatedWorkerContext.h" 72 #include "JSWorker.h" 73 #include "Worker.h" 74 #endif 75 76 #if ENABLE(NOTIFICATIONS) 77 #include "JSNotification.h" 78 #include "Notification.h" 79 #endif 80 81 #if ENABLE(INDEXED_DATABASE) 82 #include "IDBRequest.h" 83 #include "JSIDBRequest.h" 84 #endif 85 86 #if ENABLE(WEB_AUDIO) 87 #include "AudioContext.h" 88 #include "JSAudioContext.h" 89 #include "JSJavaScriptAudioNode.h" 90 #include "JavaScriptAudioNode.h" 91 #endif 92 93 #if ENABLE(WEB_SOCKETS) 94 #include "JSWebSocket.h" 95 #include "WebSocket.h" 96 #endif 97 98 #if ENABLE(BLOB) 99 #include "JSFileReader.h" 100 #include "FileReader.h" 101 #endif 102 103 using namespace JSC; 104 105 namespace WebCore { 106 107 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, EventTarget* target) 108 { 109 if (!target) 110 return jsNull(); 111 112 #if ENABLE(EVENTSOURCE) 113 if (EventSource* eventSource = target->toEventSource()) 114 return toJS(exec, globalObject, eventSource); 115 #endif 116 117 #if ENABLE(SVG) 118 // SVGElementInstance supports both toSVGElementInstance and toNode since so much mouse handling code depends on toNode returning a valid node. 119 if (SVGElementInstance* instance = target->toSVGElementInstance()) 120 return toJS(exec, globalObject, instance); 121 #endif 122 123 if (Node* node = target->toNode()) 124 return toJS(exec, globalObject, node); 125 126 if (DOMWindow* domWindow = target->toDOMWindow()) 127 return toJS(exec, globalObject, domWindow); 128 129 if (XMLHttpRequest* xhr = target->toXMLHttpRequest()) 130 return toJS(exec, globalObject, xhr); 131 132 if (XMLHttpRequestUpload* upload = target->toXMLHttpRequestUpload()) 133 return toJS(exec, globalObject, upload); 134 135 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 136 if (DOMApplicationCache* cache = target->toDOMApplicationCache()) 137 return toJS(exec, globalObject, cache); 138 #endif 139 140 if (MessagePort* messagePort = target->toMessagePort()) 141 return toJS(exec, globalObject, messagePort); 142 143 #if ENABLE(WORKERS) 144 if (Worker* worker = target->toWorker()) 145 return toJS(exec, globalObject, worker); 146 147 if (DedicatedWorkerContext* workerContext = target->toDedicatedWorkerContext()) 148 return toJSDOMGlobalObject(workerContext, exec); 149 #endif 150 151 #if ENABLE(SHARED_WORKERS) 152 if (SharedWorker* sharedWorker = target->toSharedWorker()) 153 return toJS(exec, globalObject, sharedWorker); 154 155 if (SharedWorkerContext* workerContext = target->toSharedWorkerContext()) 156 return toJSDOMGlobalObject(workerContext, exec); 157 #endif 158 159 #if ENABLE(NOTIFICATIONS) 160 if (Notification* notification = target->toNotification()) 161 return toJS(exec, notification); 162 #endif 163 164 #if ENABLE(INDEXED_DATABASE) 165 if (IDBDatabase* idbDatabase = target->toIDBDatabase()) 166 return toJS(exec, idbDatabase); 167 168 if (IDBRequest* idbRequest = target->toIDBRequest()) 169 return toJS(exec, idbRequest); 170 171 if (IDBTransaction* idbTransaction = target->toIDBTransaction()) 172 return toJS(exec, idbTransaction); 173 #endif 174 175 #if ENABLE(WEB_AUDIO) 176 if (JavaScriptAudioNode* jsAudioNode = target->toJavaScriptAudioNode()) 177 return toJS(exec, globalObject, jsAudioNode); 178 if (AudioContext* audioContext = target->toAudioContext()) 179 return toJS(exec, globalObject, audioContext); 180 #endif 181 182 #if ENABLE(WEB_SOCKETS) 183 if (WebSocket* webSocket = target->toWebSocket()) 184 return toJS(exec, webSocket); 185 #endif 186 187 #if ENABLE(BLOB) 188 if (FileReader* fileReader = target->toFileReader()) 189 return toJS(exec, globalObject, fileReader); 190 #endif 191 192 ASSERT_NOT_REACHED(); 193 return jsNull(); 194 } 195 196 EventTarget* toEventTarget(JSC::JSValue value) 197 { 198 #define CONVERT_TO_EVENT_TARGET(type) \ 199 if (value.inherits(&JS##type::s_info)) \ 200 return static_cast<JS##type*>(asObject(value))->impl(); 201 202 CONVERT_TO_EVENT_TARGET(Node) 203 CONVERT_TO_EVENT_TARGET(XMLHttpRequest) 204 CONVERT_TO_EVENT_TARGET(XMLHttpRequestUpload) 205 CONVERT_TO_EVENT_TARGET(MessagePort) 206 207 if (value.inherits(&JSDOMWindowShell::s_info)) 208 return static_cast<JSDOMWindowShell*>(asObject(value))->impl(); 209 210 #if ENABLE(EVENTSOURCE) 211 CONVERT_TO_EVENT_TARGET(EventSource) 212 #endif 213 214 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 215 CONVERT_TO_EVENT_TARGET(DOMApplicationCache) 216 #endif 217 218 #if ENABLE(SVG) 219 CONVERT_TO_EVENT_TARGET(SVGElementInstance) 220 #endif 221 222 #if ENABLE(WORKERS) 223 CONVERT_TO_EVENT_TARGET(Worker) 224 CONVERT_TO_EVENT_TARGET(DedicatedWorkerContext) 225 #endif 226 227 #if ENABLE(SHARED_WORKERS) 228 CONVERT_TO_EVENT_TARGET(SharedWorker) 229 CONVERT_TO_EVENT_TARGET(SharedWorkerContext) 230 #endif 231 232 #if ENABLE(NOTIFICATIONS) 233 CONVERT_TO_EVENT_TARGET(Notification) 234 #endif 235 236 #if ENABLE(WEB_SOCKETS) 237 CONVERT_TO_EVENT_TARGET(WebSocket) 238 #endif 239 240 return 0; 241 } 242 243 } // namespace WebCore 244