const CLASSNAME = "TD_SimpleBackgroundThread"; const CONTRACTID = "@skrul.com/threaddemo-simple-background-thread;1"; const CID = Components.ID("{1767773d-403c-4dad-91ba-990ef2fc7d99}"); const Cc = Components.classes; const Ci = Components.interfaces; function SimpleBackgroundThread() { } // nsISupports SimpleBackgroundThread.prototype.QueryInterface = function(aIID) { if(!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIRunnable) && !aIID.equals(Ci.nsIClassInfo)) throw Components.results.NS_ERROR_NO_INTERFACE; return this; } // nsIRunnable SimpleBackgroundThread.prototype.run = function() { // Not marked threadsafe but the implementation is var thread = Cc["@mozilla.org/thread;1"] .createInstance(Ci.nsIThread).currentThread; var i = 0; while(i < 10) { this.log(thread + " " + i); thread.sleep(1000); i++; } } SimpleBackgroundThread.prototype.log = function(s) { // Not marked threadsafe but the implementation is Cc["@mozilla.org/consoleservice;1"] .getService(Ci.nsIConsoleService) .logStringMessage(s); } // nsIClassInfo SimpleBackgroundThread.prototype.getInterfaces = function(count) { var ifaces = [ Ci.nsISupports, Ci.nsIRunnable, Ci.nsIClassInfo ]; count.value = ifaces.length; return ifaces; } SimpleBackgroundThread.prototype.getHelperForLanguage = function(language) { return null; } SimpleBackgroundThread.prototype.contractID = CONTRACTID; SimpleBackgroundThread.prototype.classDescription = CLASSNAME; SimpleBackgroundThread.prototype.classID = CID; SimpleBackgroundThread.prototype.implementationLanguage = Ci.nsIProgrammingLanguage.JAVASCRIPT; SimpleBackgroundThread.prototype.flags = Ci.nsIClassInfo.THREADSAFE; /** * XPCOM Registration */ var Module = new Object(); Module.registerSelf = function(compMgr, fileSpec, location, type) { compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar); compMgr.registerFactoryLocation(CID, CLASSNAME, CONTRACTID, fileSpec, location, type); } Module.getClassObject = function(compMgr, cid, iid) { if(!cid.equals(CID)) { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; } if(!iid.equals(Ci.nsIFactory)) { throw Components.results.NS_ERROR_NO_INTERFACE; } return Factory; } Module.canUnload = function(compMgr) { return true; } var Factory = {}; Factory.createInstance = function(outer, iid) { if(outer != null) { throw Components.results.NS_ERROR_NO_AGGREGATION; } return (new SimpleBackgroundThread()).QueryInterface(iid); } function NSGetModule(compMgr, fileSpec) { return Module; }