#ifndef Connection_h
#define Connection_h
#include "Arguments.h"
#include "MessageDecoder.h"
#include "MessageEncoder.h"
#include "MessageReceiver.h"
#include "WorkQueue.h"
#include <wtf/Deque.h>
#include <wtf/Forward.h>
#include <wtf/PassRefPtr.h>
#include <wtf/OwnPtr.h>
#include <wtf/Threading.h>
#include <wtf/text/CString.h>
#if OS(DARWIN)
#include <mach/mach_port.h>
#if HAVE(XPC)
#include <xpc/xpc.h>
#endif
#elif PLATFORM(QT)
QT_BEGIN_NAMESPACE
class QSocketNotifier;
QT_END_NAMESPACE
#endif
#if PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
#include "PlatformProcessIdentifier.h"
#endif
namespace WebCore {
class RunLoop;
}
namespace CoreIPC {
enum MessageSendFlags {
DispatchMessageEvenWhenWaitingForSyncReply = 1 << 0,
};
enum SyncMessageSendFlags {
SpinRunLoopWhileWaitingForReply = 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;
protected:
virtual ~Client() { }
};
class WorkQueueMessageReceiver : public MessageReceiver, public ThreadSafeRefCounted<WorkQueueMessageReceiver> {
};
#if OS(DARWIN)
struct Identifier {
Identifier()
: port(MACH_PORT_NULL)
#if HAVE(XPC)
, xpcConnection(0)
#endif
{
}
Identifier(mach_port_t port)
: port(port)
#if HAVE(XPC)
, xpcConnection(0)
#endif
{
}
#if HAVE(XPC)
Identifier(mach_port_t port, xpc_connection_t xpcConnection)
: port(port)
, xpcConnection(xpcConnection)
{
}
#endif
mach_port_t port;
#if HAVE(XPC)
xpc_connection_t xpcConnection;
#endif
};
static bool identifierIsNull(Identifier identifier) { return identifier.port == MACH_PORT_NULL; }
#elif OS(WINDOWS)
typedef HANDLE Identifier;
static bool createServerAndClientIdentifiers(Identifier& serverIdentifier, Identifier& clientIdentifier);
static bool identifierIsNull(Identifier identifier) { return !identifier; }
#elif USE(UNIX_DOMAIN_SOCKETS)
typedef int Identifier;
static bool identifierIsNull(Identifier identifier) { return !identifier; }
#endif
static PassRefPtr<Connection> createServerConnection(Identifier, Client*, WebCore::RunLoop* clientRunLoop);
static PassRefPtr<Connection> createClientConnection(Identifier, Client*, WebCore::RunLoop* clientRunLoop);
~Connection();
Client* client() const { return m_client; }
#if OS(DARWIN)
void setShouldCloseConnectionOnMachExceptions();
#elif PLATFORM(QT)
void setShouldCloseConnectionOnProcessTermination(WebKit::PlatformProcessIdentifier);
#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();
static const int NoTimeout = -1;
template<typename T> bool send(const T& message, uint64_t destinationID, unsigned messageSendFlags = 0);
template<typename T> bool sendSync(const T& message, const typename T::Reply& reply, uint64_t destinationID, double timeout = NoTimeout, unsigned syncSendFlags = 0);
template<typename T> bool waitForAndDispatchImmediately(uint64_t destinationID, double timeout);
PassOwnPtr<MessageEncoder> createSyncMessageEncoder(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, uint64_t& syncRequestID);
bool sendMessage(PassOwnPtr<MessageEncoder>, unsigned messageSendFlags = 0);
PassOwnPtr<MessageDecoder> sendSyncMessage(uint64_t syncRequestID, PassOwnPtr<MessageEncoder>, double timeout, unsigned syncSendFlags = 0);
PassOwnPtr<MessageDecoder> sendSyncMessageFromSecondaryThread(uint64_t syncRequestID, PassOwnPtr<MessageEncoder>, double timeout);
bool sendSyncReply(PassOwnPtr<MessageEncoder>);
void wakeUpRunLoop();
void incrementDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount() { ++m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount; }
void decrementDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount() { --m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount; }
bool inSendSync() const { return m_inSendSyncCount; }
private:
Connection(Identifier, bool isServer, Client*, WebCore::RunLoop* clientRunLoop);
void platformInitialize(Identifier);
void platformInvalidate();
bool isValid() const { return m_client; }
PassOwnPtr<MessageDecoder> waitForMessage(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID, double timeout);
PassOwnPtr<MessageDecoder> waitForSyncReply(uint64_t syncRequestID, double timeout, unsigned syncSendFlags);
void processIncomingMessage(PassOwnPtr<MessageDecoder>);
void processIncomingSyncReply(PassOwnPtr<MessageDecoder>);
void addWorkQueueMessageReceiverOnConnectionWorkQueue(StringReference messageReceiverName, WorkQueue*, WorkQueueMessageReceiver*);
void removeWorkQueueMessageReceiverOnConnectionWorkQueue(StringReference messageReceiverName);
void dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver*, MessageDecoder*);
bool canSendOutgoingMessages() const;
bool platformCanSendOutgoingMessages() const;
void sendOutgoingMessages();
bool sendOutgoingMessage(PassOwnPtr<MessageEncoder>);
void connectionDidClose();
void dispatchConnectionDidClose();
void dispatchOneMessage();
void dispatchMessage(PassOwnPtr<MessageDecoder>);
void dispatchMessage(MessageDecoder&);
void dispatchSyncMessage(MessageDecoder&);
void dispatchDidReceiveInvalidMessage(const CString& messageReceiverNameString, const CString& messageNameString);
void didFailToSendSyncMessage();
void enqueueIncomingMessage(PassOwnPtr<MessageDecoder>);
Client* m_client;
bool m_isServer;
uint64_t m_syncRequestID;
bool m_onlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage;
bool m_shouldExitOnSyncMessageSendFailure;
DidCloseOnConnectionWorkQueueCallback m_didCloseOnConnectionWorkQueueCallback;
bool m_isConnected;
RefPtr<WorkQueue> m_connectionQueue;
WebCore::RunLoop* m_clientRunLoop;
HashMap<StringReference, std::pair<RefPtr<WorkQueue>, RefPtr<WorkQueueMessageReceiver>>> m_workQueueMessageReceivers;
unsigned m_inSendSyncCount;
unsigned m_inDispatchMessageCount;
unsigned m_inDispatchMessageMarkedDispatchWhenWaitingForSyncReplyCount;
bool m_didReceiveInvalidMessage;
Mutex m_incomingMessagesLock;
Deque<OwnPtr<MessageDecoder>> m_incomingMessages;
Mutex m_outgoingMessagesLock;
Deque<OwnPtr<MessageEncoder>> m_outgoingMessages;
ThreadCondition m_waitForMessageCondition;
Mutex m_waitForMessageMutex;
HashMap<std::pair<std::pair<StringReference, StringReference>, uint64_t>, OwnPtr<MessageDecoder>> m_waitForMessageMap;
struct PendingSyncReply {
uint64_t syncRequestID;
MessageDecoder* replyDecoder;
bool didReceiveReply;
PendingSyncReply()
: syncRequestID(0)
, replyDecoder(0)
, didReceiveReply(false)
{
}
explicit PendingSyncReply(uint64_t syncRequestID)
: syncRequestID(syncRequestID)
, replyDecoder(0)
, didReceiveReply(0)
{
}
PassOwnPtr<MessageDecoder> releaseReplyDecoder()
{
OwnPtr<MessageDecoder> reply = adoptPtr(replyDecoder);
replyDecoder = 0;
return reply.release();
}
};
class SyncMessageState;
friend class SyncMessageState;
RefPtr<SyncMessageState> m_syncMessageState;
Mutex m_syncReplyStateMutex;
bool m_shouldWaitForSyncReplies;
Vector<PendingSyncReply> m_pendingSyncReplies;
class SecondaryThreadPendingSyncReply;
typedef HashMap<uint64_t, SecondaryThreadPendingSyncReply*> SecondaryThreadPendingSyncReplyMap;
SecondaryThreadPendingSyncReplyMap m_secondaryThreadPendingSyncReplyMap;
#if OS(DARWIN)
void receiveSourceEventHandler();
void initializeDeadNameSource();
void exceptionSourceEventHandler();
mach_port_t m_sendPort;
dispatch_source_t m_deadNameSource;
mach_port_t m_receivePort;
dispatch_source_t m_receivePortDataAvailableSource;
mach_port_t m_exceptionPort;
dispatch_source_t m_exceptionPortDataAvailableSource;
#if HAVE(XPC)
xpc_connection_t m_xpcConnection;
#endif
#elif OS(WINDOWS)
void readEventHandler();
void writeEventHandler();
static bool dispatchSentMessagesUntil(const Vector<HWND>& windows, WTF::BinarySemaphore& semaphore, double absoluteTime);
Vector<uint8_t> m_readBuffer;
OVERLAPPED m_readState;
OwnPtr<MessageEncoder> m_pendingWriteEncoder;
OVERLAPPED m_writeState;
HANDLE m_connectionPipe;
#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;
#if PLATFORM(QT)
QSocketNotifier* m_socketNotifier;
#endif
#endif
};
template<typename T> bool Connection::send(const T& message, uint64_t destinationID, unsigned messageSendFlags)
{
COMPILE_ASSERT(!T::isSync, AsyncMessageExpected);
OwnPtr<MessageEncoder> encoder = MessageEncoder::create(T::receiverName(), T::name(), destinationID);
encoder->encode(message);
return sendMessage(encoder.release(), messageSendFlags);
}
template<typename T> bool Connection::sendSync(const T& message, const typename T::Reply& reply, uint64_t destinationID, double timeout, unsigned syncSendFlags)
{
COMPILE_ASSERT(T::isSync, SyncMessageExpected);
uint64_t syncRequestID = 0;
OwnPtr<MessageEncoder> encoder = createSyncMessageEncoder(T::receiverName(), T::name(), destinationID, syncRequestID);
encoder->encode(message);
OwnPtr<MessageDecoder> replyDecoder = sendSyncMessage(syncRequestID, encoder.release(), timeout, syncSendFlags);
if (!replyDecoder)
return false;
return replyDecoder->decode(const_cast<typename T::Reply&>(reply));
}
template<typename T> bool Connection::waitForAndDispatchImmediately(uint64_t destinationID, double timeout)
{
OwnPtr<MessageDecoder> decoder = waitForMessage(T::receiverName(), T::name(), destinationID, timeout);
if (!decoder)
return false;
ASSERT(decoder->destinationID() == destinationID);
m_client->didReceiveMessage(this, *decoder);
return true;
}
}
#endif // Connection_h