diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /dom/streams/CountQueuingStrategy.cpp | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/streams/CountQueuingStrategy.cpp')
-rw-r--r-- | dom/streams/CountQueuingStrategy.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/dom/streams/CountQueuingStrategy.cpp b/dom/streams/CountQueuingStrategy.cpp new file mode 100644 index 0000000000..72f000e604 --- /dev/null +++ b/dom/streams/CountQueuingStrategy.cpp @@ -0,0 +1,103 @@ +/* -*- 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/. */ + +#include "mozilla/dom/CountQueuingStrategy.h" +#include "mozilla/dom/FunctionBinding.h" +#include "mozilla/dom/QueuingStrategyBinding.h" +#include "nsCOMPtr.h" +#include "nsISupports.h" + +namespace mozilla::dom { + +NS_IMPL_CYCLE_COLLECTION(BaseQueuingStrategy, mGlobal) +NS_IMPL_CYCLE_COLLECTING_ADDREF(BaseQueuingStrategy) +NS_IMPL_CYCLE_COLLECTING_RELEASE(BaseQueuingStrategy) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BaseQueuingStrategy) + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(CountQueuingStrategy, + BaseQueuingStrategy) +NS_IMPL_ADDREF_INHERITED(CountQueuingStrategy, BaseQueuingStrategy) +NS_IMPL_RELEASE_INHERITED(CountQueuingStrategy, BaseQueuingStrategy) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CountQueuingStrategy) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY +NS_INTERFACE_MAP_END_INHERITING(BaseQueuingStrategy) + +/* static */ +already_AddRefed<CountQueuingStrategy> CountQueuingStrategy::Constructor( + const GlobalObject& aGlobal, const QueuingStrategyInit& aInit) { + RefPtr<CountQueuingStrategy> strategy = + new CountQueuingStrategy(aGlobal.GetAsSupports(), aInit.mHighWaterMark); + return strategy.forget(); +} + +nsIGlobalObject* BaseQueuingStrategy::GetParentObject() const { + return mGlobal; +} + +JSObject* CountQueuingStrategy::WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) { + return CountQueuingStrategy_Binding::Wrap(aCx, this, aGivenProto); +} + +// https://streams.spec.whatwg.org/#count-queuing-strategy-size-function +static bool CountQueuingStrategySize(JSContext* aCx, unsigned aArgc, + JS::Value* aVp) { + JS::CallArgs args = CallArgsFromVp(aArgc, aVp); + + // Step 1.1. Return 1. + args.rval().setInt32(1); + return true; +} + +// https://streams.spec.whatwg.org/#cqs-size +already_AddRefed<Function> CountQueuingStrategy::GetSize(ErrorResult& aRv) { + // Step 1. Return this's relevant global object's count queuing strategy + // size function. + if (RefPtr<Function> fun = mGlobal->GetCountQueuingStrategySizeFunction()) { + return fun.forget(); + } + + // Note: Instead of eagerly allocating a size function for every global object + // we do it lazily once in this getter. + // After this point the steps refer to: + // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function. + + AutoJSAPI jsapi; + if (!jsapi.Init(mGlobal)) { + aRv.ThrowUnknownError("Internal error"); + return nullptr; + } + JSContext* cx = jsapi.cx(); + + // Step 1. Let steps be the following steps: + // Note: See CountQueuingStrategySize instead. + + // Step 2. Let F be + // ! CreateBuiltinFunction(steps, 0, "size", « », + // globalObject’s relevant Realm). + JS::Rooted<JSFunction*> sizeFunction( + cx, JS_NewFunction(cx, CountQueuingStrategySize, 0, 0, "size")); + if (!sizeFunction) { + aRv.StealExceptionFromJSContext(cx); + return nullptr; + } + + // Step 3. Set globalObject’s count queuing strategy size function to + // a Function that represents a reference to F, + // with callback context equal to globalObject’s relevant settings object. + JS::Rooted<JSObject*> funObj(cx, JS_GetFunctionObject(sizeFunction)); + JS::Rooted<JSObject*> global(cx, mGlobal->GetGlobalJSObject()); + RefPtr<Function> function = new Function(cx, funObj, global, mGlobal); + mGlobal->SetCountQueuingStrategySizeFunction(function); + + return function.forget(); +} + +} // namespace mozilla::dom |