<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[285729] trunk/Source/WebKit</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/285729">285729</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2021-11-12 09:52:26 -0800 (Fri, 12 Nov 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Rename ProcessLauncherMac.mm to ProcessLauncherDarwin.mm
https://bugs.webkit.org/show_bug.cgi?id=233045

Reviewed by Brent Fulgham.

Rename ProcessLauncherMac.mm to ProcessLauncherDarwin.mm since this implementation is used by both macOS and iOS.
I chose the Darwin naming (as opposed to the Cocoa one) because the implementation only relies on libXPC and mach.

* SourcesCocoa.txt:
* UIProcess/Launcher/darwin/ProcessLauncherDarwin.mm: Renamed from Source/WebKit/UIProcess/Launcher/mac/ProcessLauncherMac.mm.
(WebKit::serviceName):
(WebKit::shouldLeakBoost):
(WebKit::systemDirectoryPath):
(WebKit::ProcessLauncher::launchProcess):
(WebKit::ProcessLauncher::terminateProcess):
(WebKit::ProcessLauncher::platformInvalidate):
(WebKit::ProcessLauncher::terminateXPCConnection):
(WebKit::terminateWithReason):
* WebKit.xcodeproj/project.pbxproj:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKitChangeLog">trunk/Source/WebKit/ChangeLog</a></li>
<li><a href="#trunkSourceWebKitSourcesCocoatxt">trunk/Source/WebKit/SourcesCocoa.txt</a></li>
<li><a href="#trunkSourceWebKitWebKitxcodeprojprojectpbxproj">trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>trunk/Source/WebKit/UIProcess/Launcher/darwin/</li>
<li><a href="#trunkSourceWebKitUIProcessLauncherdarwinProcessLauncherDarwinmm">trunk/Source/WebKit/UIProcess/Launcher/darwin/ProcessLauncherDarwin.mm</a></li>
</ul>

