const CLASSNAME = "TD_ProxyToUiThread"; const CONTRACTID = "@skrul.com/threaddemo-proxy-to-ui-thread;1"; const CID = Components.ID("{303f2c60-658c-4e3b-89f5-32ed967eb84e}"); const Cc = Components.classes; const Ci = Components.interfaces; function ProxyToUiThread() { } // nsISupports ProxyToUiThread.prototype.QueryInterface = function(aIID) { if(!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIRunnable) && !aIID.equals(Ci.tdIProxyToUiThread) && !aIID.equals(Ci.nsIClassInfo)) throw Components.results.NS_ERROR_NO_INTERFACE; return this; } // nsIRunnable ProxyToUiThread.prototype.run = function() { var thread = Cc["@mozilla.org/thread;1"] .createInstance(Ci.nsIThread).currentThread; var i = 0; while(i < 10) { this.callback.notify(thread + " " + i); thread.sleep(1000); i++; } } // tdIProxyToUiThread ProxyToUiThread.prototype.setCallback = function(callback) { this.callback = callback; } // nsIClassInfo ProxyToUiThread.prototype.getInterfaces = function(count) { var ifaces = [ Ci.nsISupports, Ci.nsIRunnable, Ci.tdIProxyToUiThread, Ci.nsIClassInfo ]; count.value = ifaces.length; return ifaces; } ProxyToUiThread.prototype.getHelperForLanguage = function(language) { return null; } ProxyToUiThread.prototype.contractID = CONTRACTID; ProxyToUiThread.prototype.classDescription = CLASSNAME; ProxyToUiThread.prototype.classID = CID; ProxyToUiThread.prototype.implementationLanguage = Ci.nsIProgrammingLanguage.JAVASCRIPT; ProxyToUiThread.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 ProxyToUiThread()).QueryInterface(iid); } function NSGetModule(compMgr, fileSpec) { return Module; }