Changeset 138981 in webkit


Ignore:
Timestamp:
Jan 7, 2013 1:26:01 PM (11 years ago)
Author:
weinig@apple.com
Message:

Merge child process main functions as much as possible
https://bugs.webkit.org/show_bug.cgi?id=106192

Reviewed by Anders Carlsson.

To reduce copied code, this merges the Mac main functions for the WebProcess,
NetworkProcess, PluginProcess, and SharedWorkerProcess. Since slight variations
are needed, it adds a delegate class for hooks into the main function that each
process can override if necessary.

  • NetworkProcess/mac/NetworkProcessMainMac.mm:

(WebKit::NetworkProcessMain):
Remove almost everything with a call to ChildProcessMain with the default delegate.

  • PluginProcess/mac/PluginProcessMac.mm:

(WebKit::PluginProcess::platformInitializeProcess):
Move defaults registration here from PluginProcessMainMac.

  • PluginProcess/mac/PluginProcessMainMac.mm:

(PluginProcessMainDelegate):
Add a delegate for the new shared main function that does does PluginProcess
specific work.

(WebKit::PluginProcessMainDelegate::doPreInitializationWork):
Move DYLD_INSERT_LIBRARIES removal and createPluginMIMETypesPreferences special mode
to this pre-initialization hook.

(WebKit::PluginProcessMainDelegate::doPostRunWork):
Move private temporary and cache directories clean up to this post run hook.

(WebKit::PluginProcessMain):
Call the ChildProcessMain shared main function with the PluginProcess as the process
to initialize and PluginProcessMainDelegate as the main delegate.

  • Shared/mac/ChildProcessMain.h: Added.

(ChildProcessMainDelegate):
ChildProcessMainDelegate is a helper class that process can subclass to hook into
the main function.

(WebKit::ChildProcessMain):
Add a single version of the "main" function that uses the ChildProcessMainDelegate to
allow processes to hook into it.

  • Shared/mac/ChildProcessMain.mm:

(WebKit::ChildProcessMainDelegate::~ChildProcessMainDelegate):
(WebKit::ChildProcessMainDelegate::installSignalHandlers):
(WebKit::ChildProcessMainDelegate::doPreInitializationWork):
(WebKit::ChildProcessMainDelegate::getConnectionIdentifier):
(WebKit::ChildProcessMainDelegate::getClientIdentifier):
(WebKit::ChildProcessMainDelegate::getClientProcessName):
(WebKit::ChildProcessMainDelegate::doPostRunWork):
Add default implementations of the delegate hooks, most processes should be able to
use the defaults.

  • SharedWorkerProcess/mac/SharedWorkerProcessMainMac.mm:

(WebKit::SharedWorkerProcessMain):
Remove almost everything with a call to ChildProcessMain with the default delegate.

  • WebKit2.xcodeproj/project.pbxproj:

Add new files.

  • WebProcess/mac/WebProcessMainMac.mm:

(WebProcessMainDelegate):
Add a delegate for the new shared main function that does does WebProcess
specific work.

(WebKit::WebProcessMainDelegate::doPreInitializationWork):
Move DYLD_INSERT_LIBRARIES removal to this this pre-initialization hook.

(WebKit::WebProcessMainDelegate::getConnectionIdentifier):
A custom getConnectionIdentifier is necessary for the WebProcess to keep support for having the WebProcess
be able to launch the UIProcess (for debugging purposes).

(WebKit::WebProcessMainDelegate::getClientIdentifier):
For the same reasons as about, a custom getClientIdentifier is needed.

(WebKit::WebProcessMain):
Call the ChildProcessMain shared main function with the WebProcess as the process
to initialize and WebProcessMainDelegate as the main delegate.

