Bug 1779792 - Part 3: Use an endpoint to bind the initial actor in parent processes, r=ipc-reviewers,necko-reviewers,media-playback-reviewers,alwu,mccr8

This improves consistency with the child process case, and will make it easier
to attach additional state without needing to thread it through every child
process callsite manually.

Differential Revision: https://phabricator.services.mozilla.com/D153619
This commit is contained in:
Nika Layzell 2022-08-10 14:55:22 +00:00
Родитель d45df271ec
Коммит 2ac29a461a
11 изменённых файлов: 24 добавлений и 27 удалений

Просмотреть файл

@ -2681,9 +2681,7 @@ bool ContentParent::LaunchSubprocessResolve(bool aIsSync,
sCreatedFirstContentProcess = true;
}
base::ProcessId procId =
base::GetProcId(mSubprocess->GetChildProcessHandle());
Open(mSubprocess->TakeInitialPort(), procId);
mSubprocess->TakeInitialEndpoint().Bind(this);
ContentProcessManager* cpm = ContentProcessManager::GetSingleton();
if (!cpm) {

Просмотреть файл

@ -326,8 +326,7 @@ nsresult GMPParent::LoadProcess() {
mChildPid = base::GetProcId(mProcess->GetChildProcessHandle());
GMP_PARENT_LOG_DEBUG("%s: Launched new child process", __FUNCTION__);
bool opened = Open(mProcess->TakeInitialPort(),
base::GetProcId(mProcess->GetChildProcessHandle()));
bool opened = mProcess->TakeInitialEndpoint().Bind(this);
if (!opened) {
GMP_PARENT_LOG_DEBUG("%s: Failed to open channel to new child process",
__FUNCTION__);

Просмотреть файл

@ -169,8 +169,7 @@ void RDDProcessHost::InitAfterConnect(bool aSucceeded) {
}
mProcessToken = ++sRDDProcessTokenCounter;
mRDDChild = MakeUnique<RDDChild>(this);
DebugOnly<bool> rv = mRDDChild->Open(
TakeInitialPort(), base::GetProcId(GetChildProcessHandle()));
DebugOnly<bool> rv = TakeInitialEndpoint().Bind(mRDDChild.get());
MOZ_ASSERT(rv);
// Only clear mPrefSerializer in the success case to avoid a

Просмотреть файл

@ -144,8 +144,7 @@ void GPUProcessHost::InitAfterConnect(bool aSucceeded) {
if (aSucceeded) {
mProcessToken = ++sProcessTokenCounter;
mGPUChild = MakeUnique<GPUChild>(this);
DebugOnly<bool> rv = mGPUChild->Open(
TakeInitialPort(), base::GetProcId(GetChildProcessHandle()));
DebugOnly<bool> rv = TakeInitialEndpoint().Bind(mGPUChild.get());
MOZ_ASSERT(rv);
mGPUChild->Init();

Просмотреть файл

@ -163,8 +163,7 @@ bool VRProcessParent::InitAfterConnect(bool aSucceeded) {
mVRChild = MakeUnique<VRChild>(this);
DebugOnly<bool> rv = mVRChild->Open(
TakeInitialPort(), base::GetProcId(GetChildProcessHandle()));
DebugOnly<bool> rv = TakeInitialEndpoint().Bind(mVRChild.get());
MOZ_ASSERT(rv);
mVRChild->Init();

Просмотреть файл

@ -560,7 +560,8 @@ behavior is fairly clear:
return;
}
new DemoParent(std::move(host));
auto actor = MakeRefPtr<DemoParent>(std::move(host));
actor->Init();
});
}
@ -579,20 +580,21 @@ the new host, if successful.
In this sample, the ``DemoParent`` is owned (in the reference-counting sense)
by IPDL, which is why it doesn't get assigned to anything. This simplifies the
design dramatically. IPDL takes ownership when the actor calls ``Open`` in its
constructor:
design dramatically. IPDL takes ownership when the actor calls ``Bind`` from
the ``Init`` method:
.. code-block:: c++
DemoParent::DemoParent(UniqueHost&& aHost)
: mHost(std::move(aHost)) {
Open(mHost->TakeInitialPort(),
base::GetProcId(mHost->GetChildProcessHandle()));
: mHost(std::move(aHost)) {}
DemoParent::Init() {
mHost->TakeInitialEndpoint().Bind(this);
// ...
mHost->MakeBridgeAndResolve();
}
After the ``Open`` call, the actor is live and communication with the new
After the ``Bind`` call, the actor is live and communication with the new
process can begin. The constructor concludes by initiating the process of
connecting the ``PDemoHelpline`` actors; ``Host::MakeBridgeAndResolve`` will be
covered in `Creating a New Top Level Actor`_. However, before we get into

Просмотреть файл

@ -14,6 +14,7 @@
#include "chrome/common/ipc_message.h"
#include "mojo/core/ports/port_ref.h"
#include "mozilla/ipc/Endpoint.h"
#include "mozilla/ipc/FileDescriptor.h"
#include "mozilla/ipc/NodeChannel.h"
#include "mozilla/ipc/ScopedPort.h"
@ -127,7 +128,11 @@ class GeckoChildProcessHost : public ChildProcessHost,
IPC::Channel* GetChannel() { return channelp(); }
ChannelId GetChannelId() { return channel_id(); }
ScopedPort TakeInitialPort() { return std::move(mInitialPort); }
UntypedEndpoint TakeInitialEndpoint() {
return UntypedEndpoint{PrivateIPDLInterface{}, std::move(mInitialPort),
base::GetCurrentProcId(),
base::GetProcId(mChildProcessHandle)};
}
// Returns a "borrowed" handle to the child process - the handle returned
// by this function must not be closed by the caller.

Просмотреть файл

@ -182,8 +182,7 @@ void UtilityProcessHost::InitAfterConnect(bool aSucceeded) {
}
mUtilityProcessParent = MakeRefPtr<UtilityProcessParent>(this);
DebugOnly<bool> rv = mUtilityProcessParent->Open(
TakeInitialPort(), base::GetProcId(GetChildProcessHandle()));
DebugOnly<bool> rv = TakeInitialEndpoint().Bind(mUtilityProcessParent.get());
MOZ_ASSERT(rv);
// Only clear mPrefSerializer in the success case to avoid a

Просмотреть файл

@ -61,9 +61,7 @@ already_AddRefed<IPDLUnitTestParent> IPDLUnitTestParent::CreateCrossProcess() {
return nullptr;
}
if (!parent->Open(
parent->mSubprocess->TakeInitialPort(),
base::GetProcId(parent->mSubprocess->GetChildProcessHandle()))) {
if (!parent->mSubprocess->TakeInitialEndpoint().Bind(parent.get())) {
ADD_FAILURE() << "Opening the parent actor failed";
return nullptr;
}

Просмотреть файл

@ -166,8 +166,7 @@ void SocketProcessHost::InitAfterConnect(bool aSucceeded) {
}
mSocketProcessParent = MakeUnique<SocketProcessParent>(this);
DebugOnly<bool> rv = mSocketProcessParent->Open(
TakeInitialPort(), base::GetProcId(GetChildProcessHandle()));
DebugOnly<bool> rv = TakeInitialEndpoint().Bind(mSocketProcessParent.get());
MOZ_ASSERT(rv);
SocketPorcessInitAttributes attributes;

Просмотреть файл

@ -27,7 +27,7 @@ RefPtr<GenericPromise> RemoteSandboxBrokerParent::Launch(
// Note: we rely on the caller to keep this instance alive while we launch
// the process, so that these closures point to valid memory.
auto resolve = [this](base::ProcessHandle handle) {
mOpened = Open(mProcess->TakeInitialPort(), base::GetProcId(handle));
mOpened = mProcess->TakeInitialEndpoint().Bind(this);
if (!mOpened) {
mProcess->Destroy();
mProcess = nullptr;