2012-11-30 08:07:59 +00:00
|
|
|
/* 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";
|
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
const {Cc, Ci, Cu} = require("chrome");
|
2013-04-30 16:58:04 +00:00
|
|
|
const MAX_ORDINAL = 99;
|
2013-04-11 20:59:08 +00:00
|
|
|
let Promise = require("sdk/core/promise");
|
|
|
|
let EventEmitter = require("devtools/shared/event-emitter");
|
2013-05-24 10:26:17 +00:00
|
|
|
let Telemetry = require("devtools/shared/telemetry");
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource:///modules/devtools/gDevTools.jsm");
|
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
loader.lazyGetter(this, "Hosts", () => require("devtools/framework/toolbox-hosts").Hosts);
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CommandUtils",
|
|
|
|
"resource:///modules/devtools/DeveloperToolbar.jsm");
|
2013-01-04 20:31:38 +00:00
|
|
|
|
2012-12-18 15:11:27 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "toolboxStrings", function() {
|
|
|
|
let bundle = Services.strings.createBundle("chrome://browser/locale/devtools/toolbox.properties");
|
2013-02-26 12:40:19 +00:00
|
|
|
let l10n = function(aName, ...aArgs) {
|
2012-12-18 15:11:27 +00:00
|
|
|
try {
|
2013-02-26 12:40:19 +00:00
|
|
|
if (aArgs.length == 0) {
|
|
|
|
return bundle.GetStringFromName(aName);
|
|
|
|
} else {
|
|
|
|
return bundle.formatStringFromName(aName, aArgs, aArgs.length);
|
|
|
|
}
|
2012-12-18 15:11:27 +00:00
|
|
|
} catch (ex) {
|
2013-02-26 12:40:19 +00:00
|
|
|
Services.console.logStringMessage("Error reading '" + aName + "'");
|
2012-12-18 15:11:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return l10n;
|
|
|
|
});
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-01-04 20:31:38 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "Requisition", function() {
|
2013-04-11 20:59:08 +00:00
|
|
|
let scope = {};
|
|
|
|
Cu.import("resource://gre/modules/devtools/Require.jsm", scope);
|
2013-05-09 14:15:22 +00:00
|
|
|
Cu.import("resource://gre/modules/devtools/gcli.jsm", {});
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-04-11 20:59:08 +00:00
|
|
|
let req = scope.require;
|
|
|
|
return req('gcli/cli').Requisition;
|
2013-01-04 20:31:38 +00:00
|
|
|
});
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A "Toolbox" is the component that holds all the tools for one specific
|
|
|
|
* target. Visually, it's a document that includes the tools tabs and all
|
|
|
|
* the iframes where the tool panels will be living in.
|
|
|
|
*
|
|
|
|
* @param {object} target
|
|
|
|
* The object the toolbox is debugging.
|
|
|
|
* @param {string} selectedTool
|
|
|
|
* Tool to select initially
|
2012-12-13 13:03:55 +00:00
|
|
|
* @param {Toolbox.HostType} hostType
|
|
|
|
* Type of host that will host the toolbox (e.g. sidebar, window)
|
2012-11-30 08:07:59 +00:00
|
|
|
*/
|
2013-04-11 20:59:08 +00:00
|
|
|
function Toolbox(target, selectedTool, hostType) {
|
2012-11-30 08:07:59 +00:00
|
|
|
this._target = target;
|
|
|
|
this._toolPanels = new Map();
|
2013-05-24 10:26:17 +00:00
|
|
|
this._telemetry = new Telemetry();
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
this._toolRegistered = this._toolRegistered.bind(this);
|
|
|
|
this._toolUnregistered = this._toolUnregistered.bind(this);
|
|
|
|
this.destroy = this.destroy.bind(this);
|
|
|
|
|
2013-03-07 07:30:03 +00:00
|
|
|
this._target.on("close", this.destroy);
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
if (!hostType) {
|
|
|
|
hostType = Services.prefs.getCharPref(this._prefs.LAST_HOST);
|
|
|
|
}
|
|
|
|
if (!selectedTool) {
|
|
|
|
selectedTool = Services.prefs.getCharPref(this._prefs.LAST_TOOL);
|
|
|
|
}
|
2013-01-11 12:16:31 +00:00
|
|
|
let definitions = gDevTools.getToolDefinitionMap();
|
2013-04-15 12:34:48 +00:00
|
|
|
if (!definitions.get(selectedTool) && selectedTool != "options") {
|
2012-11-30 08:07:59 +00:00
|
|
|
selectedTool = "webconsole";
|
|
|
|
}
|
|
|
|
this._defaultToolId = selectedTool;
|
|
|
|
|
|
|
|
this._host = this._createHost(hostType);
|
|
|
|
|
2012-12-14 07:05:00 +00:00
|
|
|
EventEmitter.decorate(this);
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-02-26 12:40:19 +00:00
|
|
|
this._refreshHostTitle = this._refreshHostTitle.bind(this);
|
|
|
|
this._target.on("navigate", this._refreshHostTitle);
|
|
|
|
this.on("host-changed", this._refreshHostTitle);
|
|
|
|
this.on("select", this._refreshHostTitle);
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
gDevTools.on("tool-registered", this._toolRegistered);
|
|
|
|
gDevTools.on("tool-unregistered", this._toolUnregistered);
|
|
|
|
}
|
2013-04-11 20:59:08 +00:00
|
|
|
exports.Toolbox = Toolbox;
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The toolbox can be 'hosted' either embedded in a browser window
|
|
|
|
* or in a separate window.
|
|
|
|
*/
|
|
|
|
Toolbox.HostType = {
|
|
|
|
BOTTOM: "bottom",
|
|
|
|
SIDE: "side",
|
|
|
|
WINDOW: "window"
|
|
|
|
}
|
|
|
|
|
|
|
|
Toolbox.prototype = {
|
|
|
|
_URL: "chrome://browser/content/devtools/framework/toolbox.xul",
|
|
|
|
|
|
|
|
_prefs: {
|
|
|
|
LAST_HOST: "devtools.toolbox.host",
|
|
|
|
LAST_TOOL: "devtools.toolbox.selectedTool",
|
|
|
|
SIDE_ENABLED: "devtools.toolbox.sideEnabled"
|
|
|
|
},
|
|
|
|
|
|
|
|
HostType: Toolbox.HostType,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a *copy* of the _toolPanels collection.
|
|
|
|
*
|
|
|
|
* @return {Map} panels
|
|
|
|
* All the running panels in the toolbox
|
|
|
|
*/
|
|
|
|
getToolPanels: function TB_getToolPanels() {
|
|
|
|
let panels = new Map();
|
|
|
|
|
|
|
|
for (let [key, value] of this._toolPanels) {
|
|
|
|
panels.set(key, value);
|
|
|
|
}
|
|
|
|
return panels;
|
|
|
|
},
|
|
|
|
|
2012-12-13 13:03:55 +00:00
|
|
|
/**
|
|
|
|
* Access the panel for a given tool
|
|
|
|
*/
|
|
|
|
getPanel: function TBOX_getPanel(id) {
|
|
|
|
return this.getToolPanels().get(id);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a shortcut for getPanel(currentToolId) because it is much more
|
|
|
|
* likely that we're going to want to get the panel that we've just made
|
|
|
|
* visible
|
|
|
|
*/
|
|
|
|
getCurrentPanel: function TBOX_getCurrentPanel() {
|
|
|
|
return this.getToolPanels().get(this.currentToolId);
|
|
|
|
},
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
/**
|
|
|
|
* Get/alter the target of a Toolbox so we're debugging something different.
|
|
|
|
* See Target.jsm for more details.
|
|
|
|
* TODO: Do we allow |toolbox.target = null;| ?
|
|
|
|
*/
|
|
|
|
get target() {
|
|
|
|
return this._target;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/alter the host of a Toolbox, i.e. is it in browser or in a separate
|
|
|
|
* tab. See HostType for more details.
|
|
|
|
*/
|
|
|
|
get hostType() {
|
|
|
|
return this._host.type;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/alter the currently displayed tool.
|
|
|
|
*/
|
|
|
|
get currentToolId() {
|
|
|
|
return this._currentToolId;
|
|
|
|
},
|
|
|
|
|
|
|
|
set currentToolId(value) {
|
|
|
|
this._currentToolId = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the iframe containing the toolbox UI.
|
|
|
|
*/
|
|
|
|
get frame() {
|
|
|
|
return this._host.frame;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut to the document containing the toolbox UI
|
|
|
|
*/
|
|
|
|
get doc() {
|
|
|
|
return this.frame.contentDocument;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the toolbox
|
|
|
|
*/
|
|
|
|
open: function TBOX_open() {
|
2012-12-13 13:03:55 +00:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
2013-05-24 10:26:17 +00:00
|
|
|
this._host.create().then(iframe => {
|
|
|
|
let domReady = () => {
|
2013-01-04 11:54:54 +00:00
|
|
|
iframe.removeEventListener("DOMContentLoaded", domReady, true);
|
|
|
|
|
2012-12-13 13:03:55 +00:00
|
|
|
this.isReady = true;
|
|
|
|
|
|
|
|
let closeButton = this.doc.getElementById("toolbox-close");
|
|
|
|
closeButton.addEventListener("command", this.destroy, true);
|
|
|
|
|
|
|
|
this._buildDockButtons();
|
2013-04-15 12:34:48 +00:00
|
|
|
this._buildOptions();
|
2012-12-13 13:03:55 +00:00
|
|
|
this._buildTabs();
|
2013-01-04 20:31:38 +00:00
|
|
|
this._buildButtons();
|
2013-01-13 08:52:03 +00:00
|
|
|
this._addKeysToWindow();
|
2012-12-13 13:03:55 +00:00
|
|
|
|
2013-05-24 10:26:17 +00:00
|
|
|
this._telemetry.toolOpened("toolbox");
|
|
|
|
|
2012-12-13 13:03:55 +00:00
|
|
|
this.selectTool(this._defaultToolId).then(function(panel) {
|
|
|
|
this.emit("ready");
|
|
|
|
deferred.resolve();
|
|
|
|
}.bind(this));
|
2013-05-24 10:26:17 +00:00
|
|
|
};
|
2012-12-13 13:03:55 +00:00
|
|
|
|
2013-01-04 11:54:54 +00:00
|
|
|
iframe.addEventListener("DOMContentLoaded", domReady, true);
|
2012-11-30 08:07:59 +00:00
|
|
|
iframe.setAttribute("src", this._URL);
|
2013-05-24 10:26:17 +00:00
|
|
|
});
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2012-12-13 13:03:55 +00:00
|
|
|
return deferred.promise;
|
2012-11-30 08:07:59 +00:00
|
|
|
},
|
|
|
|
|
2013-04-15 12:34:48 +00:00
|
|
|
_buildOptions: function TBOX__buildOptions() {
|
|
|
|
let key = this.doc.getElementById("toolbox-options-key");
|
|
|
|
key.addEventListener("command", function(toolId) {
|
|
|
|
this.selectTool(toolId);
|
|
|
|
}.bind(this, "options"), true);
|
|
|
|
},
|
|
|
|
|
2013-01-13 08:52:03 +00:00
|
|
|
/**
|
|
|
|
* Adds the keys and commands to the Toolbox Window in window mode.
|
|
|
|
*/
|
|
|
|
_addKeysToWindow: function TBOX__addKeysToWindow() {
|
|
|
|
if (this.hostType != Toolbox.HostType.WINDOW) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let doc = this.doc.defaultView.parent.document;
|
2013-04-15 12:34:48 +00:00
|
|
|
for (let [id, toolDefinition] of gDevTools.getToolDefinitionMap()) {
|
2013-01-13 08:52:03 +00:00
|
|
|
if (toolDefinition.key) {
|
|
|
|
// Prevent multiple entries for the same tool.
|
|
|
|
if (doc.getElementById("key_" + id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let key = doc.createElement("key");
|
|
|
|
key.id = "key_" + id;
|
|
|
|
|
|
|
|
if (toolDefinition.key.startsWith("VK_")) {
|
|
|
|
key.setAttribute("keycode", toolDefinition.key);
|
|
|
|
} else {
|
|
|
|
key.setAttribute("key", toolDefinition.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
key.setAttribute("modifiers", toolDefinition.modifiers);
|
|
|
|
key.setAttribute("oncommand", "void(0);"); // needed. See bug 371900
|
|
|
|
key.addEventListener("command", function(toolId) {
|
|
|
|
this.selectTool(toolId);
|
|
|
|
}.bind(this, id), true);
|
|
|
|
doc.getElementById("toolbox-keyset").appendChild(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
/**
|
|
|
|
* Build the buttons for changing hosts. Called every time
|
|
|
|
* the host changes.
|
|
|
|
*/
|
|
|
|
_buildDockButtons: function TBOX_createDockButtons() {
|
|
|
|
let dockBox = this.doc.getElementById("toolbox-dock-buttons");
|
|
|
|
|
|
|
|
while (dockBox.firstChild) {
|
|
|
|
dockBox.removeChild(dockBox.firstChild);
|
|
|
|
}
|
|
|
|
|
2012-12-15 01:10:43 +00:00
|
|
|
if (!this._target.isLocalTab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-21 15:22:47 +00:00
|
|
|
let closeButton = this.doc.getElementById("toolbox-close");
|
|
|
|
if (this.hostType === this.HostType.WINDOW) {
|
|
|
|
closeButton.setAttribute("hidden", "true");
|
|
|
|
} else {
|
|
|
|
closeButton.removeAttribute("hidden");
|
|
|
|
}
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
let sideEnabled = Services.prefs.getBoolPref(this._prefs.SIDE_ENABLED);
|
|
|
|
|
|
|
|
for each (let position in this.HostType) {
|
|
|
|
if (position == this.hostType ||
|
|
|
|
(!sideEnabled && position == this.HostType.SIDE)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let button = this.doc.createElement("toolbarbutton");
|
|
|
|
button.id = "toolbox-dock-" + position;
|
|
|
|
button.className = "toolbox-dock-button";
|
2012-12-18 15:11:27 +00:00
|
|
|
button.setAttribute("tooltiptext", toolboxStrings("toolboxDockButtons." +
|
|
|
|
position + ".tooltip"));
|
2012-11-30 08:07:59 +00:00
|
|
|
button.addEventListener("command", function(position) {
|
2012-12-13 13:03:55 +00:00
|
|
|
this.switchHost(position);
|
2012-11-30 08:07:59 +00:00
|
|
|
}.bind(this, position));
|
|
|
|
|
|
|
|
dockBox.appendChild(button);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add tabs to the toolbox UI for registered tools
|
|
|
|
*/
|
|
|
|
_buildTabs: function TBOX_buildTabs() {
|
2013-01-11 12:16:31 +00:00
|
|
|
for (let definition of gDevTools.getToolDefinitionArray()) {
|
2012-11-30 08:07:59 +00:00
|
|
|
this._buildTabForTool(definition);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add buttons to the UI as specified in the devtools.window.toolbarSpec pref
|
|
|
|
*/
|
2013-01-04 20:31:38 +00:00
|
|
|
_buildButtons: function TBOX_buildButtons() {
|
|
|
|
if (!this.target.isLocalTab) {
|
2012-12-13 13:03:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
let toolbarSpec = CommandUtils.getCommandbarSpec("devtools.toolbox.toolbarSpec");
|
2013-05-21 09:18:56 +00:00
|
|
|
let env = CommandUtils.createEnvironment(this.target.tab.ownerDocument,
|
|
|
|
this.target.window.document);
|
|
|
|
let requisition = new Requisition(env);
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2012-12-18 20:47:39 +00:00
|
|
|
let buttons = CommandUtils.createButtons(toolbarSpec, this._target, this.doc, requisition);
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
let container = this.doc.getElementById("toolbox-buttons");
|
2013-05-24 10:26:17 +00:00
|
|
|
buttons.forEach(container.appendChild.bind(container));
|
2012-11-30 08:07:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a tab for one tool definition and add to the toolbox
|
|
|
|
*
|
|
|
|
* @param {string} toolDefinition
|
|
|
|
* Tool definition of the tool to build a tab for.
|
|
|
|
*/
|
|
|
|
_buildTabForTool: function TBOX_buildTabForTool(toolDefinition) {
|
|
|
|
if (!toolDefinition.isTargetSupported(this._target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tabs = this.doc.getElementById("toolbox-tabs");
|
|
|
|
let deck = this.doc.getElementById("toolbox-deck");
|
|
|
|
|
|
|
|
let id = toolDefinition.id;
|
|
|
|
|
|
|
|
let radio = this.doc.createElement("radio");
|
2013-05-22 23:24:22 +00:00
|
|
|
// The radio element is not being used in the conventional way, thus
|
|
|
|
// the devtools-tab class replaces the radio XBL binding with its base
|
|
|
|
// binding (the control-item binding).
|
2012-11-30 08:07:59 +00:00
|
|
|
radio.className = "toolbox-tab devtools-tab";
|
|
|
|
radio.id = "toolbox-tab-" + id;
|
|
|
|
radio.setAttribute("toolid", id);
|
2013-04-30 16:58:04 +00:00
|
|
|
if (toolDefinition.ordinal == undefined || toolDefinition.ordinal < 0) {
|
|
|
|
toolDefinition.ordinal = MAX_ORDINAL;
|
|
|
|
}
|
|
|
|
radio.setAttribute("ordinal", toolDefinition.ordinal);
|
2012-12-18 15:11:27 +00:00
|
|
|
radio.setAttribute("tooltiptext", toolDefinition.tooltip);
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
radio.addEventListener("command", function(id) {
|
|
|
|
this.selectTool(id);
|
|
|
|
}.bind(this, id));
|
|
|
|
|
2013-05-11 01:53:58 +00:00
|
|
|
// spacer lets us center the image and label, while allowing cropping
|
|
|
|
let spacer = this.doc.createElement("spacer");
|
|
|
|
spacer.setAttribute("flex", "1");
|
|
|
|
radio.appendChild(spacer);
|
|
|
|
|
2013-03-13 23:10:15 +00:00
|
|
|
if (toolDefinition.icon) {
|
|
|
|
let image = this.doc.createElement("image");
|
2013-05-25 08:05:34 +00:00
|
|
|
image.className = "default-icon";
|
|
|
|
image.setAttribute("src",
|
|
|
|
toolDefinition.icon || toolDefinition.highlightedicon);
|
|
|
|
radio.appendChild(image);
|
|
|
|
// Adding the highlighted icon image
|
|
|
|
image = this.doc.createElement("image");
|
|
|
|
image.className = "highlighted-icon";
|
|
|
|
image.setAttribute("src",
|
|
|
|
toolDefinition.highlightedicon || toolDefinition.icon);
|
2013-03-13 23:10:15 +00:00
|
|
|
radio.appendChild(image);
|
|
|
|
}
|
|
|
|
|
2013-05-04 06:31:07 +00:00
|
|
|
if (toolDefinition.label) {
|
|
|
|
let label = this.doc.createElement("label");
|
|
|
|
label.setAttribute("value", toolDefinition.label)
|
|
|
|
label.setAttribute("crop", "end");
|
|
|
|
label.setAttribute("flex", "1");
|
|
|
|
radio.appendChild(label);
|
|
|
|
radio.setAttribute("flex", "1");
|
|
|
|
}
|
2013-03-13 23:10:15 +00:00
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
let vbox = this.doc.createElement("vbox");
|
|
|
|
vbox.className = "toolbox-panel";
|
|
|
|
vbox.id = "toolbox-panel-" + id;
|
|
|
|
|
2013-04-30 16:58:04 +00:00
|
|
|
|
|
|
|
// If there is no tab yet, or the ordinal to be added is the largest one.
|
|
|
|
if (tabs.childNodes.length == 0 ||
|
|
|
|
+tabs.lastChild.getAttribute("ordinal") <= toolDefinition.ordinal) {
|
|
|
|
tabs.appendChild(radio);
|
|
|
|
deck.appendChild(vbox);
|
|
|
|
}
|
|
|
|
// else, iterate over all the tabs to get the correct location.
|
|
|
|
else {
|
|
|
|
Array.some(tabs.childNodes, (node, i) => {
|
|
|
|
if (+node.getAttribute("ordinal") > toolDefinition.ordinal) {
|
|
|
|
tabs.insertBefore(radio, node);
|
2013-05-04 06:31:07 +00:00
|
|
|
deck.insertBefore(vbox, deck.childNodes[i]);
|
2013-04-30 16:58:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-01-13 08:52:03 +00:00
|
|
|
|
|
|
|
this._addKeysToWindow();
|
2012-11-30 08:07:59 +00:00
|
|
|
},
|
|
|
|
|
2013-05-14 22:25:28 +00:00
|
|
|
/**
|
2013-06-23 03:00:51 +00:00
|
|
|
* Ensure the tool with the given id is loaded.
|
2013-05-14 22:25:28 +00:00
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* The id of the tool to load.
|
|
|
|
*/
|
|
|
|
loadTool: function TBOX_loadTool(id) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
let iframe = this.doc.getElementById("toolbox-panel-iframe-" + id);
|
|
|
|
|
|
|
|
if (iframe) {
|
2013-06-23 03:00:51 +00:00
|
|
|
let panel = this._toolPanels.get(id);
|
|
|
|
if (panel) {
|
|
|
|
deferred.resolve(panel);
|
|
|
|
} else {
|
|
|
|
this.once(id + "-ready", (panel) => {
|
|
|
|
deferred.resolve(panel);
|
|
|
|
});
|
|
|
|
}
|
2013-05-14 22:25:28 +00:00
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
let definition = gDevTools.getToolDefinitionMap().get(id);
|
2013-06-23 03:00:51 +00:00
|
|
|
if (!definition) {
|
|
|
|
deferred.reject(new Error("no such tool id "+id));
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
2013-05-14 22:25:28 +00:00
|
|
|
iframe = this.doc.createElement("iframe");
|
|
|
|
iframe.className = "toolbox-panel-iframe";
|
|
|
|
iframe.id = "toolbox-panel-iframe-" + id;
|
|
|
|
iframe.setAttribute("flex", 1);
|
|
|
|
iframe.setAttribute("forceOwnRefreshDriver", "");
|
|
|
|
iframe.tooltip = "aHTMLTooltip";
|
|
|
|
|
|
|
|
let vbox = this.doc.getElementById("toolbox-panel-" + id);
|
|
|
|
vbox.appendChild(iframe);
|
|
|
|
|
|
|
|
let onLoad = () => {
|
|
|
|
iframe.removeEventListener("DOMContentLoaded", onLoad, true);
|
|
|
|
|
|
|
|
let built = definition.build(iframe.contentWindow, this);
|
|
|
|
Promise.resolve(built).then((panel) => {
|
|
|
|
this._toolPanels.set(id, panel);
|
|
|
|
this.emit(id + "-ready", panel);
|
|
|
|
gDevTools.emit(id + "-ready", this, panel);
|
|
|
|
deferred.resolve(panel);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
iframe.addEventListener("DOMContentLoaded", onLoad, true);
|
|
|
|
iframe.setAttribute("src", definition.url);
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
/**
|
|
|
|
* Switch to the tool with the given id
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* The id of the tool to switch to
|
|
|
|
*/
|
|
|
|
selectTool: function TBOX_selectTool(id) {
|
2013-03-13 23:10:15 +00:00
|
|
|
let selected = this.doc.querySelector(".devtools-tab[selected]");
|
|
|
|
if (selected) {
|
|
|
|
selected.removeAttribute("selected");
|
|
|
|
}
|
|
|
|
let tab = this.doc.getElementById("toolbox-tab-" + id);
|
|
|
|
tab.setAttribute("selected", "true");
|
|
|
|
|
2013-05-24 10:26:17 +00:00
|
|
|
let prevToolId = this._currentToolId;
|
|
|
|
|
2013-01-09 09:32:35 +00:00
|
|
|
if (this._currentToolId == id) {
|
2013-03-07 07:30:03 +00:00
|
|
|
// Return the existing panel in order to have a consistent return value.
|
|
|
|
return Promise.resolve(this._toolPanels.get(id));
|
2013-01-09 09:32:35 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
if (!this.isReady) {
|
|
|
|
throw new Error("Can't select tool, wait for toolbox 'ready' event");
|
|
|
|
}
|
|
|
|
let tab = this.doc.getElementById("toolbox-tab-" + id);
|
|
|
|
|
2013-05-24 10:26:17 +00:00
|
|
|
if (tab) {
|
|
|
|
if (prevToolId) {
|
|
|
|
this._telemetry.toolClosed(prevToolId);
|
|
|
|
}
|
|
|
|
this._telemetry.toolOpened(id);
|
|
|
|
} else {
|
2012-11-30 08:07:59 +00:00
|
|
|
throw new Error("No tool found");
|
|
|
|
}
|
|
|
|
|
|
|
|
let tabstrip = this.doc.getElementById("toolbox-tabs");
|
|
|
|
|
2013-05-04 06:31:07 +00:00
|
|
|
// select the right tab, making 0th index the default tab if right tab not
|
|
|
|
// found
|
|
|
|
let index = 0;
|
2012-11-30 08:07:59 +00:00
|
|
|
let tabs = tabstrip.childNodes;
|
|
|
|
for (let i = 0; i < tabs.length; i++) {
|
|
|
|
if (tabs[i] === tab) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-15 12:34:48 +00:00
|
|
|
tabstrip.selectedItem = tab;
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
// and select the right iframe
|
|
|
|
let deck = this.doc.getElementById("toolbox-deck");
|
2013-05-04 06:31:07 +00:00
|
|
|
deck.selectedIndex = index;
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-01-09 09:32:35 +00:00
|
|
|
this._currentToolId = id;
|
2013-04-15 12:34:48 +00:00
|
|
|
if (id != "options") {
|
|
|
|
Services.prefs.setCharPref(this._prefs.LAST_TOOL, id);
|
|
|
|
}
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-06-23 03:00:51 +00:00
|
|
|
return this.loadTool(id).then((panel) => {
|
|
|
|
this.emit("select", id);
|
|
|
|
this.emit(id + "-selected", panel);
|
|
|
|
return panel;
|
|
|
|
});
|
2012-11-30 08:07:59 +00:00
|
|
|
},
|
|
|
|
|
2013-05-25 08:05:34 +00:00
|
|
|
/**
|
|
|
|
* Highlights the tool's tab if it is not the currently selected tool.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* The id of the tool to highlight
|
|
|
|
*/
|
|
|
|
highlightTool: function TBOX_highlightTool(id) {
|
|
|
|
let tab = this.doc.getElementById("toolbox-tab-" + id);
|
|
|
|
tab && tab.classList.add("highlighted");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* De-highlights the tool's tab.
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* The id of the tool to unhighlight
|
|
|
|
*/
|
|
|
|
unhighlightTool: function TBOX_unhighlightTool(id) {
|
|
|
|
let tab = this.doc.getElementById("toolbox-tab-" + id);
|
|
|
|
tab && tab.classList.remove("highlighted");
|
|
|
|
},
|
|
|
|
|
2013-01-09 09:32:35 +00:00
|
|
|
/**
|
|
|
|
* Raise the toolbox host.
|
|
|
|
*/
|
|
|
|
raise: function TBOX_raise() {
|
|
|
|
this._host.raise();
|
|
|
|
},
|
|
|
|
|
2013-02-26 12:40:19 +00:00
|
|
|
/**
|
|
|
|
* Refresh the host's title.
|
|
|
|
*/
|
|
|
|
_refreshHostTitle: function TBOX_refreshHostTitle() {
|
|
|
|
let toolName;
|
|
|
|
let toolId = this.currentToolId;
|
2013-04-15 12:34:48 +00:00
|
|
|
let toolDef = gDevTools.getToolDefinitionMap().get(toolId);
|
|
|
|
if (toolDef) {
|
2013-02-26 12:40:19 +00:00
|
|
|
toolName = toolDef.label;
|
|
|
|
} else {
|
|
|
|
// no tool is selected
|
|
|
|
toolName = toolboxStrings("toolbox.defaultTitle");
|
|
|
|
}
|
|
|
|
let title = toolboxStrings("toolbox.titleTemplate",
|
|
|
|
toolName, this.target.url);
|
|
|
|
this._host.setTitle(title);
|
|
|
|
},
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
/**
|
|
|
|
* Create a host object based on the given host type.
|
|
|
|
*
|
2013-01-11 17:31:09 +00:00
|
|
|
* Warning: some hosts require that the toolbox target provides a reference to
|
|
|
|
* the attached tab. Not all Targets have a tab property - make sure you correctly
|
|
|
|
* mix and match hosts and targets.
|
|
|
|
*
|
2012-11-30 08:07:59 +00:00
|
|
|
* @param {string} hostType
|
|
|
|
* The host type of the new host object
|
|
|
|
*
|
|
|
|
* @return {Host} host
|
|
|
|
* The created host object
|
|
|
|
*/
|
|
|
|
_createHost: function TBOX_createHost(hostType) {
|
|
|
|
if (!Hosts[hostType]) {
|
|
|
|
throw new Error('Unknown hostType: '+ hostType);
|
|
|
|
}
|
2013-01-04 20:31:38 +00:00
|
|
|
let newHost = new Hosts[hostType](this.target.tab);
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
// clean up the toolbox if its window is closed
|
|
|
|
newHost.on("window-closed", this.destroy);
|
|
|
|
|
|
|
|
return newHost;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to a new host for the toolbox UI. E.g.
|
|
|
|
* bottom, sidebar, separate window.
|
|
|
|
*
|
|
|
|
* @param {string} hostType
|
|
|
|
* The host type of the new host object
|
|
|
|
*/
|
2012-12-13 13:03:55 +00:00
|
|
|
switchHost: function TBOX_switchHost(hostType) {
|
2012-11-30 08:07:59 +00:00
|
|
|
if (hostType == this._host.type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-15 01:10:43 +00:00
|
|
|
if (!this._target.isLocalTab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
let newHost = this._createHost(hostType);
|
2013-01-04 11:54:54 +00:00
|
|
|
return newHost.create().then(function(iframe) {
|
2012-11-30 08:07:59 +00:00
|
|
|
// change toolbox document's parent to the new host
|
2012-12-13 13:03:55 +00:00
|
|
|
iframe.QueryInterface(Ci.nsIFrameLoaderOwner);
|
2012-11-30 08:07:59 +00:00
|
|
|
iframe.swapFrameLoaders(this.frame);
|
|
|
|
|
|
|
|
this._host.off("window-closed", this.destroy);
|
|
|
|
this._host.destroy();
|
|
|
|
|
|
|
|
this._host = newHost;
|
|
|
|
|
|
|
|
Services.prefs.setCharPref(this._prefs.LAST_HOST, this._host.type);
|
|
|
|
|
|
|
|
this._buildDockButtons();
|
2013-01-13 08:52:03 +00:00
|
|
|
this._addKeysToWindow();
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
this.emit("host-changed");
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the tool-registered event.
|
|
|
|
* @param {string} event
|
|
|
|
* Name of the event ("tool-registered")
|
|
|
|
* @param {string} toolId
|
|
|
|
* Id of the tool that was registered
|
|
|
|
*/
|
|
|
|
_toolRegistered: function TBOX_toolRegistered(event, toolId) {
|
2013-01-11 12:16:31 +00:00
|
|
|
let defs = gDevTools.getToolDefinitionMap();
|
2012-11-30 08:07:59 +00:00
|
|
|
let tool = defs.get(toolId);
|
|
|
|
|
|
|
|
this._buildTabForTool(tool);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the tool-unregistered event.
|
|
|
|
* @param {string} event
|
|
|
|
* Name of the event ("tool-unregistered")
|
2013-04-19 13:44:38 +00:00
|
|
|
* @param {string|object} toolId
|
|
|
|
* Definition or id of the tool that was unregistered. Passing the
|
|
|
|
* tool id should be avoided as it is a temporary measure.
|
2012-11-30 08:07:59 +00:00
|
|
|
*/
|
|
|
|
_toolUnregistered: function TBOX_toolUnregistered(event, toolId) {
|
2013-04-19 13:44:38 +00:00
|
|
|
if (typeof toolId != "string") {
|
|
|
|
toolId = toolId.id;
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:58:41 +00:00
|
|
|
if (this._toolPanels.has(toolId)) {
|
|
|
|
let instance = this._toolPanels.get(toolId);
|
|
|
|
instance.destroy();
|
|
|
|
this._toolPanels.delete(toolId);
|
|
|
|
}
|
|
|
|
|
2012-11-30 08:07:59 +00:00
|
|
|
let radio = this.doc.getElementById("toolbox-tab-" + toolId);
|
|
|
|
let panel = this.doc.getElementById("toolbox-panel-" + toolId);
|
|
|
|
|
|
|
|
if (radio) {
|
2013-01-05 00:21:27 +00:00
|
|
|
if (this._currentToolId == toolId) {
|
|
|
|
let nextToolName = null;
|
|
|
|
if (radio.nextSibling) {
|
|
|
|
nextToolName = radio.nextSibling.getAttribute("toolid");
|
|
|
|
}
|
|
|
|
if (radio.previousSibling) {
|
|
|
|
nextToolName = radio.previousSibling.getAttribute("toolid");
|
|
|
|
}
|
|
|
|
if (nextToolName) {
|
|
|
|
this.selectTool(nextToolName);
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 08:07:59 +00:00
|
|
|
radio.parentNode.removeChild(radio);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (panel) {
|
|
|
|
panel.parentNode.removeChild(panel);
|
|
|
|
}
|
|
|
|
|
2013-01-13 08:52:03 +00:00
|
|
|
if (this.hostType == Toolbox.HostType.WINDOW) {
|
|
|
|
let doc = this.doc.defaultView.parent.document;
|
2013-05-04 06:31:07 +00:00
|
|
|
let key = doc.getElementById("key_" + toolId);
|
2013-01-13 08:52:03 +00:00
|
|
|
if (key) {
|
|
|
|
key.parentNode.removeChild(key);
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 08:07:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the toolbox's notification box
|
|
|
|
*
|
|
|
|
* @return The notification box element.
|
|
|
|
*/
|
|
|
|
getNotificationBox: function TBOX_getNotificationBox() {
|
|
|
|
return this.doc.getElementById("toolbox-notificationbox");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all UI elements, detach from target and clear up
|
|
|
|
*/
|
|
|
|
destroy: function TBOX_destroy() {
|
2012-12-13 13:03:55 +00:00
|
|
|
// If several things call destroy then we give them all the same
|
|
|
|
// destruction promise so we're sure to destroy only once
|
|
|
|
if (this._destroyer) {
|
|
|
|
return this._destroyer;
|
2012-11-30 08:07:59 +00:00
|
|
|
}
|
2013-02-26 12:40:19 +00:00
|
|
|
// Assign the "_destroyer" property before calling the other
|
|
|
|
// destroyer methods to guarantee that the Toolbox's destroy
|
|
|
|
// method is only executed once.
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
this._destroyer = deferred.promise;
|
|
|
|
|
|
|
|
this._target.off("navigate", this._refreshHostTitle);
|
|
|
|
this.off("select", this._refreshHostTitle);
|
|
|
|
this.off("host-changed", this._refreshHostTitle);
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-03-07 07:30:03 +00:00
|
|
|
gDevTools.off("tool-registered", this._toolRegistered);
|
|
|
|
gDevTools.off("tool-unregistered", this._toolUnregistered);
|
2012-12-13 13:03:55 +00:00
|
|
|
|
2013-05-31 15:52:06 +00:00
|
|
|
// Revert docShell.allowJavascript back to it's original value if it was
|
|
|
|
// changed via the Disable JS option.
|
|
|
|
if (typeof this._origAllowJavascript != "undefined") {
|
|
|
|
let docShell = this._host.hostTab.linkedBrowser.docShell;
|
|
|
|
docShell.allowJavascript = this._origAllowJavascript;
|
|
|
|
delete this._origAllowJavascript;
|
|
|
|
}
|
|
|
|
|
2013-03-07 07:30:03 +00:00
|
|
|
let outstanding = [];
|
2012-11-30 08:07:59 +00:00
|
|
|
|
|
|
|
for (let [id, panel] of this._toolPanels) {
|
2012-12-13 13:03:55 +00:00
|
|
|
outstanding.push(panel.destroy());
|
2012-11-30 08:07:59 +00:00
|
|
|
}
|
|
|
|
|
2013-03-25 03:39:00 +00:00
|
|
|
let container = this.doc.getElementById("toolbox-buttons");
|
|
|
|
while(container.firstChild) {
|
|
|
|
container.removeChild(container.firstChild);
|
|
|
|
}
|
|
|
|
|
2012-12-13 13:03:55 +00:00
|
|
|
outstanding.push(this._host.destroy());
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-05-24 10:26:17 +00:00
|
|
|
this._telemetry.destroy();
|
|
|
|
|
2013-03-07 07:30:03 +00:00
|
|
|
// Targets need to be notified that the toolbox is being torn down, so that
|
|
|
|
// remote protocol connections can be gracefully terminated.
|
|
|
|
if (this._target) {
|
|
|
|
this._target.off("close", this.destroy);
|
|
|
|
outstanding.push(this._target.destroy());
|
|
|
|
}
|
|
|
|
this._target = null;
|
2012-11-30 08:07:59 +00:00
|
|
|
|
2013-02-26 12:40:19 +00:00
|
|
|
Promise.all(outstanding).then(function() {
|
2012-12-13 13:03:55 +00:00
|
|
|
this.emit("destroyed");
|
2013-03-25 03:39:00 +00:00
|
|
|
// Free _host after the call to destroyed in order to let a chance
|
|
|
|
// to destroyed listeners to still query toolbox attributes
|
|
|
|
this._host = null;
|
2013-02-26 12:40:19 +00:00
|
|
|
deferred.resolve();
|
2012-12-13 13:03:55 +00:00
|
|
|
}.bind(this));
|
|
|
|
|
2013-02-26 12:40:19 +00:00
|
|
|
return this._destroyer;
|
2012-11-30 08:07:59 +00:00
|
|
|
}
|
|
|
|
};
|