new ChomeWorker in FF >= 8

134 views
Skip to first unread message

Loic J. Duros

unread,
Dec 23, 2011, 1:02:12 PM12/23/11
to mozilla-la...@googlegroups.com
Hello,

I'm trying to use a Chrome Worker in my addon. Since Firefox 8 you
theoretically don't need to use Cc/Ci to instantiate the Chrome Worker,
instead you use new ChromeWorker.

However, this will throw an error with Jetpack since it finds
ChromeWorker to be undefined.
Would there be something special to do to make this work with jetpack? I
need to use a ChromeWorker so that expensive processing can be running
in its own thread! Many thanks! :-)

Here is my main.js:
var data = require("self").data;

var {Cc, Ci} = require('chrome');

if (Cc["@mozilla.org/threads/workerfactory;1"]) {
// Firefox 4-7
var workerFactory = Cc["@mozilla.org/threads/workerfactory;1"]
.createInstance(Ci.nsIWorkerFactory);
var worker = workerFactory.newChromeWorker(data.url("myworker.js"));
} else {
// Firefox 8+
var worker = new ChromeWorker(data.url("myworker.js"));

}

worker.onmessage = function(event) {
console.log("Worker said:" + event.data);
};

and the content of myworker.js (in the data/ folder):
self.onmessage = function(event) {
self.postMessage('Hi there!');
};

ZER0

unread,
Jan 3, 2012, 9:24:32 AM1/3/12
to mozilla-la...@googlegroups.com
On 12/23/11 19:02 PM, Loic J. Duros wrote:

> However, this will throw an error with Jetpack since it finds
> ChromeWorker to be undefined.
> Would there be something special to do to make this work with jetpack? I
> need to use a ChromeWorker so that expensive processing can be running
> in its own thread! Many thanks! :-)


ChromeWorker is defined in the global object and JSM in the regular XUL
development.
Unfortunately, a jetpack module is a different thing. I don't know if
there are any plan to have the Worker / ChromeWorker available directly
from jetpack, I hope so, because since FF8 there is no way to obtain
them using XPCOM.

At the moment I will suggest to you to load a JS Module through
`Cu.import` where export / instantiate the ChromeWorker.

Reply all
Reply to author
Forward
0 new messages