Files
tubestation/dom/ipc/UniqueContentParentKeepAlive.cpp
Nika Layzell 279b50ebe8 Bug 1728331 - Part 4: Make ContentParent KeepAlives explicit with RAII references, r=smaug,dom-worker-reviewers,asuth
This is a fairly significant patch, however it would be difficult to break it
down into smaller patches:

1) The various mechanisms used to manage ContentParent lifecycles have been
   merged together into a common "KeepAlive" system. A process will
   begin shutdown when its keepalive count reaches 0. (though it will
   still wait for all BrowserParents to also be dead before sending the
   actual shutdown message as before).

   This replaces a number of bespoke systems for tracking BrowserParent
   instances in different lifecycle states, remote workers, ongoing
   process switches, and preallocated processes.

2) KeepAlives are now managed automatically by a UniquePtr variant
   (Unique[Threadsafe]ContentParentKeepAlive). This makes the hand-off
   over KeepAlive lifecycles explicit, even for workers.

3) All KeepAlives are now keyed by a BrowserId, which will be 0 for keepalives
   not associated with a specific tab. This allows the new process
   selection logic to count all tabs other than the one being navigated
   when deciding which process to use.

4) The process switching logic now tracks it's KeepAlive with a BrowserId,
   meaning that ongoing process switches are considered when performing
   process selection, even if the BrowserParent hasn't been created yet.

Differential Revision: https://phabricator.services.mozilla.com/D213338
2024-06-24 23:19:28 +00:00

61 lines
2.3 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/dom/UniqueContentParentKeepAlive.h"
#include "mozilla/dom/ContentParent.h"
namespace mozilla::dom {
void ContentParentKeepAliveDeleter::operator()(ContentParent* aProcess) {
AssertIsOnMainThread();
if (RefPtr<ContentParent> process = dont_AddRef(aProcess)) {
process->RemoveKeepAlive(mBrowserId);
}
}
void ContentParentKeepAliveDeleter::operator()(
ThreadsafeContentParentHandle* aHandle) {
if (RefPtr<ThreadsafeContentParentHandle> handle = dont_AddRef(aHandle)) {
NS_DispatchToMainThread(NS_NewRunnableFunction(
"ThreadsafeContentParentKeepAliveDeleter",
[handle = std::move(handle), browserId = mBrowserId]() {
AssertIsOnMainThread();
if (RefPtr<ContentParent> process = handle->GetContentParent()) {
process->RemoveKeepAlive(browserId);
}
}));
}
}
UniqueContentParentKeepAlive UniqueContentParentKeepAliveFromThreadsafe(
UniqueThreadsafeContentParentKeepAlive aKeepAlive) {
AssertIsOnMainThread();
if (aKeepAlive) {
uint64_t browserId = aKeepAlive.get_deleter().mBrowserId;
RefPtr<ThreadsafeContentParentHandle> handle =
dont_AddRef(aKeepAlive.release());
RefPtr<ContentParent> process = handle->GetContentParent();
return UniqueContentParentKeepAlive{process.forget().take(),
{.mBrowserId = browserId}};
}
return nullptr;
}
UniqueThreadsafeContentParentKeepAlive UniqueContentParentKeepAliveToThreadsafe(
UniqueContentParentKeepAlive aKeepAlive) {
AssertIsOnMainThread();
if (aKeepAlive) {
uint64_t browserId = aKeepAlive.get_deleter().mBrowserId;
RefPtr<ContentParent> process = dont_AddRef(aKeepAlive.release());
RefPtr<ThreadsafeContentParentHandle> handle = process->ThreadsafeHandle();
return UniqueThreadsafeContentParentKeepAlive{handle.forget().take(),
{.mBrowserId = browserId}};
}
return nullptr;
}
} // namespace mozilla::dom