sharedWorker_sharedWorker.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. "use strict";
  6. if (!("self" in this)) {
  7. throw new Error("No 'self' exists on SharedWorkerGlobalScope!");
  8. }
  9. if (this !== self) {
  10. throw new Error("'self' not equal to global object!");
  11. }
  12. if (!(self instanceof SharedWorkerGlobalScope)) {
  13. throw new Error("self not a SharedWorkerGlobalScope instance!");
  14. }
  15. var propsToCheck = [
  16. "location",
  17. "navigator",
  18. "close",
  19. "importScripts",
  20. "setTimeout",
  21. "clearTimeout",
  22. "setInterval",
  23. "clearInterval",
  24. "dump",
  25. "atob",
  26. "btoa"
  27. ];
  28. for (var index = 0; index < propsToCheck.length; index++) {
  29. var prop = propsToCheck[index];
  30. if (!(prop in self)) {
  31. throw new Error("SharedWorkerGlobalScope has no '" + prop + "' property!");
  32. }
  33. }
  34. onconnect = function(event) {
  35. if (!("SharedWorkerGlobalScope" in self)) {
  36. throw new Error("SharedWorkerGlobalScope should be visible!");
  37. }
  38. if (!(self instanceof SharedWorkerGlobalScope)) {
  39. throw new Error("The global should be a SharedWorkerGlobalScope!");
  40. }
  41. if (!(self instanceof WorkerGlobalScope)) {
  42. throw new Error("The global should be a WorkerGlobalScope!");
  43. }
  44. if ("DedicatedWorkerGlobalScope" in self) {
  45. throw new Error("DedicatedWorkerGlobalScope should not be visible!");
  46. }
  47. if (!(event instanceof MessageEvent)) {
  48. throw new Error("'connect' event is not a MessageEvent!");
  49. }
  50. if (!("ports" in event)) {
  51. throw new Error("'connect' event doesn't have a 'ports' property!");
  52. }
  53. if (event.ports.length != 1) {
  54. throw new Error("'connect' event has a 'ports' property with length '" +
  55. event.ports.length + "'!");
  56. }
  57. if (!event.ports[0]) {
  58. throw new Error("'connect' event has a null 'ports[0]' property!");
  59. }
  60. if (!(event.ports[0] instanceof MessagePort)) {
  61. throw new Error("'connect' event has a 'ports[0]' property that isn't a " +
  62. "MessagePort!");
  63. }
  64. if (!(event.ports[0] == event.source)) {
  65. throw new Error("'connect' event source property is incorrect!");
  66. }
  67. if (event.data) {
  68. throw new Error("'connect' event has data: " + event.data);
  69. }
  70. // The expression closures should trigger a warning in debug builds, but NOT
  71. // fire error events at us. If we ever actually remove expression closures
  72. // (in bug 1083458), we'll need something else to test this case.
  73. (function() "Expected console warning: expression closures are deprecated");
  74. event.ports[0].onmessage = function(event) {
  75. if (!(event instanceof MessageEvent)) {
  76. throw new Error("'message' event is not a MessageEvent!");
  77. }
  78. if (!("ports" in event)) {
  79. throw new Error("'message' event doesn't have a 'ports' property!");
  80. }
  81. if (event.ports === null) {
  82. throw new Error("'message' event has a null 'ports' property!");
  83. }
  84. event.target.postMessage(event.data);
  85. throw new Error(event.data);
  86. };
  87. };