Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /** |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | const Cc = Components.classes; |
michael@0 | 7 | const Ci = Components.interfaces; |
michael@0 | 8 | const Cu = Components.utils; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | var gWorkerAndCallback = { |
michael@0 | 14 | _worker: null, |
michael@0 | 15 | _callback: null, |
michael@0 | 16 | |
michael@0 | 17 | _ensureStarted: function() { |
michael@0 | 18 | if (!this._worker) { |
michael@0 | 19 | throw new Error("Not yet started!"); |
michael@0 | 20 | } |
michael@0 | 21 | }, |
michael@0 | 22 | |
michael@0 | 23 | start: function() { |
michael@0 | 24 | if (!this._worker) { |
michael@0 | 25 | var file = __LOCATION__.parent.parent; |
michael@0 | 26 | var fileuri = file.isDirectory() ? |
michael@0 | 27 | Services.io.newFileURI(file) : |
michael@0 | 28 | Services.io.newURI('jar:' + file.path + '!/', null, null); |
michael@0 | 29 | var resourceName = "worker-test"; |
michael@0 | 30 | |
michael@0 | 31 | Services.io.getProtocolHandler("resource"). |
michael@0 | 32 | QueryInterface(Ci.nsIResProtocolHandler). |
michael@0 | 33 | setSubstitution(resourceName, fileuri); |
michael@0 | 34 | |
michael@0 | 35 | var worker = new Worker("resource://" + resourceName + "/worker.js"); |
michael@0 | 36 | worker.onerror = function(event) { |
michael@0 | 37 | Cu.reportError(event.message); |
michael@0 | 38 | event.preventDefault(); |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | this._worker = worker; |
michael@0 | 42 | } |
michael@0 | 43 | }, |
michael@0 | 44 | |
michael@0 | 45 | stop: function() { |
michael@0 | 46 | if (this._worker) { |
michael@0 | 47 | try { |
michael@0 | 48 | this.terminate(); |
michael@0 | 49 | } |
michael@0 | 50 | catch(e) { |
michael@0 | 51 | Cu.reportError(e); |
michael@0 | 52 | } |
michael@0 | 53 | this._worker = null; |
michael@0 | 54 | } |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | set callback(val) { |
michael@0 | 58 | this._ensureStarted(); |
michael@0 | 59 | if (val) { |
michael@0 | 60 | var callback = val.QueryInterface(Ci.nsIWorkerTestCallback); |
michael@0 | 61 | if (this.callback != callback) { |
michael@0 | 62 | this._worker.onmessage = function(event) { |
michael@0 | 63 | callback.onmessage(event.data); |
michael@0 | 64 | }; |
michael@0 | 65 | this._worker.onerror = function(event) { |
michael@0 | 66 | callback.onerror(event.message); |
michael@0 | 67 | event.preventDefault(); |
michael@0 | 68 | }; |
michael@0 | 69 | this._callback = callback; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | else { |
michael@0 | 73 | this._worker.onmessage = null; |
michael@0 | 74 | this._worker.onerror = null; |
michael@0 | 75 | this._callback = null; |
michael@0 | 76 | } |
michael@0 | 77 | }, |
michael@0 | 78 | |
michael@0 | 79 | get callback() { |
michael@0 | 80 | return this._callback; |
michael@0 | 81 | }, |
michael@0 | 82 | |
michael@0 | 83 | postMessage: function(data) { |
michael@0 | 84 | this._ensureStarted(); |
michael@0 | 85 | this._worker.postMessage(data); |
michael@0 | 86 | }, |
michael@0 | 87 | |
michael@0 | 88 | terminate: function() { |
michael@0 | 89 | this._ensureStarted(); |
michael@0 | 90 | this._worker.terminate(); |
michael@0 | 91 | this.callback = null; |
michael@0 | 92 | } |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | function WorkerTest() { |
michael@0 | 96 | } |
michael@0 | 97 | WorkerTest.prototype = { |
michael@0 | 98 | observe: function(subject, topic, data) { |
michael@0 | 99 | switch(topic) { |
michael@0 | 100 | case "profile-after-change": |
michael@0 | 101 | gWorkerAndCallback.start(); |
michael@0 | 102 | Services.obs.addObserver(this, "profile-before-change", false); |
michael@0 | 103 | break; |
michael@0 | 104 | case "profile-before-change": |
michael@0 | 105 | gWorkerAndCallback.stop(); |
michael@0 | 106 | break; |
michael@0 | 107 | default: |
michael@0 | 108 | Cu.reportError("Unknown topic: " + topic); |
michael@0 | 109 | } |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | set callback(val) { |
michael@0 | 113 | gWorkerAndCallback.callback = val; |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | get callback() { |
michael@0 | 117 | return gWorkerAndCallback.callback; |
michael@0 | 118 | }, |
michael@0 | 119 | |
michael@0 | 120 | postMessage: function(message) { |
michael@0 | 121 | gWorkerAndCallback.postMessage(message); |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | terminate: function() { |
michael@0 | 125 | gWorkerAndCallback.terminate(); |
michael@0 | 126 | }, |
michael@0 | 127 | |
michael@0 | 128 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIWorkerTest]), |
michael@0 | 129 | classID: Components.ID("{3b52b935-551f-4606-ba4c-decc18b67bfd}") |
michael@0 | 130 | }; |
michael@0 | 131 | |
michael@0 | 132 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WorkerTest]); |