2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #include "ProcessLauncher.h"
28 #include "Connection.h"
31 #include <wtf/text/WTFString.h>
34 const LPCWSTR webProcessName = L"WebKit2WebProcess_debug.exe";
36 const LPCWSTR webProcessName = L"WebKit2WebProcess.exe";
40 const LPCWSTR webKitDLLName = L"WebKit_debug.dll";
42 const LPCWSTR webKitDLLName = L"WebKit.dll";
47 void ProcessLauncher::launchProcess()
49 // First, create the server and client identifiers.
50 HANDLE serverIdentifier, clientIdentifier;
51 if (!CoreIPC::Connection::createServerAndClientIdentifiers(serverIdentifier, clientIdentifier)) {
52 // FIXME: What should we do here?
56 // Ensure that the child process inherits the client identifier.
57 ::SetHandleInformation(clientIdentifier, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
59 // To get the full file path to WebKit2WebProcess.exe, we fild the location of WebKit.dll,
60 // remove the last path component, and then append WebKit2WebProcess(_debug).exe.
61 HMODULE webKitModule = ::GetModuleHandleW(webKitDLLName);
66 WCHAR pathStr[MAX_PATH];
67 if (!::GetModuleFileNameW(webKitModule, pathStr, WTF_ARRAY_LENGTH(pathStr)))
70 ::PathRemoveFileSpecW(pathStr);
71 if (!::PathAppendW(pathStr, webProcessName))
74 String commandLine(pathStr);
76 // FIXME: It would be nice if we could just create a CommandLine object and output a command line vector from it.
77 Vector<UChar> commandLineVector;
78 append(commandLineVector, commandLine);
79 append(commandLineVector, " -type webprocess");
80 append(commandLineVector, " -clientIdentifier ");
81 append(commandLineVector, String::number(reinterpret_cast<uintptr_t>(clientIdentifier)));
82 commandLineVector.append('\0');
84 STARTUPINFO startupInfo = { 0 };
85 startupInfo.cb = sizeof(startupInfo);
86 PROCESS_INFORMATION processInformation = { 0 };
87 BOOL result = ::CreateProcessW(0, commandLineVector.data(), 0, 0, true, 0, 0, 0, &startupInfo, &processInformation);
89 // We can now close the client identifier handle.
90 ::CloseHandle(clientIdentifier);
93 // FIXME: What should we do here?
94 DWORD error = ::GetLastError();
98 // Don't leak the thread handle.
99 ::CloseHandle(processInformation.hThread);
101 // We've finished launching the process, message back to the run loop.
102 RunLoop::main()->scheduleWork(WorkItem::create(this, &ProcessLauncher::didFinishLaunchingProcess, processInformation.hProcess, serverIdentifier));
105 void ProcessLauncher::terminateProcess()
107 if (!m_processIdentifier)
110 ::TerminateProcess(m_processIdentifier, 0);
113 void ProcessLauncher::platformInvalidate()
115 if (!m_processIdentifier)
118 ::CloseHandle(m_processIdentifier);
119 m_processIdentifier = 0;
122 } // namespace WebKit