Bug 1789980 - [devtools] Stop exposing indexedDB to all DevTools modules. r=jdescottes

This symbol is specific to documents and isn't available in JSM/ESM.
So it will be best to keep it manually crafter from the modules.
It appears that only async-storage depends on the overloaded indexedDB
object created by devtools/shared/indexed-db.

Differential Revision: https://phabricator.services.mozilla.com/D157424
This commit is contained in:
Alexandre Poirot
2022-09-20 12:52:27 +00:00
parent 0c4e317914
commit 7141d6df9f
4 changed files with 26 additions and 24 deletions

View File

@@ -51,6 +51,8 @@ const DBVERSION = 1;
const STORENAME = "keyvaluepairs";
var db = null;
loader.lazyRequireGetter(this, "indexedDB", "devtools/shared/indexed-db");
function withStore(type, onsuccess, onerror) {
if (db) {
const transaction = db.transaction(STORENAME, type);

View File

@@ -9,24 +9,28 @@
* a principal dedicated to DevTools.
*/
const PSEUDOURI = "indexeddb://fx-devtools";
const principaluri = Services.io.newURI(PSEUDOURI);
const principal = Services.scriptSecurityManager.createContentPrincipal(
principaluri,
{}
);
// When running in jest, we can't use Cu.Sandbox and only expose the native indexedDB object
if (globalThis.indexedDB) {
module.exports = globalThis.indexedDB;
} else {
const PSEUDOURI = "indexeddb://fx-devtools";
const principaluri = Services.io.newURI(PSEUDOURI);
const principal = Services.scriptSecurityManager.createContentPrincipal(
principaluri,
{}
);
/**
* Create the DevTools dedicated DB, by relying on the real indexedDB object passed as a
* parameter here.
*
* @param {IDBFactory} indexedDB
* Real indexedDB object.
* @return {Object} Wrapper object that implements IDBFactory methods, but for a devtools
* specific principal.
*/
exports.createDevToolsIndexedDB = function(indexedDB) {
return Object.freeze({
// indexedDB is only exposed to document globals.
// We are retrieving an instance from a Sandbox, which has to be loaded
// from the system principal in order to avoid having wrappers around
// all indexed DB objects.
const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
const sandbox = Cu.Sandbox(systemPrincipal, {
wantGlobalProperties: ["indexedDB"],
});
const { indexedDB } = sandbox;
module.exports = Object.freeze({
/**
* Only the standard version of indexedDB.open is supported.
*/
@@ -37,12 +41,14 @@ exports.createDevToolsIndexedDB = function(indexedDB) {
}
return indexedDB.openForPrincipal(principal, name, options);
},
/**
* Only the standard version of indexedDB.deleteDatabase is supported.
*/
deleteDatabase(name) {
return indexedDB.deleteForPrincipal(principal, name);
},
cmp: indexedDB.cmp.bind(indexedDB),
});
};
}

View File

@@ -132,7 +132,6 @@ function Sandbox(options) {
"FileReader",
"FormData",
"Headers",
"indexedDB",
"InspectorUtils",
"Node",
"TextDecoder",

View File

@@ -271,8 +271,3 @@ lazyGlobal("setInterval", () => {
lazyGlobal("WebSocket", () => {
return Services.appShell.hiddenDOMWindow.WebSocket;
});
lazyGlobal("indexedDB", () => {
return require("devtools/shared/indexed-db").createDevToolsIndexedDB(
indexedDB
);
});