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/. */
9 } = require("resource://devtools/shared/commands/commands-factory.js");
11 loader
.lazyRequireGetter(
14 "resource://devtools/client/definitions.js",
17 loader
.lazyRequireGetter(
20 "resource://devtools/client/webconsole/utils/l10n.js"
22 loader
.lazyRequireGetter(
25 "resource://devtools/client/webconsole/browser-console.js"
28 const BC_WINDOW_FEATURES
=
29 "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
31 class BrowserConsoleManager
{
33 this._browserConsole
= null;
34 this._browserConsoleInitializing
= null;
35 this._browerConsoleSessionState
= false;
38 storeBrowserConsoleSessionState() {
39 this._browerConsoleSessionState
= !!this.getBrowserConsole();
42 getBrowserConsoleSessionState() {
43 return this._browerConsoleSessionState
;
47 * Open a Browser Console for the current commands context.
49 * @param nsIDOMWindow iframeWindow
50 * The window where the browser console UI is already loaded.
52 * A promise object for the opening of the new BrowserConsole instance.
54 async
openBrowserConsole(win
) {
55 const hud
= new BrowserConsole(this.commands
, win
, win
);
56 this._browserConsole
= hud
;
62 * Close the opened Browser Console
64 async
closeBrowserConsole() {
65 if (!this._browserConsole
) {
69 // Ensure destroying the commands,
70 // even if the console throws during cleanup.
72 await
this._browserConsole
.destroy();
76 this._browserConsole
= null;
78 await
this.commands
.destroy();
83 * Toggle the Browser Console.
85 async
toggleBrowserConsole() {
86 if (this._browserConsole
) {
87 return this.closeBrowserConsole();
90 if (this._browserConsoleInitializing
) {
91 return this._browserConsoleInitializing
;
94 // Temporarily cache the async startup sequence so that if toggleBrowserConsole
95 // gets called again we can return this console instead of opening another one.
96 this._browserConsoleInitializing
= (async () => {
97 this.commands
= await CommandsFactory
.forBrowserConsole();
98 const win
= await
this.openWindow();
99 const browserConsole
= await
this.openBrowserConsole(win
);
100 return browserConsole
;
104 const browserConsole
= await
this._browserConsoleInitializing
;
105 this._browserConsoleInitializing
= null;
106 return browserConsole
;
108 // Ensure always clearing this field, even in case of exception,
109 // which may happen when closing during initialization.
110 this._browserConsoleInitializing
= null;
116 const win
= Services
.ww
.openWindow(
118 Tools
.webConsole
.url
,
124 await
new Promise(resolve
=> {
125 win
.addEventListener("DOMContentLoaded", resolve
, { once
: true });
128 // It's important to declare the unload *after* the initial "DOMContentLoaded",
129 // otherwise, since the window is navigated to Tools.webConsole.url, an unload event
131 win
.addEventListener("unload", this.closeBrowserConsole
.bind(this), {
135 this.updateWindowTitle(win
);
140 * Opens or focuses the Browser Console.
142 openBrowserConsoleOrFocus() {
143 const hud
= this.getBrowserConsole();
145 hud
.iframeWindow
.focus();
146 return Promise
.resolve(hud
);
149 return this.toggleBrowserConsole();
153 * Get the Browser Console instance, if open.
155 * @return object|null
156 * A BrowserConsole instance or null if the Browser Console is not
159 getBrowserConsole() {
160 return this._browserConsole
;
164 * Set the title of the Browser Console window.
166 * @param {Window} win: The BrowserConsole window
168 updateWindowTitle(win
) {
170 const mode
= Services
.prefs
.getCharPref(
171 "devtools.browsertoolbox.scope",
174 if (mode
== "everything") {
175 title
= l10n
.getStr("multiProcessBrowserConsole.title");
176 } else if (mode
== "parent-process") {
177 title
= l10n
.getStr("parentProcessBrowserConsole.title");
179 throw new Error("Unsupported mode: " + mode
);
182 win
.document
.title
= title
;
186 exports
.BrowserConsoleManager
= new BrowserConsoleManager();