2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, 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.
28 #include "core/dom/MessagePort.h"
30 #include "bindings/core/v8/ExceptionState.h"
31 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
32 #include "bindings/core/v8/SerializedScriptValue.h"
33 #include "core/dom/CrossThreadTask.h"
34 #include "core/dom/ExceptionCode.h"
35 #include "core/dom/ExecutionContext.h"
36 #include "core/events/MessageEvent.h"
37 #include "core/frame/LocalDOMWindow.h"
38 #include "core/workers/WorkerGlobalScope.h"
39 #include "public/platform/WebString.h"
40 #include "wtf/Functional.h"
41 #include "wtf/text/AtomicString.h"
45 PassRefPtrWillBeRawPtr<MessagePort> MessagePort::create(ExecutionContext& executionContext)
47 RefPtrWillBeRawPtr<MessagePort> port = adoptRefWillBeNoop(new MessagePort(executionContext));
48 port->suspendIfNeeded();
49 return port.release();
52 MessagePort::MessagePort(ExecutionContext& executionContext)
53 : ActiveDOMObject(&executionContext)
60 MessagePort::~MessagePort()
65 void MessagePort::postMessage(ExecutionContext*, PassRefPtr<SerializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exceptionState)
69 ASSERT(executionContext());
70 ASSERT(m_entangledChannel);
72 OwnPtr<MessagePortChannelArray> channels;
73 // Make sure we aren't connected to any of the passed-in ports.
75 for (unsigned i = 0; i < ports->size(); ++i) {
76 MessagePort* dataPort = (*ports)[i].get();
77 if (dataPort == this) {
78 exceptionState.throwDOMException(DataCloneError, "Port at index " + String::number(i) + " contains the source port.");
82 channels = MessagePort::disentanglePorts(ports, exceptionState);
83 if (exceptionState.hadException())
87 WebString messageString = message->toWireString();
88 OwnPtr<WebMessagePortChannelArray> webChannels = toWebMessagePortChannelArray(channels.release());
89 m_entangledChannel->postMessage(messageString, webChannels.leakPtr());
93 PassOwnPtr<WebMessagePortChannelArray> MessagePort::toWebMessagePortChannelArray(PassOwnPtr<MessagePortChannelArray> channels)
95 OwnPtr<WebMessagePortChannelArray> webChannels;
96 if (channels && channels->size()) {
97 webChannels = adoptPtr(new WebMessagePortChannelArray(channels->size()));
98 for (size_t i = 0; i < channels->size(); ++i)
99 (*webChannels)[i] = (*channels)[i].leakPtr();
101 return webChannels.release();
105 PassOwnPtrWillBeRawPtr<MessagePortArray> MessagePort::toMessagePortArray(ExecutionContext* context, const WebMessagePortChannelArray& webChannels)
107 OwnPtrWillBeRawPtr<MessagePortArray> ports = nullptr;
108 if (!webChannels.isEmpty()) {
109 OwnPtr<MessagePortChannelArray> channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
110 for (size_t i = 0; i < webChannels.size(); ++i)
111 (*channels)[i] = adoptPtr(webChannels[i]);
112 ports = MessagePort::entanglePorts(*context, channels.release());
114 return ports.release();
117 PassOwnPtr<WebMessagePortChannel> MessagePort::disentangle()
119 ASSERT(m_entangledChannel);
120 m_entangledChannel->setClient(0);
121 return m_entangledChannel.release();
124 // Invoked to notify us that there are messages available for this port.
125 // This code may be called from another thread, and so should not call any non-threadsafe APIs (i.e. should not call into the entangled channel or access mutable variables).
126 void MessagePort::messageAvailable()
128 ASSERT(executionContext());
129 executionContext()->postTask(createCrossThreadTask(&MessagePort::dispatchMessages, m_weakFactory.createWeakPtr()));
132 void MessagePort::start()
134 // Do nothing if we've been cloned or closed.
138 ASSERT(executionContext());
146 void MessagePort::close()
149 m_entangledChannel->setClient(0);
153 void MessagePort::entangle(PassOwnPtr<WebMessagePortChannel> remote)
155 // Only invoked to set our initial entanglement.
156 ASSERT(!m_entangledChannel);
157 ASSERT(executionContext());
159 m_entangledChannel = remote;
160 m_entangledChannel->setClient(this);
163 const AtomicString& MessagePort::interfaceName() const
165 return EventTargetNames::MessagePort;
168 static bool tryGetMessageFrom(WebMessagePortChannel& webChannel, RefPtr<SerializedScriptValue>& message, OwnPtr<MessagePortChannelArray>& channels)
170 WebString messageString;
171 WebMessagePortChannelArray webChannels;
172 if (!webChannel.tryGetMessage(&messageString, webChannels))
175 if (webChannels.size()) {
176 channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
177 for (size_t i = 0; i < webChannels.size(); ++i)
178 (*channels)[i] = adoptPtr(webChannels[i]);
180 message = SerializedScriptValue::createFromWire(messageString);
184 void MessagePort::dispatchMessages()
186 // Messages for contexts that are not fully active get dispatched too, but JSAbstractEventListener::handleEvent() doesn't call handlers for these.
187 // The HTML5 spec specifies that any messages sent to a document that is not fully active should be dropped, so this behavior is OK.
191 RefPtr<SerializedScriptValue> message;
192 OwnPtr<MessagePortChannelArray> channels;
193 while (m_entangledChannel && tryGetMessageFrom(*m_entangledChannel, message, channels)) {
194 // close() in Worker onmessage handler should prevent next message from dispatching.
195 if (executionContext()->isWorkerGlobalScope() && toWorkerGlobalScope(executionContext())->isClosing())
198 OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::entanglePorts(*executionContext(), channels.release());
199 RefPtrWillBeRawPtr<Event> evt = MessageEvent::create(ports.release(), message.release());
201 dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION);
205 bool MessagePort::hasPendingActivity() const
207 // The spec says that entangled message ports should always be treated as if they have a strong reference.
208 // We'll also stipulate that the queue needs to be open (if the app drops its reference to the port before start()-ing it, then it's not really entangled as it's unreachable).
209 return m_started && isEntangled();
212 PassOwnPtr<MessagePortChannelArray> MessagePort::disentanglePorts(const MessagePortArray* ports, ExceptionState& exceptionState)
214 if (!ports || !ports->size())
217 // HashSet used to efficiently check for duplicates in the passed-in array.
218 HashSet<MessagePort*> portSet;
220 // Walk the incoming array - if there are any duplicate ports, or null ports or cloned ports, throw an error (per section 8.3.3 of the HTML5 spec).
221 for (unsigned i = 0; i < ports->size(); ++i) {
222 MessagePort* port = (*ports)[i].get();
223 if (!port || port->isNeutered() || portSet.contains(port)) {
227 else if (port->isNeutered())
228 type = "already neutered";
230 type = "a duplicate";
231 exceptionState.throwDOMException(DataCloneError, "Port at index " + String::number(i) + " is " + type + ".");
237 // Passed-in ports passed validity checks, so we can disentangle them.
238 OwnPtr<MessagePortChannelArray> portArray = adoptPtr(new MessagePortChannelArray(ports->size()));
239 for (unsigned i = 0; i < ports->size(); ++i)
240 (*portArray)[i] = (*ports)[i]->disentangle();
241 return portArray.release();
244 PassOwnPtrWillBeRawPtr<MessagePortArray> MessagePort::entanglePorts(ExecutionContext& context, PassOwnPtr<MessagePortChannelArray> channels)
246 if (!channels || !channels->size())
249 OwnPtrWillBeRawPtr<MessagePortArray> portArray = adoptPtrWillBeNoop(new MessagePortArray(channels->size()));
250 for (unsigned i = 0; i < channels->size(); ++i) {
251 RefPtrWillBeRawPtr<MessagePort> port = MessagePort::create(context);
252 port->entangle((*channels)[i].release());
253 (*portArray)[i] = port.release();
255 return portArray.release();