1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/webconsole/panel.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const {Cc, Ci, Cu} = require("chrome"); 1.11 + 1.12 +loader.lazyImporter(this, "devtools", "resource://gre/modules/devtools/Loader.jsm"); 1.13 +loader.lazyImporter(this, "promise", "resource://gre/modules/Promise.jsm", "Promise"); 1.14 +loader.lazyGetter(this, "HUDService", () => require("devtools/webconsole/hudservice")); 1.15 +loader.lazyGetter(this, "EventEmitter", () => require("devtools/toolkit/event-emitter")); 1.16 +loader.lazyImporter(this, "gDevTools", "resource:///modules/devtools/gDevTools.jsm"); 1.17 + 1.18 +/** 1.19 + * A DevToolPanel that controls the Web Console. 1.20 + */ 1.21 +function WebConsolePanel(iframeWindow, toolbox) 1.22 +{ 1.23 + this._frameWindow = iframeWindow; 1.24 + this._toolbox = toolbox; 1.25 + EventEmitter.decorate(this); 1.26 +} 1.27 + 1.28 +exports.WebConsolePanel = WebConsolePanel; 1.29 + 1.30 +WebConsolePanel.prototype = { 1.31 + hud: null, 1.32 + 1.33 + /** 1.34 + * Called by the WebConsole's onkey command handler. 1.35 + * If the WebConsole is opened, check if the JSTerm's input line has focus. 1.36 + * If not, focus it. 1.37 + */ 1.38 + focusInput: function WCP_focusInput() 1.39 + { 1.40 + let inputNode = this.hud.jsterm.inputNode; 1.41 + 1.42 + if (!inputNode.getAttribute("focused")) 1.43 + { 1.44 + inputNode.focus(); 1.45 + } 1.46 + }, 1.47 + 1.48 + /** 1.49 + * Open is effectively an asynchronous constructor. 1.50 + * 1.51 + * @return object 1.52 + * A promise that is resolved when the Web Console completes opening. 1.53 + */ 1.54 + open: function WCP_open() 1.55 + { 1.56 + let parentDoc = this._toolbox.doc; 1.57 + let iframe = parentDoc.getElementById("toolbox-panel-iframe-webconsole"); 1.58 + iframe.className = "web-console-frame"; 1.59 + 1.60 + // Make sure the iframe content window is ready. 1.61 + let deferredIframe = promise.defer(); 1.62 + let win, doc; 1.63 + if ((win = iframe.contentWindow) && 1.64 + (doc = win.document) && 1.65 + doc.readyState == "complete") { 1.66 + deferredIframe.resolve(null); 1.67 + } 1.68 + else { 1.69 + iframe.addEventListener("load", function onIframeLoad() { 1.70 + iframe.removeEventListener("load", onIframeLoad, true); 1.71 + deferredIframe.resolve(null); 1.72 + }, true); 1.73 + } 1.74 + 1.75 + // Local debugging needs to make the target remote. 1.76 + let promiseTarget; 1.77 + if (!this.target.isRemote) { 1.78 + promiseTarget = this.target.makeRemote(); 1.79 + } 1.80 + else { 1.81 + promiseTarget = promise.resolve(this.target); 1.82 + } 1.83 + 1.84 + // 1. Wait for the iframe to load. 1.85 + // 2. Wait for the remote target. 1.86 + // 3. Open the Web Console. 1.87 + return deferredIframe.promise 1.88 + .then(() => promiseTarget) 1.89 + .then((aTarget) => { 1.90 + this._frameWindow._remoteTarget = aTarget; 1.91 + 1.92 + let webConsoleUIWindow = iframe.contentWindow.wrappedJSObject; 1.93 + let chromeWindow = iframe.ownerDocument.defaultView; 1.94 + return HUDService.openWebConsole(this.target, webConsoleUIWindow, 1.95 + chromeWindow); 1.96 + }) 1.97 + .then((aWebConsole) => { 1.98 + this.hud = aWebConsole; 1.99 + this._isReady = true; 1.100 + this.emit("ready"); 1.101 + return this; 1.102 + }, (aReason) => { 1.103 + let msg = "WebConsolePanel open failed. " + 1.104 + aReason.error + ": " + aReason.message; 1.105 + dump(msg + "\n"); 1.106 + Cu.reportError(msg); 1.107 + }); 1.108 + }, 1.109 + 1.110 + get target() this._toolbox.target, 1.111 + 1.112 + _isReady: false, 1.113 + get isReady() this._isReady, 1.114 + 1.115 + destroy: function WCP_destroy() 1.116 + { 1.117 + if (this._destroyer) { 1.118 + return this._destroyer; 1.119 + } 1.120 + 1.121 + this._destroyer = this.hud.destroy(); 1.122 + this._destroyer.then(() => this.emit("destroyed")); 1.123 + 1.124 + return this._destroyer; 1.125 + }, 1.126 +};