2011-01-07 Adam Barth <abarth@webkit.org>
[WebKit-https.git] / WebCore / bindings / js / JSEventTarget.cpp
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 "JSJavaScriptAudioNode.h"
88 #include "JavaScriptAudioNode.h"
89 #endif
90
91 #if ENABLE(WEB_SOCKETS)
92 #include "JSWebSocket.h"
93 #include "WebSocket.h"
94 #endif
95
96 #if ENABLE(BLOB)
97 #include "JSFileReader.h"
98 #include "FileReader.h"
99 #endif
100
101 using namespace JSC;
102
103 namespace WebCore {
104
105 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, EventTarget* target)
106 {
107     if (!target)
108         return jsNull();
109     
110 #if ENABLE(EVENTSOURCE)
111     if (EventSource* eventSource = target->toEventSource())
112         return toJS(exec, globalObject, eventSource);
113 #endif
114
115 #if ENABLE(SVG)
116     // SVGElementInstance supports both toSVGElementInstance and toNode since so much mouse handling code depends on toNode returning a valid node.
117     if (SVGElementInstance* instance = target->toSVGElementInstance())
118         return toJS(exec, globalObject, instance);
119 #endif
120     
121     if (Node* node = target->toNode())
122         return toJS(exec, globalObject, node);
123
124     if (DOMWindow* domWindow = target->toDOMWindow())
125         return toJS(exec, globalObject, domWindow);
126
127     if (XMLHttpRequest* xhr = target->toXMLHttpRequest())
128         return toJS(exec, globalObject, xhr);
129
130     if (XMLHttpRequestUpload* upload = target->toXMLHttpRequestUpload())
131         return toJS(exec, globalObject, upload);
132
133 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
134     if (DOMApplicationCache* cache = target->toDOMApplicationCache())
135         return toJS(exec, globalObject, cache);
136 #endif
137
138     if (MessagePort* messagePort = target->toMessagePort())
139         return toJS(exec, globalObject, messagePort);
140
141 #if ENABLE(WORKERS)
142     if (Worker* worker = target->toWorker())
143         return toJS(exec, globalObject, worker);
144
145     if (DedicatedWorkerContext* workerContext = target->toDedicatedWorkerContext())
146         return toJSDOMGlobalObject(workerContext, exec);
147 #endif
148
149 #if ENABLE(SHARED_WORKERS)
150     if (SharedWorker* sharedWorker = target->toSharedWorker())
151         return toJS(exec, globalObject, sharedWorker);
152
153     if (SharedWorkerContext* workerContext = target->toSharedWorkerContext())
154         return toJSDOMGlobalObject(workerContext, exec);
155 #endif
156
157 #if ENABLE(NOTIFICATIONS)
158     if (Notification* notification = target->toNotification())
159         return toJS(exec, notification);
160 #endif
161
162 #if ENABLE(INDEXED_DATABASE)
163     if (IDBRequest* idbRequest = target->toIDBRequest())
164         return toJS(exec, idbRequest);
165 #endif
166
167 #if ENABLE(WEB_AUDIO)
168     if (JavaScriptAudioNode* jsAudioNode = target->toJavaScriptAudioNode())
169         return toJS(exec, globalObject, jsAudioNode);
170 #endif
171
172 #if ENABLE(WEB_SOCKETS)
173     if (WebSocket* webSocket = target->toWebSocket())
174         return toJS(exec, webSocket);
175 #endif
176
177 #if ENABLE(BLOB)
178     if (FileReader* fileReader = target->toFileReader())
179         return toJS(exec, globalObject, fileReader);
180 #endif
181
182     ASSERT_NOT_REACHED();
183     return jsNull();
184 }
185
186 EventTarget* toEventTarget(JSC::JSValue value)
187 {
188     #define CONVERT_TO_EVENT_TARGET(type) \
189         if (value.inherits(&JS##type::s_info)) \
190             return static_cast<JS##type*>(asObject(value))->impl();
191
192     CONVERT_TO_EVENT_TARGET(Node)
193     CONVERT_TO_EVENT_TARGET(XMLHttpRequest)
194     CONVERT_TO_EVENT_TARGET(XMLHttpRequestUpload)
195     CONVERT_TO_EVENT_TARGET(MessagePort)
196
197     if (value.inherits(&JSDOMWindowShell::s_info))
198         return static_cast<JSDOMWindowShell*>(asObject(value))->impl();
199
200 #if ENABLE(EVENTSOURCE)
201     CONVERT_TO_EVENT_TARGET(EventSource)
202 #endif
203
204 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
205     CONVERT_TO_EVENT_TARGET(DOMApplicationCache)
206 #endif
207
208 #if ENABLE(SVG)
209     CONVERT_TO_EVENT_TARGET(SVGElementInstance)
210 #endif
211
212 #if ENABLE(WORKERS)
213     CONVERT_TO_EVENT_TARGET(Worker)
214     CONVERT_TO_EVENT_TARGET(DedicatedWorkerContext)
215 #endif
216
217 #if ENABLE(SHARED_WORKERS)
218     CONVERT_TO_EVENT_TARGET(SharedWorker)
219     CONVERT_TO_EVENT_TARGET(SharedWorkerContext)
220 #endif
221
222 #if ENABLE(NOTIFICATIONS)
223     CONVERT_TO_EVENT_TARGET(Notification)
224 #endif
225
226 #if ENABLE(WEB_SOCKETS)
227     CONVERT_TO_EVENT_TARGET(WebSocket)
228 #endif
229
230     return 0;
231 }
232
233 } // namespace WebCore