#ifndef Connection_h
#define Connection_h
#include "Arguments.h"
#include "MessageDecoder.h"
#include "MessageEncoder.h"
#include "MessageReceiver.h"
#include "ProcessType.h"
#include <atomic>
#include <condition_variable>
#include <wtf/Deque.h>
#include <wtf/Forward.h>
#include <wtf/HashMap.h>
#include <wtf/WorkQueue.h>
#include <wtf/text/CString.h>
#if OS(DARWIN)
#include <mach/mach_port.h>
#include <wtf/OSObjectPtr.h>
#include <wtf/spi/darwin/XPCSPI.h>
#endif
#if PLATFORM(GTK) || PLATFORM(EFL)
#include "PlatformProcessIdentifier.h"
#endif
namespace IPC {
struct WaitForMessageState;
enum MessageSendFlags {
DispatchMessageEvenWhenWaitingForSyncReply = 1 << 0,
};
enum SyncMessageSendFlags {
InformPlatformProcessWillSuspend = 1 << 0,
SpinRunLoopWhileWaitingForReply = 1 << 1,
};
enum WaitForMessageFlags {
InterruptWaitingIfSyncMessageArrives = 1 << 0,
};
#define MESSAGE_CHECK_BASE(assertion, connection) do \
if (!(assertion)) { \
ASSERT(assertion); \
(connection)->markCurrentlyDispatchedMessageAsInvalid(); \
return; \
} \
while (0)
class Connection : public ThreadSafeRefCounted<Connection> {
public:
class Client : public MessageReceiver {
public:
virtual void didClose(Connection&) = 0;
virtual void didReceiveInvalidMessage(Connection&, StringReference messageReceiverName, StringReference messageName) = 0;
virtual IPC::ProcessType localProcessType() = 0;
virtual IPC::ProcessType remoteProcessType() = 0;
protected:
virtual ~Client() { }
};
class WorkQueueMessageReceiver : public MessageReceiver, public ThreadSafeRefCounted<WorkQueueMessageReceiver> {
};
#if OS(DARWIN)
struct Identifier {
Identifier()
: port(MACH_PORT_NULL)
{
}
Identifier(mach_port_t port)
: port(port)
{
}
Identifier(mach_port_t port, OSObjectPtr<xpc_connection_t> xpcConnection)
: port(port)
, xpcConnection(WTF::move(xpcConnection))
{
}
mach_port_t port;
OSObjectPtr<xpc_connection_t> xpcConnection;
};
static bool identifierIsNull(Identifier identifier) { return identifier.port == MACH_PORT_NULL; }
xpc_connection_t xpcConnection() const { return m_xpcConnection.get(); }
bool getAuditToken(audit_token_t&);
pid_t remoteProcessID() const;
#elif USE(UNIX_DOMAIN_SOCKETS)
typedef int Identifier;
static bool identifierIsNull(Identifier identifier) { return identifier == -1; }
struct SocketPair {
int client;
int server;
};
enum ConnectionOptions {
SetCloexecOnClient = 1 << 0,
SetCloexecOnServer = 1 << 1,
};
static Connection::SocketPair createPlatformConnection(unsigned options = SetCloexecOnClient | SetCloexecOnServer);
#endif
static Ref<Connection> createServerConnection(Identifier, Client&);
static Ref<Connection> createClientConnection(Identifier, Client&);
~Connection();
Client* client() const { return m_client; }
#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
void setShouldCloseConnectionOnMachExceptions();
#endif
void setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(bool);
void setShouldExitOnSyncMessageSendFailure(bool shouldExitOnSyncMessageSendFailure);
typedef void (*DidCloseOnConnectionWorkQueueCallback)(Connection*);
void setDidCloseOnConnectionWorkQueueCallback(DidCloseOnConnectionWorkQueueCallback callback);
void addWorkQueueMessageReceiver(StringReference messageReceiverName, WorkQueue*, WorkQueueMessageReceiver*);
void removeWorkQueueMessageReceiver(StringReference messageReceiverName);
bool open();
void invalidate();
void markCurrentlyDispatchedMessageAsInvalid();
void postConnectionDidCloseOnConnectionWorkQueue();
template<typename T> bool send(T&& message, uint64_t destinationID, unsigned messageSendFlags = 0);
template<typename T> bool sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), unsigned syncSendFlags = 0);
template<typename T> bool waitForAndDispatchImmediately(uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags = 0);
std::unique_ptr<MessageEncoder> createSyncMessageEncoder(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, uint64_t& syncRequestID);
bool sendMessage(std::unique_ptr<MessageEncoder>, unsigned messageSendFlags = 0, bool alreadyRecordedMessage = false);
std::unique_ptr<MessageDecoder> sendSyncMessage(uint64_t syncRequestID, std::unique_ptr<MessageEncoder>, std::chrono::milliseconds timeout, unsigned syncSendFlags = 0);
std::unique_ptr<MessageDecoder> sendSyncMessageFromSecondaryThread(uint64_t syncRequestID, std::unique_ptr<MessageEncoder>, std::chrono::milliseconds timeout);
bool sendSyncReply(std::unique_ptr<MessageEncoder>);
void wakeUpRunLoop();
void incrementDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount() { ++m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount; }
void decrementDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount() { --m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount; }
bool inSendSync() const { return m_inSendSyncCount; }
Identifier identifier() const;
#if PLATFORM(COCOA)
bool kill();
void terminateSoon(double intervalInSeconds);
#endif
bool isValid() const { return m_client; }
#if HAVE(QOS_CLASSES)
void setShouldBoostMainThreadOnSyncMessage(bool b) { m_shouldBoostMainThreadOnSyncMessage = b; }
#endif
uint64_t installIncomingSyncMessageCallback(std::function<void ()>);
void uninstallIncomingSyncMessageCallback(uint64_t);
bool hasIncomingSyncMessage();
private:
Connection(Identifier, bool isServer, Client&);
void platformInitialize(Identifier);
void platformInvalidate();
std::unique_ptr<MessageDecoder> waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags);
std::unique_ptr<MessageDecoder> waitForSyncReply(uint64_t syncRequestID, std::chrono::milliseconds timeout, unsigned syncSendFlags);
void processIncomingMessage(std::unique_ptr<MessageDecoder>);
void processIncomingSyncReply(std::unique_ptr<MessageDecoder>);
void dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver&, MessageDecoder&);
bool canSendOutgoingMessages() const;
bool platformCanSendOutgoingMessages() const;
void sendOutgoingMessages();
bool sendOutgoingMessage(std::unique_ptr<MessageEncoder>);
void connectionDidClose();
void dispatchOneMessage();
void dispatchMessage(std::unique_ptr<MessageDecoder>);
void dispatchMessage(MessageDecoder&);
void dispatchSyncMessage(MessageDecoder&);
void dispatchDidReceiveInvalidMessage(const CString& messageReceiverNameString, const CString& messageNameString);
void didFailToSendSyncMessage();
void enqueueIncomingMessage(std::unique_ptr<MessageDecoder>);
void willSendSyncMessage(unsigned syncSendFlags);
void didReceiveSyncReply(unsigned syncSendFlags);
Client* m_client;
bool m_isServer;
std::atomic<uint64_t> m_syncRequestID;
bool m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage;
bool m_shouldExitOnSyncMessageSendFailure;
DidCloseOnConnectionWorkQueueCallback m_didCloseOnConnectionWorkQueueCallback;
bool m_isConnected;
Ref<WorkQueue> m_connectionQueue;
HashMap<StringReference, std::pair<RefPtr<WorkQueue>, RefPtr<WorkQueueMessageReceiver>>> m_workQueueMessageReceivers;
unsigned m_inSendSyncCount;
unsigned m_inDispatchMessageCount;
unsigned m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount;
bool m_didReceiveInvalidMessage;
std::mutex m_incomingMessagesMutex;
Deque<std::unique_ptr<MessageDecoder>> m_incomingMessages;
std::mutex m_outgoingMessagesMutex;
Deque<std::unique_ptr<MessageEncoder>> m_outgoingMessages;
std::condition_variable m_waitForMessageCondition;
std::mutex m_waitForMessageMutex;
WaitForMessageState* m_waitingForMessage;
struct PendingSyncReply {
uint64_t syncRequestID;
std::unique_ptr<MessageDecoder> replyDecoder;
bool didReceiveReply;
PendingSyncReply()
: syncRequestID(0)
, didReceiveReply(false)
{
}
explicit PendingSyncReply(uint64_t syncRequestID)
: syncRequestID(syncRequestID)
, didReceiveReply(0)
{
}
};
class SyncMessageState;
friend class SyncMessageState;
Mutex m_syncReplyStateMutex;
bool m_shouldWaitForSyncReplies;
Vector<PendingSyncReply> m_pendingSyncReplies;
class SecondaryThreadPendingSyncReply;
typedef HashMap<uint64_t, SecondaryThreadPendingSyncReply*> SecondaryThreadPendingSyncReplyMap;
SecondaryThreadPendingSyncReplyMap m_secondaryThreadPendingSyncReplyMap;
std::mutex m_incomingSyncMessageCallbackMutex;
HashMap<uint64_t, std::function<void ()>> m_incomingSyncMessageCallbacks;
RefPtr<WorkQueue> m_incomingSyncMessageCallbackQueue;
uint64_t m_nextIncomingSyncMessageCallbackID { 0 };
#if HAVE(QOS_CLASSES)
pthread_t m_mainThread { 0 };
bool m_shouldBoostMainThreadOnSyncMessage { false };
#endif
#if OS(DARWIN)
void receiveSourceEventHandler();
void initializeDeadNameSource();
mach_port_t m_sendPort;
dispatch_source_t m_deadNameSource;
mach_port_t m_receivePort;
dispatch_source_t m_receivePortDataAvailableSource;
#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 101000
void exceptionSourceEventHandler();
mach_port_t m_exceptionPort;
dispatch_source_t m_exceptionPortDataAvailableSource;
#endif
OSObjectPtr<xpc_connection_t> m_xpcConnection;
#elif USE(UNIX_DOMAIN_SOCKETS)
void readyReadHandler();
bool processMessage();
Vector<uint8_t> m_readBuffer;
size_t m_readBufferSize;
Vector<int> m_fileDescriptors;
size_t m_fileDescriptorsSize;
int m_socketDescriptor;
#endif
};
template<typename T> bool Connection::send(T&& message, uint64_t destinationID, unsigned messageSendFlags)
{
COMPILE_ASSERT(!T::isSync, AsyncMessageExpected);
auto encoder = std::make_unique<MessageEncoder>(T::receiverName(), T::name(), destinationID);
encoder->encode(message.arguments());
return sendMessage(WTF::move(encoder), messageSendFlags);
}
template<typename T> bool Connection::sendSync(T&& message, typename T::Reply&& reply, uint64_t destinationID, std::chrono::milliseconds timeout, unsigned syncSendFlags)
{
COMPILE_ASSERT(T::isSync, SyncMessageExpected);
uint64_t syncRequestID = 0;
std::unique_ptr<MessageEncoder> encoder = createSyncMessageEncoder(T::receiverName(), T::name(), destinationID, syncRequestID);
encoder->encode(message.arguments());
std::unique_ptr<MessageDecoder> replyDecoder = sendSyncMessage(syncRequestID, WTF::move(encoder), timeout, syncSendFlags);
if (!replyDecoder)
return false;
return replyDecoder->decode(reply);
}
template<typename T> bool Connection::waitForAndDispatchImmediately(uint64_t destinationID, std::chrono::milliseconds timeout, unsigned waitForMessageFlags)
{
std::unique_ptr<MessageDecoder> decoder = waitForMessage(T::receiverName(), T::name(), destinationID, timeout, waitForMessageFlags);
if (!decoder)
return false;
ASSERT(decoder->destinationID() == destinationID);
m_client->didReceiveMessage(*this, *decoder);
return true;
}
}
#endif // Connection_h