My current implementation of the extension looks like this and does
not include such a "global function".
var myextensionname = {
onLoad: function()
{
//do stuff
},
onUnLoad: function()
{
//do stuff
},
// some other interesting functions
}
window.addEventListener("load", function(e) { myextensionname.onLoad
(); }, false);
window.addEventListener("unload", function(e)
{ myextensionname.onUnLoad(); }, false);
Now I want to declare a "global function" somefunction() that should
be called on application startup. It will then make an asynchronous
xmlhttprequest which will, after being successful, execute somefunction
() again via setTimeout(). And so on.
So the whole thing should run in the background as long as firefox is
running. Even if the user opens a second browser window and closes the
first one.
My second problem is that these background workers should also fire
events so that my normal functions can update the window contents with
the new data.
I would like to know the best way to implement this. Is this
documented somewhere or are there examples?
Kind regards,
Ulrich
But one problem is left. The background thread is doing some work,
that may take longer than a few seconds. If the user quits firefox
while the background thread is running, firefox will wait until the
background thread finishes. Calling nsIThread shutdown() on
application shutdown does not work, because shutdown() does not kill
the thread. Is there a function to kill a thread, or isn't that
possible?
You might find it easier to use a singleton JS module instead of a
singleton XPCOM component. Concerning killing of the thread, thread
manangement from extension javascript has been notoriously difficult
in the past. It might be easier (albeit not more performant) for your
thread to periodically check if any navigator:browser windows are open
and, if not, fall through:
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
if (wm) { /* do stuff */ } else { /* end */ }
Eric
>It might be easier (albeit not more performant) for your thread to periodically check if any navigator:browser windows
>
No! DOM objects, including windows, may only be used on the main thread.
--
Warning: May contain traces of nuts.
> Concerning killing of the thread, thread
> manangement from extension javascript has been notoriously difficult
> in the past.
"Difficult" means it is possible? I could only find the function
shutdown(). In the XPCOM component, I observe "quit-application" and
when the app quits, I tried to use this.mythread.shutdown(), but this
waits for the thread to finish (which could take some time). I've also
tried this.mythred = null: same problem.
The thread would only be used for some XML processing, so a hard kill
would be no problem.
> It might be easier (albeit not more performant) for your
> thread to periodically check if any navigator:browser windows are open
> and, if not, fall through:
>
> var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
>
> .getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
>
> if (wm) { /* do stuff */ } else { /* end */ }
Good idea. I could also use a global variable "this.stopThread" in the
XPCOM component and set it to true in my observer. It works. But
killing the thread would be better.
Kind regards,
Ulrich
>This is only checking the existence of the window, not manipulating it or its contents in any way. Are you saying even that won't behave on a worker thread?
>
>
Just looking at a window adds a reference to it, which is already unsafe.