Skip to content

Instantly share code, notes, and snippets.

@cadorn
Last active August 29, 2015 14:06
Show Gist options
  • Save cadorn/eec828a8d359fa67e800 to your computer and use it in GitHub Desktop.
Save cadorn/eec828a8d359fa67e800 to your computer and use it in GitHub Desktop.
WebIDE Custom Runtime
let { Cc, Ci, Cu } = require('chrome');
/* const FileUtils = */ Cu.import("resource://gre/modules/FileUtils.jsm");
const TIMERS = require("sdk/timers");
const WINDOW_UTILS = require("sdk/window/utils");
const PROMISE = require("sdk/core/promise");
const { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
const { Class } = require("sdk/core/heritage");
const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
const DevToolsUtils = devtools.require("devtools/" + 'toolkit/DevToolsUtils');
const { LocalDebuggerTransport } = devtools.require("devtools/" + 'toolkit/transport/transport');
function openWebIDE() {
let deferred = PROMISE.defer();
let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher);
let win = ww.openWindow(null, "chrome://webide/content/", "webide", "chrome,centerscreen,resizable", null);
win.addEventListener("load", function onLoad() {
win.removeEventListener("load", onLoad);
TIMERS.setTimeout(function() {
addRuntime(win);
openProject(win);
}, 1000);
});
return deferred.promise;
}
openWebIDE();
function openProject(window, location) {
try {
if (window.UI && window.UI.openProject) {
var folder = new FileUtils.File("/pinf/tmp");
window.AppProjects.addPackaged(folder).then(function(project) {
return window.AppManager.validateProject(project).then(function() {
window.AppManager.selectedProject = project;
console.log("item added. now open project.");
window.UI.openProject();
}, console.error);
}, console.error);
}
} catch(err) {
console.error("err", err);
}
}
var Connection = Class({
initialize: function(prefix, transport, url) {
this.url = url;
this.prefix = prefix + ".";
this.transport = transport;
this.nextID = 1;
transport.hooks = this;
this.root = null;
this.pools = new Set();
this.lazyActors = new Map();
},
send: function(packet) {
console.log("SEND: " + JSON.stringify(packet, null, 2));
this.transport.send(packet);
},
onPacket: Task.async(function*(packet) {
console.log("RECV:" + JSON.stringify(packet, null, 2));
}),
onClosed: function() {
console.log("onClosed");
},
allocID: function(prefix) {
console.log("allocID");
return this.prefix + (prefix || '') + this.nextID++;
},
manageLazy: function(parent, actorID, factory) {
console.log("manageLazy()");
this.lazyActors.set(actorID, {
parent: parent,
factory: factory
});
return actorID;
},
getActor: function(actorID) {
console.log("getActor()", actorID);
return null;
},
poolFor: function(actorID) {
console.log("poolFor()", actorID);
},
addActorPool: function(pool) {
console.log("addActorPool()", pool);
this.pools.add(pool);
},
removeActorPool: function(pool) {
console.log("removeActorPool()", pool);
this.pools.remove(pool);
},
listTabs: function () {
console.log("listTabs()");
}
});
function addRuntime(window) {
console.log("Add custom runtime");
let customRuntime = {
getName: function() {
return "custom runtime";
},
connect: function(connection) {
console.log("connect()");
let deferred = PROMISE.defer();
let serverTransport = new LocalDebuggerTransport();
let clientTransport = new LocalDebuggerTransport(serverTransport);
serverTransport.other = clientTransport;
let conn = new Connection("test" + 1, serverTransport, null);
conn.send({
from: "root",
applicationType: "browser",
// There's work to do here.
traits: {
sources: false,
editOuterHTML: false,
highlightable: false,
urlToImageDataResolver: false,
networkMonitor: false,
storageInspector: false,
storageInspectorReadOnly: false,
conditionalBreakpoints: false,
addNewRule: false
}
});
serverTransport.ready();
// clientTransport.ready();
connection.connect(clientTransport);
console.log("connect() done");
return deferred.promise;
},
}
window.AppManager.runtimeList.custom.push(customRuntime);
window.AppManager.update("runtimelist");
}
/*
Cu.import("resource://gre/modules/Services.jsm");
let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
const {AppManager} = devtools.require("devtools/" + 'webide/app-manager');
let winObserver = function(win, topic) {
if (topic == "domwindowopened") {
win.addEventListener("load", function onLoadWindow() {
win.removeEventListener("load", onLoadWindow, false);
if (win.document.documentURI == "chrome://webide/content/webide.xul") {
win.setTimeout(() => onWebIDEWindowOpen(win), 0);
}
}, false);
}
}
Services.ww.registerNotification(winObserver);
*/
/*
TIMERS.setInterval(function() {
var allWindows = WINDOW_UTILS.windows(null, {includePrivate:false});
console.log("allWindows", allWindows);
allWindows.forEach(function(window) {
if (!window.document || window.document.URL !== "chrome://webide/content/webide.xul") return;
console.log("Web IDE opened. now patching.");
openProject(window);
});
}, 2000);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment