summaryrefslogtreecommitdiff
path: root/Source/WebKit2/WebProcess/EntryPoint/mac/LegacyProcess/WebContentProcessMain.mm
blob: fe7840d9e0a8194b615b9fc21b17530ed023d1cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (C) 2010, 2011, 2012 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "config.h"

#import "ChildProcessEntryPoint.h"
#import "EnvironmentUtilities.h"
#import "EnvironmentVariables.h"
#import "StringUtilities.h"
#import "WKBase.h"
#import "WebProcess.h"
#import <WebCore/RunLoop.h>
#import <mach/mach_error.h>
#import <servers/bootstrap.h>
#import <spawn.h>
#import <stdio.h>
#import <wtf/RetainPtr.h>
#import <wtf/text/CString.h>
#import <wtf/text/WTFString.h>

#if USE(APPKIT)
@interface NSApplication (WebNSApplicationDetails)
-(void)_installAutoreleasePoolsOnCurrentThreadIfNecessary;
@end
#endif

extern "C" kern_return_t bootstrap_register2(mach_port_t, name_t, mach_port_t, uint64_t);

using namespace WebCore;

namespace WebKit {

class WebContentProcessMainDelegate : public ChildProcessMainDelegate {
public:
    WebContentProcessMainDelegate(const CommandLine& commandLine)
        : ChildProcessMainDelegate(commandLine)
    {
    }

    virtual void doPreInitializationWork()
    {
        // Remove the WebProcess shim from the DYLD_INSERT_LIBRARIES environment variable so any processes spawned by
        // the WebProcess don't try to insert the shim and crash.
        EnvironmentUtilities::stripValuesEndingWithString("DYLD_INSERT_LIBRARIES", "/WebProcessShim.dylib");
    
#if USE(APPKIT)
        RunLoop::setUseApplicationRunLoopOnMainRunLoop();

        // Initialize AppKit.
        [NSApplication sharedApplication];

        // Installs autorelease pools on the current runloop which prevents memory from accumulating between user events.
        // FIXME: Remove when <rdar://problem/8929426> is fixed.
        [NSApp _installAutoreleasePoolsOnCurrentThreadIfNecessary];
#endif
    }

    virtual bool getConnectionIdentifier(CoreIPC::Connection::Identifier& identifier)
    {
        String clientExecutable = m_commandLine["client-executable"];
        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_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);
        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;
        return true;
    }

    virtual bool getClientIdentifier(String& clientIdentifier)
    {
        String clientExecutable = m_commandLine["client-executable"];
        if (clientExecutable.isEmpty())
            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;
    }
};

} // namespace WebKit

using namespace WebKit;

extern "C" WK_EXPORT int WebContentProcessMain(int argc, char** argv);

int WebContentProcessMain(int argc, char** argv)
{
    return ChildProcessMain<WebProcess, WebContentProcessMainDelegate>(argc, argv);
}