no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / streams / ByteLengthQueuingStrategy.cpp
blob858f6d664bc8af8bf1845e870c4dba0e4c9ac0a7
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/ByteLengthQueuingStrategy.h"
8 #include "mozilla/dom/FunctionBinding.h"
9 #include "mozilla/dom/QueuingStrategyBinding.h"
10 #include "nsCOMPtr.h"
11 #include "nsISupports.h"
13 #include "js/TypeDecls.h"
14 #include "js/PropertyAndElement.h"
16 namespace mozilla::dom {
18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(ByteLengthQueuingStrategy,
19 BaseQueuingStrategy)
20 NS_IMPL_ADDREF_INHERITED(ByteLengthQueuingStrategy, BaseQueuingStrategy)
21 NS_IMPL_RELEASE_INHERITED(ByteLengthQueuingStrategy, BaseQueuingStrategy)
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ByteLengthQueuingStrategy)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_END_INHERITING(BaseQueuingStrategy)
27 /* static */
28 already_AddRefed<ByteLengthQueuingStrategy>
29 ByteLengthQueuingStrategy::Constructor(const GlobalObject& aGlobal,
30 const QueuingStrategyInit& aInit) {
31 RefPtr<ByteLengthQueuingStrategy> strategy = new ByteLengthQueuingStrategy(
32 aGlobal.GetAsSupports(), aInit.mHighWaterMark);
33 return strategy.forget();
36 JSObject* ByteLengthQueuingStrategy::WrapObject(
37 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
38 return ByteLengthQueuingStrategy_Binding::Wrap(aCx, this, aGivenProto);
41 static bool ByteLengthQueuingStrategySize(JSContext* cx, unsigned argc,
42 JS::Value* vp) {
43 // https://streams.spec.whatwg.org/#blqs-internal-slots
44 JS::CallArgs args = CallArgsFromVp(argc, vp);
46 // Step 1: Return ? GetV(chunk, "byteLength").
47 JS::Rooted<JSObject*> chunkObj(cx, JS::ToObject(cx, args.get(0)));
48 if (!chunkObj) {
49 return false;
52 return JS_GetProperty(cx, chunkObj, "byteLength", args.rval());
55 // https://streams.spec.whatwg.org/#blqs-size
56 already_AddRefed<Function> ByteLengthQueuingStrategy::GetSize(
57 ErrorResult& aRv) {
58 // Step 1. Return this's relevant global object's ByteLength queuing strategy
59 // size function.
60 if (RefPtr<Function> fun =
61 mGlobal->GetByteLengthQueuingStrategySizeFunction()) {
62 return fun.forget();
65 // Note: Instead of eagerly allocating a size function for every global object
66 // we do it lazily once in this getter.
67 // After this point the steps refer to:
68 // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
70 AutoJSAPI jsapi;
71 if (!jsapi.Init(mGlobal)) {
72 aRv.ThrowUnknownError("Internal error");
73 return nullptr;
75 JSContext* cx = jsapi.cx();
77 // Step 1. Let steps be the following steps, given chunk
78 // Note: See ByteLengthQueuingStrategySize instead.
80 // Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
81 // globalObject’s relevant Realm).
82 JS::Rooted<JSFunction*> sizeFunction(
83 cx, JS_NewFunction(cx, ByteLengthQueuingStrategySize, 1, 0, "size"));
84 if (!sizeFunction) {
85 aRv.StealExceptionFromJSContext(cx);
86 return nullptr;
89 // Step 3. Set globalObject’s byte length queuing strategy size function to
90 // a Function that represents a reference to F,
91 // with callback context equal to globalObject’s relevant settings object.
92 JS::Rooted<JSObject*> funObj(cx, JS_GetFunctionObject(sizeFunction));
93 JS::Rooted<JSObject*> global(cx, mGlobal->GetGlobalJSObject());
94 RefPtr<Function> function = new Function(cx, funObj, global, mGlobal);
95 mGlobal->SetByteLengthQueuingStrategySizeFunction(function);
97 return function.forget();
100 } // namespace mozilla::dom