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 {
60 static SyncMessageState& singleton();
63 ~SyncMessageState() = delete;
65 void wakeUpClientRunLoop()
67 m_waitForSyncReplySemaphore.signal();
70 bool wait(double absoluteTime)
72 return m_waitForSyncReplySemaphore.wait(absoluteTime);
75 // Returns true if this message will be handled on a client thread that is currently
76 // waiting for a reply to a synchronous message.
77 bool processIncomingMessage(Connection&, std::unique_ptr<MessageDecoder>&);
79 // Dispatch pending sync messages. if allowedConnection is not null, will only dispatch messages
80 // from that connection and put the other messages back in the queue.
81 void dispatchMessages(Connection* allowedConnection);
84 void dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection&);
86 BinarySemaphore m_waitForSyncReplySemaphore;
88 // Protects m_didScheduleDispatchMessagesWorkSet and m_messagesToDispatchWhileWaitingForSyncReply.
91 // The set of connections for which we've scheduled a call to dispatchMessageAndResetDidScheduleDispatchMessagesForConnection.
92 HashSet<RefPtr<Connection>> m_didScheduleDispatchMessagesWorkSet;
94 struct ConnectionAndIncomingMessage {
95 Ref<Connection> connection;
96 std::unique_ptr<MessageDecoder> message;
98 Vector<ConnectionAndIncomingMessage> m_messagesToDispatchWhileWaitingForSyncReply;
101 class Connection::SecondaryThreadPendingSyncReply {
103 // The reply decoder, will be null if there was an error processing the sync message on the other side.
104 std::unique_ptr<MessageDecoder> replyDecoder;
106 BinarySemaphore semaphore;
110 Connection::SyncMessageState& Connection::SyncMessageState::singleton()
112 static std::once_flag onceFlag;
113 static LazyNeverDestroyed<SyncMessageState> syncMessageState;
115 std::call_once(onceFlag, [] {
116 syncMessageState.construct();
119 return syncMessageState;
122 Connection::SyncMessageState::SyncMessageState()
126 bool Connection::SyncMessageState::processIncomingMessage(Connection& connection, std::unique_ptr<MessageDecoder>& message)
128 if (!message->shouldDispatchMessageWhenWaitingForSyncReply())
131 ConnectionAndIncomingMessage connectionAndIncomingMessage { connection, WTF::move(message) };
134 std::lock_guard<std::mutex> lock(m_mutex);
136 if (m_didScheduleDispatchMessagesWorkSet.add(&connection).isNewEntry) {
137 RefPtr<Connection> protectedConnection(&connection);
138 RunLoop::main().dispatch([this, protectedConnection] {
139 dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(*protectedConnection);
143 m_messagesToDispatchWhileWaitingForSyncReply.append(WTF::move(connectionAndIncomingMessage));
146 wakeUpClientRunLoop();
151 void Connection::SyncMessageState::dispatchMessages(Connection* allowedConnection)
153 ASSERT(RunLoop::isMain());
155 Vector<ConnectionAndIncomingMessage> messagesToDispatchWhileWaitingForSyncReply;
158 std::lock_guard<std::mutex> lock(m_mutex);
159 m_messagesToDispatchWhileWaitingForSyncReply.swap(messagesToDispatchWhileWaitingForSyncReply);
162 Vector<ConnectionAndIncomingMessage> messagesToPutBack;
164 for (size_t i = 0; i < messagesToDispatchWhileWaitingForSyncReply.size(); ++i) {
165 ConnectionAndIncomingMessage& connectionAndIncomingMessage = messagesToDispatchWhileWaitingForSyncReply[i];
167 if (allowedConnection && allowedConnection != connectionAndIncomingMessage.connection.ptr()) {
168 // This incoming message belongs to another connection and we don't want to dispatch it now
169 // so mark it to be put back in the message queue.
170 messagesToPutBack.append(WTF::move(connectionAndIncomingMessage));
174 connectionAndIncomingMessage.connection->dispatchMessage(WTF::move(connectionAndIncomingMessage.message));
177 if (!messagesToPutBack.isEmpty()) {
178 std::lock_guard<std::mutex> lock(m_mutex);
180 for (auto& message : messagesToPutBack)
181 m_messagesToDispatchWhileWaitingForSyncReply.append(WTF::move(message));
185 void Connection::SyncMessageState::dispatchMessageAndResetDidScheduleDispatchMessagesForConnection(Connection& connection)
188 std::lock_guard<std::mutex> lock(m_mutex);
189 ASSERT(m_didScheduleDispatchMessagesWorkSet.contains(&connection));
190 m_didScheduleDispatchMessagesWorkSet.remove(&connection);
193 dispatchMessages(&connection);
196 Ref<Connection> Connection::createServerConnection(Identifier identifier, Client& client)
198 return adoptRef(*new Connection(identifier, true, client));
201 Ref<Connection> Connection::createClientConnection(Identifier identifier, Client& client)
203 return adoptRef(*new Connection(identifier, false, client));
206 Connection::Connection(Identifier identifier, bool isServer, Client& client)
208 , m_isServer(isServer)
210 , m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(false)
211 , m_shouldExitOnSyncMessageSendFailure(false)
212 , m_didCloseOnConnectionWorkQueueCallback(0)
213 , m_isConnected(false)
214 , m_connectionQueue(WorkQueue::create("com.apple.IPC.ReceiveQueue"))
215 , m_inSendSyncCount(0)
216 , m_inDispatchMessageCount(0)
217 , m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount(0)
218 , m_didReceiveInvalidMessage(false)
219 , m_waitingForMessage(nullptr)
220 , m_shouldWaitForSyncReplies(true)
222 ASSERT(RunLoop::isMain());
224 platformInitialize(identifier);
226 #if HAVE(QOS_CLASSES)
227 ASSERT(pthread_main_np());
228 m_mainThread = pthread_self();
232 Connection::~Connection()
237 void Connection::setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(bool flag)
239 ASSERT(!m_isConnected);
241 m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage = flag;
244 void Connection::setShouldExitOnSyncMessageSendFailure(bool shouldExitOnSyncMessageSendFailure)
246 ASSERT(!m_isConnected);
248 m_shouldExitOnSyncMessageSendFailure = shouldExitOnSyncMessageSendFailure;
251 void Connection::addWorkQueueMessageReceiver(StringReference messageReceiverName, WorkQueue* workQueue, WorkQueueMessageReceiver* workQueueMessageReceiver)
253 ASSERT(RunLoop::isMain());
255 RefPtr<Connection> connection(this);
256 m_connectionQueue->dispatch([connection, messageReceiverName, workQueue, workQueueMessageReceiver] {
257 ASSERT(!connection->m_workQueueMessageReceivers.contains(messageReceiverName));
259 connection->m_workQueueMessageReceivers.add(messageReceiverName, std::make_pair(workQueue, workQueueMessageReceiver));
263 void Connection::removeWorkQueueMessageReceiver(StringReference messageReceiverName)
265 ASSERT(RunLoop::isMain());
267 RefPtr<Connection> connection(this);
268 m_connectionQueue->dispatch([connection, messageReceiverName] {
269 ASSERT(connection->m_workQueueMessageReceivers.contains(messageReceiverName));
270 connection->m_workQueueMessageReceivers.remove(messageReceiverName);
274 void Connection::dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver& workQueueMessageReceiver, MessageDecoder& decoder)
276 if (!decoder.isSyncMessage()) {
277 workQueueMessageReceiver.didReceiveMessage(*this, decoder);
281 uint64_t syncRequestID = 0;
282 if (!decoder.decode(syncRequestID) || !syncRequestID) {
283 // We received an invalid sync message.
284 // FIXME: Handle this.
285 decoder.markInvalid();
290 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID, decoder.UUID());
292 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID);
295 // Hand off both the decoder and encoder to the work queue message receiver.
296 workQueueMessageReceiver.didReceiveSyncMessage(*this, decoder, replyEncoder);
298 // FIXME: If the message was invalid, we should send back a SyncMessageError.
299 ASSERT(!decoder.isInvalid());
302 sendSyncReply(WTF::move(replyEncoder));
305 void Connection::setDidCloseOnConnectionWorkQueueCallback(DidCloseOnConnectionWorkQueueCallback callback)
307 ASSERT(!m_isConnected);
309 m_didCloseOnConnectionWorkQueueCallback = callback;
312 void Connection::invalidate()
315 // Someone already called invalidate().
321 RefPtr<Connection> protectedThis(this);
322 m_connectionQueue->dispatch([protectedThis] {
323 protectedThis->platformInvalidate();
327 void Connection::markCurrentlyDispatchedMessageAsInvalid()
329 // This should only be called while processing a message.
330 ASSERT(m_inDispatchMessageCount > 0);
332 m_didReceiveInvalidMessage = true;
335 std::unique_ptr<MessageEncoder> Connection::createSyncMessageEncoder(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, uint64_t& syncRequestID)
337 auto encoder = std::make_unique<MessageEncoder>(messageReceiverName, messageName, destinationID);
338 encoder->setIsSyncMessage(true);
340 // Encode the sync request ID.
341 syncRequestID = ++m_syncRequestID;
342 *encoder << syncRequestID;
347 bool Connection::sendMessage(std::unique_ptr<MessageEncoder> encoder, unsigned messageSendFlags, bool alreadyRecordedMessage)
352 if (messageSendFlags & DispatchMessageEvenWhenWaitingForSyncReply
353 && (!m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage
354 || m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount))
355 encoder->setShouldDispatchMessageWhenWaitingForSyncReply(true);
358 std::unique_ptr<MessageRecorder::MessageProcessingToken> token;
359 if (!alreadyRecordedMessage)
360 token = MessageRecorder::recordOutgoingMessage(*this, *encoder);
362 UNUSED_PARAM(alreadyRecordedMessage);
366 std::lock_guard<std::mutex> lock(m_outgoingMessagesMutex);
367 m_outgoingMessages.append(WTF::move(encoder));
370 // FIXME: We should add a boolean flag so we don't call this when work has already been scheduled.
371 RefPtr<Connection> protectedThis(this);
372 m_connectionQueue->dispatch([protectedThis] {
373 protectedThis->sendOutgoingMessages();
378 bool Connection::sendSyncReply(std::unique_ptr<MessageEncoder> encoder)
380 return sendMessage(WTF::move(encoder));
383 std::unique_ptr<MessageDecoder> Connection::waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags)
385 ASSERT(RunLoop::isMain());
387 bool hasIncomingSynchronousMessage = false;
389 // First, check if this message is already in the incoming messages queue.
391 std::lock_guard<std::mutex> lock(m_incomingMessagesMutex);
393 for (auto it = m_incomingMessages.begin(), end = m_incomingMessages.end(); it != end; ++it) {
394 std::unique_ptr<MessageDecoder>& message = *it;
396 if (message->messageReceiverName() == messageReceiverName && message->messageName() == messageName && message->destinationID() == destinationID) {
397 std::unique_ptr<MessageDecoder> returnedMessage = WTF::move(message);
399 m_incomingMessages.remove(it);
400 return returnedMessage;
403 if (message->isSyncMessage())
404 hasIncomingSynchronousMessage = true;
408 // Don't even start waiting if we have InterruptWaitingIfSyncMessageArrives and there's a sync message already in the queue.
409 if (hasIncomingSynchronousMessage && waitForMessageFlags & InterruptWaitingIfSyncMessageArrives) {
410 m_waitingForMessage = nullptr;
414 WaitForMessageState waitingForMessage(messageReceiverName, messageName, destinationID, waitForMessageFlags);
417 std::lock_guard<std::mutex> lock(m_waitForMessageMutex);
419 // We don't support having multiple clients waiting for messages.
420 ASSERT(!m_waitingForMessage);
422 m_waitingForMessage = &waitingForMessage;
425 // Now wait for it to be set.
427 std::unique_lock<std::mutex> lock(m_waitForMessageMutex);
429 if (m_waitingForMessage->decoder) {
430 auto decoder = WTF::move(m_waitingForMessage->decoder);
431 m_waitingForMessage = nullptr;
436 std::cv_status status = m_waitForMessageCondition.wait_for(lock, timeout);
437 // We timed out, lost our connection, or a sync message came in with InterruptWaitingIfSyncMessageArrives, so stop waiting.
438 if (status == std::cv_status::timeout || m_waitingForMessage->messageWaitingInterrupted)
442 m_waitingForMessage = nullptr;
447 std::unique_ptr<MessageDecoder> Connection::sendSyncMessage(uint64_t syncRequestID, std::unique_ptr<MessageEncoder> encoder, std::chrono::milliseconds timeout, unsigned syncSendFlags)
449 if (!RunLoop::isMain()) {
450 // No flags are supported for synchronous messages sent from secondary threads.
451 ASSERT(!syncSendFlags);
452 return sendSyncMessageFromSecondaryThread(syncRequestID, WTF::move(encoder), timeout);
456 didFailToSendSyncMessage();
460 // Push the pending sync reply information on our stack.
462 DeprecatedMutexLocker locker(m_syncReplyStateMutex);
463 if (!m_shouldWaitForSyncReplies) {
464 didFailToSendSyncMessage();
468 m_pendingSyncReplies.append(PendingSyncReply(syncRequestID));
474 auto token = MessageRecorder::recordOutgoingMessage(*this, *encoder);
477 // First send the message.
478 sendMessage(WTF::move(encoder), DispatchMessageEvenWhenWaitingForSyncReply, true);
480 // Then wait for a reply. Waiting for a reply could involve dispatching incoming sync messages, so
481 // keep an extra reference to the connection here in case it's invalidated.
482 Ref<Connection> protect(*this);
483 std::unique_ptr<MessageDecoder> reply = waitForSyncReply(syncRequestID, timeout, syncSendFlags);
487 // Finally, pop the pending sync reply information.
489 DeprecatedMutexLocker locker(m_syncReplyStateMutex);
490 ASSERT(m_pendingSyncReplies.last().syncRequestID == syncRequestID);
491 m_pendingSyncReplies.removeLast();
495 didFailToSendSyncMessage();
500 std::unique_ptr<MessageDecoder> Connection::sendSyncMessageFromSecondaryThread(uint64_t syncRequestID, std::unique_ptr<MessageEncoder> encoder, std::chrono::milliseconds timeout)
502 ASSERT(!RunLoop::isMain());
507 SecondaryThreadPendingSyncReply pendingReply;
509 // Push the pending sync reply information on our stack.
511 DeprecatedMutexLocker locker(m_syncReplyStateMutex);
512 if (!m_shouldWaitForSyncReplies)
515 ASSERT(!m_secondaryThreadPendingSyncReplyMap.contains(syncRequestID));
516 m_secondaryThreadPendingSyncReplyMap.add(syncRequestID, &pendingReply);
520 auto token = MessageRecorder::recordOutgoingMessage(*this, *encoder);
523 sendMessage(WTF::move(encoder), 0, true);
525 pendingReply.semaphore.wait(currentTime() + (timeout.count() / 1000.0));
527 // Finally, pop the pending sync reply information.
529 DeprecatedMutexLocker locker(m_syncReplyStateMutex);
530 ASSERT(m_secondaryThreadPendingSyncReplyMap.contains(syncRequestID));
531 m_secondaryThreadPendingSyncReplyMap.remove(syncRequestID);
534 return WTF::move(pendingReply.replyDecoder);
537 std::unique_ptr<MessageDecoder> Connection::waitForSyncReply(uint64_t syncRequestID, std::chrono::milliseconds timeout, unsigned syncSendFlags)
539 double absoluteTime = currentTime() + (timeout.count() / 1000.0);
541 willSendSyncMessage(syncSendFlags);
543 bool timedOut = false;
545 // First, check if we have any messages that we need to process.
546 SyncMessageState::singleton().dispatchMessages(nullptr);
549 DeprecatedMutexLocker locker(m_syncReplyStateMutex);
551 // Second, check if there is a sync reply at the top of the stack.
552 ASSERT(!m_pendingSyncReplies.isEmpty());
554 PendingSyncReply& pendingSyncReply = m_pendingSyncReplies.last();
555 ASSERT_UNUSED(syncRequestID, pendingSyncReply.syncRequestID == syncRequestID);
557 // We found the sync reply, or the connection was closed.
558 if (pendingSyncReply.didReceiveReply || !m_shouldWaitForSyncReplies) {
559 didReceiveSyncReply(syncSendFlags);
560 return WTF::move(pendingSyncReply.replyDecoder);
564 // Processing a sync message could cause the connection to be invalidated.
565 // (If the handler ends up calling Connection::invalidate).
566 // If that happens, we need to stop waiting, or we'll hang since we won't get
567 // any more incoming messages.
569 didReceiveSyncReply(syncSendFlags);
573 // We didn't find a sync reply yet, keep waiting.
574 // This allows the WebProcess to still serve clients while waiting for the message to return.
575 // Notably, it can continue to process accessibility requests, which are on the main thread.
576 if (syncSendFlags & SpinRunLoopWhileWaitingForReply) {
578 // FIXME: Although we run forever, any events incoming will cause us to drop out and exit out. This however doesn't
579 // account for a timeout value passed in. Timeout is always NoTimeout in these cases, but that could change.
580 RunLoop::current().runForDuration(1e10);
581 timedOut = currentTime() >= absoluteTime;
584 timedOut = !SyncMessageState::singleton().wait(absoluteTime);
588 didReceiveSyncReply(syncSendFlags);
593 void Connection::processIncomingSyncReply(std::unique_ptr<MessageDecoder> decoder)
595 DeprecatedMutexLocker locker(m_syncReplyStateMutex);
597 // Go through the stack of sync requests that have pending replies and see which one
598 // this reply is for.
599 for (size_t i = m_pendingSyncReplies.size(); i > 0; --i) {
600 PendingSyncReply& pendingSyncReply = m_pendingSyncReplies[i - 1];
602 if (pendingSyncReply.syncRequestID != decoder->destinationID())
605 ASSERT(!pendingSyncReply.replyDecoder);
607 pendingSyncReply.replyDecoder = WTF::move(decoder);
608 pendingSyncReply.didReceiveReply = true;
610 // We got a reply to the last send message, wake up the client run loop so it can be processed.
611 if (i == m_pendingSyncReplies.size())
612 SyncMessageState::singleton().wakeUpClientRunLoop();
617 // If it's not a reply to any primary thread message, check if it is a reply to a secondary thread one.
618 SecondaryThreadPendingSyncReplyMap::iterator secondaryThreadReplyMapItem = m_secondaryThreadPendingSyncReplyMap.find(decoder->destinationID());
619 if (secondaryThreadReplyMapItem != m_secondaryThreadPendingSyncReplyMap.end()) {
620 SecondaryThreadPendingSyncReply* reply = secondaryThreadReplyMapItem->value;
621 ASSERT(!reply->replyDecoder);
622 reply->replyDecoder = WTF::move(decoder);
623 reply->semaphore.signal();
626 // If we get here, it means we got a reply for a message that wasn't in the sync request stack or map.
627 // This can happen if the send timed out, so it's fine to ignore.
630 void Connection::processIncomingMessage(std::unique_ptr<MessageDecoder> message)
632 ASSERT(!message->messageReceiverName().isEmpty());
633 ASSERT(!message->messageName().isEmpty());
635 if (message->messageReceiverName() == "IPC" && message->messageName() == "SyncMessageReply") {
636 processIncomingSyncReply(WTF::move(message));
640 if (!m_workQueueMessageReceivers.isValidKey(message->messageReceiverName())) {
641 RefPtr<Connection> protectedThis(this);
642 StringReference messageReceiverName = message->messageReceiverName();
643 StringCapture capturedMessageReceiverName(messageReceiverName.isEmpty() ? "<unknown message receiver>" : String(messageReceiverName.data(), messageReceiverName.size()));
644 StringReference messageName = message->messageName();
645 StringCapture capturedMessageName(messageName.isEmpty() ? "<unknown message>" : String(messageName.data(), messageName.size()));
647 RunLoop::main().dispatch([protectedThis, capturedMessageReceiverName, capturedMessageName] {
648 protectedThis->dispatchDidReceiveInvalidMessage(capturedMessageReceiverName.string().utf8(), capturedMessageName.string().utf8());
653 auto it = m_workQueueMessageReceivers.find(message->messageReceiverName());
654 if (it != m_workQueueMessageReceivers.end()) {
655 RefPtr<Connection> protectedThis(this);
656 RefPtr<WorkQueueMessageReceiver>& workQueueMessageReceiver = it->value.second;
657 MessageDecoder* decoderPtr = message.release();
658 it->value.first->dispatch([protectedThis, workQueueMessageReceiver, decoderPtr] {
659 std::unique_ptr<MessageDecoder> decoder(decoderPtr);
660 protectedThis->dispatchWorkQueueMessageReceiverMessage(*workQueueMessageReceiver, *decoder);
665 #if HAVE(QOS_CLASSES)
666 if (message->isSyncMessage() && m_shouldBoostMainThreadOnSyncMessage) {
667 pthread_override_t override = pthread_override_qos_class_start_np(m_mainThread, QOS_CLASS_USER_INTERACTIVE, 0);
668 message->setQOSClassOverride(override);
672 if (message->isSyncMessage()) {
673 std::lock_guard<std::mutex> lock(m_incomingSyncMessageCallbackMutex);
675 for (auto& callback : m_incomingSyncMessageCallbacks.values())
676 m_incomingSyncMessageCallbackQueue->dispatch(callback);
678 m_incomingSyncMessageCallbacks.clear();
681 // Check if this is a sync message or if it's a message that should be dispatched even when waiting for
682 // a sync reply. If it is, and we're waiting for a sync reply this message needs to be dispatched.
683 // If we don't we'll end up with a deadlock where both sync message senders are stuck waiting for a reply.
684 if (SyncMessageState::singleton().processIncomingMessage(*this, message))
687 // Check if we're waiting for this message.
689 std::lock_guard<std::mutex> lock(m_waitForMessageMutex);
691 if (m_waitingForMessage && m_waitingForMessage->messageReceiverName == message->messageReceiverName() && m_waitingForMessage->messageName == message->messageName() && m_waitingForMessage->destinationID == message->destinationID()) {
692 m_waitingForMessage->decoder = WTF::move(message);
693 ASSERT(m_waitingForMessage->decoder);
694 m_waitForMessageCondition.notify_one();
698 if (m_waitingForMessage && (m_waitingForMessage->waitForMessageFlags & InterruptWaitingIfSyncMessageArrives) && message->isSyncMessage()) {
699 m_waitingForMessage->messageWaitingInterrupted = true;
700 m_waitForMessageCondition.notify_one();
704 enqueueIncomingMessage(WTF::move(message));
707 uint64_t Connection::installIncomingSyncMessageCallback(std::function<void ()> callback)
709 std::lock_guard<std::mutex> lock(m_incomingSyncMessageCallbackMutex);
711 m_nextIncomingSyncMessageCallbackID++;
713 if (!m_incomingSyncMessageCallbackQueue)
714 m_incomingSyncMessageCallbackQueue = WorkQueue::create("com.apple.WebKit.IPC.IncomingSyncMessageCallbackQueue");
716 m_incomingSyncMessageCallbacks.add(m_nextIncomingSyncMessageCallbackID, callback);
718 return m_nextIncomingSyncMessageCallbackID;
721 void Connection::uninstallIncomingSyncMessageCallback(uint64_t callbackID)
723 std::lock_guard<std::mutex> lock(m_incomingSyncMessageCallbackMutex);
724 m_incomingSyncMessageCallbacks.remove(callbackID);
727 bool Connection::hasIncomingSyncMessage()
729 std::lock_guard<std::mutex> lock(m_incomingMessagesMutex);
731 for (auto& message : m_incomingMessages) {
732 if (message->isSyncMessage())
739 void Connection::postConnectionDidCloseOnConnectionWorkQueue()
741 RefPtr<Connection> connection(this);
742 m_connectionQueue->dispatch([connection] {
743 connection->connectionDidClose();
747 void Connection::connectionDidClose()
749 // The connection is now invalid.
750 platformInvalidate();
753 DeprecatedMutexLocker locker(m_syncReplyStateMutex);
755 ASSERT(m_shouldWaitForSyncReplies);
756 m_shouldWaitForSyncReplies = false;
758 if (!m_pendingSyncReplies.isEmpty())
759 SyncMessageState::singleton().wakeUpClientRunLoop();
761 for (SecondaryThreadPendingSyncReplyMap::iterator iter = m_secondaryThreadPendingSyncReplyMap.begin(); iter != m_secondaryThreadPendingSyncReplyMap.end(); ++iter)
762 iter->value->semaphore.signal();
766 std::lock_guard<std::mutex> lock(m_waitForMessageMutex);
767 if (m_waitingForMessage)
768 m_waitingForMessage->messageWaitingInterrupted = true;
770 m_waitForMessageCondition.notify_all();
772 if (m_didCloseOnConnectionWorkQueueCallback)
773 m_didCloseOnConnectionWorkQueueCallback(this);
775 RefPtr<Connection> connection(this);
776 RunLoop::main().dispatch([connection] {
777 // If the connection has been explicitly invalidated before dispatchConnectionDidClose was called,
778 // then the client will be null here.
779 if (!connection->m_client)
782 // Because we define a connection as being "valid" based on wheter it has a null client, we null out
783 // the client before calling didClose here. Otherwise, sendSync will try to send a message to the connection and
784 // will then wait indefinitely for a reply.
785 Client* client = connection->m_client;
786 connection->m_client = nullptr;
788 client->didClose(*connection);
792 bool Connection::canSendOutgoingMessages() const
794 return m_isConnected && platformCanSendOutgoingMessages();
797 void Connection::sendOutgoingMessages()
799 if (!canSendOutgoingMessages())
803 std::unique_ptr<MessageEncoder> message;
806 std::lock_guard<std::mutex> lock(m_outgoingMessagesMutex);
807 if (m_outgoingMessages.isEmpty())
809 message = m_outgoingMessages.takeFirst();
812 if (!sendOutgoingMessage(WTF::move(message)))
817 void Connection::dispatchSyncMessage(MessageDecoder& decoder)
819 ASSERT(decoder.isSyncMessage());
821 uint64_t syncRequestID = 0;
822 if (!decoder.decode(syncRequestID) || !syncRequestID) {
823 // We received an invalid sync message.
824 decoder.markInvalid();
829 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID, decoder.UUID());
831 auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID);
834 // Hand off both the decoder and encoder to the client.
835 m_client->didReceiveSyncMessage(*this, decoder, replyEncoder);
837 // FIXME: If the message was invalid, we should send back a SyncMessageError.
838 ASSERT(!decoder.isInvalid());
841 sendSyncReply(WTF::move(replyEncoder));
844 void Connection::dispatchDidReceiveInvalidMessage(const CString& messageReceiverNameString, const CString& messageNameString)
846 ASSERT(RunLoop::isMain());
851 m_client->didReceiveInvalidMessage(*this, StringReference(messageReceiverNameString.data(), messageReceiverNameString.length()), StringReference(messageNameString.data(), messageNameString.length()));
854 void Connection::didFailToSendSyncMessage()
856 if (!m_shouldExitOnSyncMessageSendFailure)
862 void Connection::enqueueIncomingMessage(std::unique_ptr<MessageDecoder> incomingMessage)
865 std::lock_guard<std::mutex> lock(m_incomingMessagesMutex);
866 m_incomingMessages.append(WTF::move(incomingMessage));
869 RefPtr<Connection> protectedThis(this);
870 RunLoop::main().dispatch([protectedThis] {
871 protectedThis->dispatchOneMessage();
875 void Connection::dispatchMessage(MessageDecoder& decoder)
877 m_client->didReceiveMessage(*this, decoder);
880 void Connection::dispatchMessage(std::unique_ptr<MessageDecoder> message)
883 MessageRecorder::recordIncomingMessage(*this, *message);
889 m_inDispatchMessageCount++;
891 if (message->shouldDispatchMessageWhenWaitingForSyncReply())
892 m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount++;
894 bool oldDidReceiveInvalidMessage = m_didReceiveInvalidMessage;
895 m_didReceiveInvalidMessage = false;
897 if (message->isSyncMessage())
898 dispatchSyncMessage(*message);
900 dispatchMessage(*message);
902 m_didReceiveInvalidMessage |= message->isInvalid();
903 m_inDispatchMessageCount--;
905 // FIXME: For Delayed synchronous messages, we should not decrement the counter until we send a response.
906 // Otherwise, we would deadlock if processing the message results in a sync message back after we exit this function.
907 if (message->shouldDispatchMessageWhenWaitingForSyncReply())
908 m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount--;
910 if (m_didReceiveInvalidMessage && m_client)
911 m_client->didReceiveInvalidMessage(*this, message->messageReceiverName(), message->messageName());
913 m_didReceiveInvalidMessage = oldDidReceiveInvalidMessage;
916 void Connection::dispatchOneMessage()
918 std::unique_ptr<MessageDecoder> message;
921 std::lock_guard<std::mutex> lock(m_incomingMessagesMutex);
922 if (m_incomingMessages.isEmpty())
925 message = m_incomingMessages.takeFirst();
928 dispatchMessage(WTF::move(message));
931 void Connection::wakeUpRunLoop()
933 RunLoop::main().wakeUp();