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