ladybird/Libraries/LibWeb/HTML/StructuredSerialize.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

105 lines
3.1 KiB
C++

/*
* Copyright (c) 2022, Daniel Ehrenberg <dan@littledan.dev>
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
* Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/MemoryStream.h>
#include <AK/Vector.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibIPC/Message.h>
#include <LibJS/Forward.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/StructuredSerializeTypes.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::HTML {
class TransferDataEncoder {
public:
explicit TransferDataEncoder();
explicit TransferDataEncoder(IPC::MessageBuffer&&);
template<typename T>
void encode(T const& value)
{
MUST(m_encoder.encode(value));
}
void append(SerializationRecord&&);
void extend(Vector<TransferDataEncoder>);
IPC::MessageBuffer const& buffer() const { return m_buffer; }
IPC::MessageBuffer take_buffer() { return move(m_buffer); }
private:
IPC::MessageBuffer m_buffer;
IPC::Encoder m_encoder;
};
class TransferDataDecoder {
public:
explicit TransferDataDecoder(SerializationRecord const&);
explicit TransferDataDecoder(TransferDataEncoder&&);
template<typename T>
T decode()
{
static_assert(!IsSame<T, ByteBuffer>, "Use decode_buffer to handle OOM");
return MUST(m_decoder.decode<T>());
}
WebIDL::ExceptionOr<ByteBuffer> decode_buffer(JS::Realm&);
private:
IPC::MessageBuffer m_buffer;
FixedMemoryStream m_stream;
Queue<IPC::File> m_files;
IPC::Decoder m_decoder;
};
struct SerializedTransferRecord {
SerializationRecord serialized;
Vector<TransferDataEncoder> transfer_data_holders;
};
struct DeserializedTransferRecord {
JS::Value deserialized;
Vector<GC::Root<JS::Object>> transferred_values;
};
WebIDL::ExceptionOr<SerializationRecord> structured_serialize(JS::VM&, JS::Value);
WebIDL::ExceptionOr<SerializationRecord> structured_serialize_for_storage(JS::VM&, JS::Value);
WebIDL::ExceptionOr<SerializationRecord> structured_serialize_internal(JS::VM&, JS::Value, bool for_storage, SerializationMemory&);
WebIDL::ExceptionOr<JS::Value> structured_deserialize(JS::VM&, SerializationRecord const&, JS::Realm&, Optional<DeserializationMemory> = {});
WebIDL::ExceptionOr<JS::Value> structured_deserialize_internal(JS::VM&, TransferDataDecoder&, JS::Realm&, DeserializationMemory&);
WebIDL::ExceptionOr<SerializedTransferRecord> structured_serialize_with_transfer(JS::VM&, JS::Value, Vector<GC::Root<JS::Object>> const& transfer_list);
WebIDL::ExceptionOr<DeserializedTransferRecord> structured_deserialize_with_transfer(SerializedTransferRecord&, JS::Realm&);
WebIDL::ExceptionOr<JS::Value> structured_deserialize_with_transfer_internal(TransferDataDecoder&, JS::Realm&);
}
namespace IPC {
template<>
ErrorOr<void> encode(Encoder&, Web::HTML::TransferDataEncoder const&);
template<>
ErrorOr<Web::HTML::TransferDataEncoder> decode(Decoder&);
template<>
ErrorOr<void> encode(Encoder&, Web::HTML::SerializedTransferRecord const&);
template<>
ErrorOr<Web::HTML::SerializedTransferRecord> decode(Decoder&);
}