This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathbuiltin-modules.js
More file actions
383 lines (359 loc) · 11.6 KB
/
Copy pathbuiltin-modules.js
File metadata and controls
383 lines (359 loc) · 11.6 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* 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";
/**
* This module defines custom globals injected in all our modules and also
* pseudo modules that aren't separate files but just dynamically set values.
*
* As it does so, the module itself doesn't have access to these globals,
* nor the pseudo modules. Be careful to avoid loading any other js module as
* they would also miss them.
*/
const { Cu, Cc, Ci } = require("chrome");
const promise = require("resource://gre/modules/Promise.jsm").Promise;
const jsmScope = require("resource://devtools/shared/Loader.jsm");
const { Services } = require("resource://gre/modules/Services.jsm");
const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
// Steal various globals only available in JSM scope (and not Sandbox one)
const {
console,
DOMPoint,
DOMQuad,
DOMRect,
HeapSnapshot,
NamedNodeMap,
NodeFilter,
StructuredCloneHolder,
TelemetryStopwatch,
} = Cu.getGlobalForObject(jsmScope);
// Create a single Sandbox to access global properties needed in this module.
// Sandbox are memory expensive, so we should create as little as possible.
const debuggerSandbox = Cu.Sandbox(systemPrincipal, {
// This sandbox is also reused for ChromeDebugger implementation.
// As we want to load the `Debugger` API for debugging chrome contexts,
// we have to ensure loading it in a distinct compartment from its debuggee.
// invisibleToDebugger does that and helps the Debugger API identify the boundaries
// between debuggee and debugger code.
invisibleToDebugger: true,
wantGlobalProperties: [
"atob",
"btoa",
"Blob",
"ChromeUtils",
"CSS",
"CSSRule",
"DOMParser",
"Element",
"Event",
"FileReader",
"FormData",
"indexedDB",
"InspectorUtils",
"Node",
"TextDecoder",
"TextEncoder",
"URL",
"XMLHttpRequest",
],
});
const {
atob,
btoa,
Blob,
ChromeUtils,
CSS,
CSSRule,
DOMParser,
Element,
Event,
FileReader,
FormData,
indexedDB,
InspectorUtils,
Node,
TextDecoder,
TextEncoder,
URL,
XMLHttpRequest,
} = debuggerSandbox;
/**
* Defines a getter on a specified object that will be created upon first use.
*
* @param object
* The object to define the lazy getter on.
* @param name
* The name of the getter to define on object.
* @param lambda
* A function that returns what the getter should return. This will
* only ever be called once.
*/
function defineLazyGetter(object, name, lambda) {
Object.defineProperty(object, name, {
get: function() {
// Redefine this accessor property as a data property.
// Delete it first, to rule out "too much recursion" in case object is
// a proxy whose defineProperty handler might unwittingly trigger this
// getter again.
delete object[name];
const value = lambda.apply(object);
Object.defineProperty(object, name, {
value,
writable: true,
configurable: true,
enumerable: true,
});
return value;
},
configurable: true,
enumerable: true,
});
}
/**
* Defines a getter on a specified object for a service. The service will not
* be obtained until first use.
*
* @param object
* The object to define the lazy getter on.
* @param name
* The name of the getter to define on object for the service.
* @param contract
* The contract used to obtain the service.
* @param interfaceName
* The name of the interface to query the service to.
*/
function defineLazyServiceGetter(object, name, contract, interfaceName) {
defineLazyGetter(object, name, function() {
return Cc[contract].getService(Ci[interfaceName]);
});
}
/**
* Defines a getter on a specified object for a module. The module will not
* be imported until first use. The getter allows to execute setup and
* teardown code (e.g. to register/unregister to services) and accepts
* a proxy object which acts on behalf of the module until it is imported.
*
* @param object
* The object to define the lazy getter on.
* @param name
* The name of the getter to define on object for the module.
* @param resource
* The URL used to obtain the module.
* @param symbol
* The name of the symbol exported by the module.
* This parameter is optional and defaults to name.
* @param preLambda
* A function that is executed when the proxy is set up.
* This will only ever be called once.
* @param postLambda
* A function that is executed when the module has been imported to
* run optional teardown procedures on the proxy object.
* This will only ever be called once.
* @param proxy
* An object which acts on behalf of the module to be imported until
* the module has been imported.
*/
function defineLazyModuleGetter(object, name, resource, symbol,
preLambda, postLambda, proxy) {
proxy = proxy || {};
if (typeof (preLambda) === "function") {
preLambda.apply(proxy);
}
defineLazyGetter(object, name, function() {
const temp = {};
try {
ChromeUtils.import(resource, temp);
if (typeof (postLambda) === "function") {
postLambda.apply(proxy);
}
} catch (ex) {
Cu.reportError("Failed to load module " + resource + ".");
throw ex;
}
return temp[symbol || name];
});
}
/**
* Define a getter property on the given object that requires the given
* module. This enables delaying importing modules until the module is
* actually used.
*
* @param Object obj
* The object to define the property on.
* @param String property
* The property name.
* @param String module
* The module path.
* @param Boolean destructure
* Pass true if the property name is a member of the module's exports.
*/
function lazyRequireGetter(obj, property, module, destructure) {
Object.defineProperty(obj, property, {
get: () => {
// Redefine this accessor property as a data property.
// Delete it first, to rule out "too much recursion" in case obj is
// a proxy whose defineProperty handler might unwittingly trigger this
// getter again.
delete obj[property];
const value = destructure
? require(module)[property]
: require(module || property);
Object.defineProperty(obj, property, {
value,
writable: true,
configurable: true,
enumerable: true,
});
return value;
},
configurable: true,
enumerable: true,
});
}
// List of pseudo modules exposed to all devtools modules.
exports.modules = {
ChromeUtils,
HeapSnapshot,
promise,
// Expose "chrome" Promise, which aren't related to any document
// and so are never frozen, even if the browser loader module which
// pull it is destroyed. See bug 1402779.
Promise,
Services: Object.create(Services),
TelemetryStopwatch,
};
defineLazyGetter(exports.modules, "Debugger", () => {
const global = Cu.getGlobalForObject(this);
// Debugger may already have been added by RecordReplayControl getter
if (global.Debugger) {
return global.Debugger;
}
const { addDebuggerToGlobal } = ChromeUtils.import("resource://gre/modules/jsdebugger.jsm");
addDebuggerToGlobal(global);
return global.Debugger;
});
defineLazyGetter(exports.modules, "ChromeDebugger", () => {
const { addDebuggerToGlobal } = ChromeUtils.import("resource://gre/modules/jsdebugger.jsm");
addDebuggerToGlobal(debuggerSandbox);
return debuggerSandbox.Debugger;
});
defineLazyGetter(exports.modules, "RecordReplayControl", () => {
// addDebuggerToGlobal also adds the RecordReplayControl object.
const global = Cu.getGlobalForObject(this);
// RecordReplayControl may already have been added by Debugger getter
if (global.RecordReplayControl) {
return global.RecordReplayControl;
}
const { addDebuggerToGlobal } = ChromeUtils.import("resource://gre/modules/jsdebugger.jsm");
addDebuggerToGlobal(global);
return global.RecordReplayControl;
});
defineLazyGetter(exports.modules, "InspectorUtils", () => {
if (exports.modules.Debugger.recordReplayProcessKind() == "Middleman") {
const ReplayInspector = require("devtools/server/actors/replay/inspector");
return ReplayInspector.createInspectorUtils(InspectorUtils);
}
return InspectorUtils;
});
defineLazyGetter(exports.modules, "Timer", () => {
const {setTimeout, clearTimeout} = require("resource://gre/modules/Timer.jsm");
// Do not return Cu.import result, as DevTools loader would freeze Timer.jsm globals...
return {
setTimeout,
clearTimeout,
};
});
defineLazyGetter(exports.modules, "xpcInspector", () => {
return Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector);
});
// List of all custom globals exposed to devtools modules.
// Changes here should be mirrored to devtools/.eslintrc.
exports.globals = {
atob,
Blob,
btoa,
console,
CSS,
CSSRule,
// Make sure `define` function exists. This allows defining some modules
// in AMD format while retaining CommonJS compatibility through this hook.
// JSON Viewer needs modules in AMD format, as it currently uses RequireJS
// from a content document and can't access our usual loaders. So, any
// modules shared with the JSON Viewer should include a define wrapper:
//
// // Make this available to both AMD and CJS environments
// define(function(require, exports, module) {
// ... code ...
// });
//
// Bug 1248830 will work out a better plan here for our content module
// loading needs, especially as we head towards devtools.html.
define(factory) {
factory(this.require, this.exports, this.module);
},
DOMParser,
DOMPoint,
DOMQuad,
NamedNodeMap,
NodeFilter,
DOMRect,
Element,
Event,
FileReader,
FormData,
isWorker: false,
loader: {
lazyGetter: defineLazyGetter,
lazyImporter: defineLazyModuleGetter,
lazyServiceGetter: defineLazyServiceGetter,
lazyRequireGetter: lazyRequireGetter,
// Defined by Loader.jsm
id: null,
},
Node,
reportError: Cu.reportError,
StructuredCloneHolder,
TextDecoder,
TextEncoder,
URL,
XMLHttpRequest,
};
// DevTools loader copy globals property descriptors on each module global
// object so that we have to memoize them from here in order to instantiate each
// global only once.
// `globals` is a cache object on which we put all global values
// and we set getters on `exports.globals` returning `globals` values.
const globals = {};
function lazyGlobal(name, getter) {
defineLazyGetter(globals, name, getter);
Object.defineProperty(exports.globals, name, {
get: function() {
return globals[name];
},
configurable: true,
enumerable: true,
});
}
// Lazily define a few things so that the corresponding jsms are only loaded
// when used.
lazyGlobal("clearTimeout", () => {
return require("resource://gre/modules/Timer.jsm").clearTimeout;
});
lazyGlobal("setTimeout", () => {
return require("resource://gre/modules/Timer.jsm").setTimeout;
});
lazyGlobal("clearInterval", () => {
return require("resource://gre/modules/Timer.jsm").clearInterval;
});
lazyGlobal("setInterval", () => {
return require("resource://gre/modules/Timer.jsm").setInterval;
});
lazyGlobal("WebSocket", () => {
return Services.appShell.hiddenDOMWindow.WebSocket;
});
lazyGlobal("indexedDB", () => {
return require("devtools/shared/indexed-db").createDevToolsIndexedDB(indexedDB);
});
lazyGlobal("isReplaying", () => {
return exports.modules.Debugger.recordReplayProcessKind() == "Middleman";
});