<h3>Removed Paths</h3>
<ul>
<li>trunk/Source/WebKit/UIProcess/Launcher/mac/</li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/ChangeLog (285728 => 285729)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/ChangeLog    2021-11-12 17:40:47 UTC (rev 285728)
+++ trunk/Source/WebKit/ChangeLog       2021-11-12 17:52:26 UTC (rev 285729)
</span><span class="lines">@@ -1,5 +1,27 @@
</span><span class="cx"> 2021-11-12  Chris Dumez  <cdumez@apple.com>
</span><span class="cx"> 
</span><ins>+        Rename ProcessLauncherMac.mm to ProcessLauncherDarwin.mm
+        https://bugs.webkit.org/show_bug.cgi?id=233045
+
+        Reviewed by Brent Fulgham.
+
+        Rename ProcessLauncherMac.mm to ProcessLauncherDarwin.mm since this implementation is used by both macOS and iOS.
+        I chose the Darwin naming (as opposed to the Cocoa one) because the implementation only relies on libXPC and mach.
+
+        * SourcesCocoa.txt:
+        * UIProcess/Launcher/darwin/ProcessLauncherDarwin.mm: Renamed from Source/WebKit/UIProcess/Launcher/mac/ProcessLauncherMac.mm.
+        (WebKit::serviceName):
+        (WebKit::shouldLeakBoost):
+        (WebKit::systemDirectoryPath):
+        (WebKit::ProcessLauncher::launchProcess):
+        (WebKit::ProcessLauncher::terminateProcess):
+        (WebKit::ProcessLauncher::platformInvalidate):
+        (WebKit::ProcessLauncher::terminateXPCConnection):
+        (WebKit::terminateWithReason):
+        * WebKit.xcodeproj/project.pbxproj:
+
+2021-11-12  Chris Dumez  <cdumez@apple.com>
+
</ins><span class="cx">         Unreviewed, partial revert of r285565 to resolve a PLT5 regression.
</span><span class="cx">         <rdar://85269156>
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKitSourcesCocoatxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/SourcesCocoa.txt (285728 => 285729)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/SourcesCocoa.txt     2021-11-12 17:40:47 UTC (rev 285728)
+++ trunk/Source/WebKit/SourcesCocoa.txt        2021-11-12 17:52:26 UTC (rev 285729)
</span><span class="lines">@@ -511,7 +511,7 @@
</span><span class="cx"> UIProcess/ios/WKUSDPreviewView.mm
</span><span class="cx"> UIProcess/ios/WKWebEvent.mm
</span><span class="cx"> 
</span><del>-UIProcess/Launcher/mac/ProcessLauncherMac.mm
</del><ins>+UIProcess/Launcher/darwin/ProcessLauncherDarwin.mm
</ins><span class="cx"> 
</span><span class="cx"> UIProcess/mac/CorrectionPanel.mm
</span><span class="cx"> UIProcess/mac/DisplayLink.cpp
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessLauncherdarwinProcessLauncherDarwinmmfromrev285728trunkSourceWebKitUIProcessLaunchermacProcessLauncherMacmm"></a>
<div class="copfile"><h4>Copied: trunk/Source/WebKit/UIProcess/Launcher/darwin/ProcessLauncherDarwin.mm (from rev 285728, trunk/Source/WebKit/UIProcess/Launcher/mac/ProcessLauncherMac.mm) (0 => 285729)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/Launcher/darwin/ProcessLauncherDarwin.mm                           (rev 0)
+++ trunk/Source/WebKit/UIProcess/Launcher/darwin/ProcessLauncherDarwin.mm      2021-11-12 17:52:26 UTC (rev 285729)
</span><span class="lines">@@ -0,0 +1,358 @@
</span><ins>+/*
+ * Copyright (C) 2010-2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "ProcessLauncher.h"
+
+#import "Logging.h"
+#import "ReasonSPI.h"
+#import "WebPreferencesDefaultValues.h"
+#import <WebCore/RuntimeApplicationChecks.h>
+#import <crt_externs.h>
+#import <mach-o/dyld.h>
+#import <mach/mach_error.h>
+#import <mach/machine.h>
+#import <pal/spi/cocoa/ServersSPI.h>
+#import <spawn.h>
+#import <sys/param.h>
+#import <sys/stat.h>
+#import <wtf/MachSendRight.h>
+#import <wtf/RunLoop.h>
+#import <wtf/SoftLinking.h>
+#import <wtf/Threading.h>
+#import <wtf/cocoa/RuntimeApplicationChecksCocoa.h>
+#import <wtf/spi/cf/CFBundleSPI.h>
+#import <wtf/spi/darwin/XPCSPI.h>
+#import <wtf/text/CString.h>
+#import <wtf/text/WTFString.h>
+
+#if PLATFORM(MAC)
+#import "CodeSigning.h"
+#endif
+
+namespace WebKit {
+
+static const char* serviceName(const ProcessLauncher::LaunchOptions& launchOptions)
+{
+    switch (launchOptions.processType) {
+    case ProcessLauncher::ProcessType::Web:
+        return launchOptions.nonValidInjectedCodeAllowed ? "com.apple.WebKit.WebContent.Development" : "com.apple.WebKit.WebContent";
+    case ProcessLauncher::ProcessType::Network:
+        return "com.apple.WebKit.Networking";
+#if ENABLE(GPU_PROCESS)
+    case ProcessLauncher::ProcessType::GPU:
+        return "com.apple.WebKit.GPU";
+#endif
+#if ENABLE(WEB_AUTHN)
+    case ProcessLauncher::ProcessType::WebAuthn:
+        return "com.apple.WebKit.WebAuthn";
+#endif
+    }
+}
+
+static bool shouldLeakBoost(const ProcessLauncher::LaunchOptions& launchOptions)
+{
+#if PLATFORM(IOS_FAMILY)
+    UNUSED_PARAM(launchOptions);
+#if HAVE(RUNNINGBOARD_WEBKIT_PRIORITY_SUPPORT)
+    // On iOS, we don't need to leak a boost message when RunningBoard process assertions give us the
+    // right priorities.
+    static const bool runningBoardHandlesPriorities = isFeatureFlagEnabled("RB_full_manage_WK_jetsam"_s);
+    return !runningBoardHandlesPriorities;
+#else
+    return true;
+#endif // HAVE(RUNNINGBOARD_WEBKIT_PRIORITY_SUPPORT)
+#else
+    // On Mac, leak a boost onto the NetworkProcess, GPUProcess, and WebAuthnProcess.
+#if ENABLE(GPU_PROCESS)
+    if (launchOptions.processType == ProcessLauncher::ProcessType::GPU)
+        return true;
+#endif
+#if ENABLE(WEB_AUTHN)
+    if (launchOptions.processType == ProcessLauncher::ProcessType::WebAuthn)
+        return true;
+#endif
+    return launchOptions.processType == ProcessLauncher::ProcessType::Network;
+#endif
+}
+
+static NSString *systemDirectoryPath()
+{
+    static NeverDestroyed<RetainPtr<NSString>> path = adoptNS([^{
+#if PLATFORM(IOS_FAMILY_SIMULATOR)
+        char *simulatorRoot = getenv("SIMULATOR_ROOT");
+        return simulatorRoot ? [NSString stringWithFormat:@"%s/System/", simulatorRoot] : @"/System/";
+#else
+        return @"/System/";
+#endif
+    }() copy]);
+
+    return path.get().get();
+}
+
+void ProcessLauncher::launchProcess()
+{
+    ASSERT(!m_xpcConnection);
+
+    const char* name;
+    if (!m_launchOptions.customWebContentServiceBundleIdentifier.isNull())
+        name = m_launchOptions.customWebContentServiceBundleIdentifier.data();
+    else
+        name = serviceName(m_launchOptions);
+
+    m_xpcConnection = adoptOSObject(xpc_connection_create(name, nullptr));
+
+    uuid_t uuid;
+    uuid_generate(uuid);
+    xpc_connection_set_oneshot_instance(m_xpcConnection.get(), uuid);
+
+    // Inherit UI process localization. It can be different from child process default localization:
+    // 1. When the application and system frameworks simply have different localized resources available, we should match the application.
+    // 1.1. An important case is WebKitTestRunner, where we should use English localizations for all system frameworks.
+    // 2. When AppleLanguages is passed as command line argument for UI process, or set in its preferences, we should respect it in child processes.
+    auto initializationMessage = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
+    _CFBundleSetupXPCBootstrap(initializationMessage.get());
+#if PLATFORM(IOS_FAMILY)
+    // Clients that set these environment variables explicitly do not have the values automatically forwarded by libxpc.
+    auto containerEnvironmentVariables = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
+    if (const char* environmentHOME = getenv("HOME"))
+        xpc_dictionary_set_string(containerEnvironmentVariables.get(), "HOME", environmentHOME);
+    if (const char* environmentCFFIXED_USER_HOME = getenv("CFFIXED_USER_HOME"))
+        xpc_dictionary_set_string(containerEnvironmentVariables.get(), "CFFIXED_USER_HOME", environmentCFFIXED_USER_HOME);
+    if (const char* environmentTMPDIR = getenv("TMPDIR"))
+        xpc_dictionary_set_string(containerEnvironmentVariables.get(), "TMPDIR", environmentTMPDIR);
+    xpc_dictionary_set_value(initializationMessage.get(), "ContainerEnvironmentVariables", containerEnvironmentVariables.get());
+#endif
+
+    auto languagesIterator = m_launchOptions.extraInitializationData.find("OverrideLanguages");
+    if (languagesIterator != m_launchOptions.extraInitializationData.end()) {
+        LOG_WITH_STREAM(Language, stream << "Process Launcher is copying OverrideLanguages into initialization message: " << languagesIterator->value);
+        auto languages = adoptOSObject(xpc_array_create(nullptr, 0));
+        for (auto& language : languagesIterator->value.split(','))
+            xpc_array_set_string(languages.get(), XPC_ARRAY_APPEND, language.utf8().data());
+        xpc_dictionary_set_value(initializationMessage.get(), "OverrideLanguages", languages.get());
+    }
+
+#if PLATFORM(MAC)
+    xpc_dictionary_set_string(initializationMessage.get(), "WebKitBundleVersion", [[NSBundle bundleWithIdentifier:@"com.apple.WebKit"].infoDictionary[(__bridge NSString *)kCFBundleVersionKey] UTF8String]);
+#endif
+    xpc_connection_set_bootstrap(m_xpcConnection.get(), initializationMessage.get());
+
+    if (shouldLeakBoost(m_launchOptions)) {
+        auto preBootstrapMessage = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
+        xpc_dictionary_set_string(preBootstrapMessage.get(), "message-name", "pre-bootstrap");
+        xpc_connection_send_message(m_xpcConnection.get(), preBootstrapMessage.get());
+    }
+
+    // Create the listening port.
+    mach_port_t listeningPort = MACH_PORT_NULL;
+    auto kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
+    if (kr != KERN_SUCCESS) {
+        LOG_ERROR("Could not allocate mach port, error %x: %s", kr, mach_error_string(kr));
+        CRASH();
+    }
+
+    // Insert a send right so we can send to it.
+    mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
+
+    mach_port_t previousNotificationPort = MACH_PORT_NULL;
+    auto mc = mach_port_request_notification(mach_task_self(), listeningPort, MACH_NOTIFY_NO_SENDERS, 0, listeningPort, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previousNotificationPort);
+    ASSERT(!previousNotificationPort);
+    ASSERT(mc == KERN_SUCCESS);
+    if (mc != KERN_SUCCESS) {
+        // If mach_port_request_notification fails, 'previousNotificationPort' will be uninitialized.
+        LOG_ERROR("mach_port_request_notification failed: (%x) %s", mc, mach_error_string(mc));
+    }
+
+    String clientIdentifier;
+#if PLATFORM(MAC)
+    clientIdentifier = codeSigningIdentifierForCurrentProcess();
+#endif
+    if (clientIdentifier.isNull())
+        clientIdentifier = [[NSBundle mainBundle] bundleIdentifier];
+
+    // FIXME: Switch to xpc_connection_set_bootstrap once it's available everywhere we need.
+    auto bootstrapMessage = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
+
+    if (m_client) {
+        if (m_client->shouldConfigureJSCForTesting())
+            xpc_dictionary_set_bool(bootstrapMessage.get(), "configure-jsc-for-testing", true);
+        if (!m_client->isJITEnabled())
+            xpc_dictionary_set_bool(bootstrapMessage.get(), "disable-jit", true);
+        if (m_client->shouldEnableSharedArrayBuffer())
+            xpc_dictionary_set_bool(bootstrapMessage.get(), "enable-shared-array-buffer", true);
+        if (m_client->shouldEnableCaptivePortalMode())
+            xpc_dictionary_set_bool(bootstrapMessage.get(), "enable-captive-portal-mode", true);
+    }
+
+    xpc_dictionary_set_string(bootstrapMessage.get(), "message-name", "bootstrap");
+
+    xpc_dictionary_set_mach_send(bootstrapMessage.get(), "server-port", listeningPort);
+
+    xpc_dictionary_set_string(bootstrapMessage.get(), "client-identifier", !clientIdentifier.isEmpty() ? clientIdentifier.utf8().data() : *_NSGetProgname());
+    xpc_dictionary_set_string(bootstrapMessage.get(), "client-bundle-identifier", WebCore::applicationBundleIdentifier().utf8().data());
+    xpc_dictionary_set_string(bootstrapMessage.get(), "client-sdk-version", String::number(applicationSDKVersion()).utf8().data());
+    xpc_dictionary_set_string(bootstrapMessage.get(), "process-identifier", String::number(m_launchOptions.processIdentifier.toUInt64()).utf8().data());
+    xpc_dictionary_set_string(bootstrapMessage.get(), "ui-process-name", [[[NSProcessInfo processInfo] processName] UTF8String]);
+    xpc_dictionary_set_string(bootstrapMessage.get(), "service-name", name);
+
+    bool isWebKitDevelopmentBuild = ![[[[NSBundle bundleWithIdentifier:@"com.apple.WebKit"] bundlePath] stringByDeletingLastPathComponent] hasPrefix:systemDirectoryPath()];
+    if (isWebKitDevelopmentBuild) {
+        xpc_dictionary_set_fd(bootstrapMessage.get(), "stdout", STDOUT_FILENO);
+        xpc_dictionary_set_fd(bootstrapMessage.get(), "stderr", STDERR_FILENO);
+    }
+
+    auto extraInitializationData = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
+
+    for (const auto& keyValuePair : m_launchOptions.extraInitializationData)
+        xpc_dictionary_set_string(extraInitializationData.get(), keyValuePair.key.utf8().data(), keyValuePair.value.utf8().data());
+
+    xpc_dictionary_set_value(bootstrapMessage.get(), "extra-initialization-data", extraInitializationData.get());
+
+    auto errorHandlerImpl = [weakProcessLauncher = WeakPtr { *this }, listeningPort] (xpc_object_t event) {
+        ASSERT(!event || xpc_get_type(event) == XPC_TYPE_ERROR);
+
+        auto processLauncher = weakProcessLauncher.get();
+        if (!processLauncher)
+            return;
+
+        if (!processLauncher->isLaunching())
+            return;
+
+#if ASSERT_ENABLED
+        mach_port_urefs_t sendRightCount = 0;
+        mach_port_get_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_SEND, &sendRightCount);
+        ASSERT(sendRightCount >= 1);
+#endif
+
+        // We failed to launch. Release the send right.
+        deallocateSendRightSafely(listeningPort);
+
+        // And the receive right.
+        mach_port_mod_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_RECEIVE, -1);
+
+        if (processLauncher->m_xpcConnection)
+            xpc_connection_cancel(processLauncher->m_xpcConnection.get());
+        processLauncher->m_xpcConnection = nullptr;
+
+        processLauncher->didFinishLaunchingProcess(0, IPC::Connection::Identifier());
+    };
+
+    auto eventHandler = [errorHandlerImpl = WTFMove(errorHandlerImpl), eventHandler = m_client->xpcEventHandler()] (xpc_object_t event) mutable {
+
+        if (!event || xpc_get_type(event) == XPC_TYPE_ERROR) {
+            RunLoop::main().dispatch([errorHandlerImpl = WTFMove(errorHandlerImpl), event = OSObjectPtr(event)] {
+                errorHandlerImpl(event.get());
+            });
+            return;
+        }
+
+        if (eventHandler) {
+            RunLoop::main().dispatch([eventHandler = eventHandler, event = OSObjectPtr(event)] {
+                eventHandler->handleXPCEvent(event.get());
+            });
+        }
+    };
+
+    xpc_connection_set_event_handler(m_xpcConnection.get(), eventHandler);
+
+    xpc_connection_resume(m_xpcConnection.get());
+
+    if (UNLIKELY(m_launchOptions.shouldMakeProcessLaunchFailForTesting)) {
+        eventHandler(nullptr);
+        return;
+    }
+
+    ref();
+    xpc_connection_send_message_with_reply(m_xpcConnection.get(), bootstrapMessage.get(), dispatch_get_main_queue(), ^(xpc_object_t reply) {
+        // Errors are handled in the event handler.
+        // It is possible for this block to be called after the error event handler, in which case we're no longer
+        // launching and we already took care of cleaning things up.
+        if (isLaunching() && xpc_get_type(reply) != XPC_TYPE_ERROR) {
+            ASSERT(xpc_get_type(reply) == XPC_TYPE_DICTIONARY);
+            ASSERT(!strcmp(xpc_dictionary_get_string(reply, "message-name"), "process-finished-launching"));
+
+#if ASSERT_ENABLED
+            mach_port_urefs_t sendRightCount = 0;
+            mach_port_get_refs(mach_task_self(), listeningPort, MACH_PORT_RIGHT_SEND, &sendRightCount);
+            ASSERT(sendRightCount >= 1);
+#endif
+
+            deallocateSendRightSafely(listeningPort);
+
+            if (!m_xpcConnection) {
+                // The process was terminated.
+                didFinishLaunchingProcess(0, IPC::Connection::Identifier());
+                return;
+            }
+
+            // The process has finished launching, grab the pid from the connection.
+            pid_t processIdentifier = xpc_connection_get_pid(m_xpcConnection.get());
+
+            didFinishLaunchingProcess(processIdentifier, IPC::Connection::Identifier(listeningPort, m_xpcConnection));
+            m_xpcConnection = nullptr;
+        }
+
+        deref();
+    });
+}
+
+void ProcessLauncher::terminateProcess()
+{
+    if (m_isLaunching) {
+        terminateXPCConnection();
+        return;
+    }
+
+    if (!m_processIdentifier)
+        return;
+
+    kill(m_processIdentifier, SIGKILL);
+    m_processIdentifier = 0;
+}
+
+void ProcessLauncher::platformInvalidate()
+{
+    terminateXPCConnection();
+}
+
+void ProcessLauncher::terminateXPCConnection()
+{
+    if (!m_xpcConnection)
+        return;
+
+    xpc_connection_cancel(m_xpcConnection.get());
+    terminateWithReason(m_xpcConnection.get(), WebKit::ReasonCode::Invalidation, "ProcessLauncher::platformInvalidate");
+    m_xpcConnection = nullptr;
+}
+
+void terminateWithReason(xpc_connection_t connection, ReasonCode, const char*)
+{
+    ALLOW_DEPRECATED_DECLARATIONS_BEGIN
+    xpc_connection_kill(connection, SIGKILL);
+    ALLOW_DEPRECATED_DECLARATIONS_END
+}
+
+} // namespace WebKit
</ins></span></pre></div>
<a id="trunkSourceWebKitWebKitxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj (285728 => 285729)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj     2021-11-12 17:40:47 UTC (rev 285728)
+++ trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj        2021-11-12 17:52:26 UTC (rev 285729)
</span><span class="lines">@@ -3788,6 +3788,7 @@
</span><span class="cx">          46809A7B23D9225300C297D0 /* WebCookieCache.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebCookieCache.cpp; sourceTree = "<group>"; };
</span><span class="cx">          4683569A21E81CC7006E27A3 /* ProvisionalPageProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProvisionalPageProxy.h; sourceTree = "<group>"; };
</span><span class="cx">          4683569B21E81CC7006E27A3 /* ProvisionalPageProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ProvisionalPageProxy.cpp; sourceTree = "<group>"; };
</span><ins>+               469DE802273ECC5D00930276 /* ProcessLauncherDarwin.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ProcessLauncherDarwin.mm; sourceTree = "<group>"; };
</ins><span class="cx">           46A2B6061E5675A200C3DEDA /* BackgroundProcessResponsivenessTimer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BackgroundProcessResponsivenessTimer.cpp; sourceTree = "<group>"; };
</span><span class="cx">          46A2B6071E5675A200C3DEDA /* BackgroundProcessResponsivenessTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BackgroundProcessResponsivenessTimer.h; sourceTree = "<group>"; };
</span><span class="cx">          46AC532425DED81E003B57EC /* GPUProcessConnectionParameters.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPUProcessConnectionParameters.h; sourceTree = "<group>"; };
</span><span class="lines">@@ -5187,7 +5188,6 @@
</span><span class="cx">          BC111B0A112F5E4F00337BAB /* WebFrameProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebFrameProxy.cpp; sourceTree = "<group>"; };
</span><span class="cx">          BC111B0B112F5E4F00337BAB /* WebPageProxy.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 4; path = WebPageProxy.cpp; sourceTree = "<group>"; };
</span><span class="cx">          BC111B0D112F5E4F00337BAB /* WebProcessProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebProcessProxy.cpp; sourceTree = "<group>"; };
</span><del>-               BC111B1B112F5FE600337BAB /* ProcessLauncherMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ProcessLauncherMac.mm; sourceTree = "<group>"; };
</del><span class="cx">           BC111B5B112F629800337BAB /* WebEventFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebEventFactory.h; sourceTree = "<group>"; };
</span><span class="cx">          BC111B5C112F629800337BAB /* WebEventFactory.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebEventFactory.mm; sourceTree = "<group>"; };
</span><span class="cx">          BC14DF75120B5B7900826C0C /* InjectedBundleScriptWorld.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InjectedBundleScriptWorld.h; sourceTree = "<group>"; };
</span><span class="lines">@@ -8417,6 +8417,14 @@
</span><span class="cx">                  path = cocoa;
</span><span class="cx">                  sourceTree = "<group>";
</span><span class="cx">          };
</span><ins>+               469DE801273ECC5D00930276 /* darwin */ = {
+                       isa = PBXGroup;
+                       children = (
+                               469DE802273ECC5D00930276 /* ProcessLauncherDarwin.mm */,
+                       );
+                       path = darwin;
+                       sourceTree = "<group>";
+               };
</ins><span class="cx">           4A410F4119AF7B27002EBAB5 /* MediaStream */ = {
</span><span class="cx">                  isa = PBXGroup;
</span><span class="cx">                  children = (
</span><span class="lines">@@ -10426,7 +10434,7 @@
</span><span class="cx">          BC111B18112F5FB400337BAB /* Launcher */ = {
</span><span class="cx">                  isa = PBXGroup;
</span><span class="cx">                  children = (
</span><del>-                               BC111B19112F5FC500337BAB /* mac */,
</del><ins>+                                469DE801273ECC5D00930276 /* darwin */,
</ins><span class="cx">                           1AE117F511DBB30900981615 /* ProcessLauncher.cpp */,
</span><span class="cx">                          BC1A7C571136E19C00FB7167 /* ProcessLauncher.h */,
</span><span class="cx">                  );
</span><span class="lines">@@ -10433,14 +10441,6 @@
</span><span class="cx">                  path = Launcher;
</span><span class="cx">                  sourceTree = "<group>";
</span><span class="cx">          };
</span><del>-               BC111B19112F5FC500337BAB /* mac */ = {
-                       isa = PBXGroup;
-                       children = (
-                               BC111B1B112F5FE600337BAB /* ProcessLauncherMac.mm */,
-                       );
-                       path = mac;
-                       sourceTree = "<group>";
-               };
</del><span class="cx">           BC111B47112F616900337BAB /* mac */ = {
</span><span class="cx">                  isa = PBXGroup;
</span><span class="cx">                  children = (
</span></span></pre>
</div>
</div>

</body>
</html>