forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
317 lines (280 loc) · 11.3 KB
/
bootstrap.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* 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/. */
/* global content */
/* exported startup, shutdown, install, uninstall */
"use strict";
const Cu = Components.utils;
const Ci = Components.interfaces;
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
const {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", {});
let prefs = {
// Enable dump as some errors are only printed on the stdout
"browser.dom.window.dump.enabled": true,
// Enable the browser toolbox and various chrome-only features
"devtools.chrome.enabled": true,
"devtools.debugger.remote-enabled": true,
// Disable the prompt to ease usage of the browser toolbox
"devtools.debugger.prompt-connection": false,
};
// Values of debug pref before overriding them
let originalPrefValues = {};
// MultiWindowKeyListener instance for Ctrl+Alt+R key
let listener;
// nsIURI to the addon root folder
let resourceURI;
function actionOccurred(id) {
let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
let Telemetry = require("devtools/client/shared/telemetry");
let telemetry = new Telemetry();
telemetry.actionOccurred(id);
}
// Synchronously fetch the content of a given URL
function readURI(uri) {
let stream = NetUtil.newChannel({
uri: NetUtil.newURI(uri, "UTF-8"),
loadUsingSystemPrincipal: true}
).open2();
let count = stream.available();
let data = NetUtil.readInputStreamToString(stream, count, {
charset: "UTF-8"
});
stream.close();
return data;
}
// Read a preference file and set all of its defined pref as default values
// (This replicate the behavior of preferences files from mozilla-central)
function processPrefFile(url) {
let content = readURI(url);
content.match(/pref\("[^"]+",\s*.+\s*\)/g).forEach(item => {
let m = item.match(/pref\("([^"]+)",\s*(.+)\s*\)/);
let name = m[1];
let val = m[2];
// Prevent overriding prefs that have been changed by the user
if (Services.prefs.prefHasUserValue(name)) {
return;
}
let defaultBranch = Services.prefs.getDefaultBranch("");
if ((val.startsWith("\"") && val.endsWith("\"")) ||
(val.startsWith("'") && val.endsWith("'"))) {
defaultBranch.setCharPref(name, val.substr(1, val.length - 2));
} else if (val.match(/[0-9]+/)) {
defaultBranch.setIntPref(name, parseInt(val, 10));
} else if (val == "true" || val == "false") {
defaultBranch.setBoolPref(name, val == "true");
} else {
console.log("Unable to match preference type for value:", val);
}
});
}
function setPrefs() {
processPrefFile(resourceURI.spec + "./client/preferences/devtools.js");
processPrefFile(resourceURI.spec + "./client/preferences/debugger.js");
processPrefFile(resourceURI.spec + "./client/webide/webide-prefs.js");
}
// Helper to listen to a key on all windows
function MultiWindowKeyListener({ keyCode, ctrlKey, altKey, callback }) {
let keyListener = function (event) {
if (event.ctrlKey == !!ctrlKey &&
event.altKey == !!altKey &&
event.keyCode === keyCode) {
callback(event);
// Call preventDefault to avoid duplicated events when
// doing the key stroke within a tab.
event.preventDefault();
}
};
let observer = function (window, topic, data) {
// Listen on keyup to call keyListener only once per stroke
if (topic === "domwindowopened") {
window.addEventListener("keyup", keyListener);
} else {
window.removeEventListener("keyup", keyListener);
}
};
return {
start: function () {
// Automatically process already opened windows
let e = Services.ww.getWindowEnumerator();
while (e.hasMoreElements()) {
let window = e.getNext();
observer(window, "domwindowopened", null);
}
// And listen for new ones to come
Services.ww.registerNotification(observer);
},
stop: function () {
Services.ww.unregisterNotification(observer);
let e = Services.ww.getWindowEnumerator();
while (e.hasMoreElements()) {
let window = e.getNext();
observer(window, "domwindowclosed", null);
}
}
};
}
let getTopLevelWindow = function (window) {
return window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
};
function reload(event) {
// We automatically reload the toolbox if we are on a browser tab
// with a toolbox already opened
let reloadToolbox = false;
if (event) {
let top = getTopLevelWindow(event.view);
let isBrowser = top.location.href.includes("/browser.xul");
if (isBrowser && top.gBrowser) {
// We do not use any devtools code before the call to Loader.jsm reload as
// any attempt to use Loader.jsm to load a module will instanciate a new
// Loader.
let nbox = top.gBrowser.getNotificationBox();
reloadToolbox =
top.document.getAnonymousElementByAttribute(nbox, "class",
"devtools-toolbox-bottom-iframe") ||
top.document.getAnonymousElementByAttribute(nbox, "class",
"devtools-toolbox-side-iframe") ||
Services.wm.getMostRecentWindow("devtools:toolbox");
}
}
let browserConsole = Services.wm.getMostRecentWindow("devtools:webconsole");
let reopenBrowserConsole = false;
if (browserConsole) {
browserConsole.close();
reopenBrowserConsole = true;
}
dump("Reload DevTools. (reload-toolbox:" + reloadToolbox + ")\n");
// Invalidate xul cache in order to see changes made to chrome:// files
Services.obs.notifyObservers(null, "startupcache-invalidate");
// This frame script is going to be executed in all processes:
// parent and child
Services.ppmm.loadProcessScript("data:,new " + function () {
/* Flush message manager cached frame scripts as well as chrome locales */
let obs = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
obs.notifyObservers(null, "message-manager-flush-caches");
/* Also purge cached modules in child processes, we do it a few lines after
in the parent process */
if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) {
Services.obs.notifyObservers(null, "devtools-unload", "reload");
}
}, false);
// As we can't get a reference to existing Loader.jsm instances, we send them
// an observer service notification to unload them.
Services.obs.notifyObservers(null, "devtools-unload", "reload");
// Then spawn a brand new Loader.jsm instance and start the main module
Cu.unload("resource://devtools/shared/Loader.jsm");
// Also unload all resources loaded as jsm, hopefully all of them are going
// to be converted into regular modules
Cu.unload("resource://devtools/client/shared/browser-loader.js");
Cu.unload("resource://devtools/client/framework/ToolboxProcess.jsm");
Cu.unload("resource://devtools/shared/apps/Devices.jsm");
Cu.unload("resource://devtools/client/scratchpad/scratchpad-manager.jsm");
Cu.unload("resource://devtools/shared/Parser.jsm");
Cu.unload("resource://devtools/client/shared/DOMHelpers.jsm");
Cu.unload("resource://devtools/client/shared/widgets/VariablesView.jsm");
Cu.unload("resource://devtools/client/responsivedesign/responsivedesign.jsm");
Cu.unload("resource://devtools/client/shared/widgets/AbstractTreeItem.jsm");
Cu.unload("resource://devtools/shared/deprecated-sync-thenables.js");
// Update the preferences before starting new code
setPrefs();
const {devtools} = Cu.import("resource://devtools/shared/Loader.jsm", {});
devtools.require("devtools/client/framework/devtools-browser");
// Go over all top level windows to reload all devtools related things
let windowsEnum = Services.wm.getEnumerator(null);
while (windowsEnum.hasMoreElements()) {
let window = windowsEnum.getNext();
let windowtype = window.document.documentElement.getAttribute("windowtype");
if (windowtype == "navigator:browser" && window.gBrowser) {
// Enumerate tabs on firefox windows
for (let tab of window.gBrowser.tabs) {
let browser = tab.linkedBrowser;
let location = browser.documentURI.spec;
let mm = browser.messageManager;
// To reload JSON-View tabs and any devtools document
if (location.startsWith("about:debugging") ||
location.startsWith("chrome://devtools/")) {
browser.reload();
}
// We have to use a frame script to query "baseURI"
mm.loadFrameScript("data:text/javascript,new " + function () {
let isJSONView =
content.document.baseURI.startsWith("resource://devtools/");
if (isJSONView) {
content.location.reload();
}
}, false);
}
} else if (windowtype === "devtools:webide") {
window.location.reload();
}
}
if (reloadToolbox) {
// Reopen the toolbox automatically if we are reloading from toolbox
// shortcut and are on a browser window.
// Wait for a second before opening the toolbox to avoid races
// between the old and the new one.
let {setTimeout} = Cu.import("resource://gre/modules/Timer.jsm", {});
setTimeout(() => {
let { TargetFactory } = devtools.require("devtools/client/framework/target");
let { gDevTools } = devtools.require("devtools/client/framework/devtools");
let top = getTopLevelWindow(event.view);
let target = TargetFactory.forTab(top.gBrowser.selectedTab);
gDevTools.showToolbox(target);
}, 1000);
}
// Browser console document can't just be reloaded.
// HUDService is going to close it on unload.
// Instead we have to manually toggle it.
if (reopenBrowserConsole) {
let HUDService = devtools.require("devtools/client/webconsole/hudservice");
HUDService.toggleBrowserConsole();
}
actionOccurred("reloadAddonReload");
}
function startup(data) {
dump("DevTools addon started.\n");
resourceURI = data.resourceURI;
listener = new MultiWindowKeyListener({
keyCode: Ci.nsIDOMKeyEvent.DOM_VK_R, ctrlKey: true, altKey: true,
callback: reload
});
listener.start();
// Toggle development prefs and save original values
originalPrefValues = {};
for (let name in prefs) {
let value = prefs[name];
let userValue = Services.prefs.getBoolPref(name);
// Only toggle if the pref isn't already set to the right value
if (userValue != value) {
Services.prefs.setBoolPref(name, value);
originalPrefValues[name] = userValue;
}
}
reload();
}
function shutdown() {
listener.stop();
listener = null;
// Restore preferences that used to be before the addon was installed
for (let name in originalPrefValues) {
let userValue = Services.prefs.getBoolPref(name);
// Only reset the pref if it hasn't changed
if (userValue == prefs[name]) {
Services.prefs.setBoolPref(name, originalPrefValues[name]);
}
}
}
function install() {
try {
actionOccurred("reloadAddonInstalled");
} catch (e) {
// When installing on Firefox builds without devtools, telemetry doesn't
// work yet and throws.
}
}
function uninstall() {}