Files
palemoon27/devtools/client/framework/devtools-browser.js
roytam1 b3d6afc844 import changes from `dev' branch of rmottola/Arctic-Fox:
- bits of Bug 571294 - Part 1: Implement selection events (8af4ef3f65)
- Bug 1208885 - Remove mozGenericWordUtils; r=smaug (b1a529f772)
- fix nsRefPtr/RefPtr (c56979e04d)
- Bug 1243657 spellchecker should check if IME composition works as deleting whole text r=ehsan (8b4d4825b9)
- Bug 1137544 - New scripts to upgrade en-US.dic to the latest upstream version; r=ehsan (47150b3549)
- Bug 1137544 - Upgrade en-US.dic dictionary to upstream version 2015.02.15; r=ehsan (057540f06b)
- Bug 1137544 - Generate 5-mozilla-added and 5-mozilla-removed to aid in submitting changes upstream; r=ehsan (f0d6332df3)
- Bug 1137544 - Update the en-US dictionary based on the SCOWL 2015.04.24 wordlist using the new scripts (e87b0a0285)
- minor test alignment (f3e976adf2)
- Bug 1162823 - Do not treat non-Latin words as misspelled; r=ekanan (c298db7251)
- Bug 1160730 - Add composited, compositing and compositeness to the en-US dictionary. r=ehsan (7d62babbdb)
- Bug 1167912 - Add eldritch to the en-US dictionary. r=ehsan (4d1e08e03b)
- Bug 1168802 - Update the en-US dictionary to SCOWL 2015.05.18 (3daa6d0831)
- Bug 1183765 - Add crappiness to our en-US dictionary. r=ehsan (54ccd28fa2)
- Bug 1192054 - Add subsumption to the en-US dictionary. r=ehsan (8c5aaa92bc)
- Bug 1199540 - Update the en-US dictionary to SCOWL 2015.08.24 (cb21d0092d)
- Bug 1200508 - Add dialogs to the en-US dictionary. r=ehsan (1e69b4d574)
- Bug 1202600 - Add Fukushima to the en-US dictionary. r=ehsan (6ffca1c47b)
- Bug 1213765 - Add preliminarily to the en-US dictionary. r=ehsan (cb12d4979b)
- Bug 1228174 - Add validator to the en-US dictionary. r=ehsan (b356d7f5cb)
- Bug 1199532 - Add disassembly, disassembler to the en-US dictionary. r=ehsan (979bb4f997)
- Bug 1235506 - Correct 353 entries in the en-US dictionary. r=ehsan (efb2f4a890)
- Bug 1238031 - Fix make-new-dict to use a custom en_US dictionary that adds common variants and accented words. r=ehsan (39464a234f)
- Bug 301712 - Remove uncommon proper names, remove unneeded words. r=ehsan (a6ece3925f)
- Bug 1240916 - Part 2: Update the en-US dictionary to SCOWL 2016.01.19 (bb059318e5)
- Bug 1250430 - Remove SrcDir loader support from devtools. r=jryans (0e77cbd85f)
- Bug 1209699 - Add a 'Push' button for service workers in about:debugging. r=jdescottes (a59c681ad1)
- Bug 1247203 - Remove useless call to main(). r=jryans (e89b21e72c)
- Bug 1247203 - Do not hardcode main module path in Loader.jsm. r=jryans (01d4ca73cc)
- Bug 1255266 - create sandbox with system principal. r=jryans,khuey. (0ac3a41816)
- Bug 1240804 - Use common instance of Promise.jsm for test harness promise rejection handling. r=ochameau (5f5383e721)
- Bug 1248603 - Factor out menu and shortcut creation to dedicated module. r=jryans (2e19c46dba)
- Bug 1248603 - Remove support of the appmenu from devtools. r=jryans (3f25c2b181)
- Bug 1248603 - Toggle developer toolbar via the menuitem in tests. r=jryans (01d9940116)
- bits of  Bug 1248603 - Toggle developer toolbar (e062489e6b)
- Bug 1248601 - Register the Developer Toolbar dynamically. r=jwalker (d5e63b9e4b)
- missing bit of 878412 - Use F12 to open the Toolbox (43c193a82a)
- Bug 1252479 - Adding the Developer Dynamically (Bug 1248601) doesn't work in SeaMonkey because it doesn't have a "browser-bottombox" r=ochameau (49fb6e1f17)
- Bug 1248601 - Fix focus race due to dynamic XUL insertion. r=jwalker (35831fd629)
- Bug 1257178 - Prevent "this._input is undefined" exception when developer toolbar is hidden early. r=jwalker (a8c308acd2)
- Bug 1250120 - Change name to Responsive Design Mode in Tools -> Web Developer menu r=jryans (3575acecb2)
- Bug 1239317 - Add a Service Workers entry to the Web Developer menu. r=jryans (ce975756b3)
- Bug 1219804 - Show last 5 recent bookmarks in the bookmarks menu. r=mak (cfad5e41b2)
- Bug 1259398 - Make devtools file-watcher module more generic to watch any directory. r=jlongster (4d37fbf76e)
- Bug 1236452 - Fix DOMHelpers.onceDOMReady against already loaded documents. r=jryans (7abfab41c0)
- Bug 1259241 - Update demangling library to 1.0.1 to fix a memory leak. r=fitzgen (77450e478c)
- Bug 1261092 - Simplify gcli initialization/destruction codepaths. r=jryans,jwalker (b8648df9f1)
- Bug 1258309 - Pull ViewHelpers.Prefs into its own module, r=jsantell (2fe2251e2a)
- Bug 1260711 - Fix error 'IndexSizeError: Index or size is negative or greater than the allowed amount' when using the animation inspector. r=pbro (4d2508de75)
- Bug 1235698 - Increase the timeout of browser_animation_timeline_shows_time_info.js (ae143a305e)
- Bug 1253493 - Part1: Show iterationStart to the tooltip. r=pbro (00d72ea7b0)
- Bug 1253493 - Part2: Show iterationStart to the GUI. r=pbro (1a5c1c4a93)
- Bug 1253494 part 1 - Implement endDelay representation in the animation inspector r=pbro (f5b08cd11d)
- Bug 1253494 part 2 - Add tests for endDelay representation r=pbro (d9acd667d8)
- Bug 1261561 - Part 1: Fix TimeScale.getAnimationDimensions() to handle multiple iterations. r=pbro (0a1e13cbc9)
- Bug 1261561 - Part 2: Add a xpcshell test for TimeScale.getAnimationDimensions(). r=pbro (71a832f3db)
- Bug 1168246 - part1: CSS autocomplete picks most popular prop;r=pbrosset (8c9989abd3)
- Bug 1168246 - part3: ES lint fixes for inplace-editor and autocomplete-popup;r=pbrosset (66d113f0e2)
- Bug 1151413 - Set async caller in DevToolsUtils.executeSoon r=ejpbruel (aa5ee4cbdb)
- Bug 1234600 - executeSoon async stacks only when DEBUG_JS_MODULES enabled. r=fitzgen (5e4f771a7a)
- Bug 1219028 - Remove use of non-standard Function.prototype.isGenerator from devtools/. r=fitzgen (34327590e6)
- Bug 1235458 - Move a bunch of utilities from DevToolsUtils to ThreadSafeDevToolsUtils. r=jsantell (333e190b17)
- Bug 1205095 - Unwrap getters in DevToolsUtils.hasSafeGetter. (r=fitzgen) (784e678e66)
- Bug 1260589 - Add the ThreadSafeDevToolsUtils.flatten utility; r=jimb a=kwierso (c82b27da22)
- Bug 1260938 - Add the ThreadSafeDevToolsUtils.isSet utility; r=jimb a=kwierso (9d5601a39e)
- Bug 1262225 - Add actorID to worker fake parent ThreadActor. r=ejpbruel (7e6f761dba)
- Bug 1239705 - Add a 'Start' button for service workers in about:debugging. r=ochameau (8bdea0c588)
- Bug 1196784 - imageset will use permissions.default.image. r=jdm (9a5669f71f)
- Bug 1182727 - Part 16: Fix a bug in build_tar_package (1d8f444eba)
- Bug 1182727 - Part 18: Fix building clang on OSX 10.8 and older (a02e1baca5)
- Bug 1182727 - Part 19: Fix another stupid mistake in build_tar_package() (87bb241693)
- Bug 1261264 - Apply GCC PR64905 to fix miscompilation with -fomit-frame-pointer. r=froydnj (d746a0c878)
- missing option, for safety (6fba93dff9)
- Bug 1260605 - Remove hack from bug 967556. r=nalexander (9513dbcd26)
- Bug 1239789 - Add secondary buffers to zxx_stream::StaticAllocator to handle reentrancy. r=glandium (74b60ffaad)
- Bug 1260351 - MozGlue: Build: Add mips support. r=glandium (8a463032f6)
- Bug 1249849: Ensure that we can always reach malloc_decl.h even when MOZ_REPLACE_ALLOC is not defined; r=glandium (1a8d0356ac)
- Bug 1248915 - TSan: data race on global 'mozilla::net::CacheObserver::sDiskCacheCapacity'. r=honzab.moz, n.nethercote. (86769cd893)
- Bug 1249157 - prefapi enums into class enums, explicit conversion, cleanup. r=bsmedberg (1843c09593)
- Bug 1251393: Remove a duplicated line. r=me (c2bca1a4f4)
2024-07-10 14:32:26 +08:00

704 lines
23 KiB
JavaScript

/* 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";
/**
* This is the main module loaded in Firefox desktop that handles browser
* windows and coordinates devtools around each window.
*
* This module is loaded lazily by devtools-clhandler.js, once the first
* browser window is ready (i.e. fired browser-delayed-startup-finished event)
**/
const {Cc, Ci, Cu} = require("chrome");
const Services = require("Services");
const promise = require("promise");
const Telemetry = require("devtools/client/shared/telemetry");
const { gDevTools } = require("./devtools");
const { when: unload } = require("sdk/system/unload");
// Load target and toolbox lazily as they need gDevTools to be fully initialized
loader.lazyRequireGetter(this, "TargetFactory", "devtools/client/framework/target", true);
loader.lazyRequireGetter(this, "Toolbox", "devtools/client/framework/toolbox", true);
loader.lazyRequireGetter(this, "DebuggerServer", "devtools/server/main", true);
loader.lazyRequireGetter(this, "DebuggerClient", "devtools/shared/client/main", true);
loader.lazyRequireGetter(this, "BrowserMenus", "devtools/client/framework/browser-menus");
loader.lazyImporter(this, "CustomizableUI", "resource:///modules/CustomizableUI.jsm");
const bundle = Services.strings.createBundle("chrome://devtools/locale/toolbox.properties");
const TABS_OPEN_PEAK_HISTOGRAM = "DEVTOOLS_TABS_OPEN_PEAK_LINEAR";
const TABS_OPEN_AVG_HISTOGRAM = "DEVTOOLS_TABS_OPEN_AVERAGE_LINEAR";
const TABS_PINNED_PEAK_HISTOGRAM = "DEVTOOLS_TABS_PINNED_PEAK_LINEAR";
const TABS_PINNED_AVG_HISTOGRAM = "DEVTOOLS_TABS_PINNED_AVERAGE_LINEAR";
/**
* gDevToolsBrowser exposes functions to connect the gDevTools instance with a
* Firefox instance.
*/
var gDevToolsBrowser = exports.gDevToolsBrowser = {
/**
* A record of the windows whose menus we altered, so we can undo the changes
* as the window is closed
*/
_trackedBrowserWindows: new Set(),
_telemetry: new Telemetry(),
_tabStats: {
peakOpen: 0,
peakPinned: 0,
histOpen: [],
histPinned: []
},
/**
* This function is for the benefit of Tools:DevToolbox in
* browser/base/content/browser-sets.inc and should not be used outside
* of there
*/
// used by browser-sets.inc, command
toggleToolboxCommand: function(gBrowser) {
let target = TargetFactory.forTab(gBrowser.selectedTab);
let toolbox = gDevTools.getToolbox(target);
// If a toolbox exists, using toggle from the Main window :
// - should close a docked toolbox
// - should focus a windowed toolbox
let isDocked = toolbox && toolbox.hostType != Toolbox.HostType.WINDOW;
isDocked ? toolbox.destroy() : gDevTools.showToolbox(target);
},
/**
* This function ensures the right commands are enabled in a window,
* depending on their relevant prefs. It gets run when a window is registered,
* or when any of the devtools prefs change.
*/
updateCommandAvailability: function(win) {
let doc = win.document;
function toggleCmd(id, isEnabled) {
let cmd = doc.getElementById(id);
if (isEnabled) {
cmd.removeAttribute("disabled");
cmd.removeAttribute("hidden");
} else if (cmd != null) {
cmd.setAttribute("disabled", "true");
cmd.setAttribute("hidden", "true");
}
};
// Enable developer toolbar?
let devToolbarEnabled = Services.prefs.getBoolPref("devtools.toolbar.enabled");
toggleCmd("Tools:DevToolbar", devToolbarEnabled);
let focusEl = doc.getElementById("Tools:DevToolbarFocus");
if (devToolbarEnabled) {
focusEl.removeAttribute("disabled");
} else {
focusEl.setAttribute("disabled", "true");
}
if (devToolbarEnabled && Services.prefs.getBoolPref("devtools.toolbar.visible")) {
win.DeveloperToolbar.show(false).catch(console.error);
}
// Enable WebIDE?
let webIDEEnabled = Services.prefs.getBoolPref("devtools.webide.enabled");
toggleCmd("Tools:WebIDE", webIDEEnabled);
let showWebIDEWidget = Services.prefs.getBoolPref("devtools.webide.widget.enabled");
if (webIDEEnabled && showWebIDEWidget) {
gDevToolsBrowser.installWebIDEWidget();
} else {
gDevToolsBrowser.uninstallWebIDEWidget();
}
// Enable Browser Toolbox?
let chromeEnabled = Services.prefs.getBoolPref("devtools.chrome.enabled");
let devtoolsRemoteEnabled = Services.prefs.getBoolPref("devtools.debugger.remote-enabled");
let remoteEnabled = chromeEnabled && devtoolsRemoteEnabled;
toggleCmd("Tools:BrowserToolbox", remoteEnabled);
toggleCmd("Tools:BrowserContentToolbox", remoteEnabled && win.gMultiProcessBrowser);
// Enable Error Console?
let consoleEnabled = Services.prefs.getBoolPref("devtools.errorconsole.enabled");
toggleCmd("Tools:ErrorConsole", consoleEnabled);
// Enable DevTools connection screen, if the preference allows this.
toggleCmd("Tools:DevToolsConnect", devtoolsRemoteEnabled);
},
observe: function(subject, topic, prefName) {
switch (topic) {
case "browser-delayed-startup-finished":
this._registerBrowserWindow(subject);
break;
case "nsPref:changed":
if (prefName.endsWith("enabled")) {
for (let win of this._trackedBrowserWindows) {
this.updateCommandAvailability(win);
}
}
break;
}
},
_prefObserverRegistered: false,
ensurePrefObserver: function() {
if (!this._prefObserverRegistered) {
this._prefObserverRegistered = true;
Services.prefs.addObserver("devtools.", this, false);
}
},
/**
* 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 targeted tool is not selected,
* we select it
* - if the toolbox is open, and the targeted tool is selected,
* and the host is NOT a window, we close the toolbox
* - if the toolbox is open, and the targeted tool is selected,
* and the host is a window, we raise the toolbox window
*/
// Used when: - registering a new tool
// - new xul window, to add menu items
selectToolCommand: function(gBrowser, toolId) {
let target = TargetFactory.forTab(gBrowser.selectedTab);
let toolbox = gDevTools.getToolbox(target);
let toolDefinition = gDevTools.getToolDefinition(toolId);
if (toolbox &&
(toolbox.currentToolId == toolId ||
(toolId == "webconsole" && toolbox.splitConsole)))
{
toolbox.fireCustomKey(toolId);
if (toolDefinition.preventClosingOnKey || toolbox.hostType == Toolbox.HostType.WINDOW) {
toolbox.raise();
} else {
toolbox.destroy();
}
gDevTools.emit("select-tool-command", toolId);
} else {
gDevTools.showToolbox(target, toolId).then(() => {
let target = TargetFactory.forTab(gBrowser.selectedTab);
let toolbox = gDevTools.getToolbox(target);
toolbox.fireCustomKey(toolId);
gDevTools.emit("select-tool-command", toolId);
});
}
},
/**
* Open a tab on "about:debugging", optionally pre-select a given tab.
*/
// Used by browser-sets.inc, command
openAboutDebugging: function(gBrowser, hash) {
let url = "about:debugging" + (hash ? "#" + hash : "");
gBrowser.selectedTab = gBrowser.addTab(url);
},
/**
* Open a tab to allow connects to a remote browser
*/
// Used by browser-sets.inc, command
openConnectScreen: function(gBrowser) {
gBrowser.selectedTab = gBrowser.addTab("chrome://devtools/content/framework/connect/connect.xhtml");
},
/**
* Open WebIDE
*/
// Used by browser-sets.inc, command
// itself, webide widget
openWebIDE: function() {
let win = Services.wm.getMostRecentWindow("devtools:webide");
if (win) {
win.focus();
} else {
Services.ww.openWindow(null, "chrome://webide/content/", "webide", "chrome,centerscreen,resizable", null);
}
},
_getContentProcessTarget: function () {
// Create a DebuggerServer in order to connect locally to it
if (!DebuggerServer.initialized) {
DebuggerServer.init();
DebuggerServer.addBrowserActors();
}
DebuggerServer.allowChromeProcess = true;
let transport = DebuggerServer.connectPipe();
let client = new DebuggerClient(transport);
let deferred = promise.defer();
client.connect().then(() => {
client.mainRoot.listProcesses(response => {
// Do nothing if there is only one process, the parent process.
let contentProcesses = response.processes.filter(p => (!p.parent));
if (contentProcesses.length < 1) {
let msg = bundle.GetStringFromName("toolbox.noContentProcess.message");
Services.prompt.alert(null, "", msg);
deferred.reject("No content processes available.");
return;
}
// Otherwise, arbitrary connect to the unique content process.
client.getProcess(contentProcesses[0].id)
.then(response => {
let options = {
form: response.form,
client: client,
chrome: true,
isTabActor: false
};
return TargetFactory.forRemoteTab(options);
})
.then(target => {
// Ensure closing the connection in order to cleanup
// the debugger client and also the server created in the
// content process
target.on("close", () => {
client.close();
});
deferred.resolve(target);
});
});
});
return deferred.promise;
},
// Used by browser-sets.inc, command
openContentProcessToolbox: function () {
this._getContentProcessTarget()
.then(target => {
// Display a new toolbox, in a new window, with debugger by default
return gDevTools.showToolbox(target, "jsdebugger",
Toolbox.HostType.WINDOW);
});
},
/**
* Install WebIDE widget
*/
// Used by itself
installWebIDEWidget: function() {
// No CustomizableUI -> short circuit
return;
if (this.isWebIDEWidgetInstalled()) {
return;
}
let defaultArea;
if (Services.prefs.getBoolPref("devtools.webide.widget.inNavbarByDefault")) {
defaultArea = CustomizableUI.AREA_NAVBAR;
} else {
defaultArea = CustomizableUI.AREA_PANEL;
}
CustomizableUI.createWidget({
id: "webide-button",
shortcutId: "key_webide",
label: "devtools-webide-button2.label",
tooltiptext: "devtools-webide-button2.tooltiptext",
defaultArea: defaultArea,
onCommand: function(aEvent) {
gDevToolsBrowser.openWebIDE();
}
});
},
isWebIDEWidgetInstalled: function() {
// No CustomizableUI -> short circuit
return;
let widgetWrapper = CustomizableUI.getWidget("webide-button");
return !!(widgetWrapper && widgetWrapper.provider == CustomizableUI.PROVIDER_API);
},
/**
* The deferred promise will be resolved by WebIDE's UI.init()
*/
isWebIDEInitialized: promise.defer(),
/**
* Uninstall WebIDE widget
*/
uninstallWebIDEWidget: function() {
// No CustomizableUI -> short circuit
return;
if (this.isWebIDEWidgetInstalled()) {
CustomizableUI.removeWidgetFromArea("webide-button");
}
CustomizableUI.destroyWidget("webide-button");
},
/**
* Move WebIDE widget to the navbar
*/
// Used by webide.js
moveWebIDEWidgetInNavbar: function() {
// No CustomizableUI -> short circuit
return;
CustomizableUI.addWidgetToArea("webide-button", CustomizableUI.AREA_NAVBAR);
},
/**
* Add this DevTools's presence to a browser window's document
*
* @param {XULDocument} doc
* The document to which menuitems and handlers are to be added
*/
_registerBrowserWindow: function(win) {
BrowserMenus.addMenus(win.document);
// Inject lazily DeveloperToolbar on the chrome window
loader.lazyGetter(win, "DeveloperToolbar", function() {
let { DeveloperToolbar } = require("devtools/client/shared/developer-toolbar");
return new DeveloperToolbar(win);
});
this.updateCommandAvailability(win);
this.ensurePrefObserver();
gDevToolsBrowser._trackedBrowserWindows.add(win);
win.addEventListener("unload", this);
let tabContainer = win.gBrowser.tabContainer;
tabContainer.addEventListener("TabSelect", this, false);
tabContainer.addEventListener("TabOpen", this, false);
tabContainer.addEventListener("TabClose", this, false);
tabContainer.addEventListener("TabPinned", this, false);
tabContainer.addEventListener("TabUnpinned", this, false);
},
/**
* Hook the JS debugger tool to the "Debug Script" button of the slow script
* dialog.
*/
setSlowScriptDebugHandler: function DT_setSlowScriptDebugHandler() {
let debugService = Cc["@mozilla.org/dom/slow-script-debug;1"]
.getService(Ci.nsISlowScriptDebug);
let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
function slowScriptDebugHandler(aTab, aCallback) {
let target = TargetFactory.forTab(aTab);
gDevTools.showToolbox(target, "jsdebugger").then(toolbox => {
let threadClient = toolbox.getCurrentPanel().panelWin.gThreadClient;
// Break in place, which means resuming the debuggee thread and pausing
// right before the next step happens.
switch (threadClient.state) {
case "paused":
// When the debugger is already paused.
threadClient.resumeThenPause();
aCallback();
break;
case "attached":
// When the debugger is already open.
threadClient.interrupt(() => {
threadClient.resumeThenPause();
aCallback();
});
break;
case "resuming":
// The debugger is newly opened.
threadClient.addOneTimeListener("resumed", () => {
threadClient.interrupt(() => {
threadClient.resumeThenPause();
aCallback();
});
});
break;
default:
throw Error("invalid thread client state in slow script debug handler: " +
threadClient.state);
}
});
}
debugService.activationHandler = function(aWindow) {
let chromeWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow)
.QueryInterface(Ci.nsIDOMChromeWindow);
let setupFinished = false;
slowScriptDebugHandler(chromeWindow.gBrowser.selectedTab,
() => { setupFinished = true; });
// Don't return from the interrupt handler until the debugger is brought
// up; no reason to continue executing the slow script.
let utils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
utils.enterModalState();
while (!setupFinished) {
tm.currentThread.processNextEvent(true);
}
utils.leaveModalState();
};
debugService.remoteActivationHandler = function(aBrowser, aCallback) {
let chromeWindow = aBrowser.ownerDocument.defaultView;
let tab = chromeWindow.gBrowser.getTabForBrowser(aBrowser);
chromeWindow.gBrowser.selected = tab;
function callback() {
aCallback.finishDebuggerStartup();
}
slowScriptDebugHandler(tab, callback);
};
},
/**
* Unset the slow script debug handler.
*/
unsetSlowScriptDebugHandler: function DT_unsetSlowScriptDebugHandler() {
let debugService = Cc["@mozilla.org/dom/slow-script-debug;1"]
.getService(Ci.nsISlowScriptDebug);
debugService.activationHandler = undefined;
},
/**
* 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) {
// No menu item or global shortcut is required for options panel.
if (!toolDefinition.inMenu) {
return;
}
// Skip if the tool is disabled.
try {
if (toolDefinition.visibilityswitch &&
!Services.prefs.getBoolPref(toolDefinition.visibilityswitch)) {
return;
}
} catch(e) {}
// 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.inMenu) {
continue;
}
if (def === toolDefinition) {
break;
}
prevDef = def;
}
for (let win of gDevToolsBrowser._trackedBrowserWindows) {
BrowserMenus.insertToolMenuElements(win.document, toolDefinition, prevDef);
}
if (toolDefinition.id === "jsdebugger") {
gDevToolsBrowser.setSlowScriptDebugHandler();
}
},
hasToolboxOpened: function(win) {
let tab = win.gBrowser.selectedTab;
for (let [target, toolbox] of gDevTools._toolboxes) {
if (target.tab == tab) {
return true;
}
}
return false;
},
/**
* Update the "Toggle Tools" checkbox in the developer tools menu. This is
* called when a toolbox is created or destroyed.
*/
_updateMenuCheckbox: function DT_updateMenuCheckbox() {
for (let win of gDevToolsBrowser._trackedBrowserWindows) {
let hasToolbox = gDevToolsBrowser.hasToolboxOpened(win);
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 {string} toolId
* id of the tool to remove
*/
_removeToolFromWindows: function DT_removeToolFromWindows(toolId) {
for (let win of gDevToolsBrowser._trackedBrowserWindows) {
BrowserMenus.removeToolFromMenu(toolId, win.document);
}
if (toolId === "jsdebugger") {
gDevToolsBrowser.unsetSlowScriptDebugHandler();
}
},
/**
* 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(win) {
if (!gDevToolsBrowser._trackedBrowserWindows.has(win)) {
return;
}
gDevToolsBrowser._trackedBrowserWindows.delete(win);
win.removeEventListener("unload", this);
// Destroy toolboxes for closed window
for (let [target, toolbox] of gDevTools._toolboxes) {
if (toolbox.frame && toolbox.frame.ownerDocument.defaultView == win) {
toolbox.destroy();
}
}
// Destroy the Developer toolbar if it has been accessed
let desc = Object.getOwnPropertyDescriptor(win, "DeveloperToolbar");
if (desc && !desc.get) {
win.DeveloperToolbar.destroy();
}
let tabContainer = win.gBrowser.tabContainer;
tabContainer.removeEventListener("TabSelect", this, false);
tabContainer.removeEventListener("TabOpen", this, false);
tabContainer.removeEventListener("TabClose", this, false);
tabContainer.removeEventListener("TabPinned", this, false);
tabContainer.removeEventListener("TabUnpinned", this, false);
},
handleEvent: function(event) {
switch (event.type) {
case "TabOpen":
case "TabClose":
case "TabPinned":
case "TabUnpinned":
let open = 0;
let pinned = 0;
for (let win of this._trackedBrowserWindows) {
let tabContainer = win.gBrowser.tabContainer;
let numPinnedTabs = win.gBrowser._numPinnedTabs || 0;
let numTabs = tabContainer.itemCount - numPinnedTabs;
open += numTabs;
pinned += numPinnedTabs;
}
this._tabStats.histOpen.push(open);
this._tabStats.histPinned.push(pinned);
this._tabStats.peakOpen = Math.max(open, this._tabStats.peakOpen);
this._tabStats.peakPinned = Math.max(pinned, this._tabStats.peakPinned);
break;
case "TabSelect":
gDevToolsBrowser._updateMenuCheckbox();
break;
case "unload":
// top-level browser window unload
gDevToolsBrowser._forgetBrowserWindow(event.target.defaultView);
break;
}
},
_pingTelemetry: function() {
let mean = function(arr) {
if (arr.length === 0) {
return 0;
}
let total = arr.reduce((a, b) => a + b);
return Math.ceil(total / arr.length);
};
let tabStats = gDevToolsBrowser._tabStats;
this._telemetry.log(TABS_OPEN_PEAK_HISTOGRAM, tabStats.peakOpen);
this._telemetry.log(TABS_OPEN_AVG_HISTOGRAM, mean(tabStats.histOpen));
this._telemetry.log(TABS_PINNED_PEAK_HISTOGRAM, tabStats.peakPinned);
this._telemetry.log(TABS_PINNED_AVG_HISTOGRAM, mean(tabStats.histPinned));
},
/**
* All browser windows have been closed, tidy up remaining objects.
*/
destroy: function() {
Services.prefs.removeObserver("devtools.", gDevToolsBrowser);
Services.obs.removeObserver(gDevToolsBrowser, "browser-delayed-startup-finished");
Services.obs.removeObserver(gDevToolsBrowser.destroy, "quit-application");
gDevToolsBrowser._pingTelemetry();
gDevToolsBrowser._telemetry = null;
for (let win of gDevToolsBrowser._trackedBrowserWindows) {
gDevToolsBrowser._forgetBrowserWindow(win);
}
},
}
// Handle all already registered tools,
gDevTools.getToolDefinitionArray()
.forEach(def => gDevToolsBrowser._addToolToWindows(def));
// and the new ones.
gDevTools.on("tool-registered", function(ev, toolId) {
let toolDefinition = gDevTools._tools.get(toolId);
gDevToolsBrowser._addToolToWindows(toolDefinition);
});
gDevTools.on("tool-unregistered", function(ev, toolId) {
if (typeof toolId != "string") {
toolId = toolId.id;
}
gDevToolsBrowser._removeToolFromWindows(toolId);
});
gDevTools.on("toolbox-ready", gDevToolsBrowser._updateMenuCheckbox);
gDevTools.on("toolbox-destroyed", gDevToolsBrowser._updateMenuCheckbox);
Services.obs.addObserver(gDevToolsBrowser.destroy, "quit-application", false);
Services.obs.addObserver(gDevToolsBrowser, "browser-delayed-startup-finished", false);
// Fake end of browser window load event for all already opened windows
// that is already fully loaded.
let enumerator = Services.wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements()) {
let win = enumerator.getNext();
if (win.gBrowserInit && win.gBrowserInit.delayedStartupFinished) {
gDevToolsBrowser._registerBrowserWindow(win);
}
}
// Watch for module loader unload. Fires when the tools are reloaded.
unload(function () {
gDevToolsBrowser.destroy();
});