forked from glandium/firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOSBootstrap.mm
203 lines (168 loc) · 6.47 KB
/
IOSBootstrap.mm
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* clang-format off */
/* -*- Mode: Objective-C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* clang-format on */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GeckoView/IOSBootstrap.h"
#include "GeckoView/GeckoViewSwiftSupport.h"
#include "mozilla/Bootstrap.h"
#include "mozilla/DarwinObjectPtr.h"
#include "mozilla/GeckoArgs.h"
#include "mozilla/widget/GeckoViewSupport.h"
#include "nsDebug.h"
#include "nsPrintfCString.h"
#include "XREChildData.h"
#include "application.ini.h"
static id<SwiftGeckoViewRuntime> gRuntime;
id<SwiftGeckoViewRuntime> mozilla::widget::GetSwiftRuntime() {
return gRuntime;
}
static id<GeckoProcessExtension> gCurrentProcessExtension;
id<GeckoProcessExtension> mozilla::widget::GetCurrentProcessExtension() {
return gCurrentProcessExtension;
}
int MainProcessInit(int aArgc, char** aArgv,
id<SwiftGeckoViewRuntime> aRuntime) {
auto bootstrap = mozilla::GetBootstrap();
if (bootstrap.isErr()) {
printf_stderr("Couldn't load XPCOM.\n");
return 255;
}
gRuntime = [aRuntime retain];
mozilla::BootstrapConfig config;
config.appData = &sAppData;
config.appDataPath = nullptr;
return bootstrap.inspect()->XRE_main(aArgc, aArgv, config);
}
static void HandleBootstrapMessage(xpc_object_t aEvent);
void ChildProcessInit(xpc_connection_t aXpcConnection,
id<GeckoProcessExtension> aProcess,
id<SwiftGeckoViewRuntime> aRuntime) {
gCurrentProcessExtension = aProcess;
gRuntime = [aRuntime retain];
static std::atomic<bool> geckoViewStarted = false;
xpc_connection_set_event_handler(aXpcConnection, [](xpc_object_t aEvent) {
xpc_type_t type = xpc_get_type(aEvent);
if (type != XPC_TYPE_DICTIONARY) {
NSLog(@"[%d] Received unexpected XPC event type: %s\n", getpid(),
xpc_type_get_name(type));
if (!geckoViewStarted && type == XPC_TYPE_ERROR &&
(aEvent == XPC_ERROR_CONNECTION_INVALID ||
aEvent == XPC_ERROR_TERMINATION_IMMINENT)) {
// FIXME: handle this more gracefully?
MOZ_CRASH("Received XPC error event before bootstrap event");
}
return;
}
const char* messageName = xpc_dictionary_get_string(aEvent, "message-name");
if (!messageName) {
NSLog(@"[%d] No message name specified in XPC message", getpid());
return;
}
if (!strcmp(messageName, "bootstrap")) {
HandleBootstrapMessage(aEvent);
// Errors on the XPC channel no longer indicate we should shut down.
geckoViewStarted = true;
} else {
NS_WARNING(nsPrintfCString("Unknown XPC message: %s", messageName).get());
}
});
xpc_connection_activate(aXpcConnection);
}
static int ChildProcessInitImpl(int aArgc, char** aArgv) {
auto bootstrap = mozilla::GetBootstrap();
if (bootstrap.isErr()) {
printf_stderr("Couldn't load XPCOM.\n");
return 255;
}
// Check for the absolute minimum number of args we need to move
// forward here. We expect the last arg to be the child process type,
// and the second-last argument to be the gecko child id.
if (aArgc < 2) {
return 3;
}
// Set the process type. We don't remove the arg here as that will be
// done later in common code.
mozilla::SetGeckoProcessType(aArgv[aArgc - 1]);
XREChildData childData;
mozilla::SetGeckoChildID(aArgv[aArgc - 2]);
nsresult rv =
bootstrap.inspect()->XRE_InitChildProcess(aArgc - 2, aArgv, &childData);
return NS_FAILED(rv);
}
void HandleBootstrapMessage(xpc_object_t aEvent) {
// Set up stdout and stderr if they were provided.
int fd = xpc_dictionary_dup_fd(aEvent, "stdout");
if (fd != -1) {
MOZ_ASSERT(fd != STDOUT_FILENO);
dup2(fd, STDOUT_FILENO);
close(fd);
}
fd = xpc_dictionary_dup_fd(aEvent, "stderr");
if (fd != -1) {
MOZ_ASSERT(fd != STDERR_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
}
// Immediately send a reply with our pid and mach task port.
auto reply = mozilla::AdoptDarwinObject(xpc_dictionary_create_reply(aEvent));
xpc_dictionary_set_int64(reply.get(), "pid", getpid());
xpc_dictionary_set_mach_send(reply.get(), "task", mach_task_self());
xpc_connection_send_message(xpc_dictionary_get_remote_connection(aEvent),
reply.get());
// Load any environment variable overrides set by the parent process.
xpc_object_t newEnviron = xpc_dictionary_get_dictionary(aEvent, "environ");
xpc_dictionary_apply(newEnviron, [](const char* key, xpc_object_t value) {
setenv(key, xpc_string_get_string_ptr(value), 1);
return true;
});
xpc_object_t fds = xpc_dictionary_get_array(aEvent, "fds");
if (!fds) {
MOZ_CRASH("fds array not specified");
return;
}
size_t num_fds = xpc_array_get_count(fds);
std::vector<mozilla::UniqueFileHandle> files;
files.reserve(num_fds);
for (size_t i = 0; i < num_fds; ++i) {
files.emplace_back(xpc_array_dup_fd(fds, i));
}
mozilla::geckoargs::SetPassedFileHandles(std::move(files));
xpc_object_t sendRightsArray = xpc_dictionary_get_array(aEvent, "sendRights");
if (!sendRightsArray) {
MOZ_CRASH("sendRights array not specified");
return;
}
size_t num_rights = xpc_array_get_count(sendRightsArray);
std::vector<mozilla::UniqueMachSendRight> sendRights;
sendRights.reserve(num_rights);
for (size_t i = 0; i < num_rights; ++i) {
// NOTE: As iOS doesn't expose an xpc_array_set_mach_send method, the
// port is wrapped with a single-key dictionary.
xpc_object_t sendRightWrapper =
xpc_array_get_dictionary(sendRightsArray, i);
if (!sendRightWrapper) {
MOZ_CRASH("invalid sendRights array");
continue;
}
sendRights.emplace_back(
xpc_dictionary_copy_mach_send(sendRightWrapper, "port"));
}
mozilla::geckoargs::SetPassedMachSendRights(std::move(sendRights));
// Populate a new argv array with our argument list from IPC.
xpc_object_t args = xpc_dictionary_get_array(aEvent, "argv");
if (!args) {
MOZ_CRASH("argv array not specified");
return;
}
int argc = static_cast<int>(xpc_array_get_count(args));
char** argv = new char*[argc + 1];
for (int i = 0; i < argc; ++i) {
argv[i] = strdup(xpc_array_get_string(args, i));
}
argv[argc] = nullptr;
dispatch_async(dispatch_get_main_queue(),
[argc, argv] { _exit(ChildProcessInitImpl(argc, argv)); });
}