michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const {Cc, Ci, Cu} = require("chrome"); michael@0: michael@0: loader.lazyImporter(this, "devtools", "resource://gre/modules/devtools/Loader.jsm"); michael@0: loader.lazyImporter(this, "promise", "resource://gre/modules/Promise.jsm", "Promise"); michael@0: loader.lazyGetter(this, "HUDService", () => require("devtools/webconsole/hudservice")); michael@0: loader.lazyGetter(this, "EventEmitter", () => require("devtools/toolkit/event-emitter")); michael@0: loader.lazyImporter(this, "gDevTools", "resource:///modules/devtools/gDevTools.jsm"); michael@0: michael@0: /** michael@0: * A DevToolPanel that controls the Web Console. michael@0: */ michael@0: function WebConsolePanel(iframeWindow, toolbox) michael@0: { michael@0: this._frameWindow = iframeWindow; michael@0: this._toolbox = toolbox; michael@0: EventEmitter.decorate(this); michael@0: } michael@0: michael@0: exports.WebConsolePanel = WebConsolePanel; michael@0: michael@0: WebConsolePanel.prototype = { michael@0: hud: null, michael@0: michael@0: /** michael@0: * Called by the WebConsole's onkey command handler. michael@0: * If the WebConsole is opened, check if the JSTerm's input line has focus. michael@0: * If not, focus it. michael@0: */ michael@0: focusInput: function WCP_focusInput() michael@0: { michael@0: let inputNode = this.hud.jsterm.inputNode; michael@0: michael@0: if (!inputNode.getAttribute("focused")) michael@0: { michael@0: inputNode.focus(); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Open is effectively an asynchronous constructor. michael@0: * michael@0: * @return object michael@0: * A promise that is resolved when the Web Console completes opening. michael@0: */ michael@0: open: function WCP_open() michael@0: { michael@0: let parentDoc = this._toolbox.doc; michael@0: let iframe = parentDoc.getElementById("toolbox-panel-iframe-webconsole"); michael@0: iframe.className = "web-console-frame"; michael@0: michael@0: // Make sure the iframe content window is ready. michael@0: let deferredIframe = promise.defer(); michael@0: let win, doc; michael@0: if ((win = iframe.contentWindow) && michael@0: (doc = win.document) && michael@0: doc.readyState == "complete") { michael@0: deferredIframe.resolve(null); michael@0: } michael@0: else { michael@0: iframe.addEventListener("load", function onIframeLoad() { michael@0: iframe.removeEventListener("load", onIframeLoad, true); michael@0: deferredIframe.resolve(null); michael@0: }, true); michael@0: } michael@0: michael@0: // Local debugging needs to make the target remote. michael@0: let promiseTarget; michael@0: if (!this.target.isRemote) { michael@0: promiseTarget = this.target.makeRemote(); michael@0: } michael@0: else { michael@0: promiseTarget = promise.resolve(this.target); michael@0: } michael@0: michael@0: // 1. Wait for the iframe to load. michael@0: // 2. Wait for the remote target. michael@0: // 3. Open the Web Console. michael@0: return deferredIframe.promise michael@0: .then(() => promiseTarget) michael@0: .then((aTarget) => { michael@0: this._frameWindow._remoteTarget = aTarget; michael@0: michael@0: let webConsoleUIWindow = iframe.contentWindow.wrappedJSObject; michael@0: let chromeWindow = iframe.ownerDocument.defaultView; michael@0: return HUDService.openWebConsole(this.target, webConsoleUIWindow, michael@0: chromeWindow); michael@0: }) michael@0: .then((aWebConsole) => { michael@0: this.hud = aWebConsole; michael@0: this._isReady = true; michael@0: this.emit("ready"); michael@0: return this; michael@0: }, (aReason) => { michael@0: let msg = "WebConsolePanel open failed. " + michael@0: aReason.error + ": " + aReason.message; michael@0: dump(msg + "\n"); michael@0: Cu.reportError(msg); michael@0: }); michael@0: }, michael@0: michael@0: get target() this._toolbox.target, michael@0: michael@0: _isReady: false, michael@0: get isReady() this._isReady, michael@0: michael@0: destroy: function WCP_destroy() michael@0: { michael@0: if (this._destroyer) { michael@0: return this._destroyer; michael@0: } michael@0: michael@0: this._destroyer = this.hud.destroy(); michael@0: this._destroyer.then(() => this.emit("destroyed")); michael@0: michael@0: return this._destroyer; michael@0: }, michael@0: };