I'm writing a Firefox extension using the Add-on builder and Add-on SDK. So far, I've been able to work around any limitations without moving to XUL. But, I've run into a roadblock.
My extension has a long running process, which may block, so I need to separate this processing from the main UI thread. I've read the question here, but it doesn't quite answer my question. Web Workers aren't sufficient since the long running process requires access to js-ctypes and native code. So, it would seem that ChromeWorker might do the trick. But, I don't know how to get access to it from Add-on builder. Additionally, the Thread Manager is only accessible through C++ code. How can I either:
to avoid blocking the UI thread. Alternatively, I could roll my own in native code (which I'm trying to avoid).
You should be using chrome workers, they are meant specifically for using js-ctypes on a different thread. The thread manager has issues and using it from JavaScript has been disallowed for a reason. The main problem is however that ChromeWorker
constructor isn't exposed in the Add-on SDK modules. But you can get it from the chrome
pseudo-module:
var self = require("sdk/self");
var {ChromeWorker} = require("chrome");
var worker = new ChromeWorker(self.data.url("worker.js"));
This works only starting with Firefox 8, in earlier versions you would use the worker factory. But I guess that you don't need that - new Add-on SDK versions don't support anything below Firefox 9 anyway.
Note: You should make sure to shut down this worker if your add-on is disabled or uninstalled (e.g. using unload
module), it won't happen automatically.
Edit: Matthew Ruttley describes a simpler approach to import chrome workers in his detailed blog post, I updated this answer accordingly.