Bug 1640245, remove frame script and message manager from workers tests, r=baku
authorNeil Deakin <neil@mozilla.com>
Sat, 23 May 2020 03:20:07 +0000
changeset 531807 329c282c8bff82600c9714f164ebefc871f4645e
parent 531806 0d1da700b7da419d59b60374213f45d8a83c426a
child 531808 588155fb515a1e3e862fa3eade684e470fa335ca
push id37445
push userccoroiu@mozilla.com
push dateSun, 24 May 2020 21:20:21 +0000
treeherdermozilla-central@3334d8dff757 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1640245
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1640245, remove frame script and message manager from workers tests, r=baku Differential Revision: https://phabricator.services.mozilla.com/D76518
dom/workers/test/browser.ini
dom/workers/test/frame_script.js
dom/workers/test/head.js
--- a/dom/workers/test/browser.ini
+++ b/dom/workers/test/browser.ini
@@ -1,13 +1,12 @@
 [DEFAULT]
 support-files =
   bug1047663_tab.html
   bug1047663_worker.sjs
-  frame_script.js
   head.js
   !/dom/base/test/file_empty.html
 
 [browser_bug1047663.js]
 [browser_bug1104623.js]
 run-if = buildapp == 'browser'
 [browser_consoleSharedWorkers.js]
 support-files = sharedWorker_console.js empty.html
deleted file mode 100644
--- a/dom/workers/test/frame_script.js
+++ /dev/null
@@ -1,73 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-"use strict";
-
-let workers = {};
-
-let methods = {
-  /**
-   * Create a worker with the given `url` in this tab.
-   */
-  createWorker(url) {
-    dump("Frame script: creating worker with url '" + url + "'\n");
-
-    workers[url] = new content.Worker(url);
-    return Promise.resolve();
-  },
-
-  /**
-   * Terminate the worker with the given `url` in this tab.
-   */
-  terminateWorker(url) {
-    dump("Frame script: terminating worker with url '" + url + "'\n");
-
-    workers[url].terminate();
-    delete workers[url];
-    return Promise.resolve();
-  },
-
-  /**
-   * Post the given `message` to the worker with the given `url` in this tab.
-   */
-  postMessageToWorker(url, message) {
-    dump("Frame script: posting message to worker with url '" + url + "'\n");
-
-    let worker = workers[url];
-    worker.postMessage(message);
-    return new Promise(function(resolve) {
-      worker.onmessage = function(event) {
-        worker.onmessage = null;
-        resolve(event.data);
-      };
-    });
-  },
-
-  /**
-   * Disable the cache for this tab.
-   */
-  disableCache() {
-    docShell.defaultLoadFlags =
-      Ci.nsIRequest.LOAD_BYPASS_CACHE | Ci.nsIRequest.INHIBIT_CACHING;
-  },
-};
-
-addMessageListener("jsonrpc", function(event) {
-  let { id, method, params } = event.data;
-  Promise.resolve()
-    .then(function() {
-      return methods[method].apply(undefined, params);
-    })
-    .then(function(result) {
-      sendAsyncMessage("jsonrpc", {
-        id,
-        result,
-      });
-    })
-    .catch(function(error) {
-      sendAsyncMessage("jsonrpc", {
-        id,
-        error: error.toString(),
-      });
-    });
-});
--- a/dom/workers/test/head.js
+++ b/dom/workers/test/head.js
@@ -1,86 +1,77 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 const EXAMPLE_URL = "http://example.com/browser/dom/workers/test/";
-const FRAME_SCRIPT_URL = getRootDirectory(gTestPath) + "frame_script.js";
 
 /**
- * Add a tab with given `url`, and load a frame script in it. Returns a promise
+ * Add a tab with given `url`. Returns a promise
  * that will be resolved when the tab finished loading.
  */
 function addTab(url) {
-  let tab = BrowserTestUtils.addTab(gBrowser, TAB_URL);
-  gBrowser.selectedTab = tab;
-  let linkedBrowser = tab.linkedBrowser;
-  linkedBrowser.messageManager.loadFrameScript(FRAME_SCRIPT_URL, false);
-  return BrowserTestUtils.browserLoaded(linkedBrowser).then(() => tab);
+  return BrowserTestUtils.openNewForegroundTab(gBrowser, TAB_URL);
 }
 
 /**
  * Remove the given `tab`.
  */
 function removeTab(tab) {
   gBrowser.removeTab(tab);
 }
 
-let nextId = 0;
-
-/**
- * Send a JSON RPC request to the frame script in the given `tab`, invoking the
- * given `method` with the given `params`. Returns a promise that will be
- * resolved with the result of the invocation.
- */
-function jsonrpc(tab, method, params) {
-  let currentId = nextId++;
-  let messageManager = tab.linkedBrowser.messageManager;
-  messageManager.sendAsyncMessage("jsonrpc", {
-    id: currentId,
-    method,
-    params,
-  });
-  return new Promise(function(resolve, reject) {
-    messageManager.addMessageListener("jsonrpc", function listener(event) {
-      let { id, result, error } = event.data;
-      if (id !== currentId) {
-        return;
-      }
-      messageManager.removeMessageListener("jsonrpc", listener);
-      if (error) {
-        reject(error);
-        return;
-      }
-      resolve(result);
-    });
-  });
-}
-
 /**
  * Create a worker with the given `url` in the given `tab`.
  */
 function createWorkerInTab(tab, url) {
-  return jsonrpc(tab, "createWorker", [url]);
+  info("Creating worker with url '" + url + "'\n");
+  return SpecialPowers.spawn(tab.linkedBrowser, [url], urlChild => {
+    if (!content._workers) {
+      content._workers = {};
+    }
+    content._workers[urlChild] = new content.Worker(urlChild);
+  });
 }
 
 /**
  * Terminate the worker with the given `url` in the given `tab`.
  */
 function terminateWorkerInTab(tab, url) {
-  return jsonrpc(tab, "terminateWorker", [url]);
+  info("Terminating worker with url '" + url + "'\n");
+  return SpecialPowers.spawn(tab.linkedBrowser, [url], urlChild => {
+    content._workers[urlChild].terminate();
+    delete content._workers[urlChild];
+  });
 }
 
 /**
  * Post the given `message` to the worker with the given `url` in the given
  * `tab`.
  */
 function postMessageToWorkerInTab(tab, url, message) {
-  return jsonrpc(tab, "postMessageToWorker", [url, message]);
+  info("Posting message to worker with url '" + url + "'\n");
+  return SpecialPowers.spawn(
+    tab.linkedBrowser,
+    [url, message],
+    (urlChild, messageChild) => {
+      let worker = content._workers[urlChild];
+      worker.postMessage(messageChild);
+      return new Promise(function(resolve) {
+        worker.onmessage = function(event) {
+          worker.onmessage = null;
+          resolve(event.data);
+        };
+      });
+    }
+  );
 }
 
 /**
  * Disable the cache in the given `tab`.
  */
 function disableCacheInTab(tab) {
-  return jsonrpc(tab, "disableCache", []);
+  return SpecialPowers.spawn(tab.linkedBrowser, [], () => {
+    content.docShell.defaultLoadFlags =
+      Ci.nsIRequest.LOAD_BYPASS_CACHE | Ci.nsIRequest.INHIBIT_CACHING;
+  });
 }