Make debug-safari --target-web-process work again
https://bugs.webkit.org/show_bug.cgi?id=107088

Reviewed by Anders Carlsson.

Source/WebKit2: 

--target-web-process was made harder to use during recent refactoring
by requiring users to pass a -ui-process-name parameter in addition to
the -client-executable. Instead, we should conjure up that name from
the information we already have at our disposal.

* WebProcess/mac/WebProcessMainMac.mm:
(WebKit::WebProcessMainDelegate::getConnectionIdentifier):
Simplify by making the non-client-executable path use the base
class implementation.

(WebKit::WebProcessMainDelegate::getClientIdentifier):
Ditto.

(WebKit::WebProcessMainDelegate::getClientProcessName):
Added. Create a client process name (its actually not too important what it is, as it
is only used to show a name in Activity Monitor) from the passed in client executable
path.

Tools: 

* Scripts/webkitdirs.pm:
(execMacWebKitAppForDebugging):
The shim is now called SecItemShim.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@139961 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit2/ChangeLog b/Source/WebKit2/ChangeLog
index 035f669..37e92702 100644
--- a/Source/WebKit2/ChangeLog
+++ b/Source/WebKit2/ChangeLog
@@ -1,5 +1,30 @@
 2013-01-16  Sam Weinig  <sam@webkit.org>
 
+        Make debug-safari --target-web-process work again
+        https://bugs.webkit.org/show_bug.cgi?id=107088
+
+        Reviewed by Anders Carlsson.
+
+        --target-web-process was made harder to use during recent refactoring
+        by requiring users to pass a -ui-process-name parameter in addition to
+        the -client-executable. Instead, we should conjure up that name from
+        the information we already have at our disposal.
+
+        * WebProcess/mac/WebProcessMainMac.mm:
+        (WebKit::WebProcessMainDelegate::getConnectionIdentifier):
+        Simplify by making the non-client-executable path use the base
+        class implementation.
+
+        (WebKit::WebProcessMainDelegate::getClientIdentifier):
+        Ditto.
+
+        (WebKit::WebProcessMainDelegate::getClientProcessName):
+        Added. Create a client process name (its actually not too important what it is, as it
+        is only used to show a name in Activity Monitor) from the passed in client executable
+        path.
+
+2013-01-16  Sam Weinig  <sam@webkit.org>
+
         Fix crashes on the bots.
 
         The WebKit LoaderStrategy for loading synchronously was only implemented
diff --git a/Source/WebKit2/WebProcess/mac/WebProcessMainMac.mm b/Source/WebKit2/WebProcess/mac/WebProcessMainMac.mm
index 029a4ce..3b9d131 100644
--- a/Source/WebKit2/WebProcess/mac/WebProcessMainMac.mm
+++ b/Source/WebKit2/WebProcess/mac/WebProcessMainMac.mm
@@ -60,68 +60,58 @@
 
     virtual bool getConnectionIdentifier(CoreIPC::Connection::Identifier& identifier)
     {
-        String serviceName = m_commandLine["servicename"];
         String clientExecutable = m_commandLine["client-executable"];
-        if (serviceName.isEmpty() && clientExecutable.isEmpty())
+        if (clientExecutable.isEmpty())
+            return ChildProcessMainDelegate::getConnectionIdentifier(identifier);
+
+        mach_port_name_t publishedService;
+        mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &publishedService);
+        mach_port_insert_right(mach_task_self(), publishedService, publishedService, MACH_MSG_TYPE_MAKE_SEND);
+        // Make it possible to look up.
+        String serviceName = String::format("com.apple.WebKit.WebProcess-%d", getpid());
+        if (kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.utf8().data()), publishedService, 0)) {
+            WTFLogAlways("Failed to register service name \"%s\". %s (%x)\n", serviceName.utf8().data(), mach_error_string(kr), kr);
+            return false;
+        }
+
+        CString command = clientExecutable.utf8();
+        const char* args[] = { command.data(), 0 };
+
+        EnvironmentVariables environmentVariables;
+        environmentVariables.set(EnvironmentVariables::preexistingProcessServiceNameKey(), serviceName.utf8().data());
+        environmentVariables.set(EnvironmentVariables::preexistingProcessTypeKey(), m_commandLine["type"].utf8().data());
+
+        posix_spawn_file_actions_t fileActions;
+        posix_spawn_file_actions_init(&fileActions);
+        posix_spawn_file_actions_addinherit_np(&fileActions, STDIN_FILENO);
+        posix_spawn_file_actions_addinherit_np(&fileActions, STDOUT_FILENO);
+        posix_spawn_file_actions_addinherit_np(&fileActions, STDERR_FILENO);
+
+        posix_spawnattr_t attributes;
+        posix_spawnattr_init(&attributes);
+        posix_spawnattr_setflags(&attributes, POSIX_SPAWN_CLOEXEC_DEFAULT | POSIX_SPAWN_SETPGROUP);
+
+        int spawnResult = posix_spawn(0, command.data(), &fileActions, &attributes, const_cast<char**>(args), environmentVariables.environmentPointer());
+
+        posix_spawnattr_destroy(&attributes);
+        posix_spawn_file_actions_destroy(&fileActions);
+
+        if (spawnResult)
             return false;
 
