WebKit2/UIProcess/Launcher/mac/ProcessLauncherMac.mm
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "RunLoop.h"
       
    29 #include "WebProcess.h"
       
    30 #include "WebSystemInterface.h"
       
    31 
       
    32 #include <crt_externs.h>
       
    33 #include <runtime/InitializeThreading.h>
       
    34 #include <servers/bootstrap.h>
       
    35 #include <spawn.h>
       
    36 #include <wtf/PassRefPtr.h>
       
    37 #include <wtf/Threading.h>
       
    38 
       
    39 // FIXME: We should be doing this another way.
       
    40 extern "C" kern_return_t bootstrap_register2(mach_port_t, name_t, mach_port_t, uint64_t);
       
    41 
       
    42 namespace WebKit {
       
    43 
       
    44 void ProcessLauncher::launchProcess()
       
    45 {
       
    46     // Create the listening port.
       
    47     mach_port_t listeningPort;
       
    48     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
       
    49     
       
    50     // Insert a send right so we can send to it.
       
    51     mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
       
    52     
       
    53     NSString *webProcessAppPath = [[NSBundle bundleWithIdentifier:@"com.apple.WebKit2"] pathForAuxiliaryExecutable:@"WebProcess.app"];
       
    54     NSString *webProcessAppExecutablePath = [[NSBundle bundleWithPath:webProcessAppPath] executablePath];
       
    55 
       
    56     const char* path = [webProcessAppExecutablePath fileSystemRepresentation];
       
    57     const char* args[] = { path, "-mode", "legacywebprocess", 0 };
       
    58 
       
    59     // Register ourselves.
       
    60     kern_return_t kr = bootstrap_register2(bootstrap_port, (char*)"com.apple.WebKit.WebProcess", listeningPort, /* BOOTSTRAP_PER_PID_SERVICE */ 1);
       
    61     if (kr)
       
    62         NSLog(@"bootstrap_register2 result: %x", kr);
       
    63     
       
    64     pid_t processIdentifier;
       
    65     int result = posix_spawn(&processIdentifier, path, 0, 0, (char *const*)args, *_NSGetEnviron());
       
    66     if (result)
       
    67         NSLog(@"posix_spawn result: %d", result);
       
    68 
       
    69     // We've finished launching the process, message back to the main run loop.
       
    70     RunLoop::main()->scheduleWork(WorkItem::create(this, &ProcessLauncher::didFinishLaunchingProcess, processIdentifier, listeningPort));
       
    71 }
       
    72 
       
    73 void ProcessLauncher::terminateProcess()
       
    74 {    
       
    75     if (!m_processIdentifier)
       
    76         return;
       
    77     
       
    78     kill(m_processIdentifier, SIGKILL);
       
    79 }
       
    80 
       
    81 static void* webThreadBody(void* context)
       
    82 {
       
    83     mach_port_t serverPort = static_cast<mach_port_t>(reinterpret_cast<uintptr_t>(context));
       
    84     
       
    85     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    86 
       
    87     InitWebCoreSystemInterface();
       
    88     JSC::initializeThreading();
       
    89     WTF::initializeMainThread();
       
    90 
       
    91     WebProcess::shared().initialize(serverPort, RunLoop::current());
       
    92 
       
    93     [pool drain];
       
    94 
       
    95     RunLoop::current()->run();
       
    96 
       
    97     return 0;
       
    98 }
       
    99 
       
   100 CoreIPC::Connection::Identifier ProcessLauncher::createWebThread()
       
   101 {
       
   102     // Create the service port.
       
   103      mach_port_t listeningPort;
       
   104      mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
       
   105      
       
   106      // Insert a send right so we can send to it.
       
   107      mach_port_insert_right(mach_task_self(), listeningPort, listeningPort, MACH_MSG_TYPE_MAKE_SEND);
       
   108 
       
   109     if (!createThread(webThreadBody, reinterpret_cast<void*>(listeningPort), "WebKit2: WebThread")) {
       
   110         mach_port_destroy(mach_task_self(), listeningPort);
       
   111         return MACH_PORT_NULL;
       
   112     }
       
   113 
       
   114     return listeningPort;
       
   115 }
       
   116     
       
   117 } // namespace WebKit