Bug 1743207 - Implement ByteLengthQueuingStrategy interface r?smaug draft
authorMatthew Gaudet <mgaudet@mozilla.com>
Fri, 26 Nov 2021 15:41:19 -0700
changeset 4097849 58e742ac272503f21f1cfe04909ad6b12c5af5d0
parent 4097848 980c94436576ae439b903cc140e0069492c6c3ce
child 4097850 ef2bb70a00cfe4e5f0fc8d8f092a48cd28333ee1
push id755115
push usermgaudet@mozilla.com
push dateFri, 26 Nov 2021 22:43:38 +0000
treeherdertry@99dda87ae19f [default view] [failures only]
reviewerssmaug
bugs1743207
milestone96.0a1
Bug 1743207 - Implement ByteLengthQueuingStrategy interface r?smaug
dom/streams/ByteLengthQueuingStrategy.cpp
dom/streams/ByteLengthQueuingStrategy.h
dom/streams/moz.build
dom/webidl/QueuingStrategy.webidl
new file mode 100644
--- /dev/null
+++ b/dom/streams/ByteLengthQueuingStrategy.cpp
@@ -0,0 +1,62 @@
+/* -*- 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/ByteLengthQueuingStrategy.h"
+#include "mozilla/dom/FunctionBinding.h"
+#include "mozilla/dom/QueuingStrategyBinding.h"
+#include "nsCOMPtr.h"
+#include "nsISupports.h"
+
+#include "js/TypeDecls.h"
+#include "js/PropertyAndElement.h"
+
+namespace mozilla {
+namespace dom {
+
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ByteLengthQueuingStrategy)
+NS_IMPL_ADDREF_INHERITED(ByteLengthQueuingStrategy, BaseQueuingStrategy)
+NS_IMPL_RELEASE_INHERITED(ByteLengthQueuingStrategy, BaseQueuingStrategy)
+
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ByteLengthQueuingStrategy)
+  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
+NS_INTERFACE_MAP_END_INHERITING(BaseQueuingStrategy)
+
+/* static */
+already_AddRefed<ByteLengthQueuingStrategy>
+ByteLengthQueuingStrategy::Constructor(const GlobalObject& aGlobal,
+                                       const QueuingStrategyInit& aInit) {
+  RefPtr<ByteLengthQueuingStrategy> strategy = new ByteLengthQueuingStrategy(
+      aGlobal.GetAsSupports(), aInit.mHighWaterMark);
+  return strategy.forget();
+}
+
+JSObject* ByteLengthQueuingStrategy::WrapObject(
+    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
+  return ByteLengthQueuingStrategy_Binding::Wrap(aCx, this, aGivenProto);
+}
+
+static bool ByteLengthQueuingStrategySize(JSContext* cx, unsigned argc,
+                                          JS::Value* vp) {
+  // https://streams.spec.whatwg.org/#blqs-internal-slots
+  JS::CallArgs args = CallArgsFromVp(argc, vp);
+
+  JS::RootedObject chunkObj(cx, JS::ToObject(cx, args.get(0)));
+  if (!chunkObj) {
+    return false;
+  }
+
+  // Step 1: Return ? GetV(chunk, "byteLength").
+  return JS_GetProperty(cx, chunkObj, "byteLength", args.rval());
+}
+
+already_AddRefed<Function> ByteLengthQueuingStrategy::GetSize(
+    ErrorResult& aRv) {
+  return GetSizeHelper(".ByteLengthQueuingStrategySizeFunction",
+                       ByteLengthQueuingStrategySize, 1, aRv);
+}
+
+}  // namespace dom
+}  // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/streams/ByteLengthQueuingStrategy.h
@@ -0,0 +1,51 @@
+/* -*- 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/. */
+
+#ifndef mozilla_dom_ByteLengthQueuingStrategy_h
+#define mozilla_dom_ByteLengthQueuingStrategy_h
+
+#include "js/TypeDecls.h"
+#include "mozilla/Attributes.h"
+#include "mozilla/ErrorResult.h"
+#include "mozilla/dom/BaseQueuingStrategy.h"
+#include "mozilla/dom/BindingDeclarations.h"
+#include "mozilla/dom/FunctionBinding.h"
+#include "mozilla/dom/QueuingStrategyBinding.h"
+#include "nsCycleCollectionParticipant.h"
+#include "nsWrapperCache.h"
+#include "nsIGlobalObject.h"
+
+namespace mozilla {
+namespace dom {
+class ByteLengthQueuingStrategy final : public BaseQueuingStrategy,
+                                        public nsWrapperCache {
+ public:
+  NS_DECL_ISUPPORTS_INHERITED
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
+      ByteLengthQueuingStrategy, BaseQueuingStrategy)
+
+ public:
+  explicit ByteLengthQueuingStrategy(nsISupports* aGlobal,
+                                     double aHighWaterMark)
+      : BaseQueuingStrategy(aGlobal, aHighWaterMark) {}
+
+ protected:
+  ~ByteLengthQueuingStrategy() = default;
+
+ public:
+  JSObject* WrapObject(JSContext* aCx,
+                       JS::Handle<JSObject*> aGivenProto) override;
+
+  static already_AddRefed<ByteLengthQueuingStrategy> Constructor(
+      const GlobalObject& aGlobal, const QueuingStrategyInit& aInit);
+
+  already_AddRefed<Function> GetSize(ErrorResult& aRv);
+};
+
+}  // namespace dom
+}  // namespace mozilla
+
+#endif
\ No newline at end of file
--- a/dom/streams/moz.build
+++ b/dom/streams/moz.build
@@ -4,16 +4,17 @@
 # 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/.
 
 with Files("**"):
     BUG_COMPONENT = ("Core", "DOM: Streams")
 
 EXPORTS.mozilla.dom += [
     "BaseQueuingStrategy.h",
+    "ByteLengthQueuingStrategy.h",
     "ByteStreamHelpers.h",
     "CountQueuingStrategy.h",
     "NativeUnderlyingSource.h",
     "QueueWithSizes.h",
     "ReadableByteStreamController.h",
     "ReadableStream.h",
     "ReadableStreamBYOBReader.h",
     "ReadableStreamBYOBRequest.h",
@@ -24,16 +25,17 @@ EXPORTS.mozilla.dom += [
     "ReadableStreamTee.h",
     "ReadIntoRequest.h",
     "ReadRequest.h",
     "TeeState.h",
     "UnderlyingSourceCallbackHelpers.h",
 ]
 
 UNIFIED_SOURCES += [
+    "ByteLengthQueuingStrategy.cpp",
     "ByteStreamHelpers.cpp",
     "CountQueuingStrategy.cpp",
     "ReadableByteStreamController.cpp",
     "ReadableStream.cpp",
     "ReadableStreamBYOBReader.cpp",
     "ReadableStreamBYOBRequest.cpp",
     "ReadableStreamDefaultController.cpp",
     "ReadableStreamDefaultReader.cpp",
--- a/dom/webidl/QueuingStrategy.webidl
+++ b/dom/webidl/QueuingStrategy.webidl
@@ -14,8 +14,18 @@ dictionary QueuingStrategyInit {
 interface CountQueuingStrategy {
   constructor(QueuingStrategyInit init);
 
   readonly attribute unrestricted double highWaterMark;
 
   [Throws]
   readonly attribute Function size;
 };
+
+[Exposed=(Window,Worker,Worklet)]
+interface ByteLengthQueuingStrategy {
+  constructor(QueuingStrategyInit init);
+
+  readonly attribute unrestricted double highWaterMark;
+
+  [Throws]
+  readonly attribute Function size;
+};
\ No newline at end of file