Isolate functions passed across window contexts

This commit is contained in:
toasted-nutbread 2019-12-13 23:26:32 -05:00
parent 87ece13647
commit ddaf44ba7b

View File

@ -32,11 +32,32 @@ function utilIsolate(value) {
return typeof stringValue === 'string' ? JSON.parse(stringValue) : null; return typeof stringValue === 'string' ? JSON.parse(stringValue) : null;
} }
function utilFunctionIsolate(func) {
return function (...args) {
try {
return func.call(this, ...args);
} catch (e) {
try {
String(func);
} catch (e2) {
// Dead object
return;
}
throw e;
}
};
}
function utilBackgroundIsolate(data) { function utilBackgroundIsolate(data) {
const backgroundPage = chrome.extension.getBackgroundPage(); const backgroundPage = chrome.extension.getBackgroundPage();
return backgroundPage.utilIsolate(data); return backgroundPage.utilIsolate(data);
} }
function utilBackgroundFunctionIsolate(func) {
const backgroundPage = chrome.extension.getBackgroundPage();
return backgroundPage.utilFunctionIsolate(func);
}
function utilSetEqual(setA, setB) { function utilSetEqual(setA, setB) {
if (setA.size !== setB.size) { if (setA.size !== setB.size) {
return false; return false;
@ -110,15 +131,15 @@ function utilDatabasePurge() {
function utilDatabaseDeleteDictionary(dictionaryName, onProgress) { function utilDatabaseDeleteDictionary(dictionaryName, onProgress) {
return utilBackend().translator.database.deleteDictionary( return utilBackend().translator.database.deleteDictionary(
utilBackgroundIsolate(dictionaryName), utilBackgroundIsolate(dictionaryName),
onProgress utilBackgroundFunctionIsolate(onProgress)
); );
} }
async function utilDatabaseImport(data, progress, details) { async function utilDatabaseImport(data, onProgress, details) {
data = await utilReadFile(data); data = await utilReadFile(data);
return utilBackend().translator.database.importDictionary( return utilBackend().translator.database.importDictionary(
utilBackgroundIsolate(data), utilBackgroundIsolate(data),
progress, utilBackgroundFunctionIsolate(onProgress),
utilBackgroundIsolate(details) utilBackgroundIsolate(details)
); );
} }