1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/CountQueuingStrategy.h"
8 #include "mozilla/dom/FunctionBinding.h"
9 #include "mozilla/dom/QueuingStrategyBinding.h"
11 #include "nsISupports.h"
13 namespace mozilla::dom
{
15 NS_IMPL_CYCLE_COLLECTION(BaseQueuingStrategy
, mGlobal
)
16 NS_IMPL_CYCLE_COLLECTING_ADDREF(BaseQueuingStrategy
)
17 NS_IMPL_CYCLE_COLLECTING_RELEASE(BaseQueuingStrategy
)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BaseQueuingStrategy
)
20 NS_INTERFACE_MAP_ENTRY(nsISupports
)
23 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(CountQueuingStrategy
,
25 NS_IMPL_ADDREF_INHERITED(CountQueuingStrategy
, BaseQueuingStrategy
)
26 NS_IMPL_RELEASE_INHERITED(CountQueuingStrategy
, BaseQueuingStrategy
)
28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CountQueuingStrategy
)
29 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
30 NS_INTERFACE_MAP_END_INHERITING(BaseQueuingStrategy
)
33 already_AddRefed
<CountQueuingStrategy
> CountQueuingStrategy::Constructor(
34 const GlobalObject
& aGlobal
, const QueuingStrategyInit
& aInit
) {
35 RefPtr
<CountQueuingStrategy
> strategy
=
36 new CountQueuingStrategy(aGlobal
.GetAsSupports(), aInit
.mHighWaterMark
);
37 return strategy
.forget();
40 nsIGlobalObject
* BaseQueuingStrategy::GetParentObject() const {
44 JSObject
* CountQueuingStrategy::WrapObject(JSContext
* aCx
,
45 JS::Handle
<JSObject
*> aGivenProto
) {
46 return CountQueuingStrategy_Binding::Wrap(aCx
, this, aGivenProto
);
49 // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function
50 static bool CountQueuingStrategySize(JSContext
* aCx
, unsigned aArgc
,
52 JS::CallArgs args
= CallArgsFromVp(aArgc
, aVp
);
54 // Step 1.1. Return 1.
55 args
.rval().setInt32(1);
59 // https://streams.spec.whatwg.org/#cqs-size
60 already_AddRefed
<Function
> CountQueuingStrategy::GetSize(ErrorResult
& aRv
) {
61 // Step 1. Return this's relevant global object's count queuing strategy
63 if (RefPtr
<Function
> fun
= mGlobal
->GetCountQueuingStrategySizeFunction()) {
67 // Note: Instead of eagerly allocating a size function for every global object
68 // we do it lazily once in this getter.
69 // After this point the steps refer to:
70 // https://streams.spec.whatwg.org/#count-queuing-strategy-size-function.
73 if (!jsapi
.Init(mGlobal
)) {
74 aRv
.ThrowUnknownError("Internal error");
77 JSContext
* cx
= jsapi
.cx();
79 // Step 1. Let steps be the following steps:
80 // Note: See CountQueuingStrategySize instead.
83 // ! CreateBuiltinFunction(steps, 0, "size", « »,
84 // globalObject’s relevant Realm).
85 JS::Rooted
<JSFunction
*> sizeFunction(
86 cx
, JS_NewFunction(cx
, CountQueuingStrategySize
, 0, 0, "size"));
88 aRv
.StealExceptionFromJSContext(cx
);
92 // Step 3. Set globalObject’s count queuing strategy size function to
93 // a Function that represents a reference to F,
94 // with callback context equal to globalObject’s relevant settings object.
95 JS::Rooted
<JSObject
*> funObj(cx
, JS_GetFunctionObject(sizeFunction
));
96 JS::Rooted
<JSObject
*> global(cx
, mGlobal
->GetGlobalJSObject());
97 RefPtr
<Function
> function
= new Function(cx
, funObj
, global
, mGlobal
);
98 mGlobal
->SetCountQueuingStrategySizeFunction(function
);
100 return function
.forget();
103 } // namespace mozilla::dom