dom/workers/test/extensions/bootstrap/bootstrap.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial