summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/dev/debuggee.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-10 02:51:36 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-10 02:51:36 -0500
commit37d5300335d81cecbecc99812747a657588c63eb (patch)
tree765efa3b6a56bb715d9813a8697473e120436278 /toolkit/jetpack/dev/debuggee.js
parentb2bdac20c02b12f2057b9ef70b0a946113a00e00 (diff)
parent4fb11cd5966461bccc3ed1599b808237be6b0de9 (diff)
downloadUXP-37d5300335d81cecbecc99812747a657588c63eb.tar
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.gz
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.lz
UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.xz
UXP-37d5300335d81cecbecc99812747a657588c63eb.zip
Merge branch 'ext-work'
Diffstat (limited to 'toolkit/jetpack/dev/debuggee.js')
-rw-r--r--toolkit/jetpack/dev/debuggee.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/toolkit/jetpack/dev/debuggee.js b/toolkit/jetpack/dev/debuggee.js
new file mode 100644
index 000000000..0ca0bd37a
--- /dev/null
+++ b/toolkit/jetpack/dev/debuggee.js
@@ -0,0 +1,95 @@
+/* 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";
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { Cu } = require("chrome");
+const { Class } = require("../sdk/core/heritage");
+const { MessagePort, MessageChannel } = require("../sdk/messaging");
+const { require: devtoolsRequire } = Cu.import("resource://devtools/shared/Loader.jsm", {});
+const { DebuggerServer } = devtoolsRequire("devtools/server/main");
+
+const outputs = new WeakMap();
+const inputs = new WeakMap();
+const targets = new WeakMap();
+const transports = new WeakMap();
+
+const inputFor = port => inputs.get(port);
+const outputFor = port => outputs.get(port);
+const transportFor = port => transports.get(port);
+
+const fromTarget = target => {
+ const debuggee = new Debuggee();
+ const { port1, port2 } = new MessageChannel();
+ inputs.set(debuggee, port1);
+ outputs.set(debuggee, port2);
+ targets.set(debuggee, target);
+
+ return debuggee;
+};
+exports.fromTarget = fromTarget;
+
+const Debuggee = Class({
+ extends: MessagePort.prototype,
+ close: function() {
+ const server = transportFor(this);
+ if (server) {
+ transports.delete(this);
+ server.close();
+ }
+ outputFor(this).close();
+ },
+ start: function() {
+ const target = targets.get(this);
+ if (target.isLocalTab) {
+ // Since a remote protocol connection will be made, let's start the
+ // DebuggerServer here, once and for all tools.
+ if (!DebuggerServer.initialized) {
+ DebuggerServer.init();
+ DebuggerServer.addBrowserActors();
+ }
+
+ transports.set(this, DebuggerServer.connectPipe());
+ }
+ // TODO: Implement support for remote connections (See Bug 980421)
+ else {
+ throw Error("Remote targets are not yet supported");
+ }
+
+ // pipe messages send to the debuggee to an actual
+ // server via remote debugging protocol transport.
+ inputFor(this).addEventListener("message", ({data}) =>
+ transportFor(this).send(data));
+
+ // pipe messages received from the remote debugging
+ // server transport onto the this debuggee.
+ transportFor(this).hooks = {
+ onPacket: packet => inputFor(this).postMessage(packet),
+ onClosed: () => inputFor(this).close()
+ };
+
+ inputFor(this).start();
+ outputFor(this).start();
+ },
+ postMessage: function(data) {
+ return outputFor(this).postMessage(data);
+ },
+ get onmessage() {
+ return outputFor(this).onmessage;
+ },
+ set onmessage(onmessage) {
+ outputFor(this).onmessage = onmessage;
+ },
+ addEventListener: function(...args) {
+ return outputFor(this).addEventListener(...args);
+ },
+ removeEventListener: function(...args) {
+ return outputFor(this).removeEventListener(...args);
+ }
+});
+exports.Debuggee = Debuggee;