1/*
2 * Copyright (C) 2010, 2011 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#import "config.h"
27#import "WebProcessMain.h"
28
29#import "CommandLine.h"
30#import "RunLoop.h"
31#import "WebProcess.h"
32#import "WebSystemInterface.h"
33#import <WebKit2/WKView.h>
34#import <WebKitSystemInterface.h>
35#import <objc/objc-auto.h>
36#import <runtime/InitializeThreading.h>
37#import <servers/bootstrap.h>
38#import <signal.h>
39#import <stdio.h>
40#import <sysexits.h>
41#import <unistd.h>
42#import <wtf/RetainPtr.h>
43#import <wtf/Threading.h>
44#import <wtf/text/CString.h>
45
46// FIXME: We should be doing this another way.
47extern "C" kern_return_t bootstrap_look_up2(mach_port_t, const name_t, mach_port_t*, pid_t, uint64_t);
48
49@interface NSApplication (WebNSApplicationDetails)
50-(void)_installAutoreleasePoolsOnCurrentThreadIfNecessary;
51@end
52
53#define SHOW_CRASH_REPORTER 1
54
55using namespace WebCore;
56
57namespace WebKit {
58
59int WebProcessMain(const CommandLine& commandLine)
60{
61    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
62
63    String serviceName = commandLine["servicename"];
64    if (serviceName.isEmpty())
65        return EXIT_FAILURE;
66
67    // Get the server port.
68    mach_port_t serverPort;
69    kern_return_t kr = bootstrap_look_up2(bootstrap_port, serviceName.utf8().data(), &serverPort, 0, 0);
70    if (kr) {
71        printf("bootstrap_look_up2 result: %x", kr);
72        return 2;
73    }
74
75    String localization = commandLine["localization"];
76    RetainPtr<CFStringRef> cfLocalization(AdoptCF, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(localization.characters()), localization.length()));
77    if (cfLocalization)
78        WKSetDefaultLocalization(cfLocalization.get());
79
80#if !SHOW_CRASH_REPORTER
81    // Installs signal handlers that exit on a crash so that CrashReporter does not show up.
82    signal(SIGILL, _exit);
83    signal(SIGFPE, _exit);
84    signal(SIGBUS, _exit);
85    signal(SIGSEGV, _exit);
86#endif
87
88    InitWebCoreSystemInterface();
89    JSC::initializeThreading();
90    WTF::initializeMainThread();
91    RunLoop::initializeMainRunLoop();
92
93    // Create the connection.
94    WebProcess::shared().initialize(serverPort, RunLoop::main());
95
96    [pool drain];
97
98     // Initialize AppKit.
99    [NSApplication sharedApplication];
100
101    // Installs autorelease pools on the current CFRunLoop which prevents memory from accumulating between user events.
102    // FIXME: Remove when <rdar://problem/8929426> is fixed.
103    [[NSApplication sharedApplication] _installAutoreleasePoolsOnCurrentThreadIfNecessary];
104
105#if !defined(BUILDING_ON_SNOW_LEOPARD)
106    WKAXRegisterRemoteApp();
107#endif
108
109    RunLoop::run();
110
111    // FIXME: Do more cleanup here.
112
113    return 0;
114}
115
116} // namespace WebKit
117
118