|
1 /** |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 */ |
|
5 |
|
6 const Ci = Components.interfaces; |
|
7 const Cu = Components.utils; |
|
8 |
|
9 Cu.import("resource://gre/modules/Services.jsm"); |
|
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
11 |
|
12 function testForExpectedSymbols(stage, data) { |
|
13 const expectedSymbols = [ "Worker", "ChromeWorker" ]; |
|
14 for each (var symbol in expectedSymbols) { |
|
15 Services.prefs.setBoolPref("workertest.bootstrap." + stage + "." + symbol, |
|
16 symbol in this); |
|
17 } |
|
18 } |
|
19 |
|
20 var gWorkerAndCallback = { |
|
21 _ensureStarted: function() { |
|
22 if (!this._worker) { |
|
23 throw new Error("Not yet started!"); |
|
24 } |
|
25 }, |
|
26 |
|
27 start: function(data) { |
|
28 if (!this._worker) { |
|
29 var file = data.installPath; |
|
30 var fileuri = file.isDirectory() ? |
|
31 Services.io.newFileURI(file) : |
|
32 Services.io.newURI('jar:' + file.path + '!/', null, null); |
|
33 var resourceName = encodeURIComponent(data.id); |
|
34 |
|
35 Services.io.getProtocolHandler("resource"). |
|
36 QueryInterface(Ci.nsIResProtocolHandler). |
|
37 setSubstitution(resourceName, fileuri); |
|
38 |
|
39 this._worker = new Worker("resource://" + resourceName + "/worker.js"); |
|
40 this._worker.onerror = function(event) { |
|
41 Cu.reportError(event.message); |
|
42 event.preventDefault(); |
|
43 }; |
|
44 } |
|
45 }, |
|
46 |
|
47 stop: function() { |
|
48 if (this._worker) { |
|
49 this._worker.terminate(); |
|
50 delete this._worker; |
|
51 } |
|
52 }, |
|
53 |
|
54 set callback(val) { |
|
55 this._ensureStarted(); |
|
56 var callback = val.QueryInterface(Ci.nsIObserver); |
|
57 if (this._callback != callback) { |
|
58 if (callback) { |
|
59 this._worker.onmessage = function(event) { |
|
60 callback.observe(this, event.type, event.data); |
|
61 }; |
|
62 this._worker.onerror = function(event) { |
|
63 callback.observe(this, event.type, event.message); |
|
64 event.preventDefault(); |
|
65 }; |
|
66 } |
|
67 else { |
|
68 this._worker.onmessage = null; |
|
69 this._worker.onerror = null; |
|
70 } |
|
71 this._callback = callback; |
|
72 } |
|
73 }, |
|
74 |
|
75 get callback() { |
|
76 return this._callback; |
|
77 }, |
|
78 |
|
79 postMessage: function(data) { |
|
80 this._ensureStarted(); |
|
81 this._worker.postMessage(data); |
|
82 }, |
|
83 |
|
84 terminate: function() { |
|
85 this._ensureStarted(); |
|
86 this._worker.terminate(); |
|
87 delete this._callback; |
|
88 } |
|
89 }; |
|
90 |
|
91 function WorkerTestBootstrap() { |
|
92 } |
|
93 WorkerTestBootstrap.prototype = { |
|
94 observe: function(subject, topic, data) { |
|
95 |
|
96 gWorkerAndCallback.callback = subject; |
|
97 |
|
98 switch (topic) { |
|
99 case "postMessage": |
|
100 gWorkerAndCallback.postMessage(data); |
|
101 break; |
|
102 |
|
103 case "terminate": |
|
104 gWorkerAndCallback.terminate(); |
|
105 break; |
|
106 |
|
107 default: |
|
108 throw new Error("Unknown worker command"); |
|
109 } |
|
110 }, |
|
111 |
|
112 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]) |
|
113 }; |
|
114 |
|
115 var gFactory = { |
|
116 register: function() { |
|
117 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
|
118 |
|
119 var classID = Components.ID("{36b5df0b-8dcf-4aa2-9c45-c51d871295f9}"); |
|
120 var description = "WorkerTestBootstrap"; |
|
121 var contractID = "@mozilla.org/test/workertestbootstrap;1"; |
|
122 var factory = XPCOMUtils._getFactory(WorkerTestBootstrap); |
|
123 |
|
124 registrar.registerFactory(classID, description, contractID, factory); |
|
125 |
|
126 this.unregister = function() { |
|
127 registrar.unregisterFactory(classID, factory); |
|
128 delete this.unregister; |
|
129 }; |
|
130 } |
|
131 }; |
|
132 |
|
133 function install(data, reason) { |
|
134 testForExpectedSymbols("install"); |
|
135 } |
|
136 |
|
137 function startup(data, reason) { |
|
138 testForExpectedSymbols("startup"); |
|
139 gFactory.register(); |
|
140 gWorkerAndCallback.start(data); |
|
141 } |
|
142 |
|
143 function shutdown(data, reason) { |
|
144 testForExpectedSymbols("shutdown"); |
|
145 gWorkerAndCallback.stop(); |
|
146 gFactory.unregister(); |
|
147 } |
|
148 |
|
149 function uninstall(data, reason) { |
|
150 testForExpectedSymbols("uninstall"); |
|
151 } |