From da7bf5ad6736d44ae12e8033a53e85c8a3dee8cc Mon Sep 17 00:00:00 2001 From: Kershaw Chang Date: Thu, 31 Aug 2023 11:45:04 +0000 Subject: [PATCH] Bug 1758155 - Make PWebSocketConnection a toplevel actor, r=necko-reviewers,jesup Differential Revision: https://phabricator.services.mozilla.com/D185031 --- ipc/glue/BackgroundParentImpl.cpp | 23 ------------ ipc/glue/BackgroundParentImpl.h | 5 --- ipc/glue/PBackground.ipdl | 4 --- netwerk/ipc/PSocketProcessBackground.ipdl | 4 +++ netwerk/ipc/SocketProcessBackgroundParent.cpp | 35 +++++++++++++++++++ netwerk/ipc/SocketProcessBackgroundParent.h | 4 +++ .../websocket/PWebSocketConnection.ipdl | 4 --- .../websocket/WebSocketConnectionChild.cpp | 33 ++++++++++------- .../websocket/WebSocketConnectionParent.cpp | 8 ++++- .../websocket/WebSocketConnectionParent.h | 2 +- 10 files changed, 72 insertions(+), 50 deletions(-) diff --git a/ipc/glue/BackgroundParentImpl.cpp b/ipc/glue/BackgroundParentImpl.cpp index be73274fa0f8..aeb237558085 100644 --- a/ipc/glue/BackgroundParentImpl.cpp +++ b/ipc/glue/BackgroundParentImpl.cpp @@ -65,8 +65,6 @@ #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 "nsIHttpChannelInternal.h" #include "nsIPrincipal.h" #include "nsProxyRelease.h" @@ -1379,27 +1377,6 @@ 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) { diff --git a/ipc/glue/BackgroundParentImpl.h b/ipc/glue/BackgroundParentImpl.h index d0b80ea3029d..7bec4fae678f 100644 --- a/ipc/glue/BackgroundParentImpl.h +++ b/ipc/glue/BackgroundParentImpl.h @@ -360,11 +360,6 @@ class BackgroundParentImpl : public PBackgroundParent { const nsAString& aGroupName, const nsACString& aEndpointURL, const PrincipalInfo& aPrincipalInfo) 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 1e04f8cc718b..bab504e26df2 100644 --- a/ipc/glue/PBackground.ipdl +++ b/ipc/glue/PBackground.ipdl @@ -48,7 +48,6 @@ include protocol PWebAuthnTransaction; include protocol PUDPSocket; include protocol PVsync; include protocol PRemoteDecoderManager; -include protocol PWebSocketConnection; include protocol PWebTransport; include protocol PFetch; @@ -121,7 +120,6 @@ sync protocol PBackground manages PServiceWorkerManager; manages PServiceWorkerRegistration; manages PWebAuthnTransaction; - manages PWebSocketConnection; manages PUDPSocket; manages PVsync; manages PFetch; @@ -280,8 +278,6 @@ parent: async EnsureUtilityProcessAndCreateBridge(RemoteDecodeIn aLocation) returns (nsresult rv, Endpoint aEndpoint); - async PWebSocketConnection(uint32_t aListenerId); - async PLockManager(nsIPrincipal aPrincipalInfo, nsID aClientId); async PFetch(); diff --git a/netwerk/ipc/PSocketProcessBackground.ipdl b/netwerk/ipc/PSocketProcessBackground.ipdl index 5a02fc11193f..95d01a9abba7 100644 --- a/netwerk/ipc/PSocketProcessBackground.ipdl +++ b/netwerk/ipc/PSocketProcessBackground.ipdl @@ -7,6 +7,7 @@ include protocol PVerifySSLServerCert; include protocol PSelectTLSClientAuthCert; include protocol PIPCClientCerts; +include protocol PWebSocketConnection; include PSMIPCTypes; @@ -48,6 +49,9 @@ parent: uint64_t aBrowserId); async InitIPCClientCerts(Endpoint aEndpoint); + + async InitWebSocketConnection(Endpoint aEndpoint, + uint32_t aListenerId); }; } // namespace net diff --git a/netwerk/ipc/SocketProcessBackgroundParent.cpp b/netwerk/ipc/SocketProcessBackgroundParent.cpp index c2c91ffaba18..c17e1467b629 100644 --- a/netwerk/ipc/SocketProcessBackgroundParent.cpp +++ b/netwerk/ipc/SocketProcessBackgroundParent.cpp @@ -7,9 +7,12 @@ #include "SocketProcessLogging.h" #include "mozilla/ipc/Endpoint.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 { @@ -127,6 +130,38 @@ mozilla::ipc::IPCResult SocketProcessBackgroundParent::RecvInitIPCClientCerts( 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")); } diff --git a/netwerk/ipc/SocketProcessBackgroundParent.h b/netwerk/ipc/SocketProcessBackgroundParent.h index ba5f7f19ccd8..0714f8dd6ee5 100644 --- a/netwerk/ipc/SocketProcessBackgroundParent.h +++ b/netwerk/ipc/SocketProcessBackgroundParent.h @@ -37,6 +37,10 @@ class SocketProcessBackgroundParent final const uint32_t& aProviderTlsFlags, const ByteArray& aServerCertBytes, nsTArray&& aCANames, const uint64_t& aBrowserId); + mozilla::ipc::IPCResult RecvInitWebSocketConnection( + Endpoint&& aEndpoint, + const uint32_t& aListenerId); + void ActorDestroy(ActorDestroyReason aReason) override; private: diff --git a/netwerk/protocol/websocket/PWebSocketConnection.ipdl b/netwerk/protocol/websocket/PWebSocketConnection.ipdl index 6187c9bfa669..f2fc4f203ad0 100644 --- a/netwerk/protocol/websocket/PWebSocketConnection.ipdl +++ b/netwerk/protocol/websocket/PWebSocketConnection.ipdl @@ -6,8 +6,6 @@ include "mozilla/ipc/TransportSecurityInfoUtils.h"; -include protocol PBackground; - [RefCounted] using class nsITransportSecurityInfo from "nsITransportSecurityInfo.h"; namespace mozilla { @@ -15,8 +13,6 @@ 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 543eafebf127..045c3763b7ce 100644 --- a/netwerk/protocol/websocket/WebSocketConnectionChild.cpp +++ b/netwerk/protocol/websocket/WebSocketConnectionChild.cpp @@ -8,8 +8,8 @@ #include "WebSocketConnectionChild.h" #include "WebSocketConnection.h" -#include "mozilla/ipc/BackgroundChild.h" -#include "mozilla/ipc/PBackgroundChild.h" +#include "mozilla/ipc/Endpoint.h" +#include "mozilla/net/SocketProcessBackgroundChild.h" #include "nsISerializable.h" #include "nsITLSSocketControl.h" #include "nsITransportSecurityInfo.h" @@ -32,6 +32,8 @@ 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)); @@ -39,17 +41,24 @@ void WebSocketConnectionChild::Init(uint32_t aListenerId) { return; } - RefPtr self = this; - mSocketThread->Dispatch(NS_NewRunnableFunction( - "WebSocketConnectionChild::Init", [self, aListenerId]() { - mozilla::ipc::PBackgroundChild* actorChild = mozilla::ipc:: - BackgroundChild::GetOrCreateForSocketParentBridgeForCurrentThread(); - if (!actorChild) { - return; - } + ipc::Endpoint parentEndpoint; + ipc::Endpoint childEndpoint; + PWebSocketConnection::CreateEndpoints(&parentEndpoint, &childEndpoint); - Unused << actorChild->SendPWebSocketConnectionConstructor(self, - aListenerId); + if (NS_FAILED(SocketProcessBackgroundChild::WithActor( + "SendInitWebSocketConnection", + [aListenerId, endpoint = std::move(parentEndpoint)]( + SocketProcessBackgroundChild* aActor) mutable { + Unused << aActor->SendInitWebSocketConnection(std::move(endpoint), + aListenerId); + }))) { + return; + } + + mSocketThread->Dispatch(NS_NewRunnableFunction( + "BindWebSocketConnectionChild", + [self = RefPtr{this}, endpoint = std::move(childEndpoint)]() mutable { + endpoint.Bind(self); })); } diff --git a/netwerk/protocol/websocket/WebSocketConnectionParent.cpp b/netwerk/protocol/websocket/WebSocketConnectionParent.cpp index 3470daf94094..3fdf85337e32 100644 --- a/netwerk/protocol/websocket/WebSocketConnectionParent.cpp +++ b/netwerk/protocol/websocket/WebSocketConnectionParent.cpp @@ -103,6 +103,10 @@ 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( @@ -123,7 +127,9 @@ void WebSocketConnectionParent::Close() { mClosed = true; - auto task = [self = RefPtr{this}]() { Unused << self->Send__delete__(self); }; + auto task = [self = RefPtr{this}]() { + self->PWebSocketConnectionParent::Close(); + }; if (mBackgroundThread->IsOnCurrentThread()) { task(); diff --git a/netwerk/protocol/websocket/WebSocketConnectionParent.h b/netwerk/protocol/websocket/WebSocketConnectionParent.h index c7d67e117a7e..45b031e9cbbf 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"};