1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/workers/test/extensions/bootstrap/bootstrap.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 + 1.9 +const Ci = Components.interfaces; 1.10 +const Cu = Components.utils; 1.11 + 1.12 +Cu.import("resource://gre/modules/Services.jsm"); 1.13 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.14 + 1.15 +function testForExpectedSymbols(stage, data) { 1.16 + const expectedSymbols = [ "Worker", "ChromeWorker" ]; 1.17 + for each (var symbol in expectedSymbols) { 1.18 + Services.prefs.setBoolPref("workertest.bootstrap." + stage + "." + symbol, 1.19 + symbol in this); 1.20 + } 1.21 +} 1.22 + 1.23 +var gWorkerAndCallback = { 1.24 + _ensureStarted: function() { 1.25 + if (!this._worker) { 1.26 + throw new Error("Not yet started!"); 1.27 + } 1.28 + }, 1.29 + 1.30 + start: function(data) { 1.31 + if (!this._worker) { 1.32 + var file = data.installPath; 1.33 + var fileuri = file.isDirectory() ? 1.34 + Services.io.newFileURI(file) : 1.35 + Services.io.newURI('jar:' + file.path + '!/', null, null); 1.36 + var resourceName = encodeURIComponent(data.id); 1.37 + 1.38 + Services.io.getProtocolHandler("resource"). 1.39 + QueryInterface(Ci.nsIResProtocolHandler). 1.40 + setSubstitution(resourceName, fileuri); 1.41 + 1.42 + this._worker = new Worker("resource://" + resourceName + "/worker.js"); 1.43 + this._worker.onerror = function(event) { 1.44 + Cu.reportError(event.message); 1.45 + event.preventDefault(); 1.46 + }; 1.47 + } 1.48 + }, 1.49 + 1.50 + stop: function() { 1.51 + if (this._worker) { 1.52 + this._worker.terminate(); 1.53 + delete this._worker; 1.54 + } 1.55 + }, 1.56 + 1.57 + set callback(val) { 1.58 + this._ensureStarted(); 1.59 + var callback = val.QueryInterface(Ci.nsIObserver); 1.60 + if (this._callback != callback) { 1.61 + if (callback) { 1.62 + this._worker.onmessage = function(event) { 1.63 + callback.observe(this, event.type, event.data); 1.64 + }; 1.65 + this._worker.onerror = function(event) { 1.66 + callback.observe(this, event.type, event.message); 1.67 + event.preventDefault(); 1.68 + }; 1.69 + } 1.70 + else { 1.71 + this._worker.onmessage = null; 1.72 + this._worker.onerror = null; 1.73 + } 1.74 + this._callback = callback; 1.75 + } 1.76 + }, 1.77 + 1.78 + get callback() { 1.79 + return this._callback; 1.80 + }, 1.81 + 1.82 + postMessage: function(data) { 1.83 + this._ensureStarted(); 1.84 + this._worker.postMessage(data); 1.85 + }, 1.86 + 1.87 + terminate: function() { 1.88 + this._ensureStarted(); 1.89 + this._worker.terminate(); 1.90 + delete this._callback; 1.91 + } 1.92 +}; 1.93 + 1.94 +function WorkerTestBootstrap() { 1.95 +} 1.96 +WorkerTestBootstrap.prototype = { 1.97 + observe: function(subject, topic, data) { 1.98 + 1.99 + gWorkerAndCallback.callback = subject; 1.100 + 1.101 + switch (topic) { 1.102 + case "postMessage": 1.103 + gWorkerAndCallback.postMessage(data); 1.104 + break; 1.105 + 1.106 + case "terminate": 1.107 + gWorkerAndCallback.terminate(); 1.108 + break; 1.109 + 1.110 + default: 1.111 + throw new Error("Unknown worker command"); 1.112 + } 1.113 + }, 1.114 + 1.115 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]) 1.116 +}; 1.117 + 1.118 +var gFactory = { 1.119 + register: function() { 1.120 + var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 1.121 + 1.122 + var classID = Components.ID("{36b5df0b-8dcf-4aa2-9c45-c51d871295f9}"); 1.123 + var description = "WorkerTestBootstrap"; 1.124 + var contractID = "@mozilla.org/test/workertestbootstrap;1"; 1.125 + var factory = XPCOMUtils._getFactory(WorkerTestBootstrap); 1.126 + 1.127 + registrar.registerFactory(classID, description, contractID, factory); 1.128 + 1.129 + this.unregister = function() { 1.130 + registrar.unregisterFactory(classID, factory); 1.131 + delete this.unregister; 1.132 + }; 1.133 + } 1.134 +}; 1.135 + 1.136 +function install(data, reason) { 1.137 + testForExpectedSymbols("install"); 1.138 +} 1.139 + 1.140 +function startup(data, reason) { 1.141 + testForExpectedSymbols("startup"); 1.142 + gFactory.register(); 1.143 + gWorkerAndCallback.start(data); 1.144 +} 1.145 + 1.146 +function shutdown(data, reason) { 1.147 + testForExpectedSymbols("shutdown"); 1.148 + gWorkerAndCallback.stop(); 1.149 + gFactory.unregister(); 1.150 +} 1.151 + 1.152 +function uninstall(data, reason) { 1.153 + testForExpectedSymbols("uninstall"); 1.154 +}