Bug 1758155 - Make PWebSocketConnection a toplevel actor, r=necko-reviewers,jesup

Differential Revision: https://phabricator.services.mozilla.com/D185031
This commit is contained in:
Kershaw Chang 2023-08-31 11:45:04 +00:00
parent bda80165c9
commit da7bf5ad67
10 changed files with 72 additions and 50 deletions

View File

@ -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<dom::PFetchParent> BackgroundParentImpl::AllocPFetchParent() {
return MakeAndAddRef<dom::FetchParent>();
}
already_AddRefed<mozilla::net::PWebSocketConnectionParent>
BackgroundParentImpl::AllocPWebSocketConnectionParent(
const uint32_t& aListenerId) {
Maybe<nsCOMPtr<nsIHttpUpgradeListener>> listener =
net::HttpConnectionMgrParent::GetAndRemoveHttpUpgradeListener(
aListenerId);
if (!listener) {
return nullptr;
}
RefPtr<mozilla::net::WebSocketConnectionParent> 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) {

View File

@ -360,11 +360,6 @@ class BackgroundParentImpl : public PBackgroundParent {
const nsAString& aGroupName, const nsACString& aEndpointURL,
const PrincipalInfo& aPrincipalInfo) override;
already_AddRefed<mozilla::net::PWebSocketConnectionParent>
AllocPWebSocketConnectionParent(const uint32_t& aListenerId) override;
mozilla::ipc::IPCResult RecvPWebSocketConnectionConstructor(
PWebSocketConnectionParent* actor, const uint32_t& aListenerId) override;
already_AddRefed<PLockManagerParent> AllocPLockManagerParent(
NotNull<nsIPrincipal*> aPrincipal, const nsID& aClientId) final;

View File

@ -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<PRemoteDecoderManagerChild> aEndpoint);
async PWebSocketConnection(uint32_t aListenerId);
async PLockManager(nsIPrincipal aPrincipalInfo, nsID aClientId);
async PFetch();

View File

@ -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<PIPCClientCertsParent> aEndpoint);
async InitWebSocketConnection(Endpoint<PWebSocketConnectionParent> aEndpoint,
uint32_t aListenerId);
};
} // namespace net

View File

@ -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<PWebSocketConnectionParent>&& aEndpoint,
const uint32_t& aListenerId) {
LOG(("SocketProcessBackgroundParent::RecvInitWebSocketConnection\n"));
if (!aEndpoint.IsValid()) {
return IPC_FAIL(this, "Invalid endpoint");
}
nsCOMPtr<nsISerialEventTarget> 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<nsCOMPtr<nsIHttpUpgradeListener>> listener =
net::HttpConnectionMgrParent::GetAndRemoveHttpUpgradeListener(
aListenerId);
if (!listener) {
return;
}
RefPtr<WebSocketConnectionParent> actor =
new WebSocketConnectionParent(*listener);
endpoint.Bind(actor);
}));
return IPC_OK();
}
void SocketProcessBackgroundParent::ActorDestroy(ActorDestroyReason aReason) {
LOG(("SocketProcessBackgroundParent::ActorDestroy"));
}

View File

@ -37,6 +37,10 @@ class SocketProcessBackgroundParent final
const uint32_t& aProviderTlsFlags, const ByteArray& aServerCertBytes,
nsTArray<ByteArray>&& aCANames, const uint64_t& aBrowserId);
mozilla::ipc::IPCResult RecvInitWebSocketConnection(
Endpoint<PWebSocketConnectionParent>&& aEndpoint,
const uint32_t& aListenerId);
void ActorDestroy(ActorDestroyReason aReason) override;
private:

View File

@ -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);

View File

@ -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<WebSocketConnectionChild> self = this;
mSocketThread->Dispatch(NS_NewRunnableFunction(
"WebSocketConnectionChild::Init", [self, aListenerId]() {
mozilla::ipc::PBackgroundChild* actorChild = mozilla::ipc::
BackgroundChild::GetOrCreateForSocketParentBridgeForCurrentThread();
if (!actorChild) {
return;
}
ipc::Endpoint<PWebSocketConnectionParent> parentEndpoint;
ipc::Endpoint<PWebSocketConnectionChild> 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);
}));
}

View File

@ -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();

View File

@ -56,7 +56,7 @@ class WebSocketConnectionParent final : public PWebSocketConnectionParent,
nsCOMPtr<nsIHttpUpgradeListener> mUpgradeListener;
RefPtr<WebSocketConnectionListener> mListener;
nsCOMPtr<nsIEventTarget> mBackgroundThread;
nsCOMPtr<nsISerialEventTarget> mBackgroundThread;
nsCOMPtr<nsITransportSecurityInfo> mSecurityInfo;
Atomic<bool> mClosed{false};
Mutex mMutex MOZ_UNANNOTATED{"WebSocketConnectionParent::mMutex"};