|
1 /* |
|
2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
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. |
|
12 * |
|
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. |
|
24 */ |
|
25 |
|
26 #include "ProcessLauncher.h" |
|
27 |
|
28 #include "Connection.h" |
|
29 #include "RunLoop.h" |
|
30 #include "WebProcess.h" |
|
31 #include <WebCore/PlatformString.h> |
|
32 #include <runtime/InitializeThreading.h> |
|
33 #include <wtf/Threading.h> |
|
34 |
|
35 using namespace WebCore; |
|
36 |
|
37 namespace WebKit { |
|
38 |
|
39 void ProcessLauncher::launchProcess() |
|
40 { |
|
41 // First, create the server and client identifiers. |
|
42 HANDLE serverIdentifier, clientIdentifier; |
|
43 if (!CoreIPC::Connection::createServerAndClientIdentifiers(serverIdentifier, clientIdentifier)) { |
|
44 // FIXME: What should we do here? |
|
45 ASSERT_NOT_REACHED(); |
|
46 } |
|
47 |
|
48 // Ensure that the child process inherits the client identifier. |
|
49 ::SetHandleInformation(clientIdentifier, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); |
|
50 |
|
51 Vector<UChar> commandLineVector; |
|
52 |
|
53 // FIXME: We would like to pass a full path to the .exe here. |
|
54 #ifndef DEBUG_ALL |
|
55 String commandLine(L"WebKit2WebProcess.exe"); |
|
56 #else |
|
57 String commandLine(L"WebKit2WebProcess_debug.exe"); |
|
58 #endif |
|
59 |
|
60 append(commandLineVector, commandLine); |
|
61 append(commandLineVector, " -mode webprocess"); |
|
62 append(commandLineVector, " -clientIdentifier "); |
|
63 append(commandLineVector, String::number(reinterpret_cast<uintptr_t>(clientIdentifier))); |
|
64 commandLineVector.append('\0'); |
|
65 |
|
66 STARTUPINFO startupInfo = { 0 }; |
|
67 startupInfo.cb = sizeof(startupInfo); |
|
68 PROCESS_INFORMATION processInformation = { 0 }; |
|
69 BOOL result = ::CreateProcessW(0, commandLineVector.data(), 0, 0, true, 0, 0, 0, &startupInfo, &processInformation); |
|
70 |
|
71 // We can now close the client identifier handle. |
|
72 ::CloseHandle(clientIdentifier); |
|
73 |
|
74 if (!result) { |
|
75 // FIXME: What should we do here? |
|
76 DWORD error = ::GetLastError(); |
|
77 ASSERT_NOT_REACHED(); |
|
78 } |
|
79 |
|
80 // Don't leak the thread handle. |
|
81 ::CloseHandle(processInformation.hThread); |
|
82 |
|
83 // We've finished launching the process, message back to the run loop. |
|
84 RunLoop::main()->scheduleWork(WorkItem::create(this, &ProcessLauncher::didFinishLaunchingProcess, processInformation.hProcess, serverIdentifier)); |
|
85 } |
|
86 |
|
87 void ProcessLauncher::terminateProcess() |
|
88 { |
|
89 if (!m_processIdentifier) |
|
90 return; |
|
91 |
|
92 ::TerminateProcess(m_processIdentifier, 0); |
|
93 } |
|
94 |
|
95 static void* webThreadBody(void* context) |
|
96 { |
|
97 HANDLE clientIdentifier = reinterpret_cast<HANDLE>(context); |
|
98 |
|
99 // Initialization |
|
100 JSC::initializeThreading(); |
|
101 WTF::initializeMainThread(); |
|
102 |
|
103 WebProcess::shared().initialize(clientIdentifier, RunLoop::current()); |
|
104 RunLoop::run(); |
|
105 |
|
106 return 0; |
|
107 } |
|
108 |
|
109 CoreIPC::Connection::Identifier ProcessLauncher::createWebThread() |
|
110 { |
|
111 // First, create the server and client identifiers. |
|
112 HANDLE serverIdentifier, clientIdentifier; |
|
113 if (!CoreIPC::Connection::createServerAndClientIdentifiers(serverIdentifier, clientIdentifier)) { |
|
114 // FIXME: What should we do here? |
|
115 ASSERT_NOT_REACHED(); |
|
116 } |
|
117 |
|
118 if (!createThread(webThreadBody, reinterpret_cast<void*>(clientIdentifier), "WebKit2: WebThread")) { |
|
119 ::CloseHandle(serverIdentifier); |
|
120 ::CloseHandle(clientIdentifier); |
|
121 return 0; |
|
122 } |
|
123 |
|
124 return serverIdentifier; |
|
125 } |
|
126 |
|
127 } // namespace WebKit |