Kaynağa Gözat

LibWeb: Implement ByteLengthQueuingStrategy highWaterMark property

Shannon Booth 2 yıl önce
ebeveyn
işleme
f86c3ab148

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -493,6 +493,7 @@ set(SOURCES
     SecureContexts/AbstractOperations.cpp
     SRI/SRI.cpp
     Streams/AbstractOperations.cpp
+    Streams/ByteLengthQueuingStrategy.cpp
     Streams/CountQueuingStrategy.cpp
     Streams/ReadableByteStreamController.cpp
     Streams/ReadableStream.cpp

+ 1 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -535,6 +535,7 @@ class Selection;
 }
 
 namespace Web::Streams {
+class ByteLengthQueuingStrategy;
 class CountQueuingStrategy;
 class ReadableByteStreamController;
 class ReadableStream;

+ 38 - 0
Userland/Libraries/LibWeb/Streams/ByteLengthQueuingStrategy.cpp

@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Bindings/ByteLengthQueuingStrategyPrototype.h>
+#include <LibWeb/Bindings/Intrinsics.h>
+#include <LibWeb/Streams/ByteLengthQueuingStrategy.h>
+#include <LibWeb/WebIDL/ExceptionOr.h>
+
+namespace Web::Streams {
+
+// https://streams.spec.whatwg.org/#blqs-constructor
+WebIDL::ExceptionOr<JS::NonnullGCPtr<ByteLengthQueuingStrategy>> ByteLengthQueuingStrategy::construct_impl(JS::Realm& realm, QueuingStrategyInit const& init)
+{
+    // The new ByteLengthQueuingStrategy(init) constructor steps are:
+    // 1. Set this.[[highWaterMark]] to init["highWaterMark"].
+    return MUST_OR_THROW_OOM(realm.heap().allocate<ByteLengthQueuingStrategy>(realm, realm, init.high_water_mark));
+}
+
+ByteLengthQueuingStrategy::ByteLengthQueuingStrategy(JS::Realm& realm, double high_water_mark)
+    : PlatformObject(realm)
+    , m_high_water_mark(high_water_mark)
+{
+}
+
+ByteLengthQueuingStrategy::~ByteLengthQueuingStrategy() = default;
+
+JS::ThrowCompletionOr<void> ByteLengthQueuingStrategy::initialize(JS::Realm& realm)
+{
+    MUST_OR_THROW_OOM(Base::initialize(realm));
+    set_prototype(&Bindings::ensure_web_prototype<Bindings::ByteLengthQueuingStrategyPrototype>(realm, "ByteLengthQueuingStrategy"));
+
+    return {};
+}
+
+}

+ 43 - 0
Userland/Libraries/LibWeb/Streams/ByteLengthQueuingStrategy.h

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Forward.h>
+#include <LibJS/Forward.h>
+#include <LibWeb/Bindings/PlatformObject.h>
+#include <LibWeb/Forward.h>
+#include <LibWeb/Streams/QueuingStrategyInit.h>
+
+namespace Web::Streams {
+
+// https://streams.spec.whatwg.org/#bytelengthqueuingstrategy
+class ByteLengthQueuingStrategy final : public Bindings::PlatformObject {
+    WEB_PLATFORM_OBJECT(ByteLengthQueuingStrategy, Bindings::PlatformObject);
+
+public:
+    static WebIDL::ExceptionOr<JS::NonnullGCPtr<ByteLengthQueuingStrategy>> construct_impl(JS::Realm&, QueuingStrategyInit const&);
+
+    virtual ~ByteLengthQueuingStrategy() override;
+
+    // https://streams.spec.whatwg.org/#blqs-high-water-mark
+    double high_water_mark() const
+    {
+        // The highWaterMark getter steps are:
+        // 1. Return this.[[highWaterMark]].
+        return m_high_water_mark;
+    }
+
+private:
+    explicit ByteLengthQueuingStrategy(JS::Realm&, double high_water_mark);
+
+    virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
+
+    // https://streams.spec.whatwg.org/#bytelengthqueuingstrategy-highwatermark
+    double m_high_water_mark { 0 };
+};
+
+}

+ 11 - 0
Userland/Libraries/LibWeb/Streams/ByteLengthQueuingStrategy.idl

@@ -0,0 +1,11 @@
+#import <Streams/QueuingStrategyInit.idl>
+
+// https://streams.spec.whatwg.org/#blqs-class-definition
+[Exposed=*]
+interface ByteLengthQueuingStrategy {
+    constructor(QueuingStrategyInit init);
+
+    readonly attribute unrestricted double highWaterMark;
+
+    // FIXME: readonly attribute Function size;
+};

+ 1 - 0
Userland/Libraries/LibWeb/idl_files.cmake

@@ -186,6 +186,7 @@ libweb_js_bindings(NavigationTiming/PerformanceTiming)
 libweb_js_bindings(PerformanceTimeline/PerformanceEntry)
 libweb_js_bindings(RequestIdleCallback/IdleDeadline)
 libweb_js_bindings(ResizeObserver/ResizeObserver)
+libweb_js_bindings(Streams/ByteLengthQueuingStrategy)
 libweb_js_bindings(Streams/CountQueuingStrategy)
 libweb_js_bindings(Streams/ReadableByteStreamController)
 libweb_js_bindings(Streams/ReadableStream)