michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu } = Components; michael@0: michael@0: let { gDevTools } = Cu.import("resource:///modules/devtools/gDevTools.jsm", {}); michael@0: let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); michael@0: let { Services } = Cu.import("resource://gre/modules/Services.jsm", {}); michael@0: let { debuggerSocketConnect, DebuggerClient } = michael@0: Cu.import("resource://gre/modules/devtools/dbg-client.jsm", {}); michael@0: let { ViewHelpers } = michael@0: Cu.import("resource:///modules/devtools/ViewHelpers.jsm", {}); michael@0: michael@0: /** michael@0: * Shortcuts for accessing various debugger preferences. michael@0: */ michael@0: let Prefs = new ViewHelpers.Prefs("devtools.debugger", { michael@0: chromeDebuggingHost: ["Char", "chrome-debugging-host"], michael@0: chromeDebuggingPort: ["Int", "chrome-debugging-port"] michael@0: }); michael@0: michael@0: let gToolbox, gClient; michael@0: michael@0: function connect() { michael@0: window.removeEventListener("load", connect); michael@0: // Initiate the connection michael@0: let transport = debuggerSocketConnect( michael@0: Prefs.chromeDebuggingHost, michael@0: Prefs.chromeDebuggingPort michael@0: ); michael@0: gClient = new DebuggerClient(transport); michael@0: gClient.connect(() => { michael@0: let addonID = getParameterByName("addonID"); michael@0: michael@0: if (addonID) { michael@0: gClient.listAddons(({addons}) => { michael@0: let addonActor = addons.filter(addon => addon.id === addonID).pop(); michael@0: openToolbox({ addonActor: addonActor.actor, title: addonActor.name }); michael@0: }); michael@0: } else { michael@0: gClient.listTabs(openToolbox); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: window.addEventListener("load", connect); michael@0: michael@0: function openToolbox(form) { michael@0: let options = { michael@0: form: form, michael@0: client: gClient, michael@0: chrome: true michael@0: }; michael@0: devtools.TargetFactory.forRemoteTab(options).then(target => { michael@0: let frame = document.getElementById("toolbox-iframe"); michael@0: let options = { customIframe: frame }; michael@0: gDevTools.showToolbox(target, michael@0: "jsdebugger", michael@0: devtools.Toolbox.HostType.CUSTOM, michael@0: options) michael@0: .then(onNewToolbox); michael@0: }); michael@0: } michael@0: michael@0: function onNewToolbox(toolbox) { michael@0: gToolbox = toolbox; michael@0: bindToolboxHandlers(); michael@0: raise(); michael@0: } michael@0: michael@0: function bindToolboxHandlers() { michael@0: gToolbox.once("destroyed", quitApp); michael@0: window.addEventListener("unload", onUnload); michael@0: } michael@0: michael@0: function onUnload() { michael@0: window.removeEventListener("unload", onUnload); michael@0: window.removeEventListener("message", onMessage); michael@0: gToolbox.destroy(); michael@0: } michael@0: michael@0: function onMessage(event) { michael@0: try { michael@0: let json = JSON.parse(event.data); michael@0: switch (json.name) { michael@0: case "toolbox-raise": michael@0: raise(); michael@0: break; michael@0: case "toolbox-title": michael@0: setTitle(json.data.value); michael@0: break; michael@0: } michael@0: } catch(e) { Cu.reportError(e); } michael@0: } michael@0: michael@0: window.addEventListener("message", onMessage); michael@0: michael@0: function raise() { michael@0: window.focus(); michael@0: } michael@0: michael@0: function setTitle(title) { michael@0: document.title = title; michael@0: } michael@0: michael@0: function quitApp() { michael@0: let quit = Cc["@mozilla.org/supports-PRBool;1"] michael@0: .createInstance(Ci.nsISupportsPRBool); michael@0: Services.obs.notifyObservers(quit, "quit-application-requested", null); michael@0: michael@0: let shouldProceed = !quit.data; michael@0: if (shouldProceed) { michael@0: Services.startup.quit(Ci.nsIAppStartup.eForceQuit); michael@0: } michael@0: } michael@0: michael@0: function getParameterByName (name) { michael@0: let name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); michael@0: let regex = new RegExp("[\\?&]" + name + "=([^&#]*)"); michael@0: let results = regex.exec(window.location.search); michael@0: return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); michael@0: }