const CLASSNAME = "TD_Service"; const CONTRACTID = "@skrul.com/threaddemo-service;1"; const CID = Components.ID("{4c875a25-c5ce-4c3e-99cf-f844ee4d6f59}"); const Cc = Components.classes; const Ci = Components.interfaces; var threadDemoService = {} // nsISupports threadDemoService.QueryInterface = function(aIID) { if(!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.tdIService)) throw Components.results.NS_ERROR_NO_INTERFACE; return this; } // nsIThreadDemoService threadDemoService.simpleBackgroundThread = function() { var runnable = Cc["@skrul.com/threaddemo-simple-background-thread;1"] .createInstance(Ci.nsIRunnable); var thread = Cc["@mozilla.org/thread;1"] .createInstance(Ci.nsIThread); thread.init(runnable, 0, Ci.nsIThread.PRIORITY_NORMAL, Ci.nsIThread.SCOPE_LOCAL, Ci.nsIThread.STATE_UNJOINABLE); return thread; } threadDemoService.proxyToUiThread = function(callback) { // Get a reference to the UI's event queue var eqs = Cc["@mozilla.org/event-queue-service;1"] .getService(Ci.nsIEventQueueService); var uiEventQueue = eqs.getSpecialEventQueue(Ci.UI_THREAD_EVENT_QUEUE); // Wrap the supplied callback in a proxy using the ui event queue var pom = Cc["@mozilla.org/xpcomproxy;1"] .getService(Ci.nsIProxyObjectManager); var proxiedCallback = pom.getProxyForObject(uiEventQueue, Ci.tdICallback, callback, Ci.nsIProxyObjectManager.INVOKE_ASYNC); // Create the component and set the callback var runnable = Cc["@skrul.com/threaddemo-proxy-to-ui-thread;1"] .createInstance(Ci.tdIProxyToUiThread); runnable.setCallback(proxiedCallback); // Run the component on a new thread var thread = Cc["@mozilla.org/thread;1"] .createInstance(Ci.nsIThread); thread.init(runnable, 0, Ci.nsIThread.PRIORITY_NORMAL, Ci.nsIThread.SCOPE_LOCAL, Ci.nsIThread.STATE_UNJOINABLE); return thread; } threadDemoService.startAsynchService = function(callback) { var asynchService = Cc["@skrul.com/threaddemo-asynch-service;1"] .createInstance(Ci.tdIAsynchService); return asynchService.start(callback); } /** * 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 threadDemoService; } function NSGetModule(compMgr, fileSpec) { return Module; }