Bug 1202478 - [webext] Run WebExtension callbacks asynchronously (r=gabor)

This commit is contained in:
Bill McCloskey 2015-08-30 19:54:13 -07:00
parent a80f464d6a
commit 6b53f0338d
4 changed files with 42 additions and 10 deletions

View File

@ -30,7 +30,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
var {
runSafeWithoutClone,
runSafeSyncWithoutClone,
MessageBroker,
Messenger,
ignoreEvent,
@ -137,12 +137,12 @@ Script.prototype = {
for (let url of this.css) {
url = extension.baseURI.resolve(url);
runSafeWithoutClone(winUtils.loadSheetUsingURIString, url, winUtils.AUTHOR_SHEET);
runSafeSyncWithoutClone(winUtils.loadSheetUsingURIString, url, winUtils.AUTHOR_SHEET);
}
if (this.options.cssCode) {
let url = "data:text/css;charset=utf-8," + encodeURIComponent(this.options.cssCode);
runSafeWithoutClone(winUtils.loadSheetUsingURIString, url, winUtils.AUTHOR_SHEET);
runSafeSyncWithoutClone(winUtils.loadSheetUsingURIString, url, winUtils.AUTHOR_SHEET);
}
}
@ -162,7 +162,7 @@ Script.prototype = {
charset: "UTF-8",
async: AppConstants.platform == "gonk"
}
Services.scriptloader.loadSubScriptWithOptions(url, options);
runSafeSyncWithoutClone(Services.scriptloader.loadSubScriptWithOptions, url, options);
}
if (this.options.jsCode) {

View File

@ -15,16 +15,41 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
// Run a function and report exceptions.
function runSafeWithoutClone(f, ...args)
function runSafeSyncWithoutClone(f, ...args)
{
try {
return f(...args);
} catch (e) {
dump(`Extension error: ${e} ${e.fileName} ${e.lineNumber}\n${e.stack}\n${Error().stack}`);
dump(`Extension error: ${e} ${e.fileName} ${e.lineNumber}\n[[Exception stack\n${e.stack}Current stack\n${Error().stack}]]\n`);
Cu.reportError(e);
}
}
// Run a function and report exceptions.
function runSafeWithoutClone(f, ...args)
{
if (typeof(f) != "function") {
dump(`Extension error: expected function\n${Error().stack}`);
return;
}
Services.tm.currentThread.dispatch(function() {
runSafeSyncWithoutClone(f, ...args);
}, Ci.nsIEventTarget.DISPATCH_NORMAL);
}
// Run a function, cloning arguments into context.cloneScope, and
// report exceptions. |f| is expected to be in context.cloneScope.
function runSafeSync(context, f, ...args)
{
try {
args = Cu.cloneInto(args, context.cloneScope);
} catch (e) {
dump(`runSafe failure\n${context.cloneScope}\n${Error().stack}`);
}
return runSafeSyncWithoutClone(f, ...args);
}
// Run a function, cloning arguments into context.cloneScope, and
// report exceptions. |f| is expected to be in context.cloneScope.
function runSafe(context, f, ...args)
@ -96,6 +121,11 @@ function EventManager(context, name, register)
EventManager.prototype = {
addListener(callback) {
if (typeof(callback) != "function") {
dump(`Expected function\n${Error().stack}`);
return;
}
if (!this.registered) {
this.context.callOnClose(this);
@ -132,7 +162,7 @@ EventManager.prototype = {
fireWithoutClone(...args) {
for (let callback of this.callbacks) {
runSafeWithoutClone(callback, ...args);
runSafeSyncWithoutClone(callback, ...args);
}
},
@ -534,7 +564,9 @@ function flushJarCache(jarFile)
this.ExtensionUtils = {
runSafeWithoutClone,
runSafeSyncWithoutClone,
runSafe,
runSafeSync,
DefaultWeakMap,
EventManager,
SingletonEventManager,

View File

@ -45,7 +45,7 @@ function WebNavigationEventManager(context, eventName)
return;
}
return runSafe(context, callback, data2);
runSafe(context, callback, data2);
};
WebNavigation[eventName].addListener(listener);

View File

@ -10,7 +10,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "WebRequest",
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
var {
SingletonEventManager,
runSafe,
runSafeSync,
} = ExtensionUtils;
// EventManager-like class specifically for WebRequest. Inherits from
@ -53,7 +53,7 @@ function WebRequestEventManager(context, eventName)
}
}
return runSafe(context, callback, data2);
return runSafeSync(context, callback, data2);
};
let filter2 = {};