1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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()
});
});
});
|