ladybird/Libraries/LibWeb/FileAPI/FileList.h
Timothy Flynn 64abc6101d LibWeb+WebWorker: Use IPC mechanics for structured serialization
Our structured serialization implementation had its own bespoke encoder
and decoder to serialize JS values. It also used a u32 buffer under the
hood, which made using its structures a bit awkward. We had previously
worked around its data structures in transferable streams, which nested
transfers of MessagePort instances. We basically had to add hooks into
the MessagePort to route to the correct transfer receiving steps, and
we could not invoke the correct AOs directly as the spec dictates.

We now use IPC mechanics to encode and decode data. This works because,
although we are encoding JS values, we are only ultimately encoding
primitive and basic AK types. The resulting data structures actually
enforce that we implement transferable streams exactly as the spec is
worded (I had planned to do that in a separate commit, but the fallout
of this patch actually required that change).
2025-07-18 10:09:02 -04:00

61 lines
1.8 KiB
C++

/*
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibGC/Ptr.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/FileAPI/File.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::FileAPI {
class FileList
: public Bindings::PlatformObject
, public Bindings::Serializable {
WEB_PLATFORM_OBJECT(FileList, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(FileList);
public:
[[nodiscard]] static GC::Ref<FileList> create(JS::Realm&);
void add_file(GC::Ref<File> file) { m_files.append(file); }
virtual ~FileList() override;
// https://w3c.github.io/FileAPI/#dfn-length
WebIDL::UnsignedLong length() const { return m_files.size(); }
// https://w3c.github.io/FileAPI/#dfn-item
File* item(size_t index)
{
return index < m_files.size() ? m_files[index].ptr() : nullptr;
}
// https://w3c.github.io/FileAPI/#dfn-item
File const* item(size_t index) const
{
return index < m_files.size() ? m_files[index].ptr() : nullptr;
}
virtual Optional<JS::Value> item_value(size_t index) const override;
virtual HTML::SerializeType serialize_type() const override { return HTML::SerializeType::FileList; }
virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::TransferDataEncoder&, bool for_storage, HTML::SerializationMemory&) override;
virtual WebIDL::ExceptionOr<void> deserialization_steps(HTML::TransferDataDecoder&, HTML::DeserializationMemory&) override;
private:
explicit FileList(JS::Realm&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Vector<GC::Ref<File>> m_files;
};
}