blob: 0329af1379b04cc2ee3d813b0bf206ab972d088a [file] [log] [blame]
darin@apple.comc8da90b2008-01-01 00:18:16 +00001/*
2 * Copyright (C) 2007 Henry Mason (hmason@mac.com)
darin@apple.com48ac3c42008-06-14 08:46:51 +00003 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
darin@apple.comc8da90b2008-01-01 00:18:16 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#include "config.h"
darin@apple.com48ac3c42008-06-14 08:46:51 +000029#include "MessageEvent.h"
darin@apple.comc8da90b2008-01-01 00:18:16 +000030
31#include "DOMWindow.h"
32#include "EventNames.h"
darin@apple.comc8da90b2008-01-01 00:18:16 +000033
34namespace WebCore {
35
commit-queue@webkit.org9a6aee22011-09-28 02:02:18 +000036MessageEventInit::MessageEventInit()
commit-queue@webkit.org9a6aee22011-09-28 02:02:18 +000037{
38}
39
darin@apple.comc8da90b2008-01-01 00:18:16 +000040MessageEvent::MessageEvent()
haraken@chromium.orgedc286c2011-10-20 06:02:42 +000041 : m_dataType(DataTypeScriptValue)
darin@apple.comc8da90b2008-01-01 00:18:16 +000042{
43}
44
commit-queue@webkit.org9a6aee22011-09-28 02:02:18 +000045MessageEvent::MessageEvent(const AtomicString& type, const MessageEventInit& initializer)
46 : Event(type, initializer)
haraken@chromium.orgedc286c2011-10-20 06:02:42 +000047 , m_dataType(DataTypeScriptValue)
48 , m_dataAsScriptValue(initializer.data)
commit-queue@webkit.org9a6aee22011-09-28 02:02:18 +000049 , m_origin(initializer.origin)
50 , m_lastEventId(initializer.lastEventId)
51 , m_source(initializer.source)
52 , m_ports(adoptPtr(new MessagePortArray(initializer.ports)))
53{
54}
55
haraken@chromium.orgedc286c2011-10-20 06:02:42 +000056MessageEvent::MessageEvent(const ScriptValue& data, const String& origin, const String& lastEventId, PassRefPtr<DOMWindow> source, PassOwnPtr<MessagePortArray> ports)
57 : Event(eventNames().messageEvent, false, false)
58 , m_dataType(DataTypeScriptValue)
59 , m_dataAsScriptValue(data)
60 , m_origin(origin)
61 , m_lastEventId(lastEventId)
62 , m_source(source)
63 , m_ports(ports)
64{
65}
66
oliver@apple.com0ef2c5b2009-10-07 02:06:03 +000067MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, PassRefPtr<DOMWindow> source, PassOwnPtr<MessagePortArray> ports)
atwilson@chromium.org70fae522009-09-15 18:27:45 +000068 : Event(eventNames().messageEvent, false, false)
yutak@chromium.org89952172011-08-25 09:25:16 +000069 , m_dataType(DataTypeSerializedScriptValue)
70 , m_dataAsSerializedScriptValue(data)
weinig@apple.com205006e2008-04-27 01:56:37 +000071 , m_origin(origin)
weinig@apple.combb5d25f2008-05-08 02:13:39 +000072 , m_lastEventId(lastEventId)
darin@apple.comc8da90b2008-01-01 00:18:16 +000073 , m_source(source)
atwilson@chromium.org4a7bb132009-08-26 20:45:24 +000074 , m_ports(ports)
darin@apple.comc8da90b2008-01-01 00:18:16 +000075{
76}
77
commit-queue@webkit.orgb5d0a902012-11-23 11:52:11 +000078MessageEvent::MessageEvent(const String& data, const String& origin)
yutak@chromium.org89952172011-08-25 09:25:16 +000079 : Event(eventNames().messageEvent, false, false)
80 , m_dataType(DataTypeString)
81 , m_dataAsString(data)
commit-queue@webkit.orgb5d0a902012-11-23 11:52:11 +000082 , m_origin(origin)
yutak@chromium.org89952172011-08-25 09:25:16 +000083 , m_lastEventId("")
84{
85}
86
commit-queue@webkit.orgb5d0a902012-11-23 11:52:11 +000087MessageEvent::MessageEvent(PassRefPtr<Blob> data, const String& origin)
yutak@chromium.orgeadc20d2011-08-30 03:38:50 +000088 : Event(eventNames().messageEvent, false, false)
89 , m_dataType(DataTypeBlob)
90 , m_dataAsBlob(data)
commit-queue@webkit.orgb5d0a902012-11-23 11:52:11 +000091 , m_origin(origin)
yutak@chromium.orgeadc20d2011-08-30 03:38:50 +000092 , m_lastEventId("")
93{
94}
95
commit-queue@webkit.orgb5d0a902012-11-23 11:52:11 +000096MessageEvent::MessageEvent(PassRefPtr<ArrayBuffer> data, const String& origin)
yutak@chromium.org6c30c9762011-08-31 08:33:38 +000097 : Event(eventNames().messageEvent, false, false)
98 , m_dataType(DataTypeArrayBuffer)
99 , m_dataAsArrayBuffer(data)
commit-queue@webkit.orgb5d0a902012-11-23 11:52:11 +0000100 , m_origin(origin)
yutak@chromium.org6c30c9762011-08-31 08:33:38 +0000101 , m_lastEventId("")
102{
103}
104
darin@apple.comc8da90b2008-01-01 00:18:16 +0000105MessageEvent::~MessageEvent()
106{
107}
108
haraken@chromium.orgedc286c2011-10-20 06:02:42 +0000109void MessageEvent::initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, const ScriptValue& data, const String& origin, const String& lastEventId, DOMWindow* source, PassOwnPtr<MessagePortArray> ports)
110{
111 if (dispatched())
112 return;
113
114 initEvent(type, canBubble, cancelable);
115
116 m_dataType = DataTypeScriptValue;
117 m_dataAsScriptValue = data;
118 m_origin = origin;
119 m_lastEventId = lastEventId;
120 m_source = source;
121 m_ports = ports;
122}
123
oliver@apple.com0ef2c5b2009-10-07 02:06:03 +0000124void MessageEvent::initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, DOMWindow* source, PassOwnPtr<MessagePortArray> ports)
darin@apple.comc8da90b2008-01-01 00:18:16 +0000125{
126 if (dispatched())
127 return;
commit-queue@webkit.org9a6aee22011-09-28 02:02:18 +0000128
darin@apple.comc8da90b2008-01-01 00:18:16 +0000129 initEvent(type, canBubble, cancelable);
yutak@chromium.org89952172011-08-25 09:25:16 +0000130
131 m_dataType = DataTypeSerializedScriptValue;
132 m_dataAsSerializedScriptValue = data;
weinig@apple.com205006e2008-04-27 01:56:37 +0000133 m_origin = origin;
weinig@apple.combb5d25f2008-05-08 02:13:39 +0000134 m_lastEventId = lastEventId;
darin@apple.comc8da90b2008-01-01 00:18:16 +0000135 m_source = source;
atwilson@chromium.org4a7bb132009-08-26 20:45:24 +0000136 m_ports = ports;
137}
138
yutak@chromium.org89952172011-08-25 09:25:16 +0000139// FIXME: Remove this when we have custom ObjC binding support.
140SerializedScriptValue* MessageEvent::data() const
141{
142 // WebSocket is not exposed in ObjC bindings, thus the data type should always be SerializedScriptValue.
143 ASSERT(m_dataType == DataTypeSerializedScriptValue);
144 return m_dataAsSerializedScriptValue.get();
145}
146
atwilson@chromium.org4a7bb132009-08-26 20:45:24 +0000147MessagePort* MessageEvent::messagePort()
148{
149 if (!m_ports)
150 return 0;
151 ASSERT(m_ports->size() == 1);
152 return (*m_ports)[0].get();
153}
154
oliver@apple.com0ef2c5b2009-10-07 02:06:03 +0000155void MessageEvent::initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, DOMWindow* source, MessagePort* port)
atwilson@chromium.org4a7bb132009-08-26 20:45:24 +0000156{
abarth@webkit.org462d90a2011-04-27 21:25:22 +0000157 OwnPtr<MessagePortArray> ports;
atwilson@chromium.org4a7bb132009-08-26 20:45:24 +0000158 if (port) {
abarth@webkit.org462d90a2011-04-27 21:25:22 +0000159 ports = adoptPtr(new MessagePortArray);
atwilson@chromium.org4a7bb132009-08-26 20:45:24 +0000160 ports->append(port);
161 }
abarth@webkit.org462d90a2011-04-27 21:25:22 +0000162 initMessageEvent(type, canBubble, cancelable, data, origin, lastEventId, source, ports.release());
darin@apple.comc8da90b2008-01-01 00:18:16 +0000163}
164
abarth@webkit.org090fa4b2011-10-21 00:00:37 +0000165const AtomicString& MessageEvent::interfaceName() const
166{
167 return eventNames().interfaceForMessageEvent;
168}
169
darin@apple.comc8da90b2008-01-01 00:18:16 +0000170} // namespace WebCore