browser/devtools/framework/gDevTools.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = [ "gDevTools", "DevTools", "gDevToolsBrowser" ];
michael@0 8
michael@0 9 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 12 Cu.import("resource://gre/modules/Services.jsm");
michael@0 13 Cu.import("resource://gre/modules/devtools/event-emitter.js");
michael@0 14 Cu.import("resource://gre/modules/devtools/Loader.jsm");
michael@0 15 XPCOMUtils.defineLazyModuleGetter(this, "promise", "resource://gre/modules/Promise.jsm", "Promise");
michael@0 16
michael@0 17 const FORBIDDEN_IDS = new Set(["toolbox", ""]);
michael@0 18 const MAX_ORDINAL = 99;
michael@0 19
michael@0 20
michael@0 21 /**
michael@0 22 * DevTools is a class that represents a set of developer tools, it holds a
michael@0 23 * set of tools and keeps track of open toolboxes in the browser.
michael@0 24 */
michael@0 25 this.DevTools = function DevTools() {
michael@0 26 this._tools = new Map(); // Map<toolId, tool>
michael@0 27 this._toolboxes = new Map(); // Map<target, toolbox>
michael@0 28
michael@0 29 // destroy() is an observer's handler so we need to preserve context.
michael@0 30 this.destroy = this.destroy.bind(this);
michael@0 31 this._teardown = this._teardown.bind(this);
michael@0 32
michael@0 33 this._testing = false;
michael@0 34
michael@0 35 EventEmitter.decorate(this);
michael@0 36
michael@0 37 Services.obs.addObserver(this._teardown, "devtools-unloaded", false);
michael@0 38 Services.obs.addObserver(this.destroy, "quit-application", false);
michael@0 39 }
michael@0 40
michael@0 41 DevTools.prototype = {
michael@0 42 /**
michael@0 43 * When the testing flag is set we take appropriate action to prevent race
michael@0 44 * conditions in our testing environment. This means setting
michael@0 45 * dom.send_after_paint_to_content to false to prevent infinite MozAfterPaint
michael@0 46 * loops and not autohiding the highlighter.
michael@0 47 */
michael@0 48 get testing() {
michael@0 49 return this._testing;
michael@0 50 },
michael@0 51
michael@0 52 set testing(state) {
michael@0 53 this._testing = state;
michael@0 54
michael@0 55 if (state) {
michael@0 56 // dom.send_after_paint_to_content is set to true (non-default) in
michael@0 57 // testing/profiles/prefs_general.js so lets set it to the same as it is
michael@0 58 // in a default browser profile for the duration of the test.
michael@0 59 Services.prefs.setBoolPref("dom.send_after_paint_to_content", false);
michael@0 60 } else {
michael@0 61 Services.prefs.setBoolPref("dom.send_after_paint_to_content", true);
michael@0 62 }
michael@0 63 },
michael@0 64
michael@0 65 /**
michael@0 66 * Register a new developer tool.
michael@0 67 *
michael@0 68 * A definition is a light object that holds different information about a
michael@0 69 * developer tool. This object is not supposed to have any operational code.
michael@0 70 * See it as a "manifest".
michael@0 71 * The only actual code lives in the build() function, which will be used to
michael@0 72 * start an instance of this tool.
michael@0 73 *
michael@0 74 * Each toolDefinition has the following properties:
michael@0 75 * - id: Unique identifier for this tool (string|required)
michael@0 76 * - visibilityswitch: Property name to allow us to hide this tool from the
michael@0 77 * DevTools Toolbox.
michael@0 78 * A falsy value indicates that it cannot be hidden.
michael@0 79 * - icon: URL pointing to a graphic which will be used as the src for an
michael@0 80 * 16x16 img tag (string|required)
michael@0 81 * - invertIconForLightTheme: The icon can automatically have an inversion
michael@0 82 * filter applied (default is false). All builtin tools are true, but
michael@0 83 * addons may omit this to prevent unwanted changes to the `icon`
michael@0 84 * image. See browser/themes/shared/devtools/filters.svg#invert for
michael@0 85 * the filter being applied to the images (boolean|optional)
michael@0 86 * - url: URL pointing to a XUL/XHTML document containing the user interface
michael@0 87 * (string|required)
michael@0 88 * - label: Localized name for the tool to be displayed to the user
michael@0 89 * (string|required)
michael@0 90 * - build: Function that takes an iframe, which has been populated with the
michael@0 91 * markup from |url|, and also the toolbox containing the panel.
michael@0 92 * And returns an instance of ToolPanel (function|required)
michael@0 93 */
michael@0 94 registerTool: function DT_registerTool(toolDefinition) {
michael@0 95 let toolId = toolDefinition.id;
michael@0 96
michael@0 97 if (!toolId || FORBIDDEN_IDS.has(toolId)) {
michael@0 98 throw new Error("Invalid definition.id");
michael@0 99 }
michael@0 100
michael@0 101 // Make sure that additional tools will always be able to be hidden.
michael@0 102 // When being called from main.js, defaultTools has not yet been exported.
michael@0 103 // But, we can assume that in this case, it is a default tool.
michael@0 104 if (devtools.defaultTools && devtools.defaultTools.indexOf(toolDefinition) == -1) {
michael@0 105 toolDefinition.visibilityswitch = "devtools." + toolId + ".enabled";
michael@0 106 }
michael@0 107
michael@0 108 this._tools.set(toolId, toolDefinition);
michael@0 109
michael@0 110 this.emit("tool-registered", toolId);
michael@0 111 },
michael@0 112
michael@0 113 /**
michael@0 114 * Removes all tools that match the given |toolId|
michael@0 115 * Needed so that add-ons can remove themselves when they are deactivated
michael@0 116 *
michael@0 117 * @param {string|object} tool
michael@0 118 * Definition or the id of the tool to unregister. Passing the
michael@0 119 * tool id should be avoided as it is a temporary measure.
michael@0 120 * @param {boolean} isQuitApplication
michael@0 121 * true to indicate that the call is due to app quit, so we should not
michael@0 122 * cause a cascade of costly events
michael@0 123 */
michael@0 124 unregisterTool: function DT_unregisterTool(tool, isQuitApplication) {
michael@0 125 let toolId = null;
michael@0 126 if (typeof tool == "string") {
michael@0 127 toolId = tool;
michael@0 128 tool = this._tools.get(tool);
michael@0 129 }
michael@0 130 else {
michael@0 131 toolId = tool.id;
michael@0 132 }
michael@0 133 this._tools.delete(toolId);
michael@0 134
michael@0 135 if (!isQuitApplication) {
michael@0 136 this.emit("tool-unregistered", tool);
michael@0 137 }
michael@0 138 },
michael@0 139
michael@0 140 /**
michael@0 141 * Sorting function used for sorting tools based on their ordinals.
michael@0 142 */
michael@0 143 ordinalSort: function DT_ordinalSort(d1, d2) {
michael@0 144 let o1 = (typeof d1.ordinal == "number") ? d1.ordinal : MAX_ORDINAL;
michael@0 145 let o2 = (typeof d2.ordinal == "number") ? d2.ordinal : MAX_ORDINAL;
michael@0 146 return o1 - o2;
michael@0 147 },
michael@0 148
michael@0 149 getDefaultTools: function DT_getDefaultTools() {
michael@0 150 return devtools.defaultTools.sort(this.ordinalSort);
michael@0 151 },
michael@0 152
michael@0 153 getAdditionalTools: function DT_getAdditionalTools() {
michael@0 154 let tools = [];
michael@0 155 for (let [key, value] of this._tools) {
michael@0 156 if (devtools.defaultTools.indexOf(value) == -1) {
michael@0 157 tools.push(value);
michael@0 158 }
michael@0 159 }
michael@0 160 return tools.sort(this.ordinalSort);
michael@0 161 },
michael@0 162
michael@0 163 /**
michael@0 164 * Get a tool definition if it exists and is enabled.
michael@0 165 *
michael@0 166 * @param {string} toolId
michael@0 167 * The id of the tool to show
michael@0 168 *
michael@0 169 * @return {ToolDefinition|null} tool
michael@0 170 * The ToolDefinition for the id or null.
michael@0 171 */
michael@0 172 getToolDefinition: function DT_getToolDefinition(toolId) {
michael@0 173 let tool = this._tools.get(toolId);
michael@0 174 if (!tool) {
michael@0 175 return null;
michael@0 176 } else if (!tool.visibilityswitch) {
michael@0 177 return tool;
michael@0 178 }
michael@0 179
michael@0 180 let enabled;
michael@0 181 try {
michael@0 182 enabled = Services.prefs.getBoolPref(tool.visibilityswitch);
michael@0 183 } catch (e) {
michael@0 184 enabled = true;
michael@0 185 }
michael@0 186
michael@0 187 return enabled ? tool : null;
michael@0 188 },
michael@0 189
michael@0 190 /**
michael@0 191 * Allow ToolBoxes to get at the list of tools that they should populate
michael@0 192 * themselves with.
michael@0 193 *
michael@0 194 * @return {Map} tools
michael@0 195 * A map of the the tool definitions registered in this instance
michael@0 196 */
michael@0 197 getToolDefinitionMap: function DT_getToolDefinitionMap() {
michael@0 198 let tools = new Map();
michael@0 199
michael@0 200 for (let [id, definition] of this._tools) {
michael@0 201 if (this.getToolDefinition(id)) {
michael@0 202 tools.set(id, definition);
michael@0 203 }
michael@0 204 }
michael@0 205
michael@0 206 return tools;
michael@0 207 },
michael@0 208
michael@0 209 /**
michael@0 210 * Tools have an inherent ordering that can't be represented in a Map so
michael@0 211 * getToolDefinitionArray provides an alternative representation of the
michael@0 212 * definitions sorted by ordinal value.
michael@0 213 *
michael@0 214 * @return {Array} tools
michael@0 215 * A sorted array of the tool definitions registered in this instance
michael@0 216 */
michael@0 217 getToolDefinitionArray: function DT_getToolDefinitionArray() {
michael@0 218 let definitions = [];
michael@0 219
michael@0 220 for (let [id, definition] of this._tools) {
michael@0 221 if (this.getToolDefinition(id)) {
michael@0 222 definitions.push(definition);
michael@0 223 }
michael@0 224 }
michael@0 225
michael@0 226 return definitions.sort(this.ordinalSort);
michael@0 227 },
michael@0 228
michael@0 229 /**
michael@0 230 * Show a Toolbox for a target (either by creating a new one, or if a toolbox
michael@0 231 * already exists for the target, by bring to the front the existing one)
michael@0 232 * If |toolId| is specified then the displayed toolbox will have the
michael@0 233 * specified tool selected.
michael@0 234 * If |hostType| is specified then the toolbox will be displayed using the
michael@0 235 * specified HostType.
michael@0 236 *
michael@0 237 * @param {Target} target
michael@0 238 * The target the toolbox will debug
michael@0 239 * @param {string} toolId
michael@0 240 * The id of the tool to show
michael@0 241 * @param {Toolbox.HostType} hostType
michael@0 242 * The type of host (bottom, window, side)
michael@0 243 * @param {object} hostOptions
michael@0 244 * Options for host specifically
michael@0 245 *
michael@0 246 * @return {Toolbox} toolbox
michael@0 247 * The toolbox that was opened
michael@0 248 */
michael@0 249 showToolbox: function(target, toolId, hostType, hostOptions) {
michael@0 250 let deferred = promise.defer();
michael@0 251
michael@0 252 let toolbox = this._toolboxes.get(target);
michael@0 253 if (toolbox) {
michael@0 254
michael@0 255 let hostPromise = (hostType != null && toolbox.hostType != hostType) ?
michael@0 256 toolbox.switchHost(hostType) :
michael@0 257 promise.resolve(null);
michael@0 258
michael@0 259 if (toolId != null && toolbox.currentToolId != toolId) {
michael@0 260 hostPromise = hostPromise.then(function() {
michael@0 261 return toolbox.selectTool(toolId);
michael@0 262 });
michael@0 263 }
michael@0 264
michael@0 265 return hostPromise.then(function() {
michael@0 266 toolbox.raise();
michael@0 267 return toolbox;
michael@0 268 });
michael@0 269 }
michael@0 270 else {
michael@0 271 // No toolbox for target, create one
michael@0 272 toolbox = new devtools.Toolbox(target, toolId, hostType, hostOptions);
michael@0 273
michael@0 274 this._toolboxes.set(target, toolbox);
michael@0 275
michael@0 276 toolbox.once("destroyed", function() {
michael@0 277 this._toolboxes.delete(target);
michael@0 278 this.emit("toolbox-destroyed", target);
michael@0 279 }.bind(this));
michael@0 280
michael@0 281 // If we were asked for a specific tool then we need to wait for the
michael@0 282 // tool to be ready, otherwise we can just wait for toolbox open
michael@0 283 if (toolId != null) {
michael@0 284 toolbox.once(toolId + "-ready", function(event, panel) {
michael@0 285 this.emit("toolbox-ready", toolbox);
michael@0 286 deferred.resolve(toolbox);
michael@0 287 }.bind(this));
michael@0 288 toolbox.open();
michael@0 289 }
michael@0 290 else {
michael@0 291 toolbox.open().then(function() {
michael@0 292 deferred.resolve(toolbox);
michael@0 293 this.emit("toolbox-ready", toolbox);
michael@0 294 }.bind(this));
michael@0 295 }
michael@0 296 }
michael@0 297
michael@0 298 return deferred.promise;
michael@0 299 },
michael@0 300
michael@0 301 /**
michael@0 302 * Return the toolbox for a given target.
michael@0 303 *
michael@0 304 * @param {object} target
michael@0 305 * Target value e.g. the target that owns this toolbox
michael@0 306 *
michael@0 307 * @return {Toolbox} toolbox
michael@0 308 * The toobox that is debugging the given target
michael@0 309 */
michael@0 310 getToolbox: function DT_getToolbox(target) {
michael@0 311 return this._toolboxes.get(target);
michael@0 312 },
michael@0 313
michael@0 314 /**
michael@0 315 * Close the toolbox for a given target
michael@0 316 *
michael@0 317 * @return promise
michael@0 318 * This promise will resolve to false if no toolbox was found
michael@0 319 * associated to the target. true, if the toolbox was successfuly
michael@0 320 * closed.
michael@0 321 */
michael@0 322 closeToolbox: function DT_closeToolbox(target) {
michael@0 323 let toolbox = this._toolboxes.get(target);
michael@0 324 if (toolbox == null) {
michael@0 325 return promise.resolve(false);
michael@0 326 }
michael@0 327 return toolbox.destroy().then(() => true);
michael@0 328 },
michael@0 329
michael@0 330 /**
michael@0 331 * Called to tear down a tools provider.
michael@0 332 */
michael@0 333 _teardown: function DT_teardown() {
michael@0 334 for (let [target, toolbox] of this._toolboxes) {
michael@0 335 toolbox.destroy();
michael@0 336 }
michael@0 337 },
michael@0 338
michael@0 339 /**
michael@0 340 * All browser windows have been closed, tidy up remaining objects.
michael@0 341 */
michael@0 342 destroy: function() {
michael@0 343 Services.obs.removeObserver(this.destroy, "quit-application");
michael@0 344 Services.obs.removeObserver(this._teardown, "devtools-unloaded");
michael@0 345
michael@0 346 for (let [key, tool] of this.getToolDefinitionMap()) {
michael@0 347 this.unregisterTool(key, true);
michael@0 348 }
michael@0 349
michael@0 350 // Cleaning down the toolboxes: i.e.
michael@0 351 // for (let [target, toolbox] of this._toolboxes) toolbox.destroy();
michael@0 352 // Is taken care of by the gDevToolsBrowser.forgetBrowserWindow
michael@0 353 },
michael@0 354
michael@0 355 /**
michael@0 356 * Iterator that yields each of the toolboxes.
michael@0 357 */
michael@0 358 '@@iterator': function*() {
michael@0 359 for (let toolbox of this._toolboxes) {
michael@0 360 yield toolbox;
michael@0 361 }
michael@0 362 }
michael@0 363 };
michael@0 364
michael@0 365 /**
michael@0 366 * gDevTools is a singleton that controls the Firefox Developer Tools.
michael@0 367 *
michael@0 368 * It is an instance of a DevTools class that holds a set of tools. It has the
michael@0 369 * same lifetime as the browser.
michael@0 370 */
michael@0 371 let gDevTools = new DevTools();
michael@0 372 this.gDevTools = gDevTools;
michael@0 373
michael@0 374 /**
michael@0 375 * gDevToolsBrowser exposes functions to connect the gDevTools instance with a
michael@0 376 * Firefox instance.
michael@0 377 */
michael@0 378 let gDevToolsBrowser = {
michael@0 379 /**
michael@0 380 * A record of the windows whose menus we altered, so we can undo the changes
michael@0 381 * as the window is closed
michael@0 382 */
michael@0 383 _trackedBrowserWindows: new Set(),
michael@0 384
michael@0 385 /**
michael@0 386 * This function is for the benefit of Tools:DevToolbox in
michael@0 387 * browser/base/content/browser-sets.inc and should not be used outside
michael@0 388 * of there
michael@0 389 */
michael@0 390 toggleToolboxCommand: function(gBrowser) {
michael@0 391 let target = devtools.TargetFactory.forTab(gBrowser.selectedTab);
michael@0 392 let toolbox = gDevTools.getToolbox(target);
michael@0 393
michael@0 394 toolbox ? toolbox.destroy() : gDevTools.showToolbox(target);
michael@0 395 },
michael@0 396
michael@0 397 toggleBrowserToolboxCommand: function(gBrowser) {
michael@0 398 let target = devtools.TargetFactory.forWindow(gBrowser.ownerDocument.defaultView);
michael@0 399 let toolbox = gDevTools.getToolbox(target);
michael@0 400
michael@0 401 toolbox ? toolbox.destroy()
michael@0 402 : gDevTools.showToolbox(target, "inspector", Toolbox.HostType.WINDOW);
michael@0 403 },
michael@0 404
michael@0 405 /**
michael@0 406 * This function ensures the right commands are enabled in a window,
michael@0 407 * depending on their relevant prefs. It gets run when a window is registered,
michael@0 408 * or when any of the devtools prefs change.
michael@0 409 */
michael@0 410 updateCommandAvailability: function(win) {
michael@0 411 let doc = win.document;
michael@0 412
michael@0 413 function toggleCmd(id, isEnabled) {
michael@0 414 let cmd = doc.getElementById(id);
michael@0 415 if (isEnabled) {
michael@0 416 cmd.removeAttribute("disabled");
michael@0 417 cmd.removeAttribute("hidden");
michael@0 418 } else {
michael@0 419 cmd.setAttribute("disabled", "true");
michael@0 420 cmd.setAttribute("hidden", "true");
michael@0 421 }
michael@0 422 };
michael@0 423
michael@0 424 // Enable developer toolbar?
michael@0 425 let devToolbarEnabled = Services.prefs.getBoolPref("devtools.toolbar.enabled");
michael@0 426 toggleCmd("Tools:DevToolbar", devToolbarEnabled);
michael@0 427 let focusEl = doc.getElementById("Tools:DevToolbarFocus");
michael@0 428 if (devToolbarEnabled) {
michael@0 429 focusEl.removeAttribute("disabled");
michael@0 430 } else {
michael@0 431 focusEl.setAttribute("disabled", "true");
michael@0 432 }
michael@0 433 if (devToolbarEnabled && Services.prefs.getBoolPref("devtools.toolbar.visible")) {
michael@0 434 win.DeveloperToolbar.show(false);
michael@0 435 }
michael@0 436
michael@0 437 // Enable App Manager?
michael@0 438 let appMgrEnabled = Services.prefs.getBoolPref("devtools.appmanager.enabled");
michael@0 439 toggleCmd("Tools:DevAppMgr", appMgrEnabled);
michael@0 440
michael@0 441 // Enable Browser Toolbox?
michael@0 442 let chromeEnabled = Services.prefs.getBoolPref("devtools.chrome.enabled");
michael@0 443 let devtoolsRemoteEnabled = Services.prefs.getBoolPref("devtools.debugger.remote-enabled");
michael@0 444 let remoteEnabled = chromeEnabled && devtoolsRemoteEnabled &&
michael@0 445 Services.prefs.getBoolPref("devtools.debugger.chrome-enabled");
michael@0 446 toggleCmd("Tools:BrowserToolbox", remoteEnabled);
michael@0 447
michael@0 448 // Enable Error Console?
michael@0 449 let consoleEnabled = Services.prefs.getBoolPref("devtools.errorconsole.enabled");
michael@0 450 toggleCmd("Tools:ErrorConsole", consoleEnabled);
michael@0 451
michael@0 452 // Enable DevTools connection screen, if the preference allows this.
michael@0 453 toggleCmd("Tools:DevToolsConnect", devtoolsRemoteEnabled);
michael@0 454 },
michael@0 455
michael@0 456 observe: function(subject, topic, prefName) {
michael@0 457 if (prefName.endsWith("enabled")) {
michael@0 458 for (let win of this._trackedBrowserWindows) {
michael@0 459 this.updateCommandAvailability(win);
michael@0 460 }
michael@0 461 }
michael@0 462 },
michael@0 463
michael@0 464 _prefObserverRegistered: false,
michael@0 465
michael@0 466 ensurePrefObserver: function() {
michael@0 467 if (!this._prefObserverRegistered) {
michael@0 468 this._prefObserverRegistered = true;
michael@0 469 Services.prefs.addObserver("devtools.", this, false);
michael@0 470 }
michael@0 471 },
michael@0 472
michael@0 473
michael@0 474 /**
michael@0 475 * This function is for the benefit of Tools:{toolId} commands,
michael@0 476 * triggered from the WebDeveloper menu and keyboard shortcuts.
michael@0 477 *
michael@0 478 * selectToolCommand's behavior:
michael@0 479 * - if the toolbox is closed,
michael@0 480 * we open the toolbox and select the tool
michael@0 481 * - if the toolbox is open, and the targetted tool is not selected,
michael@0 482 * we select it
michael@0 483 * - if the toolbox is open, and the targetted tool is selected,
michael@0 484 * and the host is NOT a window, we close the toolbox
michael@0 485 * - if the toolbox is open, and the targetted tool is selected,
michael@0 486 * and the host is a window, we raise the toolbox window
michael@0 487 */
michael@0 488 selectToolCommand: function(gBrowser, toolId) {
michael@0 489 let target = devtools.TargetFactory.forTab(gBrowser.selectedTab);
michael@0 490 let toolbox = gDevTools.getToolbox(target);
michael@0 491 let toolDefinition = gDevTools.getToolDefinition(toolId);
michael@0 492
michael@0 493 if (toolbox &&
michael@0 494 (toolbox.currentToolId == toolId ||
michael@0 495 (toolId == "webconsole" && toolbox.splitConsole)))
michael@0 496 {
michael@0 497 toolbox.fireCustomKey(toolId);
michael@0 498
michael@0 499 if (toolDefinition.preventClosingOnKey || toolbox.hostType == devtools.Toolbox.HostType.WINDOW) {
michael@0 500 toolbox.raise();
michael@0 501 } else {
michael@0 502 toolbox.destroy();
michael@0 503 }
michael@0 504 } else {
michael@0 505 gDevTools.showToolbox(target, toolId).then(() => {
michael@0 506 let target = devtools.TargetFactory.forTab(gBrowser.selectedTab);
michael@0 507 let toolbox = gDevTools.getToolbox(target);
michael@0 508
michael@0 509 toolbox.fireCustomKey(toolId);
michael@0 510 });
michael@0 511 }
michael@0 512 },
michael@0 513
michael@0 514 /**
michael@0 515 * Open a tab to allow connects to a remote browser
michael@0 516 */
michael@0 517 openConnectScreen: function(gBrowser) {
michael@0 518 gBrowser.selectedTab = gBrowser.addTab("chrome://browser/content/devtools/connect.xhtml");
michael@0 519 },
michael@0 520
michael@0 521 /**
michael@0 522 * Open the App Manager
michael@0 523 */
michael@0 524 openAppManager: function(gBrowser) {
michael@0 525 gBrowser.selectedTab = gBrowser.addTab("about:app-manager");
michael@0 526 },
michael@0 527
michael@0 528 /**
michael@0 529 * Add this DevTools's presence to a browser window's document
michael@0 530 *
michael@0 531 * @param {XULDocument} doc
michael@0 532 * The document to which menuitems and handlers are to be added
michael@0 533 */
michael@0 534 registerBrowserWindow: function DT_registerBrowserWindow(win) {
michael@0 535 this.updateCommandAvailability(win);
michael@0 536 this.ensurePrefObserver();
michael@0 537 gDevToolsBrowser._trackedBrowserWindows.add(win);
michael@0 538 gDevToolsBrowser._addAllToolsToMenu(win.document);
michael@0 539
michael@0 540 if (this._isFirebugInstalled()) {
michael@0 541 let broadcaster = win.document.getElementById("devtoolsMenuBroadcaster_DevToolbox");
michael@0 542 broadcaster.removeAttribute("key");
michael@0 543 }
michael@0 544
michael@0 545 let tabContainer = win.document.getElementById("tabbrowser-tabs")
michael@0 546 tabContainer.addEventListener("TabSelect",
michael@0 547 gDevToolsBrowser._updateMenuCheckbox, false);
michael@0 548 },
michael@0 549
michael@0 550 /**
michael@0 551 * Add a <key> to <keyset id="devtoolsKeyset">.
michael@0 552 * Appending a <key> element is not always enough. The <keyset> needs
michael@0 553 * to be detached and reattached to make sure the <key> is taken into
michael@0 554 * account (see bug 832984).
michael@0 555 *
michael@0 556 * @param {XULDocument} doc
michael@0 557 * The document to which keys are to be added
michael@0 558 * @param {XULElement} or {DocumentFragment} keys
michael@0 559 * Keys to add
michael@0 560 */
michael@0 561 attachKeybindingsToBrowser: function DT_attachKeybindingsToBrowser(doc, keys) {
michael@0 562 let devtoolsKeyset = doc.getElementById("devtoolsKeyset");
michael@0 563
michael@0 564 if (!devtoolsKeyset) {
michael@0 565 devtoolsKeyset = doc.createElement("keyset");
michael@0 566 devtoolsKeyset.setAttribute("id", "devtoolsKeyset");
michael@0 567 }
michael@0 568 devtoolsKeyset.appendChild(keys);
michael@0 569 let mainKeyset = doc.getElementById("mainKeyset");
michael@0 570 mainKeyset.parentNode.insertBefore(devtoolsKeyset, mainKeyset);
michael@0 571 },
michael@0 572
michael@0 573
michael@0 574 /**
michael@0 575 * Detect the presence of a Firebug.
michael@0 576 *
michael@0 577 * @return promise
michael@0 578 */
michael@0 579 _isFirebugInstalled: function DT_isFirebugInstalled() {
michael@0 580 let bootstrappedAddons = Services.prefs.getCharPref("extensions.bootstrappedAddons");
michael@0 581 return bootstrappedAddons.indexOf("firebug@software.joehewitt.com") != -1;
michael@0 582 },
michael@0 583
michael@0 584 /**
michael@0 585 * Add the menuitem for a tool to all open browser windows.
michael@0 586 *
michael@0 587 * @param {object} toolDefinition
michael@0 588 * properties of the tool to add
michael@0 589 */
michael@0 590 _addToolToWindows: function DT_addToolToWindows(toolDefinition) {
michael@0 591 // No menu item or global shortcut is required for options panel.
michael@0 592 if (!toolDefinition.inMenu) {
michael@0 593 return;
michael@0 594 }
michael@0 595
michael@0 596 // Skip if the tool is disabled.
michael@0 597 try {
michael@0 598 if (toolDefinition.visibilityswitch &&
michael@0 599 !Services.prefs.getBoolPref(toolDefinition.visibilityswitch)) {
michael@0 600 return;
michael@0 601 }
michael@0 602 } catch(e) {}
michael@0 603
michael@0 604 // We need to insert the new tool in the right place, which means knowing
michael@0 605 // the tool that comes before the tool that we're trying to add
michael@0 606 let allDefs = gDevTools.getToolDefinitionArray();
michael@0 607 let prevDef;
michael@0 608 for (let def of allDefs) {
michael@0 609 if (!def.inMenu) {
michael@0 610 continue;
michael@0 611 }
michael@0 612 if (def === toolDefinition) {
michael@0 613 break;
michael@0 614 }
michael@0 615 prevDef = def;
michael@0 616 }
michael@0 617
michael@0 618 for (let win of gDevToolsBrowser._trackedBrowserWindows) {
michael@0 619 let doc = win.document;
michael@0 620 let elements = gDevToolsBrowser._createToolMenuElements(toolDefinition, doc);
michael@0 621
michael@0 622 doc.getElementById("mainCommandSet").appendChild(elements.cmd);
michael@0 623
michael@0 624 if (elements.key) {
michael@0 625 this.attachKeybindingsToBrowser(doc, elements.key);
michael@0 626 }
michael@0 627
michael@0 628 doc.getElementById("mainBroadcasterSet").appendChild(elements.bc);
michael@0 629
michael@0 630 let amp = doc.getElementById("appmenu_webDeveloper_popup");
michael@0 631 if (amp) {
michael@0 632 let ref;
michael@0 633
michael@0 634 if (prevDef != null) {
michael@0 635 let menuitem = doc.getElementById("appmenuitem_" + prevDef.id);
michael@0 636 ref = menuitem && menuitem.nextSibling ? menuitem.nextSibling : null;
michael@0 637 } else {
michael@0 638 ref = doc.getElementById("appmenu_devtools_separator");
michael@0 639 }
michael@0 640
michael@0 641 if (ref) {
michael@0 642 amp.insertBefore(elements.appmenuitem, ref);
michael@0 643 }
michael@0 644 }
michael@0 645
michael@0 646 let mp = doc.getElementById("menuWebDeveloperPopup");
michael@0 647 if (mp) {
michael@0 648 let ref;
michael@0 649
michael@0 650 if (prevDef != null) {
michael@0 651 let menuitem = doc.getElementById("menuitem_" + prevDef.id);
michael@0 652 ref = menuitem && menuitem.nextSibling ? menuitem.nextSibling : null;
michael@0 653 } else {
michael@0 654 ref = doc.getElementById("menu_devtools_separator");
michael@0 655 }
michael@0 656
michael@0 657 if (ref) {
michael@0 658 mp.insertBefore(elements.menuitem, ref);
michael@0 659 }
michael@0 660 }
michael@0 661 }
michael@0 662 },
michael@0 663
michael@0 664 /**
michael@0 665 * Add all tools to the developer tools menu of a window.
michael@0 666 *
michael@0 667 * @param {XULDocument} doc
michael@0 668 * The document to which the tool items are to be added.
michael@0 669 */
michael@0 670 _addAllToolsToMenu: function DT_addAllToolsToMenu(doc) {
michael@0 671 let fragCommands = doc.createDocumentFragment();
michael@0 672 let fragKeys = doc.createDocumentFragment();
michael@0 673 let fragBroadcasters = doc.createDocumentFragment();
michael@0 674 let fragAppMenuItems = doc.createDocumentFragment();
michael@0 675 let fragMenuItems = doc.createDocumentFragment();
michael@0 676
michael@0 677 for (let toolDefinition of gDevTools.getToolDefinitionArray()) {
michael@0 678 if (!toolDefinition.inMenu) {
michael@0 679 continue;
michael@0 680 }
michael@0 681
michael@0 682 let elements = gDevToolsBrowser._createToolMenuElements(toolDefinition, doc);
michael@0 683
michael@0 684 if (!elements) {
michael@0 685 return;
michael@0 686 }
michael@0 687
michael@0 688 fragCommands.appendChild(elements.cmd);
michael@0 689 if (elements.key) {
michael@0 690 fragKeys.appendChild(elements.key);
michael@0 691 }
michael@0 692 fragBroadcasters.appendChild(elements.bc);
michael@0 693 fragAppMenuItems.appendChild(elements.appmenuitem);
michael@0 694 fragMenuItems.appendChild(elements.menuitem);
michael@0 695 }
michael@0 696
michael@0 697 let mcs = doc.getElementById("mainCommandSet");
michael@0 698 mcs.appendChild(fragCommands);
michael@0 699
michael@0 700 this.attachKeybindingsToBrowser(doc, fragKeys);
michael@0 701
michael@0 702 let mbs = doc.getElementById("mainBroadcasterSet");
michael@0 703 mbs.appendChild(fragBroadcasters);
michael@0 704
michael@0 705 let amp = doc.getElementById("appmenu_webDeveloper_popup");
michael@0 706 if (amp) {
michael@0 707 let amps = doc.getElementById("appmenu_devtools_separator");
michael@0 708 amp.insertBefore(fragAppMenuItems, amps);
michael@0 709 }
michael@0 710
michael@0 711 let mp = doc.getElementById("menuWebDeveloperPopup");
michael@0 712 let mps = doc.getElementById("menu_devtools_separator");
michael@0 713 mp.insertBefore(fragMenuItems, mps);
michael@0 714 },
michael@0 715
michael@0 716 /**
michael@0 717 * Add a menu entry for a tool definition
michael@0 718 *
michael@0 719 * @param {string} toolDefinition
michael@0 720 * Tool definition of the tool to add a menu entry.
michael@0 721 * @param {XULDocument} doc
michael@0 722 * The document to which the tool menu item is to be added.
michael@0 723 */
michael@0 724 _createToolMenuElements: function DT_createToolMenuElements(toolDefinition, doc) {
michael@0 725 let id = toolDefinition.id;
michael@0 726
michael@0 727 // Prevent multiple entries for the same tool.
michael@0 728 if (doc.getElementById("Tools:" + id)) {
michael@0 729 return;
michael@0 730 }
michael@0 731
michael@0 732 let cmd = doc.createElement("command");
michael@0 733 cmd.id = "Tools:" + id;
michael@0 734 cmd.setAttribute("oncommand",
michael@0 735 'gDevToolsBrowser.selectToolCommand(gBrowser, "' + id + '");');
michael@0 736
michael@0 737 let key = null;
michael@0 738 if (toolDefinition.key) {
michael@0 739 key = doc.createElement("key");
michael@0 740 key.id = "key_" + id;
michael@0 741
michael@0 742 if (toolDefinition.key.startsWith("VK_")) {
michael@0 743 key.setAttribute("keycode", toolDefinition.key);
michael@0 744 } else {
michael@0 745 key.setAttribute("key", toolDefinition.key);
michael@0 746 }
michael@0 747
michael@0 748 key.setAttribute("command", cmd.id);
michael@0 749 key.setAttribute("modifiers", toolDefinition.modifiers);
michael@0 750 }
michael@0 751
michael@0 752 let bc = doc.createElement("broadcaster");
michael@0 753 bc.id = "devtoolsMenuBroadcaster_" + id;
michael@0 754 bc.setAttribute("label", toolDefinition.menuLabel || toolDefinition.label);
michael@0 755 bc.setAttribute("command", cmd.id);
michael@0 756
michael@0 757 if (key) {
michael@0 758 bc.setAttribute("key", "key_" + id);
michael@0 759 }
michael@0 760
michael@0 761 let appmenuitem = doc.createElement("menuitem");
michael@0 762 appmenuitem.id = "appmenuitem_" + id;
michael@0 763 appmenuitem.setAttribute("observes", "devtoolsMenuBroadcaster_" + id);
michael@0 764
michael@0 765 let menuitem = doc.createElement("menuitem");
michael@0 766 menuitem.id = "menuitem_" + id;
michael@0 767 menuitem.setAttribute("observes", "devtoolsMenuBroadcaster_" + id);
michael@0 768
michael@0 769 if (toolDefinition.accesskey) {
michael@0 770 menuitem.setAttribute("accesskey", toolDefinition.accesskey);
michael@0 771 }
michael@0 772
michael@0 773 return {
michael@0 774 cmd: cmd,
michael@0 775 key: key,
michael@0 776 bc: bc,
michael@0 777 appmenuitem: appmenuitem,
michael@0 778 menuitem: menuitem
michael@0 779 };
michael@0 780 },
michael@0 781
michael@0 782 /**
michael@0 783 * Update the "Toggle Tools" checkbox in the developer tools menu. This is
michael@0 784 * called when a toolbox is created or destroyed.
michael@0 785 */
michael@0 786 _updateMenuCheckbox: function DT_updateMenuCheckbox() {
michael@0 787 for (let win of gDevToolsBrowser._trackedBrowserWindows) {
michael@0 788
michael@0 789 let hasToolbox = false;
michael@0 790 if (devtools.TargetFactory.isKnownTab(win.gBrowser.selectedTab)) {
michael@0 791 let target = devtools.TargetFactory.forTab(win.gBrowser.selectedTab);
michael@0 792 if (gDevTools._toolboxes.has(target)) {
michael@0 793 hasToolbox = true;
michael@0 794 }
michael@0 795 }
michael@0 796
michael@0 797 let broadcaster = win.document.getElementById("devtoolsMenuBroadcaster_DevToolbox");
michael@0 798 if (hasToolbox) {
michael@0 799 broadcaster.setAttribute("checked", "true");
michael@0 800 } else {
michael@0 801 broadcaster.removeAttribute("checked");
michael@0 802 }
michael@0 803 }
michael@0 804 },
michael@0 805
michael@0 806 /**
michael@0 807 * Connects to the SPS profiler when the developer tools are open.
michael@0 808 */
michael@0 809 _connectToProfiler: function DT_connectToProfiler() {
michael@0 810 let ProfilerController = devtools.require("devtools/profiler/controller");
michael@0 811
michael@0 812 for (let win of gDevToolsBrowser._trackedBrowserWindows) {
michael@0 813 if (devtools.TargetFactory.isKnownTab(win.gBrowser.selectedTab)) {
michael@0 814 let target = devtools.TargetFactory.forTab(win.gBrowser.selectedTab);
michael@0 815 if (gDevTools._toolboxes.has(target)) {
michael@0 816 target.makeRemote().then(() => {
michael@0 817 let profiler = new ProfilerController(target);
michael@0 818 profiler.connect();
michael@0 819 }).then(null, Cu.reportError);
michael@0 820
michael@0 821 return;
michael@0 822 }
michael@0 823 }
michael@0 824 }
michael@0 825 },
michael@0 826
michael@0 827 /**
michael@0 828 * Remove the menuitem for a tool to all open browser windows.
michael@0 829 *
michael@0 830 * @param {string} toolId
michael@0 831 * id of the tool to remove
michael@0 832 */
michael@0 833 _removeToolFromWindows: function DT_removeToolFromWindows(toolId) {
michael@0 834 for (let win of gDevToolsBrowser._trackedBrowserWindows) {
michael@0 835 gDevToolsBrowser._removeToolFromMenu(toolId, win.document);
michael@0 836 }
michael@0 837 },
michael@0 838
michael@0 839 /**
michael@0 840 * Remove a tool's menuitem from a window
michael@0 841 *
michael@0 842 * @param {string} toolId
michael@0 843 * Id of the tool to add a menu entry for
michael@0 844 * @param {XULDocument} doc
michael@0 845 * The document to which the tool menu item is to be removed from
michael@0 846 */
michael@0 847 _removeToolFromMenu: function DT_removeToolFromMenu(toolId, doc) {
michael@0 848 let command = doc.getElementById("Tools:" + toolId);
michael@0 849 if (command) {
michael@0 850 command.parentNode.removeChild(command);
michael@0 851 }
michael@0 852
michael@0 853 let key = doc.getElementById("key_" + toolId);
michael@0 854 if (key) {
michael@0 855 key.parentNode.removeChild(key);
michael@0 856 }
michael@0 857
michael@0 858 let bc = doc.getElementById("devtoolsMenuBroadcaster_" + toolId);
michael@0 859 if (bc) {
michael@0 860 bc.parentNode.removeChild(bc);
michael@0 861 }
michael@0 862
michael@0 863 let appmenuitem = doc.getElementById("appmenuitem_" + toolId);
michael@0 864 if (appmenuitem) {
michael@0 865 appmenuitem.parentNode.removeChild(appmenuitem);
michael@0 866 }
michael@0 867
michael@0 868 let menuitem = doc.getElementById("menuitem_" + toolId);
michael@0 869 if (menuitem) {
michael@0 870 menuitem.parentNode.removeChild(menuitem);
michael@0 871 }
michael@0 872 },
michael@0 873
michael@0 874 /**
michael@0 875 * Called on browser unload to remove menu entries, toolboxes and event
michael@0 876 * listeners from the closed browser window.
michael@0 877 *
michael@0 878 * @param {XULWindow} win
michael@0 879 * The window containing the menu entry
michael@0 880 */
michael@0 881 forgetBrowserWindow: function DT_forgetBrowserWindow(win) {
michael@0 882 gDevToolsBrowser._trackedBrowserWindows.delete(win);
michael@0 883
michael@0 884 // Destroy toolboxes for closed window
michael@0 885 for (let [target, toolbox] of gDevTools._toolboxes) {
michael@0 886 if (toolbox.frame && toolbox.frame.ownerDocument.defaultView == win) {
michael@0 887 toolbox.destroy();
michael@0 888 }
michael@0 889 }
michael@0 890
michael@0 891 let tabContainer = win.document.getElementById("tabbrowser-tabs")
michael@0 892 tabContainer.removeEventListener("TabSelect",
michael@0 893 gDevToolsBrowser._updateMenuCheckbox, false);
michael@0 894 },
michael@0 895
michael@0 896 /**
michael@0 897 * All browser windows have been closed, tidy up remaining objects.
michael@0 898 */
michael@0 899 destroy: function() {
michael@0 900 gDevTools.off("toolbox-ready", gDevToolsBrowser._connectToProfiler);
michael@0 901 Services.prefs.removeObserver("devtools.", gDevToolsBrowser);
michael@0 902 Services.obs.removeObserver(gDevToolsBrowser.destroy, "quit-application");
michael@0 903 },
michael@0 904 }
michael@0 905
michael@0 906 this.gDevToolsBrowser = gDevToolsBrowser;
michael@0 907
michael@0 908 gDevTools.on("tool-registered", function(ev, toolId) {
michael@0 909 let toolDefinition = gDevTools._tools.get(toolId);
michael@0 910 gDevToolsBrowser._addToolToWindows(toolDefinition);
michael@0 911 });
michael@0 912
michael@0 913 gDevTools.on("tool-unregistered", function(ev, toolId) {
michael@0 914 if (typeof toolId != "string") {
michael@0 915 toolId = toolId.id;
michael@0 916 }
michael@0 917 gDevToolsBrowser._removeToolFromWindows(toolId);
michael@0 918 });
michael@0 919
michael@0 920 gDevTools.on("toolbox-ready", gDevToolsBrowser._updateMenuCheckbox);
michael@0 921 gDevTools.on("toolbox-ready", gDevToolsBrowser._connectToProfiler);
michael@0 922 gDevTools.on("toolbox-destroyed", gDevToolsBrowser._updateMenuCheckbox);
michael@0 923
michael@0 924 Services.obs.addObserver(gDevToolsBrowser.destroy, "quit-application", false);
michael@0 925
michael@0 926 // Load the browser devtools main module as the loader's main module.
michael@0 927 devtools.main("main");

mercurial