From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- netwerk/base/SimpleChannel.h | 152 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 netwerk/base/SimpleChannel.h (limited to 'netwerk/base/SimpleChannel.h') diff --git a/netwerk/base/SimpleChannel.h b/netwerk/base/SimpleChannel.h new file mode 100644 index 0000000000..d469adf65d --- /dev/null +++ b/netwerk/base/SimpleChannel.h @@ -0,0 +1,152 @@ +/* -*- 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 SimpleChannel_h +#define SimpleChannel_h + +#include "mozilla/ResultExtensions.h" +#include "mozilla/UniquePtr.h" +#include "nsBaseChannel.h" +#include "nsIChildChannel.h" +#include "mozilla/net/PSimpleChannelChild.h" +#include "nsCOMPtr.h" + +class nsIChannel; +class nsIInputStream; +class nsILoadInfo; +class nsIRequest; +class nsIStreamListener; +class nsIURI; + +//----------------------------------------------------------------------------- + +namespace mozilla { + +using InputStreamOrReason = Result, nsresult>; +using NotNullRequest = NotNull>; +using NotNullCancelable = NotNull>; +using RequestOrCancelable = Variant; +using RequestOrReason = Result; + +namespace net { + +class SimpleChannelCallbacks { + public: + virtual InputStreamOrReason OpenContentStream(bool async, + nsIChannel* channel) = 0; + + virtual RequestOrReason StartAsyncRead(nsIStreamListener* stream, + nsIChannel* channel) = 0; + + virtual ~SimpleChannelCallbacks() = default; +}; + +template +class SimpleChannelCallbacksImpl final : public SimpleChannelCallbacks { + public: + SimpleChannelCallbacksImpl(F1&& aStartAsyncRead, F2&& aOpenContentStream, + T* context) + : mStartAsyncRead(aStartAsyncRead), + mOpenContentStream(aOpenContentStream), + mContext(context) {} + + virtual ~SimpleChannelCallbacksImpl() = default; + + virtual InputStreamOrReason OpenContentStream(bool async, + nsIChannel* channel) override { + return mOpenContentStream(async, channel, mContext); + } + + virtual RequestOrReason StartAsyncRead(nsIStreamListener* listener, + nsIChannel* channel) override { + return mStartAsyncRead(listener, channel, mContext); + } + + private: + F1 mStartAsyncRead; + F2 mOpenContentStream; + RefPtr mContext; +}; + +class SimpleChannel : public nsBaseChannel { + public: + explicit SimpleChannel(UniquePtr&& aCallbacks); + + protected: + virtual ~SimpleChannel() = default; + + virtual nsresult OpenContentStream(bool async, nsIInputStream** streamOut, + nsIChannel** channel) override; + + virtual nsresult BeginAsyncRead(nsIStreamListener* listener, + nsIRequest** request, + nsICancelable** cancelableRequest) override; + + private: + UniquePtr mCallbacks; +}; + +class SimpleChannelChild final : public SimpleChannel, + public nsIChildChannel, + public PSimpleChannelChild { + public: + explicit SimpleChannelChild(UniquePtr&& aCallbacks); + + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_NSICHILDCHANNEL + + private: + virtual ~SimpleChannelChild() = default; +}; + +already_AddRefed NS_NewSimpleChannelInternal( + nsIURI* aURI, nsILoadInfo* aLoadInfo, + UniquePtr&& aCallbacks); + +} // namespace net +} // namespace mozilla + +/** + * Creates a simple channel which wraps an input stream created by the given + * callbacks. The callbacks are not called until the underlying AsyncOpen or + * Open methods are called, and correspond to the nsBaseChannel::StartAsyncRead + * and nsBaseChannel::OpenContentStream methods of the same names. + * + * The last two arguments of each callback are the created channel instance, + * and the ref-counted context object passed to NS_NewSimpleChannel. A strong + * reference to that object is guaranteed to be kept alive until after a + * callback successfully completes. + */ +template +inline already_AddRefed NS_NewSimpleChannel( + nsIURI* aURI, nsILoadInfo* aLoadInfo, T* context, F1&& aStartAsyncRead, + F2&& aOpenContentStream) { + using namespace mozilla; + + auto callbacks = MakeUnique>( + std::forward(aStartAsyncRead), std::forward(aOpenContentStream), + context); + + return net::NS_NewSimpleChannelInternal(aURI, aLoadInfo, + std::move(callbacks)); +} + +template +inline already_AddRefed NS_NewSimpleChannel(nsIURI* aURI, + nsILoadInfo* aLoadInfo, + T* context, + F1&& aStartAsyncRead) { + using namespace mozilla; + + auto openContentStream = [](bool async, nsIChannel* channel, T* context) { + return Err(NS_ERROR_NOT_IMPLEMENTED); + }; + + return NS_NewSimpleChannel(aURI, aLoadInfo, context, + std::forward(aStartAsyncRead), + std::move(openContentStream)); +} + +#endif // SimpleChannel_h -- cgit v1.2.3