Bug 1932730 - Pass HybridEventTarget for endpoint binding r=asuth

Doing so registers a shutdown task to workerprivate and prevents CC.

Differential Revision: https://phabricator.services.mozilla.com/D229822
This commit is contained in:
Kagami Sascha Rosylight 2024-11-22 16:09:00 +00:00
parent ea38eaf128
commit ef42dea31f
2 changed files with 40 additions and 3 deletions

View File

@ -1151,16 +1151,15 @@ bool Notification::CreateActor() {
mActor = new notification::NotificationChild(
persistent ? nullptr : this,
window ? window->GetWindowGlobalChild() : nullptr);
if (!childEndpoint.Bind(mActor)) {
return false;
}
nsISerialEventTarget* target = nullptr;
nsIPrincipal* principal;
nsIPrincipal* effectiveStoragePrincipal;
bool isSecureContext;
// TODO: Should get nsIGlobalObject methods for each method
if (WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate()) {
target = workerPrivate->HybridEventTarget();
principal = workerPrivate->GetPrincipal();
effectiveStoragePrincipal = workerPrivate->GetEffectiveStoragePrincipal();
isSecureContext = workerPrivate->IsSecureContext();
@ -1172,6 +1171,10 @@ bool Notification::CreateActor() {
isSecureContext = win->IsSecureContext();
}
if (!childEndpoint.Bind(mActor, target)) {
return false;
}
(void)backgroundActor->SendCreateNotificationParent(
std::move(parentEndpoint), WrapNotNull(principal),
WrapNotNull(effectiveStoragePrincipal), isSecureContext, mID, mScope,

View File

@ -0,0 +1,34 @@
// META: title=An active notification should prevent worker cycle collection
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/common/gc.js
// META: script=resources/helpers.js
promise_setup(async () => {
await trySettingPermission("granted");
});
promise_test(async t => {
let worker = new Worker(URL.createObjectURL(new Blob([`
const n = new Notification("foo");
onmessage = () => n.close();
n.onclose = () => self.postMessage("closed");
postMessage("ready");
`])));
await new Promise(resolve => {
worker.addEventListener("message", ev => {
if (ev.data === "ready") {
resolve();
}
}, { once: true });
});
const weakref = new WeakRef(worker);
worker = null;
t.add_cleanup(() => {
weakref.deref()?.postMessage("close");
});
await garbageCollect();
assert_true(!!weakref.deref());
});