#include "config.h"
#include "CommandLine.h"
#include "PluginProcessMain.h"
#include "ProcessLauncher.h"
#include "WebProcessMain.h"
#include <wtf/text/CString.h>
#if PLATFORM(MAC)
#include <objc/objc-auto.h>
#endif
using namespace WebKit;
static int WebKitMain(const CommandLine& commandLine)
{
ProcessLauncher::ProcessType processType;
if (!ProcessLauncher::getProcessTypeFromString(commandLine["type"].utf8().data(), processType))
return EXIT_FAILURE;
switch (processType) {
case ProcessLauncher::WebProcess:
return WebProcessMain(commandLine);
case ProcessLauncher::PluginProcess:
#if ENABLE(PLUGIN_PROCESS)
return PluginProcessMain(commandLine);
#else
break;
#endif
}
return EXIT_FAILURE;
}
#if PLATFORM(MAC)
extern "C" WK_EXPORT int WebKitMain(int argc, char** argv);
int WebKitMain(int argc, char** argv)
{
ASSERT(!objc_collectingEnabled());
CommandLine commandLine;
if (!commandLine.parse(argc, argv))
return EXIT_FAILURE;
return WebKitMain(commandLine);
}
#elif PLATFORM(WIN)
#ifndef DEBUG_ALL
#define PROCESS_NAME L"WebKit2WebKitProcess.exe"
#else
#define PROCESS_NAME L"WebKit2WebProcess_debug.exe"
#endif
static void enableDataExecutionPrevention()
{
const DWORD enableDEP = 0x00000001;
HMODULE hMod = ::GetModuleHandleW(L"Kernel32.dll");
if (!hMod)
return;
typedef BOOL (WINAPI *PSETDEP)(DWORD);
PSETDEP procSet = reinterpret_cast<PSETDEP>(::GetProcAddress(hMod, "SetProcessDEPPolicy"));
if (!procSet)
return;
procSet(enableDEP);
}
static void enableTerminationOnHeapCorruption()
{
const HEAP_INFORMATION_CLASS heapEnableTerminationOnCorruption = static_cast<HEAP_INFORMATION_CLASS>(1);
HMODULE hMod = ::GetModuleHandleW(L"kernel32.dll");
if (!hMod)
return;
typedef BOOL (WINAPI*HSI)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T);
HSI heapSetInformation = reinterpret_cast<HSI>(::GetProcAddress(hMod, "HeapSetInformation"));
if (!heapSetInformation)
return;
heapSetInformation(0, heapEnableTerminationOnCorruption, 0, 0);
}
static void disableUserModeCallbackExceptionFilter()
{
const DWORD PROCESS_CALLBACK_FILTER_ENABLED = 0x1;
typedef BOOL (NTAPI *getProcessUserModeExceptionPolicyPtr)(LPDWORD lpFlags);
typedef BOOL (NTAPI *setProcessUserModeExceptionPolicyPtr)(DWORD dwFlags);
HMODULE lib = LoadLibrary(TEXT("kernel32.dll"));
ASSERT(lib);
getProcessUserModeExceptionPolicyPtr getPolicyPtr = (getProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "GetProcessUserModeExceptionPolicy");
setProcessUserModeExceptionPolicyPtr setPolicyPtr = (setProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "SetProcessUserModeExceptionPolicy");
DWORD dwFlags;
if (!getPolicyPtr || !setPolicyPtr || !getPolicyPtr(&dwFlags)) {
FreeLibrary(lib);
return;
}
setPolicyPtr(dwFlags & ~PROCESS_CALLBACK_FILTER_ENABLED);
FreeLibrary(lib);
}
extern "C" __declspec(dllexport)
int WebKitMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow)
{
#ifndef NDEBUG
const unsigned short highBitMaskShort = 0x8000;
if (getenv("WEBKIT2_PAUSE_WEB_PROCESS_ON_LAUNCH") || (::GetKeyState(VK_CONTROL) & highBitMaskShort) && (::GetKeyState(VK_MENU) & highBitMaskShort) && (::GetKeyState(VK_SHIFT) & highBitMaskShort))
::MessageBoxW(0, L"You can now attach a debugger to " PROCESS_NAME L". You can use\nthe same debugger for WebKit2WebProcessand the UI process, if desired.\nClick OK when you are ready for WebKit2WebProcess to continue.", L"WebKit2WebProcess has launched", MB_OK | MB_ICONINFORMATION);
#endif
enableDataExecutionPrevention();
enableTerminationOnHeapCorruption();
disableUserModeCallbackExceptionFilter();
CommandLine commandLine;
if (!commandLine.parse(lpstrCmdLine))
return EXIT_FAILURE;
return WebKitMain(commandLine);
}
#endif