2012-11-30 00:07:59 -08: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";
|
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
this.EXPORTED_SYMBOLS = [ "gDevTools", "DevTools", "gDevToolsBrowser" ];
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-12-13 05:03:55 -08:00
|
|
|
Cu.import("resource://gre/modules/commonjs/promise/core.js");
|
2012-11-30 00:07:59 -08:00
|
|
|
Cu.import("resource:///modules/devtools/EventEmitter.jsm");
|
|
|
|
Cu.import("resource:///modules/devtools/ToolDefinitions.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Toolbox",
|
|
|
|
"resource:///modules/devtools/Toolbox.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "TargetFactory",
|
|
|
|
"resource:///modules/devtools/Target.jsm");
|
|
|
|
|
|
|
|
const FORBIDDEN_IDS = new Set("toolbox", "");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DevTools is a class that represents a set of developer tools, it holds a
|
|
|
|
* set of tools and keeps track of open toolboxes in the browser.
|
|
|
|
*/
|
|
|
|
this.DevTools = function DevTools() {
|
2013-01-11 04:16:24 -08:00
|
|
|
this._tools = new Map(); // Map<toolId, tool>
|
|
|
|
this._toolboxes = new Map(); // Map<target, toolbox>
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
// destroy() is an observer's handler so we need to preserve context.
|
|
|
|
this.destroy = this.destroy.bind(this);
|
|
|
|
|
2012-12-13 23:05:00 -08:00
|
|
|
EventEmitter.decorate(this);
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
Services.obs.addObserver(this.destroy, "quit-application", false);
|
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
// Register the set of default tools
|
2012-11-30 00:07:59 -08:00
|
|
|
for (let definition of defaultTools) {
|
|
|
|
this.registerTool(definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DevTools.prototype = {
|
|
|
|
/**
|
|
|
|
* Register a new developer tool.
|
|
|
|
*
|
|
|
|
* A definition is a light object that holds different information about a
|
|
|
|
* developer tool. This object is not supposed to have any operational code.
|
|
|
|
* See it as a "manifest".
|
|
|
|
* The only actual code lives in the build() function, which will be used to
|
|
|
|
* start an instance of this tool.
|
|
|
|
*
|
|
|
|
* Each toolDefinition has the following properties:
|
|
|
|
* - id: Unique identifier for this tool (string|required)
|
|
|
|
* - killswitch: Property name to allow us to turn this tool on/off globally
|
|
|
|
* (string|required) (TODO: default to devtools.{id}.enabled?)
|
|
|
|
* - icon: URL pointing to a graphic which will be used as the src for an
|
|
|
|
* 16x16 img tag (string|required)
|
|
|
|
* - url: URL pointing to a XUL/XHTML document containing the user interface
|
|
|
|
* (string|required)
|
|
|
|
* - label: Localized name for the tool to be displayed to the user
|
|
|
|
* (string|required)
|
|
|
|
* - build: Function that takes an iframe, which has been populated with the
|
|
|
|
* markup from |url|, and also the toolbox containing the panel.
|
|
|
|
* And returns an instance of ToolPanel (function|required)
|
|
|
|
*/
|
|
|
|
registerTool: function DT_registerTool(toolDefinition) {
|
|
|
|
let toolId = toolDefinition.id;
|
|
|
|
|
|
|
|
if (!toolId || FORBIDDEN_IDS.has(toolId)) {
|
|
|
|
throw new Error("Invalid definition.id");
|
|
|
|
}
|
|
|
|
|
|
|
|
toolDefinition.killswitch = toolDefinition.killswitch ||
|
2012-12-13 05:03:55 -08:00
|
|
|
"devtools." + toolId + ".enabled";
|
2012-11-30 00:07:59 -08:00
|
|
|
this._tools.set(toolId, toolDefinition);
|
|
|
|
|
|
|
|
this.emit("tool-registered", toolId);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all tools that match the given |toolId|
|
|
|
|
* Needed so that add-ons can remove themselves when they are deactivated
|
|
|
|
*
|
|
|
|
* @param {string} toolId
|
|
|
|
* id of the tool to unregister
|
2013-01-11 04:16:24 -08:00
|
|
|
* @param {boolean} isQuitApplication
|
|
|
|
* true to indicate that the call is due to app quit, so we should not
|
|
|
|
* cause a cascade of costly events
|
2012-11-30 00:07:59 -08:00
|
|
|
*/
|
2013-01-11 04:16:24 -08:00
|
|
|
unregisterTool: function DT_unregisterTool(toolId, isQuitApplication) {
|
2012-11-30 00:07:59 -08:00
|
|
|
this._tools.delete(toolId);
|
|
|
|
|
2013-01-11 04:16:24 -08:00
|
|
|
if (!isQuitApplication) {
|
|
|
|
this.emit("tool-unregistered", toolId);
|
|
|
|
}
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow ToolBoxes to get at the list of tools that they should populate
|
|
|
|
* themselves with.
|
|
|
|
*
|
|
|
|
* @return {Map} tools
|
|
|
|
* A map of the the tool definitions registered in this instance
|
|
|
|
*/
|
2013-01-11 04:16:31 -08:00
|
|
|
getToolDefinitionMap: function DT_getToolDefinitionMap() {
|
2012-11-30 00:07:59 -08:00
|
|
|
let tools = new Map();
|
|
|
|
|
|
|
|
for (let [key, value] of this._tools) {
|
|
|
|
let enabled;
|
|
|
|
|
|
|
|
try {
|
|
|
|
enabled = Services.prefs.getBoolPref(value.killswitch);
|
|
|
|
} catch(e) {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
tools.set(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tools;
|
|
|
|
},
|
|
|
|
|
2013-01-11 04:16:31 -08:00
|
|
|
/**
|
|
|
|
* Tools have an inherent ordering that can't be represented in a Map so
|
|
|
|
* getToolDefinitionArray provides an alternative representation of the
|
|
|
|
* definitions sorted by ordinal value.
|
|
|
|
*
|
|
|
|
* @return {Array} tools
|
|
|
|
* A sorted array of the tool definitions registered in this instance
|
|
|
|
*/
|
|
|
|
getToolDefinitionArray: function DT_getToolDefinitionArray() {
|
|
|
|
const MAX_ORDINAL = 99;
|
|
|
|
|
|
|
|
let definitions = [];
|
|
|
|
for (let [id, definition] of this.getToolDefinitionMap()) {
|
|
|
|
definitions.push(definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
definitions.sort(function(d1, d2) {
|
|
|
|
let o1 = (typeof d1.ordinal == "number") ? d1.ordinal : MAX_ORDINAL;
|
|
|
|
let o2 = (typeof d2.ordinal == "number") ? d2.ordinal : MAX_ORDINAL;
|
|
|
|
return o1 - o2;
|
|
|
|
});
|
|
|
|
|
|
|
|
return definitions;
|
|
|
|
},
|
|
|
|
|
2012-11-30 00:07:59 -08:00
|
|
|
/**
|
2012-12-13 05:03:55 -08:00
|
|
|
* Show a Toolbox for a target (either by creating a new one, or if a toolbox
|
|
|
|
* already exists for the target, by bring to the front the existing one)
|
|
|
|
* If |toolId| is specified then the displayed toolbox will have the
|
|
|
|
* specified tool selected.
|
|
|
|
* If |hostType| is specified then the toolbox will be displayed using the
|
|
|
|
* specified HostType.
|
2012-11-30 00:07:59 -08:00
|
|
|
*
|
|
|
|
* @param {Target} target
|
|
|
|
* The target the toolbox will debug
|
2012-12-13 05:03:55 -08:00
|
|
|
* @param {string} toolId
|
|
|
|
* The id of the tool to show
|
2012-11-30 00:07:59 -08:00
|
|
|
* @param {Toolbox.HostType} hostType
|
2012-12-13 05:03:55 -08:00
|
|
|
* The type of host (bottom, window, side)
|
2012-11-30 00:07:59 -08:00
|
|
|
*
|
|
|
|
* @return {Toolbox} toolbox
|
|
|
|
* The toolbox that was opened
|
|
|
|
*/
|
2012-12-13 05:03:55 -08:00
|
|
|
showToolbox: function(target, toolId, hostType) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
|
|
|
|
let toolbox = this._toolboxes.get(target);
|
|
|
|
if (toolbox) {
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
let promise = (hostType != null && toolbox.hostType != hostType) ?
|
|
|
|
toolbox.switchHost(hostType) :
|
|
|
|
Promise.resolve(null);
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
if (toolId != null && toolbox.currentToolId != toolId) {
|
|
|
|
promise = promise.then(function() {
|
|
|
|
return toolbox.selectTool(toolId);
|
|
|
|
});
|
|
|
|
}
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
return promise.then(function() {
|
2013-01-09 01:32:35 -08:00
|
|
|
toolbox.raise();
|
2012-12-13 05:03:55 -08:00
|
|
|
return toolbox;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No toolbox for target, create one
|
|
|
|
toolbox = new Toolbox(target, toolId, hostType);
|
|
|
|
|
|
|
|
this._toolboxes.set(target, toolbox);
|
|
|
|
|
|
|
|
toolbox.once("destroyed", function() {
|
|
|
|
this._toolboxes.delete(target);
|
|
|
|
this.emit("toolbox-destroyed", target);
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
// If we were asked for a specific tool then we need to wait for the
|
|
|
|
// tool to be ready, otherwise we can just wait for toolbox open
|
|
|
|
if (toolId != null) {
|
|
|
|
toolbox.once(toolId + "-ready", function(event, panel) {
|
|
|
|
this.emit("toolbox-ready", toolbox);
|
|
|
|
deferred.resolve(toolbox);
|
|
|
|
}.bind(this));
|
|
|
|
toolbox.open();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
toolbox.open().then(function() {
|
|
|
|
deferred.resolve(toolbox);
|
|
|
|
this.emit("toolbox-ready", toolbox);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
return deferred.promise;
|
|
|
|
},
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
/**
|
|
|
|
* Return the toolbox for a given target.
|
|
|
|
*
|
|
|
|
* @param {object} target
|
|
|
|
* Target value e.g. the target that owns this toolbox
|
|
|
|
*
|
|
|
|
* @return {Toolbox} toolbox
|
|
|
|
* The toobox that is debugging the given target
|
|
|
|
*/
|
|
|
|
getToolbox: function DT_getToolbox(target) {
|
|
|
|
return this._toolboxes.get(target);
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the toolbox for a given target
|
|
|
|
*/
|
|
|
|
closeToolbox: function DT_closeToolbox(target) {
|
|
|
|
let toolbox = this._toolboxes.get(target);
|
|
|
|
if (toolbox == null) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-13 05:03:55 -08:00
|
|
|
return toolbox.destroy();
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-12-13 05:03:55 -08:00
|
|
|
* All browser windows have been closed, tidy up remaining objects.
|
2012-11-30 00:07:59 -08:00
|
|
|
*/
|
2012-12-13 05:03:55 -08:00
|
|
|
destroy: function() {
|
|
|
|
Services.obs.removeObserver(this.destroy, "quit-application");
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2013-01-11 04:16:24 -08:00
|
|
|
for (let [key, tool] of this._tools) {
|
|
|
|
this.unregisterTool(key, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleaning down the toolboxes: i.e.
|
|
|
|
// for (let [target, toolbox] of this._toolboxes) toolbox.destroy();
|
|
|
|
// Is taken care of by the gDevToolsBrowser.forgetBrowserWindow
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
2012-12-13 05:03:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gDevTools is a singleton that controls the Firefox Developer Tools.
|
|
|
|
*
|
|
|
|
* It is an instance of a DevTools class that holds a set of tools. It has the
|
|
|
|
* same lifetime as the browser.
|
|
|
|
*/
|
|
|
|
let gDevTools = new DevTools();
|
|
|
|
this.gDevTools = gDevTools;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gDevToolsBrowser exposes functions to connect the gDevTools instance with a
|
|
|
|
* Firefox instance.
|
|
|
|
*/
|
|
|
|
let gDevToolsBrowser = {
|
|
|
|
/**
|
|
|
|
* A record of the windows whose menus we altered, so we can undo the changes
|
|
|
|
* as the window is closed
|
|
|
|
*/
|
|
|
|
_trackedBrowserWindows: new Set(),
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
/**
|
2013-01-09 01:32:35 -08:00
|
|
|
* This function is for the benefit of Tools:DevToolbox in
|
2012-11-30 00:07:59 -08:00
|
|
|
* browser/base/content/browser-sets.inc and should not be used outside
|
|
|
|
* of there
|
|
|
|
*/
|
2013-01-09 01:32:35 -08:00
|
|
|
toggleToolboxCommand: function(gBrowser) {
|
2012-11-30 00:07:59 -08:00
|
|
|
let target = TargetFactory.forTab(gBrowser.selectedTab);
|
2012-12-13 05:03:55 -08:00
|
|
|
let toolbox = gDevTools.getToolbox(target);
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2013-01-09 01:32:35 -08:00
|
|
|
toolbox ? toolbox.destroy() : gDevTools.showToolbox(target);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is for the benefit of Tools:{toolId} commands,
|
|
|
|
* triggered from the WebDeveloper menu and keyboard shortcuts.
|
|
|
|
*
|
|
|
|
* selectToolCommand's behavior:
|
|
|
|
* - if the toolbox is closed,
|
|
|
|
* we open the toolbox and select the tool
|
|
|
|
* - if the toolbox is open, and the targetted tool is not selected,
|
|
|
|
* we select it
|
|
|
|
* - if the toolbox is open, and the targetted tool is selected,
|
|
|
|
* and the host is NOT a window, we close the toolbox
|
|
|
|
* - if the toolbox is open, and the targetted tool is selected,
|
|
|
|
* and the host is a window, we raise the toolbox window
|
|
|
|
*/
|
|
|
|
selectToolCommand: function(gBrowser, toolId) {
|
|
|
|
let target = TargetFactory.forTab(gBrowser.selectedTab);
|
|
|
|
let toolbox = gDevTools.getToolbox(target);
|
|
|
|
|
|
|
|
if (toolbox && toolbox.currentToolId == toolId) {
|
|
|
|
if (toolbox.hostType == Toolbox.HostType.WINDOW) {
|
|
|
|
toolbox.raise();
|
|
|
|
} else {
|
|
|
|
toolbox.destroy();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gDevTools.showToolbox(target, toolId);
|
|
|
|
}
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-12-13 05:03:55 -08:00
|
|
|
* Open a tab to allow connects to a remote browser
|
2012-11-30 00:07:59 -08:00
|
|
|
*/
|
2012-12-13 05:03:55 -08:00
|
|
|
openConnectScreen: function(gBrowser) {
|
|
|
|
gBrowser.selectedTab = gBrowser.addTab("chrome://browser/content/devtools/connect.xhtml");
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add this DevTools's presence to a browser window's document
|
|
|
|
*
|
2012-12-13 05:03:55 -08:00
|
|
|
* @param {XULDocument} doc
|
|
|
|
* The document to which menuitems and handlers are to be added
|
2012-11-30 00:07:59 -08:00
|
|
|
*/
|
|
|
|
registerBrowserWindow: function DT_registerBrowserWindow(win) {
|
2012-12-13 05:03:55 -08:00
|
|
|
gDevToolsBrowser._trackedBrowserWindows.add(win);
|
|
|
|
gDevToolsBrowser._addAllToolsToMenu(win.document);
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
let tabContainer = win.document.getElementById("tabbrowser-tabs")
|
2012-12-13 05:03:55 -08:00
|
|
|
tabContainer.addEventListener("TabSelect",
|
|
|
|
gDevToolsBrowser._updateMenuCheckbox, false);
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the menuitem for a tool to all open browser windows.
|
|
|
|
*
|
|
|
|
* @param {object} toolDefinition
|
|
|
|
* properties of the tool to add
|
|
|
|
*/
|
|
|
|
_addToolToWindows: function DT_addToolToWindows(toolDefinition) {
|
2013-01-11 04:16:31 -08:00
|
|
|
// We need to insert the new tool in the right place, which means knowing
|
|
|
|
// the tool that comes before the tool that we're trying to add
|
|
|
|
let allDefs = gDevTools.getToolDefinitionArray();
|
|
|
|
let prevDef;
|
|
|
|
for (let def of allDefs) {
|
|
|
|
if (def === toolDefinition) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prevDef = def;
|
|
|
|
}
|
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
for (let win of gDevToolsBrowser._trackedBrowserWindows) {
|
2013-01-11 04:16:31 -08:00
|
|
|
let doc = win.document;
|
|
|
|
let elements = gDevToolsBrowser._createToolMenuElements(toolDefinition, doc);
|
|
|
|
|
|
|
|
doc.getElementById("mainCommandSet").appendChild(elements.cmd);
|
|
|
|
|
|
|
|
if (elements.key) {
|
|
|
|
doc.getElementById("mainKeyset").appendChild(elements.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.getElementById("mainBroadcasterSet").appendChild(elements.bc);
|
|
|
|
|
|
|
|
let amp = doc.getElementById("appmenu_webDeveloper_popup");
|
|
|
|
if (amp) {
|
|
|
|
let ref = (prevDef != null) ?
|
|
|
|
doc.getElementById("appmenuitem_" + prevDef.id).nextSibling :
|
|
|
|
doc.getElementById("appmenu_devtools_separator");
|
|
|
|
|
|
|
|
amp.insertBefore(elements.appmenuitem, ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mp = doc.getElementById("menuWebDeveloperPopup");
|
|
|
|
let ref = (prevDef != null) ?
|
|
|
|
doc.getElementById("menuitem_" + prevDef.id).nextSibling :
|
|
|
|
doc.getElementById("menu_devtools_separator");
|
|
|
|
mp.insertBefore(elements.menuitem, ref);
|
2012-11-30 00:07:59 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add all tools to the developer tools menu of a window.
|
|
|
|
*
|
|
|
|
* @param {XULDocument} doc
|
|
|
|
* The document to which the tool items are to be added.
|
|
|
|
*/
|
|
|
|
_addAllToolsToMenu: function DT_addAllToolsToMenu(doc) {
|
|
|
|
let fragCommands = doc.createDocumentFragment();
|
|
|
|
let fragKeys = doc.createDocumentFragment();
|
|
|
|
let fragBroadcasters = doc.createDocumentFragment();
|
|
|
|
let fragAppMenuItems = doc.createDocumentFragment();
|
|
|
|
let fragMenuItems = doc.createDocumentFragment();
|
|
|
|
|
2013-01-11 04:16:31 -08:00
|
|
|
for (let toolDefinition of gDevTools.getToolDefinitionArray()) {
|
|
|
|
let elements = gDevToolsBrowser._createToolMenuElements(toolDefinition, doc);
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2013-01-11 04:16:31 -08:00
|
|
|
if (!elements) {
|
2012-11-30 00:07:59 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-11 04:16:31 -08:00
|
|
|
fragCommands.appendChild(elements.cmd);
|
|
|
|
if (elements.key) {
|
|
|
|
fragKeys.appendChild(elements.key);
|
2012-11-30 00:07:59 -08:00
|
|
|
}
|
2013-01-11 04:16:31 -08:00
|
|
|
fragBroadcasters.appendChild(elements.bc);
|
|
|
|
fragAppMenuItems.appendChild(elements.appmenuitem);
|
|
|
|
fragMenuItems.appendChild(elements.menuitem);
|
2012-11-30 00:07:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let mcs = doc.getElementById("mainCommandSet");
|
|
|
|
mcs.appendChild(fragCommands);
|
|
|
|
|
|
|
|
let mks = doc.getElementById("mainKeyset");
|
|
|
|
mks.appendChild(fragKeys);
|
|
|
|
|
|
|
|
let mbs = doc.getElementById("mainBroadcasterSet");
|
|
|
|
mbs.appendChild(fragBroadcasters);
|
|
|
|
|
|
|
|
let amp = doc.getElementById("appmenu_webDeveloper_popup");
|
|
|
|
if (amp) {
|
|
|
|
let amps = doc.getElementById("appmenu_devtools_separator");
|
|
|
|
amp.insertBefore(fragAppMenuItems, amps);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mp = doc.getElementById("menuWebDeveloperPopup");
|
|
|
|
let mps = doc.getElementById("menu_devtools_separator");
|
|
|
|
mp.insertBefore(fragMenuItems, mps);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a menu entry for a tool definition
|
|
|
|
*
|
|
|
|
* @param {string} toolDefinition
|
|
|
|
* Tool definition of the tool to add a menu entry.
|
|
|
|
* @param {XULDocument} doc
|
|
|
|
* The document to which the tool menu item is to be added.
|
|
|
|
*/
|
2013-01-11 04:16:31 -08:00
|
|
|
_createToolMenuElements: function DT_createToolMenuElements(toolDefinition, doc) {
|
2012-11-30 00:07:59 -08:00
|
|
|
let id = toolDefinition.id;
|
|
|
|
|
|
|
|
// Prevent multiple entries for the same tool.
|
|
|
|
if (doc.getElementById("Tools:" + id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cmd = doc.createElement("command");
|
|
|
|
cmd.id = "Tools:" + id;
|
|
|
|
cmd.setAttribute("oncommand",
|
2013-01-09 01:32:35 -08:00
|
|
|
'gDevToolsBrowser.selectToolCommand(gBrowser, "' + id + '");');
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
let key = null;
|
|
|
|
if (toolDefinition.key) {
|
|
|
|
key = doc.createElement("key");
|
|
|
|
key.id = "key_" + id;
|
|
|
|
|
|
|
|
if (toolDefinition.key.startsWith("VK_")) {
|
|
|
|
key.setAttribute("keycode", toolDefinition.key);
|
|
|
|
} else {
|
|
|
|
key.setAttribute("key", toolDefinition.key);
|
|
|
|
}
|
|
|
|
|
2013-01-09 01:32:35 -08:00
|
|
|
key.setAttribute("command", cmd.id);
|
2012-11-30 00:07:59 -08:00
|
|
|
key.setAttribute("modifiers", toolDefinition.modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
let bc = doc.createElement("broadcaster");
|
|
|
|
bc.id = "devtoolsMenuBroadcaster_" + id;
|
|
|
|
bc.setAttribute("label", toolDefinition.label);
|
2013-01-09 01:32:35 -08:00
|
|
|
bc.setAttribute("command", cmd.id);
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
if (key) {
|
|
|
|
bc.setAttribute("key", "key_" + id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let appmenuitem = doc.createElement("menuitem");
|
|
|
|
appmenuitem.id = "appmenuitem_" + id;
|
|
|
|
appmenuitem.setAttribute("observes", "devtoolsMenuBroadcaster_" + id);
|
|
|
|
|
|
|
|
let menuitem = doc.createElement("menuitem");
|
|
|
|
menuitem.id = "menuitem_" + id;
|
|
|
|
menuitem.setAttribute("observes", "devtoolsMenuBroadcaster_" + id);
|
|
|
|
|
|
|
|
if (toolDefinition.accesskey) {
|
|
|
|
menuitem.setAttribute("accesskey", toolDefinition.accesskey);
|
|
|
|
}
|
|
|
|
|
2013-01-11 04:16:31 -08:00
|
|
|
return {
|
|
|
|
cmd: cmd,
|
|
|
|
key: key,
|
|
|
|
bc: bc,
|
|
|
|
appmenuitem: appmenuitem,
|
|
|
|
menuitem: menuitem
|
|
|
|
};
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-12-17 11:01:31 -08:00
|
|
|
* Update the "Toggle Tools" checkbox in the developer tools menu. This is
|
2012-11-30 00:07:59 -08:00
|
|
|
* called when a toolbox is created or destroyed.
|
|
|
|
*/
|
|
|
|
_updateMenuCheckbox: function DT_updateMenuCheckbox() {
|
2012-12-13 05:03:55 -08:00
|
|
|
for (let win of gDevToolsBrowser._trackedBrowserWindows) {
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
let hasToolbox = false;
|
|
|
|
if (TargetFactory.isKnownTab(win.gBrowser.selectedTab)) {
|
|
|
|
let target = TargetFactory.forTab(win.gBrowser.selectedTab);
|
2012-12-13 05:03:55 -08:00
|
|
|
if (gDevTools._toolboxes.has(target)) {
|
2012-11-30 00:07:59 -08:00
|
|
|
hasToolbox = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let broadcaster = win.document.getElementById("devtoolsMenuBroadcaster_DevToolbox");
|
|
|
|
if (hasToolbox) {
|
|
|
|
broadcaster.setAttribute("checked", "true");
|
|
|
|
} else {
|
|
|
|
broadcaster.removeAttribute("checked");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the menuitem for a tool to all open browser windows.
|
|
|
|
*
|
|
|
|
* @param {object} toolId
|
|
|
|
* id of the tool to remove
|
|
|
|
*/
|
|
|
|
_removeToolFromWindows: function DT_removeToolFromWindows(toolId) {
|
2012-12-13 05:03:55 -08:00
|
|
|
for (let win of gDevToolsBrowser._trackedBrowserWindows) {
|
|
|
|
gDevToolsBrowser._removeToolFromMenu(toolId, win.document);
|
2012-11-30 00:07:59 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a tool's menuitem from a window
|
|
|
|
*
|
|
|
|
* @param {string} toolId
|
|
|
|
* Id of the tool to add a menu entry for
|
|
|
|
* @param {XULDocument} doc
|
|
|
|
* The document to which the tool menu item is to be removed from
|
|
|
|
*/
|
|
|
|
_removeToolFromMenu: function DT_removeToolFromMenu(toolId, doc) {
|
|
|
|
let command = doc.getElementById("Tools:" + toolId);
|
2013-01-04 16:21:27 -08:00
|
|
|
if (command) {
|
|
|
|
command.parentNode.removeChild(command);
|
|
|
|
}
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
let key = doc.getElementById("key_" + toolId);
|
|
|
|
if (key) {
|
|
|
|
key.parentNode.removeChild(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
let bc = doc.getElementById("devtoolsMenuBroadcaster_" + toolId);
|
2013-01-04 16:21:27 -08:00
|
|
|
if (bc) {
|
|
|
|
bc.parentNode.removeChild(bc);
|
|
|
|
}
|
|
|
|
|
|
|
|
let appmenuitem = doc.getElementById("appmenuitem_" + toolId);
|
|
|
|
if (appmenuitem) {
|
|
|
|
appmenuitem.parentNode.removeChild(appmenuitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
let menuitem = doc.getElementById("menuitem_" + toolId);
|
|
|
|
if (menuitem) {
|
|
|
|
menuitem.parentNode.removeChild(menuitem);
|
|
|
|
}
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on browser unload to remove menu entries, toolboxes and event
|
|
|
|
* listeners from the closed browser window.
|
|
|
|
*
|
|
|
|
* @param {XULWindow} win
|
|
|
|
* The window containing the menu entry
|
|
|
|
*/
|
|
|
|
forgetBrowserWindow: function DT_forgetBrowserWindow(win) {
|
2012-12-13 05:03:55 -08:00
|
|
|
gDevToolsBrowser._trackedBrowserWindows.delete(win);
|
2012-11-30 00:07:59 -08:00
|
|
|
|
|
|
|
// Destroy toolboxes for closed window
|
2012-12-13 05:03:55 -08:00
|
|
|
for (let [target, toolbox] of gDevTools._toolboxes) {
|
2012-11-30 00:07:59 -08:00
|
|
|
if (toolbox.frame.ownerDocument.defaultView == win) {
|
|
|
|
toolbox.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let tabContainer = win.document.getElementById("tabbrowser-tabs")
|
|
|
|
tabContainer.removeEventListener("TabSelect",
|
2012-12-13 05:03:55 -08:00
|
|
|
gDevToolsBrowser._updateMenuCheckbox, false);
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All browser windows have been closed, tidy up remaining objects.
|
|
|
|
*/
|
|
|
|
destroy: function() {
|
2012-12-13 05:03:55 -08:00
|
|
|
Services.obs.removeObserver(gDevToolsBrowser.destroy, "quit-application");
|
2012-11-30 00:07:59 -08:00
|
|
|
},
|
2012-12-13 05:03:55 -08:00
|
|
|
}
|
|
|
|
this.gDevToolsBrowser = gDevToolsBrowser;
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
gDevTools.on("tool-registered", function(ev, toolId) {
|
|
|
|
let toolDefinition = gDevTools._tools.get(toolId);
|
|
|
|
gDevToolsBrowser._addToolToWindows(toolDefinition);
|
|
|
|
});
|
2012-11-30 00:07:59 -08:00
|
|
|
|
2012-12-13 05:03:55 -08:00
|
|
|
gDevTools.on("tool-unregistered", function(ev, toolId) {
|
|
|
|
gDevToolsBrowser._removeToolFromWindows(toolId);
|
|
|
|
});
|
|
|
|
|
|
|
|
gDevTools.on("toolbox-ready", gDevToolsBrowser._updateMenuCheckbox);
|
|
|
|
gDevTools.on("toolbox-destroyed", gDevToolsBrowser._updateMenuCheckbox);
|
|
|
|
|
|
|
|
Services.obs.addObserver(gDevToolsBrowser.destroy, "quit-application", false);
|