2 * Copyright (C) 2009 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "PlatformMessagePortChannel.h"
34 #include "MessagePort.h"
35 #include "ScriptExecutionContext.h"
39 // MessagePortChannel implementations - just delegate to the PlatformMessagePortChannel.
40 void MessagePortChannel::createChannel(PassRefPtr<MessagePort> port1, PassRefPtr<MessagePort> port2)
42 PlatformMessagePortChannel::createChannel(port1, port2);
45 PassOwnPtr<MessagePortChannel> MessagePortChannel::create(PassRefPtr<PlatformMessagePortChannel> channel)
47 return adoptPtr(new MessagePortChannel(channel));
50 MessagePortChannel::MessagePortChannel(PassRefPtr<PlatformMessagePortChannel> channel)
55 MessagePortChannel::~MessagePortChannel()
57 // Make sure we close our platform channel when the base is freed, to keep the channel objects from leaking.
61 bool MessagePortChannel::entangleIfOpen(MessagePort* port)
63 return m_channel->entangleIfOpen(port);
66 void MessagePortChannel::disentangle()
68 m_channel->disentangle();
71 void MessagePortChannel::postMessageToRemote(PassOwnPtr<MessagePortChannel::EventData> message)
73 m_channel->postMessageToRemote(message);
76 bool MessagePortChannel::tryGetMessageFromRemote(OwnPtr<MessagePortChannel::EventData>& result)
78 return m_channel->tryGetMessageFromRemote(result);
81 void MessagePortChannel::close()
86 bool MessagePortChannel::isConnectedTo(MessagePort* port)
88 return m_channel->isConnectedTo(port);
91 bool MessagePortChannel::hasPendingActivity()
93 return m_channel->hasPendingActivity();
96 MessagePort* MessagePortChannel::locallyEntangledPort(const ScriptExecutionContext* context)
98 return m_channel->locallyEntangledPort(context);
101 PassRefPtr<PlatformMessagePortChannel> PlatformMessagePortChannel::create(PassRefPtr<MessagePortQueue> incoming, PassRefPtr<MessagePortQueue> outgoing)
103 return adoptRef(new PlatformMessagePortChannel(incoming, outgoing));
106 PlatformMessagePortChannel::PlatformMessagePortChannel(PassRefPtr<MessagePortQueue> incoming, PassRefPtr<MessagePortQueue> outgoing)
107 : m_entangledChannel(0)
108 , m_incomingQueue(incoming)
109 , m_outgoingQueue(outgoing)
114 PlatformMessagePortChannel::~PlatformMessagePortChannel()
118 void PlatformMessagePortChannel::createChannel(PassRefPtr<MessagePort> port1, PassRefPtr<MessagePort> port2)
120 // Create incoming/outgoing queues.
121 RefPtr<PlatformMessagePortChannel::MessagePortQueue> queue1 = PlatformMessagePortChannel::MessagePortQueue::create();
122 RefPtr<PlatformMessagePortChannel::MessagePortQueue> queue2 = PlatformMessagePortChannel::MessagePortQueue::create();
124 // Create proxies for each endpoint.
125 RefPtr<PlatformMessagePortChannel> channel1 = PlatformMessagePortChannel::create(queue1, queue2);
126 RefPtr<PlatformMessagePortChannel> channel2 = PlatformMessagePortChannel::create(queue2, queue1);
128 // Entangle the two endpoints.
129 channel1->setEntangledChannel(channel2);
130 channel2->setEntangledChannel(channel1);
132 // Now entangle the proxies with the appropriate local ports.
133 port1->entangle(MessagePortChannel::create(channel2));
134 port2->entangle(MessagePortChannel::create(channel1));
137 bool PlatformMessagePortChannel::entangleIfOpen(MessagePort* port)
139 // We can't call member functions on our remote pair while holding our mutex or we'll deadlock, but we need to guard against the remote port getting closed/freed, so create a standalone reference.
140 RefPtr<PlatformMessagePortChannel> remote = entangledChannel();
143 remote->setRemotePort(port);
147 void PlatformMessagePortChannel::disentangle()
149 RefPtr<PlatformMessagePortChannel> remote = entangledChannel();
151 remote->setRemotePort(0);
154 void PlatformMessagePortChannel::setRemotePort(MessagePort* port)
156 MutexLocker lock(m_mutex);
157 // Should never set port if it is already set.
158 ASSERT(!port || !m_remotePort);
162 MessagePort* PlatformMessagePortChannel::remotePort()
164 MutexLocker lock(m_mutex);
168 PassRefPtr<PlatformMessagePortChannel> PlatformMessagePortChannel::entangledChannel()
170 MutexLocker lock(m_mutex);
171 return m_entangledChannel;
174 void PlatformMessagePortChannel::setEntangledChannel(PassRefPtr<PlatformMessagePortChannel> remote)
176 MutexLocker lock(m_mutex);
177 // Should only be set as part of initial creation/entanglement.
179 ASSERT(!m_entangledChannel.get());
180 m_entangledChannel = remote;
183 void PlatformMessagePortChannel::postMessageToRemote(PassOwnPtr<MessagePortChannel::EventData> message)
185 MutexLocker lock(m_mutex);
186 if (!m_outgoingQueue)
188 bool wasEmpty = m_outgoingQueue->appendAndCheckEmpty(message);
189 if (wasEmpty && m_remotePort)
190 m_remotePort->messageAvailable();
193 bool PlatformMessagePortChannel::tryGetMessageFromRemote(OwnPtr<MessagePortChannel::EventData>& result)
195 MutexLocker lock(m_mutex);
196 result = m_incomingQueue->tryGetMessage();
200 bool PlatformMessagePortChannel::isConnectedTo(MessagePort* port)
202 MutexLocker lock(m_mutex);
203 return m_remotePort == port;
206 // Closes the port so no further messages can be sent from either end.
207 void PlatformMessagePortChannel::close()
209 RefPtr<PlatformMessagePortChannel> remote = entangledChannel();
213 remote->closeInternal();
216 void PlatformMessagePortChannel::closeInternal()
218 MutexLocker lock(m_mutex);
219 // Disentangle ourselves from the other end. We still maintain a reference to our incoming queue, since previously-existing messages should still be delivered.
221 m_entangledChannel = 0;
225 bool PlatformMessagePortChannel::hasPendingActivity()
227 MutexLocker lock(m_mutex);
228 return !m_incomingQueue->isEmpty();
231 MessagePort* PlatformMessagePortChannel::locallyEntangledPort(const ScriptExecutionContext* context)
233 MutexLocker lock(m_mutex);
234 // See if both contexts are run by the same thread (are the same context, or are both documents).
236 // The remote port's ScriptExecutionContext is guaranteed not to change here - MessagePort::contextDestroyed() will close the port before the context goes away, and close() will block because we are holding the mutex.
237 ScriptExecutionContext* remoteContext = m_remotePort->scriptExecutionContext();
238 if (remoteContext == context || (remoteContext && remoteContext->isDocument() && context->isDocument()))
244 } // namespace WebCore