dom/workers/test/extensions/traditional/WorkerTest.js

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

mercurial