Location:
trunk/Source/WebKit2
Files:
1 added
9 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r138973 r138981  
     12013-01-05  Sam Weinig  <sam@webkit.org>
     2
     3        Merge child process main functions as much as possible
     4        https://bugs.webkit.org/show_bug.cgi?id=106192
     5
     6        Reviewed by Anders Carlsson.
     7
     8        To reduce copied code, this merges the Mac main functions for the WebProcess,
     9        NetworkProcess, PluginProcess, and SharedWorkerProcess. Since slight variations
     10        are needed, it adds a delegate class for hooks into the main function that each
     11        process can override if necessary.
     12
     13        * NetworkProcess/mac/NetworkProcessMainMac.mm:
     14        (WebKit::NetworkProcessMain):
     15        Remove almost everything with a call to ChildProcessMain with the default delegate.
     16
     17        * PluginProcess/mac/PluginProcessMac.mm:
     18        (WebKit::PluginProcess::platformInitializeProcess):
     19        Move defaults registration here from PluginProcessMainMac.
     20
     21        * PluginProcess/mac/PluginProcessMainMac.mm:
     22        (PluginProcessMainDelegate):
     23        Add a delegate for the new shared main function that does does PluginProcess
     24        specific work.
     25
     26        (WebKit::PluginProcessMainDelegate::doPreInitializationWork):
     27        Move DYLD_INSERT_LIBRARIES removal and createPluginMIMETypesPreferences special mode
     28        to this pre-initialization hook.
     29
     30        (WebKit::PluginProcessMainDelegate::doPostRunWork):
     31        Move private temporary and cache directories clean up to this post run hook.
     32
     33        (WebKit::PluginProcessMain):
     34        Call the ChildProcessMain shared main function with the PluginProcess as the process
     35        to initialize and PluginProcessMainDelegate as the main delegate.
     36
     37        * Shared/mac/ChildProcessMain.h: Added.
     38        (ChildProcessMainDelegate):
     39        ChildProcessMainDelegate is a helper class that process can subclass to hook into
     40        the main function.
     41
     42        (WebKit::ChildProcessMain):
     43        Add a single version of the "main" function that uses the ChildProcessMainDelegate to
     44        allow processes to hook into it.
     45
     46        * Shared/mac/ChildProcessMain.mm:
     47        (WebKit::ChildProcessMainDelegate::~ChildProcessMainDelegate):
     48        (WebKit::ChildProcessMainDelegate::installSignalHandlers):
     49        (WebKit::ChildProcessMainDelegate::doPreInitializationWork):
     50        (WebKit::ChildProcessMainDelegate::getConnectionIdentifier):
     51        (WebKit::ChildProcessMainDelegate::getClientIdentifier):
     52        (WebKit::ChildProcessMainDelegate::getClientProcessName):
     53        (WebKit::ChildProcessMainDelegate::doPostRunWork):
     54        Add default implementations of the delegate hooks, most processes should be able to
     55        use the defaults.
     56
     57        * SharedWorkerProcess/mac/SharedWorkerProcessMainMac.mm:
     58        (WebKit::SharedWorkerProcessMain):
     59        Remove almost everything with a call to ChildProcessMain with the default delegate.
     60
     61        * WebKit2.xcodeproj/project.pbxproj:
     62        Add new files.
     63
     64        * WebProcess/mac/WebProcessMainMac.mm:
     65        (WebProcessMainDelegate):
     66        Add a delegate for the new shared main function that does does WebProcess
     67        specific work.
     68
     69        (WebKit::WebProcessMainDelegate::doPreInitializationWork):
     70        Move DYLD_INSERT_LIBRARIES removal to this this pre-initialization hook.
     71
     72        (WebKit::WebProcessMainDelegate::getConnectionIdentifier):
     73        A custom getConnectionIdentifier is necessary for the WebProcess to keep support for having the WebProcess
     74        be able to launch the UIProcess (for debugging purposes).
     75
     76        (WebKit::WebProcessMainDelegate::getClientIdentifier):
     77        For the same reasons as about, a custom getClientIdentifier is needed.
     78
     79        (WebKit::WebProcessMain):
     80        Call the ChildProcessMain shared main function with the WebProcess as the process
     81        to initialize and WebProcessMainDelegate as the main delegate.
     82
    1832013-01-07  Simon Fraser  <simon.fraser@apple.com>
    284
  • trunk/Source/WebKit2/NetworkProcess/mac/NetworkProcessMainMac.mm

    r138913 r138981  
    2929#if ENABLE(NETWORK_PROCESS)
    3030
    31 #import "CommandLine.h"
     31#import "ChildProcessMain.h"
    3232#import "NetworkProcess.h"
    33 #import "WebKit2Initialize.h"
    34 #import <WebCore/RunLoop.h>
    35 #import <WebKitSystemInterface.h>
    36 #import <mach/mach_error.h>
    37 #import <servers/bootstrap.h>
    38 #import <stdio.h>
    39 #import <wtf/MainThread.h>
    40 #import <wtf/RetainPtr.h>
    41 #import <wtf/text/CString.h>
    42 #import <wtf/text/WTFString.h>
    43 
    44 #define SHOW_CRASH_REPORTER 1
    45 
    46 using namespace WebCore;
    4733
    4834namespace WebKit {
    4935
    50 // FIXME: There is much code here that is duplicated in WebProcessMainMac.mm, we should add a shared base class where
    51 // we can put everything.
    52 
    5336int NetworkProcessMain(const CommandLine& commandLine)
    5437{
    55     String serviceName = commandLine["servicename"];
    56     if (serviceName.isEmpty())
    57         return EXIT_FAILURE;
    58    
    59     // Get the server port.
    60     mach_port_t serverPort;
    61     kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
    62     if (kr) {
    63         WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
    64         return EXIT_FAILURE;
    65     }
    66 
    67     String localization = commandLine["localization"];
    68     RetainPtr<CFStringRef> cfLocalization(AdoptCF, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(localization.characters()), localization.length()));
    69     if (cfLocalization)
    70         WKSetDefaultLocalization(cfLocalization.get());
    71 
    72 #if !SHOW_CRASH_REPORTER
    73     // Installs signal handlers that exit on a crash so that CrashReporter does not show up.
    74     signal(SIGILL, _exit);
    75     signal(SIGFPE, _exit);
    76     signal(SIGBUS, _exit);
    77     signal(SIGSEGV, _exit);
    78 #endif
    79 
    80     @autoreleasepool {
    81         // FIXME: The Network process should not need to use AppKit, but right now, WebCore::RunLoop depends
    82         // on the outer most runloop being an AppKit runloop.
    83         [NSApplication sharedApplication];
    84 
    85         InitializeWebKit2();
    86 
    87         ChildProcessInitializationParameters parameters;
    88         parameters.uiProcessName = commandLine["ui-process-name"];
    89         parameters.clientIdentifier = commandLine["client-identifier"];
    90         parameters.connectionIdentifier = serverPort;
    91 
    92         NetworkProcess::shared().initialize(parameters);
    93     }
    94 
    95     RunLoop::run();
    96 
    97     return 0;
     38    return ChildProcessMain<NetworkProcess, ChildProcessMainDelegate>(commandLine);
    9839}
    9940
  • trunk/Source/WebKit2/PluginProcess/mac/PluginProcessMac.mm

    r138913 r138981  
    282282    // allowing plug-ins to change the mouse cursor at any time.
    283283    WKEnableSettingCursorWhenInBackground();
     284
     285#if defined(__i386__)
     286    NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"AppleMagnifiedMode", nil];
     287    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
     288    [defaults release];
     289#endif
    284290}
    285291
  • trunk/Source/WebKit2/PluginProcess/mac/PluginProcessMainMac.mm

    r138913 r138981  
    2929#if ENABLE(PLUGIN_PROCESS)
    3030
    31 #import "CommandLine.h"
     31#import "ChildProcessMain.h"
    3232#import "EnvironmentUtilities.h"
    3333#import "NetscapePluginModule.h"
    3434#import "PluginProcess.h"
    35 #import "WebKit2Initialize.h"
    36 #import <Foundation/NSUserDefaults.h>
    37 #import <WebCore/RunLoop.h>
    38 #import <WebKitSystemInterface.h>
    39 #import <mach/mach_error.h>
    40 #import <servers/bootstrap.h>
    41 #import <stdio.h>
    42 #import <wtf/RetainPtr.h>
    43 #import <wtf/text/CString.h>
    44 #import <wtf/text/WTFString.h>
    45 
    46 #define SHOW_CRASH_REPORTER 1
    47 
    48 using namespace WebCore;
    4935
    5036namespace WebKit {
    5137
    52 // FIXME: There is much code here that is duplicated in WebProcessMainMac.mm, we should add a shared base class where
    53 // we can put everything.
     38class PluginProcessMainDelegate : public ChildProcessMainDelegate {
     39public:
     40    PluginProcessMainDelegate(const CommandLine& commandLine)
     41        : ChildProcessMainDelegate(commandLine)
     42    {
     43    }
     44
     45    virtual void doPreInitializationWork()
     46    {
     47        // Remove the PluginProcess shim from the DYLD_INSERT_LIBRARIES environment variable so any processes
     48        // spawned by the PluginProcess don't try to insert the shim and crash.
     49        EnvironmentUtilities::stripValuesEndingWithString("DYLD_INSERT_LIBRARIES", "/PluginProcessShim.dylib");
     50
     51        // Check if we're being spawned to write a MIME type preferences file.
     52        String pluginPath = m_commandLine["createPluginMIMETypesPreferences"];
     53        if (!pluginPath.isEmpty()) {
     54            if (!NetscapePluginModule::createPluginMIMETypesPreferences(pluginPath))
     55                exit(EXIT_FAILURE);
     56            exit(EXIT_SUCCESS);
     57        }
     58    }
     59
     60    virtual void doPostRunWork()
     61    {
     62#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
     63        // If we have private temporary and cache directories, clean them up.
     64        if (getenv("DIRHELPER_USER_DIR_SUFFIX")) {
     65            char darwinDirectory[PATH_MAX];
     66            if (confstr(_CS_DARWIN_USER_TEMP_DIR, darwinDirectory, sizeof(darwinDirectory)))
     67                [[NSFileManager defaultManager] removeItemAtPath:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:darwinDirectory length:strlen(darwinDirectory)] error:nil];
     68            if (confstr(_CS_DARWIN_USER_CACHE_DIR, darwinDirectory, sizeof(darwinDirectory)))
     69                [[NSFileManager defaultManager] removeItemAtPath:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:darwinDirectory length:strlen(darwinDirectory)] error:nil];
     70        }
     71#endif
     72    }
     73};
    5474
    5575int PluginProcessMain(const CommandLine& commandLine)
    5676{
    57     // Remove the PluginProcess shim from the DYLD_INSERT_LIBRARIES environment variable so any processes
    58     // spawned by the PluginProcess don't try to insert the shim and crash.
    59     EnvironmentUtilities::stripValuesEndingWithString("DYLD_INSERT_LIBRARIES", "/PluginProcessShim.dylib");
    60 
    61     // Check if we're being spawned to write a MIME type preferences file.
    62     String pluginPath = commandLine["createPluginMIMETypesPreferences"];
    63     if (!pluginPath.isEmpty()) {
    64         InitializeWebKit2();
    65 
    66         if (!NetscapePluginModule::createPluginMIMETypesPreferences(pluginPath))
    67             return EXIT_FAILURE;
    68 
    69         return EXIT_SUCCESS;
    70     }
    71 
    72     String serviceName = commandLine["servicename"];
    73     if (serviceName.isEmpty())
    74         return EXIT_FAILURE;
    75    
    76     // Get the server port.
    77     mach_port_t serverPort;
    78     kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
    79     if (kr) {
    80         WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
    81         return EXIT_FAILURE;
    82     }
    83 
    84     String localization = commandLine["localization"];
    85     if (!localization.isEmpty()) {
    86         RetainPtr<CFStringRef> cfLocalization(AdoptCF, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(localization.characters()), localization.length()));
    87         WKSetDefaultLocalization(cfLocalization.get());
    88     }
    89 
    90 #if defined(__i386__)
    91     {
    92         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    93 
    94         NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"AppleMagnifiedMode", nil];
    95         [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
    96         [defaults release];
    97 
    98         [pool drain];
    99     }
    100 #endif
    101 
    102 #if !SHOW_CRASH_REPORTER
    103     // Installs signal handlers that exit on a crash so that CrashReporter does not show up.
    104     signal(SIGILL, _exit);
    105     signal(SIGFPE, _exit);
    106     signal(SIGBUS, _exit);
    107     signal(SIGSEGV, _exit);
    108 #endif
    109 
    110     @autoreleasepool {
    111         InitializeWebKit2();
    112 
    113         ChildProcessInitializationParameters parameters;
    114         parameters.uiProcessName = commandLine["ui-process-name"];
    115         parameters.clientIdentifier = commandLine["client-identifier"];
    116         parameters.connectionIdentifier = CoreIPC::Connection::Identifier(serverPort);
    117 
    118         PluginProcess::shared().initialize(parameters);
    119 
    120         [NSApplication sharedApplication];
    121     }
    122 
    123     RunLoop::run();
    124    
    125 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
    126     // If we have private temporary and cache directories, clean them up.
    127     if (getenv("DIRHELPER_USER_DIR_SUFFIX")) {
    128         char darwinDirectory[PATH_MAX];
    129         if (confstr(_CS_DARWIN_USER_TEMP_DIR, darwinDirectory, sizeof(darwinDirectory)))
    130             [[NSFileManager defaultManager] removeItemAtPath:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:darwinDirectory length:strlen(darwinDirectory)] error:nil];
    131         if (confstr(_CS_DARWIN_USER_CACHE_DIR, darwinDirectory, sizeof(darwinDirectory)))
    132             [[NSFileManager defaultManager] removeItemAtPath:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:darwinDirectory length:strlen(darwinDirectory)] error:nil];
    133     }
    134 #endif
    135 
    136     return 0;
     77    return ChildProcessMain<PluginProcess, PluginProcessMainDelegate>(commandLine);
    13778}
    13879
  • trunk/Source/WebKit2/Shared/mac/ChildProcessMain.mm

    r138980 r138981  
    11/*
    2  * Copyright (C) 2012 Apple Inc. All rights reserved.
     2 * Copyright (C) 2013 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2424 */
    2525
    26 #import "config.h"
    27 #import "NetworkProcessMain.h"
     26#include "config.h"
     27#include "ChildProcessMain.h"
    2828
    29 #if ENABLE(NETWORK_PROCESS)
    30 
    31 #import "CommandLine.h"
    32 #import "NetworkProcess.h"
    33 #import "WebKit2Initialize.h"
    34 #import <WebCore/RunLoop.h>
    35 #import <WebKitSystemInterface.h>
    3629#import <mach/mach_error.h>
    3730#import <servers/bootstrap.h>
    3831#import <stdio.h>
    39 #import <wtf/MainThread.h>
    4032#import <wtf/RetainPtr.h>
    4133#import <wtf/text/CString.h>
    42 #import <wtf/text/WTFString.h>
    4334
    4435#define SHOW_CRASH_REPORTER 1
    4536
    46 using namespace WebCore;
    47 
    4837namespace WebKit {
    4938
    50 // FIXME: There is much code here that is duplicated in WebProcessMainMac.mm, we should add a shared base class where
    51 // we can put everything.
     39ChildProcessMainDelegate::~ChildProcessMainDelegate()
     40{
     41}
    5242
    53 int NetworkProcessMain(const CommandLine& commandLine)
     43void ChildProcessMainDelegate::installSignalHandlers()
    5444{
    55     String serviceName = commandLine["servicename"];
    56     if (serviceName.isEmpty())
    57         return EXIT_FAILURE;
    58    
    59     // Get the server port.
    60     mach_port_t serverPort;
    61     kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
    62     if (kr) {
    63         WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
    64         return EXIT_FAILURE;
    65     }
    66 
    67     String localization = commandLine["localization"];
    68     RetainPtr<CFStringRef> cfLocalization(AdoptCF, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(localization.characters()), localization.length()));
    69     if (cfLocalization)
    70         WKSetDefaultLocalization(cfLocalization.get());
    71 
    7245#if !SHOW_CRASH_REPORTER
    7346    // Installs signal handlers that exit on a crash so that CrashReporter does not show up.
     
    7750    signal(SIGSEGV, _exit);
    7851#endif
     52}
    7953
    80     @autoreleasepool {
    81         // FIXME: The Network process should not need to use AppKit, but right now, WebCore::RunLoop depends
    82         // on the outer most runloop being an AppKit runloop.
    83         [NSApplication sharedApplication];
     54void ChildProcessMainDelegate::doPreInitializationWork()
     55{
     56}
    8457
    85         InitializeWebKit2();
    86 
    87         ChildProcessInitializationParameters parameters;
    88         parameters.uiProcessName = commandLine["ui-process-name"];
    89         parameters.clientIdentifier = commandLine["client-identifier"];
    90         parameters.connectionIdentifier = serverPort;
    91 
    92         NetworkProcess::shared().initialize(parameters);
     58bool ChildProcessMainDelegate::getConnectionIdentifier(CoreIPC::Connection::Identifier& identifier)
     59{
     60    String serviceName = m_commandLine["servicename"];
     61    if (serviceName.isEmpty())
     62        return false;
     63   
     64    mach_port_t serverPort;
     65    kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
     66    if (kr) {
     67        WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
     68        return false;
    9369    }
    9470
    95     RunLoop::run();
     71    identifier = serverPort;
     72    return true;
     73}
    9674
    97     return 0;
     75bool ChildProcessMainDelegate::getClientIdentifier(String& clientIdentifier)
     76{
     77    clientIdentifier = m_commandLine["client-identifier"];
     78    if (clientIdentifier.isEmpty())
     79        return false;
     80    return true;
     81}
     82
     83bool ChildProcessMainDelegate::getClientProcessName(String& clientProcessName)
     84{
     85    clientProcessName = m_commandLine["ui-process-name"];
     86    if (clientProcessName.isEmpty())
     87        return false;
     88    return true;
     89}
     90
     91void ChildProcessMainDelegate::doPostRunWork()
     92{
    9893}
    9994
    10095} // namespace WebKit
    101 
    102 #endif // ENABLE(NETWORK_PROCESS)
  • trunk/Source/WebKit2/SharedWorkerProcess/mac/SharedWorkerProcessMainMac.mm

    r138913 r138981  
    2929#if ENABLE(SHARED_WORKER_PROCESS)
    3030
    31 #import "CommandLine.h"
    32 #import "EnvironmentUtilities.h"
     31#import "ChildProcessMain.h"
    3332#import "SharedWorkerProcess.h"
    34 #import "WebKit2Initialize.h"
    35 #import <Foundation/NSUserDefaults.h>
    36 #import <WebCore/RunLoop.h>
    37 #import <WebKitSystemInterface.h>
    38 #import <mach/mach_error.h>
    39 #import <servers/bootstrap.h>
    40 #import <stdio.h>
    41 #import <wtf/RetainPtr.h>
    42 #import <wtf/text/CString.h>
    43 #import <wtf/text/WTFString.h>
    44 
    45 #define SHOW_CRASH_REPORTER 1
    46 
    47 using namespace WebCore;
    4833
    4934namespace WebKit {
    5035
    51 // FIXME: There is much code here that is duplicated in WebProcessMainMac.mm, we should add a shared base class where
    52 // we can put everything.
    53 
    5436int SharedWorkerProcessMain(const CommandLine& commandLine)
    5537{
    56     String serviceName = commandLine["servicename"];
    57     if (serviceName.isEmpty())
    58         return EXIT_FAILURE;
    59    
    60     // Get the server port.
    61     mach_port_t serverPort;
    62     kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
    63     if (kr) {
    64         WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
    65         return EXIT_FAILURE;
    66     }
    67 
    68     String localization = commandLine["localization"];
    69     if (!localization.isEmpty()) {
    70         RetainPtr<CFStringRef> cfLocalization(AdoptCF, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(localization.characters()), localization.length()));
    71         WKSetDefaultLocalization(cfLocalization.get());
    72     }
    73 
    74 #if defined(__i386__)
    75     {
    76         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    77 
    78         NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"AppleMagnifiedMode", nil];
    79         [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
    80         [defaults release];
    81 
    82         [pool drain];
    83     }
    84 #endif
    85 
    86 #if !SHOW_CRASH_REPORTER
    87     // Installs signal handlers that exit on a crash so that CrashReporter does not show up.
    88     signal(SIGILL, _exit);
    89     signal(SIGFPE, _exit);
    90     signal(SIGBUS, _exit);
    91     signal(SIGSEGV, _exit);
    92 #endif
    93 
    94     @autoreleasepool {
    95         InitializeWebKit2();
    96 
    97         ChildProcessInitializationParameters parameters;
    98         parameters.uiProcessName = commandLine["ui-process-name"];
    99         parameters.clientIdentifier = commandLine["client-identifier"];
    100         parameters.connectionIdentifier = CoreIPC::Connection::Identifier(serverPort);
    101 
    102         SharedWorkerProcess::shared().initialize(parameters);
    103 
    104         [NSApplication sharedApplication];
    105     }
    106 
    107     RunLoop::run();
    108    
    109     return 0;
     38    return ChildProcessMain<SharedWorkerProcess, ChildProcessMainDelegate>(commandLine);
    11039}
    11140
    112 }
     41} // namespace WebKit
    11342
    11443#endif // ENABLE(SHARED_WORKER_PROCESS)
  • trunk/Source/WebKit2/UIProcess/Launcher/mac/EnvironmentVariables.cpp

    r136620 r138981  
    147147}
    148148
     149void EnvironmentVariables::dump()
     150{
     151    for (size_t i = 0; (*_NSGetEnviron())[i]; i++)
     152        printf("%s\n", (*_NSGetEnviron())[i]);
     153
     154    printf("\n\n\n");
     155}
     156
    149157} // namespace WebKit
  • trunk/Source/WebKit2/UIProcess/Launcher/mac/EnvironmentVariables.h

    r136620 r138981  
    5050    static const char* preexistingProcessTypeKey();
    5151
     52    static void dump();
     53
    5254private:
    5355    const char* valueIfVariableHasName(const char* environmentVariable, const char* name) const;
  • trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj

    r138913 r138981  
    831831                BC9BA5041697C45300E44616 /* WebKit2Initialize.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BC9BA5021697C45300E44616 /* WebKit2Initialize.cpp */; };
    832832                BC9BA5051697C45300E44616 /* WebKit2Initialize.h in Headers */ = {isa = PBXBuildFile; fileRef = BC9BA5031697C45300E44616 /* WebKit2Initialize.h */; };
     833                BC9BA50816991C3C00E44616 /* ChildProcessMain.mm in Sources */ = {isa = PBXBuildFile; fileRef = BC9BA50616991C3C00E44616 /* ChildProcessMain.mm */; };
     834                BC9BA50916991C3C00E44616 /* ChildProcessMain.h in Headers */ = {isa = PBXBuildFile; fileRef = BC9BA50716991C3C00E44616 /* ChildProcessMain.h */; };
    833835                BC9FA520160D3B430054DF9A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC3DE46815A91763008D26FC /* Foundation.framework */; };
    834836                BC9FA522160D4A0C0054DF9A /* com.apple.WebKit.PluginProcess.sb in Resources */ = {isa = PBXBuildFile; fileRef = E1967E35150AB5D500C73169 /* com.apple.WebKit.PluginProcess.sb */; };
     
    21032105                BC9BA5021697C45300E44616 /* WebKit2Initialize.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebKit2Initialize.cpp; sourceTree = "<group>"; };
    21042106                BC9BA5031697C45300E44616 /* WebKit2Initialize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebKit2Initialize.h; sourceTree = "<group>"; };
     2107                BC9BA50616991C3C00E44616 /* ChildProcessMain.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ChildProcessMain.mm; sourceTree = "<group>"; };
     2108                BC9BA50716991C3C00E44616 /* ChildProcessMain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ChildProcessMain.h; sourceTree = "<group>"; };
    21052109                BCA0EF7D12331E78007D3CFB /* WebUndoStep.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebUndoStep.h; sourceTree = "<group>"; };
    21062110                BCA0EF7E12331E78007D3CFB /* WebUndoStep.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebUndoStep.cpp; sourceTree = "<group>"; };
     
    38383842                                BCE231C0122C466E00D5C35A /* WebURLRequestMac.mm */,
    38393843                                BC90A1D5122DD66A00CC8C50 /* WebURLResponseMac.mm */,
     3844                                BC9BA50616991C3C00E44616 /* ChildProcessMain.mm */,
     3845                                BC9BA50716991C3C00E44616 /* ChildProcessMain.h */,
    38403846                        );
    38413847                        path = mac;
     
    50335039                                BCF4DE25168FA44800C94AFC /* WebContextSupplement.h in Headers */,
    50345040                                BC9BA5051697C45300E44616 /* WebKit2Initialize.h in Headers */,
     5041                                BC9BA50916991C3C00E44616 /* ChildProcessMain.h in Headers */,
    50355042                        );
    50365043                        runOnlyForDeploymentPostprocessing = 0;
     
    60076014                                31A505F91680025500A930EB /* WebContextClient.cpp in Sources */,
    60086015                                BC9BA5041697C45300E44616 /* WebKit2Initialize.cpp in Sources */,
     6016                                BC9BA50816991C3C00E44616 /* ChildProcessMain.mm in Sources */,
    60096017                        );
    60106018                        runOnlyForDeploymentPostprocessing = 0;
  • trunk/Source/WebKit2/WebProcess/mac/WebProcessMainMac.mm

    r138913 r138981  
    2828
    2929#import "CommandLine.h"
     30#import "ChildProcessMain.h"
    3031#import "EnvironmentUtilities.h"
    3132#import "EnvironmentVariables.h"
    3233#import "StringUtilities.h"
    33 #import "WebKit2Initialize.h"
    3434#import "WebProcess.h"
    35 #import <WebCore/RunLoop.h>
    36 #import <WebKitSystemInterface.h>
    3735#import <mach/mach_error.h>
    38 #import <objc/objc-auto.h>
    3936#import <servers/bootstrap.h>
    40 #import <signal.h>
    4137#import <spawn.h>
    4238#import <stdio.h>
    43 #import <sysexits.h>
    44 #import <unistd.h>
    4539#import <wtf/RetainPtr.h>
    4640#import <wtf/text/CString.h>
     
    4943extern "C" kern_return_t bootstrap_register2(mach_port_t, name_t, mach_port_t, uint64_t);
    5044
    51 #if USE(APPKIT)
    52 @interface NSApplication (WebNSApplicationDetails)
    53 -(void)_installAutoreleasePoolsOnCurrentThreadIfNecessary;
    54 @end
    55 #endif
     45namespace WebKit {
    5646
    57 #define SHOW_CRASH_REPORTER 1
     47class WebProcessMainDelegate : public ChildProcessMainDelegate {
     48public:
     49    WebProcessMainDelegate(const CommandLine& commandLine)
     50        : ChildProcessMainDelegate(commandLine)
     51    {
     52    }
    5853
    59 using namespace WebCore;
     54    virtual void doPreInitializationWork()
     55    {
     56        // Remove the WebProcess shim from the DYLD_INSERT_LIBRARIES environment variable so any processes spawned by
     57        // the WebProcess don't try to insert the shim and crash.
     58        EnvironmentUtilities::stripValuesEndingWithString("DYLD_INSERT_LIBRARIES", "/WebProcessShim.dylib");
     59    }
    6060
    61 namespace WebKit {
     61    virtual bool getConnectionIdentifier(CoreIPC::Connection::Identifier& identifier)
     62    {
     63        String serviceName = m_commandLine["servicename"];
     64        String clientExecutable = m_commandLine["client-executable"];
     65        if (serviceName.isEmpty() && clientExecutable.isEmpty())
     66            return false;
     67
     68        mach_port_t serverPort;
     69        if (clientExecutable.isEmpty()) {
     70            kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
     71            if (kr) {
     72                WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
     73                return false;
     74            }
     75        } else {
     76            mach_port_name_t publishedService;
     77            mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &publishedService);
     78            mach_port_insert_right(mach_task_self(), publishedService, publishedService, MACH_MSG_TYPE_MAKE_SEND);
     79            // Make it possible to look up.
     80            serviceName = String::format("com.apple.WebKit.WebProcess-%d", getpid());
     81            if (kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.utf8().data()), publishedService, 0)) {
     82                WTFLogAlways("Failed to register service name \"%s\". %s (%x)\n", serviceName.utf8().data(), mach_error_string(kr), kr);
     83                return false;
     84            }
     85
     86            CString command = clientExecutable.utf8();
     87            const char* args[] = { command.data(), 0 };
     88
     89            EnvironmentVariables environmentVariables;
     90            environmentVariables.set(EnvironmentVariables::preexistingProcessServiceNameKey(), serviceName.utf8().data());
     91            environmentVariables.set(EnvironmentVariables::preexistingProcessTypeKey(), m_commandLine["type"].utf8().data());
     92
     93            posix_spawn_file_actions_t fileActions;
     94            posix_spawn_file_actions_init(&fileActions);
     95            posix_spawn_file_actions_addinherit_np(&fileActions, STDIN_FILENO);
     96            posix_spawn_file_actions_addinherit_np(&fileActions, STDOUT_FILENO);
     97            posix_spawn_file_actions_addinherit_np(&fileActions, STDERR_FILENO);
     98
     99            posix_spawnattr_t attributes;
     100            posix_spawnattr_init(&attributes);
     101            posix_spawnattr_setflags(&attributes, POSIX_SPAWN_CLOEXEC_DEFAULT | POSIX_SPAWN_SETPGROUP);
     102
     103            int spawnResult = posix_spawn(0, command.data(), &fileActions, &attributes, const_cast<char**>(args), environmentVariables.environmentPointer());
     104
     105            posix_spawnattr_destroy(&attributes);
     106            posix_spawn_file_actions_destroy(&fileActions);
     107
     108            if (spawnResult)
     109                return false;
     110
     111            mach_msg_empty_rcv_t message;
     112            if (kern_return_t kr = mach_msg(&message.header, MACH_RCV_MSG, 0, sizeof(message), publishedService, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)) {
     113                WTFLogAlways("Failed to receive port from the UI process. %s (%x)\n", mach_error_string(kr), kr);
     114                return false;
     115            }
     116
     117            mach_port_mod_refs(mach_task_self(), publishedService, MACH_PORT_RIGHT_RECEIVE, -1);
     118            serverPort = message.header.msgh_remote_port;
     119            mach_port_type_t portType;
     120            kern_return_t kr = mach_port_type(mach_task_self(), serverPort, &portType);
     121            if (kr || !(portType & MACH_PORT_TYPE_SEND)) {
     122                WTFLogAlways("Failed to obtain send right for port received from the UI process.\n");
     123                return false;
     124            }
     125        }
     126
     127        identifier = serverPort;
     128        return true;
     129    }
     130
     131    virtual bool getClientIdentifier(String& clientIdentifier)
     132    {
     133        String clientExecutable = m_commandLine["client-executable"];
     134
     135        if (clientExecutable.isEmpty())
     136            clientIdentifier = m_commandLine["client-identifier"];
     137        else {
     138            RetainPtr<NSURL> clientExecutableURL = adoptNS([[NSURL alloc] initFileURLWithPath:nsStringFromWebCoreString(clientExecutable)]);
     139            RetainPtr<CFURLRef> clientBundleURL = adoptCF(WKCopyBundleURLForExecutableURL((CFURLRef)clientExecutableURL.get()));
     140            RetainPtr<NSBundle> clientBundle = adoptNS([[NSBundle alloc] initWithURL:(NSURL *)clientBundleURL.get()]);
     141            clientIdentifier = [clientBundle.get() bundleIdentifier];
     142        }
     143
     144        if (clientIdentifier.isEmpty())
     145            return false;
     146        return true;
     147    }
     148};
    62149
    63150int WebProcessMain(const CommandLine& commandLine)
    64151{
    65     // Remove the WebProcess shim from the DYLD_INSERT_LIBRARIES environment variable so any processes spawned by
    66     // the WebProcess don't try to insert the shim and crash.
    67     EnvironmentUtilities::stripValuesEndingWithString("DYLD_INSERT_LIBRARIES", "/WebProcessShim.dylib");
    68 
    69     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    70 
    71     String serviceName = commandLine["servicename"];
    72     String clientExecutable;
    73     clientExecutable = commandLine["client-executable"];
    74 
    75     if (serviceName.isEmpty() && clientExecutable.isEmpty())
    76         return EXIT_FAILURE;
    77 
    78     String clientIdentifier;
    79 
    80     // Get the server port.
    81     mach_port_t serverPort;
    82     if (clientExecutable.isEmpty()) {
    83         kern_return_t kr = bootstrap_look_up(bootstrap_port, serviceName.utf8().data(), &serverPort);
    84         if (kr) {
    85             WTFLogAlways("bootstrap_look_up result: %s (%x)\n", mach_error_string(kr), kr);
    86             return 2;
    87         }
    88        
    89         clientIdentifier = commandLine["client-identifier"];
    90         if (!clientIdentifier) {
    91             WTFLogAlways("No client identifier passed to the WebProcess");
    92             return EXIT_FAILURE;
    93         }
    94     } else {
    95         mach_port_name_t publishedService;
    96         mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &publishedService);
    97         mach_port_insert_right(mach_task_self(), publishedService, publishedService, MACH_MSG_TYPE_MAKE_SEND);
    98         // Make it possible to look up.
    99         serviceName = String::format("com.apple.WebKit.WebProcess-%d", getpid());
    100         if (kern_return_t kr = bootstrap_register2(bootstrap_port, const_cast<char*>(serviceName.utf8().data()), publishedService, 0)) {
    101             WTFLogAlways("Failed to register service name \"%s\". %s (%x)\n", serviceName.utf8().data(), mach_error_string(kr), kr);
    102             return EXIT_FAILURE;
    103         }
    104 
    105         CString command = clientExecutable.utf8();
    106         const char* args[] = { command.data(), 0 };
    107 
    108         EnvironmentVariables environmentVariables;
    109         environmentVariables.set(EnvironmentVariables::preexistingProcessServiceNameKey(), serviceName.utf8().data());
    110         environmentVariables.set(EnvironmentVariables::preexistingProcessTypeKey(), commandLine["type"].utf8().data());
    111 
    112         posix_spawn_file_actions_t fileActions;
    113         posix_spawn_file_actions_init(&fileActions);
    114         posix_spawn_file_actions_addinherit_np(&fileActions, STDIN_FILENO);
    115         posix_spawn_file_actions_addinherit_np(&fileActions, STDOUT_FILENO);
    116         posix_spawn_file_actions_addinherit_np(&fileActions, STDERR_FILENO);
    117 
    118         posix_spawnattr_t attributes;
    119         posix_spawnattr_init(&attributes);
    120         posix_spawnattr_setflags(&attributes, POSIX_SPAWN_CLOEXEC_DEFAULT | POSIX_SPAWN_SETPGROUP);
    121 
    122         int spawnResult = posix_spawn(0, command.data(), &fileActions, &attributes, const_cast<char**>(args), environmentVariables.environmentPointer());
    123 
    124         posix_spawnattr_destroy(&attributes);
    125         posix_spawn_file_actions_destroy(&fileActions);
    126 
    127         if (spawnResult)
    128             return 2;
    129 
    130         mach_msg_empty_rcv_t message;
    131         if (kern_return_t kr = mach_msg(&message.header, MACH_RCV_MSG, 0, sizeof(message), publishedService, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)) {
    132             WTFLogAlways("Failed to receive port from the UI process. %s (%x)\n", mach_error_string(kr), kr);
    133             return EXIT_FAILURE;
    134         }
    135 
    136         mach_port_mod_refs(mach_task_self(), publishedService, MACH_PORT_RIGHT_RECEIVE, -1);
    137         serverPort = message.header.msgh_remote_port;
    138         mach_port_type_t portType;
    139         kern_return_t kr = mach_port_type(mach_task_self(), serverPort, &portType);
    140         if (kr || !(portType & MACH_PORT_TYPE_SEND)) {
    141             WTFLogAlways("Failed to obtain send right for port received from the UI process.\n");
    142             return EXIT_FAILURE;
    143         }
    144 
    145         RetainPtr<NSURL> clientExecutableURL = adoptNS([[NSURL alloc] initFileURLWithPath:nsStringFromWebCoreString(clientExecutable)]);
    146         RetainPtr<CFURLRef> clientBundleURL = adoptCF(WKCopyBundleURLForExecutableURL((CFURLRef)clientExecutableURL.get()));
    147         RetainPtr<NSBundle> clientBundle = adoptNS([[NSBundle alloc] initWithURL:(NSURL *)clientBundleURL.get()]);
    148        
    149         clientIdentifier = [clientBundle.get() bundleIdentifier];
    150         if (!clientIdentifier) {
    151             WTFLogAlways("Failed to obtain bundle identifier from the client executable. .\n");
    152             return EXIT_FAILURE;
    153         }
    154     }
    155 
    156     String localization = commandLine["localization"];
    157     if (!localization.isEmpty()) {
    158         RetainPtr<CFStringRef> cfLocalization(AdoptCF, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(localization.characters()), localization.length()));
    159         WKSetDefaultLocalization(cfLocalization.get());
    160     }
    161 
    162     [pool drain];
    163 
    164 #if !SHOW_CRASH_REPORTER
    165     // Installs signal handlers that exit on a crash so that CrashReporter does not show up.
    166     signal(SIGILL, _exit);
    167     signal(SIGFPE, _exit);
    168     signal(SIGBUS, _exit);
    169     signal(SIGSEGV, _exit);
    170 #endif
    171 
    172 #if USE(APPKIT)
    173      // Initialize AppKit.
    174     [NSApplication sharedApplication];
    175 
    176     // Installs autorelease pools on the current CFRunLoop which prevents memory from accumulating between user events.
    177     // FIXME: Remove when <rdar://problem/8929426> is fixed.
    178     [[NSApplication sharedApplication] _installAutoreleasePoolsOnCurrentThreadIfNecessary];
    179 #endif
    180 
    181     @autoreleasepool {
    182         InitializeWebKit2();
    183 
    184         ChildProcessInitializationParameters parameters;
    185         parameters.uiProcessName = commandLine["ui-process-name"];
    186         parameters.clientIdentifier = clientIdentifier;
    187         parameters.connectionIdentifier = serverPort;
    188 
    189         WebProcess::shared().initialize(parameters);
    190     }
    191 
    192     RunLoop::run();
    193 
    194     // FIXME: Do more cleanup here.
    195 
    196     return 0;
     152    return ChildProcessMain<WebProcess, WebProcessMainDelegate>(commandLine);
    197153}
    198154
Note: See TracChangeset for help on using the changeset viewer.