tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit d50f9cb3f3b18966d66f90f271d96bd9ae10cbf1
parent f839d0ebb6e5349fdd7637bdb7e9c39cc177c2ae
Author: Nika Layzell <nika@thelayzells.com>
Date:   Thu, 20 Nov 2025 23:12:06 +0000

Bug 2000994 - Part 1: Introduce a common base nsIParentChannel type, r=necko-reviewers,valentin

This is a utility type for basic nsIParentChannel implementations which
generally do not care about the full feature set of the nsIParentChannel
interface.

For simple channels which can load entirely within the content process, the
nsIParentChannel instance is only required to complete the redirect hand-off
successfully.

Differential Revision: https://phabricator.services.mozilla.com/D273124

Diffstat:
Mnetwerk/base/moz.build | 2++
Anetwerk/base/nsBaseParentChannel.cpp | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Anetwerk/base/nsBaseParentChannel.h | 38++++++++++++++++++++++++++++++++++++++
Mnetwerk/ipc/NeckoParent.cpp | 11+++++++++++
Mnetwerk/ipc/NeckoParent.h | 2++
Mnetwerk/ipc/PNecko.ipdl | 6++++++
6 files changed, 132 insertions(+), 0 deletions(-)

diff --git a/netwerk/base/moz.build b/netwerk/base/moz.build @@ -134,6 +134,7 @@ EXPORTS += [ "nsASocketHandler.h", "nsAsyncRedirectVerifyHelper.h", "nsBaseChannel.h", + "nsBaseParentChannel.h", "nsFileStreams.h", "nsInputStreamPump.h", "nsMIMEInputStream.h", @@ -203,6 +204,7 @@ UNIFIED_SOURCES += [ "nsBase64Encoder.cpp", "nsBaseChannel.cpp", "nsBaseContentStream.cpp", + "nsBaseParentChannel.cpp", "nsBufferedStreams.cpp", "nsDirectoryIndexStream.cpp", "nsDNSPrefetch.cpp", diff --git a/netwerk/base/nsBaseParentChannel.cpp b/netwerk/base/nsBaseParentChannel.cpp @@ -0,0 +1,73 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=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 "nsBaseParentChannel.h" + +NS_IMPL_ISUPPORTS(nsBaseParentChannel, nsIParentChannel, nsIStreamListener, + nsIRequestObserver) + +NS_IMETHODIMP +nsBaseParentChannel::SetParentListener( + mozilla::net::ParentChannelListener* aListener) { + // Nothing to do. + return NS_OK; +} + +NS_IMETHODIMP +nsBaseParentChannel::NotifyClassificationFlags(uint32_t aClassificationFlags, + bool aIsThirdParty) { + // Nothing to do + return NS_OK; +} + +NS_IMETHODIMP +nsBaseParentChannel::SetClassifierMatchedInfo(const nsACString& aList, + const nsACString& aProvider, + const nsACString& aPrefix) { + // Nothing to do + return NS_OK; +} + +NS_IMETHODIMP +nsBaseParentChannel::SetClassifierMatchedTrackingInfo( + const nsACString& aLists, const nsACString& aFullHashes) { + // Nothing to do + return NS_OK; +} + +NS_IMETHODIMP +nsBaseParentChannel::Delete() { + // Nothing to do + return NS_OK; +} + +NS_IMETHODIMP +nsBaseParentChannel::GetRemoteType(nsACString& aRemoteType) { + aRemoteType = mRemoteType; + return NS_OK; +} + +NS_IMETHODIMP +nsBaseParentChannel::OnStartRequest(nsIRequest* aRequest) { + // The redirect unconditionally resumes the channel we're bound to. + // We cancel the channel ASAP so we stop receiving further data. + return NS_BINDING_ABORTED; +} + +NS_IMETHODIMP +nsBaseParentChannel::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) { + // See above. + MOZ_ASSERT(NS_FAILED(aStatusCode)); + return NS_OK; +} + +NS_IMETHODIMP +nsBaseParentChannel::OnDataAvailable(nsIRequest* aRequest, + nsIInputStream* aInputStream, + uint64_t aOffset, uint32_t aCount) { + // See above. + MOZ_CRASH("Should never be called unless overridden"); +} diff --git a/netwerk/base/nsBaseParentChannel.h b/netwerk/base/nsBaseParentChannel.h @@ -0,0 +1,38 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=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/. */ + +#ifndef nsBaseParentChannel_h +#define nsBaseParentChannel_h + +#include "nsIParentChannel.h" + +// Basic type which implements a no-op nsIParentChannel instance. +// +// nsBaseChannel can be used when implementing simple channels which need to +// support cross-process redirects, and do not need the other features +// nsIParentChannel supports. +// +// The default behaviour if nothing is overridden is to close the channel as +// soon as `OnStartRequest` is received. The implementations of +// `nsIRequestObserver` and `nsIStreamListener` can be overridden to change this +// behaviour. +class nsBaseParentChannel : public nsIParentChannel { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIPARENTCHANNEL + NS_DECL_NSIREQUESTOBSERVER + NS_DECL_NSISTREAMLISTENER + + explicit nsBaseParentChannel(const nsACString& aRemoteType) + : mRemoteType(aRemoteType) {} + + protected: + virtual ~nsBaseParentChannel() = default; + + nsCString mRemoteType; +}; + +#endif // nsBaseParentChannel_h diff --git a/netwerk/ipc/NeckoParent.cpp b/netwerk/ipc/NeckoParent.cpp @@ -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 "nsBaseParentChannel.h" #include "nsHttp.h" #include "mozilla/BasePrincipal.h" #include "mozilla/Components.h" @@ -318,6 +319,16 @@ bool NeckoParent::DeallocPWebSocketEventListenerParent( return true; } +mozilla::ipc::IPCResult NeckoParent::RecvConnectBaseChannel( + const uint32_t& channelId) { + RefPtr<nsBaseParentChannel> parentChannel = + new nsBaseParentChannel(ContentParent::Cast(Manager())->GetRemoteType()); + + nsCOMPtr<nsIChannel> channel; + NS_LinkRedirectChannels(channelId, parentChannel, getter_AddRefs(channel)); + return IPC_OK(); +} + already_AddRefed<PDataChannelParent> NeckoParent::AllocPDataChannelParent( const uint32_t& channelId) { RefPtr<DataChannelParent> p = new DataChannelParent(); diff --git a/netwerk/ipc/NeckoParent.h b/netwerk/ipc/NeckoParent.h @@ -137,6 +137,8 @@ class NeckoParent : public PNeckoParent { const uint64_t& aInnerWindowID); bool DeallocPWebSocketEventListenerParent(PWebSocketEventListenerParent*); + mozilla::ipc::IPCResult RecvConnectBaseChannel(const uint32_t& channelId); + already_AddRefed<PDataChannelParent> AllocPDataChannelParent( const uint32_t& channelId); diff --git a/netwerk/ipc/PNecko.ipdl b/netwerk/ipc/PNecko.ipdl @@ -117,6 +117,12 @@ parent: DNSFlags flags, nsresult reason); /** + * Establish a connection between generic redirect channels which do not need + * specialized nsIParentChannel handling. + */ + async ConnectBaseChannel(uint32_t channelId); + + /** * channelId is used to establish a connection between redirect channels in * the parent and the child when we're redirecting to a data: URI. */