forked from mozilla/r2d2b2g
/
remote-simulator-client.js
517 lines (433 loc) · 17.1 KB
/
remote-simulator-client.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* 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/.
*/
'use strict';
const { Cc, Ci, Cu, ChromeWorker } = require("chrome");
Cu.import("resource://gre/modules/Services.jsm");
const { EventTarget } = require("sdk/event/target");
const { emit, off } = require("sdk/event/core");
const { Class } = require("sdk/core/heritage");
const Environment = require("sdk/system/environment").env;
const Runtime = require("runtime");
const Self = require("self");
const URL = require("url");
const Subprocess = require("subprocess");
const { setTimeout, clearTimeout } = require("sdk/timers");
const { rootURI: ROOT_URI } = require('@loader/options');
const PROFILE_URL = ROOT_URI + "profile/";
// import debuggerSocketConnect and DebuggerClient
const dbgClient = Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
const Geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
const DEBUGGER_CONNECT_TIMEOUT = 30000;
// add unsolicited notifications
dbgClient.UnsolicitedNotifications.enableRealGeolocation = "enableRealGeolocation";
dbgClient.UnsolicitedNotifications.disableRealGeolocation = "disableRealGeolocation";
dbgClient.UnsolicitedNotifications.appUpdateRequest = "appUpdateRequest";
dbgClient.UnsolicitedNotifications.appOpen = "appOpen";
dbgClient.UnsolicitedNotifications.appClose = "appClose";
// Log subprocess error and debug messages to the console. This logs messages
// for all consumers of the API. We trim the messages because they sometimes
// have trailing newlines. And note that registerLogHandler actually registers
// an error handler, despite its name.
Subprocess.registerLogHandler(
function(s) console.error("subprocess: " + s.trim())
);
Subprocess.registerDebugHandler(
function(s) console.debug("subprocess: " + s.trim())
);
const RemoteSimulatorClient = Class({
extends: EventTarget,
initialize: function initialize(options) {
this._appUpdateHandler = options.appUpdateHandler;
EventTarget.prototype.initialize.call(this, options);
this._hookInternalEvents();
},
// Flags for connection state
// TODO: remove them when bug 800447 is fixed.
_clientConnecting: false,
_clientConnected: false,
// check if b2g is running and connected
get isConnected() this._clientConnected,
// check if b2g is running
get isRunning() !!this.process,
// check if the remote is connected and fully initialized
// (means that we can start using the client!)
get isReady() !!this._remote,
get client() this._remote.client,
getActorForApp: function (manifestURL, callback) {
this.client.request({to: this._remote.webapps, type: "listApps"},
function (reply) {
let actor = reply.apps[manifestURL];
callback(actor);
});
},
_hookInternalEvents: function () {
// on clientConnected, register an handler to close current connection
// on kill and send a "listTabs" debug protocol request, finally
// emit a clientReady event on "listTabs" reply
this.on("clientConnected", function (data) {
console.debug("rsc.onClientConnected");
this._clientConnecting = false;
this._clientConnected = true;
let client = data.client;
this.once("kill", function () client.close());
client.request({to: "root", type: "listTabs"}, (function (reply) {
emit(this, "clientReady", {
client: client,
globals: reply,
tabs: reply.tabs,
selected: reply.selected,
simulator: reply.simulatorActor,
webapps: reply.simulatorWebappsActor,
geolocation: reply.simulatorGeolocationActor,
geolocationUI: reply.simulatorGeolocationUIActor
});
}).bind(this));
});
// on clientReady, track remote target
// listeners and emit an high level "ready" event
this.on("clientReady", function (remote) {
console.debug("rsc.onClientReady");
// Actors are initialized lazily, so if they have any initialization work
// to perform (set up listeners / observers, etc.), then some message must
// be sent to trigger that process.
remote.client.request({to: remote.simulator, type: "ping"});
remote.client.request({to: remote.geolocationUI, type: "attach"});
this._remote = remote;
// Start watching app open/close
this.client.request({to: this._remote.webapps, type: "watchApps"},
(function () {
emit(this, "ready", null);
}).bind(this));
});
// on clientClosed, untrack old remote target and emit
// an high level "disconnected" event
this.on("clientClosed", function () {
console.debug("rsc.onClientClosed");
this._clientConnecting = false;
if (!this._clientConnected && this.process) {
// If the connection was closed before connection succeed,
// and the process is still alive, the attempt was rejected or timed out.
// We should keep trying to connect until we reach our own timeout.
if (Date.now() - this._startConnectingTime < DEBUGGER_CONNECT_TIMEOUT) {
setTimeout(this.connectDebuggerClient.bind(this), 250);
}
else {
emit(this, "timeout", null);
this.kill();
}
}
else {
this._remote = null;
if (this._clientConnected) {
this._clientConnected = false;
emit(this, "disconnected", null);
}
}
});
this.on("stdout", function onStdout(data) console.log(data.trim()));
this.on("stderr", function onStderr(data) console.error(data.trim()));
},
/**
* Start the process and connect the debugger client.
*/
run: function() {
// resolve b2g binaries path (raise exception if not found)
let b2gExecutable = this.b2gExecutable;
// kill before start if already running
if (this.process != null) {
this.process.kill();
}
this.once("stdout", function () {
if (Runtime.OS == "Darwin") {
console.debug("WORKAROUND run osascript to show b2g-desktop window"+
" on Runtime.OS=='Darwin'");
// Escape double quotes and escape characters for use in AppleScript.
let path = b2gExecutable.path
.replace(/\\/g, "\\\\").replace(/\"/g, '\\"');
Subprocess.call({
command: "/usr/bin/osascript",
arguments: ["-e", 'tell application "' + path + '" to activate'],
});
}
});
let environment;
if (Runtime.OS == "Linux") {
environment = ["TMPDIR=" + Services.dirsvc.get("TmpD",Ci.nsIFile).path];
if ("DISPLAY" in Environment) {
environment.push("DISPLAY=" + Environment.DISPLAY);
}
}
// spawn a b2g instance
this.process = Subprocess.call({
command: b2gExecutable,
arguments: this.b2gArguments,
environment: environment,
// emit stdout event
stdout: (function(data) {
emit(this, "stdout", data);
}).bind(this),
// emit stderr event
stderr: (function(data) {
emit(this, "stderr", data);
}).bind(this),
// on b2g instance exit, reset tracked process, remoteDebuggerPort and
// shuttingDown flag, then finally emit an exit event
done: (function(result) {
console.log(this.b2gFilename + " terminated with " + result.exitCode);
this.process = null;
// NOTE: reset old allocated remoteDebuggerPort
this.remoteDebuggerPort = null;
this._clientConnecting = false;
this.shuttingDown = false;
emit(this, "exit", result.exitCode);
}).bind(this)
});
this._startConnectingTime = Date.now();
this.connectDebuggerClient();
},
// request a b2g instance kill and optionally execute a callback on exit
kill: function(onKilled) {
if (this.process && !this.shuttingDown) {
emit(this, "kill", null);
this.shuttingDown = true;
if (typeof onKilled === "function")
this.once("exit", onKilled);
this.process.kill();
}
},
// connect simulator using debugging protocol
// NOTE: this control channel will be auto-created on every b2g instance run
connectDebuggerClient: function() {
if (this._clientConnected || this._clientConnecting) {
console.warn("remote-simulator-client: already connected.");
return;
}
this._clientConnecting = true;
let transport = debuggerSocketConnect("127.0.0.1", this.remoteDebuggerPort);
// Unbrick some rare connection attempts that ends up facing a race.
// In such scenario, the connection isn't refused but input-stream-pump
// component used in DebuggerTransport never gets its onStartRequest/onStopRequest
// methods called.
// We cancel attempts taking more than 3seconds to achieve.
let timeout = setTimeout(function () {
console.log("Killing stuck connection");
client.close();
}, 1000);
let client = new DebuggerClient(transport);
client.addListener("closed", (function () {
clearTimeout(timeout);
this._stopGeolocation();
emit(this, "clientClosed", {client: client});
}).bind(this));
client.addListener("enableRealGeolocation",
this.onEnableRealGeolocation.bind(this));
client.addListener("disableRealGeolocation",
this.onDisableRealGeolocation.bind(this));
client.addListener("appOpen", this.onAppOpen.bind(this));
client.addListener("appClose", this.onAppClose.bind(this));
this._registerAppUpdateRequest(client);
client.connect((function () {
clearTimeout(timeout);
emit(this, "clientConnected", {client: client});
}).bind(this));
},
_registerAppUpdateRequest: function(client) {
client.addListener("appUpdateRequest", (function(type, pkt) {
console.log("APP UPDATE REQUEST RECEIVED", JSON.stringify(arguments, null, 2));
this._appUpdateHandler(pkt.appId);
}).bind(this));
},
_geolocationID: null,
onEnableRealGeolocation: function onEnableRealGeolocation() {
console.log("Firefox received enable real geolocation request");
let onSuccess = (function onSuccess(position) {
if (!this._remote) {
console.warn("position watcher called while Simulator not running");
// Try clearing the geolocation watch, even though we shouldn't have to.
this._stopGeolocation();
return;
}
console.log("Firefox sending geolocation response");
this._remote.client.request({
to: this._remote.geolocation,
message: {
lat: position.coords.latitude,
lon: position.coords.longitude,
},
type: "update"
});
}).bind(this);
let onError = function onError(error) {
console.error("geolocation error " + error.code + ": " + error.message);
};
this._geolocationID = Geolocation.watchPosition(onSuccess, onError);
},
onDisableRealGeolocation: function onDisableRealGeolocation() {
console.log("Firefox received disable real geolocation request");
this._stopGeolocation();
},
_stopGeolocation: function _stopGeolocation() {
if (this._geolocationID) {
Geolocation.clearWatch(this._geolocationID);
this._geolocationID = null;
}
},
onAppOpen: function (type, packet) {
emit(this, "appOpen", {
manifestURL: packet.manifestURL
});
},
onAppClose: function (type, packet) {
emit(this, "appClose", {
manifestURL: packet.manifestURL
});
},
// send a getBuildID request to the remote simulator actor
getBuildID: function(onResponse) {
let remote = this._remote;
remote.client.request({to: remote.simulator, type: "getBuildID"}, onResponse);
},
// send a runApp request to the remote simulator actor
runApp: function(appId, onResponse) {
let remote = this._remote;
remote.client.request({to: remote.simulator, type: "runApp", appId: appId},
onResponse);
},
// send an install request to the remote webappsActor
install: function(appInfo, onResponse) {
this._remote.client.request({ to: this._remote.webapps,
type: "install",
appId: appInfo.appId,
appReceipt: appInfo.appReceipt,
appType: appInfo.appType},
onResponse);
},
uninstall: function(appId, onResponse) {
this._remote.client.request({ to: this._remote.simulator,
type: "uninstallApp",
appId: appId,
},
onResponse);
},
validateManifest: function(manifest, onResponse) {
this._remote.client.request({ to: this._remote.simulator,
type: "validateManifest",
manifest: manifest,
},
onResponse);
},
showNotification: function(userMessage, onResponse) {
this._remote.client.request({ to: this._remote.simulator,
type: "showNotification",
userMessage: userMessage,
},
onResponse);
},
appNotFound: function(appId, onResponse) {
this._remote.client.request({ to: this._remote.simulator,
type: "appNotFound",
appId: appId
},
onResponse);
},
// send a ping request to the remote simulator actor
ping: function(onResponse) {
let remote = this._remote;
remote.client.request({to: remote.simulator, type: "ping"}, onResponse);
},
/* REMOTE PACKET LISTENERS MANAGEMENT */
// add unsolicited remote packet type listener
_onRemotePacket: function(type, aListener) {
let remote = this._remote;
remote.client.addListener(type, aListener);
},
// remove unsolicited remote packet type listener
_offRemotePacket: function(type, aListener) {
let remote = this._remote;
remote.client.removeListener(type, aListener);
},
// clear unsolicited remote packet type listener
_unsubscribeRemotePacket: function(type) {
let remote = this._remote;
let listeners = remote.client._getListeners(type);
listeners.forEach(function (l) remote.client.removeListener(type, l));
},
// add remote packet type listener (run only once)
_onceRemotePacket: function(type, aListener) {
let remote = this._remote;
remote.client.addOneTimeListener(type, aListener);
},
// compute current b2g filename
get b2gFilename() {
return this._executable ? this._executableFilename : "B2G";
},
// compute current b2g file handle
get b2gExecutable() {
if (this._executable) return this._executable;
let executables = {
WINNT: "win32/b2g/b2g-bin.exe",
Darwin: "mac64/B2G.app/Contents/MacOS/b2g-bin",
Linux: (Runtime.XPCOMABI.indexOf("x86_64") == 0 ? "linux64" : "linux") +
"/b2g/b2g-bin",
};
let url = Self.data.url(executables[Runtime.OS]);
let path = URL.toFilename(url);
let executable = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
executable.initWithPath(path);
let executableFilename = executables[Runtime.OS];
// Support B2G binaries built without GAIADIR.
if (!executable.exists()) {
let executables = {
WINNT: "win32/b2g/b2g.exe",
Darwin: "mac64/B2G.app/Contents/MacOS/b2g",
Linux: (Runtime.XPCOMABI.indexOf("x86_64") == 0 ? "linux64" : "linux") +
"/b2g/b2g",
};
let url = Self.data.url(executables[Runtime.OS]);
let path = URL.toFilename(url);
executable.initWithPath(path);
executableFilename = executables[Runtime.OS];
}
if (!executable.exists()) {
// B2G binaries not found
throw Error("b2g-desktop Executable not found.");
}
this._executable = executable;
this._executableFilename = executableFilename;
return executable;
},
// compute b2g CLI arguments
get b2gArguments() {
let args = [];
let profile = URL.toFilename(PROFILE_URL);
args.push("-profile", profile);
// NOTE: push dbgport option on the b2g-desktop commandline
args.push("-dbgport", ""+this.remoteDebuggerPort);
// Ignore eventual zombie instances of b2g that are left over
args.push("-no-remote");
return args;
},
// NOTE: find a port for remoteDebuggerPort if it's null or undefined
get remoteDebuggerPort() {
var port = this._foundRemoteDebuggerPort;
if (port) {
return port;
}
var serv = Cc['@mozilla.org/network/server-socket;1']
.createInstance(Ci.nsIServerSocket);
serv.init(-1, true, -1);
var found = serv.port;
console.log("rsc.remoteDebuggerPort: found free port ", found);
this.remoteDebuggerPort = found;
serv.close();
return found;
},
// NOTE: manual set and reset allocated remoteDebuggingPort
// (used by process done handler)
set remoteDebuggerPort(port) {
this._foundRemoteDebuggerPort = port;
},
});
module.exports = RemoteSimulatorClient;