2016-05-17 18:25:54 +00:00
|
|
|
"use strict";
|
2015-06-17 08:56:58 +00:00
|
|
|
|
2015-06-17 15:22:18 +00:00
|
|
|
// This function is used to do remote procedure calls from the worker to the
|
|
|
|
// main thread. It is exposed as a built-in global to every module by the
|
|
|
|
// worker loader. To make sure the worker loader can access it, it needs to be
|
|
|
|
// defined before loading the worker loader script below.
|
|
|
|
this.rpc = function (method, ...params) {
|
|
|
|
let id = nextId++;
|
|
|
|
|
|
|
|
postMessage(JSON.stringify({
|
|
|
|
type: "rpc",
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: id
|
|
|
|
}));
|
|
|
|
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
rpcDeferreds[id] = deferred;
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2015-10-13 23:18:43 +00:00
|
|
|
loadSubScript("resource://devtools/shared/worker/loader.js");
|
2015-06-17 08:56:58 +00:00
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var Promise = worker.require("promise");
|
|
|
|
var { ActorPool } = worker.require("devtools/server/actors/common");
|
|
|
|
var { ThreadActor } = worker.require("devtools/server/actors/script");
|
2015-10-26 16:13:11 +00:00
|
|
|
var { WebConsoleActor } = worker.require("devtools/server/actors/webconsole");
|
2015-09-15 18:19:45 +00:00
|
|
|
var { TabSources } = worker.require("devtools/server/actors/utils/TabSources");
|
|
|
|
var makeDebugger = worker.require("devtools/server/actors/utils/make-debugger");
|
|
|
|
var { DebuggerServer } = worker.require("devtools/server/main");
|
2015-06-17 08:56:58 +00:00
|
|
|
|
|
|
|
DebuggerServer.init();
|
|
|
|
DebuggerServer.createRootActor = function () {
|
|
|
|
throw new Error("Should never get here!");
|
|
|
|
};
|
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var connections = Object.create(null);
|
|
|
|
var nextId = 0;
|
|
|
|
var rpcDeferreds = [];
|
2015-06-17 08:56:58 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
this.addEventListener("message", function (event) {
|
2015-06-17 08:56:58 +00:00
|
|
|
let packet = JSON.parse(event.data);
|
|
|
|
switch (packet.type) {
|
2016-05-17 18:25:54 +00:00
|
|
|
case "connect":
|
2015-06-17 08:56:58 +00:00
|
|
|
// Step 3: Create a connection to the parent.
|
2016-05-17 18:25:54 +00:00
|
|
|
let connection = DebuggerServer.connectToParent(packet.id, this);
|
|
|
|
connections[packet.id] = {
|
|
|
|
connection : connection,
|
|
|
|
rpcs: []
|
|
|
|
};
|
2015-06-17 08:56:58 +00:00
|
|
|
|
|
|
|
// Step 4: Create a thread actor for the connection to the parent.
|
2016-05-17 18:25:54 +00:00
|
|
|
let pool = new ActorPool(connection);
|
|
|
|
connection.addActorPool(pool);
|
2015-06-17 08:56:58 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
let sources = null;
|
2015-06-17 08:56:58 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
let parent = {
|
|
|
|
actorID: packet.id,
|
2016-04-07 06:29:28 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
makeDebugger: makeDebugger.bind(null, {
|
|
|
|
findDebuggees: () => {
|
|
|
|
return [this.global];
|
|
|
|
},
|
2015-06-17 08:56:58 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
shouldAddNewGlobalAsDebuggee: () => {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
}),
|
2015-06-17 08:56:58 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
get sources() {
|
|
|
|
if (sources === null) {
|
|
|
|
sources = new TabSources(threadActor);
|
|
|
|
}
|
|
|
|
return sources;
|
|
|
|
},
|
2015-10-26 16:13:11 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
window: global
|
|
|
|
};
|
2015-10-26 16:13:11 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
let threadActor = new ThreadActor(parent, global);
|
|
|
|
pool.addActor(threadActor);
|
2015-10-26 16:13:11 +00:00
|
|
|
|
2016-05-17 18:25:54 +00:00
|
|
|
let consoleActor = new WebConsoleActor(connection, parent);
|
|
|
|
pool.addActor(consoleActor);
|
2015-10-26 16:13:11 +00:00
|
|
|
|
|
|
|
// Step 5: Send a response packet to the parent to notify
|
|
|
|
// it that a connection has been established.
|
2016-05-17 18:25:54 +00:00
|
|
|
postMessage(JSON.stringify({
|
|
|
|
type: "connected",
|
|
|
|
id: packet.id,
|
|
|
|
threadActor: threadActor.actorID,
|
|
|
|
consoleActor: consoleActor.actorID,
|
|
|
|
}));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "disconnect":
|
|
|
|
connections[packet.id].connection.close();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "rpc":
|
|
|
|
let deferred = rpcDeferreds[packet.id];
|
|
|
|
delete rpcDeferreds[packet.id];
|
|
|
|
if (packet.error) {
|
2015-06-17 15:22:18 +00:00
|
|
|
deferred.reject(packet.error);
|
2016-05-17 18:25:54 +00:00
|
|
|
}
|
|
|
|
deferred.resolve(packet.result);
|
|
|
|
break;
|
|
|
|
}
|
2015-06-17 08:56:58 +00:00
|
|
|
});
|