1/*
2 * Copyright (C) 2007 Henry Mason (hmason@mac.com)
3 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
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"
29#include "MessageEvent.h"
30
31#include "DOMWindow.h"
32#include "EventNames.h"
33
34namespace WebCore {
35
36MessageEventInit::MessageEventInit()
37{
38}
39
40MessageEvent::MessageEvent()
41    : m_dataType(DataTypeScriptValue)
42{
43}
44
45MessageEvent::MessageEvent(const AtomicString& type, const MessageEventInit& initializer)
46    : Event(type, initializer)
47    , m_dataType(DataTypeScriptValue)
48    , m_dataAsScriptValue(initializer.data)
49    , 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
56MessageEvent::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
67MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, PassRefPtr<DOMWindow> source, PassOwnPtr<MessagePortArray> ports)
68    : Event(eventNames().messageEvent, false, false)
69    , m_dataType(DataTypeSerializedScriptValue)
70    , m_dataAsSerializedScriptValue(data)
71    , m_origin(origin)
72    , m_lastEventId(lastEventId)
73    , m_source(source)
74    , m_ports(ports)
75{
76}
77
78MessageEvent::MessageEvent(const String& data, const String& origin)
79    : Event(eventNames().messageEvent, false, false)
80    , m_dataType(DataTypeString)
81    , m_dataAsString(data)
82    , m_origin(origin)
83    , m_lastEventId("")
84{
85}
86
87MessageEvent::MessageEvent(PassRefPtr<Blob> data, const String& origin)
88    : Event(eventNames().messageEvent, false, false)
89    , m_dataType(DataTypeBlob)
90    , m_dataAsBlob(data)
91    , m_origin(origin)
92    , m_lastEventId("")
93{
94}
95
96MessageEvent::MessageEvent(PassRefPtr<ArrayBuffer> data, const String& origin)
97    : Event(eventNames().messageEvent, false, false)
98    , m_dataType(DataTypeArrayBuffer)
99    , m_dataAsArrayBuffer(data)
100    , m_origin(origin)
101    , m_lastEventId("")
102{
103}
104
105MessageEvent::~MessageEvent()
106{
107}
108
109void 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
124void MessageEvent::initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, DOMWindow* source, PassOwnPtr<MessagePortArray> ports)
125{
126    if (dispatched())
127        return;
128
129    initEvent(type, canBubble, cancelable);
130
131    m_dataType = DataTypeSerializedScriptValue;
132    m_dataAsSerializedScriptValue = data;
133    m_origin = origin;
134    m_lastEventId = lastEventId;
135    m_source = source;
136    m_ports = ports;
137}
138
139// 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
147MessagePort* MessageEvent::messagePort()
148{
149    if (!m_ports)
150        return 0;
151    ASSERT(m_ports->size() == 1);
152    return (*m_ports)[0].get();
153}
154
155void MessageEvent::initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, DOMWindow* source, MessagePort* port)
156{
157    OwnPtr<MessagePortArray> ports;
158    if (port) {
159        ports = adoptPtr(new MessagePortArray);
160        ports->append(port);
161    }
162    initMessageEvent(type, canBubble, cancelable, data, origin, lastEventId, source, ports.release());
163}
164
165const AtomicString& MessageEvent::interfaceName() const
166{
167    return eventNames().interfaceForMessageEvent;
168}
169
170} // namespace WebCore
171