dom/workers/test/sharedWorker_sharedWorker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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 "use strict";
michael@0 6
michael@0 7 if (!("self" in this)) {
michael@0 8 throw new Error("No 'self' exists on SharedWorkerGlobalScope!");
michael@0 9 }
michael@0 10 if (this !== self) {
michael@0 11 throw new Error("'self' not equal to global object!");
michael@0 12 }
michael@0 13 if (!(self instanceof SharedWorkerGlobalScope)) {
michael@0 14 throw new Error("self not a SharedWorkerGlobalScope instance!");
michael@0 15 }
michael@0 16
michael@0 17 var propsToCheck = [
michael@0 18 "location",
michael@0 19 "navigator",
michael@0 20 "close",
michael@0 21 "importScripts",
michael@0 22 "setTimeout",
michael@0 23 "clearTimeout",
michael@0 24 "setInterval",
michael@0 25 "clearInterval",
michael@0 26 "dump",
michael@0 27 "atob",
michael@0 28 "btoa"
michael@0 29 ];
michael@0 30
michael@0 31 for (var index = 0; index < propsToCheck.length; index++) {
michael@0 32 var prop = propsToCheck[index];
michael@0 33 if (!(prop in self)) {
michael@0 34 throw new Error("SharedWorkerGlobalScope has no '" + prop + "' property!");
michael@0 35 }
michael@0 36 }
michael@0 37
michael@0 38 onconnect = function(event) {
michael@0 39 if (!("SharedWorkerGlobalScope" in self)) {
michael@0 40 throw new Error("SharedWorkerGlobalScope should be visible!");
michael@0 41 }
michael@0 42 if (!(self instanceof SharedWorkerGlobalScope)) {
michael@0 43 throw new Error("The global should be a SharedWorkerGlobalScope!");
michael@0 44 }
michael@0 45 if (!(self instanceof WorkerGlobalScope)) {
michael@0 46 throw new Error("The global should be a WorkerGlobalScope!");
michael@0 47 }
michael@0 48 if ("DedicatedWorkerGlobalScope" in self) {
michael@0 49 throw new Error("DedicatedWorkerGlobalScope should not be visible!");
michael@0 50 }
michael@0 51 if (!(event instanceof MessageEvent)) {
michael@0 52 throw new Error("'connect' event is not a MessageEvent!");
michael@0 53 }
michael@0 54 if (!("ports" in event)) {
michael@0 55 throw new Error("'connect' event doesn't have a 'ports' property!");
michael@0 56 }
michael@0 57 if (event.ports.length != 1) {
michael@0 58 throw new Error("'connect' event has a 'ports' property with length '" +
michael@0 59 event.ports.length + "'!");
michael@0 60 }
michael@0 61 if (!event.ports[0]) {
michael@0 62 throw new Error("'connect' event has a null 'ports[0]' property!");
michael@0 63 }
michael@0 64 if (!(event.ports[0] instanceof MessagePort)) {
michael@0 65 throw new Error("'connect' event has a 'ports[0]' property that isn't a " +
michael@0 66 "MessagePort!");
michael@0 67 }
michael@0 68 if (!(event.ports[0] == event.source)) {
michael@0 69 throw new Error("'connect' event source property is incorrect!");
michael@0 70 }
michael@0 71 if (event.data) {
michael@0 72 throw new Error("'connect' event has data: " + event.data);
michael@0 73 }
michael@0 74
michael@0 75 event.ports[0].onmessage = function(event) {
michael@0 76 if (!(event instanceof MessageEvent)) {
michael@0 77 throw new Error("'message' event is not a MessageEvent!");
michael@0 78 }
michael@0 79 if (!("ports" in event)) {
michael@0 80 throw new Error("'message' event doesn't have a 'ports' property!");
michael@0 81 }
michael@0 82 if (!(event.ports === null)) {
michael@0 83 throw new Error("'message' event has a non-null 'ports' property!");
michael@0 84 }
michael@0 85 event.target.postMessage(event.data);
michael@0 86 throw new Error(event.data);
michael@0 87 };
michael@0 88 };

mercurial