From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- dom/workers/test/head.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 dom/workers/test/head.js (limited to 'dom/workers/test/head.js') diff --git a/dom/workers/test/head.js b/dom/workers/test/head.js new file mode 100644 index 000000000..5f0c5c26e --- /dev/null +++ b/dom/workers/test/head.js @@ -0,0 +1,91 @@ +/* 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 + * that will be resolved when the tab finished loading. + */ +function addTab(url) { + let tab = gBrowser.addTab(TAB_URL); + gBrowser.selectedTab = tab; + let linkedBrowser = tab.linkedBrowser; + linkedBrowser.messageManager.loadFrameScript(FRAME_SCRIPT_URL, false); + return new Promise(function (resolve) { + linkedBrowser.addEventListener("load", function onload() { + linkedBrowser.removeEventListener("load", onload, true); + resolve(tab); + }, true); + }); +} + +/** + * 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: method, + params: 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]); +} + +/** + * Terminate the worker with the given `url` in the given `tab`. + */ +function terminateWorkerInTab(tab, url) { + return jsonrpc(tab, "terminateWorker", [url]); +} + +/** + * 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]); +} + +/** + * Disable the cache in the given `tab`. + */ +function disableCacheInTab(tab) { + return jsonrpc(tab, "disableCache", []); +} -- cgit v1.2.3