diff --git a/dom/media/webrtc/MediaTransportChild.h b/dom/media/webrtc/MediaTransportChild.h index 1006a7b48d78..b84dce70b2f1 100644 --- a/dom/media/webrtc/MediaTransportChild.h +++ b/dom/media/webrtc/MediaTransportChild.h @@ -13,10 +13,8 @@ class MediaTransportHandlerIPC; class MediaTransportChild : public dom::PMediaTransportChild { public: #ifdef MOZ_WEBRTC - NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTransportChild, override) - explicit MediaTransportChild(MediaTransportHandlerIPC* aUser); - + virtual ~MediaTransportChild(); mozilla::ipc::IPCResult RecvOnCandidate(const string& transportId, const CandidateInfo& candidateInfo); mozilla::ipc::IPCResult RecvOnAlpnNegotiated(const string& alpn); @@ -32,7 +30,6 @@ class MediaTransportChild : public dom::PMediaTransportChild { const int& state); private: - virtual ~MediaTransportChild(); RefPtr mUser; #endif // MOZ_WEBRTC }; diff --git a/dom/media/webrtc/MediaTransportParent.h b/dom/media/webrtc/MediaTransportParent.h index 9487c6f4af72..8c698d0f54bd 100644 --- a/dom/media/webrtc/MediaTransportParent.h +++ b/dom/media/webrtc/MediaTransportParent.h @@ -13,9 +13,8 @@ namespace mozilla { class MediaTransportParent : public dom::PMediaTransportParent { public: #ifdef MOZ_WEBRTC - NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTransportParent, override) - MediaTransportParent(); + virtual ~MediaTransportParent(); mozilla::ipc::IPCResult RecvGetIceLog(const nsCString& pattern, GetIceLogResolver&& aResolve); @@ -58,9 +57,9 @@ class MediaTransportParent : public dom::PMediaTransportParent { const double& now, GetIceStatsResolver&& aResolve); - private: - virtual ~MediaTransportParent(); + void ActorDestroy(ActorDestroyReason aWhy); + private: // Hide the sigslot/MediaTransportHandler stuff from IPC. class Impl; std::unique_ptr mImpl; diff --git a/dom/media/webrtc/PMediaTransport.ipdl b/dom/media/webrtc/PMediaTransport.ipdl index d12f411d7e0e..b754d34ad2a9 100644 --- a/dom/media/webrtc/PMediaTransport.ipdl +++ b/dom/media/webrtc/PMediaTransport.ipdl @@ -28,8 +28,13 @@ using mozilla::net::NrIceStunAddrArray from "mozilla/net/PStunAddrsParams.h"; namespace mozilla { namespace dom { +[ManualDealloc] async protocol PMediaTransport { + manager PBackground; + parent: + async __delete__(); + #ifdef MOZ_WEBRTC async GetIceLog(nsCString pattern) returns (WebrtcGlobalLog loglines); async ClearIceLog(); diff --git a/dom/media/webrtc/jsapi/MediaTransportHandlerIPC.cpp b/dom/media/webrtc/jsapi/MediaTransportHandlerIPC.cpp index ea2df0a49a85..6369d229eead 100644 --- a/dom/media/webrtc/jsapi/MediaTransportHandlerIPC.cpp +++ b/dom/media/webrtc/jsapi/MediaTransportHandlerIPC.cpp @@ -8,7 +8,6 @@ #include "mozilla/net/SocketProcessBridgeChild.h" #include "mozilla/RefPtr.h" #include "mozilla/ipc/BackgroundChild.h" -#include "mozilla/ipc/Endpoint.h" #include "mozilla/ipc/PBackgroundChild.h" #include "common/browser_logging/CSFLog.h" @@ -25,64 +24,40 @@ MediaTransportHandlerIPC::MediaTransportHandlerIPC( : MediaTransportHandler(aCallbackThread) {} void MediaTransportHandlerIPC::Initialize() { - using EndpointPromise = - MozPromise, - nsCString, true>; - mInitPromise = - net::SocketProcessBridgeChild::GetSocketProcessBridge() - ->Then( - GetCurrentSerialEventTarget(), __func__, - [](const RefPtr& aBridge) { - mozilla::ipc::Endpoint - parentEndpoint; - mozilla::ipc::Endpoint - childEndpoint; - mozilla::dom::PMediaTransport::CreateEndpoints(&parentEndpoint, - &childEndpoint); - - if (!aBridge || !aBridge->SendInitMediaTransport( - std::move(parentEndpoint))) { - NS_WARNING( - "MediaTransportHandlerIPC async init failed! Webrtc " - "networking " - "will not work!"); - return EndpointPromise::CreateAndReject( - nsCString("SendInitMediaTransport failed!"), __func__); - } - - return EndpointPromise::CreateAndResolve( - std::move(childEndpoint), __func__); - }, - [](const nsCString& aError) { - return EndpointPromise::CreateAndReject(aError, __func__); - }) - ->Then( - mCallbackThread, __func__, - [this, self = RefPtr(this)]( - mozilla::ipc::Endpoint&& - aEndpoint) { - RefPtr child = - new MediaTransportChild(this); - aEndpoint.Bind(child); - // IPC owns mChild! When it is done with it, mChild will let us - // know it is going away. - mChild = child; - - CSFLogDebug(LOGTAG, "%s Init done", __func__); - return InitPromise::CreateAndResolve(true, __func__); - }, - [=](const nsCString& aError) { - CSFLogError( - LOGTAG, + mInitPromise = net::SocketProcessBridgeChild::GetSocketProcessBridge()->Then( + mCallbackThread, __func__, + [this, self = RefPtr(this)]( + const RefPtr& aBridge) { + ipc::PBackgroundChild* actor = + ipc::BackgroundChild::GetOrCreateSocketActorForCurrentThread(); + // An actor that can't send is possible if the socket process has + // crashed but hasn't been reconnected properly. See + // SocketProcessBridgeChild::ActorDestroy for more info. + if (!actor || !actor->CanSend()) { + NS_WARNING( + "MediaTransportHandlerIPC async init failed! Webrtc networking " + "will not work!"); + return InitPromise::CreateAndReject( + nsCString("GetOrCreateSocketActorForCurrentThread failed!"), + __func__); + } + MediaTransportChild* child = new MediaTransportChild(this); + // PBackgroungChild owns mChild! When it is done with it, + // mChild will let us know it it going away. + mChild = actor->SendPMediaTransportConstructor(child); + CSFLogDebug(LOGTAG, "%s Init done", __func__); + return InitPromise::CreateAndResolve(true, __func__); + }, + [=](const nsCString& aError) { + CSFLogError(LOGTAG, "MediaTransportHandlerIPC async init failed! Webrtc " "networking will not work! Error was %s", aError.get()); - NS_WARNING( - "MediaTransportHandlerIPC async init failed! Webrtc " - "networking " - "will not work!"); - return InitPromise::CreateAndReject(aError, __func__); - }); + NS_WARNING( + "MediaTransportHandlerIPC async init failed! Webrtc networking " + "will not work!"); + return InitPromise::CreateAndReject(aError, __func__); + }); } RefPtr @@ -196,7 +171,7 @@ nsresult MediaTransportHandlerIPC::SetIceConfig( void MediaTransportHandlerIPC::Destroy() { if (mChild) { - mChild->Close(); + MediaTransportChild::Send__delete__(mChild); mChild = nullptr; } delete this; diff --git a/dom/media/webrtc/jsapi/MediaTransportParent.cpp b/dom/media/webrtc/jsapi/MediaTransportParent.cpp index 31d0b9f30f07..d0f90df8c5fe 100644 --- a/dom/media/webrtc/jsapi/MediaTransportParent.cpp +++ b/dom/media/webrtc/jsapi/MediaTransportParent.cpp @@ -235,4 +235,6 @@ mozilla::ipc::IPCResult MediaTransportParent::RecvGetIceStats( return ipc::IPCResult::Ok(); } +void MediaTransportParent::ActorDestroy(ActorDestroyReason aWhy) {} + } // namespace mozilla diff --git a/ipc/glue/BackgroundChild.h b/ipc/glue/BackgroundChild.h index 89de00795f6d..82a28c43d565 100644 --- a/ipc/glue/BackgroundChild.h +++ b/ipc/glue/BackgroundChild.h @@ -21,6 +21,13 @@ class ContentProcess; } // namespace dom +namespace net { + +class SocketProcessChild; +class SocketProcessBridgeChild; + +} // namespace net + namespace ipc { class PBackgroundChild; @@ -37,6 +44,10 @@ class PBackgroundStarterChild; // create the actor if it doesn't exist yet. Thereafter (assuming success) // GetForCurrentThread() will return the same actor every time. // +// GetOrCreateSocketActorForCurrentThread, which is like +// GetOrCreateForCurrentThread, is used to get or create PBackground actor +// between child process and socket process. +// // CloseForCurrentThread() will close the current PBackground actor. Subsequent // calls to GetForCurrentThread will return null. CloseForCurrentThread() may // only be called exactly once for each thread-specific actor. Currently it is @@ -45,12 +56,13 @@ class PBackgroundStarterChild; // The PBackgroundChild actor and all its sub-protocol actors will be // automatically destroyed when its designated thread completes. // -// InitContentStarter must be called on the main thread +// Init{Content,Socket,SocketBridge}Starter must be called on the main thread // with an actor bridging to the relevant target process type before these // methods can be used. class BackgroundChild final { friend class mozilla::dom::ContentParent; friend class mozilla::dom::ContentProcess; + friend class mozilla::net::SocketProcessChild; public: // See above. @@ -59,12 +71,25 @@ class BackgroundChild final { // See above. static PBackgroundChild* GetOrCreateForCurrentThread(); + // See above. + static PBackgroundChild* GetOrCreateSocketActorForCurrentThread(); + + // See above. + static PBackgroundChild* GetOrCreateForSocketParentBridgeForCurrentThread(); + // See above. static void CloseForCurrentThread(); // See above. static void InitContentStarter(mozilla::dom::ContentChild* aContent); + // See above. + static void InitSocketStarter(mozilla::net::SocketProcessChild* aSocket); + + // See above. + static void InitSocketBridgeStarter( + mozilla::net::SocketProcessBridgeChild* aSocketBridge); + private: // Only called by this class's friends. static void Startup(); diff --git a/ipc/glue/BackgroundChildImpl.cpp b/ipc/glue/BackgroundChildImpl.cpp index 76ad8942da2f..536ef2a10e8c 100644 --- a/ipc/glue/BackgroundChildImpl.cpp +++ b/ipc/glue/BackgroundChildImpl.cpp @@ -21,6 +21,7 @@ #include "mozilla/dom/PBackgroundSDBConnectionChild.h" #include "mozilla/dom/PFileSystemRequestChild.h" #include "mozilla/dom/EndpointForReportChild.h" +#include "mozilla/dom/PMediaTransportChild.h" #include "mozilla/dom/PVsync.h" #include "mozilla/dom/TemporaryIPCBlobChild.h" #include "mozilla/dom/cache/ActorUtils.h" @@ -514,6 +515,21 @@ bool BackgroundChildImpl::DeallocPEndpointForReportChild( return true; } +dom::PMediaTransportChild* BackgroundChildImpl::AllocPMediaTransportChild() { + // We don't allocate here: MediaTransportHandlerIPC is in charge of that, + // so we don't need to know the implementation particulars here. + MOZ_ASSERT_UNREACHABLE( + "The only thing that ought to be creating a PMediaTransportChild is " + "MediaTransportHandlerIPC!"); + return nullptr; +} + +bool BackgroundChildImpl::DeallocPMediaTransportChild( + dom::PMediaTransportChild* aActor) { + delete aActor; + return true; +} + } // namespace mozilla::ipc mozilla::ipc::IPCResult TestChild::Recv__delete__(const nsACString& aTestArg) { diff --git a/ipc/glue/BackgroundChildImpl.h b/ipc/glue/BackgroundChildImpl.h index 1c17e141c3bd..28d3dbc14cfb 100644 --- a/ipc/glue/BackgroundChildImpl.h +++ b/ipc/glue/BackgroundChildImpl.h @@ -198,6 +198,11 @@ class BackgroundChildImpl : public PBackgroundChild { virtual bool DeallocPEndpointForReportChild( PEndpointForReportChild* aActor) override; + + virtual dom::PMediaTransportChild* AllocPMediaTransportChild() override; + + virtual bool DeallocPMediaTransportChild( + dom::PMediaTransportChild* aActor) override; }; class BackgroundChildImpl::ThreadLocal final { diff --git a/ipc/glue/BackgroundImpl.cpp b/ipc/glue/BackgroundImpl.cpp index f12250a87f42..420f047f42e0 100644 --- a/ipc/glue/BackgroundImpl.cpp +++ b/ipc/glue/BackgroundImpl.cpp @@ -34,6 +34,8 @@ #include "mozilla/ipc/Endpoint.h" #include "mozilla/ipc/PBackgroundStarter.h" #include "mozilla/ipc/ProtocolTypes.h" +#include "mozilla/net/SocketProcessChild.h" +#include "mozilla/net/SocketProcessBridgeChild.h" #include "nsCOMPtr.h" #include "nsIEventTarget.h" #include "nsIObserver.h" @@ -79,6 +81,10 @@ class ChildImpl; // Utility Functions // ----------------------------------------------------------------------------- +void AssertIsInMainOrSocketProcess() { + MOZ_ASSERT(XRE_IsParentProcess() || XRE_IsSocketProcess()); +} + void AssertIsOnMainThread() { THREADSAFETY_ASSERT(NS_IsMainThread()); } // ----------------------------------------------------------------------------- @@ -100,7 +106,7 @@ class ParentImpl final : public BackgroundParentImpl { TimerCallbackClosure(nsIThread* aThread, nsTArray* aLiveActors) : mThread(aThread), mLiveActors(aLiveActors) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_ASSERT(aThread); MOZ_ASSERT(aLiveActors); @@ -140,7 +146,7 @@ class ParentImpl final : public BackgroundParentImpl { // create the background thread after application shutdown has started. static bool sShutdownHasStarted; - // null if this is a same-process actor. + // null if this is a same-process or socket process actor. const RefPtr mContent; // Set when the actor is opened successfully and used to handle shutdown @@ -195,29 +201,31 @@ class ParentImpl final : public BackgroundParentImpl { static void ShutdownTimerCallback(nsITimer* aTimer, void* aClosure); - // NOTE: ParentImpl could be used in 2 cases below. + // NOTE: ParentImpl could be used in 4 cases below. // 1. Within the parent process. // 2. Between parent process and content process. - // |aContent| should be not null for case 2. For cases 1, it's null. + // 3. Between socket process and content process. + // 4. Between parent process and socket process. + // |aContent| should be not null for case 2. For cases 1, 3 and 4, it's null. explicit ParentImpl(ThreadsafeContentParentHandle* aContent, bool aIsOtherProcessActor) : mContent(aContent), mLiveActorArray(nullptr), mIsOtherProcessActor(aIsOtherProcessActor), mActorDestroyed(false) { - AssertIsInMainProcess(); + MOZ_ASSERT(XRE_IsParentProcess() || XRE_IsSocketProcess()); MOZ_ASSERT_IF(!aIsOtherProcessActor, XRE_IsParentProcess()); } ~ParentImpl() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); } void MainThreadActorDestroy(); void SetLiveActorArray(nsTArray* aLiveActorArray) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aLiveActorArray); MOZ_ASSERT(!aLiveActorArray->Contains(this)); @@ -501,6 +509,12 @@ class ChildImpl final : public BackgroundChildImpl { // For PBackground between parent and content process. static ThreadInfoWrapper sParentAndContentProcessThreadInfo; + // For PBackground between socket and content process. + static ThreadInfoWrapper sSocketAndContentProcessThreadInfo; + + // For PBackground between socket and parent process. + static ThreadInfoWrapper sSocketAndParentProcessThreadInfo; + // This is only modified on the main thread. It prevents us from trying to // create the background thread after application shutdown has started. static bool sShutdownHasStarted; @@ -569,6 +583,12 @@ class ChildImpl final : public BackgroundChildImpl { // Forwarded from BackgroundChild. static PBackgroundChild* GetOrCreateForCurrentThread(); + // Forwarded from BackgroundChild. + static PBackgroundChild* GetOrCreateSocketActorForCurrentThread(); + + // Forwarded from BackgroundChild. + static PBackgroundChild* GetOrCreateForSocketParentBridgeForCurrentThread(); + static void CloseForCurrentThread(); // Forwarded from BackgroundChildImpl. @@ -577,6 +597,13 @@ class ChildImpl final : public BackgroundChildImpl { // Forwarded from BackgroundChild. static void InitContentStarter(mozilla::dom::ContentChild* aContent); + // Forwarded from BackgroundChild. + static void InitSocketStarter(mozilla::net::SocketProcessChild* aSocket); + + // Forwarded from BackgroundChild. + static void InitSocketBridgeStarter( + mozilla::net::SocketProcessBridgeChild* aSocketBridge); + static void ThreadLocalDestructor(void* aThreadLocal); // This class is reference counted. @@ -676,6 +703,17 @@ PBackgroundChild* BackgroundChild::GetOrCreateForCurrentThread() { return ChildImpl::GetOrCreateForCurrentThread(); } +// static +PBackgroundChild* BackgroundChild::GetOrCreateSocketActorForCurrentThread() { + return ChildImpl::GetOrCreateSocketActorForCurrentThread(); +} + +// static +PBackgroundChild* +BackgroundChild::GetOrCreateForSocketParentBridgeForCurrentThread() { + return ChildImpl::GetOrCreateForSocketParentBridgeForCurrentThread(); +} + // static void BackgroundChild::CloseForCurrentThread() { ChildImpl::CloseForCurrentThread(); @@ -686,6 +724,17 @@ void BackgroundChild::InitContentStarter(ContentChild* aContent) { ChildImpl::InitContentStarter(aContent); } +// static +void BackgroundChild::InitSocketStarter(net::SocketProcessChild* aSocket) { + ChildImpl::InitSocketStarter(aSocket); +} + +// static +void BackgroundChild::InitSocketBridgeStarter( + net::SocketProcessBridgeChild* aSocketBridge) { + ChildImpl::InitSocketBridgeStarter(aSocketBridge); +} + // ----------------------------------------------------------------------------- // BackgroundChildImpl Public Methods // ----------------------------------------------------------------------------- @@ -720,6 +769,10 @@ bool ParentImpl::sShutdownHasStarted = false; ChildImpl::ThreadInfoWrapper ChildImpl::sParentAndContentProcessThreadInfo; +ChildImpl::ThreadInfoWrapper ChildImpl::sSocketAndContentProcessThreadInfo; + +ChildImpl::ThreadInfoWrapper ChildImpl::sSocketAndParentProcessThreadInfo; + bool ChildImpl::sShutdownHasStarted = false; // ----------------------------------------------------------------------------- @@ -760,7 +813,7 @@ uint64_t ParentImpl::GetChildID(PBackgroundParent* aBackgroundActor) { bool ParentImpl::AllocStarter(ContentParent* aContent, Endpoint&& aEndpoint, bool aCrossProcess) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_ASSERT(aEndpoint.IsValid()); @@ -794,7 +847,7 @@ bool ParentImpl::AllocStarter(ContentParent* aContent, // static bool ParentImpl::CreateBackgroundThread() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_ASSERT(!sBackgroundThread); MOZ_ASSERT(!sLiveActorsForBackgroundThread); @@ -861,7 +914,7 @@ bool ParentImpl::CreateBackgroundThread() { // static void ParentImpl::ShutdownBackgroundThread() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_ASSERT(sShutdownHasStarted); MOZ_ASSERT_IF(!sBackgroundThread, !sLiveActorCount); @@ -913,7 +966,7 @@ void ParentImpl::ShutdownBackgroundThread() { // static void ParentImpl::ShutdownTimerCallback(nsITimer* aTimer, void* aClosure) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_ASSERT(sShutdownHasStarted); MOZ_ASSERT(sLiveActorCount); @@ -949,7 +1002,7 @@ void ParentImpl::ShutdownTimerCallback(nsITimer* aTimer, void* aClosure) { void ParentImpl::Destroy() { // May be called on any thread! - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread( NewNonOwningRunnableMethod("ParentImpl::MainThreadActorDestroy", this, @@ -957,7 +1010,7 @@ void ParentImpl::Destroy() { } void ParentImpl::MainThreadActorDestroy() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_ASSERT_IF(!mIsOtherProcessActor, !mContent); @@ -969,7 +1022,7 @@ void ParentImpl::MainThreadActorDestroy() { } void ParentImpl::ActorDestroy(ActorDestroyReason aWhy) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(!mActorDestroyed); MOZ_ASSERT_IF(mIsOtherProcessActor, mLiveActorArray); @@ -1000,7 +1053,7 @@ NS_IMPL_ISUPPORTS(ParentImpl::ShutdownObserver, nsIObserver) NS_IMETHODIMP ParentImpl::ShutdownObserver::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_ASSERT(!sShutdownHasStarted); MOZ_ASSERT(!strcmp(aTopic, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID)); @@ -1020,14 +1073,14 @@ BackgroundStarterParent::BackgroundStarterParent( ThreadsafeContentParentHandle* aContent, bool aCrossProcess) : mCrossProcess(aCrossProcess), mContent(aContent) { AssertIsOnMainThread(); - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); MOZ_ASSERT_IF(!mCrossProcess, !mContent); MOZ_ASSERT_IF(!mCrossProcess, XRE_IsParentProcess()); } void BackgroundStarterParent::SetLiveActorArray( nsTArray* aLiveActorArray) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aLiveActorArray); MOZ_ASSERT(!aLiveActorArray->Contains(this)); @@ -1090,6 +1143,8 @@ void ChildImpl::Startup() { // assert that we're being called on the main thread here. sParentAndContentProcessThreadInfo.Startup(); + sSocketAndContentProcessThreadInfo.Startup(); + sSocketAndParentProcessThreadInfo.Startup(); nsCOMPtr observerService = services::GetObserverService(); MOZ_RELEASE_ASSERT(observerService); @@ -1119,6 +1174,8 @@ void ChildImpl::Shutdown() { AssertIsOnMainThread(); sParentAndContentProcessThreadInfo.Shutdown(); + sSocketAndContentProcessThreadInfo.Shutdown(); + sSocketAndParentProcessThreadInfo.Shutdown(); sShutdownHasStarted = true; } @@ -1146,6 +1203,17 @@ PBackgroundChild* ChildImpl::GetOrCreateForCurrentThread() { return sParentAndContentProcessThreadInfo.GetOrCreateForCurrentThread(); } +/* static */ +PBackgroundChild* ChildImpl::GetOrCreateSocketActorForCurrentThread() { + return sSocketAndContentProcessThreadInfo.GetOrCreateForCurrentThread(); +} + +/* static */ +PBackgroundChild* +ChildImpl::GetOrCreateForSocketParentBridgeForCurrentThread() { + return sSocketAndParentProcessThreadInfo.GetOrCreateForCurrentThread(); +} + // static void ChildImpl::CloseForCurrentThread() { MOZ_ASSERT(!NS_IsMainThread(), @@ -1153,6 +1221,8 @@ void ChildImpl::CloseForCurrentThread() { "ChildImpl::Shutdown()."); sParentAndContentProcessThreadInfo.CloseForCurrentThread(); + sSocketAndContentProcessThreadInfo.CloseForCurrentThread(); + sSocketAndParentProcessThreadInfo.CloseForCurrentThread(); } // static @@ -1184,6 +1254,17 @@ void ChildImpl::InitContentStarter(mozilla::dom::ContentChild* aContent) { sParentAndContentProcessThreadInfo.InitStarter(aContent); } +// static +void ChildImpl::InitSocketStarter(mozilla::net::SocketProcessChild* aSocket) { + sSocketAndParentProcessThreadInfo.InitStarter(aSocket); +} + +// static +void ChildImpl::InitSocketBridgeStarter( + mozilla::net::SocketProcessBridgeChild* aSocketBridge) { + sSocketAndContentProcessThreadInfo.InitStarter(aSocketBridge); +} + // static void ChildImpl::ThreadLocalDestructor(void* aThreadLocal) { auto threadLocalInfo = static_cast(aThreadLocal); diff --git a/ipc/glue/BackgroundParent.h b/ipc/glue/BackgroundParent.h index 80f3c0984a7c..650cf2ea29f4 100644 --- a/ipc/glue/BackgroundParent.h +++ b/ipc/glue/BackgroundParent.h @@ -103,6 +103,10 @@ inline void AssertIsOnBackgroundThread() {} inline void AssertIsInMainProcess() { MOZ_ASSERT(XRE_IsParentProcess()); } +inline void AssertIsInMainOrSocketProcess() { + MOZ_ASSERT(XRE_IsParentProcess() || XRE_IsSocketProcess()); +} + } // namespace ipc } // namespace mozilla diff --git a/ipc/glue/BackgroundParentImpl.cpp b/ipc/glue/BackgroundParentImpl.cpp index aaf6c9cfe5d4..d26fbdcc4c70 100644 --- a/ipc/glue/BackgroundParentImpl.cpp +++ b/ipc/glue/BackgroundParentImpl.cpp @@ -29,6 +29,7 @@ #include "mozilla/dom/MIDIManagerParent.h" #include "mozilla/dom/MIDIPlatformService.h" #include "mozilla/dom/MIDIPortParent.h" +#include "mozilla/dom/MediaTransportParent.h" #include "mozilla/dom/MessagePortParent.h" #include "mozilla/dom/PGamepadEventChannelParent.h" #include "mozilla/dom/PGamepadTestChannelParent.h" @@ -65,6 +66,11 @@ #include "mozilla/ipc/PBackgroundTestParent.h" #include "mozilla/net/BackgroundDataBridgeParent.h" #include "mozilla/net/HttpBackgroundChannelParent.h" +#include "mozilla/net/HttpConnectionMgrParent.h" +#include "mozilla/net/WebSocketConnectionParent.h" +#include "mozilla/psm/IPCClientCertsParent.h" +#include "mozilla/psm/SelectTLSClientAuthCertParent.h" +#include "mozilla/psm/VerifySSLServerCertParent.h" #include "nsIHttpChannelInternal.h" #include "nsIPrincipal.h" #include "nsProxyRelease.h" @@ -114,26 +120,37 @@ using mozilla::dom::ContentParent; using mozilla::dom::ThreadsafeContentParentHandle; BackgroundParentImpl::BackgroundParentImpl() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); MOZ_COUNT_CTOR(mozilla::ipc::BackgroundParentImpl); } BackgroundParentImpl::~BackgroundParentImpl() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnMainThread(); MOZ_COUNT_DTOR(mozilla::ipc::BackgroundParentImpl); } void BackgroundParentImpl::ActorDestroy(ActorDestroyReason aWhy) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); } +already_AddRefed +BackgroundParentImpl::AllocPBackgroundDataBridgeParent( + const uint64_t& aChannelID) { + MOZ_ASSERT(XRE_IsSocketProcess(), "Should be in socket process"); + AssertIsOnBackgroundThread(); + + RefPtr actor = + new net::BackgroundDataBridgeParent(aChannelID); + return actor.forget(); +} + BackgroundParentImpl::PBackgroundTestParent* BackgroundParentImpl::AllocPBackgroundTestParent(const nsACString& aTestArg) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return new TestParent(); @@ -141,7 +158,7 @@ BackgroundParentImpl::AllocPBackgroundTestParent(const nsACString& aTestArg) { mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundTestConstructor( PBackgroundTestParent* aActor, const nsACString& aTestArg) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -153,7 +170,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundTestConstructor( bool BackgroundParentImpl::DeallocPBackgroundTestParent( PBackgroundTestParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -166,7 +183,7 @@ auto BackgroundParentImpl::AllocPBackgroundIDBFactoryParent( -> already_AddRefed { using mozilla::dom::indexedDB::AllocPBackgroundIDBFactoryParent; - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return AllocPBackgroundIDBFactoryParent(aLoggingInfo); @@ -177,7 +194,7 @@ BackgroundParentImpl::RecvPBackgroundIDBFactoryConstructor( PBackgroundIDBFactoryParent* aActor, const LoggingInfo& aLoggingInfo) { using mozilla::dom::indexedDB::RecvPBackgroundIDBFactoryConstructor; - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -189,7 +206,7 @@ BackgroundParentImpl::RecvPBackgroundIDBFactoryConstructor( auto BackgroundParentImpl::AllocPBackgroundIndexedDBUtilsParent() -> PBackgroundIndexedDBUtilsParent* { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::indexedDB::AllocPBackgroundIndexedDBUtilsParent(); @@ -197,7 +214,7 @@ auto BackgroundParentImpl::AllocPBackgroundIndexedDBUtilsParent() bool BackgroundParentImpl::DeallocPBackgroundIndexedDBUtilsParent( PBackgroundIndexedDBUtilsParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -206,7 +223,7 @@ bool BackgroundParentImpl::DeallocPBackgroundIndexedDBUtilsParent( } mozilla::ipc::IPCResult BackgroundParentImpl::RecvFlushPendingFileDeletions() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (!mozilla::dom::indexedDB::RecvFlushPendingFileDeletions()) { @@ -219,7 +236,7 @@ BackgroundParentImpl::PBackgroundSDBConnectionParent* BackgroundParentImpl::AllocPBackgroundSDBConnectionParent( const PersistenceType& aPersistenceType, const PrincipalInfo& aPrincipalInfo) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::AllocPBackgroundSDBConnectionParent(aPersistenceType, @@ -231,7 +248,7 @@ BackgroundParentImpl::RecvPBackgroundSDBConnectionConstructor( PBackgroundSDBConnectionParent* aActor, const PersistenceType& aPersistenceType, const PrincipalInfo& aPrincipalInfo) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -244,7 +261,7 @@ BackgroundParentImpl::RecvPBackgroundSDBConnectionConstructor( bool BackgroundParentImpl::DeallocPBackgroundSDBConnectionParent( PBackgroundSDBConnectionParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -255,7 +272,7 @@ BackgroundParentImpl::PBackgroundLSDatabaseParent* BackgroundParentImpl::AllocPBackgroundLSDatabaseParent( const PrincipalInfo& aPrincipalInfo, const uint32_t& aPrivateBrowsingId, const uint64_t& aDatastoreId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::AllocPBackgroundLSDatabaseParent( @@ -266,7 +283,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundLSDatabaseConstructor( PBackgroundLSDatabaseParent* aActor, const PrincipalInfo& aPrincipalInfo, const uint32_t& aPrivateBrowsingId, const uint64_t& aDatastoreId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -279,7 +296,7 @@ BackgroundParentImpl::RecvPBackgroundLSDatabaseConstructor( bool BackgroundParentImpl::DeallocPBackgroundLSDatabaseParent( PBackgroundLSDatabaseParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -289,7 +306,7 @@ bool BackgroundParentImpl::DeallocPBackgroundLSDatabaseParent( BackgroundParentImpl::PBackgroundLSObserverParent* BackgroundParentImpl::AllocPBackgroundLSObserverParent( const uint64_t& aObserverId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::AllocPBackgroundLSObserverParent(aObserverId); @@ -298,7 +315,7 @@ BackgroundParentImpl::AllocPBackgroundLSObserverParent( mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundLSObserverConstructor( PBackgroundLSObserverParent* aActor, const uint64_t& aObserverId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -311,7 +328,7 @@ BackgroundParentImpl::RecvPBackgroundLSObserverConstructor( bool BackgroundParentImpl::DeallocPBackgroundLSObserverParent( PBackgroundLSObserverParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -321,7 +338,7 @@ bool BackgroundParentImpl::DeallocPBackgroundLSObserverParent( BackgroundParentImpl::PBackgroundLSRequestParent* BackgroundParentImpl::AllocPBackgroundLSRequestParent( const LSRequestParams& aParams) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::AllocPBackgroundLSRequestParent(this, aParams); @@ -330,7 +347,7 @@ BackgroundParentImpl::AllocPBackgroundLSRequestParent( mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundLSRequestConstructor( PBackgroundLSRequestParent* aActor, const LSRequestParams& aParams) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -342,7 +359,7 @@ BackgroundParentImpl::RecvPBackgroundLSRequestConstructor( bool BackgroundParentImpl::DeallocPBackgroundLSRequestParent( PBackgroundLSRequestParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -352,7 +369,7 @@ bool BackgroundParentImpl::DeallocPBackgroundLSRequestParent( BackgroundParentImpl::PBackgroundLSSimpleRequestParent* BackgroundParentImpl::AllocPBackgroundLSSimpleRequestParent( const LSSimpleRequestParams& aParams) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::AllocPBackgroundLSSimpleRequestParent(this, aParams); @@ -362,7 +379,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundLSSimpleRequestConstructor( PBackgroundLSSimpleRequestParent* aActor, const LSSimpleRequestParams& aParams) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -375,7 +392,7 @@ BackgroundParentImpl::RecvPBackgroundLSSimpleRequestConstructor( bool BackgroundParentImpl::DeallocPBackgroundLSSimpleRequestParent( PBackgroundLSSimpleRequestParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -386,7 +403,7 @@ BackgroundParentImpl::PBackgroundLocalStorageCacheParent* BackgroundParentImpl::AllocPBackgroundLocalStorageCacheParent( const PrincipalInfo& aPrincipalInfo, const nsACString& aOriginKey, const uint32_t& aPrivateBrowsingId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::AllocPBackgroundLocalStorageCacheParent( @@ -398,7 +415,7 @@ BackgroundParentImpl::RecvPBackgroundLocalStorageCacheConstructor( PBackgroundLocalStorageCacheParent* aActor, const PrincipalInfo& aPrincipalInfo, const nsACString& aOriginKey, const uint32_t& aPrivateBrowsingId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -408,7 +425,7 @@ BackgroundParentImpl::RecvPBackgroundLocalStorageCacheConstructor( bool BackgroundParentImpl::DeallocPBackgroundLocalStorageCacheParent( PBackgroundLocalStorageCacheParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -418,7 +435,7 @@ bool BackgroundParentImpl::DeallocPBackgroundLocalStorageCacheParent( auto BackgroundParentImpl::AllocPBackgroundStorageParent( const nsAString& aProfilePath, const uint32_t& aPrivateBrowsingId) -> PBackgroundStorageParent* { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::AllocPBackgroundStorageParent(aProfilePath, @@ -428,7 +445,7 @@ auto BackgroundParentImpl::AllocPBackgroundStorageParent( mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundStorageConstructor( PBackgroundStorageParent* aActor, const nsAString& aProfilePath, const uint32_t& aPrivateBrowsingId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -438,7 +455,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBackgroundStorageConstructor( bool BackgroundParentImpl::DeallocPBackgroundStorageParent( PBackgroundStorageParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -448,7 +465,7 @@ bool BackgroundParentImpl::DeallocPBackgroundStorageParent( already_AddRefed BackgroundParentImpl::AllocPBackgroundSessionStorageManagerParent( const uint64_t& aTopContextId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return dom::AllocPBackgroundSessionStorageManagerParent(aTopContextId); @@ -456,7 +473,7 @@ BackgroundParentImpl::AllocPBackgroundSessionStorageManagerParent( already_AddRefed BackgroundParentImpl::AllocPBackgroundSessionStorageServiceParent() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return MakeAndAddRef(); @@ -635,7 +652,7 @@ bool BackgroundParentImpl::DeallocPTemporaryIPCBlobParent( already_AddRefed BackgroundParentImpl::AllocPVsyncParent() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); RefPtr actor = new mozilla::dom::VsyncParent(); @@ -647,7 +664,7 @@ BackgroundParentImpl::AllocPVsyncParent() { } camera::PCamerasParent* BackgroundParentImpl::AllocPCamerasParent() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); #ifdef MOZ_WEBRTC @@ -662,7 +679,7 @@ camera::PCamerasParent* BackgroundParentImpl::AllocPCamerasParent() { #ifdef MOZ_WEBRTC mozilla::ipc::IPCResult BackgroundParentImpl::RecvPCamerasConstructor( camera::PCamerasParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); return static_cast(aActor)->RecvPCamerasConstructor(); @@ -671,7 +688,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPCamerasConstructor( bool BackgroundParentImpl::DeallocPCamerasParent( camera::PCamerasParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -693,7 +710,7 @@ auto BackgroundParentImpl::AllocPUDPSocketParent( mozilla::ipc::IPCResult BackgroundParentImpl::RecvPUDPSocketConstructor( PUDPSocketParent* aActor, const Maybe& aOptionalPrincipal, const nsACString& aFilter) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (aOptionalPrincipal.isSome()) { @@ -726,11 +743,71 @@ bool BackgroundParentImpl::DeallocPUDPSocketParent(PUDPSocketParent* actor) { return true; } +already_AddRefed +BackgroundParentImpl::AllocPVerifySSLServerCertParent( + const nsTArray& aPeerCertChain, const nsACString& aHostName, + const int32_t& aPort, const OriginAttributes& aOriginAttributes, + const Maybe& aStapledOCSPResponse, + const Maybe& aSctsFromTLSExtension, + const Maybe& aDcInfo, + const uint32_t& aProviderFlags, const uint32_t& aCertVerifierFlags) { + RefPtr parent = + new mozilla::psm::VerifySSLServerCertParent(); + return parent.forget(); +} + +mozilla::ipc::IPCResult +BackgroundParentImpl::RecvPVerifySSLServerCertConstructor( + PVerifySSLServerCertParent* aActor, nsTArray&& aPeerCertChain, + const nsACString& aHostName, const int32_t& aPort, + const OriginAttributes& aOriginAttributes, + const Maybe& aStapledOCSPResponse, + const Maybe& aSctsFromTLSExtension, + const Maybe& aDcInfo, + const uint32_t& aProviderFlags, const uint32_t& aCertVerifierFlags) { + mozilla::psm::VerifySSLServerCertParent* authCert = + static_cast(aActor); + if (!authCert->Dispatch(std::move(aPeerCertChain), aHostName, aPort, + aOriginAttributes, aStapledOCSPResponse, + aSctsFromTLSExtension, aDcInfo, aProviderFlags, + aCertVerifierFlags)) { + return IPC_FAIL_NO_REASON(this); + } + return IPC_OK(); +} + +already_AddRefed +BackgroundParentImpl::AllocPSelectTLSClientAuthCertParent( + const nsACString& aHostName, const OriginAttributes& aOriginAttributes, + const int32_t& aPort, const uint32_t& aProviderFlags, + const uint32_t& aProviderTlsFlags, const ByteArray& aServerCertBytes, + const nsTArray& aCANames) { + RefPtr parent = + new mozilla::psm::SelectTLSClientAuthCertParent(); + return parent.forget(); +} + +mozilla::ipc::IPCResult +BackgroundParentImpl::RecvPSelectTLSClientAuthCertConstructor( + PSelectTLSClientAuthCertParent* actor, const nsACString& aHostName, + const OriginAttributes& aOriginAttributes, const int32_t& aPort, + const uint32_t& aProviderFlags, const uint32_t& aProviderTlsFlags, + const ByteArray& aServerCertBytes, nsTArray&& aCANames) { + mozilla::psm::SelectTLSClientAuthCertParent* selectTLSClientAuthCertParent = + static_cast(actor); + if (!selectTLSClientAuthCertParent->Dispatch( + aHostName, aOriginAttributes, aPort, aProviderFlags, + aProviderTlsFlags, aServerCertBytes, std::move(aCANames))) { + return IPC_FAIL_NO_REASON(this); + } + return IPC_OK(); +} + mozilla::dom::PBroadcastChannelParent* BackgroundParentImpl::AllocPBroadcastChannelParent( const PrincipalInfo& aPrincipalInfo, const nsACString& aOrigin, const nsAString& aChannel) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); nsString originChannelKey; @@ -758,7 +835,7 @@ class CheckPrincipalRunnable final : public Runnable { mContentParent(aParent), mPrincipalInfo(aPrincipalInfo), mOrigin(aOrigin) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(mContentParent); @@ -805,7 +882,7 @@ class CheckPrincipalRunnable final : public Runnable { mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBroadcastChannelConstructor( PBroadcastChannelParent* actor, const PrincipalInfo& aPrincipalInfo, const nsACString& aOrigin, const nsAString& aChannel) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); RefPtr parent = @@ -825,7 +902,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBroadcastChannelConstructor( bool BackgroundParentImpl::DeallocPBroadcastChannelParent( PBroadcastChannelParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -835,7 +912,7 @@ bool BackgroundParentImpl::DeallocPBroadcastChannelParent( mozilla::dom::PServiceWorkerManagerParent* BackgroundParentImpl::AllocPServiceWorkerManagerParent() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); RefPtr agent = @@ -845,7 +922,7 @@ BackgroundParentImpl::AllocPServiceWorkerManagerParent() { bool BackgroundParentImpl::DeallocPServiceWorkerManagerParent( PServiceWorkerManagerParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -857,7 +934,7 @@ bool BackgroundParentImpl::DeallocPServiceWorkerManagerParent( mozilla::ipc::IPCResult BackgroundParentImpl::RecvShutdownServiceWorkerRegistrar() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (BackgroundParent::IsOtherProcessActor(this)) { @@ -881,7 +958,7 @@ BackgroundParentImpl::AllocPCacheStorageParent( PMessagePortParent* BackgroundParentImpl::AllocPMessagePortParent( const nsID& aUUID, const nsID& aDestinationUUID, const uint32_t& aSequenceID) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return new MessagePortParent(aUUID); @@ -890,7 +967,7 @@ PMessagePortParent* BackgroundParentImpl::AllocPMessagePortParent( mozilla::ipc::IPCResult BackgroundParentImpl::RecvPMessagePortConstructor( PMessagePortParent* aActor, const nsID& aUUID, const nsID& aDestinationUUID, const uint32_t& aSequenceID) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MessagePortParent* mp = static_cast(aActor); @@ -900,9 +977,24 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPMessagePortConstructor( return IPC_OK(); } +already_AddRefed +BackgroundParentImpl::AllocPIPCClientCertsParent() { + // This should only be called in the parent process with the socket process + // as the child process, not any content processes, hence the check that the + // child ID be 0. + MOZ_ASSERT(XRE_IsParentProcess()); + MOZ_ASSERT(mozilla::ipc::BackgroundParent::GetChildID(this) == 0); + if (!XRE_IsParentProcess() || + mozilla::ipc::BackgroundParent::GetChildID(this) != 0) { + return nullptr; + } + RefPtr result = new psm::IPCClientCertsParent(); + return result.forget(); +} + bool BackgroundParentImpl::DeallocPMessagePortParent( PMessagePortParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -913,7 +1005,7 @@ bool BackgroundParentImpl::DeallocPMessagePortParent( mozilla::ipc::IPCResult BackgroundParentImpl::RecvMessagePortForceClose( const nsID& aUUID, const nsID& aDestinationUUID, const uint32_t& aSequenceID) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (!MessagePortParent::ForceClose(aUUID, aDestinationUUID, aSequenceID)) { @@ -924,14 +1016,14 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvMessagePortForceClose( } BackgroundParentImpl::PQuotaParent* BackgroundParentImpl::AllocPQuotaParent() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); return mozilla::dom::quota::AllocPQuotaParent(); } bool BackgroundParentImpl::DeallocPQuotaParent(PQuotaParent* aActor) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); MOZ_ASSERT(aActor); @@ -939,7 +1031,7 @@ bool BackgroundParentImpl::DeallocPQuotaParent(PQuotaParent* aActor) { } mozilla::ipc::IPCResult BackgroundParentImpl::RecvShutdownQuotaManager() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (BackgroundParent::IsOtherProcessActor(this)) { @@ -954,7 +1046,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvShutdownQuotaManager() { mozilla::ipc::IPCResult BackgroundParentImpl::RecvShutdownBackgroundSessionStorageManagers() { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (BackgroundParent::IsOtherProcessActor(this)) { @@ -970,7 +1062,7 @@ BackgroundParentImpl::RecvShutdownBackgroundSessionStorageManagers() { mozilla::ipc::IPCResult BackgroundParentImpl::RecvPropagateBackgroundSessionStorageManager( const uint64_t& aCurrentTopContextId, const uint64_t& aTargetTopContextId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (BackgroundParent::IsOtherProcessActor(this)) { @@ -986,7 +1078,7 @@ BackgroundParentImpl::RecvPropagateBackgroundSessionStorageManager( mozilla::ipc::IPCResult BackgroundParentImpl::RecvRemoveBackgroundSessionStorageManager( const uint64_t& aTopContextId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (BackgroundParent::IsOtherProcessActor(this)) { @@ -1040,7 +1132,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvLoadSessionStorageManagerData( already_AddRefed BackgroundParentImpl::AllocPFileSystemRequestParent( const FileSystemParams& aParams) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); RefPtr result = new FileSystemRequestParent(); @@ -1084,7 +1176,7 @@ bool BackgroundParentImpl::DeallocPWebAuthnTransactionParent( already_AddRefed BackgroundParentImpl::AllocPHttpBackgroundChannelParent( const uint64_t& aChannelId) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); RefPtr actor = @@ -1096,7 +1188,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPHttpBackgroundChannelConstructor( net::PHttpBackgroundChannelParent* aActor, const uint64_t& aChannelId) { MOZ_ASSERT(aActor); - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); net::HttpBackgroundChannelParent* aParent = @@ -1112,7 +1204,7 @@ BackgroundParentImpl::RecvPHttpBackgroundChannelConstructor( mozilla::ipc::IPCResult BackgroundParentImpl::RecvCreateMIDIPort( Endpoint&& aEndpoint, const MIDIPortInfo& aPortInfo, const bool& aSysexEnabled) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (!aEndpoint.IsValid()) { @@ -1131,7 +1223,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvCreateMIDIPort( mozilla::ipc::IPCResult BackgroundParentImpl::RecvCreateMIDIManager( Endpoint&& aEndpoint) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); if (!aEndpoint.IsValid()) { @@ -1151,7 +1243,7 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvCreateMIDIManager( mozilla::ipc::IPCResult BackgroundParentImpl::RecvHasMIDIDevice( HasMIDIDeviceResolver&& aResolver) { - AssertIsInMainProcess(); + AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); InvokeAsync(MIDIPlatformService::OwnerThread(), __func__, @@ -1366,6 +1458,22 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvRemoveEndpoint( return IPC_OK(); } +dom::PMediaTransportParent* BackgroundParentImpl::AllocPMediaTransportParent() { +#ifdef MOZ_WEBRTC + return new MediaTransportParent; +#else + return nullptr; +#endif +} + +bool BackgroundParentImpl::DeallocPMediaTransportParent( + dom::PMediaTransportParent* aActor) { +#ifdef MOZ_WEBRTC + delete aActor; +#endif + return true; +} + already_AddRefed BackgroundParentImpl::AllocPLockManagerParent(NotNull aPrincipal, const nsID& aClientId) { @@ -1377,9 +1485,30 @@ already_AddRefed BackgroundParentImpl::AllocPFetchParent() { return MakeAndAddRef(); } +already_AddRefed +BackgroundParentImpl::AllocPWebSocketConnectionParent( + const uint32_t& aListenerId) { + Maybe> listener = + net::HttpConnectionMgrParent::GetAndRemoveHttpUpgradeListener( + aListenerId); + if (!listener) { + return nullptr; + } + + RefPtr actor = + new mozilla::net::WebSocketConnectionParent(*listener); + return actor.forget(); +} + +mozilla::ipc::IPCResult +BackgroundParentImpl::RecvPWebSocketConnectionConstructor( + PWebSocketConnectionParent* actor, const uint32_t& aListenerId) { + return IPC_OK(); +} + } // namespace mozilla::ipc void TestParent::ActorDestroy(ActorDestroyReason aWhy) { - mozilla::ipc::AssertIsInMainProcess(); + mozilla::ipc::AssertIsInMainOrSocketProcess(); AssertIsOnBackgroundThread(); } diff --git a/ipc/glue/BackgroundParentImpl.h b/ipc/glue/BackgroundParentImpl.h index 7bec4fae678f..3fc8908d0a84 100644 --- a/ipc/glue/BackgroundParentImpl.h +++ b/ipc/glue/BackgroundParentImpl.h @@ -31,6 +31,9 @@ class BackgroundParentImpl : public PBackgroundParent { already_AddRefed AllocPBackgroundIDBFactoryParent(const LoggingInfo& aLoggingInfo) override; + already_AddRefed + AllocPBackgroundDataBridgeParent(const uint64_t& aChannelID) override; + mozilla::ipc::IPCResult RecvPBackgroundIDBFactoryConstructor( PBackgroundIDBFactoryParent* aActor, const LoggingInfo& aLoggingInfo) override; @@ -192,6 +195,39 @@ class BackgroundParentImpl : public PBackgroundParent { already_AddRefed AllocPVsyncParent() override; + already_AddRefed + AllocPVerifySSLServerCertParent( + const nsTArray& aPeerCertChain, const nsACString& aHostName, + const int32_t& aPort, const OriginAttributes& aOriginAttributes, + const Maybe& aStapledOCSPResponse, + const Maybe& aSctsFromTLSExtension, + const Maybe& aDcInfo, + const uint32_t& aProviderFlags, + const uint32_t& aCertVerifierFlags) override; + + mozilla::ipc::IPCResult RecvPVerifySSLServerCertConstructor( + PVerifySSLServerCertParent* aActor, nsTArray&& aPeerCertChain, + const nsACString& aHostName, const int32_t& aPort, + const OriginAttributes& aOriginAttributes, + const Maybe& aStapledOCSPResponse, + const Maybe& aSctsFromTLSExtension, + const Maybe& aDcInfo, + const uint32_t& aProviderFlags, + const uint32_t& aCertVerifierFlags) override; + + virtual already_AddRefed + AllocPSelectTLSClientAuthCertParent( + const nsACString& aHostName, const OriginAttributes& aOriginAttributes, + const int32_t& aPort, const uint32_t& aProviderFlags, + const uint32_t& aProviderTlsFlags, const ByteArray& aServerCertBytes, + const nsTArray& aCANames) override; + virtual mozilla::ipc::IPCResult RecvPSelectTLSClientAuthCertConstructor( + PSelectTLSClientAuthCertParent* actor, const nsACString& aHostName, + const OriginAttributes& aOriginAttributes, const int32_t& aPort, + const uint32_t& aProviderFlags, const uint32_t& aProviderTlsFlags, + const ByteArray& aServerCertBytes, + nsTArray&& aCANames) override; + PBroadcastChannelParent* AllocPBroadcastChannelParent( const PrincipalInfo& aPrincipalInfo, const nsACString& aOrigin, const nsAString& aChannel) override; @@ -235,6 +271,8 @@ class BackgroundParentImpl : public PBackgroundParent { PMessagePortParent* aActor, const nsID& aUUID, const nsID& aDestinationUUID, const uint32_t& aSequenceID) override; + already_AddRefed AllocPIPCClientCertsParent() override; + bool DeallocPMessagePortParent(PMessagePortParent* aActor) override; mozilla::ipc::IPCResult RecvMessagePortForceClose( @@ -360,6 +398,15 @@ class BackgroundParentImpl : public PBackgroundParent { const nsAString& aGroupName, const nsACString& aEndpointURL, const PrincipalInfo& aPrincipalInfo) override; + dom::PMediaTransportParent* AllocPMediaTransportParent() override; + bool DeallocPMediaTransportParent( + dom::PMediaTransportParent* aActor) override; + + already_AddRefed + AllocPWebSocketConnectionParent(const uint32_t& aListenerId) override; + mozilla::ipc::IPCResult RecvPWebSocketConnectionConstructor( + PWebSocketConnectionParent* actor, const uint32_t& aListenerId) override; + already_AddRefed AllocPLockManagerParent( NotNull aPrincipal, const nsID& aClientId) final; diff --git a/ipc/glue/PBackground.ipdl b/ipc/glue/PBackground.ipdl index bab504e26df2..8ecc230c111f 100644 --- a/ipc/glue/PBackground.ipdl +++ b/ipc/glue/PBackground.ipdl @@ -26,6 +26,7 @@ include protocol PFileSystemRequest; include protocol PGamepadEventChannel; include protocol PGamepadTestChannel; include protocol PHttpBackgroundChannel; +include protocol PIPCClientCerts; include protocol PIdleScheduler; include protocol PMediaTransport; include protocol PRemoteWorker; @@ -40,14 +41,17 @@ include protocol PLockManager; include protocol PMIDIManager; include protocol PMIDIPort; include protocol PQuota; +include protocol PSelectTLSClientAuthCert; include protocol PServiceWorker; include protocol PServiceWorkerContainer; include protocol PServiceWorkerManager; include protocol PServiceWorkerRegistration; include protocol PWebAuthnTransaction; include protocol PUDPSocket; +include protocol PVerifySSLServerCert; include protocol PVsync; include protocol PRemoteDecoderManager; +include protocol PWebSocketConnection; include protocol PWebTransport; include protocol PFetch; @@ -61,6 +65,7 @@ include PBackgroundSharedTypes; include PBackgroundIDBSharedTypes; include PFileSystemParams; include ProtocolTypes; +include PSMIPCTypes; include RemoteWorkerTypes; include MIDITypes; @@ -82,6 +87,7 @@ namespace ipc { [NeedsOtherPid, ChildImpl=virtual, ParentImpl=virtual] sync protocol PBackground { + manages PBackgroundDataBridge; manages PBackgroundIDBFactory; manages PBackgroundIndexedDBUtils; manages PBackgroundSDBConnection; @@ -104,8 +110,10 @@ sync protocol PBackground manages PGamepadEventChannel; manages PGamepadTestChannel; manages PHttpBackgroundChannel; + manages PIPCClientCerts; manages PIdleScheduler; manages PLockManager; + manages PMediaTransport; manages PRemoteWorker; manages PRemoteWorkerController; manages PRemoteWorkerService; @@ -115,12 +123,15 @@ sync protocol PBackground manages PMessagePort; manages PCameras; manages PQuota; + manages PSelectTLSClientAuthCert; manages PServiceWorker; manages PServiceWorkerContainer; manages PServiceWorkerManager; manages PServiceWorkerRegistration; manages PWebAuthnTransaction; + manages PWebSocketConnection; manages PUDPSocket; + manages PVerifySSLServerCert; manages PVsync; manages PFetch; @@ -128,6 +139,8 @@ parent: // Only called at startup during mochitests to check the basic infrastructure. async PBackgroundTest(nsCString testArg); + async PBackgroundDataBridge(uint64_t channelID); + async PBackgroundIDBFactory(LoggingInfo loggingInfo); async PBackgroundIndexedDBUtils(); @@ -272,14 +285,38 @@ parent: async PIdleScheduler(); + async PMediaTransport(); + + async PSelectTLSClientAuthCert(nsCString aHostName, + OriginAttributes aOriginAttributes, + int32_t aPort, + uint32_t aProviderFlags, + uint32_t aProviderTlsFlags, + ByteArray aServerCertBytes, + ByteArray[] aCANames); + + async PVerifySSLServerCert(ByteArray[] aPeerCertChain, + nsCString aHostName, + int32_t aPort, + OriginAttributes aOriginAttributes, + ByteArray? aStapledOCSPResponse, + ByteArray? aSctsFromTLSExtension, + DelegatedCredentialInfoArg? aDcInfo, + uint32_t aProviderFlags, + uint32_t aCertVerifierFlags); + async EnsureRDDProcessAndCreateBridge() returns (nsresult rv, Endpoint aEndpoint); async EnsureUtilityProcessAndCreateBridge(RemoteDecodeIn aLocation) returns (nsresult rv, Endpoint aEndpoint); + async PWebSocketConnection(uint32_t aListenerId); + async PLockManager(nsIPrincipal aPrincipalInfo, nsID aClientId); + async PIPCClientCerts(); + async PFetch(); async RequestCameraAccess() returns (nsresult rv); diff --git a/netwerk/ipc/PSocketProcess.ipdl b/netwerk/ipc/PSocketProcess.ipdl index 6defb0059555..6a922ab57421 100644 --- a/netwerk/ipc/PSocketProcess.ipdl +++ b/netwerk/ipc/PSocketProcess.ipdl @@ -10,13 +10,13 @@ include protocol PWebrtcTCPSocket; include protocol PHttpTransaction; include protocol PHttpConnectionMgr; include protocol PInputChannelThrottleQueue; +include protocol PBackgroundStarter; include protocol PAltService; include protocol PAltSvcTransaction; include protocol PTRRService; include protocol PProxyConfigLookup; include protocol PNativeDNSResolverOverride; include protocol PProxyAutoConfig; -include protocol PSocketProcessBackground; #if defined(MOZ_SANDBOX) && defined(MOZ_DEBUG) && defined(ENABLE_TESTS) include protocol PSandboxTesting; @@ -25,6 +25,7 @@ include protocol PSandboxTesting; include MemoryReportTypes; include NeckoChannelParams; include PrefsTypes; +include PSMIPCTypes; include "mozilla/ipc/ByteBufUtils.h"; @@ -120,7 +121,7 @@ parent: PRTime aTimestamp, uint64_t aExtraSizeData, nsCString aExtraStringData); - async InitSocketBackground(Endpoint aEndpoint); + async InitBackground(Endpoint aEndpoint); async PAltService(); async PProxyConfigLookup(nullable nsIURI aUri, uint32_t aFlags); async CachePushCheck(nullable nsIURI aPushedURL, diff --git a/netwerk/ipc/PSocketProcessBackground.ipdl b/netwerk/ipc/PSocketProcessBackground.ipdl deleted file mode 100644 index 5d18d311b028..000000000000 --- a/netwerk/ipc/PSocketProcessBackground.ipdl +++ /dev/null @@ -1,57 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=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 protocol PVerifySSLServerCert; -include protocol PSelectTLSClientAuthCert; -include protocol PIPCClientCerts; -include protocol PWebSocketConnection; - -include PSMIPCTypes; - -using mozilla::OriginAttributes from "mozilla/ipc/BackgroundUtils.h"; - -namespace mozilla { -namespace net { - -/** - * PSocketProcessBackground is the top level IPC protocol between - * a background task queue in the socket process and the parent process. - * We use this to create several IPC protocols between the socket process - * and the parent process. - */ -sync protocol PSocketProcessBackground -{ -parent: - async InitVerifySSLServerCert( - Endpoint aEndpoint, - ByteArray[] aPeerCertChain, - nsCString aHostName, - int32_t aPort, - OriginAttributes aOriginAttributes, - ByteArray? aStapledOCSPResponse, - ByteArray? aSctsFromTLSExtension, - DelegatedCredentialInfoArg? aDcInfo, - uint32_t aProviderFlags, - uint32_t aCertVerifierFlags); - - async InitSelectTLSClientAuthCert( - Endpoint aEndpoint, - nsCString aHostName, - OriginAttributes aOriginAttributes, - int32_t aPort, - uint32_t aProviderFlags, - uint32_t aProviderTlsFlags, - ByteArray aServerCertBytes, - ByteArray[] aCANames); - - async InitIPCClientCerts(Endpoint aEndpoint); - - async InitWebSocketConnection(Endpoint aEndpoint, - uint32_t aListenerId); -}; - -} // namespace net -} // namespace mozilla diff --git a/netwerk/ipc/PSocketProcessBridge.ipdl b/netwerk/ipc/PSocketProcessBridge.ipdl index 4c2c6a06af90..3dd4035b9ede 100644 --- a/netwerk/ipc/PSocketProcessBridge.ipdl +++ b/netwerk/ipc/PSocketProcessBridge.ipdl @@ -4,17 +4,15 @@ * 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 protocol PBackgroundDataBridge; -include protocol PMediaTransport; +include protocol PBackgroundStarter; namespace mozilla { namespace net { /** - * PSocketProcessBridge is the IPC protocol between the main thread in - * the content process and the main thread in the socket process. - * We use this to create PBackgroundDataBridge and PMediaTransport protocols - * between content process and socket process. + * PSocketProcessBridge is the IPC protocol between content process and + * socket process. This protocol allows socket process to send data to + * content process bypassing parent process. * Once created, PSocketProcessBridgeChild is the actor that lives in * content process and PSocketProcessBridgeParent lives in * socket process. @@ -22,19 +20,11 @@ namespace net { [NeedsOtherPid] sync protocol PSocketProcessBridge { -parent: - /** - * For setting up PBackgroundDataBridge protocol, we use this message to - * create a background task queue and the BackgroundDataBridgeParent actor in - * socket process. - */ - async InitBackgroundDataBridge(Endpoint aEndpoint, - uint64_t aChannelID); - /** - * Similar to the above, this message is for PMediaTransport. - */ - async InitMediaTransport(Endpoint aEndpoint); +parent: + async InitBackground(Endpoint aEndpoint); +both: + async Test(); }; } diff --git a/netwerk/ipc/SocketProcessBackgroundChild.cpp b/netwerk/ipc/SocketProcessBackgroundChild.cpp deleted file mode 100644 index d3835ad0f5bd..000000000000 --- a/netwerk/ipc/SocketProcessBackgroundChild.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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 "SocketProcessBackgroundChild.h" -#include "SocketProcessLogging.h" - -#include "mozilla/ipc/Endpoint.h" -#include "nsThreadUtils.h" - -namespace mozilla::net { - -StaticMutex SocketProcessBackgroundChild::sMutex; -StaticRefPtr - SocketProcessBackgroundChild::sInstance; -StaticRefPtr SocketProcessBackgroundChild::sTaskQueue; - -// static -RefPtr -SocketProcessBackgroundChild::GetSingleton() { - StaticMutexAutoLock lock(sMutex); - return sInstance; -} - -// static -void SocketProcessBackgroundChild::Create( - ipc::Endpoint&& aEndpoint) { - if (NS_WARN_IF(!aEndpoint.IsValid())) { - MOZ_ASSERT_UNREACHABLE( - "Can't create SocketProcessBackgroundChild with invalid endpoint"); - return; - } - - nsCOMPtr transportQueue; - if (NS_WARN_IF(NS_FAILED(NS_CreateBackgroundTaskQueue( - "SocketBackgroundChildQueue", getter_AddRefs(transportQueue))))) { - return; - } - - RefPtr actor = - new SocketProcessBackgroundChild(); - - transportQueue->Dispatch(NS_NewRunnableFunction( - "BindSocketBackgroundChild", - [endpoint = std::move(aEndpoint), actor]() mutable { - // We checked endpoint validity before the dispatch, so this cannot - // fail. - MOZ_ALWAYS_TRUE(endpoint.Bind(actor)); - })); - - // Immediately store the actor and queue into the global. - // Any messages dispatched to the queue will arrive after it has been bound. - LOG(("SocketProcessBackgroundChild::Create")); - StaticMutexAutoLock lock(sMutex); - MOZ_ASSERT(!sInstance && !sTaskQueue, - "Cannot initialize SocketProcessBackgroundChild twice!"); - sInstance = actor; - sTaskQueue = transportQueue; -} - -// static -void SocketProcessBackgroundChild::Shutdown() { - nsCOMPtr taskQueue = TaskQueue(); - if (!taskQueue) { - return; - } - - taskQueue->Dispatch( - NS_NewRunnableFunction("SocketProcessBackgroundChild::Shutdown", []() { - LOG(("SocketProcessBackgroundChild::Shutdown")); - StaticMutexAutoLock lock(sMutex); - sInstance->Close(); - sInstance = nullptr; - sTaskQueue = nullptr; - })); -} - -// static -already_AddRefed -SocketProcessBackgroundChild::TaskQueue() { - StaticMutexAutoLock lock(sMutex); - return do_AddRef(sTaskQueue); -} - -// static -nsresult SocketProcessBackgroundChild::WithActor( - const char* aName, - MoveOnlyFunction aCallback) { - nsCOMPtr taskQueue = TaskQueue(); - if (!taskQueue) { - return NS_ERROR_NOT_AVAILABLE; - } - - return taskQueue->Dispatch(NS_NewRunnableFunction( - aName, [callback = std::move(aCallback)]() mutable { - RefPtr actor = - SocketProcessBackgroundChild::GetSingleton(); - if (actor) { - callback(actor); - } - })); -} - -SocketProcessBackgroundChild::SocketProcessBackgroundChild() { - LOG(("SocketProcessBackgroundChild ctor")); -} - -SocketProcessBackgroundChild::~SocketProcessBackgroundChild() { - LOG(("SocketProcessBackgroundChild dtor")); -} - -} // namespace mozilla::net diff --git a/netwerk/ipc/SocketProcessBackgroundChild.h b/netwerk/ipc/SocketProcessBackgroundChild.h deleted file mode 100644 index b83c8dbcc116..000000000000 --- a/netwerk/ipc/SocketProcessBackgroundChild.h +++ /dev/null @@ -1,45 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef mozilla_net_SocketProcessBackgroundChild_h -#define mozilla_net_SocketProcessBackgroundChild_h - -#include "mozilla/MoveOnlyFunction.h" -#include "mozilla/net/PSocketProcessBackgroundChild.h" -#include "mozilla/StaticMutex.h" -#include "mozilla/StaticPtr.h" - -namespace mozilla { -namespace net { - -class SocketProcessBackgroundChild final - : public PSocketProcessBackgroundChild { - public: - NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessBackgroundChild, final) - - static void Create(ipc::Endpoint&& aEndpoint); - static void Shutdown(); - // |aCallback| will be called with the actor asynchronously on |sTaskQueue|. - static nsresult WithActor( - const char* aName, - MoveOnlyFunction aCallback); - - private: - SocketProcessBackgroundChild(); - virtual ~SocketProcessBackgroundChild(); - - static RefPtr GetSingleton(); - static already_AddRefed TaskQueue(); - - static StaticMutex sMutex; - static StaticRefPtr sInstance - MOZ_GUARDED_BY(sMutex); - static StaticRefPtr sTaskQueue MOZ_GUARDED_BY(sMutex); -}; - -} // namespace net -} // namespace mozilla - -#endif // mozilla_net_SocketProcessBackgroundChild_h diff --git a/netwerk/ipc/SocketProcessBackgroundParent.cpp b/netwerk/ipc/SocketProcessBackgroundParent.cpp deleted file mode 100644 index d53e0f1f9cb6..000000000000 --- a/netwerk/ipc/SocketProcessBackgroundParent.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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 "SocketProcessBackgroundParent.h" -#include "SocketProcessLogging.h" - -#include "mozilla/net/HttpConnectionMgrParent.h" -#include "mozilla/net/WebSocketConnectionParent.h" -#include "mozilla/psm/IPCClientCertsParent.h" -#include "mozilla/psm/VerifySSLServerCertParent.h" -#include "mozilla/psm/SelectTLSClientAuthCertParent.h" -#include "nsIHttpChannelInternal.h" - -namespace mozilla::net { - -SocketProcessBackgroundParent::SocketProcessBackgroundParent() { - LOG(("SocketProcessBackgroundParent ctor")); -} - -SocketProcessBackgroundParent::~SocketProcessBackgroundParent() { - LOG(("SocketProcessBackgroundParent dtor")); -} - -mozilla::ipc::IPCResult -SocketProcessBackgroundParent::RecvInitVerifySSLServerCert( - Endpoint&& aEndpoint, - nsTArray&& aPeerCertChain, const nsACString& aHostName, - const int32_t& aPort, const OriginAttributes& aOriginAttributes, - const Maybe& aStapledOCSPResponse, - const Maybe& aSctsFromTLSExtension, - const Maybe& aDcInfo, - const uint32_t& aProviderFlags, const uint32_t& aCertVerifierFlags) { - LOG(("SocketProcessBackgroundParent::RecvInitVerifySSLServerCert\n")); - if (!aEndpoint.IsValid()) { - return IPC_FAIL(this, "Invalid endpoint"); - } - - nsCOMPtr transportQueue; - if (NS_FAILED(NS_CreateBackgroundTaskQueue("VerifySSLServerCert", - getter_AddRefs(transportQueue)))) { - return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed"); - } - - transportQueue->Dispatch(NS_NewRunnableFunction( - "InitVerifySSLServerCert", - [endpoint = std::move(aEndpoint), - peerCertChain = std::move(aPeerCertChain), - hostName = nsCString(aHostName), port(aPort), - originAttributes(aOriginAttributes), - stapledOCSPResponse = std::move(aStapledOCSPResponse), - sctsFromTLSExtension = std::move(aSctsFromTLSExtension), - dcInfo = std::move(aDcInfo), providerFlags(aProviderFlags), - certVerifierFlags(aCertVerifierFlags)]() mutable { - RefPtr parent = - new psm::VerifySSLServerCertParent(); - if (!endpoint.Bind(parent)) { - return; - } - - parent->Dispatch(std::move(peerCertChain), hostName, port, - originAttributes, stapledOCSPResponse, - sctsFromTLSExtension, dcInfo, providerFlags, - certVerifierFlags); - })); - - return IPC_OK(); -} - -mozilla::ipc::IPCResult -SocketProcessBackgroundParent::RecvInitSelectTLSClientAuthCert( - Endpoint&& aEndpoint, - const nsACString& aHostName, const OriginAttributes& aOriginAttributes, - const int32_t& aPort, const uint32_t& aProviderFlags, - const uint32_t& aProviderTlsFlags, const ByteArray& aServerCertBytes, - nsTArray&& aCANames) { - LOG(("SocketProcessBackgroundParent::RecvInitSelectTLSClientAuthCert\n")); - if (!aEndpoint.IsValid()) { - return IPC_FAIL(this, "Invalid endpoint"); - } - - nsCOMPtr transportQueue; - if (NS_FAILED(NS_CreateBackgroundTaskQueue("SelectTLSClientAuthCert", - getter_AddRefs(transportQueue)))) { - return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed"); - } - - transportQueue->Dispatch(NS_NewRunnableFunction( - "InitSelectTLSClientAuthCert", - [endpoint = std::move(aEndpoint), hostName = nsCString(aHostName), - originAttributes(aOriginAttributes), port(aPort), - providerFlags(aProviderFlags), providerTlsFlags(aProviderTlsFlags), - serverCertBytes(aServerCertBytes), - CANAMEs(std::move(aCANames))]() mutable { - RefPtr parent = - new psm::SelectTLSClientAuthCertParent(); - if (!endpoint.Bind(parent)) { - return; - } - - parent->Dispatch(hostName, originAttributes, port, providerFlags, - providerTlsFlags, serverCertBytes, std::move(CANAMEs)); - })); - - return IPC_OK(); -} - -mozilla::ipc::IPCResult SocketProcessBackgroundParent::RecvInitIPCClientCerts( - Endpoint&& aEndpoint) { - LOG(("SocketProcessBackgroundParent::RecvInitIPCClientCerts\n")); - if (!aEndpoint.IsValid()) { - return IPC_FAIL(this, "Invalid endpoint"); - } - - nsCOMPtr transportQueue; - if (NS_FAILED(NS_CreateBackgroundTaskQueue("IPCClientCerts", - getter_AddRefs(transportQueue)))) { - return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed"); - } - - transportQueue->Dispatch(NS_NewRunnableFunction( - "InitIPCClientCerts", [endpoint = std::move(aEndpoint)]() mutable { - RefPtr parent = - new psm::IPCClientCertsParent(); - endpoint.Bind(parent); - })); - return IPC_OK(); -} - -mozilla::ipc::IPCResult -SocketProcessBackgroundParent::RecvInitWebSocketConnection( - Endpoint&& aEndpoint, - const uint32_t& aListenerId) { - LOG(("SocketProcessBackgroundParent::RecvInitWebSocketConnection\n")); - if (!aEndpoint.IsValid()) { - return IPC_FAIL(this, "Invalid endpoint"); - } - - nsCOMPtr transportQueue; - if (NS_FAILED(NS_CreateBackgroundTaskQueue("WebSocketConnection", - getter_AddRefs(transportQueue)))) { - return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed"); - } - - transportQueue->Dispatch(NS_NewRunnableFunction( - "InitWebSocketConnection", - [endpoint = std::move(aEndpoint), aListenerId]() mutable { - Maybe> listener = - net::HttpConnectionMgrParent::GetAndRemoveHttpUpgradeListener( - aListenerId); - if (!listener) { - return; - } - - RefPtr actor = - new WebSocketConnectionParent(*listener); - endpoint.Bind(actor); - })); - return IPC_OK(); -} - -void SocketProcessBackgroundParent::ActorDestroy(ActorDestroyReason aReason) { - LOG(("SocketProcessBackgroundParent::ActorDestroy")); -} - -} // namespace mozilla::net diff --git a/netwerk/ipc/SocketProcessBackgroundParent.h b/netwerk/ipc/SocketProcessBackgroundParent.h deleted file mode 100644 index 603992dfddaa..000000000000 --- a/netwerk/ipc/SocketProcessBackgroundParent.h +++ /dev/null @@ -1,52 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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/. */ - -#ifndef mozilla_net_SocketProcessBackgroundParent_h -#define mozilla_net_SocketProcessBackgroundParent_h - -#include "mozilla/net/PSocketProcessBackgroundParent.h" - -namespace mozilla { -namespace net { - -class SocketProcessBackgroundParent final - : public PSocketProcessBackgroundParent { - public: - NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessBackgroundParent, final) - - SocketProcessBackgroundParent(); - - mozilla::ipc::IPCResult RecvInitVerifySSLServerCert( - Endpoint&& aEndpoint, - nsTArray&& aPeerCertChain, const nsACString& aHostName, - const int32_t& aPort, const OriginAttributes& aOriginAttributes, - const Maybe& aStapledOCSPResponse, - const Maybe& aSctsFromTLSExtension, - const Maybe& aDcInfo, - const uint32_t& aProviderFlags, const uint32_t& aCertVerifierFlags); - - mozilla::ipc::IPCResult RecvInitIPCClientCerts( - Endpoint&& aEndpoint); - - mozilla::ipc::IPCResult RecvInitSelectTLSClientAuthCert( - Endpoint&& aEndpoint, - const nsACString& aHostName, const OriginAttributes& aOriginAttributes, - const int32_t& aPort, const uint32_t& aProviderFlags, - const uint32_t& aProviderTlsFlags, const ByteArray& aServerCertBytes, - nsTArray&& aCANames); - mozilla::ipc::IPCResult RecvInitWebSocketConnection( - Endpoint&& aEndpoint, - const uint32_t& aListenerId); - - void ActorDestroy(ActorDestroyReason aReason) override; - - private: - ~SocketProcessBackgroundParent(); -}; - -} // namespace net -} // namespace mozilla - -#endif // mozilla_net_SocketProcessBackgroundParent_h diff --git a/netwerk/ipc/SocketProcessBridgeChild.cpp b/netwerk/ipc/SocketProcessBridgeChild.cpp index 798ce37bf149..ed4ae04a9075 100644 --- a/netwerk/ipc/SocketProcessBridgeChild.cpp +++ b/netwerk/ipc/SocketProcessBridgeChild.cpp @@ -46,6 +46,9 @@ bool SocketProcessBridgeChild::Create( } sSocketProcessBridgeChild->mSocketProcessPid = aEndpoint.OtherPid(); + + mozilla::ipc::BackgroundChild::InitSocketBridgeStarter( + sSocketProcessBridgeChild); return true; } diff --git a/netwerk/ipc/SocketProcessBridgeParent.cpp b/netwerk/ipc/SocketProcessBridgeParent.cpp index e1a833860447..020d8c2f3bd3 100644 --- a/netwerk/ipc/SocketProcessBridgeParent.cpp +++ b/netwerk/ipc/SocketProcessBridgeParent.cpp @@ -6,87 +6,59 @@ #include "SocketProcessBridgeParent.h" #include "SocketProcessLogging.h" -#include "mozilla/dom/MediaTransportParent.h" #include "mozilla/ipc/BackgroundParent.h" #include "mozilla/ipc/Endpoint.h" #include "SocketProcessChild.h" -#include "mozilla/net/BackgroundDataBridgeParent.h" -#include "nsThreadUtils.h" namespace mozilla { namespace net { -SocketProcessBridgeParent::SocketProcessBridgeParent(ProcessId aId) : mId(aId) { +SocketProcessBridgeParent::SocketProcessBridgeParent(ProcessId aId) + : mId(aId), mClosed(false) { LOG( ("CONSTRUCT SocketProcessBridgeParent::SocketProcessBridgeParent " "mId=%" PRIPID "\n", mId)); + MOZ_COUNT_CTOR(SocketProcessBridgeParent); } SocketProcessBridgeParent::~SocketProcessBridgeParent() { - LOG(("DESTRUCT SocketProcessBridgeParent::SocketProcessBridgeParent\n")); + LOG( + ("DESTRUCT SocketProcessBridgeParent::SocketProcessBridgeParent " + "mId=%" PRIPID "\n", + mId)); + MOZ_COUNT_DTOR(SocketProcessBridgeParent); } -mozilla::ipc::IPCResult SocketProcessBridgeParent::RecvInitBackgroundDataBridge( - mozilla::ipc::Endpoint&& aEndpoint, - uint64_t aChannelID) { - LOG(("SocketProcessBridgeParent::RecvInitBackgroundDataBridge\n")); - - nsCOMPtr transportQueue; - if (NS_FAILED(NS_CreateBackgroundTaskQueue("BackgroundDataBridge", - getter_AddRefs(transportQueue)))) { - return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed"); - } - - if (!aEndpoint.IsValid()) { - return IPC_FAIL(this, "Invalid endpoint"); - } - - transportQueue->Dispatch(NS_NewRunnableFunction( - "BackgroundDataBridgeParent::Bind", - [endpoint = std::move(aEndpoint), aChannelID]() mutable { - RefPtr actor = - new net::BackgroundDataBridgeParent(aChannelID); - endpoint.Bind(actor); - })); +mozilla::ipc::IPCResult SocketProcessBridgeParent::RecvTest() { + LOG(("SocketProcessBridgeParent::RecvTest\n")); + Unused << SendTest(); return IPC_OK(); } -mozilla::ipc::IPCResult SocketProcessBridgeParent::RecvInitMediaTransport( - mozilla::ipc::Endpoint&& aEndpoint) { - LOG(("SocketProcessBridgeParent::RecvInitMediaTransport\n")); - - if (!aEndpoint.IsValid()) { - return IPC_FAIL(this, "Invalid endpoint"); +mozilla::ipc::IPCResult SocketProcessBridgeParent::RecvInitBackground( + Endpoint&& aEndpoint) { + LOG(("SocketProcessBridgeParent::RecvInitBackground mId=%" PRIPID "\n", mId)); + if (!ipc::BackgroundParent::AllocStarter(nullptr, std::move(aEndpoint))) { + return IPC_FAIL(this, "BackgroundParent::Alloc failed"); } - if (!mMediaTransportTaskQueue) { - nsCOMPtr transportQueue; - if (NS_FAILED(NS_CreateBackgroundTaskQueue( - "MediaTransport", getter_AddRefs(transportQueue)))) { - return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed"); - } - - mMediaTransportTaskQueue = std::move(transportQueue); - } - - mMediaTransportTaskQueue->Dispatch(NS_NewRunnableFunction( - "BackgroundDataBridgeParent::Bind", - [endpoint = std::move(aEndpoint)]() mutable { - RefPtr actor = new MediaTransportParent(); - endpoint.Bind(actor); - })); return IPC_OK(); } -void SocketProcessBridgeParent::ActorDestroy(ActorDestroyReason aReason) { - // See bug 1846478. We might be able to remove this dispatch. - GetCurrentSerialEventTarget()->Dispatch(NS_NewRunnableFunction( - "SocketProcessBridgeParent::ActorDestroy", [id = mId] { - if (SocketProcessChild* child = SocketProcessChild::GetSingleton()) { - child->DestroySocketProcessBridgeParent(id); - } - })); +void SocketProcessBridgeParent::ActorDestroy(ActorDestroyReason aWhy) { + LOG(("SocketProcessBridgeParent::ActorDestroy mId=%" PRIPID "\n", mId)); + + mClosed = true; + GetCurrentSerialEventTarget()->Dispatch( + NewRunnableMethod("net::SocketProcessBridgeParent::DeferredDestroy", this, + &SocketProcessBridgeParent::DeferredDestroy)); +} + +void SocketProcessBridgeParent::DeferredDestroy() { + if (SocketProcessChild* child = SocketProcessChild::GetSingleton()) { + child->DestroySocketProcessBridgeParent(mId); + } } } // namespace net diff --git a/netwerk/ipc/SocketProcessBridgeParent.h b/netwerk/ipc/SocketProcessBridgeParent.h index c3d116db10f3..7dbedc5c4d52 100644 --- a/netwerk/ipc/SocketProcessBridgeParent.h +++ b/netwerk/ipc/SocketProcessBridgeParent.h @@ -21,19 +21,20 @@ class SocketProcessBridgeParent final : public PSocketProcessBridgeParent { explicit SocketProcessBridgeParent(ProcessId aId); - mozilla::ipc::IPCResult RecvInitBackgroundDataBridge( - Endpoint&& aEndpoint, uint64_t aChannelID); + mozilla::ipc::IPCResult RecvTest(); + mozilla::ipc::IPCResult RecvInitBackground( + Endpoint&& aEndpoint); - mozilla::ipc::IPCResult RecvInitMediaTransport( - Endpoint&& aEndpoint); + void ActorDestroy(ActorDestroyReason aWhy) override; + void DeferredDestroy(); - void ActorDestroy(ActorDestroyReason aReason) override; + bool Closed() const { return mClosed; } private: ~SocketProcessBridgeParent(); - nsCOMPtr mMediaTransportTaskQueue; ProcessId mId; + bool mClosed; }; } // namespace net diff --git a/netwerk/ipc/SocketProcessChild.cpp b/netwerk/ipc/SocketProcessChild.cpp index e678677192af..54b2fc9db994 100644 --- a/netwerk/ipc/SocketProcessChild.cpp +++ b/netwerk/ipc/SocketProcessChild.cpp @@ -18,6 +18,8 @@ #include "mozilla/FOGIPC.h" #include "mozilla/glean/GleanMetrics.h" #include "mozilla/ipc/CrashReporterClient.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "mozilla/ipc/BackgroundParent.h" #include "mozilla/ipc/ProcessChild.h" #include "mozilla/net/AltSvcTransactionChild.h" #include "mozilla/net/BackgroundDataBridgeParent.h" @@ -25,7 +27,6 @@ #include "mozilla/net/DNSRequestParent.h" #include "mozilla/net/NativeDNSResolverOverrideChild.h" #include "mozilla/net/ProxyAutoConfigChild.h" -#include "mozilla/net/SocketProcessBackgroundChild.h" #include "mozilla/net/TRRServiceChild.h" #include "mozilla/ipc/ProcessUtils.h" #include "mozilla/Preferences.h" @@ -104,46 +105,6 @@ void CGSShutdownServerConnections(); }; #endif -void SocketProcessChild::InitSocketBackground() { - Endpoint parentEndpoint; - Endpoint childEndpoint; - if (NS_WARN_IF(NS_FAILED(PSocketProcessBackground::CreateEndpoints( - &parentEndpoint, &childEndpoint)))) { - return; - } - - SocketProcessBackgroundChild::Create(std::move(childEndpoint)); - - Unused << SendInitSocketBackground(std::move(parentEndpoint)); -} - -namespace { - -class NetTeardownObserver final : public nsIObserver { - public: - NetTeardownObserver() = default; - - NS_DECL_ISUPPORTS - NS_DECL_NSIOBSERVER - - private: - ~NetTeardownObserver() = default; -}; - -NS_IMPL_ISUPPORTS(NetTeardownObserver, nsIObserver) - -NS_IMETHODIMP -NetTeardownObserver::Observe(nsISupports* aSubject, const char* aTopic, - const char16_t* aData) { - if (SocketProcessChild* child = SocketProcessChild::GetSingleton()) { - child->CloseIPCClientCertsActor(); - } - - return NS_OK; -} - -} // namespace - bool SocketProcessChild::Init(mozilla::ipc::UntypedEndpoint&& aEndpoint, const char* aParentBuildID) { if (NS_WARN_IF(NS_FAILED(nsThreadManager::get().Init()))) { @@ -169,7 +130,8 @@ bool SocketProcessChild::Init(mozilla::ipc::UntypedEndpoint&& aEndpoint, return false; } - InitSocketBackground(); + BackgroundChild::Startup(); + BackgroundChild::InitSocketStarter(this); SetThisProcessName("Socket Process"); #if defined(XP_MACOSX) @@ -202,27 +164,13 @@ bool SocketProcessChild::Init(mozilla::ipc::UntypedEndpoint&& aEndpoint, return false; } - nsCOMPtr obs = services::GetObserverService(); - if (obs) { - nsCOMPtr observer = new NetTeardownObserver(); - Unused << obs->AddObserver(observer, "profile-change-net-teardown", false); - } - - mSocketThread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID); - if (!mSocketThread) { - return false; - } - return true; } void SocketProcessChild::ActorDestroy(ActorDestroyReason aWhy) { LOG(("SocketProcessChild::ActorDestroy\n")); - { - MutexAutoLock lock(mMutex); - mShuttingDown = true; - } + mShuttingDown = true; if (AbnormalShutdown == aWhy) { NS_WARNING("Shutting down Socket process early due to a crash!"); @@ -245,10 +193,8 @@ void SocketProcessChild::ActorDestroy(ActorDestroyReason aWhy) { void SocketProcessChild::CleanUp() { LOG(("SocketProcessChild::CleanUp\n")); - SocketProcessBackgroundChild::Shutdown(); - for (const auto& parent : mSocketProcessBridgeParentMap.Values()) { - if (parent->CanSend()) { + if (!parent->Closed()) { parent->Close(); } } @@ -510,11 +456,13 @@ mozilla::ipc::IPCResult SocketProcessChild::RecvPDNSRequestConstructor( void SocketProcessChild::AddDataBridgeToMap( uint64_t aChannelId, BackgroundDataBridgeParent* aActor) { + ipc::AssertIsOnBackgroundThread(); MutexAutoLock lock(mMutex); mBackgroundDataBridgeMap.InsertOrUpdate(aChannelId, RefPtr{aActor}); } void SocketProcessChild::RemoveDataBridgeFromMap(uint64_t aChannelId) { + ipc::AssertIsOnBackgroundThread(); MutexAutoLock lock(mMutex); mBackgroundDataBridgeMap.Remove(aChannelId); } @@ -763,66 +711,5 @@ SocketProcessChild::RecvUnblockUntrustedModulesThread() { } #endif // defined(XP_WIN) -bool SocketProcessChild::IsShuttingDown() { - MutexAutoLock lock(mMutex); - return mShuttingDown; -} - -void SocketProcessChild::CloseIPCClientCertsActor() { - LOG(("SocketProcessChild::CloseIPCClientCertsActor")); - MOZ_ASSERT(NS_IsMainThread()); - - mSocketThread->Dispatch(NS_NewRunnableFunction( - "CloseIPCClientCertsActor", [self = RefPtr{this}]() { - LOG(("CloseIPCClientCertsActor")); - if (self->mIPCClientCertsChild) { - self->mIPCClientCertsChild->Close(); - self->mIPCClientCertsChild = nullptr; - } - })); -} - -already_AddRefed -SocketProcessChild::GetIPCClientCertsActor() { - LOG(("SocketProcessChild::GetIPCClientCertsActor")); - // Only socket thread can access the mIPCClientCertsChild. - if (!OnSocketThread()) { - return nullptr; - } - - { - MutexAutoLock lock(mMutex); - if (mShuttingDown) { - return nullptr; - } - } - - if (mIPCClientCertsChild) { - RefPtr actorChild = mIPCClientCertsChild; - return actorChild.forget(); - } - - ipc::Endpoint parentEndpoint; - ipc::Endpoint childEndpoint; - psm::PIPCClientCerts::CreateEndpoints(&parentEndpoint, &childEndpoint); - - if (NS_FAILED(SocketProcessBackgroundChild::WithActor( - "SendInitIPCClientCerts", - [endpoint = std::move(parentEndpoint)]( - SocketProcessBackgroundChild* aActor) mutable { - Unused << aActor->SendInitIPCClientCerts(std::move(endpoint)); - }))) { - return nullptr; - } - - RefPtr actor = new psm::IPCClientCertsChild(); - if (!childEndpoint.Bind(actor)) { - return nullptr; - } - - mIPCClientCertsChild = actor; - return actor.forget(); -} - } // namespace net } // namespace mozilla diff --git a/netwerk/ipc/SocketProcessChild.h b/netwerk/ipc/SocketProcessChild.h index fbcac7462de5..ed4b235ca46d 100644 --- a/netwerk/ipc/SocketProcessChild.h +++ b/netwerk/ipc/SocketProcessChild.h @@ -8,7 +8,6 @@ #include "mozilla/net/PSocketProcessChild.h" #include "mozilla/ipc/InputStreamUtils.h" -#include "mozilla/psm/IPCClientCertsChild.h" #include "mozilla/Mutex.h" #include "nsRefPtrHashtable.h" #include "nsTHashMap.h" @@ -25,7 +24,6 @@ namespace mozilla { namespace net { class ProxyAutoConfigChild; -class SocketProcessBackgroundChild; class SocketProcessBridgeParent; class BackgroundDataBridgeParent; @@ -92,7 +90,7 @@ class SocketProcessChild final : public PSocketProcessChild { already_AddRefed AllocPAltSvcTransactionChild( const HttpConnectionInfoCloneArgs& aConnInfo, const uint32_t& aCaps); - bool IsShuttingDown(); + bool IsShuttingDown() { return mShuttingDown; } already_AddRefed AllocPDNSRequestChild( const nsACString& aHost, const nsACString& aTrrServer, @@ -153,15 +151,10 @@ class SocketProcessChild final : public PSocketProcessChild { mozilla::ipc::IPCResult RecvUnblockUntrustedModulesThread(); #endif // defined(XP_WIN) - already_AddRefed GetIPCClientCertsActor(); - void CloseIPCClientCertsActor(); - protected: friend class SocketProcessImpl; ~SocketProcessChild(); - void InitSocketBackground(); - private: // Mapping of content process id and the SocketProcessBridgeParent. // This table keeps SocketProcessBridgeParent alive in socket process. @@ -170,16 +163,11 @@ class SocketProcessChild final : public PSocketProcessChild { RefPtr mProfilerController; + bool mShuttingDown{false}; // Protect the table below. Mutex mMutex MOZ_UNANNOTATED{"SocketProcessChild::mMutex"}; nsTHashMap> mBackgroundDataBridgeMap; - - bool mShuttingDown MOZ_GUARDED_BY(mMutex) = false; - - nsCOMPtr mSocketThread; - // mIPCClientCertsChild is only accessed on the socket thread. - RefPtr mIPCClientCertsChild; }; } // namespace net diff --git a/netwerk/ipc/SocketProcessParent.cpp b/netwerk/ipc/SocketProcessParent.cpp index 3bf3c7c7ba8b..dabd2a66ce55 100644 --- a/netwerk/ipc/SocketProcessParent.cpp +++ b/netwerk/ipc/SocketProcessParent.cpp @@ -16,7 +16,6 @@ #include "mozilla/FOGIPC.h" #include "mozilla/net/DNSRequestParent.h" #include "mozilla/net/ProxyConfigLookupParent.h" -#include "mozilla/net/SocketProcessBackgroundParent.h" #include "mozilla/RemoteLazyInputStreamParent.h" #include "mozilla/Telemetry.h" #include "mozilla/TelemetryIPC.h" @@ -248,25 +247,13 @@ mozilla::ipc::IPCResult SocketProcessParent::RecvObserveHttpActivity( return IPC_OK(); } -mozilla::ipc::IPCResult SocketProcessParent::RecvInitSocketBackground( - Endpoint&& aEndpoint) { - if (!aEndpoint.IsValid()) { - return IPC_FAIL(this, "Invalid endpoint"); +mozilla::ipc::IPCResult SocketProcessParent::RecvInitBackground( + Endpoint&& aEndpoint) { + LOG(("SocketProcessParent::RecvInitBackground\n")); + if (!ipc::BackgroundParent::AllocStarter(nullptr, std::move(aEndpoint))) { + return IPC_FAIL(this, "BackgroundParent::Alloc failed"); } - nsCOMPtr transportQueue; - if (NS_FAILED(NS_CreateBackgroundTaskQueue("SocketBackgroundParentQueue", - getter_AddRefs(transportQueue)))) { - return IPC_FAIL(this, "NS_CreateBackgroundTaskQueue failed"); - } - - transportQueue->Dispatch( - NS_NewRunnableFunction("BindSocketBackgroundParent", - [endpoint = std::move(aEndpoint)]() mutable { - RefPtr parent = - new SocketProcessBackgroundParent(); - endpoint.Bind(parent); - })); return IPC_OK(); } diff --git a/netwerk/ipc/SocketProcessParent.h b/netwerk/ipc/SocketProcessParent.h index 732e431e35e9..fb7840288675 100644 --- a/netwerk/ipc/SocketProcessParent.h +++ b/netwerk/ipc/SocketProcessParent.h @@ -7,6 +7,7 @@ #define mozilla_net_SocketProcessParent_h #include "mozilla/UniquePtr.h" +#include "mozilla/ipc/BackgroundParent.h" #include "mozilla/ipc/CrashReporterHelper.h" #include "mozilla/ipc/InputStreamUtils.h" #include "mozilla/net/PSocketProcessParent.h" @@ -75,11 +76,18 @@ class SocketProcessParent final const uint32_t& aActivitySubtype, const PRTime& aTimestamp, const uint64_t& aExtraSizeData, const nsACString& aExtraStringData); - mozilla::ipc::IPCResult RecvInitSocketBackground( - Endpoint&& aEndpoint); + mozilla::ipc::IPCResult RecvInitBackground( + Endpoint&& aEndpoint); already_AddRefed AllocPAltServiceParent(); + mozilla::ipc::IPCResult RecvFindIPCClientCertObjects( + nsTArray* aObjects); + mozilla::ipc::IPCResult RecvIPCClientCertSign(ByteArray aCert, + ByteArray aData, + ByteArray aParams, + ByteArray* aSignature); + already_AddRefed AllocPProxyConfigLookupParent( nsIURI* aURI, const uint32_t& aProxyResolveFlags); mozilla::ipc::IPCResult RecvPProxyConfigLookupConstructor( diff --git a/netwerk/ipc/moz.build b/netwerk/ipc/moz.build index 401c0621b66f..16f164d638b4 100644 --- a/netwerk/ipc/moz.build +++ b/netwerk/ipc/moz.build @@ -24,8 +24,6 @@ EXPORTS.mozilla.net += [ "ProxyConfigLookup.h", "ProxyConfigLookupChild.h", "ProxyConfigLookupParent.h", - "SocketProcessBackgroundChild.h", - "SocketProcessBackgroundParent.h", "SocketProcessBridgeChild.h", "SocketProcessBridgeParent.h", "SocketProcessChild.h", @@ -51,8 +49,6 @@ UNIFIED_SOURCES += [ "ProxyConfigLookup.cpp", "ProxyConfigLookupChild.cpp", "ProxyConfigLookupParent.cpp", - "SocketProcessBackgroundChild.cpp", - "SocketProcessBackgroundParent.cpp", "SocketProcessBridgeChild.cpp", "SocketProcessBridgeParent.cpp", "SocketProcessChild.cpp", @@ -81,7 +77,6 @@ IPDL_SOURCES = [ "PProxyAutoConfig.ipdl", "PProxyConfigLookup.ipdl", "PSimpleChannel.ipdl", - "PSocketProcessBackground.ipdl", "PSocketProcessBridge.ipdl", ] diff --git a/netwerk/protocol/http/BackgroundDataBridgeChild.cpp b/netwerk/protocol/http/BackgroundDataBridgeChild.cpp index 218d79db3a21..77ba36ba417c 100644 --- a/netwerk/protocol/http/BackgroundDataBridgeChild.cpp +++ b/netwerk/protocol/http/BackgroundDataBridgeChild.cpp @@ -27,7 +27,7 @@ mozilla::ipc::IPCResult BackgroundDataBridgeChild::RecvOnTransportAndData( } if (mBgChild->ChannelClosed()) { - Close(); + Unused << Send__delete__(this); return IPC_OK(); } @@ -44,7 +44,7 @@ mozilla::ipc::IPCResult BackgroundDataBridgeChild::RecvOnStopRequest( } if (mBgChild->ChannelClosed()) { - Close(); + Unused << Send__delete__(this); return IPC_OK(); } diff --git a/netwerk/protocol/http/BackgroundDataBridgeParent.cpp b/netwerk/protocol/http/BackgroundDataBridgeParent.cpp index 28736e09e181..f039dc1bc556 100644 --- a/netwerk/protocol/http/BackgroundDataBridgeParent.cpp +++ b/netwerk/protocol/http/BackgroundDataBridgeParent.cpp @@ -10,7 +10,7 @@ namespace mozilla { namespace net { BackgroundDataBridgeParent::BackgroundDataBridgeParent(uint64_t aChannelID) - : mChannelID(aChannelID), mBackgroundThread(GetCurrentSerialEventTarget()) { + : mChannelID(aChannelID), mBackgroundThread(NS_GetCurrentThread()) { if (SocketProcessChild* child = SocketProcessChild::GetSingleton()) { child->AddDataBridgeToMap(aChannelID, this); } @@ -22,9 +22,9 @@ void BackgroundDataBridgeParent::ActorDestroy(ActorDestroyReason aWhy) { } } -already_AddRefed -BackgroundDataBridgeParent::GetBackgroundThread() { - return do_AddRef(mBackgroundThread); +already_AddRefed BackgroundDataBridgeParent::GetBackgroundThread() { + nsCOMPtr thread = mBackgroundThread; + return thread.forget(); } void BackgroundDataBridgeParent::Destroy() { @@ -33,7 +33,7 @@ void BackgroundDataBridgeParent::Destroy() { NS_NewRunnableFunction("BackgroundDataBridgeParent::Destroy", [self]() { if (self->CanSend()) { - self->Close(); + Unused << self->Send__delete__(self); } }), NS_DISPATCH_NORMAL)); @@ -51,7 +51,7 @@ void BackgroundDataBridgeParent::OnStopRequest( if (self->CanSend()) { Unused << self->SendOnStopRequest( aStatus, aTiming, aLastActiveTabOptHit, aResponseTrailers); - self->Close(); + Unused << self->Send__delete__(self); } }), NS_DISPATCH_NORMAL)); diff --git a/netwerk/protocol/http/BackgroundDataBridgeParent.h b/netwerk/protocol/http/BackgroundDataBridgeParent.h index 7828442056ef..15fd4ca4ec1c 100644 --- a/netwerk/protocol/http/BackgroundDataBridgeParent.h +++ b/netwerk/protocol/http/BackgroundDataBridgeParent.h @@ -16,7 +16,7 @@ class BackgroundDataBridgeParent final : public PBackgroundDataBridgeParent { explicit BackgroundDataBridgeParent(uint64_t aChannelID); void ActorDestroy(ActorDestroyReason aWhy) override; - already_AddRefed GetBackgroundThread(); + already_AddRefed GetBackgroundThread(); void Destroy(); void OnStopRequest(nsresult aStatus, const ResourceTimingStructArgs& aTiming, const TimeStamp& aLastActiveTabOptHit, @@ -26,7 +26,7 @@ class BackgroundDataBridgeParent final : public PBackgroundDataBridgeParent { virtual ~BackgroundDataBridgeParent() = default; uint64_t mChannelID; - nsCOMPtr mBackgroundThread; + nsCOMPtr mBackgroundThread; }; } // namespace net diff --git a/netwerk/protocol/http/HttpBackgroundChannelChild.cpp b/netwerk/protocol/http/HttpBackgroundChannelChild.cpp index 1723d05f0c77..4ef2cd11e7de 100644 --- a/netwerk/protocol/http/HttpBackgroundChannelChild.cpp +++ b/netwerk/protocol/http/HttpBackgroundChannelChild.cpp @@ -12,7 +12,6 @@ #include "HttpChannelChild.h" #include "mozilla/ipc/BackgroundChild.h" -#include "mozilla/ipc/Endpoint.h" #include "mozilla/ipc/PBackgroundChild.h" #include "mozilla/IntegerPrintfMacros.h" #include "mozilla/net/BackgroundDataBridgeChild.h" @@ -54,17 +53,23 @@ nsresult HttpBackgroundChannelChild::Init(HttpChannelChild* aChannelChild) { return NS_OK; } -void HttpBackgroundChannelChild::CreateDataBridge( - Endpoint&& aEndpoint) { +void HttpBackgroundChannelChild::CreateDataBridge() { MOZ_ASSERT(OnSocketThread()); if (!mChannelChild) { return; } + PBackgroundChild* actorChild = + BackgroundChild::GetOrCreateSocketActorForCurrentThread(); + if (NS_WARN_IF(!actorChild)) { + return; + } + RefPtr dataBridgeChild = new BackgroundDataBridgeChild(this); - aEndpoint.Bind(dataBridgeChild); + Unused << actorChild->SendPBackgroundDataBridgeConstructor( + dataBridgeChild, mChannelChild->ChannelId()); } void HttpBackgroundChannelChild::OnChannelClosed() { diff --git a/netwerk/protocol/http/HttpBackgroundChannelChild.h b/netwerk/protocol/http/HttpBackgroundChannelChild.h index cc5730be7ec5..7456c5991a53 100644 --- a/netwerk/protocol/http/HttpBackgroundChannelChild.h +++ b/netwerk/protocol/http/HttpBackgroundChannelChild.h @@ -9,7 +9,6 @@ #define mozilla_net_HttpBackgroundChannelChild_h #include "mozilla/net/PHttpBackgroundChannelChild.h" -#include "mozilla/ipc/Endpoint.h" #include "nsIRunnable.h" #include "nsTArray.h" @@ -18,7 +17,6 @@ using mozilla::ipc::IPCResult; namespace mozilla { namespace net { -class PBackgroundDataBridgeChild; class BackgroundDataBridgeChild; class HttpChannelChild; @@ -97,7 +95,7 @@ class HttpBackgroundChannelChild final : public PHttpBackgroundChannelChild { void ActorDestroy(ActorDestroyReason aWhy) override; - void CreateDataBridge(Endpoint&& aEndpoint); + void CreateDataBridge(); private: virtual ~HttpBackgroundChannelChild(); diff --git a/netwerk/protocol/http/HttpChannelChild.cpp b/netwerk/protocol/http/HttpChannelChild.cpp index c09a47de21bf..26588f831a8b 100644 --- a/netwerk/protocol/http/HttpChannelChild.cpp +++ b/netwerk/protocol/http/HttpChannelChild.cpp @@ -8,7 +8,6 @@ // HttpLog.h should generally be included first #include "HttpLog.h" -#include "mozilla/net/PBackgroundDataBridge.h" #include "nsHttp.h" #include "nsICacheEntry.h" #include "mozilla/BasePrincipal.h" @@ -3171,20 +3170,11 @@ void HttpChannelChild::MaybeConnectToSocketProcess() { } SocketProcessBridgeChild::GetSocketProcessBridge()->Then( GetCurrentSerialEventTarget(), __func__, - [bgChild, channelId = ChannelId()]( - const RefPtr& aBridge) { - Endpoint parentEndpoint; - Endpoint childEndpoint; - PBackgroundDataBridge::CreateEndpoints(&parentEndpoint, &childEndpoint); - aBridge->SendInitBackgroundDataBridge(std::move(parentEndpoint), - channelId); - + [bgChild]() { gSocketTransportService->Dispatch( - NS_NewRunnableFunction( - "HttpBackgroundChannelChild::CreateDataBridge", - [bgChild, endpoint = std::move(childEndpoint)]() mutable { - bgChild->CreateDataBridge(std::move(endpoint)); - }), + NewRunnableMethod("HttpBackgroundChannelChild::CreateDataBridge", + bgChild, + &HttpBackgroundChannelChild::CreateDataBridge), NS_DISPATCH_NORMAL); }, []() { NS_WARNING("Failed to create SocketProcessBridgeChild"); }); diff --git a/netwerk/protocol/http/HttpTransactionChild.cpp b/netwerk/protocol/http/HttpTransactionChild.cpp index 69ec175f6771..127d55eb9e3f 100644 --- a/netwerk/protocol/http/HttpTransactionChild.cpp +++ b/netwerk/protocol/http/HttpTransactionChild.cpp @@ -10,6 +10,7 @@ #include "HttpTransactionChild.h" #include "mozilla/ipc/IPCStreamUtils.h" +#include "mozilla/ipc/BackgroundParent.h" #include "mozilla/net/BackgroundDataBridgeParent.h" #include "mozilla/net/ChannelEventQueue.h" #include "mozilla/net/InputChannelThrottleQueueChild.h" @@ -26,6 +27,8 @@ #include "nsSerializationHelper.h" #include "OpaqueResponseUtils.h" +using mozilla::ipc::BackgroundParent; + namespace mozilla::net { NS_IMPL_ISUPPORTS(HttpTransactionChild, nsIRequestObserver, nsIStreamListener, @@ -285,6 +288,7 @@ HttpTransactionChild::OnDataAvailable(nsIRequest* aRequest, return NS_OK; } + ipc::AssertIsOnBackgroundThread(); MOZ_ASSERT(mDataBridgeParent); if (!mDataBridgeParent->CanSend()) { @@ -446,7 +450,7 @@ HttpTransactionChild::OnStartRequest(nsIRequest* aRequest) { if (dataBridgeParent) { mDataBridgeParent = std::move(dataBridgeParent.ref()); - nsCOMPtr backgroundThread = + nsCOMPtr backgroundThread = mDataBridgeParent->GetBackgroundThread(); nsCOMPtr retargetableTransactionPump; retargetableTransactionPump = do_QueryObject(mTransactionPump); diff --git a/netwerk/protocol/http/PBackgroundDataBridge.ipdl b/netwerk/protocol/http/PBackgroundDataBridge.ipdl index afb73e7bd9fa..c5335f64d47d 100644 --- a/netwerk/protocol/http/PBackgroundDataBridge.ipdl +++ b/netwerk/protocol/http/PBackgroundDataBridge.ipdl @@ -2,6 +2,7 @@ * 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 protocol PBackground; include HttpChannelParams; include NeckoChannelParams; @@ -11,6 +12,8 @@ namespace net { //------------------------------------------------------------------- async protocol PBackgroundDataBridge { + manager PBackground; + child: async OnTransportAndData(uint64_t offset, uint32_t count, diff --git a/netwerk/protocol/websocket/PWebSocketConnection.ipdl b/netwerk/protocol/websocket/PWebSocketConnection.ipdl index f2fc4f203ad0..6187c9bfa669 100644 --- a/netwerk/protocol/websocket/PWebSocketConnection.ipdl +++ b/netwerk/protocol/websocket/PWebSocketConnection.ipdl @@ -6,6 +6,8 @@ include "mozilla/ipc/TransportSecurityInfoUtils.h"; +include protocol PBackground; + [RefCounted] using class nsITransportSecurityInfo from "nsITransportSecurityInfo.h"; namespace mozilla { @@ -13,6 +15,8 @@ namespace net { protocol PWebSocketConnection { + manager PBackground; + parent: async OnTransportAvailable(nullable nsITransportSecurityInfo aSecurityInfo); async OnError(nsresult aStatus); diff --git a/netwerk/protocol/websocket/WebSocketConnectionChild.cpp b/netwerk/protocol/websocket/WebSocketConnectionChild.cpp index 409dfae38dd6..543eafebf127 100644 --- a/netwerk/protocol/websocket/WebSocketConnectionChild.cpp +++ b/netwerk/protocol/websocket/WebSocketConnectionChild.cpp @@ -8,7 +8,8 @@ #include "WebSocketConnectionChild.h" #include "WebSocketConnection.h" -#include "mozilla/net/SocketProcessBackgroundChild.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "mozilla/ipc/PBackgroundChild.h" #include "nsISerializable.h" #include "nsITLSSocketControl.h" #include "nsITransportSecurityInfo.h" @@ -31,8 +32,6 @@ WebSocketConnectionChild::~WebSocketConnectionChild() { } void WebSocketConnectionChild::Init(uint32_t aListenerId) { - MOZ_ASSERT(NS_IsMainThread()); - nsresult rv; mSocketThread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv); MOZ_ASSERT(NS_SUCCEEDED(rv)); @@ -40,24 +39,17 @@ void WebSocketConnectionChild::Init(uint32_t aListenerId) { return; } - ipc::Endpoint parentEndpoint; - ipc::Endpoint childEndpoint; - PWebSocketConnection::CreateEndpoints(&parentEndpoint, &childEndpoint); - - if (NS_FAILED(SocketProcessBackgroundChild::WithActor( - "SendInitWebSocketConnection", - [aListenerId, endpoint = std::move(parentEndpoint)]( - SocketProcessBackgroundChild* aActor) mutable { - Unused << aActor->SendInitWebSocketConnection(std::move(endpoint), - aListenerId); - }))) { - return; - } - + RefPtr self = this; mSocketThread->Dispatch(NS_NewRunnableFunction( - "BindWebSocketConnectionChild", - [self = RefPtr{this}, endpoint = std::move(childEndpoint)]() mutable { - endpoint.Bind(self); + "WebSocketConnectionChild::Init", [self, aListenerId]() { + mozilla::ipc::PBackgroundChild* actorChild = mozilla::ipc:: + BackgroundChild::GetOrCreateForSocketParentBridgeForCurrentThread(); + if (!actorChild) { + return; + } + + Unused << actorChild->SendPWebSocketConnectionConstructor(self, + aListenerId); })); } diff --git a/netwerk/protocol/websocket/WebSocketConnectionParent.cpp b/netwerk/protocol/websocket/WebSocketConnectionParent.cpp index e2aa19e6d39e..3470daf94094 100644 --- a/netwerk/protocol/websocket/WebSocketConnectionParent.cpp +++ b/netwerk/protocol/websocket/WebSocketConnectionParent.cpp @@ -7,6 +7,7 @@ #include "WebSocketLog.h" #include "WebSocketConnectionParent.h" +#include "nsIHttpChannelInternal.h" #include "nsITransportSecurityInfo.h" #include "nsSerializationHelper.h" #include "nsThreadUtils.h" @@ -102,10 +103,6 @@ void WebSocketConnectionParent::ActorDestroy(ActorDestroyReason aWhy) { listener->OnError(NS_ERROR_FAILURE); } } - mBackgroundThread->Dispatch(NS_NewRunnableFunction( - "WebSocketConnectionParent::DefereredDestroy", [self = RefPtr{this}]() { - LOG(("WebSocketConnectionParent::DefereredDestroy")); - })); }; nsresult WebSocketConnectionParent::Init( @@ -126,9 +123,7 @@ void WebSocketConnectionParent::Close() { mClosed = true; - auto task = [self = RefPtr{this}]() { - self->PWebSocketConnectionParent::Close(); - }; + auto task = [self = RefPtr{this}]() { Unused << self->Send__delete__(self); }; if (mBackgroundThread->IsOnCurrentThread()) { task(); diff --git a/netwerk/protocol/websocket/WebSocketConnectionParent.h b/netwerk/protocol/websocket/WebSocketConnectionParent.h index 45b031e9cbbf..c7d67e117a7e 100644 --- a/netwerk/protocol/websocket/WebSocketConnectionParent.h +++ b/netwerk/protocol/websocket/WebSocketConnectionParent.h @@ -56,7 +56,7 @@ class WebSocketConnectionParent final : public PWebSocketConnectionParent, nsCOMPtr mUpgradeListener; RefPtr mListener; - nsCOMPtr mBackgroundThread; + nsCOMPtr mBackgroundThread; nsCOMPtr mSecurityInfo; Atomic mClosed{false}; Mutex mMutex MOZ_UNANNOTATED{"WebSocketConnectionParent::mMutex"}; diff --git a/security/manager/ssl/IPCClientCertsParent.cpp b/security/manager/ssl/IPCClientCertsParent.cpp index 12bf5183c5d5..bb8791d0fab6 100644 --- a/security/manager/ssl/IPCClientCertsParent.cpp +++ b/security/manager/ssl/IPCClientCertsParent.cpp @@ -11,6 +11,7 @@ #include "nsNSSIOLayer.h" #include "mozilla/SyncRunnable.h" +#include "mozilla/ipc/BackgroundParent.h" namespace mozilla::psm { diff --git a/security/manager/ssl/IPCClientCertsParent.h b/security/manager/ssl/IPCClientCertsParent.h index 46ce96523bab..09ad589aca05 100644 --- a/security/manager/ssl/IPCClientCertsParent.h +++ b/security/manager/ssl/IPCClientCertsParent.h @@ -12,14 +12,14 @@ namespace mozilla { -namespace net { -class SocketProcessBackgroundParent; -} // namespace net +namespace ipc { +class BackgroundParentImpl; +} // namespace ipc namespace psm { class IPCClientCertsParent final : public PIPCClientCertsParent { - friend class mozilla::net::SocketProcessBackgroundParent; + friend class mozilla::ipc::BackgroundParentImpl; public: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(IPCClientCertsParent) diff --git a/security/manager/ssl/PIPCClientCerts.ipdl b/security/manager/ssl/PIPCClientCerts.ipdl index 51ec0dd3b646..9278ad5715ba 100644 --- a/security/manager/ssl/PIPCClientCerts.ipdl +++ b/security/manager/ssl/PIPCClientCerts.ipdl @@ -3,6 +3,8 @@ * 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 protocol PBackground; + include PSMIPCTypes; using mozilla::OriginAttributes from "mozilla/ipc/BackgroundUtils.h"; @@ -12,6 +14,8 @@ namespace psm { sync protocol PIPCClientCerts { + manager PBackground; + parent: // Called from the socket process to the parent process to find client // certificates and associated keys. diff --git a/security/manager/ssl/PSelectTLSClientAuthCert.ipdl b/security/manager/ssl/PSelectTLSClientAuthCert.ipdl index e78edc1622ae..2270b8b12ce8 100644 --- a/security/manager/ssl/PSelectTLSClientAuthCert.ipdl +++ b/security/manager/ssl/PSelectTLSClientAuthCert.ipdl @@ -5,6 +5,7 @@ * 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 protocol PBackground; include PSMIPCTypes; namespace mozilla { @@ -12,6 +13,8 @@ namespace psm { protocol PSelectTLSClientAuthCert { + manager PBackground; + child: async TLSClientAuthCertSelected(ByteArray aSelectedCertBytes, ByteArray[] aSelectedCertChainBytes); diff --git a/security/manager/ssl/PVerifySSLServerCert.ipdl b/security/manager/ssl/PVerifySSLServerCert.ipdl index 2f178c91797d..5277d00ac665 100644 --- a/security/manager/ssl/PVerifySSLServerCert.ipdl +++ b/security/manager/ssl/PVerifySSLServerCert.ipdl @@ -5,15 +5,16 @@ * 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 protocol PBackground; include PSMIPCTypes; -using mozilla::OriginAttributes from "mozilla/ipc/BackgroundUtils.h"; - namespace mozilla { namespace psm { protocol PVerifySSLServerCert { + manager PBackground; + child: async OnVerifiedSSLServerCertSuccess(ByteArray[] aBuiltCertChain, uint16_t aCertTransparencyStatus, diff --git a/security/manager/ssl/TLSClientAuthCertSelection.cpp b/security/manager/ssl/TLSClientAuthCertSelection.cpp index 04480a394457..8a2e7fd8b634 100644 --- a/security/manager/ssl/TLSClientAuthCertSelection.cpp +++ b/security/manager/ssl/TLSClientAuthCertSelection.cpp @@ -27,8 +27,9 @@ #include "TLSClientAuthCertSelection.h" #include "cert_storage/src/cert_storage.h" #include "mozilla/Logging.h" -#include "mozilla/ipc/Endpoint.h" -#include "mozilla/net/SocketProcessBackgroundChild.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "mozilla/ipc/BackgroundParent.h" +#include "mozilla/ipc/PBackgroundChild.h" #include "mozilla/psm/SelectTLSClientAuthCertChild.h" #include "mozilla/psm/SelectTLSClientAuthCertParent.h" #include "nsArray.h" @@ -749,30 +750,15 @@ SECStatus SSLGetClientAuthDataHook(void* arg, PRFileDesc* socket, hostname(std::move(hostname)), originAttributes(std::move(originAttributes)), port, providerFlags, providerTlsFlags, serverCertBytes(std::move(serverCertBytes)), - caNamesBytes(std::move(caNamesBytes))]() mutable { - ipc::Endpoint parentEndpoint; - ipc::Endpoint childEndpoint; - PSelectTLSClientAuthCert::CreateEndpoints(&parentEndpoint, - &childEndpoint); - if (NS_FAILED(net::SocketProcessBackgroundChild::WithActor( - "SendInitSelectTLSClientAuthCert", - [endpoint = std::move(parentEndpoint), - hostname(std::move(hostname)), - originAttributes(std::move(originAttributes)), port, - providerFlags, providerTlsFlags, - serverCertBytes(std::move(serverCertBytes)), - caNamesBytes(std::move(caNamesBytes))]( - net::SocketProcessBackgroundChild* aActor) mutable { - Unused << aActor->SendInitSelectTLSClientAuthCert( - std::move(endpoint), hostname, originAttributes, - port, providerFlags, providerTlsFlags, - ByteArray(serverCertBytes), caNamesBytes); - }))) { - return; - } - - if (!childEndpoint.Bind(selectClientAuthCertificate)) { - return; + caNamesBytes(std::move(caNamesBytes))]() { + mozilla::ipc::PBackgroundChild* actorChild = + mozilla::ipc::BackgroundChild:: + GetOrCreateForSocketParentBridgeForCurrentThread(); + if (actorChild) { + Unused << actorChild->SendPSelectTLSClientAuthCertConstructor( + selectClientAuthCertificate, hostname, originAttributes, + port, providerFlags, providerTlsFlags, + ByteArray(serverCertBytes), caNamesBytes); } })); info->SetPendingSelectClientAuthCertificate( @@ -794,6 +780,9 @@ SECStatus SSLGetClientAuthDataHook(void* arg, PRFileDesc* socket, return SECWouldBlock; } +using mozilla::ipc::AssertIsOnBackgroundThread; +using mozilla::ipc::IsOnBackgroundThread; + // Helper continuation for when a client authentication certificate has been // selected in the parent process and the information needs to be sent to the // socket process. @@ -803,13 +792,13 @@ class RemoteClientAuthCertificateSelected explicit RemoteClientAuthCertificateSelected( SelectTLSClientAuthCertParent* selectTLSClientAuthCertParent) : mSelectTLSClientAuthCertParent(selectTLSClientAuthCertParent), - mEventTarget(GetCurrentSerialEventTarget()) {} + mEventTarget(NS_GetCurrentThread()) {} NS_IMETHOD Run() override; private: RefPtr mSelectTLSClientAuthCertParent; - nsCOMPtr mEventTarget; + nsCOMPtr mEventTarget; }; NS_IMETHODIMP @@ -886,6 +875,8 @@ bool SelectTLSClientAuthCertParent::Dispatch( void SelectTLSClientAuthCertParent::TLSClientAuthCertSelected( const nsTArray& aSelectedCertBytes, nsTArray>&& aSelectedCertChainBytes) { + AssertIsOnBackgroundThread(); + if (!CanSend()) { return; } @@ -897,7 +888,7 @@ void SelectTLSClientAuthCertParent::TLSClientAuthCertSelected( Unused << SendTLSClientAuthCertSelected(aSelectedCertBytes, selectedCertChainBytes); - Close(); + Unused << Send__delete__(this); } void SelectTLSClientAuthCertParent::ActorDestroy( diff --git a/security/manager/ssl/VerifySSLServerCertChild.cpp b/security/manager/ssl/VerifySSLServerCertChild.cpp index c1f9781e7fe7..b8b00d1c341f 100644 --- a/security/manager/ssl/VerifySSLServerCertChild.cpp +++ b/security/manager/ssl/VerifySSLServerCertChild.cpp @@ -8,9 +8,8 @@ #include "VerifySSLServerCertChild.h" #include "CertVerifier.h" -#include "mozilla/net/SocketProcessBackgroundChild.h" -#include "mozilla/psm/PVerifySSLServerCertParent.h" -#include "mozilla/psm/PVerifySSLServerCertChild.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "mozilla/ipc/PBackgroundChild.h" #include "nsNSSIOLayer.h" #include "nsSerializationHelper.h" @@ -99,33 +98,19 @@ SECStatus RemoteProcessCertVerification( dcInfo.ref().authKeyBits() = static_cast(aDcInfo->authKeyBits); } - ipc::Endpoint parentEndpoint; - ipc::Endpoint childEndpoint; - PVerifySSLServerCert::CreateEndpoints(&parentEndpoint, &childEndpoint); - - if (NS_FAILED(net::SocketProcessBackgroundChild::WithActor( - "SendInitVerifySSLServerCert", - [endpoint = std::move(parentEndpoint), - peerCertBytes = std::move(peerCertBytes), - hostName = PromiseFlatCString(aHostName), port(aPort), - originAttributes(aOriginAttributes), - stapledOCSPResponse = std::move(stapledOCSPResponse), - sctsFromTLSExtension = std::move(sctsFromTLSExtension), - dcInfo = std::move(dcInfo), providerFlags(aProviderFlags), - certVerifierFlags(aCertVerifierFlags)]( - net::SocketProcessBackgroundChild* aActor) mutable { - Unused << aActor->SendInitVerifySSLServerCert( - std::move(endpoint), peerCertBytes, hostName, port, - originAttributes, stapledOCSPResponse, sctsFromTLSExtension, - dcInfo, providerFlags, certVerifierFlags); - }))) { + mozilla::ipc::PBackgroundChild* actorChild = mozilla::ipc::BackgroundChild:: + GetOrCreateForSocketParentBridgeForCurrentThread(); + if (!actorChild) { PR_SetError(SEC_ERROR_LIBRARY_FAILURE, 0); return SECFailure; } RefPtr authCert = new VerifySSLServerCertChild( aResultTask, std::move(aPeerCertChain), aProviderFlags); - if (!childEndpoint.Bind(authCert)) { + if (!actorChild->SendPVerifySSLServerCertConstructor( + authCert, peerCertBytes, PromiseFlatCString(aHostName), aPort, + aOriginAttributes, stapledOCSPResponse, sctsFromTLSExtension, dcInfo, + aProviderFlags, aCertVerifierFlags)) { PR_SetError(SEC_ERROR_LIBRARY_FAILURE, 0); return SECFailure; } diff --git a/security/manager/ssl/VerifySSLServerCertParent.cpp b/security/manager/ssl/VerifySSLServerCertParent.cpp index 5aaaba8197c8..7216c571d1aa 100644 --- a/security/manager/ssl/VerifySSLServerCertParent.cpp +++ b/security/manager/ssl/VerifySSLServerCertParent.cpp @@ -15,10 +15,14 @@ #include "SSLServerCertVerification.h" #include "nsNSSIOLayer.h" #include "nsISocketProvider.h" +#include "mozilla/ipc/BackgroundParent.h" #include "mozilla/Unused.h" extern mozilla::LazyLogModule gPIPNSSLog; +using mozilla::ipc::AssertIsOnBackgroundThread; +using mozilla::ipc::IsOnBackgroundThread; + using namespace mozilla::pkix; namespace mozilla { @@ -31,6 +35,8 @@ void VerifySSLServerCertParent::OnVerifiedSSLServerCert( uint16_t aCertificateTransparencyStatus, uint8_t aEVStatus, bool aSucceeded, PRErrorCode aFinalError, uint32_t aOverridableErrorCategory, bool aIsBuiltCertChainRootBuiltInRoot, bool aMadeOCSPRequests) { + AssertIsOnBackgroundThread(); + if (!CanSend()) { return; } @@ -43,8 +49,7 @@ void VerifySSLServerCertParent::OnVerifiedSSLServerCert( Unused << SendOnVerifiedSSLServerCertFailure( aFinalError, aOverridableErrorCategory, aMadeOCSPRequests); } - - Close(); + Unused << Send__delete__(this); } namespace { @@ -129,8 +134,9 @@ bool VerifySSLServerCertParent::Dispatch( const Maybe& aDcInfo, const uint32_t& aProviderFlags, const uint32_t& aCertVerifierFlags) { MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("VerifySSLServerCertParent::Dispatch")); + AssertIsOnBackgroundThread(); - mBackgroundThread = GetCurrentSerialEventTarget(); + mBackgroundThread = NS_GetCurrentThread(); nsTArray> peerCertBytes; for (auto& certBytes : aPeerCertChain) { diff --git a/security/manager/ssl/VerifySSLServerCertParent.h b/security/manager/ssl/VerifySSLServerCertParent.h index de2c062935e8..b1ba35c393d6 100644 --- a/security/manager/ssl/VerifySSLServerCertParent.h +++ b/security/manager/ssl/VerifySSLServerCertParent.h @@ -51,7 +51,7 @@ class VerifySSLServerCertParent : public PVerifySSLServerCertParent { // PVerifySSLServerCertParent void ActorDestroy(ActorDestroyReason aWhy) override; - nsCOMPtr mBackgroundThread; + nsCOMPtr mBackgroundThread; }; } // namespace psm diff --git a/security/manager/ssl/nsNSSIOLayer.cpp b/security/manager/ssl/nsNSSIOLayer.cpp index 25951f404c80..63b31e05a787 100644 --- a/security/manager/ssl/nsNSSIOLayer.cpp +++ b/security/manager/ssl/nsNSSIOLayer.cpp @@ -27,6 +27,8 @@ #include "mozilla/RandomNum.h" #include "mozilla/StaticPrefs_security.h" #include "mozilla/Telemetry.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "mozilla/ipc/PBackgroundChild.h" #include "mozilla/net/SSLTokensCache.h" #include "mozilla/net/SocketProcessChild.h" #include "mozilla/psm/IPCClientCertsChild.h" @@ -1658,6 +1660,24 @@ loser: return NS_ERROR_FAILURE; } +already_AddRefed GetIPCClientCertsActor() { + PBackgroundChild* backgroundActor = + BackgroundChild::GetOrCreateForSocketParentBridgeForCurrentThread(); + if (!backgroundActor) { + return nullptr; + } + RefPtr actor = + SingleManagedOrNull(backgroundActor->ManagedPIPCClientCertsChild()); + if (!actor) { + actor = backgroundActor->SendPIPCClientCertsConstructor( + new IPCClientCertsChild()); + if (!actor) { + return nullptr; + } + } + return actor.forget().downcast(); +} + extern "C" { const uint8_t kIPCClientCertsObjectTypeCert = 1; @@ -1668,14 +1688,7 @@ const uint8_t kIPCClientCertsObjectTypeECKey = 3; // parent process to find certificates and keys and send identifying // information about them over IPC. void DoFindObjects(FindObjectsCallback cb, void* ctx) { - net::SocketProcessChild* socketChild = - net::SocketProcessChild::GetSingleton(); - if (!socketChild) { - return; - } - - RefPtr ipcClientCertsActor( - socketChild->GetIPCClientCertsActor()); + RefPtr ipcClientCertsActor(GetIPCClientCertsActor()); if (!ipcClientCertsActor) { return; } @@ -1719,14 +1732,7 @@ void DoFindObjects(FindObjectsCallback cb, void* ctx) { void DoSign(size_t cert_len, const uint8_t* cert, size_t data_len, const uint8_t* data, size_t params_len, const uint8_t* params, SignCallback cb, void* ctx) { - net::SocketProcessChild* socketChild = - net::SocketProcessChild::GetSingleton(); - if (!socketChild) { - return; - } - - RefPtr ipcClientCertsActor( - socketChild->GetIPCClientCertsActor()); + RefPtr ipcClientCertsActor(GetIPCClientCertsActor()); if (!ipcClientCertsActor) { return; }