Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const {Cc, Ci, Cu} = require("chrome"); |
michael@0 | 8 | |
michael@0 | 9 | loader.lazyImporter(this, "devtools", "resource://gre/modules/devtools/Loader.jsm"); |
michael@0 | 10 | loader.lazyImporter(this, "promise", "resource://gre/modules/Promise.jsm", "Promise"); |
michael@0 | 11 | loader.lazyGetter(this, "HUDService", () => require("devtools/webconsole/hudservice")); |
michael@0 | 12 | loader.lazyGetter(this, "EventEmitter", () => require("devtools/toolkit/event-emitter")); |
michael@0 | 13 | loader.lazyImporter(this, "gDevTools", "resource:///modules/devtools/gDevTools.jsm"); |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * A DevToolPanel that controls the Web Console. |
michael@0 | 17 | */ |
michael@0 | 18 | function WebConsolePanel(iframeWindow, toolbox) |
michael@0 | 19 | { |
michael@0 | 20 | this._frameWindow = iframeWindow; |
michael@0 | 21 | this._toolbox = toolbox; |
michael@0 | 22 | EventEmitter.decorate(this); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | exports.WebConsolePanel = WebConsolePanel; |
michael@0 | 26 | |
michael@0 | 27 | WebConsolePanel.prototype = { |
michael@0 | 28 | hud: null, |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * Called by the WebConsole's onkey command handler. |
michael@0 | 32 | * If the WebConsole is opened, check if the JSTerm's input line has focus. |
michael@0 | 33 | * If not, focus it. |
michael@0 | 34 | */ |
michael@0 | 35 | focusInput: function WCP_focusInput() |
michael@0 | 36 | { |
michael@0 | 37 | let inputNode = this.hud.jsterm.inputNode; |
michael@0 | 38 | |
michael@0 | 39 | if (!inputNode.getAttribute("focused")) |
michael@0 | 40 | { |
michael@0 | 41 | inputNode.focus(); |
michael@0 | 42 | } |
michael@0 | 43 | }, |
michael@0 | 44 | |
michael@0 | 45 | /** |
michael@0 | 46 | * Open is effectively an asynchronous constructor. |
michael@0 | 47 | * |
michael@0 | 48 | * @return object |
michael@0 | 49 | * A promise that is resolved when the Web Console completes opening. |
michael@0 | 50 | */ |
michael@0 | 51 | open: function WCP_open() |
michael@0 | 52 | { |
michael@0 | 53 | let parentDoc = this._toolbox.doc; |
michael@0 | 54 | let iframe = parentDoc.getElementById("toolbox-panel-iframe-webconsole"); |
michael@0 | 55 | iframe.className = "web-console-frame"; |
michael@0 | 56 | |
michael@0 | 57 | // Make sure the iframe content window is ready. |
michael@0 | 58 | let deferredIframe = promise.defer(); |
michael@0 | 59 | let win, doc; |
michael@0 | 60 | if ((win = iframe.contentWindow) && |
michael@0 | 61 | (doc = win.document) && |
michael@0 | 62 | doc.readyState == "complete") { |
michael@0 | 63 | deferredIframe.resolve(null); |
michael@0 | 64 | } |
michael@0 | 65 | else { |
michael@0 | 66 | iframe.addEventListener("load", function onIframeLoad() { |
michael@0 | 67 | iframe.removeEventListener("load", onIframeLoad, true); |
michael@0 | 68 | deferredIframe.resolve(null); |
michael@0 | 69 | }, true); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // Local debugging needs to make the target remote. |
michael@0 | 73 | let promiseTarget; |
michael@0 | 74 | if (!this.target.isRemote) { |
michael@0 | 75 | promiseTarget = this.target.makeRemote(); |
michael@0 | 76 | } |
michael@0 | 77 | else { |
michael@0 | 78 | promiseTarget = promise.resolve(this.target); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // 1. Wait for the iframe to load. |
michael@0 | 82 | // 2. Wait for the remote target. |
michael@0 | 83 | // 3. Open the Web Console. |
michael@0 | 84 | return deferredIframe.promise |
michael@0 | 85 | .then(() => promiseTarget) |
michael@0 | 86 | .then((aTarget) => { |
michael@0 | 87 | this._frameWindow._remoteTarget = aTarget; |
michael@0 | 88 | |
michael@0 | 89 | let webConsoleUIWindow = iframe.contentWindow.wrappedJSObject; |
michael@0 | 90 | let chromeWindow = iframe.ownerDocument.defaultView; |
michael@0 | 91 | return HUDService.openWebConsole(this.target, webConsoleUIWindow, |
michael@0 | 92 | chromeWindow); |
michael@0 | 93 | }) |
michael@0 | 94 | .then((aWebConsole) => { |
michael@0 | 95 | this.hud = aWebConsole; |
michael@0 | 96 | this._isReady = true; |
michael@0 | 97 | this.emit("ready"); |
michael@0 | 98 | return this; |
michael@0 | 99 | }, (aReason) => { |
michael@0 | 100 | let msg = "WebConsolePanel open failed. " + |
michael@0 | 101 | aReason.error + ": " + aReason.message; |
michael@0 | 102 | dump(msg + "\n"); |
michael@0 | 103 | Cu.reportError(msg); |
michael@0 | 104 | }); |
michael@0 | 105 | }, |
michael@0 | 106 | |
michael@0 | 107 | get target() this._toolbox.target, |
michael@0 | 108 | |
michael@0 | 109 | _isReady: false, |
michael@0 | 110 | get isReady() this._isReady, |
michael@0 | 111 | |
michael@0 | 112 | destroy: function WCP_destroy() |
michael@0 | 113 | { |
michael@0 | 114 | if (this._destroyer) { |
michael@0 | 115 | return this._destroyer; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | this._destroyer = this.hud.destroy(); |
michael@0 | 119 | this._destroyer.then(() => this.emit("destroyed")); |
michael@0 | 120 | |
michael@0 | 121 | return this._destroyer; |
michael@0 | 122 | }, |
michael@0 | 123 | }; |