Truly independent web browser
LibWeb: Implement WebSocket.bufferedAmount
Andreas Klingb90e4767fcb6
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cppindex fde3e2bc7f3..b3d3181de53 100644--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp@@ -365,37 +365,70 @@ WebIDL::ExceptionOr<void> WebSocket::close(Optional<u16> code, Optional<Utf16Strreturn {};}+// https://websockets.spec.whatwg.org/#dom-websocket-bufferedamount+u64 WebSocket::buffered_amount() const+{+ // The bufferedAmount getter steps are to return the number of bytes of application data (UTF-8 text and binary+ // data) that have been queued using send() but that, as of the last time the event loop reached step 1, had not+ // yet been transmitted to the network.+ return m_buffered_amount;+}+// https://websockets.spec.whatwg.org/#dom-websocket-sendWebIDL::ExceptionOr<void> WebSocket::send(WebSocketSendData const& data){+ // 1. If this's ready state is CONNECTING, then throw an "InvalidStateError" DOMException.auto state = ready_state();if (state == Requests::WebSocket::ReadyState::Connecting)return WebIDL::InvalidStateError::create("Websocket is still CONNECTING"_utf16);- if (state == Requests::WebSocket::ReadyState::Open) {- ByteBuffer buffer_storage;- data.visit(- [this](Utf16String const& string) {- auto encoded_string = string.to_utf8();++ // 2. Run the appropriate set of steps from the following list:+ // NB: Every kind of data is sent if the WebSocket connection is established and the WebSocket closing handshake+ // has not yet started, and increases the bufferedAmount attribute by the number of bytes that make up the+ // message.+ bool is_open = state == Requests::WebSocket::ReadyState::Open;+ u64 message_size = 0;+ data.visit(+ [&](Utf16String const& string) {+ auto encoded_string = string.to_utf8();+ message_size = encoded_string.bytes().size();+ if (is_open)m_websocket->send(encoded_string);- },- [this, &buffer_storage](auto const& buffer_source_value) {- WebIDL::BufferSource buffer_source { WebIDL::BufferSourceVariant { buffer_source_value } };- ReadonlyBytes buffer;-- if (auto array_buffer = buffer_source.viewed_array_buffer(); array_buffer && !array_buffer->is_detached() && !buffer_source.is_out_of_bounds()) {- buffer_storage = MUST(ByteBuffer::create_uninitialized(buffer_source.byte_length()));- array_buffer->copy_to(buffer_source.byte_offset(), buffer_storage);- buffer = buffer_storage;- }-- m_websocket->send(buffer, false);- },- [this](GC::Ref<FileAPI::Blob> blob) {- m_websocket->send(blob->raw_bytes(), false);- });- // TODO : If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user agent must flag the WebSocket as full and then close the WebSocket connection.- // TODO : Any invocation of this method with a string argument that does not throw an exception must increase the bufferedAmount attribute by the number of bytes needed to express the argument as UTF-8.+ },+ [&](auto const& buffer_source_value) {+ WebIDL::BufferSource buffer_source { WebIDL::BufferSourceVariant { buffer_source_value } };+ auto array_buffer = buffer_source.viewed_array_buffer();+ bool has_bytes = array_buffer && !array_buffer->is_detached() && !buffer_source.is_out_of_bounds();+ message_size = has_bytes ? buffer_source.byte_length() : 0;+ if (!is_open)+ return;++ ByteBuffer buffer;+ if (has_bytes) {+ buffer = MUST(ByteBuffer::create_uninitialized(buffer_source.byte_length()));+ array_buffer->copy_to(buffer_source.byte_offset(), buffer);+ }+ m_websocket->send(buffer.bytes(), false);+ },+ [&](GC::Ref<FileAPI::Blob> blob) {+ auto bytes = blob->raw_bytes();+ message_size = bytes.size();+ if (is_open)+ m_websocket->send(bytes, false);+ });+ // TODO: If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user+ // agent must flag the WebSocket as full and then close the WebSocket connection.++ m_buffered_amount += message_size;++ // NB: A sent message is handed to the network process right away, so it counts as transmitted to the network once+ // the event loop has moved on from the current task.+ if (is_open && message_size > 0) {+ HTML::queue_a_task(HTML::Task::Source::WebSocket, nullptr, nullptr, GC::create_function(GC::Heap::the(), [self = GC::Ref { *this }, message_size] {+ self->m_buffered_amount -= message_size;+ }));}+return {};}diff --git a/Libraries/LibWeb/WebSockets/WebSocket.h b/Libraries/LibWeb/WebSockets/WebSocket.hindex b483b0b53f0..11e3986ea0b 100644--- a/Libraries/LibWeb/WebSockets/WebSocket.h+++ b/Libraries/LibWeb/WebSockets/WebSocket.h@@ -54,6 +54,7 @@ public:Requests::WebSocket::ReadyState ready_state() const;Utf16String extensions() const;WebIDL::ExceptionOr<Utf16String> protocol() const;+ u64 buffered_amount() const;Bindings::BinaryType binary_type() { return m_binary_type; }void set_binary_type(Bindings::BinaryType binary_type) { m_binary_type = binary_type; }@@ -89,6 +90,7 @@ private:URL::URL m_url;Requests::WebSocket::ReadyState m_ready_state { Requests::WebSocket::ReadyState::Connecting };Bindings::BinaryType m_binary_type { Bindings::BinaryType::Blob };+ u64 m_buffered_amount { 0 };RefPtr<Requests::WebSocket> m_websocket;GC::Ref<DOM::EventTarget> m_global_object;GC::ActivityRoot m_activity_root;diff --git a/Libraries/LibWeb/WebSockets/WebSocket.idl b/Libraries/LibWeb/WebSockets/WebSocket.idlindex b674025d454..59b31acdf08 100644--- a/Libraries/LibWeb/WebSockets/WebSocket.idl+++ b/Libraries/LibWeb/WebSockets/WebSocket.idl@@ -14,7 +14,7 @@ interface WebSocket : EventTarget {const unsigned short CLOSING = 2;const unsigned short CLOSED = 3;readonly attribute unsigned short readyState;- [FIXME] readonly attribute unsigned long long bufferedAmount;+ readonly attribute unsigned long long bufferedAmount;// networkingattribute EventHandler onopen;diff --git a/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.txt b/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.txtindex 0a263f8c609..0f8c89e5963 100644--- a/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.txt@@ -2,8 +2,7 @@ Harness status: OKFound 62 tests-60 Pass-2 Fail+62 PassPass idl_test setupPass idl_test validationPass WebSocket interface: existence and properties of interface object@@ -22,7 +21,7 @@ Pass WebSocket interface: constant CLOSING on interface prototype objectPass WebSocket interface: constant CLOSED on interface objectPass WebSocket interface: constant CLOSED on interface prototype objectPass WebSocket interface: attribute readyState-Fail WebSocket interface: attribute bufferedAmount+Pass WebSocket interface: attribute bufferedAmountPass WebSocket interface: attribute onopenPass WebSocket interface: attribute onerrorPass WebSocket interface: attribute onclose@@ -40,7 +39,7 @@ Pass WebSocket interface: new WebSocket("ws://invalid") must inherit property "OPass WebSocket interface: new WebSocket("ws://invalid") must inherit property "CLOSING" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "CLOSED" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "readyState" with the proper type-Fail WebSocket interface: new WebSocket("ws://invalid") must inherit property "bufferedAmount" with the proper type+Pass WebSocket interface: new WebSocket("ws://invalid") must inherit property "bufferedAmount" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "onopen" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "onerror" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "onclose" with the proper typediff --git a/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.worker.txtindex 0a263f8c609..0f8c89e5963 100644--- a/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.worker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/websockets/idlharness.any.worker.txt@@ -2,8 +2,7 @@ Harness status: OKFound 62 tests-60 Pass-2 Fail+62 PassPass idl_test setupPass idl_test validationPass WebSocket interface: existence and properties of interface object@@ -22,7 +21,7 @@ Pass WebSocket interface: constant CLOSING on interface prototype objectPass WebSocket interface: constant CLOSED on interface objectPass WebSocket interface: constant CLOSED on interface prototype objectPass WebSocket interface: attribute readyState-Fail WebSocket interface: attribute bufferedAmount+Pass WebSocket interface: attribute bufferedAmountPass WebSocket interface: attribute onopenPass WebSocket interface: attribute onerrorPass WebSocket interface: attribute onclose@@ -40,7 +39,7 @@ Pass WebSocket interface: new WebSocket("ws://invalid") must inherit property "OPass WebSocket interface: new WebSocket("ws://invalid") must inherit property "CLOSING" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "CLOSED" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "readyState" with the proper type-Fail WebSocket interface: new WebSocket("ws://invalid") must inherit property "bufferedAmount" with the proper type+Pass WebSocket interface: new WebSocket("ws://invalid") must inherit property "bufferedAmount" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "onopen" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "onerror" with the proper typePass WebSocket interface: new WebSocket("ws://invalid") must inherit property "onclose" with the proper type