Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
5 "use strict";
7 const {Cc, Ci, Cu} = require("chrome");
9 loader.lazyImporter(this, "devtools", "resource://gre/modules/devtools/Loader.jsm");
10 loader.lazyImporter(this, "promise", "resource://gre/modules/Promise.jsm", "Promise");
11 loader.lazyGetter(this, "HUDService", () => require("devtools/webconsole/hudservice"));
12 loader.lazyGetter(this, "EventEmitter", () => require("devtools/toolkit/event-emitter"));
13 loader.lazyImporter(this, "gDevTools", "resource:///modules/devtools/gDevTools.jsm");
15 /**
16 * A DevToolPanel that controls the Web Console.
17 */
18 function WebConsolePanel(iframeWindow, toolbox)
19 {
20 this._frameWindow = iframeWindow;
21 this._toolbox = toolbox;
22 EventEmitter.decorate(this);
23 }
25 exports.WebConsolePanel = WebConsolePanel;
27 WebConsolePanel.prototype = {
28 hud: null,
30 /**
31 * Called by the WebConsole's onkey command handler.
32 * If the WebConsole is opened, check if the JSTerm's input line has focus.
33 * If not, focus it.
34 */
35 focusInput: function WCP_focusInput()
36 {
37 let inputNode = this.hud.jsterm.inputNode;
39 if (!inputNode.getAttribute("focused"))
40 {
41 inputNode.focus();
42 }
43 },
45 /**
46 * Open is effectively an asynchronous constructor.
47 *
48 * @return object
49 * A promise that is resolved when the Web Console completes opening.
50 */
51 open: function WCP_open()
52 {
53 let parentDoc = this._toolbox.doc;
54 let iframe = parentDoc.getElementById("toolbox-panel-iframe-webconsole");
55 iframe.className = "web-console-frame";
57 // Make sure the iframe content window is ready.
58 let deferredIframe = promise.defer();
59 let win, doc;
60 if ((win = iframe.contentWindow) &&
61 (doc = win.document) &&
62 doc.readyState == "complete") {
63 deferredIframe.resolve(null);
64 }
65 else {
66 iframe.addEventListener("load", function onIframeLoad() {
67 iframe.removeEventListener("load", onIframeLoad, true);
68 deferredIframe.resolve(null);
69 }, true);
70 }
72 // Local debugging needs to make the target remote.
73 let promiseTarget;
74 if (!this.target.isRemote) {
75 promiseTarget = this.target.makeRemote();
76 }
77 else {
78 promiseTarget = promise.resolve(this.target);
79 }
81 // 1. Wait for the iframe to load.
82 // 2. Wait for the remote target.
83 // 3. Open the Web Console.
84 return deferredIframe.promise
85 .then(() => promiseTarget)
86 .then((aTarget) => {
87 this._frameWindow._remoteTarget = aTarget;
89 let webConsoleUIWindow = iframe.contentWindow.wrappedJSObject;
90 let chromeWindow = iframe.ownerDocument.defaultView;
91 return HUDService.openWebConsole(this.target, webConsoleUIWindow,
92 chromeWindow);
93 })
94 .then((aWebConsole) => {
95 this.hud = aWebConsole;
96 this._isReady = true;
97 this.emit("ready");
98 return this;
99 }, (aReason) => {
100 let msg = "WebConsolePanel open failed. " +
101 aReason.error + ": " + aReason.message;
102 dump(msg + "\n");
103 Cu.reportError(msg);
104 });
105 },
107 get target() this._toolbox.target,
109 _isReady: false,
110 get isReady() this._isReady,
112 destroy: function WCP_destroy()
113 {
114 if (this._destroyer) {
115 return this._destroyer;
116 }
118 this._destroyer = this.hud.destroy();
119 this._destroyer.then(() => this.emit("destroyed"));
121 return this._destroyer;
122 },
123 };