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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1828264
-->
<head>
<title>Basic structured clone tests</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1828264">Mozilla Bug 1828264</a>
<p id="display">
<iframe id="frame"></iframe>
<image href="four-colors.png" height="320" width="240"/>
</p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
class WebIDLGlobal {
globalNames;
resolve;
#otherSide;
constructor(globalNames, initSides) {
this.globalNames = globalNames;
let global = this;
this.#otherSide = initSides().then(([ourSide, otherSide, cleanup]) => {
if (cleanup) {
SimpleTest.registerCleanupFunction(cleanup);
}
ourSide.addEventListener("message", ({ data }) => {
global.resultReceived(data);
});
return otherSide;
});
}
get otherSide() {
return this.#otherSide;
}
waitForResult() {
ok(!this.resolve, "Still waiting on previous message?");
return new Promise(resolve => {
this.resolve = resolve;
});
}
resultReceived(value) {
this.resolve(value);
this.resolve = undefined;
}
}
// Init functions return an array containing in order the object to use for
// sending a messages to the other global, the object to use to listen for
// messages from the other global and optioanlly a cleanup function.
function waitForInitMessage(target) {
return new Promise(resolve => {
target.addEventListener("message", resolve, { once: true });
});
}
function getModuleURL(additionalScript = "") {
return new URL(
`file_structuredCloneAndExposed.sjs?additionalScript=${encodeURIComponent(
additionalScript
)}`,
location.href
);
}
function initFrame() {
let callInstallListeners = `
installListeners(globalThis, parent);
`;
frame.srcdoc =
`<script src="${getModuleURL(callInstallListeners)}" type="module"></` +
`script>`;
return waitForInitMessage(self).then(() => [self, frame.contentWindow]);
}
function initDedicatedWorker() {
let callInstallListeners = `
installListeners(globalThis, globalThis);
`;
const worker = new Worker(getModuleURL(callInstallListeners), {
type: "module",
});
return waitForInitMessage(worker).then(() => [worker, worker]);
}
function initSharedWorker() {
let callInstallListeners = `
self.addEventListener("connect", (e) => {
const port = e.ports[0];
port.start();
installListeners(port, port);
});
`;
const worker = new SharedWorker(getModuleURL(callInstallListeners), {
type: "module",
});
worker.port.start();
return waitForInitMessage(worker.port).then(() => [worker.port, worker.port]);
}
function initServiceWorker() {
let callInstallListeners = `
addEventListener("message", ({ source: client }) => {
installListeners(globalThis, client);
}, { once: true });
`;
return navigator.serviceWorker
.register(getModuleURL(callInstallListeners), { type: "module" })
.then(registration => {
let worker =
registration.installing || registration.waiting || registration.active;
worker.postMessage("installListeners");
return waitForInitMessage(navigator.serviceWorker).then(() => [
navigator.serviceWorker,
worker,
() => registration.unregister(),
]);
});
}
function initAudioWorklet() {
const exporter = `
export { installListeners };
`;
const workletSource = `
import { installListeners } from "${getModuleURL(exporter)}";
class CustomAudioWorkletProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.port.start();
installListeners(this.port, this.port, "AudioWorklet");
}
process(inputs, outputs, params) {
// Do nothing, output silence
}
}
registerProcessor("custom-audio-worklet-processor", CustomAudioWorkletProcessor);
`;
let workletURL = URL.createObjectURL(
new Blob([workletSource], { type: "text/javascript" })
);
// We need to keep the context alive while we're using the message ports.
globalThis.context = new OfflineAudioContext(1, 64, 8000);
return context.audioWorklet.addModule(workletURL).then(() => {
let node = new AudioWorkletNode(context, "custom-audio-worklet-processor");
node.port.start();
return waitForInitMessage(node.port).then(() => [
node.port,
node.port,
() => {
node.port.close();
delete globalThis.context;
},
]);
});
}
const globals = [
["Window", ["Window"], initFrame],
[
"DedicatedWorkerGlobalScope",
["Worker", "DedicatedWorker"],
initDedicatedWorker,
],
["SharedWorkerGlobalScope", ["Worker", "SharedWorker"], initSharedWorker],
["ServiceWorkerGlobalScope", ["Worker", "ServiceWorker"], initServiceWorker],
// WorkerDebuggerGlobalScope can only receive string messages.
["AudioWorkletGlobalScope", ["Worklet", "AudioWorklet"], initAudioWorklet],
// PaintWorkletGlobalScope doesn't have a messaging mechanism
].map(([global, globalNames, init]) => [
global,
new WebIDLGlobal(globalNames, init),
]);
function generateCertificate() {
return RTCPeerConnection.generateCertificate({
name: "ECDSA",
namedCurve: "P-256",
});
}
function makeCanvas() {
const width = 20;
const height = 20;
let canvas = new OffscreenCanvas(width, height);
let ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(50, 100, 150, 255)";
ctx.fillRect(0, 0, width, height);
return canvas;
}
function makeVideoFrame() {
return new VideoFrame(makeCanvas(), { timestamp: 1, alpha: "discard" });
}
function makeImageBitmap() {
return makeCanvas().transferToImageBitmap();
}
const serializable = [
["DOMException", ["Window", "Worker"], () => new DOMException()],
["DOMMatrixReadOnly", ["Window", "Worker"], () => new DOMMatrixReadOnly()],
["ImageBitmap", ["Window", "Worker"], makeImageBitmap],
["RTCCertificate", ["Window"], generateCertificate],
["VideoFrame", ["Window", "DedicatedWorker"], makeVideoFrame],
];
const transferable = [
[
"MessagePort",
["Window", "Worker", "AudioWorklet"],
() => new MessageChannel().port1,
],
["ImageBitmap", ["Window", "Worker"], makeImageBitmap],
["ReadableStream", ["*"], () => new ReadableStream()],
["VideoFrame", ["Window", "DedicatedWorker"], makeVideoFrame],
];
function isExposed(exposure, global) {
if (exposure.length === 1 && exposure[0] === "*") {
return true;
}
return !!global.globalNames.filter(v => exposure.includes(v)).length;
}
async function runTest() {
await SpecialPowers.pushPrefEnv({ set: [["dom.media.webcodecs.enabled", true]] });
async function testDOMClass(domClass, exposure, createObject, transferable) {
for ([globalName, webidlGlobal] of globals) {
info(
`Testing ${
transferable ? "transfer" : "serialization"
} of ${domClass} with ${globalName}`
);
let object = await createObject();
let otherSide = await webidlGlobal.otherSide;
let options = { targetOrigin: "*" };
let message;
if (transferable) {
options.transfer = [object];
} else {
message = object;
}
otherSide.postMessage(message, options);
let result = await webidlGlobal.waitForResult();
let expected = isExposed(exposure, webidlGlobal);
if (
domClass === "ImageBitmap" &&
globalName === "ServiceWorkerGlobalScope"
) {
// Service workers don't support transferring shared memory objects
// (see ServiceWorker::PostMessage).
expected = false;
}
is(
result,
expected,
`Deserialization for ${domClass} in ${globalName} ${
expected ? "should" : "shouldn't"
} succeed`
);
}
}
for ([domClass, exposure, createObject] of serializable) {
await testDOMClass(domClass, exposure, createObject, false);
}
for ([domClass, exposure, createObject] of transferable) {
await testDOMClass(domClass, exposure, createObject, true);
}
SimpleTest.finish();
}
runTest();
</script>
</pre>
</body>
</html>
|