-        mach_port_t serverPort;
-        if (clientExecutable.isEmpty()) {
-            kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
-            if (kr) {
-                WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
-                return false;
-            }
-        } else {
-            mach_port_name_t publishedService;
-            mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &publishedService);
-            mach_port_insert_right(mach_task_self(), publishedService, publishedService, MACH_MSG_TYPE_MAKE_SEND);
-            // Make it possible to look up.
-            serviceName = String::format("com.apple.WebKit.WebProcess-%d", getpid());
-            if (kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.utf8().data()), publishedService, 0)) {
-                WTFLogAlways("Failed to register service name \"%s\". %s (%x)\n", serviceName.utf8().data(), mach_error_string(kr), kr);
-                return false;
-            }
+        mach_msg_empty_rcv_t message;
+        if (kern_return_t kr = mach_msg(&message.header, MACH_RCV_MSG, 0, sizeof(message), publishedService, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)) {
+            WTFLogAlways("Failed to receive port from the UI process. %s (%x)\n", mach_error_string(kr), kr);
+            return false;
+        }
 
-            CString command = clientExecutable.utf8();
-            const char* args[] = { command.data(), 0 };
-
-            EnvironmentVariables environmentVariables;
-            environmentVariables.set(EnvironmentVariables::preexistingProcessServiceNameKey(), serviceName.utf8().data());
-            environmentVariables.set(EnvironmentVariables::preexistingProcessTypeKey(), m_commandLine["type"].utf8().data());
-
-            posix_spawn_file_actions_t fileActions;
-            posix_spawn_file_actions_init(&fileActions);
-            posix_spawn_file_actions_addinherit_np(&fileActions, STDIN_FILENO);
-            posix_spawn_file_actions_addinherit_np(&fileActions, STDOUT_FILENO);
-            posix_spawn_file_actions_addinherit_np(&fileActions, STDERR_FILENO);
-
-            posix_spawnattr_t attributes;
-            posix_spawnattr_init(&attributes);
-            posix_spawnattr_setflags(&attributes, POSIX_SPAWN_CLOEXEC_DEFAULT | POSIX_SPAWN_SETPGROUP);
-
-            int spawnResult = posix_spawn(0, command.data(), &fileActions, &attributes, const_cast<char**>(args), environmentVariables.environmentPointer());
-
-            posix_spawnattr_destroy(&attributes);
-            posix_spawn_file_actions_destroy(&fileActions);
-
-            if (spawnResult)
-                return false;
-
-            mach_msg_empty_rcv_t message;
-            if (kern_return_t kr = mach_msg(&message.header, MACH_RCV_MSG, 0, sizeof(message), publishedService, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)) {
-                WTFLogAlways("Failed to receive port from the UI process. %s (%x)\n", mach_error_string(kr), kr);
-                return false;
-            }
-
-            mach_port_mod_refs(mach_task_self(), publishedService, MACH_PORT_RIGHT_RECEIVE, -1);
-            serverPort = message.header.msgh_remote_port;
-            mach_port_type_t portType;
-            kern_return_t kr = mach_port_type(mach_task_self(), serverPort, &portType);
-            if (kr || !(portType & MACH_PORT_TYPE_SEND)) {
-                WTFLogAlways("Failed to obtain send right for port received from the UI process.\n");
-                return false;
-            }
+        mach_port_mod_refs(mach_task_self(), publishedService, MACH_PORT_RIGHT_RECEIVE, -1);
+        mach_port_t serverPort = message.header.msgh_remote_port;
+        mach_port_type_t portType;
+        kern_return_t kr = mach_port_type(mach_task_self(), serverPort, &portType);
+        if (kr || !(portType & MACH_PORT_TYPE_SEND)) {
+            WTFLogAlways("Failed to obtain send right for port received from the UI process.\n");
+            return false;
         }
 
         identifier = serverPort;
