#include "config.h"
#include "Connection.h"
#include "DataReference.h"
#include <wtf/RandomNumber.h>
#include <wtf/text/WTFString.h>
using namespace std;
namespace IPC {
static const size_t inlineMessageMaxSize = 4096;
bool Connection::createServerAndClientIdentifiers(HANDLE& serverIdentifier, HANDLE& clientIdentifier)
{
String pipeName;
do {
unsigned uniqueID = randomNumber() * std::numeric_limits<unsigned>::max();
pipeName = String::format("\\\\.\\pipe\\com.apple.WebKit.%x", uniqueID);
serverIdentifier = ::CreateNamedPipe(pipeName.charactersWithNullTermination().data(),
PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, inlineMessageMaxSize, inlineMessageMaxSize,
0, 0);
} while (!serverIdentifier && ::GetLastError() == ERROR_PIPE_BUSY);
if (!serverIdentifier)
return false;
clientIdentifier = ::CreateFileW(pipeName.charactersWithNullTermination().data(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (!clientIdentifier) {
::CloseHandle(serverIdentifier);
return false;
}
DWORD mode = PIPE_READMODE_MESSAGE;
if (!::SetNamedPipeHandleState(clientIdentifier, &mode, 0, 0)) {
::CloseHandle(serverIdentifier);
::CloseHandle(clientIdentifier);
return false;
}
return true;
}
void Connection::platformInitialize(Identifier identifier)
{
m_connectionPipe = identifier;
}
void Connection::platformInvalidate()
{
if (m_connectionPipe == INVALID_HANDLE_VALUE)
return;
m_isConnected = false;
m_readListener.close();
m_writeListener.close();
::CloseHandle(m_connectionPipe);
m_connectionPipe = INVALID_HANDLE_VALUE;
}
void Connection::readEventHandler()
{
if (m_connectionPipe == INVALID_HANDLE_VALUE)
return;
while (true) {
DWORD numberOfBytesRead = 0;
if (!::GetOverlappedResult(m_connectionPipe, &m_readListener.state(), &numberOfBytesRead, FALSE)) {
DWORD error = ::GetLastError();
switch (error) {
case ERROR_BROKEN_PIPE:
connectionDidClose();
return;
case ERROR_MORE_DATA: {
DWORD bytesToRead = 0;
if (!::PeekNamedPipe(m_connectionPipe, 0, 0, 0, 0, &bytesToRead)) {
DWORD error = ::GetLastError();
if (error == ERROR_BROKEN_PIPE) {
connectionDidClose();
return;
}
ASSERT_NOT_REACHED();
return;
}
ASSERT(bytesToRead);
if (!bytesToRead)
break;
m_readBuffer.grow(m_readBuffer.size() + bytesToRead);
if (!::ReadFile(m_connectionPipe, m_readBuffer.data() + numberOfBytesRead, bytesToRead, 0, &m_readListener.state())) {
DWORD error = ::GetLastError();
ASSERT_NOT_REACHED();
return;
}
continue;
}
case ERROR_IO_INCOMPLETE:
return;
default:
ASSERT_NOT_REACHED();
}
}
if (!m_readBuffer.isEmpty()) {
Vector<Attachment> attachments(0);
auto decoder = std::make_unique<Decoder>(m_readBuffer.data(), m_readBuffer.size(), nullptr, WTFMove(attachments));
processIncomingMessage(WTFMove(decoder));
}
DWORD bytesToRead = 0;
if (!::PeekNamedPipe(m_connectionPipe, 0, 0, 0, 0, &bytesToRead)) {
DWORD error = ::GetLastError();
if (error == ERROR_BROKEN_PIPE) {
connectionDidClose();
return;
}
ASSERT_NOT_REACHED();
}
if (!bytesToRead) {
bytesToRead = 1;
}
m_readBuffer.resize(bytesToRead);
BOOL result = ::ReadFile(m_connectionPipe, m_readBuffer.data(), m_readBuffer.size(), 0, &m_readListener.state());
if (result) {
continue;
}
DWORD error = ::GetLastError();
if (error == ERROR_IO_PENDING) {
return;
}
if (error == ERROR_MORE_DATA) {
continue;
}
if (error == ERROR_BROKEN_PIPE)
return;
ASSERT_NOT_REACHED();
}
}
void Connection::writeEventHandler()
{
if (m_connectionPipe == INVALID_HANDLE_VALUE)
return;
DWORD numberOfBytesWritten = 0;
if (!::GetOverlappedResult(m_connectionPipe, &m_writeListener.state(), &numberOfBytesWritten, FALSE)) {
DWORD error = ::GetLastError();
if (error == ERROR_IO_INCOMPLETE) {
return;
}
if (error == ERROR_BROKEN_PIPE) {
connectionDidClose();
return;
}
ASSERT_NOT_REACHED();
}
m_pendingWriteEncoder = nullptr;
sendOutgoingMessages();
}
void Connection::invokeReadEventHandler()
{
m_connectionQueue->dispatch([this, protectedThis = makeRef(*this)] {
readEventHandler();
});
}
void Connection::invokeWriteEventHandler()
{
m_connectionQueue->dispatch([this, protectedThis = makeRef(*this)] {
writeEventHandler();
});
}
bool Connection::open()
{
m_isConnected = true;
m_readListener.open([this] {
invokeReadEventHandler();
});
m_writeListener.open([this] {
invokeWriteEventHandler();
});
invokeReadEventHandler();
return true;
}
bool Connection::platformCanSendOutgoingMessages() const
{
return !m_pendingWriteEncoder;
}
bool Connection::sendOutgoingMessage(std::unique_ptr<Encoder> encoder)
{
ASSERT(!m_pendingWriteEncoder);
if (m_connectionPipe == INVALID_HANDLE_VALUE)
return false;
*encoder << 0;
if (::WriteFile(m_connectionPipe, encoder->buffer(), encoder->bufferSize(), 0, &m_writeListener.state())) {
return true;
}
DWORD error = ::GetLastError();
if (error == ERROR_NO_DATA) {
connectionDidClose();
return false;
}
if (error != ERROR_IO_PENDING) {
ASSERT_NOT_REACHED();
return false;
}
m_pendingWriteEncoder = WTFMove(encoder);
return false;
}
void Connection::willSendSyncMessage(OptionSet<SendSyncOption>)
{
}
void Connection::didReceiveSyncReply(OptionSet<SendSyncOption>)
{
}
void Connection::EventListener::open(Function<void()>&& handler)
{
m_handler = WTFMove(handler);
memset(&m_state, 0, sizeof(m_state));
m_state.hEvent = ::CreateEventW(0, FALSE, FALSE, 0);
BOOL result;
result = ::RegisterWaitForSingleObject(&m_waitHandle, m_state.hEvent, callback, this, INFINITE, WT_EXECUTEDEFAULT);
ASSERT(result);
}
void Connection::EventListener::callback(void* data, BOOLEAN timerOrWaitFired)
{
ASSERT_ARG(data, data);
ASSERT_ARG(timerOrWaitFired, !timerOrWaitFired);
auto* listener = static_cast<Connection::EventListener*>(data);
listener->m_handler();
}
void Connection::EventListener::close()
{
::UnregisterWaitEx(m_waitHandle, INVALID_HANDLE_VALUE);
m_waitHandle = INVALID_HANDLE_VALUE;
::CloseHandle(m_state.hEvent);
m_state.hEvent = 0;
m_handler = Function<void()>();
}
}