|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 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. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include "config.h" |
| 27 | +#include "InProcessMessagePortChannel.h" |
| 28 | + |
| 29 | +#include "MessagePort.h" |
| 30 | +#include <wtf/Locker.h> |
| 31 | + |
| 32 | +namespace WebCore { |
| 33 | + |
| 34 | +void InProcessMessagePortChannel::createChannelBetweenPorts(MessagePort& port1, MessagePort& port2) |
| 35 | +{ |
| 36 | + auto queue1 = MessagePortQueue::create(); |
| 37 | + auto queue2 = MessagePortQueue::create(); |
| 38 | + |
| 39 | + auto channel1 = InProcessMessagePortChannel::create(queue1.get(), queue2.get()); |
| 40 | + auto channel2 = InProcessMessagePortChannel::create(queue2.get(), queue1.get()); |
| 41 | + |
| 42 | + channel1->m_entangledChannel = channel2.ptr(); |
| 43 | + channel2->m_entangledChannel = channel1.ptr(); |
| 44 | + |
| 45 | + port1.entangle(WTFMove(channel2)); |
| 46 | + port2.entangle(WTFMove(channel1)); |
| 47 | +} |
| 48 | + |
| 49 | +Ref<InProcessMessagePortChannel> InProcessMessagePortChannel::create(MessagePortQueue& incoming, MessagePortQueue& outgoing) |
| 50 | +{ |
| 51 | + return adoptRef(*new InProcessMessagePortChannel(incoming, outgoing)); |
| 52 | +} |
| 53 | + |
| 54 | +InProcessMessagePortChannel::InProcessMessagePortChannel(MessagePortQueue& incoming, MessagePortQueue& outgoing) |
| 55 | + : m_incomingQueue(&incoming) |
| 56 | + , m_outgoingQueue(&outgoing) |
| 57 | +{ |
| 58 | +} |
| 59 | + |
| 60 | +InProcessMessagePortChannel::~InProcessMessagePortChannel() |
| 61 | +{ |
| 62 | + // Channels being destroyed should to have been closed. |
| 63 | + ASSERT(!m_outgoingQueue); |
| 64 | +} |
| 65 | + |
| 66 | +void InProcessMessagePortChannel::postMessageToRemote(Ref<SerializedScriptValue>&& message, std::unique_ptr<MessagePortChannelArray>&& channels) |
| 67 | +{ |
| 68 | + Locker<Lock> locker(m_lock); |
| 69 | + |
| 70 | + if (!m_outgoingQueue) |
| 71 | + return; |
| 72 | + |
| 73 | + bool wasEmpty = m_outgoingQueue->appendAndCheckEmpty(std::make_unique<EventData>(WTFMove(message), WTFMove(channels))); |
| 74 | + if (wasEmpty && m_remotePort) |
| 75 | + m_remotePort->messageAvailable(); |
| 76 | +} |
| 77 | + |
| 78 | +Deque<std::unique_ptr<MessagePortChannel::EventData>> InProcessMessagePortChannel::takeAllMessagesFromRemote() |
| 79 | +{ |
| 80 | + Locker<Lock> locker(m_lock); |
| 81 | + return m_incomingQueue->takeAllMessages(); |
| 82 | +} |
| 83 | + |
| 84 | +bool InProcessMessagePortChannel::isConnectedTo(MessagePort& port) |
| 85 | +{ |
| 86 | + // FIXME: What guarantees that the result remains the same after we release the lock? |
| 87 | + Locker<Lock> locker(m_lock); |
| 88 | + return m_remotePort == &port; |
| 89 | +} |
| 90 | + |
| 91 | +bool InProcessMessagePortChannel::entangleIfOpen(MessagePort& port) |
| 92 | +{ |
| 93 | + // We can't call member functions on our remote pair while holding our mutex or we'll deadlock, |
| 94 | + // but we need to guard against the remote port getting closed/freed, so create a standalone reference. |
| 95 | + RefPtr<InProcessMessagePortChannel> remote; |
| 96 | + { |
| 97 | + Locker<Lock> locker(m_lock); |
| 98 | + remote = m_entangledChannel; |
| 99 | + } |
| 100 | + |
| 101 | + if (!remote) |
| 102 | + return false; |
| 103 | + |
| 104 | + remote->setRemotePort(&port); |
| 105 | + |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +void InProcessMessagePortChannel::disentangle() |
| 110 | +{ |
| 111 | + Locker<Lock> locker(m_lock); |
| 112 | + |
| 113 | + if (m_entangledChannel) |
| 114 | + m_entangledChannel->setRemotePort(nullptr); |
| 115 | +} |
| 116 | + |
| 117 | +bool InProcessMessagePortChannel::hasPendingActivity() |
| 118 | +{ |
| 119 | + // FIXME: What guarantees that the result remains the same after we release the lock? |
| 120 | + Locker<Lock> locker(m_lock); |
| 121 | + return !m_incomingQueue->isEmpty(); |
| 122 | +} |
| 123 | + |
| 124 | +MessagePort* InProcessMessagePortChannel::locallyEntangledPort(const ScriptExecutionContext* context) |
| 125 | +{ |
| 126 | + Locker<Lock> locker(m_lock); |
| 127 | + |
| 128 | + // See if both contexts are run by the same thread (are the same context, or are both documents). |
| 129 | + if (!m_remotePort) |
| 130 | + return nullptr; |
| 131 | + |
| 132 | + // The remote port's ScriptExecutionContext is guaranteed not to change here - MessagePort::contextDestroyed() |
| 133 | + // will close the port before the context goes away, and close() will block because we are holding the mutex. |
| 134 | + ScriptExecutionContext* remoteContext = m_remotePort->scriptExecutionContext(); |
| 135 | + if (remoteContext == context || (remoteContext && remoteContext->isDocument() && context->isDocument())) |
| 136 | + return m_remotePort; |
| 137 | + |
| 138 | + return nullptr; |
| 139 | +} |
| 140 | + |
| 141 | +RefPtr<InProcessMessagePortChannel> InProcessMessagePortChannel::takeEntangledChannel() |
| 142 | +{ |
| 143 | + RefPtr<InProcessMessagePortChannel> channel; |
| 144 | + |
| 145 | + { |
| 146 | + Locker<Lock> locker(m_lock); |
| 147 | + channel = WTFMove(m_entangledChannel); |
| 148 | + } |
| 149 | + |
| 150 | + return channel; |
| 151 | +} |
| 152 | + |
| 153 | +void InProcessMessagePortChannel::close() |
| 154 | +{ |
| 155 | + Locker<Lock> locker(m_lock); |
| 156 | + |
| 157 | + RefPtr<InProcessMessagePortChannel> channel; |
| 158 | + if (m_entangledChannel) { |
| 159 | + channel = m_entangledChannel->takeEntangledChannel(); |
| 160 | + ASSERT(channel == this); |
| 161 | + m_entangledChannel->close(); |
| 162 | + } |
| 163 | + |
| 164 | + // Disentangle ourselves from the other end. We still maintain a reference to our incoming queue, since previously-existing messages should still be delivered. |
| 165 | + m_remotePort = nullptr; |
| 166 | + m_outgoingQueue = nullptr; |
| 167 | + m_entangledChannel = nullptr; |
| 168 | +} |
| 169 | + |
| 170 | +void InProcessMessagePortChannel::setRemotePort(MessagePort* port) |
| 171 | +{ |
| 172 | + Locker<Lock> locker(m_lock); |
| 173 | + |
| 174 | + // Should never set port if it is already set. |
| 175 | + ASSERT(!port || !m_remotePort); |
| 176 | + |
| 177 | + m_remotePort = port; |
| 178 | +} |
| 179 | + |
| 180 | +} // namespace WebCore |
0 commit comments