• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/events/MessageEvent.h"
30 
31 #include "bindings/v8/ExceptionMessages.h"
32 #include "bindings/v8/ExceptionState.h"
33 #include "core/events/ThreadLocalEventNames.h"
34 
35 namespace WebCore {
36 
isValidSource(EventTarget * source)37 static inline bool isValidSource(EventTarget* source)
38 {
39     return !source || source->toDOMWindow() || source->toMessagePort();
40 }
41 
MessageEventInit()42 MessageEventInit::MessageEventInit()
43 {
44 }
45 
MessageEvent()46 MessageEvent::MessageEvent()
47     : m_dataType(DataTypeScriptValue)
48 {
49     ScriptWrappable::init(this);
50 }
51 
MessageEvent(const AtomicString & type,const MessageEventInit & initializer)52 MessageEvent::MessageEvent(const AtomicString& type, const MessageEventInit& initializer)
53     : Event(type, initializer)
54     , m_dataType(DataTypeScriptValue)
55     , m_origin(initializer.origin)
56     , m_lastEventId(initializer.lastEventId)
57     , m_source(isValidSource(initializer.source.get()) ? initializer.source : 0)
58     , m_ports(adoptPtr(new MessagePortArray(initializer.ports)))
59 {
60     ScriptWrappable::init(this);
61     ASSERT(isValidSource(m_source.get()));
62 }
63 
MessageEvent(const String & origin,const String & lastEventId,PassRefPtr<EventTarget> source,PassOwnPtr<MessagePortArray> ports)64 MessageEvent::MessageEvent(const String& origin, const String& lastEventId, PassRefPtr<EventTarget> source, PassOwnPtr<MessagePortArray> ports)
65     : Event(EventTypeNames::message, false, false)
66     , m_dataType(DataTypeScriptValue)
67     , m_origin(origin)
68     , m_lastEventId(lastEventId)
69     , m_source(source)
70     , m_ports(ports)
71 {
72     ScriptWrappable::init(this);
73     ASSERT(isValidSource(m_source.get()));
74 }
75 
MessageEvent(PassRefPtr<SerializedScriptValue> data,const String & origin,const String & lastEventId,PassRefPtr<EventTarget> source,PassOwnPtr<MessagePortArray> ports)76 MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, PassRefPtr<EventTarget> source, PassOwnPtr<MessagePortArray> ports)
77     : Event(EventTypeNames::message, false, false)
78     , m_dataType(DataTypeSerializedScriptValue)
79     , m_dataAsSerializedScriptValue(data)
80     , m_origin(origin)
81     , m_lastEventId(lastEventId)
82     , m_source(source)
83     , m_ports(ports)
84 {
85     ScriptWrappable::init(this);
86     if (m_dataAsSerializedScriptValue)
87         m_dataAsSerializedScriptValue->registerMemoryAllocatedWithCurrentScriptContext();
88     ASSERT(isValidSource(m_source.get()));
89 }
90 
MessageEvent(PassRefPtr<SerializedScriptValue> data,const String & origin,const String & lastEventId,PassRefPtr<EventTarget> source,PassOwnPtr<MessagePortChannelArray> channels)91 MessageEvent::MessageEvent(PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, PassRefPtr<EventTarget> source, PassOwnPtr<MessagePortChannelArray> channels)
92     : Event(EventTypeNames::message, false, false)
93     , m_dataType(DataTypeSerializedScriptValue)
94     , m_dataAsSerializedScriptValue(data)
95     , m_origin(origin)
96     , m_lastEventId(lastEventId)
97     , m_source(source)
98     , m_channels(channels)
99 {
100     ScriptWrappable::init(this);
101     if (m_dataAsSerializedScriptValue)
102         m_dataAsSerializedScriptValue->registerMemoryAllocatedWithCurrentScriptContext();
103     ASSERT(isValidSource(m_source.get()));
104 }
105 
MessageEvent(const String & data,const String & origin)106 MessageEvent::MessageEvent(const String& data, const String& origin)
107     : Event(EventTypeNames::message, false, false)
108     , m_dataType(DataTypeString)
109     , m_dataAsString(data)
110     , m_origin(origin)
111 {
112     ScriptWrappable::init(this);
113 }
114 
MessageEvent(PassRefPtr<Blob> data,const String & origin)115 MessageEvent::MessageEvent(PassRefPtr<Blob> data, const String& origin)
116     : Event(EventTypeNames::message, false, false)
117     , m_dataType(DataTypeBlob)
118     , m_dataAsBlob(data)
119     , m_origin(origin)
120 {
121     ScriptWrappable::init(this);
122 }
123 
MessageEvent(PassRefPtr<ArrayBuffer> data,const String & origin)124 MessageEvent::MessageEvent(PassRefPtr<ArrayBuffer> data, const String& origin)
125     : Event(EventTypeNames::message, false, false)
126     , m_dataType(DataTypeArrayBuffer)
127     , m_dataAsArrayBuffer(data)
128     , m_origin(origin)
129 {
130     ScriptWrappable::init(this);
131 }
132 
~MessageEvent()133 MessageEvent::~MessageEvent()
134 {
135 }
136 
create(const AtomicString & type,const MessageEventInit & initializer,ExceptionState & exceptionState)137 PassRefPtr<MessageEvent> MessageEvent::create(const AtomicString& type, const MessageEventInit& initializer, ExceptionState& exceptionState)
138 {
139     if (initializer.source.get() && !isValidSource(initializer.source.get())) {
140         exceptionState.throwTypeError("The optional 'source' property is neither a Window nor MessagePort.");
141         return 0;
142     }
143     return adoptRef(new MessageEvent(type, initializer));
144 }
145 
initMessageEvent(const AtomicString & type,bool canBubble,bool cancelable,const String & origin,const String & lastEventId,DOMWindow * source,PassOwnPtr<MessagePortArray> ports)146 void MessageEvent::initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, const String& origin, const String& lastEventId, DOMWindow* source, PassOwnPtr<MessagePortArray> ports)
147 {
148     if (dispatched())
149         return;
150 
151     initEvent(type, canBubble, cancelable);
152 
153     m_dataType = DataTypeScriptValue;
154     m_origin = origin;
155     m_lastEventId = lastEventId;
156     m_source = source;
157     m_ports = ports;
158 }
159 
initMessageEvent(const AtomicString & type,bool canBubble,bool cancelable,PassRefPtr<SerializedScriptValue> data,const String & origin,const String & lastEventId,DOMWindow * source,PassOwnPtr<MessagePortArray> ports)160 void MessageEvent::initMessageEvent(const AtomicString& type, bool canBubble, bool cancelable, PassRefPtr<SerializedScriptValue> data, const String& origin, const String& lastEventId, DOMWindow* source, PassOwnPtr<MessagePortArray> ports)
161 {
162     if (dispatched())
163         return;
164 
165     initEvent(type, canBubble, cancelable);
166 
167     m_dataType = DataTypeSerializedScriptValue;
168     m_dataAsSerializedScriptValue = data;
169     m_origin = origin;
170     m_lastEventId = lastEventId;
171     m_source = source;
172     m_ports = ports;
173 
174     if (m_dataAsSerializedScriptValue)
175         m_dataAsSerializedScriptValue->registerMemoryAllocatedWithCurrentScriptContext();
176 }
177 
interfaceName() const178 const AtomicString& MessageEvent::interfaceName() const
179 {
180     return EventNames::MessageEvent;
181 }
182 
entangleMessagePorts(ExecutionContext * context)183 void MessageEvent::entangleMessagePorts(ExecutionContext* context)
184 {
185     m_ports = MessagePort::entanglePorts(*context, m_channels.release());
186 }
187 
188 } // namespace WebCore
189