2016-05-12 13:07:56 +03:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2019-03-05 17:58:05 +03:00
|
|
|
const { Cu, Cc, Ci } = require("chrome");
|
2018-02-07 12:37:36 +03:00
|
|
|
const promise = require("resource://gre/modules/Promise.jsm").Promise;
|
2018-04-12 14:44:12 +03:00
|
|
|
const jsmScope = require("resource://devtools/shared/Loader.jsm");
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = require("resource://gre/modules/Services.jsm");
|
2019-03-05 17:58:05 +03:00
|
|
|
|
|
|
|
const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
|
|
|
|
|
2016-05-12 13:07:56 +03:00
|
|
|
// Steal various globals only available in JSM scope (and not Sandbox one)
|
2018-03-01 22:56:07 +03:00
|
|
|
const {
|
2019-09-02 16:24:20 +03:00
|
|
|
BrowsingContext,
|
2018-03-01 22:56:07 +03:00
|
|
|
console,
|
2019-06-20 01:27:57 +03:00
|
|
|
DebuggerNotificationObserver,
|
2018-11-12 19:16:38 +03:00
|
|
|
DOMPoint,
|
|
|
|
DOMQuad,
|
2018-11-20 00:15:44 +03:00
|
|
|
DOMRect,
|
2018-03-01 22:56:07 +03:00
|
|
|
HeapSnapshot,
|
2019-02-01 17:03:44 +03:00
|
|
|
NamedNodeMap,
|
2019-01-16 23:55:23 +03:00
|
|
|
NodeFilter,
|
2018-04-12 14:44:12 +03:00
|
|
|
StructuredCloneHolder,
|
2018-10-26 23:17:55 +03:00
|
|
|
TelemetryStopwatch,
|
2018-03-01 22:56:07 +03:00
|
|
|
} = Cu.getGlobalForObject(jsmScope);
|
2017-08-01 15:52:23 +03:00
|
|
|
|
|
|
|
// Create a single Sandbox to access global properties needed in this module.
|
|
|
|
// Sandbox are memory expensive, so we should create as little as possible.
|
2019-03-05 17:58:05 +03:00
|
|
|
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.
|
2019-09-24 19:48:43 +03:00
|
|
|
freshCompartment: true,
|
2019-03-05 17:58:05 +03:00
|
|
|
|
2018-03-01 22:56:07 +03:00
|
|
|
wantGlobalProperties: [
|
2018-03-01 22:46:43 +03:00
|
|
|
"atob",
|
|
|
|
"btoa",
|
2018-12-20 05:11:56 +03:00
|
|
|
"Blob",
|
2018-03-01 22:46:43 +03:00
|
|
|
"ChromeUtils",
|
2018-03-01 22:56:07 +03:00
|
|
|
"CSS",
|
|
|
|
"CSSRule",
|
2018-04-21 06:04:40 +03:00
|
|
|
"DOMParser",
|
2018-04-27 06:37:34 +03:00
|
|
|
"Element",
|
2018-04-20 19:55:31 +03:00
|
|
|
"Event",
|
2018-03-01 22:56:07 +03:00
|
|
|
"FileReader",
|
2018-03-03 01:48:57 +03:00
|
|
|
"FormData",
|
2018-03-01 22:56:07 +03:00
|
|
|
"indexedDB",
|
2018-05-30 05:58:50 +03:00
|
|
|
"InspectorUtils",
|
|
|
|
"Node",
|
2018-03-01 22:46:43 +03:00
|
|
|
"TextDecoder",
|
|
|
|
"TextEncoder",
|
2018-03-01 22:56:07 +03:00
|
|
|
"URL",
|
2018-03-01 22:46:43 +03:00
|
|
|
"XMLHttpRequest",
|
2018-03-01 22:56:07 +03:00
|
|
|
],
|
|
|
|
});
|
2016-05-12 13:07:56 +03:00
|
|
|
|
2019-03-05 17:58:05 +03:00
|
|
|
const {
|
|
|
|
atob,
|
|
|
|
btoa,
|
|
|
|
Blob,
|
|
|
|
ChromeUtils,
|
|
|
|
CSS,
|
|
|
|
CSSRule,
|
|
|
|
DOMParser,
|
|
|
|
Element,
|
|
|
|
Event,
|
|
|
|
FileReader,
|
|
|
|
FormData,
|
|
|
|
indexedDB,
|
|
|
|
InspectorUtils,
|
|
|
|
Node,
|
|
|
|
TextDecoder,
|
|
|
|
TextEncoder,
|
|
|
|
URL,
|
|
|
|
XMLHttpRequest,
|
|
|
|
} = debuggerSandbox;
|
|
|
|
|
2016-05-12 13:07:56 +03:00
|
|
|
/**
|
|
|
|
* Defines a getter on a specified object that will be created upon first use.
|
|
|
|
*
|
2016-12-02 20:32:04 +03:00
|
|
|
* @param object
|
2016-05-12 13:07:56 +03:00
|
|
|
* The object to define the lazy getter on.
|
2016-12-02 20:32:04 +03:00
|
|
|
* @param name
|
|
|
|
* The name of the getter to define on object.
|
|
|
|
* @param lambda
|
2016-05-12 13:07:56 +03:00
|
|
|
* A function that returns what the getter should return. This will
|
|
|
|
* only ever be called once.
|
|
|
|
*/
|
2016-12-02 20:32:04 +03:00
|
|
|
function defineLazyGetter(object, name, lambda) {
|
|
|
|
Object.defineProperty(object, name, {
|
2018-03-12 21:24:38 +03:00
|
|
|
get: function() {
|
2016-05-12 13:07:56 +03:00
|
|
|
// Redefine this accessor property as a data property.
|
2016-12-02 20:32:04 +03:00
|
|
|
// Delete it first, to rule out "too much recursion" in case object is
|
2016-05-12 13:07:56 +03:00
|
|
|
// a proxy whose defineProperty handler might unwittingly trigger this
|
|
|
|
// getter again.
|
2016-12-02 20:32:04 +03:00
|
|
|
delete object[name];
|
2018-06-01 13:36:09 +03:00
|
|
|
const value = lambda.apply(object);
|
2016-12-02 20:32:04 +03:00
|
|
|
Object.defineProperty(object, name, {
|
2016-05-12 13:07:56 +03:00
|
|
|
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.
|
|
|
|
*
|
2016-12-02 20:32:04 +03:00
|
|
|
* @param object
|
2016-05-12 13:07:56 +03:00
|
|
|
* The object to define the lazy getter on.
|
2016-12-02 20:32:04 +03:00
|
|
|
* @param name
|
|
|
|
* The name of the getter to define on object for the service.
|
|
|
|
* @param contract
|
2016-05-12 13:07:56 +03:00
|
|
|
* The contract used to obtain the service.
|
2016-12-02 20:32:04 +03:00
|
|
|
* @param interfaceName
|
2016-05-12 13:07:56 +03:00
|
|
|
* The name of the interface to query the service to.
|
|
|
|
*/
|
2016-12-02 20:32:04 +03:00
|
|
|
function defineLazyServiceGetter(object, name, contract, interfaceName) {
|
2018-03-12 21:24:38 +03:00
|
|
|
defineLazyGetter(object, name, function() {
|
2016-12-02 20:32:04 +03:00
|
|
|
return Cc[contract].getService(Ci[interfaceName]);
|
2016-05-12 13:07:56 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a getter on a specified object for a module. The module will not
|
2020-03-30 11:24:40 +03:00
|
|
|
* be imported until first use.
|
2016-05-12 13:07:56 +03:00
|
|
|
*
|
2016-12-02 20:32:04 +03:00
|
|
|
* @param object
|
2016-05-12 13:07:56 +03:00
|
|
|
* The object to define the lazy getter on.
|
2016-12-02 20:32:04 +03:00
|
|
|
* @param name
|
|
|
|
* The name of the getter to define on object for the module.
|
|
|
|
* @param resource
|
2016-05-12 13:07:56 +03:00
|
|
|
* The URL used to obtain the module.
|
|
|
|
*/
|
2020-03-30 11:24:40 +03:00
|
|
|
function defineLazyModuleGetter(object, name, resource) {
|
2018-03-12 21:24:38 +03:00
|
|
|
defineLazyGetter(object, name, function() {
|
2016-05-12 13:07:56 +03:00
|
|
|
try {
|
2020-03-30 11:24:40 +03:00
|
|
|
return ChromeUtils.import(resource)[name];
|
2016-05-12 13:07:56 +03:00
|
|
|
} catch (ex) {
|
2016-12-02 20:32:04 +03:00
|
|
|
Cu.reportError("Failed to load module " + resource + ".");
|
2016-05-12 13:07:56 +03:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define a getter property on the given object that requires the given
|
|
|
|
* module. This enables delaying importing modules until the module is
|
|
|
|
* actually used.
|
|
|
|
*
|
2020-08-03 08:43:59 +03:00
|
|
|
* Several getters can be defined at once by providing an array of
|
|
|
|
* properties and enabling destructuring.
|
|
|
|
*
|
|
|
|
* @param { Object } obj
|
2016-05-12 13:07:56 +03:00
|
|
|
* The object to define the property on.
|
2020-08-03 08:43:59 +03:00
|
|
|
* @param { String | Array<String> } properties
|
|
|
|
* String: Name of the property for the getter.
|
|
|
|
* Array<String>: When destructure is true, properties can be an array of
|
|
|
|
* strings to create several getters at once.
|
|
|
|
* @param { String } module
|
2016-05-12 13:07:56 +03:00
|
|
|
* The module path.
|
2020-08-03 08:43:59 +03:00
|
|
|
* @param { Boolean } destructure
|
2016-05-12 13:07:56 +03:00
|
|
|
* Pass true if the property name is a member of the module's exports.
|
|
|
|
*/
|
2020-08-03 08:43:59 +03:00
|
|
|
function lazyRequireGetter(obj, properties, module, destructure) {
|
|
|
|
if (Array.isArray(properties) && !destructure) {
|
|
|
|
throw new Error(
|
|
|
|
"Pass destructure=true to call lazyRequireGetter with an array of properties"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(properties)) {
|
|
|
|
properties = [properties];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const property of properties) {
|
|
|
|
defineLazyGetter(obj, property, () => {
|
|
|
|
return destructure
|
|
|
|
? require(module)[property]
|
|
|
|
: require(module || property);
|
|
|
|
});
|
|
|
|
}
|
2016-05-12 13:07:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// List of pseudo modules exposed to all devtools modules.
|
|
|
|
exports.modules = {
|
2018-03-01 22:56:07 +03:00
|
|
|
ChromeUtils,
|
2019-06-20 01:27:57 +03:00
|
|
|
DebuggerNotificationObserver,
|
2018-03-01 22:56:07 +03:00
|
|
|
HeapSnapshot,
|
2020-03-30 11:24:28 +03:00
|
|
|
InspectorUtils,
|
2016-05-12 13:07:56 +03:00
|
|
|
promise,
|
2017-09-24 23:45:16 +03:00
|
|
|
// 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,
|
2018-03-01 22:56:07 +03:00
|
|
|
Services: Object.create(Services),
|
2018-10-26 23:17:55 +03:00
|
|
|
TelemetryStopwatch,
|
2016-05-12 13:07:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
defineLazyGetter(exports.modules, "Debugger", () => {
|
2018-08-13 15:47:40 +03:00
|
|
|
const global = Cu.getGlobalForObject(this);
|
2020-02-11 00:03:28 +03:00
|
|
|
// Debugger may already have been added.
|
2018-08-13 15:47:40 +03:00
|
|
|
if (global.Debugger) {
|
|
|
|
return global.Debugger;
|
|
|
|
}
|
2019-01-17 21:18:31 +03:00
|
|
|
const { addDebuggerToGlobal } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/jsdebugger.jsm"
|
|
|
|
);
|
2018-08-13 15:47:40 +03:00
|
|
|
addDebuggerToGlobal(global);
|
|
|
|
return global.Debugger;
|
2016-05-12 13:07:56 +03:00
|
|
|
});
|
|
|
|
|
2019-03-05 17:58:05 +03:00
|
|
|
defineLazyGetter(exports.modules, "ChromeDebugger", () => {
|
|
|
|
const { addDebuggerToGlobal } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/jsdebugger.jsm"
|
|
|
|
);
|
|
|
|
addDebuggerToGlobal(debuggerSandbox);
|
|
|
|
return debuggerSandbox.Debugger;
|
|
|
|
});
|
|
|
|
|
2016-05-12 13:07:56 +03:00
|
|
|
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.
|
2016-11-28 18:44:15 +03:00
|
|
|
exports.globals = {
|
2018-03-01 22:56:07 +03:00
|
|
|
atob,
|
2018-12-20 05:11:56 +03:00
|
|
|
Blob,
|
2018-03-01 22:56:07 +03:00
|
|
|
btoa,
|
2019-09-02 16:24:20 +03:00
|
|
|
BrowsingContext,
|
2018-03-01 22:56:07 +03:00
|
|
|
console,
|
2017-08-01 15:52:23 +03:00
|
|
|
CSS,
|
2020-03-30 11:24:28 +03:00
|
|
|
CSSRule,
|
2018-04-21 06:04:40 +03:00
|
|
|
DOMParser,
|
2018-11-12 19:16:38 +03:00
|
|
|
DOMPoint,
|
|
|
|
DOMQuad,
|
2020-03-30 11:24:28 +03:00
|
|
|
Event,
|
2019-02-01 17:03:44 +03:00
|
|
|
NamedNodeMap,
|
2019-01-16 23:55:23 +03:00
|
|
|
NodeFilter,
|
2018-11-20 00:15:44 +03:00
|
|
|
DOMRect,
|
2018-04-27 06:37:34 +03:00
|
|
|
Element,
|
2018-12-20 05:11:56 +03:00
|
|
|
FileReader,
|
2018-03-03 01:48:57 +03:00
|
|
|
FormData,
|
2018-03-01 22:56:07 +03:00
|
|
|
isWorker: false,
|
|
|
|
loader: {
|
|
|
|
lazyGetter: defineLazyGetter,
|
|
|
|
lazyImporter: defineLazyModuleGetter,
|
|
|
|
lazyServiceGetter: defineLazyServiceGetter,
|
|
|
|
lazyRequireGetter: lazyRequireGetter,
|
|
|
|
// Defined by Loader.jsm
|
|
|
|
id: null,
|
|
|
|
},
|
2018-05-30 05:58:50 +03:00
|
|
|
Node,
|
2018-03-01 22:56:07 +03:00
|
|
|
reportError: Cu.reportError,
|
2018-04-12 14:44:12 +03:00
|
|
|
StructuredCloneHolder,
|
2018-03-01 22:56:07 +03:00
|
|
|
TextDecoder,
|
|
|
|
TextEncoder,
|
|
|
|
URL,
|
|
|
|
XMLHttpRequest,
|
2016-05-12 13:07:56 +03:00
|
|
|
};
|
2018-03-09 18:36:45 +03:00
|
|
|
// 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
|
2016-11-28 18:44:15 +03:00
|
|
|
// 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.
|
2018-06-01 13:36:09 +03:00
|
|
|
const globals = {};
|
2016-11-28 18:44:15 +03:00
|
|
|
function lazyGlobal(name, getter) {
|
|
|
|
defineLazyGetter(globals, name, getter);
|
|
|
|
Object.defineProperty(exports.globals, name, {
|
2018-03-12 21:24:38 +03:00
|
|
|
get: function() {
|
2016-11-28 18:44:15 +03:00
|
|
|
return globals[name];
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
});
|
|
|
|
}
|
2016-05-12 13:07:56 +03:00
|
|
|
|
|
|
|
// Lazily define a few things so that the corresponding jsms are only loaded
|
|
|
|
// when used.
|
2016-11-28 18:44:15 +03:00
|
|
|
lazyGlobal("clearTimeout", () => {
|
2018-02-07 12:37:36 +03:00
|
|
|
return require("resource://gre/modules/Timer.jsm").clearTimeout;
|
2016-05-12 13:07:56 +03:00
|
|
|
});
|
2016-11-28 18:44:15 +03:00
|
|
|
lazyGlobal("setTimeout", () => {
|
2018-02-07 12:37:36 +03:00
|
|
|
return require("resource://gre/modules/Timer.jsm").setTimeout;
|
2016-05-12 13:07:56 +03:00
|
|
|
});
|
2016-11-28 18:44:15 +03:00
|
|
|
lazyGlobal("clearInterval", () => {
|
2018-02-07 12:37:36 +03:00
|
|
|
return require("resource://gre/modules/Timer.jsm").clearInterval;
|
2016-05-12 13:07:56 +03:00
|
|
|
});
|
2016-11-28 18:44:15 +03:00
|
|
|
lazyGlobal("setInterval", () => {
|
2018-02-07 12:37:36 +03:00
|
|
|
return require("resource://gre/modules/Timer.jsm").setInterval;
|
2016-05-12 13:07:56 +03:00
|
|
|
});
|
2016-11-28 18:44:15 +03:00
|
|
|
lazyGlobal("WebSocket", () => {
|
2016-08-05 15:00:00 +03:00
|
|
|
return Services.appShell.hiddenDOMWindow.WebSocket;
|
|
|
|
});
|
2016-11-28 18:44:15 +03:00
|
|
|
lazyGlobal("indexedDB", () => {
|
2017-07-31 22:55:33 +03:00
|
|
|
return require("devtools/shared/indexed-db").createDevToolsIndexedDB(
|
|
|
|
indexedDB
|
|
|
|
);
|
2016-11-28 18:44:15 +03:00
|
|
|
});
|