2 * Copyright (C) 2010 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 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.
27 #include "Connection.h"
30 #include <wtf/CurrentTime.h>
31 #include <wtf/HashSet.h>
32 #include <wtf/NeverDestroyed.h>
33 #include <wtf/RunLoop.h>
34 #include <wtf/text/WTFString.h>
35 #include <wtf/threads/BinarySemaphore.h>
39 struct WaitForMessageState {
40 WaitForMessageState(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, unsigned waitForMessageFlags)
41 : messageReceiverName(messageReceiverName)
42 , messageName(messageName)
43 , destinationID(destinationID)
44 , waitForMessageFlags(waitForMessageFlags)
48 StringReference messageReceiverName;
49 StringReference messageName;
50 uint64_t destinationID;
52 unsigned waitForMessageFlags;
53 bool messageWaitingInterrupted = false;
55 std::unique_ptr<MessageDecoder> decoder;
58 class Connection::SyncMessageState : public ThreadSafeRefCounted<Connection::SyncMessageState> {
60 static Ref<SyncMessageState> getOrCreate(RunLoop&);
63 void wakeUpClientRunLoop()
65 m_waitForSyncReplySemaphore.signal();
68 bool wait(double absoluteTime)
70 return m_waitForSyncReplySemaphore.wait(absoluteTime);
73 // Returns true if this message will be handled on a client thread that is currently
74 // waiting for a reply to a synchronous message.
75 bool processIncomingMessage(Connection&, std::unique_ptr<MessageDecoder>&);
77 // Dispatch pending sync messages. if allowedConnection is not null, will only dispatch messages
78 // from that connection and put the other messages back in the queue.
79 void dispatchMessages(Connection* allowedConnection);
82 explicit SyncMessageState(RunLoop&);
84 typedef HashMap<RunLoop*, SyncMessageState*> SyncMessageStateMap;
85 static SyncMessageStateMap& syncMessageStateMap()
87 static NeverDestroyed<SyncMessageStateMap> syncMessageStateMap;
88 return syncMessageStateMap;
91 static std::mutex& syncMessageStateMapMutex()
93 static LazyNeverDestroyed<std::mutex> syncMessageStateMapMutex;
94 static std::once_flag onceFlag;
95 std::call_once(onceFlag, [] {
96 syncMessageStateMapMutex.construct();
99 return syncMessageStateMapMutex;
102 void dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection&);
105 BinarySemaphore m_waitForSyncReplySemaphore;
107 // Protects m_didScheduleDispatchMessagesWorkSet and m_messagesToDispatchWhileWaitingForSyncReply.
110 // The set of connections for which we've scheduled a call to dispatchMessageAndResetDidScheduleDispatchMessagesForConnection.
111 HashSet<RefPtr<Connection>> m_didScheduleDispatchMessagesWorkSet;
113 struct ConnectionAndIncomingMessage {
114 Ref<Connection> connection;
115 std::unique_ptr<MessageDecoder> message;
117 Vector<ConnectionAndIncomingMessage> m_messagesToDispatchWhileWaitingForSyncReply;
120 class Connection::SecondaryThreadPendingSyncReply {
122 // The reply decoder, will be null if there was an error processing the sync message on the other side.
123 std::unique_ptr<MessageDecoder> replyDecoder;
125 BinarySemaphore semaphore;
129 Ref<Connection::SyncMessageState> Connection::SyncMessageState::getOrCreate(RunLoop& runLoop)
131 std::lock_guard<std::mutex> lock(syncMessageStateMapMutex());
133 auto& slot = syncMessageStateMap().add(&runLoop, nullptr).iterator->value;
137 Ref<SyncMessageState> syncMessageState = adoptRef(*new SyncMessageState(runLoop));
138 slot = syncMessageState.ptr();
140 return syncMessageState;
143 Connection::SyncMessageState::SyncMessageState(RunLoop& runLoop)
148 Connection::SyncMessageState::~SyncMessageState()
150 std::lock_guard<std::mutex> lock(syncMessageStateMapMutex());
152 ASSERT(syncMessageStateMap().contains(&m_runLoop));
153 syncMessageStateMap().remove(&m_runLoop);
155 ASSERT(m_messagesToDispatchWhileWaitingForSyncReply.isEmpty());
158 bool Connection::SyncMessageState::processIncomingMessage(Connection& connection, std::unique_ptr<MessageDecoder>& message)
160 if (!message->shouldDispatchMessageWhenWaitingForSyncReply())
163 ConnectionAndIncomingMessage connectionAndIncomingMessage { connection, WTF::move(message) };
166 std::lock_guard<std::mutex> lock(m_mutex);
168 if (m_didScheduleDispatchMessagesWorkSet.add(&connection).isNewEntry) {
169 RefPtr<Connection> protectedConnection(&connection);
170 m_runLoop.dispatch([this, protectedConnection] {
171 dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(*protectedConnection);
175 m_messagesToDispatchWhileWaitingForSyncReply.append(WTF::move(connectionAndIncomingMessage));
178 wakeUpClientRunLoop();
183 void Connection::SyncMessageState::dispatchMessages(Connection* allowedConnection)
185 ASSERT(&m_runLoop == &RunLoop::current());
187 Vector<ConnectionAndIncomingMessage> messagesToDispatchWhileWaitingForSyncReply;
190 std::lock_guard<std::mutex> lock(m_mutex);
191 m_messagesToDispatchWhileWaitingForSyncReply.swap(messagesToDispatchWhileWaitingForSyncReply);
194 Vector<ConnectionAndIncomingMessage> messagesToPutBack;
196 for (size_t i = 0; i < messagesToDispatchWhileWaitingForSyncReply.size(); ++i) {
197 ConnectionAndIncomingMessage& connectionAndIncomingMessage = messagesToDispatchWhileWaitingForSyncReply[i];
199 if (allowedConnection && allowedConnection != connectionAndIncomingMessage.connection.ptr()) {
200 // This incoming message belongs to another connection and we don't want to dispatch it now
201 // so mark it to be put back in the message queue.
202 messagesToPutBack.append(WTF::move(connectionAndIncomingMessage));
206 connectionAndIncomingMessage.connection->dispatchMessage(WTF::move(connectionAndIncomingMessage.message));
209 if (!messagesToPutBack.isEmpty()) {
210 std::lock_guard<std::mutex> lock(m_mutex);
212 for (auto& message : messagesToPutBack)
213 m_messagesToDispatchWhileWaitingForSyncReply.append(WTF::move(message));
217 void Connection::SyncMessageState::dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection& connection)
220 std::lock_guard<std::mutex> lock(m_mutex);
221 ASSERT(m_didScheduleDispatchMessagesWorkSet.contains(&connection));
222 m_didScheduleDispatchMessagesWorkSet.remove(&connection);
225 dispatchMessages(&connection);
228 Ref<Connection> Connection::createServerConnection(Identifier identifier, Client& client, RunLoop& clientRunLoop)
230 return adoptRef(*new Connection(identifier, true, client, clientRunLoop));
233 Ref<Connection> Connection::createClientConnection(Identifier identifier, Client& client, RunLoop& clientRunLoop)
235 return adoptRef(*new Connection(identifier, false, client, clientRunLoop));
238 Connection::Connection(Identifier identifier, bool isServer, Client& client, RunLoop& clientRunLoop)
240 , m_isServer(isServer)
242 , m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(false)
243 , m_shouldExitOnSyncMessageSendFailure(false)
244 , m_didCloseOnConnectionWorkQueueCallback(0)
245 , m_isConnected(false)
246 , m_connectionQueue(WorkQueue::create("com.apple.IPC.ReceiveQueue"))
247 , m_clientRunLoop(clientRunLoop)
248 , m_inSendSyncCount(0)
249 , m_inDispatchMessageCount(0)
250 , m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount(0)
251 , m_didReceiveInvalidMessage(false)
252 , m_waitingForMessage(nullptr)
253 , m_syncMessageState(SyncMessageState::getOrCreate(clientRunLoop))
254 , m_shouldWaitForSyncReplies(true)
258 platformInitialize(identifier);
261 Connection::~Connection()
266 void Connection::setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(bool flag)
268 ASSERT(!m_isConnected);
270 m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage = flag;
273 void Connection::setShouldExitOnSyncMessageSendFailure(bool shouldExitOnSyncMessageSendFailure)
275 ASSERT(!m_isConnected);
277 m_shouldExitOnSyncMessageSendFailure = shouldExitOnSyncMessageSendFailure;
280 void Connection::addWorkQueueMessageReceiver(StringReference messageReceiverName, WorkQueue* workQueue, WorkQueueMessageReceiver* workQueueMessageReceiver)
282 ASSERT(&RunLoop::current() == &m_clientRunLoop);
284 RefPtr<Connection> connection(this);
285 m_connectionQueue->dispatch([connection, messageReceiverName, workQueue, workQueueMessageReceiver] {
286 ASSERT(!connection->m_workQueueMessageReceivers.contains(messageReceiverName));
288 connection->m_workQueueMessageReceivers.add(messageReceiverName, std::make_pair(workQueue, workQueueMessageReceiver));
292 void Connection::removeWorkQueueMessageReceiver(StringReference messageReceiverName)
294 ASSERT(&RunLoop::current() == &m_clientRunLoop);
296 RefPtr<Connection> connection(this);
297 m_connectionQueue->dispatch([connection, messageReceiverName] {
298 ASSERT(connection->m_workQueueMessageReceivers.contains(messageReceiverName));
299 connection->m_workQueueMessageReceivers.remove(messageReceiverName);
303 void Connection::dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver& workQueueMessageReceiver, MessageDecoder& decoder)
305 if (!decoder.isSyncMessage()) {
306 workQueueMessageReceiver.didReceiveMessage(*this, decoder);
310 uint64_t syncRequestID = 0;
311 if (!decoder.decode(syncRequestID) || !syncRequestID) {
312 // We received an invalid sync message.
313 // FIXME: Handle this.
314 decoder.markInvalid();
319 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID, decoder.UUID());
321 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID);
324 // Hand off both the decoder and encoder to the work queue message receiver.
325 workQueueMessageReceiver.didReceiveSyncMessage(*this, decoder, replyEncoder);
327 // FIXME: If the message was invalid, we should send back a SyncMessageError.
328 ASSERT(!decoder.isInvalid());
331 sendSyncReply(WTF::move(replyEncoder));
334 void Connection::setDidCloseOnConnectionWorkQueueCallback(DidCloseOnConnectionWorkQueueCallback callback)
336 ASSERT(!m_isConnected);
338 m_didCloseOnConnectionWorkQueueCallback = callback;
341 void Connection::invalidate()
344 // Someone already called invalidate().
350 RefPtr<Connection> protectedThis(this);
351 m_connectionQueue->dispatch([protectedThis] {
352 protectedThis->platformInvalidate();
356 void Connection::markCurrentlyDispatchedMessageAsInvalid()
358 // This should only be called while processing a message.
359 ASSERT(m_inDispatchMessageCount > 0);
361 m_didReceiveInvalidMessage = true;
364 std::unique_ptr<MessageEncoder> Connection::createSyncMessageEncoder(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, uint64_t& syncRequestID)
366 auto encoder = std::make_unique<MessageEncoder>(messageReceiverName, messageName, destinationID);
367 encoder->setIsSyncMessage(true);
369 // Encode the sync request ID.
370 syncRequestID = ++m_syncRequestID;
371 *encoder << syncRequestID;
376 bool Connection::sendMessage(std::unique_ptr<MessageEncoder> encoder, unsigned messageSendFlags, bool alreadyRecordedMessage)
381 if (messageSendFlags & DispatchMessageEvenWhenWaitingForSyncReply
382 && (!m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage
383 || m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount))
384 encoder->setShouldDispatchMessageWhenWaitingForSyncReply(true);
387 std::unique_ptr<MessageRecorder::MessageProcessingToken> token;
388 if (!alreadyRecordedMessage)
389 token = MessageRecorder::recordOutgoingMessage(*this, *encoder);
391 UNUSED_PARAM(alreadyRecordedMessage);
395 std::lock_guard<std::mutex> lock(m_outgoingMessagesMutex);
396 m_outgoingMessages.append(WTF::move(encoder));
399 // FIXME: We should add a boolean flag so we don't call this when work has already been scheduled.
400 RefPtr<Connection> protectedThis(this);
401 m_connectionQueue->dispatch([protectedThis] {
402 protectedThis->sendOutgoingMessages();
407 bool Connection::sendSyncReply(std::unique_ptr<MessageEncoder> encoder)
409 return sendMessage(WTF::move(encoder));
412 std::unique_ptr<MessageDecoder> Connection::waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags)
414 ASSERT(&m_clientRunLoop == &RunLoop::current());
416 // First, check if this message is already in the incoming messages queue.
418 std::lock_guard<std::mutex> lock(m_incomingMessagesMutex);
420 for (auto it = m_incomingMessages.begin(), end = m_incomingMessages.end(); it != end; ++it) {
421 std::unique_ptr<MessageDecoder>& message = *it;
423 if (message->messageReceiverName() == messageReceiverName && message->messageName() == messageName && message->destinationID() == destinationID) {
424 std::unique_ptr<MessageDecoder> returnedMessage = WTF::move(message);
426 m_incomingMessages.remove(it);
427 return returnedMessage;
432 WaitForMessageState waitingForMessage(messageReceiverName, messageName, destinationID, waitForMessageFlags);
435 std::lock_guard<std::mutex> lock(m_waitForMessageMutex);
437 // We don't support having multiple clients waiting for messages.
438 ASSERT(!m_waitingForMessage);
440 m_waitingForMessage = &waitingForMessage;
443 // Now wait for it to be set.
445 std::unique_lock<std::mutex> lock(m_waitForMessageMutex);
447 if (m_waitingForMessage->decoder) {
448 auto decoder = WTF::move(m_waitingForMessage->decoder);
449 m_waitingForMessage = nullptr;
454 std::cv_status status = m_waitForMessageCondition.wait_for(lock, timeout);
455 // We timed out, lost our connection, or a sync message came in with InterruptWaitingIfSyncMessageArrives, so stop waiting.
456 if (status == std::cv_status::timeout || m_waitingForMessage->messageWaitingInterrupted)
460 m_waitingForMessage = nullptr;
465 std::unique_ptr<MessageDecoder> Connection::sendSyncMessage(uint64_t syncRequestID, std::unique_ptr<MessageEncoder> encoder, std::chrono::milliseconds timeout, unsigned syncSendFlags)
467 if (&RunLoop::current() != &m_clientRunLoop) {
468 // No flags are supported for synchronous messages sent from secondary threads.
469 ASSERT(!syncSendFlags);
470 return sendSyncMessageFromSecondaryThread(syncRequestID, WTF::move(encoder), timeout);
474 didFailToSendSyncMessage();
478 // Push the pending sync reply information on our stack.
480 MutexLocker locker(m_syncReplyStateMutex);
481 if (!m_shouldWaitForSyncReplies) {
482 didFailToSendSyncMessage();
486 m_pendingSyncReplies.append(PendingSyncReply(syncRequestID));
492 auto token = MessageRecorder::recordOutgoingMessage(*this, *encoder);
495 // First send the message.
496 sendMessage(WTF::move(encoder), DispatchMessageEvenWhenWaitingForSyncReply, true);
498 // Then wait for a reply. Waiting for a reply could involve dispatching incoming sync messages, so
499 // keep an extra reference to the connection here in case it's invalidated.
500 Ref<Connection> protect(*this);
501 std::unique_ptr<MessageDecoder> reply = waitForSyncReply(syncRequestID, timeout, syncSendFlags);
505 // Finally, pop the pending sync reply information.
507 MutexLocker locker(m_syncReplyStateMutex);
508 ASSERT(m_pendingSyncReplies.last().syncRequestID == syncRequestID);
509 m_pendingSyncReplies.removeLast();
513 didFailToSendSyncMessage();
518 std::unique_ptr<MessageDecoder> Connection::sendSyncMessageFromSecondaryThread(uint64_t syncRequestID, std::unique_ptr<MessageEncoder> encoder, std::chrono::milliseconds timeout)
520 ASSERT(&RunLoop::current() != &m_clientRunLoop);
525 SecondaryThreadPendingSyncReply pendingReply;
527 // Push the pending sync reply information on our stack.
529 MutexLocker locker(m_syncReplyStateMutex);
530 if (!m_shouldWaitForSyncReplies)
533 ASSERT(!m_secondaryThreadPendingSyncReplyMap.contains(syncRequestID));
534 m_secondaryThreadPendingSyncReplyMap.add(syncRequestID, &pendingReply);
538 auto token = MessageRecorder::recordOutgoingMessage(*this, *encoder);
541 sendMessage(WTF::move(encoder), 0, true);
543 pendingReply.semaphore.wait(currentTime() + (timeout.count() / 1000.0));
545 // Finally, pop the pending sync reply information.
547 MutexLocker locker(m_syncReplyStateMutex);
548 ASSERT(m_secondaryThreadPendingSyncReplyMap.contains(syncRequestID));
549 m_secondaryThreadPendingSyncReplyMap.remove(syncRequestID);
552 return WTF::move(pendingReply.replyDecoder);
555 std::unique_ptr<MessageDecoder> Connection::waitForSyncReply(uint64_t syncRequestID, std::chrono::milliseconds timeout, unsigned syncSendFlags)
557 double absoluteTime = currentTime() + (timeout.count() / 1000.0);
559 willSendSyncMessage(syncSendFlags);
561 bool timedOut = false;
563 // First, check if we have any messages that we need to process.
564 m_syncMessageState->dispatchMessages(nullptr);
567 MutexLocker locker(m_syncReplyStateMutex);
569 // Second, check if there is a sync reply at the top of the stack.
570 ASSERT(!m_pendingSyncReplies.isEmpty());
572 PendingSyncReply& pendingSyncReply = m_pendingSyncReplies.last();
573 ASSERT_UNUSED(syncRequestID, pendingSyncReply.syncRequestID == syncRequestID);
575 // We found the sync reply, or the connection was closed.
576 if (pendingSyncReply.didReceiveReply || !m_shouldWaitForSyncReplies) {
577 didReceiveSyncReply(syncSendFlags);
578 return WTF::move(pendingSyncReply.replyDecoder);
582 // Processing a sync message could cause the connection to be invalidated.
583 // (If the handler ends up calling Connection::invalidate).
584 // If that happens, we need to stop waiting, or we'll hang since we won't get
585 // any more incoming messages.
587 didReceiveSyncReply(syncSendFlags);
591 // We didn't find a sync reply yet, keep waiting.
592 // This allows the WebProcess to still serve clients while waiting for the message to return.
593 // Notably, it can continue to process accessibility requests, which are on the main thread.
594 if (syncSendFlags & SpinRunLoopWhileWaitingForReply) {
596 // FIXME: Although we run forever, any events incoming will cause us to drop out and exit out. This however doesn't
597 // account for a timeout value passed in. Timeout is always NoTimeout in these cases, but that could change.
598 RunLoop::current().runForDuration(1e10);
599 timedOut = currentTime() >= absoluteTime;
602 timedOut = !m_syncMessageState->wait(absoluteTime);
606 didReceiveSyncReply(syncSendFlags);
611 void Connection::processIncomingSyncReply(std::unique_ptr<MessageDecoder> decoder)
613 MutexLocker locker(m_syncReplyStateMutex);
615 // Go through the stack of sync requests that have pending replies and see which one
616 // this reply is for.
617 for (size_t i = m_pendingSyncReplies.size(); i > 0; --i) {
618 PendingSyncReply& pendingSyncReply = m_pendingSyncReplies[i - 1];
620 if (pendingSyncReply.syncRequestID != decoder->destinationID())
623 ASSERT(!pendingSyncReply.replyDecoder);
625 pendingSyncReply.replyDecoder = WTF::move(decoder);
626 pendingSyncReply.didReceiveReply = true;
628 // We got a reply to the last send message, wake up the client run loop so it can be processed.
629 if (i == m_pendingSyncReplies.size())
630 m_syncMessageState->wakeUpClientRunLoop();
635 // If it's not a reply to any primary thread message, check if it is a reply to a secondary thread one.
636 SecondaryThreadPendingSyncReplyMap::iterator secondaryThreadReplyMapItem = m_secondaryThreadPendingSyncReplyMap.find(decoder->destinationID());
637 if (secondaryThreadReplyMapItem != m_secondaryThreadPendingSyncReplyMap.end()) {
638 SecondaryThreadPendingSyncReply* reply = secondaryThreadReplyMapItem->value;
639 ASSERT(!reply->replyDecoder);
640 reply->replyDecoder = WTF::move(decoder);
641 reply->semaphore.signal();
644 // If we get here, it means we got a reply for a message that wasn't in the sync request stack or map.
645 // This can happen if the send timed out, so it's fine to ignore.
648 void Connection::processIncomingMessage(std::unique_ptr<MessageDecoder> message)
650 ASSERT(!message->messageReceiverName().isEmpty());
651 ASSERT(!message->messageName().isEmpty());
653 if (message->messageReceiverName() == "IPC" && message->messageName() == "SyncMessageReply") {
654 processIncomingSyncReply(WTF::move(message));
658 if (!m_workQueueMessageReceivers.isValidKey(message->messageReceiverName())) {
659 RefPtr<Connection> protectedThis(this);
660 StringReference messageReceiverName = message->messageReceiverName();
661 StringCapture capturedMessageReceiverName(messageReceiverName.isEmpty() ? "<unknown message receiver>" : String(messageReceiverName.data(), messageReceiverName.size()));
662 StringReference messageName = message->messageName();
663 StringCapture capturedMessageName(messageName.isEmpty() ? "<unknown message>" : String(messageName.data(), messageName.size()));
665 m_clientRunLoop.dispatch([protectedThis, capturedMessageReceiverName, capturedMessageName] {
666 protectedThis->dispatchDidReceiveInvalidMessage(capturedMessageReceiverName.string().utf8(), capturedMessageName.string().utf8());
671 auto it = m_workQueueMessageReceivers.find(message->messageReceiverName());
672 if (it != m_workQueueMessageReceivers.end()) {
673 RefPtr<Connection> protectedThis(this);
674 RefPtr<WorkQueueMessageReceiver>& workQueueMessageReceiver = it->value.second;
675 MessageDecoder* decoderPtr = message.release();
676 it->value.first->dispatch([protectedThis, workQueueMessageReceiver, decoderPtr] {
677 std::unique_ptr<MessageDecoder> decoder(decoderPtr);
678 protectedThis->dispatchWorkQueueMessageReceiverMessage(*workQueueMessageReceiver, *decoder);
683 // Check if this is a sync message or if it's a message that should be dispatched even when waiting for
684 // a sync reply. If it is, and we're waiting for a sync reply this message needs to be dispatched.
685 // If we don't we'll end up with a deadlock where both sync message senders are stuck waiting for a reply.
686 if (m_syncMessageState->processIncomingMessage(*this, message))
689 // Check if we're waiting for this message.
691 std::lock_guard<std::mutex> lock(m_waitForMessageMutex);
693 if (m_waitingForMessage && m_waitingForMessage->messageReceiverName == message->messageReceiverName() && m_waitingForMessage->messageName == message->messageName() && m_waitingForMessage->destinationID == message->destinationID()) {
694 m_waitingForMessage->decoder = WTF::move(message);
695 ASSERT(m_waitingForMessage->decoder);
696 m_waitForMessageCondition.notify_one();
700 if (m_waitingForMessage && (m_waitingForMessage->waitForMessageFlags & InterruptWaitingIfSyncMessageArrives) && message->isSyncMessage()) {
701 m_waitingForMessage->messageWaitingInterrupted = true;
702 m_waitForMessageCondition.notify_one();
706 enqueueIncomingMessage(WTF::move(message));
709 void Connection::postConnectionDidCloseOnConnectionWorkQueue()
711 RefPtr<Connection> connection(this);
712 m_connectionQueue->dispatch([connection] {
713 connection->connectionDidClose();
717 void Connection::connectionDidClose()
719 // The connection is now invalid.
720 platformInvalidate();
723 MutexLocker locker(m_syncReplyStateMutex);
725 ASSERT(m_shouldWaitForSyncReplies);
726 m_shouldWaitForSyncReplies = false;
728 if (!m_pendingSyncReplies.isEmpty())
729 m_syncMessageState->wakeUpClientRunLoop();
731 for (SecondaryThreadPendingSyncReplyMap::iterator iter = m_secondaryThreadPendingSyncReplyMap.begin(); iter != m_secondaryThreadPendingSyncReplyMap.end(); ++iter)
732 iter->value->semaphore.signal();
736 std::lock_guard<std::mutex> lock(m_waitForMessageMutex);
737 if (m_waitingForMessage)
738 m_waitingForMessage->messageWaitingInterrupted = true;
740 m_waitForMessageCondition.notify_all();
742 if (m_didCloseOnConnectionWorkQueueCallback)
743 m_didCloseOnConnectionWorkQueueCallback(this);
745 RefPtr<Connection> connection(this);
746 m_clientRunLoop.dispatch([connection] {
747 // If the connection has been explicitly invalidated before dispatchConnectionDidClose was called,
748 // then the client will be null here.
749 if (!connection->m_client)
752 // Because we define a connection as being "valid" based on wheter it has a null client, we null out
753 // the client before calling didClose here. Otherwise, sendSync will try to send a message to the connection and
754 // will then wait indefinitely for a reply.
755 Client* client = connection->m_client;
756 connection->m_client = nullptr;
758 client->didClose(*connection);
762 bool Connection::canSendOutgoingMessages() const
764 return m_isConnected && platformCanSendOutgoingMessages();
767 void Connection::sendOutgoingMessages()
769 if (!canSendOutgoingMessages())
773 std::unique_ptr<MessageEncoder> message;
776 std::lock_guard<std::mutex> lock(m_outgoingMessagesMutex);
777 if (m_outgoingMessages.isEmpty())
779 message = m_outgoingMessages.takeFirst();
782 if (!sendOutgoingMessage(WTF::move(message)))
787 void Connection::dispatchSyncMessage(MessageDecoder& decoder)
789 ASSERT(decoder.isSyncMessage());
791 uint64_t syncRequestID = 0;
792 if (!decoder.decode(syncRequestID) || !syncRequestID) {
793 // We received an invalid sync message.
794 decoder.markInvalid();
799 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID, decoder.UUID());
801 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID);
804 // Hand off both the decoder and encoder to the client.
805 m_client->didReceiveSyncMessage(*this, decoder, replyEncoder);
807 // FIXME: If the message was invalid, we should send back a SyncMessageError.
808 ASSERT(!decoder.isInvalid());
811 sendSyncReply(WTF::move(replyEncoder));
814 void Connection::dispatchDidReceiveInvalidMessage(const CString& messageReceiverNameString, const CString& messageNameString)
816 ASSERT(&RunLoop::current() == &m_clientRunLoop);
821 m_client->didReceiveInvalidMessage(*this, StringReference(messageReceiverNameString.data(), messageReceiverNameString.length()), StringReference(messageNameString.data(), messageNameString.length()));
824 void Connection::didFailToSendSyncMessage()
826 if (!m_shouldExitOnSyncMessageSendFailure)
832 void Connection::enqueueIncomingMessage(std::unique_ptr<MessageDecoder> incomingMessage)
835 std::lock_guard<std::mutex> lock(m_incomingMessagesMutex);
836 m_incomingMessages.append(WTF::move(incomingMessage));
839 RefPtr<Connection> protectedThis(this);
840 m_clientRunLoop.dispatch([protectedThis] {
841 protectedThis->dispatchOneMessage();
845 void Connection::dispatchMessage(MessageDecoder& decoder)
847 m_client->didReceiveMessage(*this, decoder);
850 void Connection::dispatchMessage(std::unique_ptr<MessageDecoder> message)
853 MessageRecorder::recordIncomingMessage(*this, *message);
859 m_inDispatchMessageCount++;
861 if (message->shouldDispatchMessageWhenWaitingForSyncReply())
862 m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount++;
864 bool oldDidReceiveInvalidMessage = m_didReceiveInvalidMessage;
865 m_didReceiveInvalidMessage = false;
867 if (message->isSyncMessage())
868 dispatchSyncMessage(*message);
870 dispatchMessage(*message);
872 m_didReceiveInvalidMessage |= message->isInvalid();
873 m_inDispatchMessageCount--;
875 // FIXME: For Delayed synchronous messages, we should not decrement the counter until we send a response.
876 // Otherwise, we would deadlock if processing the message results in a sync message back after we exit this function.
877 if (message->shouldDispatchMessageWhenWaitingForSyncReply())
878 m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount--;
880 if (m_didReceiveInvalidMessage && m_client)
881 m_client->didReceiveInvalidMessage(*this, message->messageReceiverName(), message->messageName());
883 m_didReceiveInvalidMessage = oldDidReceiveInvalidMessage;
886 void Connection::dispatchOneMessage()
888 std::unique_ptr<MessageDecoder> message;
891 std::lock_guard<std::mutex> lock(m_incomingMessagesMutex);
892 if (m_incomingMessages.isEmpty())
895 message = m_incomingMessages.takeFirst();
898 dispatchMessage(WTF::move(message));
901 void Connection::wakeUpRunLoop()
903 m_clientRunLoop.wakeUp();