@@ -131,20 +121,32 @@
     virtual bool getClientIdentifier(String& clientIdentifier)
     {
         String clientExecutable = m_commandLine["client-executable"];
-
         if (clientExecutable.isEmpty())
-            clientIdentifier = m_commandLine["client-identifier"];
-        else {
-            RetainPtr<NSURL> clientExecutableURL = adoptNS([[NSURL alloc] initFileURLWithPath:nsStringFromWebCoreString(clientExecutable)]);
-            RetainPtr<CFURLRef> clientBundleURL = adoptCF(WKCopyBundleURLForExecutableURL((CFURLRef)clientExecutableURL.get()));
-            RetainPtr<NSBundle> clientBundle = adoptNS([[NSBundle alloc] initWithURL:(NSURL *)clientBundleURL.get()]);
-            clientIdentifier = [clientBundle.get() bundleIdentifier];
-        }
+            return ChildProcessMainDelegate::getClientIdentifier(clientIdentifier);
 
+        RetainPtr<NSURL> clientExecutableURL = adoptNS([[NSURL alloc] initFileURLWithPath:nsStringFromWebCoreString(clientExecutable)]);
+        RetainPtr<CFURLRef> clientBundleURL = adoptCF(WKCopyBundleURLForExecutableURL((CFURLRef)clientExecutableURL.get()));
+        RetainPtr<NSBundle> clientBundle = adoptNS([[NSBundle alloc] initWithURL:(NSURL *)clientBundleURL.get()]);
+        clientIdentifier = [clientBundle.get() bundleIdentifier];
         if (clientIdentifier.isEmpty())
             return false;
         return true;
     }
+
+    virtual bool getClientProcessName(String& clientProcessName)
+    {
+        String clientExecutable = m_commandLine["client-executable"];
+        if (clientExecutable.isEmpty())
+            return ChildProcessMainDelegate::getClientProcessName(clientProcessName);
+
+        // Conjure up a process name by using everything after the last slash from the client-executable,
+        // e.g. /Applications/Safari.app/Contents/MacOS/Safari becomes Safari.
+        size_t lastSlash = clientExecutable.reverseFind('/');
+        clientProcessName = clientExecutable.substring(lastSlash + 1);
+        if (clientProcessName.isEmpty())
+            return false;
+        return true;
+    }
 };
 
 int WebProcessMain(const CommandLine& commandLine)
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index c58525f..a0de022 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,14 @@
+2013-01-16  Sam Weinig  <sam@webkit.org>
+
+        Make debug-safari --target-web-process work again
+        https://bugs.webkit.org/show_bug.cgi?id=107088
+
+        Reviewed by Anders Carlsson.
+
+        * Scripts/webkitdirs.pm:
+        (execMacWebKitAppForDebugging):
+        The shim is now called SecItemShim.
+
 2013-01-16  Alan Cutter  <alancutter@chromium.org>
 
         GCE build scripts don't take working directory into account when calling findzone.sh
diff --git a/Tools/Scripts/webkitdirs.pm b/Tools/Scripts/webkitdirs.pm
index bf08938..8d37a8c 100755
--- a/Tools/Scripts/webkitdirs.pm
+++ b/Tools/Scripts/webkitdirs.pm
@@ -2685,7 +2685,7 @@
             die "Targetting the Web Process is not compatible with using an XPC Service for the Web Process at this time.";
         }
         
-        my $webProcessShimPath = File::Spec->catfile($productDir, "WebProcessShim.dylib");
+        my $webProcessShimPath = File::Spec->catfile($productDir, "SecItemShim.dylib");
         my $webProcessPath = File::Spec->catdir($productDir, "WebProcess.app");
         my $webKit2ExecutablePath = File::Spec->catfile($productDir, "WebKit2.framework", "WebKit2");