diff --git a/netwerk/base/Dashboard.cpp b/netwerk/base/Dashboard.cpp index f69d355327d6..75a45a476068 100644 --- a/netwerk/base/Dashboard.cpp +++ b/netwerk/base/Dashboard.cpp @@ -498,7 +498,9 @@ Dashboard::RequestSockets(nsINetDashboardCallback* aCallback) { } RefPtr self(this); - SocketProcessParent::GetSingleton()->SendGetSocketData()->Then( + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + socketParent->SendGetSocketData()->Then( GetMainThreadSerialEventTarget(), __func__, [self{std::move(self)}, socketData{std::move(socketData)}](SocketDataArgs&& args) { @@ -588,7 +590,9 @@ Dashboard::RequestHttpConnections(nsINetDashboardCallback* aCallback) { } RefPtr self(this); - SocketProcessParent::GetSingleton()->SendGetHttpConnectionData()->Then( + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + socketParent->SendGetHttpConnectionData()->Then( GetMainThreadSerialEventTarget(), __func__, [self{std::move(self)}, httpData](nsTArray&& params) { httpData->mData.Assign(std::move(params)); @@ -834,7 +838,9 @@ Dashboard::RequestDNSInfo(nsINetDashboardCallback* aCallback) { } RefPtr self(this); - SocketProcessParent::GetSingleton()->SendGetDNSCacheEntries()->Then( + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + socketParent->SendGetDNSCacheEntries()->Then( GetMainThreadSerialEventTarget(), __func__, [self{std::move(self)}, dnsData{std::move(dnsData)}](nsTArray&& entries) { diff --git a/netwerk/base/NetworkConnectivityService.cpp b/netwerk/base/NetworkConnectivityService.cpp index 599cdf3dd00f..b0633fe44e9f 100644 --- a/netwerk/base/NetworkConnectivityService.cpp +++ b/netwerk/base/NetworkConnectivityService.cpp @@ -312,7 +312,7 @@ NetworkConnectivityService::RecheckDNS() { } if (nsIOService::UseSocketProcess()) { - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = SocketProcessParent::GetSingleton(); if (parent) { Unused << parent->SendRecheckDNS(); } @@ -482,7 +482,7 @@ NetworkConnectivityService::RecheckIPConnectivity() { } if (nsIOService::UseSocketProcess()) { - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = SocketProcessParent::GetSingleton(); if (parent) { Unused << parent->SendRecheckIPConnectivity(); } diff --git a/netwerk/base/ProxyAutoConfig.cpp b/netwerk/base/ProxyAutoConfig.cpp index a93d040a1402..b4bc46fe0b7a 100644 --- a/netwerk/base/ProxyAutoConfig.cpp +++ b/netwerk/base/ProxyAutoConfig.cpp @@ -878,7 +878,7 @@ RemoteProxyAutoConfig::~RemoteProxyAutoConfig() = default; nsresult RemoteProxyAutoConfig::Init(nsIThread* aPACThread) { MOZ_ASSERT(NS_IsMainThread()); - SocketProcessParent* socketProcessParent = + RefPtr socketProcessParent = SocketProcessParent::GetSingleton(); if (!socketProcessParent) { return NS_ERROR_NOT_AVAILABLE; diff --git a/netwerk/dns/DNSRequestChild.cpp b/netwerk/dns/DNSRequestChild.cpp index ec0f56b3f179..2d02bd5c8baf 100644 --- a/netwerk/dns/DNSRequestChild.cpp +++ b/netwerk/dns/DNSRequestChild.cpp @@ -462,7 +462,9 @@ void DNSRequestSender::StartRequest() { RefPtr requestParent = parent; RefPtr self = this; auto task = [requestParent, self]() { - Unused << SocketProcessParent::GetSingleton()->SendPDNSRequestConstructor( + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + Unused << socketParent->SendPDNSRequestConstructor( requestParent, self->mHost, self->mTrrServer, self->mPort, self->mType, self->mOriginAttributes, self->mFlags); }; diff --git a/netwerk/dns/NativeDNSResolverOverrideParent.cpp b/netwerk/dns/NativeDNSResolverOverrideParent.cpp index 47ba16918f10..6c42dc35e314 100644 --- a/netwerk/dns/NativeDNSResolverOverrideParent.cpp +++ b/netwerk/dns/NativeDNSResolverOverrideParent.cpp @@ -34,9 +34,10 @@ NativeDNSResolverOverrideParent::GetSingleton() { ClearOnShutdown(&gNativeDNSResolverOverrideParent); auto initTask = []() { - Unused << SocketProcessParent::GetSingleton() - ->SendPNativeDNSResolverOverrideConstructor( - gNativeDNSResolverOverrideParent); + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + Unused << socketParent->SendPNativeDNSResolverOverrideConstructor( + gNativeDNSResolverOverrideParent); }; gIOService->CallOrWaitForSocketProcess(initTask); return do_AddRef(gNativeDNSResolverOverrideParent); diff --git a/netwerk/dns/TRRServiceParent.cpp b/netwerk/dns/TRRServiceParent.cpp index 32a9fb712234..a111aec4d41e 100644 --- a/netwerk/dns/TRRServiceParent.cpp +++ b/netwerk/dns/TRRServiceParent.cpp @@ -47,7 +47,8 @@ void TRRServiceParent::Init() { return; } - SocketProcessParent* socketParent = SocketProcessParent::GetSingleton(); + RefPtr socketParent = + SocketProcessParent::GetSingleton(); if (!socketParent) { return; } diff --git a/netwerk/ipc/InputChannelThrottleQueueParent.cpp b/netwerk/ipc/InputChannelThrottleQueueParent.cpp index d1e6e82f9f5b..625100394016 100644 --- a/netwerk/ipc/InputChannelThrottleQueueParent.cpp +++ b/netwerk/ipc/InputChannelThrottleQueueParent.cpp @@ -83,9 +83,10 @@ InputChannelThrottleQueueParent::Init(uint32_t aMeanBytesPerSecond, gIOService->CallOrWaitForSocketProcess( [self, meanBytesPerSecond(mMeanBytesPerSecond), maxBytesPerSecond(mMaxBytesPerSecond)] { - Unused << SocketProcessParent::GetSingleton() - ->SendPInputChannelThrottleQueueConstructor( - self, meanBytesPerSecond, maxBytesPerSecond); + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + Unused << socketParent->SendPInputChannelThrottleQueueConstructor( + self, meanBytesPerSecond, maxBytesPerSecond); }); return NS_OK; diff --git a/netwerk/ipc/NeckoParent.cpp b/netwerk/ipc/NeckoParent.cpp index b8241bd39187..cca4445dd35d 100644 --- a/netwerk/ipc/NeckoParent.cpp +++ b/netwerk/ipc/NeckoParent.cpp @@ -736,7 +736,7 @@ mozilla::ipc::IPCResult NeckoParent::RecvInitSocketProcessBridge( return; } - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = SocketProcessParent::GetSingleton(); if (NS_WARN_IF(!parent)) { resolver(std::move(invalidEndpoint)); return; diff --git a/netwerk/ipc/SocketProcessParent.cpp b/netwerk/ipc/SocketProcessParent.cpp index 6b1ccfb1ad56..4b4ee6af9f26 100644 --- a/netwerk/ipc/SocketProcessParent.cpp +++ b/netwerk/ipc/SocketProcessParent.cpp @@ -11,6 +11,7 @@ #include "HttpTransactionParent.h" #include "SocketProcessHost.h" #include "TLSClientAuthCertSelection.h" +#include "mozilla/Atomics.h" #include "mozilla/Components.h" #include "mozilla/dom/MemoryReportRequest.h" #include "mozilla/FOGIPC.h" @@ -45,7 +46,7 @@ namespace mozilla { namespace net { -static SocketProcessParent* sSocketProcessParent; +static Atomic sSocketProcessParent; SocketProcessParent::SocketProcessParent(SocketProcessHost* aHost) : mHost(aHost) { @@ -57,17 +58,14 @@ SocketProcessParent::SocketProcessParent(SocketProcessHost* aHost) } SocketProcessParent::~SocketProcessParent() { - MOZ_ASSERT(NS_IsMainThread()); - MOZ_COUNT_DTOR(SocketProcessParent); sSocketProcessParent = nullptr; } /* static */ -SocketProcessParent* SocketProcessParent::GetSingleton() { - MOZ_ASSERT(NS_IsMainThread()); - - return sSocketProcessParent; +already_AddRefed SocketProcessParent::GetSingleton() { + RefPtr parent(sSocketProcessParent); + return parent.forget(); } void SocketProcessParent::ActorDestroy(ActorDestroyReason aWhy) { diff --git a/netwerk/ipc/SocketProcessParent.h b/netwerk/ipc/SocketProcessParent.h index 732e431e35e9..d3c95a283068 100644 --- a/netwerk/ipc/SocketProcessParent.h +++ b/netwerk/ipc/SocketProcessParent.h @@ -30,11 +30,11 @@ class SocketProcessParent final public: friend class SocketProcessHost; - NS_INLINE_DECL_REFCOUNTING(SocketProcessParent, final) + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessParent, final) explicit SocketProcessParent(SocketProcessHost* aHost); - static SocketProcessParent* GetSingleton(); + static already_AddRefed GetSingleton(); mozilla::ipc::IPCResult RecvAddMemoryReport(const MemoryReport& aReport); mozilla::ipc::IPCResult RecvAccumulateChildHistograms( diff --git a/netwerk/protocol/http/AltSvcTransactionParent.cpp b/netwerk/protocol/http/AltSvcTransactionParent.cpp index 494efa7f654f..163dd2d038d4 100644 --- a/netwerk/protocol/http/AltSvcTransactionParent.cpp +++ b/netwerk/protocol/http/AltSvcTransactionParent.cpp @@ -36,7 +36,7 @@ AltSvcTransactionParent::~AltSvcTransactionParent() { } bool AltSvcTransactionParent::Init() { - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = SocketProcessParent::GetSingleton(); if (!parent) { return false; } diff --git a/netwerk/protocol/http/nsHttpActivityDistributor.cpp b/netwerk/protocol/http/nsHttpActivityDistributor.cpp index b432bb5ea85a..01e80aff0bcf 100644 --- a/netwerk/protocol/http/nsHttpActivityDistributor.cpp +++ b/netwerk/protocol/http/nsHttpActivityDistributor.cpp @@ -193,7 +193,8 @@ nsHttpActivityDistributor::AddObserver(nsIHttpActivityObserver* aObserver) { mActivated = true; if (nsIOService::UseSocketProcess()) { auto task = []() { - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = + SocketProcessParent::GetSingleton(); if (parent && parent->CanSend()) { Unused << parent->SendOnHttpActivityDistributorActivated(true); } @@ -225,7 +226,7 @@ nsHttpActivityDistributor::RemoveObserver(nsIHttpActivityObserver* aObserver) { if (nsIOService::UseSocketProcess() && !mActivated) { auto task = []() { - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = SocketProcessParent::GetSingleton(); if (parent && parent->CanSend()) { Unused << parent->SendOnHttpActivityDistributorActivated(false); } @@ -254,7 +255,7 @@ nsHttpActivityDistributor::SetObserveProxyResponse(bool aObserveProxyResponse) { mObserveProxyResponse = aObserveProxyResponse; if (nsIOService::UseSocketProcess()) { auto task = [aObserveProxyResponse]() { - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = SocketProcessParent::GetSingleton(); if (parent && parent->CanSend()) { Unused << parent->SendOnHttpActivityDistributorObserveProxyResponse( aObserveProxyResponse); @@ -283,7 +284,7 @@ nsHttpActivityDistributor::SetObserveConnection(bool aObserveConnection) { mObserveConnection = aObserveConnection; if (nsIOService::UseSocketProcess()) { auto task = [aObserveConnection]() { - SocketProcessParent* parent = SocketProcessParent::GetSingleton(); + RefPtr parent = SocketProcessParent::GetSingleton(); if (parent && parent->CanSend()) { Unused << parent->SendOnHttpActivityDistributorObserveConnection( aObserveConnection); diff --git a/netwerk/protocol/http/nsHttpChannel.cpp b/netwerk/protocol/http/nsHttpChannel.cpp index 8858f2e78c8c..20d26b4d6245 100644 --- a/netwerk/protocol/http/nsHttpChannel.cpp +++ b/netwerk/protocol/http/nsHttpChannel.cpp @@ -1635,7 +1635,8 @@ nsresult nsHttpChannel::InitTransaction() { if (NS_WARN_IF(!gIOService->SocketProcessReady())) { return NS_ERROR_NOT_AVAILABLE; } - SocketProcessParent* socketProcess = SocketProcessParent::GetSingleton(); + RefPtr socketProcess = + SocketProcessParent::GetSingleton(); if (!socketProcess->CanSend()) { return NS_ERROR_NOT_AVAILABLE; } diff --git a/netwerk/protocol/http/nsHttpHandler.cpp b/netwerk/protocol/http/nsHttpHandler.cpp index 00aa8d562230..45206dfa792a 100644 --- a/netwerk/protocol/http/nsHttpHandler.cpp +++ b/netwerk/protocol/http/nsHttpHandler.cpp @@ -588,15 +588,15 @@ nsresult nsHttpHandler::InitConnectionMgr() { auto task = [self]() { RefPtr parent = self->mConnMgr->AsHttpConnectionMgrParent(); - Unused << SocketProcessParent::GetSingleton() - ->SendPHttpConnectionMgrConstructor( - parent, - HttpHandlerInitArgs( - self->mLegacyAppName, self->mLegacyAppVersion, - self->mPlatform, self->mOscpu, self->mMisc, - self->mProduct, self->mProductSub, self->mAppName, - self->mAppVersion, self->mCompatFirefox, - self->mCompatDevice, self->mDeviceModelId)); + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + Unused << socketParent->SendPHttpConnectionMgrConstructor( + parent, + HttpHandlerInitArgs(self->mLegacyAppName, self->mLegacyAppVersion, + self->mPlatform, self->mOscpu, self->mMisc, + self->mProduct, self->mProductSub, self->mAppName, + self->mAppVersion, self->mCompatFirefox, + self->mCompatDevice, self->mDeviceModelId)); }; gIOService->CallOrWaitForSocketProcess(std::move(task)); } else { @@ -1158,8 +1158,9 @@ void nsHttpHandler::PrefsChanged(const char* pref) { if (!XRE_IsSocketProcess()) { mDeviceModelId = mozilla::net::GetDeviceModelId(); if (gIOService->SocketProcessReady()) { - Unused << SocketProcessParent::GetSingleton() - ->SendUpdateDeviceModelId(mDeviceModelId); + RefPtr socketParent = + SocketProcessParent::GetSingleton(); + Unused << socketParent->SendUpdateDeviceModelId(mDeviceModelId); } } } else { diff --git a/security/manager/ssl/nsNSSComponent.cpp b/security/manager/ssl/nsNSSComponent.cpp index d9f400be6baa..6e3baf055057 100644 --- a/security/manager/ssl/nsNSSComponent.cpp +++ b/security/manager/ssl/nsNSSComponent.cpp @@ -1929,8 +1929,9 @@ nsNSSComponent::ClearSSLExternalAndInternalSessionCache() { if (mozilla::net::nsIOService::UseSocketProcess()) { if (mozilla::net::gIOService) { mozilla::net::gIOService->CallOrWaitForSocketProcess([]() { - Unused << mozilla::net::SocketProcessParent::GetSingleton() - ->SendClearSessionCache(); + RefPtr socketParent = + mozilla::net::SocketProcessParent::GetSingleton(); + Unused << socketParent->SendClearSessionCache(); }); } } @@ -1960,19 +1961,15 @@ nsNSSComponent::AsyncClearSSLExternalAndInternalSessionCache( if (mozilla::net::nsIOService::UseSocketProcess() && mozilla::net::gIOService) { - mozilla::net::gIOService->CallOrWaitForSocketProcess( - [p = RefPtr{promise}]() { - Unused << mozilla::net::SocketProcessParent::GetSingleton() - ->SendClearSessionCache() - ->Then( - GetCurrentSerialEventTarget(), __func__, - [promise = RefPtr{p}] { - promise->MaybeResolveWithUndefined(); - }, - [promise = RefPtr{p}] { - promise->MaybeReject(NS_ERROR_UNEXPECTED); - }); - }); + mozilla::net::gIOService->CallOrWaitForSocketProcess([p = RefPtr{ + promise}]() { + RefPtr socketParent = + mozilla::net::SocketProcessParent::GetSingleton(); + Unused << socketParent->SendClearSessionCache()->Then( + GetCurrentSerialEventTarget(), __func__, + [promise = RefPtr{p}] { promise->MaybeResolveWithUndefined(); }, + [promise = RefPtr{p}] { promise->MaybeReject(NS_ERROR_UNEXPECTED); }); + }); } else { promise->MaybeResolveWithUndefined(); } diff --git a/security/sandbox/common/test/SandboxTest.cpp b/security/sandbox/common/test/SandboxTest.cpp index 0cf5279b57d6..e62f5da3a0d2 100644 --- a/security/sandbox/common/test/SandboxTest.cpp +++ b/security/sandbox/common/test/SandboxTest.cpp @@ -242,10 +242,10 @@ SandboxTest::StartTests(const nsTArray& aProcessesList) { // If socket process was previously disabled by env, // nsIOService code will take some time before it creates the new // process and it triggers this callback - net::SocketProcessParent* parent = + RefPtr parent = net::SocketProcessParent::GetSingleton(); if (parent) { - return InitializeSandboxTestingActors(parent, processPromise); + return InitializeSandboxTestingActors(parent.get(), processPromise); } return processPromise->Reject(NS_ERROR_FAILURE, __func__); }); diff --git a/toolkit/components/glean/ipc/FOGIPC.cpp b/toolkit/components/glean/ipc/FOGIPC.cpp index 92ae5c349b02..03a3851e58ac 100644 --- a/toolkit/components/glean/ipc/FOGIPC.cpp +++ b/toolkit/components/glean/ipc/FOGIPC.cpp @@ -480,7 +480,7 @@ void FlushAllChildData( } } - if (net::SocketProcessParent* socketParent = + if (RefPtr socketParent = net::SocketProcessParent::GetSingleton()) { promises.EmplaceBack(socketParent->SendFlushFOGData()); } @@ -616,14 +616,14 @@ void TestTriggerMetrics(uint32_t aProcessType, [promise]() { promise->MaybeResolveWithUndefined(); }, [promise]() { promise->MaybeRejectWithUndefined(); }); break; - case nsIXULRuntime::PROCESS_TYPE_SOCKET: - Unused << net::SocketProcessParent::GetSingleton() - ->SendTestTriggerMetrics() - ->Then( - GetCurrentSerialEventTarget(), __func__, - [promise]() { promise->MaybeResolveWithUndefined(); }, - [promise]() { promise->MaybeRejectWithUndefined(); }); - break; + case nsIXULRuntime::PROCESS_TYPE_SOCKET: { + RefPtr socketParent( + net::SocketProcessParent::GetSingleton()); + Unused << socketParent->SendTestTriggerMetrics()->Then( + GetCurrentSerialEventTarget(), __func__, + [promise]() { promise->MaybeResolveWithUndefined(); }, + [promise]() { promise->MaybeRejectWithUndefined(); }); + } break; case nsIXULRuntime::PROCESS_TYPE_UTILITY: Unused << ipc::UtilityProcessManager::GetSingleton() ->GetProcessParent(ipc::SandboxingKind::GENERIC_UTILITY) diff --git a/toolkit/components/telemetry/other/UntrustedModules.cpp b/toolkit/components/telemetry/other/UntrustedModules.cpp index 7a80cd9c8d60..11c3f6f4ded8 100644 --- a/toolkit/components/telemetry/other/UntrustedModules.cpp +++ b/toolkit/components/telemetry/other/UntrustedModules.cpp @@ -133,7 +133,8 @@ MultiGetUntrustedModulesData::GetUntrustedModuleLoadEvents() { AddPending(contentParent->SendGetUntrustedModulesData()); } - if (auto* socketActor = net::SocketProcessParent::GetSingleton()) { + if (RefPtr socketActor = + net::SocketProcessParent::GetSingleton()) { AddPending(socketActor->SendGetUntrustedModulesData()); } diff --git a/toolkit/xre/dllservices/UntrustedModulesProcessor.cpp b/toolkit/xre/dllservices/UntrustedModulesProcessor.cpp index 35dd814acddd..fe79cab32c4d 100644 --- a/toolkit/xre/dllservices/UntrustedModulesProcessor.cpp +++ b/toolkit/xre/dllservices/UntrustedModulesProcessor.cpp @@ -193,7 +193,8 @@ NS_IMETHODIMP UntrustedModulesProcessor::Observe(nsISupports* aSubject, for (auto* proc : contentProcesses) { Unused << proc->SendUnblockUntrustedModulesThread(); } - if (auto* proc = net::SocketProcessParent::GetSingleton()) { + if (RefPtr proc = + net::SocketProcessParent::GetSingleton()) { Unused << proc->SendUnblockUntrustedModulesThread(); } if (auto* rddMgr = RDDProcessManager::Get()) {