Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / devtools / client / webconsole / browser-console-manager.js
blobb38391f685700b06eca0ebb154328ebcf9f7fada
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 {
8 CommandsFactory,
9 } = require("resource://devtools/shared/commands/commands-factory.js");
11 loader.lazyRequireGetter(
12 this,
13 "Tools",
14 "resource://devtools/client/definitions.js",
15 true
17 loader.lazyRequireGetter(
18 this,
19 "l10n",
20 "resource://devtools/client/webconsole/utils/l10n.js"
22 loader.lazyRequireGetter(
23 this,
24 "BrowserConsole",
25 "resource://devtools/client/webconsole/browser-console.js"
28 const BC_WINDOW_FEATURES =
29 "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
31 class BrowserConsoleManager {
32 constructor() {
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;
46 /**
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.
51 * @return object
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;
57 await hud.init();
58 return hud;
61 /**
62 * Close the opened Browser Console
64 async closeBrowserConsole() {
65 if (!this._browserConsole) {
66 return;
69 // Ensure destroying the commands,
70 // even if the console throws during cleanup.
71 try {
72 await this._browserConsole.destroy();
73 } catch (e) {
74 console.error(e);
76 this._browserConsole = null;
78 await this.commands.destroy();
79 this.commands = null;
82 /**
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;
101 })();
103 try {
104 const browserConsole = await this._browserConsoleInitializing;
105 this._browserConsoleInitializing = null;
106 return browserConsole;
107 } catch (e) {
108 // Ensure always clearing this field, even in case of exception,
109 // which may happen when closing during initialization.
110 this._browserConsoleInitializing = null;
111 throw e;
115 async openWindow() {
116 const win = Services.ww.openWindow(
117 null,
118 Tools.webConsole.url,
119 "_blank",
120 BC_WINDOW_FEATURES,
121 null
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
130 // is fired.
131 win.addEventListener("unload", this.closeBrowserConsole.bind(this), {
132 once: true,
135 this.updateWindowTitle(win);
136 return win;
140 * Opens or focuses the Browser Console.
142 openBrowserConsoleOrFocus() {
143 const hud = this.getBrowserConsole();
144 if (hud) {
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
157 * open.
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) {
169 let title;
170 const mode = Services.prefs.getCharPref(
171 "devtools.browsertoolbox.scope",
172 null
174 if (mode == "everything") {
175 title = l10n.getStr("multiProcessBrowserConsole.title");
176 } else if (mode == "parent-process") {
177 title = l10n.getStr("parentProcessBrowserConsole.title");
178 } else {
179 throw new Error("Unsupported mode: " + mode);
182 win.document.title = title;
186 exports.BrowserConsoleManager = new BrowserConsoleManager();