Skip to content

Truly independent web browser

LibWeb/FileAPI: Add Blob::immutable_bytes()

Luke Wilde5ce4a56c99b4
diff --git a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
index 9adf989926a..f61882dcc91 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/file_api/blob.rs
@@ -98,7 +98,8 @@ pub(crate) struct ForeignBytesAbi {
pub(crate) length: usize,
}
-/// Bytes that a C++ Web::FileAPI::BlobBytes owns: those that C++ created the Blob with, or moved to shared memory.
+/// Bytes that a C++ Web::FileAPI::BlobBytes owns: those that C++ created the Blob with, or moved into immutable bytes
+/// or shared memory.
struct ForeignBytes {
owner: NonNull<c_void>,
data: *const u8,
@@ -149,7 +150,8 @@ impl ByteSequence {
#[gc(crate = libjs_rust)]
pub(crate) struct Blob {
// https://w3c.github.io/FileAPI/#dfn-byte-sequence
- // NB: The bytes move to shared memory the first time they are shared, and stay there.
+ // NB: The bytes move into immutable bytes or shared memory the first time C++ shares them, and stay in shared
+ // memory once there.
#[gc(untraced)]
byte_sequence: RefCell<ByteSequence>,
// https://w3c.github.io/FileAPI/#dfn-type
diff --git a/Libraries/LibWeb/Bindings/Rust/src/file_api/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/file_api/exports.rs
index 02c6c9b1095..88836a29131 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/file_api/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/file_api/exports.rs
@@ -66,7 +66,8 @@ pub unsafe extern "C" fn web_blob_create(vm: *mut c_void, bytes: ForeignBytesAbi
cell_into_abi(allocate(vm, |_| blob))
}
-/// The byte sequence of the Blob or File, which lives until the object dies or its bytes move to shared memory.
+/// The byte sequence of the Blob or File, which lives until the object dies or C++ moves its bytes into immutable
+/// bytes or shared memory.
///
/// # Safety
///
diff --git a/Libraries/LibWeb/FileAPI/Blob.cpp b/Libraries/LibWeb/FileAPI/Blob.cpp
index c5cfbb40d1b..06184b4b4a9 100644
--- a/Libraries/LibWeb/FileAPI/Blob.cpp
+++ b/Libraries/LibWeb/FileAPI/Blob.cpp
@@ -31,7 +31,7 @@ namespace Web::FileAPI {
struct BlobBytes {
AK_ALLOC_WITH_KMALLOC;
- Variant<ByteBuffer, Core::AnonymousBuffer> bytes;
+ Variant<ByteBuffer, Core::AnonymousBuffer, Core::ImmutableBytes> bytes;
};
// The bytes as Bindings/Rust/src/file_api/blob.rs takes them over.
@@ -41,7 +41,7 @@ struct BlobBytesABI {
size_t length { 0 };
};
-static BlobBytesABI hand_over(Variant<ByteBuffer, Core::AnonymousBuffer> bytes)
+static BlobBytesABI hand_over(Variant<ByteBuffer, Core::AnonymousBuffer, Core::ImmutableBytes> bytes)
{
auto* owner = new BlobBytes { move(bytes) };
auto data = owner->bytes.visit([](auto const& bytes) { return bytes.bytes(); });
@@ -105,6 +105,20 @@ ErrorOr<Core::AnonymousBuffer> Blob::shared_bytes()
return buffer;
}
+ErrorOr<Core::ImmutableBytes> Blob::immutable_bytes()
+{
+ if (auto* owner = web_blob_foreign_bytes_owner(this)) {
+ if (auto const* bytes = owner->bytes.get_pointer<Core::ImmutableBytes>())
+ return *bytes;
+ if (auto const* buffer = owner->bytes.get_pointer<Core::AnonymousBuffer>())
+ return Core::ImmutableBytes::adopt_anonymous_buffer(*buffer);
+ }
+
+ auto bytes = TRY(Core::ImmutableBytes::copy(raw_bytes()));
+ web_blob_set_foreign_bytes(this, hand_over(bytes));
+ return bytes;
+}
+
GC::Ref<Blob> Blob::slice_blob(Optional<i64> start, Optional<i64> end, Optional<Utf16String> content_type) const
{
auto has_content_type = content_type.has_value();
diff --git a/Libraries/LibWeb/FileAPI/Blob.h b/Libraries/LibWeb/FileAPI/Blob.h
index c4a4468ee71..64fdeb8f5df 100644
--- a/Libraries/LibWeb/FileAPI/Blob.h
+++ b/Libraries/LibWeb/FileAPI/Blob.h
@@ -9,6 +9,7 @@
#include <AK/ByteBuffer.h>
#include <AK/Utf16String.h>
#include <LibCore/AnonymousBuffer.h>
+#include <LibCore/ImmutableBytes.h>
#include <LibWeb/Bindings/Blob.h>
#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/Export.h>
@@ -35,6 +36,9 @@ public:
ReadonlyBytes raw_bytes() const LIFETIME_BOUND;
ErrorOr<Core::AnonymousBuffer> shared_bytes();
+ // The Blob's bytes, which are in shared memory only if they already were.
+ ErrorOr<Core::ImmutableBytes> immutable_bytes();
+
// https://w3c.github.io/FileAPI/#slice-blob
GC::Ref<Blob> slice_blob(Optional<i64> start, Optional<i64> end, Optional<Utf16String> content_type) const;
diff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txt
index 5e08a27554f..bf6659dc922 100644
--- a/Tests/LibWeb/CMakeLists.txt
+++ b/Tests/LibWeb/CMakeLists.txt
@@ -1,6 +1,7 @@
set(TEST_SOURCES
BenchmarkDisplayListRecording.cpp
TestAudioWorkletPipe.cpp
+ TestBlobBytes.cpp
TestContentBlocker.cpp
TestControlMessageQueue.cpp
TestCSSIDSpeed.cpp
@@ -65,6 +66,7 @@ endforeach()
target_link_libraries(BenchmarkDisplayListRecording PRIVATE LibGC LibGfx LibIPC LibJS LibURL LibWebView)
target_link_libraries(TestAudioWorkletPipe PRIVATE LibCore LibThreading)
+target_link_libraries(TestBlobBytes PRIVATE LibCore LibGC LibJS)
target_link_libraries(TestContentBlocker PRIVATE LibURL)
target_link_libraries(TestStorageAreaMirror PRIVATE LibURL LibWebCommon)
target_link_libraries(TestFetchEngineMessages PRIVATE LibCore LibIPC LibRequests)
diff --git a/Tests/LibWeb/TestBlobBytes.cpp b/Tests/LibWeb/TestBlobBytes.cpp
new file mode 100644
index 00000000000..8e0a87f0adc
--- /dev/null
+++ b/Tests/LibWeb/TestBlobBytes.cpp
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ByteBuffer.h>
+#include <AK/StringView.h>
+#include <LibCore/AnonymousBuffer.h>
+#include <LibCore/ImmutableBytes.h>
+#include <LibGC/Root.h>
+#include <LibJS/Runtime/Realm.h>
+#include <LibTest/TestCase.h>
+#include <LibWeb/Bindings/MainThreadVM.h>
+#include <LibWeb/FileAPI/Blob.h>
+
+static void ensure_vm()
+{
+ static GC::Root<JS::Realm> realm;
+ if (!realm)
+ realm = Web::Bindings::create_a_simple_javascript_realm();
+}
+
+static GC::Ref<Web::FileAPI::Blob> blob_with_bytes(StringView bytes)
+{
+ ensure_vm();
+ return Web::FileAPI::Blob::create(MUST(ByteBuffer::copy(bytes.bytes())), Utf16String {});
+}
+
+TEST_CASE(a_blob_shares_its_immutable_bytes)
+{
+ auto blob = blob_with_bytes("hello"sv);
+
+ auto bytes = MUST(blob->immutable_bytes());
+ EXPECT_EQ(StringView { bytes.bytes() }, "hello"sv);
+ EXPECT_EQ(blob->raw_bytes().data(), bytes.bytes().data());
+
+ auto again = MUST(blob->immutable_bytes());
+ EXPECT_EQ(again.bytes().data(), bytes.bytes().data());
+}
+
+TEST_CASE(a_sliced_blob_shares_its_immutable_bytes)
+{
+ auto blob = blob_with_bytes("hello"sv);
+ auto slice = blob->slice_blob(1, 4, {});
+
+ auto bytes = MUST(slice->immutable_bytes());
+ EXPECT_EQ(StringView { bytes.bytes() }, "ell"sv);
+ EXPECT_EQ(slice->raw_bytes().data(), bytes.bytes().data());
+ EXPECT_EQ(StringView { blob->raw_bytes() }, "hello"sv);
+}
+
+TEST_CASE(immutable_bytes_stay_in_a_blob_s_shared_memory)
+{
+ ensure_vm();
+ auto buffer = MUST(Core::AnonymousBuffer::create_with_size(3));
+ "abc"sv.bytes().copy_to({ buffer.data<u8>(), buffer.size() });
+ auto blob = Web::FileAPI::Blob::create(buffer, Utf16String {});
+
+ auto bytes = MUST(blob->immutable_bytes());
+ EXPECT_EQ(StringView { bytes.bytes() }, "abc"sv);
+ EXPECT_EQ(bytes.bytes().data(), buffer.data<u8>());
+
+ auto shared = MUST(blob->shared_bytes());
+ EXPECT_EQ(shared.data<u8>(), buffer.data<u8>());
+}
+
+TEST_CASE(shared_bytes_copy_immutable_bytes_into_shared_memory)
+{
+ auto blob = blob_with_bytes("hello"sv);
+ auto bytes = MUST(blob->immutable_bytes());
+
+ auto shared = MUST(blob->shared_bytes());
+ EXPECT_EQ(StringView { shared.bytes() }, "hello"sv);
+ EXPECT_EQ(blob->raw_bytes().data(), shared.data<u8>());
+ EXPECT_EQ(StringView { bytes.bytes() }, "hello"sv);
+
+ auto again = MUST(blob->immutable_bytes());
+ EXPECT_EQ(again.bytes().data(), shared.data<u8>());
+}
+
+TEST_CASE(an_empty_blob_has_empty_immutable_bytes)
+{
+ auto blob = blob_with_bytes(""sv);
+
+ auto bytes = MUST(blob->immutable_bytes());
+ EXPECT(bytes.is_empty());
+ EXPECT_EQ(blob->size(), 0u);
+}