From 147edc04a3c4b5026f6bd942a17e945c13c19fe4 Mon Sep 17 00:00:00 2001 From: Mihai Sucan Date: Mon, 23 Sep 2013 13:43:37 +0300 Subject: [PATCH] Bug 860672 - Command line option -jsconsole should open the Browser Console. r=robcee --- browser/devtools/devtools-clhandler.js | 40 +++++++++++++++++++ browser/devtools/devtools-clhandler.manifest | 2 + browser/devtools/moz.build | 5 +++ browser/devtools/webconsole/hudservice.js | 5 ++- .../webconsole/test/browser_console.js | 2 +- .../test/browser_console_dead_objects.js | 5 ++- toolkit/devtools/server/actors/webbrowser.js | 15 ++++++- toolkit/devtools/server/actors/webconsole.js | 12 +++++- 8 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 browser/devtools/devtools-clhandler.js create mode 100644 browser/devtools/devtools-clhandler.manifest diff --git a/browser/devtools/devtools-clhandler.js b/browser/devtools/devtools-clhandler.js new file mode 100644 index 000000000000..070d48e8ed24 --- /dev/null +++ b/browser/devtools/devtools-clhandler.js @@ -0,0 +1,40 @@ +/* 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/. */ + +const { classes: Cc, interfaces: Ci, utils: Cu } = Components; +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +function devtoolsCommandlineHandler() { +} +devtoolsCommandlineHandler.prototype = { + handle: function(cmdLine) { + if (!cmdLine.handleFlag("jsconsole", false)) { + return; + } + + Cu.import("resource://gre/modules/Services.jsm"); + let window = Services.wm.getMostRecentWindow("devtools:webconsole"); + if (!window) { + let devtools = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools; + // Load the browser devtools main module as the loader's main module. + devtools.main("main"); + let hudservice = devtools.require("devtools/webconsole/hudservice"); + let console = Cu.import("resource://gre/modules/devtools/Console.jsm", {}).console; + hudservice.toggleBrowserConsole().then(null, console.error); + } else { + window.focus(); // the Browser Console was already open + } + + if (cmdLine.state == Ci.nsICommandLine.STATE_REMOTE_AUTO) { + cmdLine.preventDefault = true; + } + }, + + helpInfo : " -jsconsole Open the Browser Console.\n", + + classID: Components.ID("{9e9a9283-0ce9-4e4a-8f1c-ba129a032c32}"), + QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler]), +}; + +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([devtoolsCommandlineHandler]); diff --git a/browser/devtools/devtools-clhandler.manifest b/browser/devtools/devtools-clhandler.manifest new file mode 100644 index 000000000000..5dbcf9f18ab3 --- /dev/null +++ b/browser/devtools/devtools-clhandler.manifest @@ -0,0 +1,2 @@ +component {9e9a9283-0ce9-4e4a-8f1c-ba129a032c32} devtools-clhandler.js +contract @mozilla.org/toolkit/console-clh;1 {9e9a9283-0ce9-4e4a-8f1c-ba129a032c32} diff --git a/browser/devtools/moz.build b/browser/devtools/moz.build index 9f66af5b6604..92116b82765a 100644 --- a/browser/devtools/moz.build +++ b/browser/devtools/moz.build @@ -24,3 +24,8 @@ DIRS += [ 'fontinspector', 'app-manager', ] + +EXTRA_COMPONENTS += [ + 'devtools-clhandler.js', + 'devtools-clhandler.manifest', +] diff --git a/browser/devtools/webconsole/hudservice.js b/browser/devtools/webconsole/hudservice.js index fb99ac96584d..52a0f5d5bad4 100644 --- a/browser/devtools/webconsole/hudservice.js +++ b/browser/devtools/webconsole/hudservice.js @@ -254,13 +254,14 @@ HUD_SERVICE.prototype = return deferred.promise; } - connect().then(getTarget).then(openWindow).then((aWindow) => + connect().then(getTarget).then(openWindow).then((aWindow) => { this.openBrowserConsole(target, aWindow, aWindow) .then((aBrowserConsole) => { this._browserConsoleID = aBrowserConsole.hudId; this._browserConsoleDefer.resolve(aBrowserConsole); this._browserConsoleDefer = null; - })); + }) + }, console.error); return this._browserConsoleDefer.promise; }, diff --git a/browser/devtools/webconsole/test/browser_console.js b/browser/devtools/webconsole/test/browser_console.js index 8701dedbc3ac..dc1ee4aca476 100644 --- a/browser/devtools/webconsole/test/browser_console.js +++ b/browser/devtools/webconsole/test/browser_console.js @@ -62,7 +62,7 @@ function consoleOpened(hud) let text = output.textContent; chromeConsole = text.indexOf("bug587757a"); contentConsole = text.indexOf("bug587757b"); - execValue = text.indexOf("browser.xul"); + execValue = text.indexOf("webconsole.xul"); exception = text.indexOf("foobarExceptionBug587757"); xhrRequest = text.indexOf("test-console.html"); } diff --git a/browser/devtools/webconsole/test/browser_console_dead_objects.js b/browser/devtools/webconsole/test/browser_console_dead_objects.js index 86551a2fe8df..32cfc6def4f9 100644 --- a/browser/devtools/webconsole/test/browser_console_dead_objects.js +++ b/browser/devtools/webconsole/test/browser_console_dead_objects.js @@ -24,7 +24,10 @@ function test() ok(hud, "browser console opened"); hud.jsterm.clearOutput(); - hud.jsterm.execute("foobarzTezt = content.document", onAddVariable); + hud.jsterm.execute("Cu = Components.utils;" + + "Cu.import('resource://gre/modules/Services.jsm');" + + "chromeWindow = Services.wm.getMostRecentWindow('navigator:browser');" + + "foobarzTezt = chromeWindow.content.document", onAddVariable); } function onAddVariable() diff --git a/toolkit/devtools/server/actors/webbrowser.js b/toolkit/devtools/server/actors/webbrowser.js index a9aa8a56685a..761ce3a13ac1 100644 --- a/toolkit/devtools/server/actors/webbrowser.js +++ b/toolkit/devtools/server/actors/webbrowser.js @@ -185,8 +185,18 @@ function BrowserTabList(aConnection) BrowserTabList.prototype.constructor = BrowserTabList; +/** + * Get the selected browser for the given navigator:browser window. + * @private + * @param aWindow nsIChromeWindow + * The navigator:browser window for which you want the selected browser. + * @return nsIDOMElement|null + * The currently selected xul:browser element, if any. Note that the + * browser window might not be loaded yet - the function will return + * |null| in such cases. + */ BrowserTabList.prototype._getSelectedBrowser = function(aWindow) { - return aWindow.gBrowser.selectedBrowser; + return aWindow.gBrowser ? aWindow.gBrowser.selectedBrowser : null; }; BrowserTabList.prototype._getChildren = function(aWindow) { @@ -209,6 +219,9 @@ BrowserTabList.prototype.getList = function() { // Iterate over all navigator:browser XUL windows. for (let win of allAppShellDOMWindows(DebuggerServer.chromeWindowType)) { let selectedBrowser = this._getSelectedBrowser(win); + if (!selectedBrowser) { + continue; + } // For each tab in this XUL window, ensure that we have an actor for // it, reusing existing actors where possible. We actually iterate diff --git a/toolkit/devtools/server/actors/webconsole.js b/toolkit/devtools/server/actors/webconsole.js index aee95a9359b9..e148152afecc 100644 --- a/toolkit/devtools/server/actors/webconsole.js +++ b/toolkit/devtools/server/actors/webconsole.js @@ -118,10 +118,18 @@ WebConsoleActor.prototype = conn: null, /** - * The content window we work with. + * The window we work with. * @type nsIDOMWindow */ - get window() this.parentActor.window, + get window() { + if (this.parentActor.isRootActor) { + // Try to find the Browser Console window, otherwise use the window of + // the root actor. + let window = Services.wm.getMostRecentWindow("devtools:webconsole"); + return window || this.parentActor.window; + } + return this.parentActor.window; + }, /** * The ConsoleServiceListener instance.