Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
6 const Ci = Components.interfaces;
7 const Cu = Components.utils;
9 Cu.import("resource://gre/modules/Services.jsm");
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
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 }
20 var gWorkerAndCallback = {
21 _ensureStarted: function() {
22 if (!this._worker) {
23 throw new Error("Not yet started!");
24 }
25 },
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);
35 Services.io.getProtocolHandler("resource").
36 QueryInterface(Ci.nsIResProtocolHandler).
37 setSubstitution(resourceName, fileuri);
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 },
47 stop: function() {
48 if (this._worker) {
49 this._worker.terminate();
50 delete this._worker;
51 }
52 },
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 },
75 get callback() {
76 return this._callback;
77 },
79 postMessage: function(data) {
80 this._ensureStarted();
81 this._worker.postMessage(data);
82 },
84 terminate: function() {
85 this._ensureStarted();
86 this._worker.terminate();
87 delete this._callback;
88 }
89 };
91 function WorkerTestBootstrap() {
92 }
93 WorkerTestBootstrap.prototype = {
94 observe: function(subject, topic, data) {
96 gWorkerAndCallback.callback = subject;
98 switch (topic) {
99 case "postMessage":
100 gWorkerAndCallback.postMessage(data);
101 break;
103 case "terminate":
104 gWorkerAndCallback.terminate();
105 break;
107 default:
108 throw new Error("Unknown worker command");
109 }
110 },
112 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver])
113 };
115 var gFactory = {
116 register: function() {
117 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
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);
124 registrar.registerFactory(classID, description, contractID, factory);
126 this.unregister = function() {
127 registrar.unregisterFactory(classID, factory);
128 delete this.unregister;
129 };
130 }
131 };
133 function install(data, reason) {
134 testForExpectedSymbols("install");
135 }
137 function startup(data, reason) {
138 testForExpectedSymbols("startup");
139 gFactory.register();
140 gWorkerAndCallback.start(data);
141 }
143 function shutdown(data, reason) {
144 testForExpectedSymbols("shutdown");
145 gWorkerAndCallback.stop();
146 gFactory.unregister();
147 }
149 function uninstall(data, reason) {
150 testForExpectedSymbols("uninstall");
151 }