From 633e8f7952fff874459bcc78b0ef8a5773ff19a0 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Wed, 19 Apr 2023 22:10:10 +0000 Subject: [PATCH] Bug 1824465 - Part 18: Make PSocketProcess refcounted, r=necko-reviewers,kershaw Differential Revision: https://phabricator.services.mozilla.com/D173733 --- netwerk/ipc/PSocketProcess.ipdl | 2 +- netwerk/ipc/SocketProcessChild.cpp | 12 +++++++++--- netwerk/ipc/SocketProcessChild.h | 2 +- netwerk/ipc/SocketProcessHost.cpp | 2 +- netwerk/ipc/SocketProcessHost.h | 2 +- netwerk/ipc/SocketProcessImpl.cpp | 4 ++-- netwerk/ipc/SocketProcessImpl.h | 2 +- netwerk/ipc/SocketProcessParent.cpp | 6 +++--- netwerk/ipc/SocketProcessParent.h | 7 +++++-- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/netwerk/ipc/PSocketProcess.ipdl b/netwerk/ipc/PSocketProcess.ipdl index b39b24e371246..8347644e38233 100644 --- a/netwerk/ipc/PSocketProcess.ipdl +++ b/netwerk/ipc/PSocketProcess.ipdl @@ -88,7 +88,7 @@ struct SocketPorcessInitAttributes { FileDescriptor? mSandboxBroker; }; -[ManualDealloc, NeedsOtherPid] +[NeedsOtherPid] sync protocol PSocketProcess { manages PDNSRequest; diff --git a/netwerk/ipc/SocketProcessChild.cpp b/netwerk/ipc/SocketProcessChild.cpp index 97fccc35ccde4..54b2fc9db9944 100644 --- a/netwerk/ipc/SocketProcessChild.cpp +++ b/netwerk/ipc/SocketProcessChild.cpp @@ -295,9 +295,15 @@ mozilla::ipc::IPCResult SocketProcessChild::RecvInitSocketProcessBridgeParent( MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(!mSocketProcessBridgeParentMap.Contains(aContentProcessId)); - mSocketProcessBridgeParentMap.InsertOrUpdate( - aContentProcessId, MakeRefPtr( - aContentProcessId, std::move(aEndpoint))); + if (NS_WARN_IF(!aEndpoint.IsValid())) { + return IPC_FAIL(this, "invalid endpoint"); + } + + auto bridge = MakeRefPtr(aContentProcessId); + MOZ_ALWAYS_TRUE(aEndpoint.Bind(bridge)); + + mSocketProcessBridgeParentMap.InsertOrUpdate(aContentProcessId, + std::move(bridge)); return IPC_OK(); } diff --git a/netwerk/ipc/SocketProcessChild.h b/netwerk/ipc/SocketProcessChild.h index 2f01ae47acdc3..d652b86cdb846 100644 --- a/netwerk/ipc/SocketProcessChild.h +++ b/netwerk/ipc/SocketProcessChild.h @@ -27,7 +27,7 @@ class BackgroundDataBridgeParent; // This is allocated and kept alive by SocketProcessImpl. class SocketProcessChild final : public PSocketProcessChild { public: - NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessChild) + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessChild, final) SocketProcessChild(); diff --git a/netwerk/ipc/SocketProcessHost.cpp b/netwerk/ipc/SocketProcessHost.cpp index e4ff9231cd9e6..931612d07cdd9 100644 --- a/netwerk/ipc/SocketProcessHost.cpp +++ b/netwerk/ipc/SocketProcessHost.cpp @@ -165,7 +165,7 @@ void SocketProcessHost::InitAfterConnect(bool aSucceeded) { return; } - mSocketProcessParent = MakeUnique(this); + mSocketProcessParent = MakeRefPtr(this); DebugOnly rv = TakeInitialEndpoint().Bind(mSocketProcessParent.get()); MOZ_ASSERT(rv); diff --git a/netwerk/ipc/SocketProcessHost.h b/netwerk/ipc/SocketProcessHost.h index 1993d796be2d5..df5ed9312afc3 100644 --- a/netwerk/ipc/SocketProcessHost.h +++ b/netwerk/ipc/SocketProcessHost.h @@ -115,7 +115,7 @@ class SocketProcessHost final : public mozilla::ipc::GeckoChildProcessHost { enum class LaunchPhase { Unlaunched, Waiting, Complete }; LaunchPhase mLaunchPhase; - UniquePtr mSocketProcessParent; + RefPtr mSocketProcessParent; // mShutdownRequested is set to true only when Shutdown() is called. // If mShutdownRequested is false and the IPC channel is closed, // OnProcessUnexpectedShutdown will be invoked. diff --git a/netwerk/ipc/SocketProcessImpl.cpp b/netwerk/ipc/SocketProcessImpl.cpp index 1cb9e2f8613c9..40c64dc2bb736 100644 --- a/netwerk/ipc/SocketProcessImpl.cpp +++ b/netwerk/ipc/SocketProcessImpl.cpp @@ -67,10 +67,10 @@ bool SocketProcessImpl::Init(int aArgc, char* aArgv[]) { return false; } - return mSocketProcessChild.Init(TakeInitialEndpoint(), *parentBuildID); + return mSocketProcessChild->Init(TakeInitialEndpoint(), *parentBuildID); } -void SocketProcessImpl::CleanUp() { mSocketProcessChild.CleanUp(); } +void SocketProcessImpl::CleanUp() { mSocketProcessChild->CleanUp(); } } // namespace net } // namespace mozilla diff --git a/netwerk/ipc/SocketProcessImpl.h b/netwerk/ipc/SocketProcessImpl.h index 7710ea00cd080..c471ca283434d 100644 --- a/netwerk/ipc/SocketProcessImpl.h +++ b/netwerk/ipc/SocketProcessImpl.h @@ -26,7 +26,7 @@ class SocketProcessImpl final : public mozilla::ipc::ProcessChild { void CleanUp() override; private: - SocketProcessChild mSocketProcessChild; + RefPtr mSocketProcessChild = new SocketProcessChild; }; } // namespace net diff --git a/netwerk/ipc/SocketProcessParent.cpp b/netwerk/ipc/SocketProcessParent.cpp index ec6e4b2bdac3b..f55ec680af629 100644 --- a/netwerk/ipc/SocketProcessParent.cpp +++ b/netwerk/ipc/SocketProcessParent.cpp @@ -293,18 +293,18 @@ mozilla::ipc::IPCResult SocketProcessParent::RecvCachePushCheck( class DeferredDeleteSocketProcessParent : public Runnable { public: explicit DeferredDeleteSocketProcessParent( - UniquePtr&& aParent) + RefPtr&& aParent) : Runnable("net::DeferredDeleteSocketProcessParent"), mParent(std::move(aParent)) {} NS_IMETHODIMP Run() override { return NS_OK; } private: - UniquePtr mParent; + RefPtr mParent; }; /* static */ -void SocketProcessParent::Destroy(UniquePtr&& aParent) { +void SocketProcessParent::Destroy(RefPtr&& aParent) { NS_DispatchToMainThread( new DeferredDeleteSocketProcessParent(std::move(aParent))); } diff --git a/netwerk/ipc/SocketProcessParent.h b/netwerk/ipc/SocketProcessParent.h index 206b6996111d8..ee11701ed129a 100644 --- a/netwerk/ipc/SocketProcessParent.h +++ b/netwerk/ipc/SocketProcessParent.h @@ -31,8 +31,9 @@ class SocketProcessParent final public: friend class SocketProcessHost; + NS_INLINE_DECL_REFCOUNTING(SocketProcessParent, final) + explicit SocketProcessParent(SocketProcessHost* aHost); - ~SocketProcessParent(); static SocketProcessParent* GetSingleton(); @@ -112,10 +113,12 @@ class SocketProcessParent final #endif // defined(XP_WIN) private: + ~SocketProcessParent(); + SocketProcessHost* mHost; UniquePtr mMemoryReportRequest; - static void Destroy(UniquePtr&& aParent); + static void Destroy(RefPtr&& aParent); }; } // namespace net