Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 4839800

Browse files
committed
Bug 1725942 - Part 4: Add StrongWorkerRef r=smaug
Differential Revision: https://phabricator.services.mozilla.com/D127514
1 parent b393a76 commit 4839800

9 files changed

Lines changed: 79 additions & 1 deletion

dom/locks/LockManager.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66

77
#include "mozilla/dom/LockManager.h"
8+
#include "mozilla/dom/WorkerCommon.h"
89
#include "mozilla/dom/locks/LockManagerChild.h"
910
#include "mozilla/dom/locks/LockRequestChild.h"
1011
#include "mozilla/Assertions.h"
@@ -51,6 +52,18 @@ LockManager::LockManager(nsIGlobalObject* aGlobal) : mOwner(aGlobal) {
5152
mActor = new locks::LockManagerChild(aGlobal);
5253
backgroundActor->SendPLockManagerConstructor(mActor, principalInfo,
5354
clientInfo->Id());
55+
56+
if (!NS_IsMainThread()) {
57+
mWorkerRef = WeakWorkerRef::Create(GetCurrentThreadWorkerPrivate(),
58+
[self = RefPtr(this)]() {
59+
// Others may grab a strong reference
60+
// and block immediate destruction.
61+
// Shutdown early as we don't have to
62+
// wait for them.
63+
self->Shutdown();
64+
self->mWorkerRef = nullptr;
65+
});
66+
}
5467
}
5568

5669
static bool ValidateRequestArguments(const nsAString& name,

dom/locks/LockManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "mozilla/dom/BindingDeclarations.h"
1414
#include "mozilla/dom/Lock.h"
1515
#include "mozilla/dom/LockManagerBinding.h"
16+
#include "mozilla/dom/WorkerRef.h"
1617
#include "nsCycleCollectionParticipant.h"
1718
#include "nsHashKeys.h"
1819
#include "nsTHashMap.h"
@@ -59,6 +60,8 @@ class LockManager final : public nsISupports, public nsWrapperCache {
5960

6061
nsCOMPtr<nsIGlobalObject> mOwner;
6162
RefPtr<locks::LockManagerChild> mActor;
63+
64+
RefPtr<WeakWorkerRef> mWorkerRef;
6265
};
6366

6467
} // namespace mozilla::dom

dom/locks/LockRequestChild.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "LockRequestChild.h"
99

1010
#include "mozilla/dom/Promise.h"
11+
#include "mozilla/dom/WorkerPrivate.h"
1112

1213
namespace mozilla::dom::locks {
1314

@@ -36,6 +37,15 @@ MOZ_CAN_RUN_SCRIPT_BOUNDARY static void RunCallbackAndSettlePromise(
3637
MOZ_ASSERT(!rv.Failed());
3738
}
3839

40+
LockRequestChild::LockRequestChild(const LockRequest& aRequest)
41+
: mRequest(aRequest) {
42+
if (!NS_IsMainThread()) {
43+
mWorkerRef = StrongWorkerRef::Create(
44+
GetCurrentThreadWorkerPrivate(), "LockManager",
45+
[self = RefPtr(this)]() { self->mWorkerRef = nullptr; });
46+
}
47+
}
48+
3949
IPCResult LockRequestChild::RecvResolve(const LockMode& aLockMode,
4050
bool aIsAvailable) {
4151
RefPtr<Lock> lock;

dom/locks/LockRequestChild.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "mozilla/dom/locks/PLockRequestChild.h"
1111
#include "mozilla/dom/Lock.h"
12+
#include "mozilla/dom/WorkerRef.h"
1213

1314
namespace mozilla::dom::locks {
1415

@@ -25,7 +26,7 @@ class LockRequestChild final : public PLockRequestChild,
2526
NS_INLINE_DECL_REFCOUNTING(LockRequestChild)
2627

2728
public:
28-
explicit LockRequestChild(const LockRequest& aRequest) : mRequest(aRequest){};
29+
explicit LockRequestChild(const LockRequest& aRequest);
2930

3031
IPCResult RecvResolve(const LockMode& aLockMode, bool aIsAvailable);
3132
IPCResult RecvAbort();
@@ -34,6 +35,7 @@ class LockRequestChild final : public PLockRequestChild,
3435
~LockRequestChild() = default;
3536

3637
LockRequest mRequest;
38+
RefPtr<StrongWorkerRef> mWorkerRef;
3739
};
3840

3941
} // namespace mozilla::dom::locks

dom/locks/moz.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
with Files("**"):
88
BUG_COMPONENT = ("Core", "DOM: Core & HTML")
99

10+
MOCHITEST_MANIFESTS += ["test/mochitest.ini"]
11+
1012
EXPORTS.mozilla.dom += [
1113
"Lock.h",
1214
"LockManager.h",

dom/locks/test/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
module.exports = {
4+
extends: ["plugin:mozilla/mochitest-test"],
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
navigator.locks.request("exclusive", () => {
2+
const channel = new BroadcastChannel("strongworker");
3+
channel.postMessage("lock acquired");
4+
});
5+
postMessage("onload");

dom/locks/test/mochitest.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[DEFAULT]
2+
scheme = https
3+
prefs =
4+
dom.weblocks.enabled=true
5+
6+
[test_strongworker.html]
7+
support-files =
8+
file_strongworker.js
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<script src="/tests/SimpleTest/SimpleTest.js"></script>
4+
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
5+
6+
<script>
7+
SimpleTest.waitForExplicitFinish();
8+
9+
async function run() {
10+
const channel = new BroadcastChannel("strongworker");
11+
await navigator.locks.request("exclusive", async () => {
12+
await new Promise(resolve => {
13+
let worker = new Worker("./file_strongworker.js");
14+
worker.onmessage = resolve; // onload
15+
});
16+
const query = await navigator.locks.query();
17+
is(query.pending.length, 1, "Pending request exists");
18+
19+
// Garbage collect the worker
20+
SpecialPowers.DOMWindowUtils.garbageCollect();
21+
});
22+
23+
channel.onmessage = async event => {
24+
const query = await navigator.locks.query();
25+
is(query.pending.length, 0, "No pending request");
26+
SimpleTest.finish();
27+
};
28+
}
29+
run();
30+
</script>

0 commit comments

Comments
 (0)