After doing some research I found a way to create an extension consisting of several threads.
I've been able to create multiple threads, but have encountered problems when setting up the inter-thread communication.
I'd really like to use oberservers for this since they are visible from every window in Firefox.
The problem is that using basic observers as communication channels chrashes Firefox.
So I've read that wrapping objects inside proxies might solve the problem:
http://www.mozilla.org/projects/xpcom/Proxies.html
The problem is that I can't seem to get my proxy to work. I've followed examples where their used:
http://www.koders.com/javascript/fidCDF ... 4956C.aspx
And doing it the same way doesn't work for me.
When running the code below, Firefox locks up as proxyObjectManager.getProxyForObject gets called.
I'm not sure that this is the correct way to set up communication between threads.
But, if anyone has any knowledge as to how this should be done, I would appreciate to take part in that knowledge:)
Here is my code:
Code: Select all
function InterfaceAPI()
{
this.register();
}
InterfaceAPI.prototype = {
observerService : null,
register: function () {
/* Register "UI" observer callback */
this.observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
this.observerService.addObserver(this,"UI",false);
},
/* Callback for observed data on topic "UI" */
observe: function(subject ,topic, data){
alert("Response: " + data);
}
};
/* Client watcher running in separate thread */
var Watcher = {
proxyObserverService: null,
/* Call this to start the thread */
threadStart: function()
{
const nsIThread = Components.interfaces.nsIThread;
var thread = Components.classes["@mozilla.org/thread;1"].createInstance(nsIThread);
thread.init(this, /* Execute run() in this object */
0,
nsIThread.PRIORITY_NORMAL,
nsIThread.SCOPE_GLOBAL,
nsIThread.STATE_JOINABLE);
},
/* Separate thread */
run: function ()
{
/* Get UI thread event queue */
const nsIEventQueueService = Components.interfaces.nsIEventQueueService;
var eqService = Components.classes["@mozilla.org/event-queue-service;1"]
.getService(nsIEventQueueService);
var uiQ = eqService.getSpecialEventQueue(nsIEventQueueService.UI_THREAD_EVENT_QUEUE);
/* Create basic observer */
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
/* Create a proxy around the observer */
var proxyObjectManager = Components.classes["@mozilla.org/xpcomproxy;1"]
.getService(Components.interfaces.nsIProxyObjectManager);
var proxyType = proxyObjectManager.INVOKE_ASYNC | proxyObjectManager.FORCE_PROXY_CREATION
/* !!!!!!! Firefox chrashes during getProxyForObject call !!!!!!!
* EDIT: The second parameter was not syntactically correct. Fixed now, and the code works. */
this.proxyObserverService = proxyObjectManager.getProxyForObject(uiQ,
Components.interfaces.nsIObserverService,
observerService,
proxyType);
/* Notify other thread */
this.proxyObserverService.notifyObservers(null,"UI","I'm alive!");
}
};
new InterfaceAPI();
Watcher.threadStart();