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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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", []);
}
|