1/*
2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "WebDOMEventTarget.h"
23
24#include "DOMApplicationCache.h"
25#include "DOMWindow.h"
26#include "DedicatedWorkerContext.h"
27#include "EventSource.h"
28#include "MessagePort.h"
29#include "Node.h"
30#include "Notification.h"
31#include "SharedWorker.h"
32#include "SharedWorkerContext.h"
33#include "ThreadCheck.h"
34#include "WebDOMDOMApplicationCache.h"
35#include "WebDOMDOMWindow.h"
36#include "WebDOMDedicatedWorkerContext.h"
37#include "WebDOMEventSource.h"
38#include "WebDOMMessagePort.h"
39#include "WebDOMNode.h"
40#include "WebDOMNotification.h"
41#include "WebDOMSharedWorker.h"
42#include "WebDOMSharedWorkerContext.h"
43#include "WebDOMWebSocket.h"
44#include "WebDOMWorker.h"
45#include "WebDOMXMLHttpRequest.h"
46#include "WebDOMXMLHttpRequestUpload.h"
47#include "WebExceptionHandler.h"
48#include "WebSocket.h"
49#include "Worker.h"
50#include "XMLHttpRequest.h"
51#include "XMLHttpRequestUpload.h"
52
53#include <wtf/RefPtr.h>
54
55struct WebDOMEventTarget::WebDOMEventTargetPrivate {
56    WebDOMEventTargetPrivate(WebCore::EventTarget* object = 0)
57        : impl(object)
58    {
59    }
60
61    RefPtr<WebCore::EventTarget> impl;
62};
63
64WebDOMEventTarget::WebDOMEventTarget()
65    : WebDOMObject()
66    , m_impl(0)
67{
68}
69
70WebDOMEventTarget::WebDOMEventTarget(WebCore::EventTarget* impl)
71    : WebDOMObject()
72    , m_impl(new WebDOMEventTargetPrivate(impl))
73{
74}
75
76WebDOMEventTarget::WebDOMEventTarget(const WebDOMEventTarget& copy)
77    : WebDOMObject()
78{
79    m_impl = copy.impl() ? new WebDOMEventTargetPrivate(copy.impl()) : 0;
80}
81
82WebDOMEventTarget::~WebDOMEventTarget()
83{
84    delete m_impl;
85    m_impl = 0;
86}
87
88WebCore::EventTarget* WebDOMEventTarget::impl() const
89{
90    return m_impl ? m_impl->impl.get() : 0;
91}
92
93#define ConvertTo(type) \
94WebDOM##type WebDOMEventTarget::to##type() \
95{ \
96    WebCore::EventTarget* target = impl(); \
97    return WebDOM##type(target ? target->to##type() : 0); \
98}
99
100ConvertTo(Node)
101ConvertTo(DOMWindow)
102ConvertTo(XMLHttpRequest)
103ConvertTo(XMLHttpRequestUpload)
104ConvertTo(MessagePort)
105
106#if ENABLE(EVENTSOURCE)
107ConvertTo(EventSource)
108#endif
109
110#if ENABLE(OFFLINE_WEB_APPLICATIONS)
111ConvertTo(DOMApplicationCache)
112#endif
113
114#if ENABLE(WORKERS)
115ConvertTo(Worker)
116ConvertTo(DedicatedWorkerContext)
117#endif
118
119#if ENABLE(SHARED_WORKERS)
120ConvertTo(SharedWorker)
121ConvertTo(SharedWorkerContext)
122#endif
123
124#if ENABLE(NOTIFICATIONS)
125ConvertTo(Notification)
126#endif
127
128#if ENABLE(WEB_SOCKETS)
129ConvertTo(WebSocket)
130#endif
131
132WebCore::EventTarget* toWebCore(const WebDOMEventTarget& wrapper)
133{
134    return wrapper.impl();
135}
136
137WebDOMEventTarget toWebKit(WebCore::EventTarget* value)
138{
139    if (WebCore::Node* node = value->toNode())
140        return toWebKit(node);
141
142    if (WebCore::DOMWindow* window = value->toDOMWindow())
143        return toWebKit(window);
144
145    if (WebCore::XMLHttpRequest* xhr = value->toXMLHttpRequest())
146        return toWebKit(xhr);
147
148    if (WebCore::XMLHttpRequestUpload* upload = value->toXMLHttpRequestUpload())
149        return toWebKit(upload);
150
151    if (WebCore::MessagePort* messagePort = value->toMessagePort())
152        return toWebKit(messagePort);
153
154#if ENABLE(EVENTSOURCE)
155    if (WebCore::EventSource* eventSource = value->toEventSource())
156        return toWebKit(eventSource);
157#endif
158
159#if ENABLE(SVG) && 0
160    // FIXME: Enable once SVG bindings are generated.
161    // SVGElementInstance supports both toSVGElementInstance and toNode since so much mouse handling code depends on toNode returning a valid node.
162    if (WebCore::SVGElementInstance* instance = value->toSVGElementInstance())
163        return toWebKit(instance);
164#endif
165
166#if ENABLE(OFFLINE_WEB_APPLICATIONS)
167    if (WebCore::DOMApplicationCache* cache = value->toDOMApplicationCache())
168        return toWebKit(cache);
169#endif
170
171#if ENABLE(WORKERS)
172    if (WebCore::Worker* worker = value->toWorker())
173        return toWebKit(worker);
174
175    if (WebCore::DedicatedWorkerContext* workerContext = value->toDedicatedWorkerContext())
176        return toWebKit(workerContext);
177#endif
178
179#if ENABLE(SHARED_WORKERS)
180    if (WebCore::SharedWorker* sharedWorker = value->toSharedWorker())
181        return toWebKit(sharedWorker);
182
183    if (WebCore::SharedWorkerContext* workerContext = value->toSharedWorkerContext())
184        return toWebKit(workerContext);
185#endif
186
187#if ENABLE(NOTIFICATIONS)
188    if (WebCore::Notification* notification = value->toNotification())
189        return toWebKit(notification);
190#endif
191
192#if ENABLE(WEB_SOCKETS)
193    if (WebCore::WebSocket* webSocket = value->toWebSocket())
194        return toWebKit(webSocket);
195#endif
196
197    ASSERT_NOT_REACHED();
198    return WebDOMEventTarget();
199}
200
201WebDOMEventTarget& WebDOMEventTarget::operator=(const WebDOMEventTarget& copy)
202{
203    delete m_impl;
204    m_impl = copy.impl() ? new WebDOMEventTargetPrivate(copy.impl()) : 0;
205    return *this;
206}
207