2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "PlatformMessagePortChannel.h"
35 #include "MessagePort.h"
36 #include "ScriptExecutionContext.h"
40 PlatformMessagePortChannel::EventData::EventData(RefPtr<SerializedScriptValue>&& message, std::unique_ptr<MessagePortChannelArray> channels)
41 : m_message(WTFMove(message))
42 , m_channels(WTFMove(channels))
46 void MessagePortChannel::createChannel(MessagePort* port1, MessagePort* port2)
48 Ref<PlatformMessagePortChannel::MessagePortQueue> queue1 = PlatformMessagePortChannel::MessagePortQueue::create();
49 Ref<PlatformMessagePortChannel::MessagePortQueue> queue2 = PlatformMessagePortChannel::MessagePortQueue::create();
51 auto channel1 = std::make_unique<MessagePortChannel>(PlatformMessagePortChannel::create(queue1.ptr(), queue2.ptr()));
52 auto channel2 = std::make_unique<MessagePortChannel>(PlatformMessagePortChannel::create(queue2.ptr(), queue1.ptr()));
54 channel1->m_channel->m_entangledChannel = channel2->m_channel;
55 channel2->m_channel->m_entangledChannel = channel1->m_channel;
57 port1->entangle(WTFMove(channel2));
58 port2->entangle(WTFMove(channel1));
61 MessagePortChannel::MessagePortChannel(RefPtr<PlatformMessagePortChannel>&& channel)
62 : m_channel(WTFMove(channel))
66 MessagePortChannel::~MessagePortChannel()
71 bool MessagePortChannel::entangleIfOpen(MessagePort* port)
73 // We can't call member functions on our remote pair while holding our mutex or we'll deadlock,
74 // but we need to guard against the remote port getting closed/freed, so create a standalone reference.
75 RefPtr<PlatformMessagePortChannel> remote = m_channel->entangledChannel();
78 remote->setRemotePort(port);
82 void MessagePortChannel::disentangle()
84 RefPtr<PlatformMessagePortChannel> remote = m_channel->entangledChannel();
86 remote->setRemotePort(nullptr);
89 void MessagePortChannel::postMessageToRemote(RefPtr<SerializedScriptValue>&& message, std::unique_ptr<MessagePortChannelArray> channels)
91 LockHolder lock(m_channel->m_mutex);
92 if (!m_channel->m_outgoingQueue)
94 bool wasEmpty = m_channel->m_outgoingQueue->appendAndCheckEmpty(std::make_unique<PlatformMessagePortChannel::EventData>(WTFMove(message), WTFMove(channels)));
95 if (wasEmpty && m_channel->m_remotePort)
96 m_channel->m_remotePort->messageAvailable();
99 bool MessagePortChannel::tryGetMessageFromRemote(RefPtr<SerializedScriptValue>& message, std::unique_ptr<MessagePortChannelArray>& channels)
101 LockHolder lock(m_channel->m_mutex);
102 auto result = m_channel->m_incomingQueue->tryGetMessage();
106 message = result->message();
107 channels = result->channels();
112 void MessagePortChannel::close()
114 RefPtr<PlatformMessagePortChannel> remote = m_channel->entangledChannel();
117 m_channel->closeInternal();
118 remote->closeInternal();
121 bool MessagePortChannel::isConnectedTo(MessagePort* port)
123 // FIXME: What guarantees that the result remains the same after we release the lock?
124 LockHolder lock(m_channel->m_mutex);
125 return m_channel->m_remotePort == port;
128 bool MessagePortChannel::hasPendingActivity()
130 // FIXME: What guarantees that the result remains the same after we release the lock?
131 LockHolder lock(m_channel->m_mutex);
132 return !m_channel->m_incomingQueue->isEmpty();
135 MessagePort* MessagePortChannel::locallyEntangledPort(const ScriptExecutionContext* context)
137 LockHolder lock(m_channel->m_mutex);
138 // See if both contexts are run by the same thread (are the same context, or are both documents).
139 if (m_channel->m_remotePort) {
140 // The remote port's ScriptExecutionContext is guaranteed not to change here - MessagePort::contextDestroyed()
141 // will close the port before the context goes away, and close() will block because we are holding the mutex.
142 ScriptExecutionContext* remoteContext = m_channel->m_remotePort->scriptExecutionContext();
143 if (remoteContext == context || (remoteContext && remoteContext->isDocument() && context->isDocument()))
144 return m_channel->m_remotePort;
149 Ref<PlatformMessagePortChannel> PlatformMessagePortChannel::create(MessagePortQueue* incoming, MessagePortQueue* outgoing)
151 return adoptRef(*new PlatformMessagePortChannel(incoming, outgoing));
154 PlatformMessagePortChannel::PlatformMessagePortChannel(MessagePortQueue* incoming, MessagePortQueue* outgoing)
155 : m_incomingQueue(incoming)
156 , m_outgoingQueue(outgoing)
160 PlatformMessagePortChannel::~PlatformMessagePortChannel()
164 void PlatformMessagePortChannel::setRemotePort(MessagePort* port)
166 LockHolder lock(m_mutex);
167 // Should never set port if it is already set.
168 ASSERT(!port || !m_remotePort);
172 RefPtr<PlatformMessagePortChannel> PlatformMessagePortChannel::entangledChannel()
174 // FIXME: What guarantees that the result remains the same after we release the lock?
175 // This lock only guarantees that the returned pointer will not be pointing to released memory,
176 // but not that it will still be pointing to this object's entangled port channel.
177 LockHolder lock(m_mutex);
178 return m_entangledChannel;
181 void PlatformMessagePortChannel::closeInternal()
183 LockHolder lock(m_mutex);
184 // Disentangle ourselves from the other end. We still maintain a reference to our incoming queue, since previously-existing messages should still be delivered.
185 m_remotePort = nullptr;
186 m_entangledChannel = nullptr;
187 m_outgoingQueue = nullptr;
190 } // namespace WebCore