1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {
CommandsFactory,
} = require("resource://devtools/shared/commands/commands-factory.js");
loader.lazyRequireGetter(
this,
"Tools",
"resource://devtools/client/definitions.js",
true
);
loader.lazyRequireGetter(
this,
"l10n",
"resource://devtools/client/webconsole/utils/l10n.js"
);
loader.lazyRequireGetter(
this,
"BrowserConsole",
"resource://devtools/client/webconsole/browser-console.js"
);
const BC_WINDOW_FEATURES =
"chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
class BrowserConsoleManager {
constructor() {
this._browserConsole = null;
this._browserConsoleInitializing = null;
this._browerConsoleSessionState = false;
}
storeBrowserConsoleSessionState() {
this._browerConsoleSessionState = !!this.getBrowserConsole();
}
getBrowserConsoleSessionState() {
return this._browerConsoleSessionState;
}
/**
* Open a Browser Console for the current commands context.
*
* @param nsIDOMWindow iframeWindow
* The window where the browser console UI is already loaded.
* @return object
* A promise object for the opening of the new BrowserConsole instance.
*/
async openBrowserConsole(win) {
const hud = new BrowserConsole(this.commands, win, win);
this._browserConsole = hud;
await hud.init();
return hud;
}
/**
* Close the opened Browser Console
*/
async closeBrowserConsole() {
if (!this._browserConsole) {
return;
}
// Ensure destroying the commands,
// even if the console throws during cleanup.
try {
await this._browserConsole.destroy();
} catch (e) {
console.error(e);
}
this._browserConsole = null;
await this.commands.destroy();
this.commands = null;
}
/**
* Toggle the Browser Console.
*/
async toggleBrowserConsole() {
if (this._browserConsole) {
return this.closeBrowserConsole();
}
if (this._browserConsoleInitializing) {
return this._browserConsoleInitializing;
}
// Temporarily cache the async startup sequence so that if toggleBrowserConsole
// gets called again we can return this console instead of opening another one.
this._browserConsoleInitializing = (async () => {
this.commands = await CommandsFactory.forBrowserConsole();
const win = await this.openWindow();
const browserConsole = await this.openBrowserConsole(win);
return browserConsole;
})();
try {
const browserConsole = await this._browserConsoleInitializing;
this._browserConsoleInitializing = null;
return browserConsole;
} catch (e) {
// Ensure always clearing this field, even in case of exception,
// which may happen when closing during initialization.
this._browserConsoleInitializing = null;
throw e;
}
}
async openWindow() {
const win = Services.ww.openWindow(
null,
Tools.webConsole.url,
"_blank",
BC_WINDOW_FEATURES,
null
);
await new Promise(resolve => {
win.addEventListener("DOMContentLoaded", resolve, { once: true });
});
// It's important to declare the unload *after* the initial "DOMContentLoaded",
// otherwise, since the window is navigated to Tools.webConsole.url, an unload event
// is fired.
win.addEventListener("unload", this.closeBrowserConsole.bind(this), {
once: true,
});
this.updateWindowTitle(win);
return win;
}
/**
* Opens or focuses the Browser Console.
*/
openBrowserConsoleOrFocus() {
const hud = this.getBrowserConsole();
if (hud) {
hud.iframeWindow.focus();
return Promise.resolve(hud);
}
return this.toggleBrowserConsole();
}
/**
* Get the Browser Console instance, if open.
*
* @return object|null
* A BrowserConsole instance or null if the Browser Console is not
* open.
*/
getBrowserConsole() {
return this._browserConsole;
}
/**
* Set the title of the Browser Console window.
*
* @param {Window} win: The BrowserConsole window
*/
updateWindowTitle(win) {
let title;
const mode = Services.prefs.getCharPref(
"devtools.browsertoolbox.scope",
null
);
if (mode == "everything") {
title = l10n.getStr("multiProcessBrowserConsole.title");
} else if (mode == "parent-process") {
title = l10n.getStr("parentProcessBrowserConsole.title");
} else {
throw new Error("Unsupported mode: " + mode);
}
win.document.title = title;
}
}
exports.BrowserConsoleManager = new BrowserConsoleManager();
|