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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
/* 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";
function ChromeTask_ChromeScript() {
/* eslint-env mozilla/chrome-script */
"use strict";
const { Assert: AssertCls } = ChromeUtils.importESModule(
"resource://testing-common/Assert.sys.mjs"
);
addMessageListener("chrome-task:spawn", async function (aData) {
let id = aData.id;
let source = aData.runnable || "()=>{}";
function getStack(aStack) {
let frames = [];
for (let frame = aStack; frame; frame = frame.caller) {
frames.push(frame.filename + ":" + frame.name + ":" + frame.lineNumber);
}
return frames.join("\n");
}
/* eslint-disable no-unused-vars */
var Assert = new AssertCls((err, message, stack) => {
sendAsyncMessage("chrome-task:test-result", {
id,
condition: !err,
name: err ? err.message : message,
stack: getStack(err ? err.stack : stack),
});
});
var ok = Assert.ok.bind(Assert);
var is = Assert.equal.bind(Assert);
var isnot = Assert.notEqual.bind(Assert);
function todo(expr, name) {
sendAsyncMessage("chrome-task:test-todo", { id, expr, name });
}
function todo_is(a, b, name) {
sendAsyncMessage("chrome-task:test-todo_is", { id, a, b, name });
}
function info(name) {
sendAsyncMessage("chrome-task:test-info", { id, name });
}
/* eslint-enable no-unused-vars */
try {
let runnablestr = `
(() => {
return (${source});
})();`;
// eslint-disable-next-line no-eval
let runnable = eval(runnablestr);
let result = await runnable.call(this, aData.arg);
sendAsyncMessage("chrome-task:complete", {
id,
result,
});
} catch (ex) {
sendAsyncMessage("chrome-task:complete", {
id,
error: ex.toString(),
});
}
});
}
/**
* This object provides the public module functions.
*/
var ChromeTask = {
/**
* the ChromeScript if it has already been loaded.
*/
_chromeScript: null,
/**
* Mapping from message id to associated promise.
*/
_promises: new Map(),
/**
* Incrementing integer to generate unique message id.
*/
_messageID: 1,
/**
* Creates and starts a new task in the chrome process.
*
* @param arg A single serializable argument that will be passed to the
* task when executed on the content process.
* @param task
* - A generator or function which will be serialized and sent to
* the remote browser to be executed. Unlike Task.spawn, this
* argument may not be an iterator as it will be serialized and
* sent to the remote browser.
* @return A promise object where you can register completion callbacks to be
* called when the task terminates.
* @resolves With the final returned value of the task if it executes
* successfully.
* @rejects An error message if execution fails.
*/
spawn: function ChromeTask_spawn(arg, task) {
// Load the frame script if needed.
let handle = ChromeTask._chromeScript;
if (!handle) {
handle = SpecialPowers.loadChromeScript(ChromeTask_ChromeScript);
handle.addMessageListener("chrome-task:complete", ChromeTask.onComplete);
handle.addMessageListener("chrome-task:test-result", ChromeTask.onResult);
handle.addMessageListener("chrome-task:test-info", ChromeTask.onInfo);
handle.addMessageListener("chrome-task:test-todo", ChromeTask.onTodo);
handle.addMessageListener(
"chrome-task:test-todo_is",
ChromeTask.onTodoIs
);
ChromeTask._chromeScript = handle;
}
let deferred = {};
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});
let id = ChromeTask._messageID++;
ChromeTask._promises.set(id, deferred);
handle.sendAsyncMessage("chrome-task:spawn", {
id,
runnable: task.toString(),
arg,
});
return deferred.promise;
},
onComplete(aData) {
let deferred = ChromeTask._promises.get(aData.id);
ChromeTask._promises.delete(aData.id);
if (aData.error) {
deferred.reject(aData.error);
} else {
deferred.resolve(aData.result);
}
},
onResult(aData) {
SimpleTest.record(aData.condition, aData.name);
},
onInfo(aData) {
SimpleTest.info(aData.name);
},
onTodo(aData) {
SimpleTest.todo(aData.expr, aData.name);
},
onTodoIs(aData) {
SimpleTest.todo_is(aData.a, aData.b, aData.name);
},
};
|