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(WEB_SOCKETS) 82 #include "JSWebSocket.h" 83 #include "WebSocket.h" 84 #endif 85 86 using namespace JSC; 87 88 namespace WebCore { 89 90 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, EventTarget* target) 91 { 92 if (!target) 93 return jsNull(); 94 95 #if ENABLE(EVENTSOURCE) 96 if (EventSource* eventSource = target->toEventSource()) 97 return toJS(exec, globalObject, eventSource); 98 #endif 99 100 #if ENABLE(SVG) 101 // SVGElementInstance supports both toSVGElementInstance and toNode since so much mouse handling code depends on toNode returning a valid node. 102 if (SVGElementInstance* instance = target->toSVGElementInstance()) 103 return toJS(exec, globalObject, instance); 104 #endif 105 106 if (Node* node = target->toNode()) 107 return toJS(exec, globalObject, node); 108 109 if (DOMWindow* domWindow = target->toDOMWindow()) 110 return toJS(exec, globalObject, domWindow); 111 112 if (XMLHttpRequest* xhr = target->toXMLHttpRequest()) 113 return toJS(exec, globalObject, xhr); 114 115 if (XMLHttpRequestUpload* upload = target->toXMLHttpRequestUpload()) 116 return toJS(exec, globalObject, upload); 117 118 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 119 if (DOMApplicationCache* cache = target->toDOMApplicationCache()) 120 return toJS(exec, globalObject, cache); 121 #endif 122 123 if (MessagePort* messagePort = target->toMessagePort()) 124 return toJS(exec, globalObject, messagePort); 125 126 #if ENABLE(WORKERS) 127 if (Worker* worker = target->toWorker()) 128 return toJS(exec, globalObject, worker); 129 130 if (DedicatedWorkerContext* workerContext = target->toDedicatedWorkerContext()) 131 return toJSDOMGlobalObject(workerContext, exec); 132 #endif 133 134 #if ENABLE(SHARED_WORKERS) 135 if (SharedWorker* sharedWorker = target->toSharedWorker()) 136 return toJS(exec, globalObject, sharedWorker); 137 138 if (SharedWorkerContext* workerContext = target->toSharedWorkerContext()) 139 return toJSDOMGlobalObject(workerContext, exec); 140 #endif 141 142 #if ENABLE(NOTIFICATIONS) 143 if (Notification* notification = target->toNotification()) 144 return toJS(exec, notification); 145 #endif 146 147 #if ENABLE(WEB_SOCKETS) 148 if (WebSocket* webSocket = target->toWebSocket()) 149 return toJS(exec, webSocket); 150 #endif 151 152 ASSERT_NOT_REACHED(); 153 return jsNull(); 154 } 155 156 EventTarget* toEventTarget(JSC::JSValue value) 157 { 158 #define CONVERT_TO_EVENT_TARGET(type) \ 159 if (value.inherits(&JS##type::s_info)) \ 160 return static_cast<JS##type*>(asObject(value))->impl(); 161 162 CONVERT_TO_EVENT_TARGET(Node) 163 CONVERT_TO_EVENT_TARGET(XMLHttpRequest) 164 CONVERT_TO_EVENT_TARGET(XMLHttpRequestUpload) 165 CONVERT_TO_EVENT_TARGET(MessagePort) 166 167 if (value.inherits(&JSDOMWindowShell::s_info)) 168 return static_cast<JSDOMWindowShell*>(asObject(value))->impl(); 169 170 #if ENABLE(EVENTSOURCE) 171 CONVERT_TO_EVENT_TARGET(EventSource) 172 #endif 173 174 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 175 CONVERT_TO_EVENT_TARGET(DOMApplicationCache) 176 #endif 177 178 #if ENABLE(SVG) 179 CONVERT_TO_EVENT_TARGET(SVGElementInstance) 180 #endif 181 182 #if ENABLE(WORKERS) 183 CONVERT_TO_EVENT_TARGET(Worker) 184 CONVERT_TO_EVENT_TARGET(DedicatedWorkerContext) 185 #endif 186 187 #if ENABLE(SHARED_WORKERS) 188 CONVERT_TO_EVENT_TARGET(SharedWorker) 189 CONVERT_TO_EVENT_TARGET(SharedWorkerContext) 190 #endif 191 192 #if ENABLE(NOTIFICATIONS) 193 CONVERT_TO_EVENT_TARGET(Notification) 194 #endif 195 196 #if ENABLE(WEB_SOCKETS) 197 CONVERT_TO_EVENT_TARGET(WebSocket) 198 #endif 199 200 return 0; 201 } 202 203 } // namespace WebCore 204