ChildProcessProxy.cpp [plain text]
#include "config.h"
#include "ChildProcessProxy.h"
#include "ChildProcessMessages.h"
#include <wtf/RunLoop.h>
namespace WebKit {
ChildProcessProxy::ChildProcessProxy(bool alwaysRunsAtBackgroundPriority)
: m_alwaysRunsAtBackgroundPriority(alwaysRunsAtBackgroundPriority)
{
}
ChildProcessProxy::~ChildProcessProxy()
{
if (m_connection)
m_connection->invalidate();
if (m_processLauncher) {
m_processLauncher->invalidate();
m_processLauncher = nullptr;
}
}
void ChildProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
{
launchOptions.processIdentifier = m_processIdentifier;
if (const char* userDirectorySuffix = getenv("DIRHELPER_USER_DIR_SUFFIX"))
launchOptions.extraInitializationData.add("user-directory-suffix"_s, userDirectorySuffix);
if (m_alwaysRunsAtBackgroundPriority)
launchOptions.extraInitializationData.add("always-runs-at-background-priority"_s, "true");
#if ENABLE(DEVELOPER_MODE) && (PLATFORM(GTK) || PLATFORM(WPE))
const char* varname;
switch (launchOptions.processType) {
case ProcessLauncher::ProcessType::Web:
varname = "WEB_PROCESS_CMD_PREFIX";
break;
#if ENABLE(NETSCAPE_PLUGIN_API)
case ProcessLauncher::ProcessType::Plugin64:
case ProcessLauncher::ProcessType::Plugin32:
varname = "PLUGIN_PROCESS_CMD_PREFIX";
break;
#endif
case ProcessLauncher::ProcessType::Network:
varname = "NETWORK_PROCESS_CMD_PREFIX";
break;
}
const char* processCmdPrefix = getenv(varname);
if (processCmdPrefix && *processCmdPrefix)
launchOptions.processCmdPrefix = String::fromUTF8(processCmdPrefix);
#endif // ENABLE(DEVELOPER_MODE) && (PLATFORM(GTK) || PLATFORM(WPE))
platformGetLaunchOptions(launchOptions);
}
void ChildProcessProxy::connect()
{
ASSERT(!m_processLauncher);
ProcessLauncher::LaunchOptions launchOptions;
getLaunchOptions(launchOptions);
m_processLauncher = ProcessLauncher::create(this, launchOptions);
}
void ChildProcessProxy::terminate()
{
#if PLATFORM(COCOA)
if (m_connection && m_connection->kill())
return;
#endif
if (m_processLauncher)
m_processLauncher->terminateProcess();
}
ChildProcessProxy::State ChildProcessProxy::state() const
{
if (m_processLauncher && m_processLauncher->isLaunching())
return ChildProcessProxy::State::Launching;
if (!m_connection)
return ChildProcessProxy::State::Terminated;
return ChildProcessProxy::State::Running;
}
bool ChildProcessProxy::sendMessage(std::unique_ptr<IPC::Encoder> encoder, OptionSet<IPC::SendOption> sendOptions)
{
switch (state()) {
case State::Launching:
m_pendingMessages.append(std::make_pair(WTFMove(encoder), sendOptions));
return true;
case State::Running:
return connection()->sendMessage(WTFMove(encoder), sendOptions);
case State::Terminated:
return false;
}
return false;
}
void ChildProcessProxy::addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver& messageReceiver)
{
m_messageReceiverMap.addMessageReceiver(messageReceiverName, messageReceiver);
}
void ChildProcessProxy::addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver& messageReceiver)
{
m_messageReceiverMap.addMessageReceiver(messageReceiverName, destinationID, messageReceiver);
}
void ChildProcessProxy::removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID)
{
m_messageReceiverMap.removeMessageReceiver(messageReceiverName, destinationID);
}
void ChildProcessProxy::removeMessageReceiver(IPC::StringReference messageReceiverName)
{
m_messageReceiverMap.removeMessageReceiver(messageReceiverName);
}
bool ChildProcessProxy::dispatchMessage(IPC::Connection& connection, IPC::Decoder& decoder)
{
return m_messageReceiverMap.dispatchMessage(connection, decoder);
}
bool ChildProcessProxy::dispatchSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
{
return m_messageReceiverMap.dispatchSyncMessage(connection, decoder, replyEncoder);
}
void ChildProcessProxy::didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier connectionIdentifier)
{
ASSERT(!m_connection);
if (!IPC::Connection::identifierIsValid(connectionIdentifier))
return;
m_connection = IPC::Connection::createServerConnection(connectionIdentifier, *this);
connectionWillOpen(*m_connection);
m_connection->open();
for (size_t i = 0; i < m_pendingMessages.size(); ++i) {
std::unique_ptr<IPC::Encoder> message = WTFMove(m_pendingMessages[i].first);
OptionSet<IPC::SendOption> sendOptions = m_pendingMessages[i].second;
m_connection->sendMessage(WTFMove(message), sendOptions);
}
m_pendingMessages.clear();
}
void ChildProcessProxy::shutDownProcess()
{
switch (state()) {
case State::Launching:
m_processLauncher->invalidate();
m_processLauncher = nullptr;
break;
case State::Running:
#if PLATFORM(IOS_FAMILY)
ASSERT(m_connection);
m_connection->terminateSoon(30_s);
#endif
break;
case State::Terminated:
return;
}
if (!m_connection)
return;
processWillShutDown(*m_connection);
if (canSendMessage())
send(Messages::ChildProcess::ShutDown(), 0);
m_connection->invalidate();
m_connection = nullptr;
}
void ChildProcessProxy::setProcessSuppressionEnabled(bool processSuppressionEnabled)
{
#if PLATFORM(COCOA)
if (state() != State::Running)
return;
connection()->send(Messages::ChildProcess::SetProcessSuppressionEnabled(processSuppressionEnabled), 0);
#else
UNUSED_PARAM(processSuppressionEnabled);
#endif
}
void ChildProcessProxy::connectionWillOpen(IPC::Connection&)
{
}
}