summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/frame_script.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /dom/workers/test/frame_script.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'dom/workers/test/frame_script.js')
-rw-r--r--dom/workers/test/frame_script.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/dom/workers/test/frame_script.js b/dom/workers/test/frame_script.js
new file mode 100644
index 000000000..ffc384416
--- /dev/null
+++ b/dom/workers/test/frame_script.js
@@ -0,0 +1,72 @@
+/* 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 { interfaces: Ci } = Components;
+
+let workers = {};
+
+let methods = {
+ /**
+ * Create a worker with the given `url` in this tab.
+ */
+ createWorker: function (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: function (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: function (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: function () {
+ 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: id,
+ result: result
+ });
+ }).catch(function (error) {
+ sendAsyncMessage("jsonrpc", {
+ id: id,
+ error: error.toString()
+ });
+ });
+});