SharedWorkerProcessProxy.cpp [plain text]
#include "config.h"
#include "SharedWorkerProcessProxy.h"
#if ENABLE(SHARED_WORKER_PROCESS)
#include "SharedWorkerProcessCreationParameters.h"
#include "SharedWorkerProcessManager.h"
#include "SharedWorkerProcessMessages.h"
#include "WebContext.h"
#include "WebCoreArgumentCoders.h"
#include "WebProcessMessages.h"
#include "WebProcessProxy.h"
#include <WebCore/NotImplemented.h>
#include <WebCore/RunLoop.h>
#if PLATFORM(MAC)
#include "MachPort.h"
#endif
using namespace WebCore;
namespace WebKit {
static const double minimumLifetime = 30 * 60;
static const double shutdownTimeout = 10 * 60;
PassRefPtr<SharedWorkerProcessProxy> SharedWorkerProcessProxy::create(SharedWorkerProcessManager* sharedWorkerProcessManager, const String& url, const String& name)
{
return adoptRef(new SharedWorkerProcessProxy(sharedWorkerProcessManager, url, name));
}
SharedWorkerProcessProxy::SharedWorkerProcessProxy(SharedWorkerProcessManager* sharedWorkerProcessManager, const String& url, const String& name)
: m_sharedWorkerProcessManager(sharedWorkerProcessManager)
, m_numPendingConnectionRequests(0)
{
ProcessLauncher::LaunchOptions launchOptions;
launchOptions.processType = ProcessLauncher::SharedWorkerProcess;
#if PLATFORM(MAC)
#if HAVE(XPC)
launchOptions.useXPC = false;
#endif
#endif
m_processLauncher = ProcessLauncher::create(this, launchOptions);
}
SharedWorkerProcessProxy::~SharedWorkerProcessProxy()
{
}
void SharedWorkerProcessProxy::getSharedWorkerProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetSharedWorkerProcessConnection::DelayedReply> reply)
{
m_pendingConnectionReplies.append(reply);
if (m_processLauncher->isLaunching()) {
m_numPendingConnectionRequests++;
return;
}
m_connection->send(Messages::SharedWorkerProcess::CreateWebProcessConnection(), 0, CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply);
}
void SharedWorkerProcessProxy::terminate()
{
m_processLauncher->terminateProcess();
}
void SharedWorkerProcessProxy::sharedWorkerProcessCrashedOrFailedToLaunch()
{
while (!m_pendingConnectionReplies.isEmpty()) {
RefPtr<Messages::WebProcessProxy::GetSharedWorkerProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
#if PLATFORM(MAC)
reply->send(CoreIPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
#elif USE(UNIX_DOMAIN_SOCKETS)
reply->send(CoreIPC::Attachment());
#else
notImplemented();
#endif
}
m_sharedWorkerProcessManager->removeSharedWorkerProcessProxy(this);
}
void SharedWorkerProcessProxy::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder)
{
didReceiveSharedWorkerProcessProxyMessage(connection, decoder);
}
void SharedWorkerProcessProxy::didClose(CoreIPC::Connection*)
{
sharedWorkerProcessCrashedOrFailedToLaunch();
}
void SharedWorkerProcessProxy::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::StringReference, CoreIPC::StringReference)
{
}
void SharedWorkerProcessProxy::didFinishLaunching(ProcessLauncher*, CoreIPC::Connection::Identifier connectionIdentifier)
{
ASSERT(!m_connection);
if (CoreIPC::Connection::identifierIsNull(connectionIdentifier)) {
sharedWorkerProcessCrashedOrFailedToLaunch();
return;
}
m_connection = CoreIPC::Connection::createServerConnection(connectionIdentifier, this, RunLoop::main());
#if PLATFORM(MAC)
m_connection->setShouldCloseConnectionOnMachExceptions();
#elif PLATFORM(QT)
m_connection->setShouldCloseConnectionOnProcessTermination(m_processLauncher->processIdentifier());
#endif
m_connection->open();
SharedWorkerProcessCreationParameters parameters;
parameters.minimumLifetime = minimumLifetime;
parameters.terminationTimeout = shutdownTimeout;
platformInitializeSharedWorkerProcess(parameters);
m_connection->send(Messages::SharedWorkerProcess::InitializeSharedWorkerProcess(parameters), 0);
for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
m_connection->send(Messages::SharedWorkerProcess::CreateWebProcessConnection(), 0);
m_numPendingConnectionRequests = 0;
#if PLATFORM(MAC)
if (WebContext::canEnableProcessSuppressionForGlobalChildProcesses())
setProcessSuppressionEnabled(true);
#endif
}
void SharedWorkerProcessProxy::didCreateWebProcessConnection(const CoreIPC::Attachment& connectionIdentifier)
{
ASSERT(!m_pendingConnectionReplies.isEmpty());
RefPtr<Messages::WebProcessProxy::GetSharedWorkerProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
#if PLATFORM(MAC)
reply->send(CoreIPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
#elif USE(UNIX_DOMAIN_SOCKETS)
reply->send(connectionIdentifier);
#else
notImplemented();
#endif
}
}
#endif // ENABLE(SHARED_WORKER_PROCESS)