Quelle UnderlyingSourceCallbackHelpers.h
Sprache: C
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim:set ts=2 sw=2 sts=2 et cindent: */ /* 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/. */
/* Since the streams specification has native descriptions of some callbacks * (i.e. described in prose, rather than provided by user code), we need to be * able to pass around native callbacks. To handle this, we define polymorphic * classes That cover the difference between native callback and user-provided. * * The Streams specification wants us to invoke these callbacks, run through * WebIDL as if they were methods. So we have to preserve the underlying object * to use as the This value on invocation.
*/ enumclass nsresult : uint32_t;
namespace mozilla::dom {
class StrongWorkerRef; class BodyStreamHolder; class ReadableStreamController; class ReadableStream;
class UnderlyingSourceAlgorithmsBase : public nsISupports { public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(UnderlyingSourceAlgorithmsBase)
// A promise-returning algorithm that pulls data from the underlying byte // source
MOZ_CAN_RUN_SCRIPT virtual already_AddRefed<Promise> PullCallback(
JSContext* aCx, ReadableStreamController& aController,
ErrorResult& aRv) = 0;
// A promise-returning algorithm, taking one argument (the cancel reason), // which communicates a requested cancelation to the underlying byte source
MOZ_CAN_RUN_SCRIPT virtual already_AddRefed<Promise> CancelCallback(
JSContext* aCx, const Optional<JS::Handle<JS::Value>>& aReason,
ErrorResult& aRv) = 0;
// Implement this when you need to release underlying resources immediately // from closed(canceled)/errored streams, without waiting for GC. virtualvoid ReleaseObjects() {}
// Can be used to read chunks directly via nsIInputStream to skip JS-related // overhead, if this readable stream is a wrapper of a native stream. // Currently used by Fetch helper functions e.g. new Response(stream).text() virtual nsIInputStream* MaybeGetInputStreamIfUnread() { return nullptr; }
// https://streams.spec.whatwg.org/#other-specs-rs-create // By "native" we mean "instances initialized via the above set up or set up // with byte reading support algorithms (not, e.g., on web-developer-created // instances)" virtualbool IsNative() { returntrue; }
class UnderlyingSourceAlgorithms final : public UnderlyingSourceAlgorithmsBase { public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
UnderlyingSourceAlgorithms, UnderlyingSourceAlgorithmsBase)
// https://streams.spec.whatwg.org/#readablestream-set-up // https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-support // Wrappers defined by the "Set up" methods in the spec. This helps you just // return nullptr when an error occurred as this wrapper converts it to a // rejected promise. // Note that StartCallback is only for JS consumers to access // the controller, and thus is no-op here since native consumers can call // `EnqueueNative()` etc. without direct controller access. class UnderlyingSourceAlgorithmsWrapper
: public UnderlyingSourceAlgorithmsBase { void StartCallback(JSContext*, ReadableStreamController&,
JS::MutableHandle<JS::Value> aRetVal, ErrorResult&) final;
// This class exists to isolate InputToReadableStreamAlgorithms from the // nsIAsyncInputStream. If we call AsyncWait(this,...), it holds a // reference to 'this' which can't be cc'd, and we can leak the stream, // causing a Worker to assert with globalScopeAlive. By isolating // ourselves from the inputstream, we can safely be CC'd if needed and // will inform the inputstream to shut down. class InputStreamHolder final : public nsIInputStreamCallback, public GlobalTeardownObserver { public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINPUTSTREAMCALLBACK
// WeakPtr to avoid cycles
WeakPtr<InputToReadableStreamAlgorithms> mCallback; // To ensure the worker sticks around
RefPtr<StrongWorkerRef> mAsyncWaitWorkerRef;
RefPtr<StrongWorkerRef> mWorkerRef;
nsCOMPtr<nsIAsyncInputStream> mInput;
// To ensure the underlying source sticks around during an ongoing read // operation. mAlgorithms is not cycle collected on purpose, and this holder // is responsible to keep the underlying source algorithms until // nsIAsyncInputStream responds. // // This is done because otherwise the whole stream objects may be cycle // collected, including the promises created from read(), as our JS engine may // throw unsettled promises away for optimization. See bug 1849860.
RefPtr<InputToReadableStreamAlgorithms> mAsyncWaitAlgorithms;
};
// Using this class means you are also passing the lifetime control of your // nsIAsyncInputStream, as it will be closed when this class tears down. class InputToReadableStreamAlgorithms final
: public UnderlyingSourceAlgorithmsWrapper, public nsIInputStreamCallback, public SupportsWeakPtr {
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIINPUTSTREAMCALLBACK
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InputToReadableStreamAlgorithms,
UnderlyingSourceAlgorithmsWrapper)
// This promise is created by PullCallback and resolved when // OnInputStreamReady succeeds. No need to try hard to settle it though, see // also ReleaseObjects() for the reason.
RefPtr<Promise> mPullPromise;
RefPtr<InputStreamHolder> mInput;
// mStream never changes after construction and before CC
MOZ_KNOWN_LIVE RefPtr<ReadableStream> mStream;
};
class NonAsyncInputToReadableStreamAlgorithms
: public UnderlyingSourceAlgorithmsWrapper { public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(
NonAsyncInputToReadableStreamAlgorithms,
UnderlyingSourceAlgorithmsWrapper)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.