-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchild_plugin.js
330 lines (287 loc) · 10.6 KB
/
child_plugin.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Pepper child_plugin plugin javascript code.
var childPlugins = [];
/**
* @constructor
*
* Sets up a plugin component to emulate an Android process.
*
* @param container Containing document object.
* @param data Requesting child process information.
* @param handleExitMessage Callback to handle exit message.
* @param handleStdoutMessage Callback to handle stdout message.
* @param handleStderrMessage Callback to handle stdout message.
*/
function ChildPlugin(container,
data,
handleExitMessage,
handleStdoutMessage,
handleStderrMessage) {
this.active = true;
/** @private */
this.args_ = data.args;
/** @private */
this.backgroundPage_ = null;
/** @private */
this.command_ = data.plugin;
/** @private */
this.container_ = container;
/** @private */
this.currentWorkingDirectory_ = data.currentworkingdirectory;
/** @private */
this.envs_ = data.envs;
/** @private */
this.handleExitMessage_ = handleExitMessage;
/** @private */
this.handleStderrMessage_ = handleStderrMessage;
/** @private */
this.handleStdoutMessage_ = handleStdoutMessage;
/** @private */
this.loaded_ = false;
/** @private */
this.plugin_ = null;
// |preopened_fd_args_| and |preopened_fd_names_| are used to open
// some files ahead of child process launch, and replace arguments
// with opened descriptor numbers. This is to emulate what would have
// happened if the plugin was forked from a parent process with these
// files open.
// 'data': {
// 'preopened_fd_args': [ '$fd', null ],
// 'preopened_fd_names': [ '/foo/bar.txt', null ],
// 'plugin': 'dalvikvm',
// 'args': [ '--baz', '$fd' ]
// 'envs': [ 'FOO=foo', 'BAR=bar' ]
// }
// In this example, ChildPluginInstance open '/foo/bar.txt', and launch
// 'dalvikvm' with argument '--baz' and '3'. The last number is the
// file descriptor number which is bound to '/foo/bar.txt'.
/** @private */
this.preopened_fd_args_ = data.preopened_fd_args;
/** @private */
this.preopened_fd_names_ = data.preopened_fd_names;
/** @private */
this.requestId_ = data.requestid;
/** @private */
this.requester_ = data.requester;
/** @private */
this.timeoutId_ = null;
/** @private */
this.timeout_ = 2;
this.create_();
}
/** @private */
ChildPlugin.prototype.create_ = function() {
var metadata = arcMetadata.get();
this.plugin_ = document.createElement('embed');
this.plugin_.setAttribute('src', 'arc.nmf');
this.plugin_.setAttribute('type', 'application/x-nacl');
this.plugin_.setAttribute('plugintype', this.command_);
if (this.currentWorkingDirectory_)
this.plugin_.setAttribute('current_working_directory',
this.currentWorkingDirectory_);
this.plugin_.setAttribute('args', this.args_.join(_STRING_DELIMITER));
if (this.envs_) {
this.plugin_.setAttribute('envs', this.envs_.join(_STRING_DELIMITER));
}
if (this.preopened_fd_args_ && this.preopened_fd_names_) {
this.plugin_.setAttribute('preopened_fd_args',
this.preopened_fd_args_.join(_STRING_DELIMITER));
this.plugin_.setAttribute('preopened_fd_names',
this.preopened_fd_names_.join(_STRING_DELIMITER));
}
this.plugin_.setAttribute('requestid', this.requestId_);
this.plugin_.setAttribute('requester', this.requester_);
// TODO(crbug.com/390063): Factor out a function which copies data
// from metadata to attributes
// Child plugin will run a command, e.g., dalvikvm, dexopt, and so on.
// They never need a screen.
this.plugin_.setAttribute('width', 0);
this.plugin_.setAttribute('height', 0);
this.plugin_.setAttribute('packagename', metadata.packageName);
this.plugin_.setAttribute('stderrlog', metadata.stderrLog);
this.plugin_.setAttribute('enablearcstrace', metadata.enableArcStrace);
// TODO(crbug.com/327980): Make it possible to set timezone dynamically
// when Chrome changes timezone.
this.plugin_.setAttribute('timezone', getTimeZone());
var locale = getLocale();
this.plugin_.setAttribute('language', locale.language);
if (locale.country) // Country code is optional.
this.plugin_.setAttribute('country', locale.country);
// Enable stdio bridge only in system mode.
if (metadata.packageName == 'org.chromium.arc.system')
this.plugin_.setAttribute('enable_stdio_bridge', true);
console.time('ChildPlugin ' + this.requester_ + ':' + this.requestId_ +
':' + this.args_[0] + '(' + this.command_ + ') Run');
this.container_.appendChild(this.plugin_);
this.plugin_.addEventListener(
'message', this.handleMessageEvent_.bind(this), false);
this.plugin_.addEventListener('crash', function(crashEvent) {
this.postExitMessage_(this.createExitMessage_(-1),
'Plugin process crashed');
}.bind(this), false);
this.timeoutId_ = setTimeout(function() {
this.timeoutId_ = null;
if (this.loaded_)
return;
var container = this.container_;
this.remove('timeout to spawn ChildPlugin process: ' + this.args_[0] + '(' +
this.command_ + '), ' + this.args_.join(' '));
if (this.timeout_ >= 32) {
console.error('give up to spawn ChildPlugin process.');
// Usually, src/plugin/child_plugin_instance.cc sends this message,
// but when the child plugin is timed out, this code takes care of
// sending the exit message.
// Potentially, sending the message has a race issue since the
// child plugin instance may start asynchronously. In that case,
// the second message is just ignored. See also mods/android/
// frameworks/base/arc/java/org/chromium/arc/shell/
// ChildPluginProcess.java.
this.postExitMessage_(this.createExitMessage_(-1));
return;
}
this.timeout_ *= 2;
console.log('retry to spawn with timeout ' + this.timeout_ + 'sec.');
this.active = true;
this.container_ = container;
this.create_();
}.bind(this), this.timeout_ * 1000);
if (window['arc'])
this.backgroundPage_ = window['arc'].backgroundPage;
else
this.backgroundPage_ = window;
};
/** @private */
ChildPlugin.prototype.handleMessageEvent_ = function(messageEvent) {
if (!this.active) {
// The plugin process had already been removed.
return;
}
// Check primitive messages firstly.
var message = messageEvent.data;
console.log(message);
if (message.constructor == ArrayBuffer) {
// Assume all ArrayBuffers are minidumps.
this.backgroundPage_.crashReporter.reportCrash(
'minidump on ' + this.command_, message);
return;
}
// Handle plugin message.
if (message.namespace == 'jsPlugin') {
if (message.command == 'loadResult') {
if (message.data.result) {
var command = this.command_;
if (this.args_.length > 0) {
command += '/' + this.args_[0];
}
console.log('the child process has been loaded: ' + command);
this.loaded_ = true;
} else {
this.remove('Cannot load plugin process');
}
return;
} else if (message.command == 'crashLogMessage') {
// This is only handled by the main plugin. Ignore.
return;
}
}
if (message.namespace != 'jsChildplugin') {
this.remove('Received invalid namespace message. ' +
'Namespace: "' + message.namespace + '", ' +
'Command: "' + message.command + '"');
return;
}
if (message.command == 'spawn') {
// ChildPlugin may spawn another ChildPlugin, e.g., dalvikvm may spawn
// dexopt.
ChildPlugin.handleChildPluginMessage(
message,
this.plugin_.postMessage.bind(this.plugin_),
this.handleStdoutMessage_,
this.handleStderrMessage_);
} else if (message.command == 'stdout') {
// Redirect stdout message to androidChildplugin.
message.namespace = 'androidChildplugin';
// Bypass stdout to the ChildPlugin owner process.
if (this.handleStdoutMessage_)
this.handleStdoutMessage_(message);
} else if (message.command == 'stderr') {
// Redirect stderr message to androidChildplugin.
message.namespace = 'androidChildplugin';
// Bypass stderr to the ChildPlugin owner process.
if (this.handleStderrMessage_)
this.handleStderrMessage_(message);
} else if (message.command == 'exit') {
// Notify the caller and shut down the ChildPlugin process.
// Call remove() first to work around crbug.com/386312.
message.namespace = this.requester_;
this.postExitMessage_(message);
} else {
this.remove('Received unknown childplugin command: ' + message.command);
}
};
/** @private */
ChildPlugin.prototype.postExitMessage_ = function(message, error) {
if (this.remove(error))
this.handleExitMessage_(message);
};
/** @private */
ChildPlugin.prototype.createExitMessage_ = function(exitCode) {
return {
namespace: 'androidChildplugin',
command: 'exit',
data: {
requestid: this.requestId_,
requester: this.requester_,
result: exitCode
}
};
};
/**
* Removes the child plugin process, and optionally displays an error message.
*
* @param errorString optional error message to display.
* @return {boolean} False if the process is alread removed.
*/
ChildPlugin.prototype.remove = function(errorString) {
if (!this.active) {
// The plugin process had already been removed.
return false;
}
this.active = false;
if (this.timeoutId_) {
clearTimeout(this.timeoutId_);
this.timeoutId_ = null;
}
console.timeEnd('ChildPlugin ' + this.requester_ + ':' + this.requestId_ +
':' + this.args_[0] + '(' + this.command_ + ') Run');
if (errorString) {
console.log(errorString);
}
this.container_.removeChild(this.plugin_);
this.container_ = null;
this.plugin_ = null;
childPlugins = childPlugins.filter(function(instance) { instance != this; });
return true;
};
/**
* Handle childplugin spawn message to spawn a ChildPlugin instance.
*/
ChildPlugin.handleChildPluginMessage = function(message,
handleExitMessage,
handleStdoutMessage,
handleStderrMessage) {
if (message.command == 'spawn') {
childPlugins.push(new ChildPlugin(
document.getElementById('appdiv'),
message.data,
handleExitMessage,
handleStdoutMessage,
handleStderrMessage));
} else {
console.log('Received unknown childplugin message: ');
console.log(message);
}
};