worker_wrapper.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Any copyright is dedicated to the Public Domain.
  2. // http://creativecommons.org/publicdomain/zero/1.0/
  3. //
  4. // ServiceWorker equivalent of worker_wrapper.js.
  5. var client;
  6. var context;
  7. function ok(a, msg) {
  8. client.postMessage({type: 'status', status: !!a,
  9. msg: a + ": " + msg, context: context});
  10. }
  11. function is(a, b, msg) {
  12. client.postMessage({type: 'status', status: a === b,
  13. msg: a + " === " + b + ": " + msg, context: context });
  14. }
  15. function workerTestArrayEquals(a, b) {
  16. if (!Array.isArray(a) || !Array.isArray(b) || a.length != b.length) {
  17. return false;
  18. }
  19. for (var i = 0, n = a.length; i < n; ++i) {
  20. if (a[i] !== b[i]) {
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26. function testDone() {
  27. client.postMessage({ type: 'finish', context: context });
  28. }
  29. function workerTestGetPrefs(prefs, cb) {
  30. addEventListener('message', function workerTestGetPrefsCB(e) {
  31. if (e.data.type != 'returnPrefs' ||
  32. !workerTestArrayEquals(prefs, e.data.prefs)) {
  33. return;
  34. }
  35. removeEventListener('message', workerTestGetPrefsCB);
  36. cb(e.data.result);
  37. });
  38. client.postMessage({
  39. type: 'getPrefs',
  40. context: context,
  41. prefs: prefs
  42. });
  43. }
  44. function workerTestGetPermissions(permissions, cb) {
  45. addEventListener('message', function workerTestGetPermissionsCB(e) {
  46. if (e.data.type != 'returnPermissions' ||
  47. !workerTestArrayEquals(permissions, e.data.permissions)) {
  48. return;
  49. }
  50. removeEventListener('message', workerTestGetPermissionsCB);
  51. cb(e.data.result);
  52. });
  53. client.postMessage({
  54. type: 'getPermissions',
  55. context: context,
  56. permissions: permissions
  57. });
  58. }
  59. function workerTestGetVersion(cb) {
  60. addEventListener('message', function workerTestGetVersionCB(e) {
  61. if (e.data.type !== 'returnVersion') {
  62. return;
  63. }
  64. removeEventListener('message', workerTestGetVersionCB);
  65. cb(e.data.result);
  66. });
  67. client.postMessage({
  68. context: context,
  69. type: 'getVersion'
  70. });
  71. }
  72. function workerTestGetUserAgent(cb) {
  73. addEventListener('message', function workerTestGetUserAgentCB(e) {
  74. if (e.data.type !== 'returnUserAgent') {
  75. return;
  76. }
  77. removeEventListener('message', workerTestGetUserAgentCB);
  78. cb(e.data.result);
  79. });
  80. client.postMessage({
  81. context: context,
  82. type: 'getUserAgent'
  83. });
  84. }
  85. addEventListener('message', function workerWrapperOnMessage(e) {
  86. removeEventListener('message', workerWrapperOnMessage);
  87. var data = e.data;
  88. function runScript() {
  89. try {
  90. importScripts(data.script);
  91. } catch(e) {
  92. client.postMessage({
  93. type: 'status',
  94. status: false,
  95. context: context,
  96. msg: 'worker failed to import ' + data.script + "; error: " + e.message
  97. });
  98. }
  99. }
  100. if ("ServiceWorker" in self) {
  101. self.clients.matchAll().then(function(clients) {
  102. for (var i = 0; i < clients.length; ++i) {
  103. if (clients[i].url.indexOf("message_receiver.html") > -1) {
  104. client = clients[i];
  105. break;
  106. }
  107. }
  108. if (!client) {
  109. dump("We couldn't find the message_receiver window, the test will fail\n");
  110. }
  111. context = "ServiceWorker";
  112. runScript();
  113. });
  114. } else {
  115. client = self;
  116. context = "Worker";
  117. runScript();
  118. }
  119. });