LibWeb: Disentangle associated MessagePorts when a document is destroyed

This commit is contained in:
Timothy Flynn 2024-10-17 07:59:04 -04:00 committed by Tim Flynn
parent d44dd756b3
commit ba1b26cdc2
Notes: github-actions[bot] 2024-10-17 20:35:40 +00:00
3 changed files with 30 additions and 3 deletions

View file

@ -29,6 +29,12 @@ constexpr u8 IPC_FILE_TAG = 0xA5;
JS_DEFINE_ALLOCATOR(MessagePort);
static HashTable<JS::RawGCPtr<MessagePort>>& all_message_ports()
{
static HashTable<JS::RawGCPtr<MessagePort>> ports;
return ports;
}
JS::NonnullGCPtr<MessagePort> MessagePort::create(JS::Realm& realm)
{
return realm.heap().allocate<MessagePort>(realm, realm);
@ -37,13 +43,21 @@ JS::NonnullGCPtr<MessagePort> MessagePort::create(JS::Realm& realm)
MessagePort::MessagePort(JS::Realm& realm)
: DOM::EventTarget(realm)
{
all_message_ports().set(this);
}
MessagePort::~MessagePort()
{
all_message_ports().remove(this);
disentangle();
}
void MessagePort::for_each_message_port(Function<void(MessagePort&)> callback)
{
for (auto port : all_message_ports())
callback(*port);
}
void MessagePort::initialize(JS::Realm& realm)
{
Base::initialize(realm);