#pragma once
#include "Connection.h"
#include <WebCore/ProcessIdentifier.h>
#include <wtf/HashMap.h>
#include <wtf/ProcessID.h>
#include <wtf/RefPtr.h>
#include <wtf/Threading.h>
#include <wtf/WeakPtr.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/WTFString.h>
#if PLATFORM(WIN)
#include <wtf/win/Win32Handle.h>
#endif
#if PLATFORM(COCOA)
#include "XPCEventHandler.h"
#endif
namespace WebKit {
#if PLATFORM(GTK) || PLATFORM(WPE)
enum class SandboxPermission {
ReadOnly,
ReadWrite,
};
#endif
class ProcessLauncher : public ThreadSafeRefCounted<ProcessLauncher>, public CanMakeWeakPtr<ProcessLauncher> {
public:
class Client {
public:
virtual ~Client() { }
virtual void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) = 0;
virtual bool shouldConfigureJSCForTesting() const { return false; }
virtual bool isJITEnabled() const { return true; }
#if PLATFORM(COCOA)
virtual RefPtr<XPCEventHandler> xpcEventHandler() const { return nullptr; }
#endif
};
enum class ProcessType {
Web,
#if ENABLE(NETSCAPE_PLUGIN_API)
Plugin,
#endif
Network,
#if ENABLE(GPU_PROCESS)
GPU,
#endif
#if ENABLE(WEB_AUTHN)
WebAuthn
#endif
};
struct LaunchOptions {
ProcessType processType;
WebCore::ProcessIdentifier processIdentifier;
HashMap<String, String> extraInitializationData;
bool nonValidInjectedCodeAllowed { false };
bool shouldMakeProcessLaunchFailForTesting { false };
CString customWebContentServiceBundleIdentifier;
#if PLATFORM(GTK) || PLATFORM(WPE)
HashMap<CString, SandboxPermission> extraWebProcessSandboxPaths;
#if ENABLE(DEVELOPER_MODE)
String processCmdPrefix;
#endif
#endif
#if PLATFORM(PLAYSTATION)
String processPath;
int32_t userId { -1 };
#endif
};
static Ref<ProcessLauncher> create(Client* client, LaunchOptions&& launchOptions)
{
return adoptRef(*new ProcessLauncher(client, WTFMove(launchOptions)));
}
bool isLaunching() const { return m_isLaunching; }
ProcessID processIdentifier() const { return m_processIdentifier; }
void terminateProcess();
void invalidate();
private:
ProcessLauncher(Client*, LaunchOptions&&);
void launchProcess();
void didFinishLaunchingProcess(ProcessID, IPC::Connection::Identifier);
void platformInvalidate();
Client* m_client;
#if PLATFORM(COCOA)
OSObjectPtr<xpc_connection_t> m_xpcConnection;
#endif
#if PLATFORM(WIN)
WTF::Win32Handle m_hProcess;
#endif
const LaunchOptions m_launchOptions;
bool m_isLaunching { true };
ProcessID m_processIdentifier { 0 };
};
}