michael@0: /** michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: var gWorkerAndCallback = { michael@0: _worker: null, michael@0: _callback: null, michael@0: michael@0: _ensureStarted: function() { michael@0: if (!this._worker) { michael@0: throw new Error("Not yet started!"); michael@0: } michael@0: }, michael@0: michael@0: start: function() { michael@0: if (!this._worker) { michael@0: var file = __LOCATION__.parent.parent; michael@0: var fileuri = file.isDirectory() ? michael@0: Services.io.newFileURI(file) : michael@0: Services.io.newURI('jar:' + file.path + '!/', null, null); michael@0: var resourceName = "worker-test"; michael@0: michael@0: Services.io.getProtocolHandler("resource"). michael@0: QueryInterface(Ci.nsIResProtocolHandler). michael@0: setSubstitution(resourceName, fileuri); michael@0: michael@0: var worker = new Worker("resource://" + resourceName + "/worker.js"); michael@0: worker.onerror = function(event) { michael@0: Cu.reportError(event.message); michael@0: event.preventDefault(); michael@0: }; michael@0: michael@0: this._worker = worker; michael@0: } michael@0: }, michael@0: michael@0: stop: function() { michael@0: if (this._worker) { michael@0: try { michael@0: this.terminate(); michael@0: } michael@0: catch(e) { michael@0: Cu.reportError(e); michael@0: } michael@0: this._worker = null; michael@0: } michael@0: }, michael@0: michael@0: set callback(val) { michael@0: this._ensureStarted(); michael@0: if (val) { michael@0: var callback = val.QueryInterface(Ci.nsIWorkerTestCallback); michael@0: if (this.callback != callback) { michael@0: this._worker.onmessage = function(event) { michael@0: callback.onmessage(event.data); michael@0: }; michael@0: this._worker.onerror = function(event) { michael@0: callback.onerror(event.message); michael@0: event.preventDefault(); michael@0: }; michael@0: this._callback = callback; michael@0: } michael@0: } michael@0: else { michael@0: this._worker.onmessage = null; michael@0: this._worker.onerror = null; michael@0: this._callback = null; michael@0: } michael@0: }, michael@0: michael@0: get callback() { michael@0: return this._callback; michael@0: }, michael@0: michael@0: postMessage: function(data) { michael@0: this._ensureStarted(); michael@0: this._worker.postMessage(data); michael@0: }, michael@0: michael@0: terminate: function() { michael@0: this._ensureStarted(); michael@0: this._worker.terminate(); michael@0: this.callback = null; michael@0: } michael@0: }; michael@0: michael@0: function WorkerTest() { michael@0: } michael@0: WorkerTest.prototype = { michael@0: observe: function(subject, topic, data) { michael@0: switch(topic) { michael@0: case "profile-after-change": michael@0: gWorkerAndCallback.start(); michael@0: Services.obs.addObserver(this, "profile-before-change", false); michael@0: break; michael@0: case "profile-before-change": michael@0: gWorkerAndCallback.stop(); michael@0: break; michael@0: default: michael@0: Cu.reportError("Unknown topic: " + topic); michael@0: } michael@0: }, michael@0: michael@0: set callback(val) { michael@0: gWorkerAndCallback.callback = val; michael@0: }, michael@0: michael@0: get callback() { michael@0: return gWorkerAndCallback.callback; michael@0: }, michael@0: michael@0: postMessage: function(message) { michael@0: gWorkerAndCallback.postMessage(message); michael@0: }, michael@0: michael@0: terminate: function() { michael@0: gWorkerAndCallback.terminate(); michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIWorkerTest]), michael@0: classID: Components.ID("{3b52b935-551f-4606-ba4c-decc18b67bfd}") michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WorkerTest]);