panel.js (2627B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 loader.lazyRequireGetter( 8 this, 9 "WebConsole", 10 "resource://devtools/client/webconsole/webconsole.js" 11 ); 12 const EventEmitter = require("resource://devtools/shared/event-emitter.js"); 13 14 /** 15 * A DevToolPanel that controls the Web Console. 16 */ 17 class WebConsolePanel extends EventEmitter { 18 constructor(iframeWindow, toolbox, commands) { 19 super(); 20 21 this._frameWindow = iframeWindow; 22 this._toolbox = toolbox; 23 this._commands = commands; 24 } 25 26 hud = null; 27 /** 28 * Called by the WebConsole's onkey command handler. 29 * If the WebConsole is opened, check if the JSTerm's input line has focus. 30 * If not, focus it. 31 */ 32 focusInput() { 33 this.hud.jsterm.focus(); 34 } 35 36 /** 37 * Open is effectively an asynchronous constructor. 38 * 39 * @return object 40 * A promise that is resolved when the Web Console completes opening. 41 */ 42 async open() { 43 try { 44 const parentDoc = this._toolbox.doc; 45 const iframe = parentDoc.getElementById( 46 "toolbox-panel-iframe-webconsole" 47 ); 48 49 // Make sure the iframe content window is ready. 50 const win = iframe.contentWindow; 51 const doc = win && win.document; 52 if (!doc || doc.readyState !== "complete") { 53 await new Promise(resolve => { 54 iframe.addEventListener("load", resolve, { 55 capture: true, 56 once: true, 57 }); 58 }); 59 } 60 61 const webConsoleUIWindow = iframe.contentWindow.wrappedJSObject; 62 const chromeWindow = iframe.ownerDocument.defaultView; 63 64 // Open the Web Console. 65 this.hud = new WebConsole( 66 this._toolbox, 67 this._commands, 68 webConsoleUIWindow, 69 chromeWindow 70 ); 71 await this.hud.init(); 72 73 // Pipe 'reloaded' event from WebConsoleUI to WebConsolePanel. 74 // These events are listened by the Toolbox. 75 this.hud.ui.on("reloaded", () => { 76 this.emit("reloaded"); 77 }); 78 } catch (e) { 79 const msg = "WebConsolePanel open failed. " + e.error + ": " + e.message; 80 dump(msg + "\n"); 81 console.error(msg, e); 82 } 83 84 return this; 85 } 86 87 get currentTarget() { 88 return this._toolbox.target; 89 } 90 91 destroy() { 92 if (!this._toolbox) { 93 return; 94 } 95 this.hud.destroy(); 96 this.hud = null; 97 this._frameWindow = null; 98 this._toolbox = null; 99 this.emit("destroyed"); 100 } 101 } 102 103 exports.WebConsolePanel = WebConsolePanel;