Skip to content

Truly independent web browser

5.9 KB
Raw Blame
/*
 * Copyright (c) 2022, Daniel Ehrenberg <[email protected]>
 * Copyright (c) 2022, Andrew Kaster <[email protected]>
 * Copyright (c) 2023-2024, Kenneth Myhra <[email protected]>
 * Copyright (c) 2023, Idan Horowitz <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Array.h>
#include <AK/ByteBuffer.h>
#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibGC/Forward.h>
#include <LibIPC/Encoder.h>
#include <LibIPC/File.h>
#include <LibIPC/Forward.h>
#include <LibIPC/Message.h>
#include <LibWebCommon/Export.h>

namespace Web::HTML {

// NB: References that are only meaningful inside the process that produced a serialization record, such as LibWeb's
//     same-agent SharedArrayBuffer aliases. It is never IPC-encoded, and is opaque outside of LibWeb.
class WEBCOMMON_API SameAgentSerializationSideTable : public RefCounted<SameAgentSerializationSideTable> {
public:
    virtual ~SameAgentSerializationSideTable();
};

struct IPCSerializationRecord {
    IPC::MessageDataType data;

    // Same-agent SharedArrayBuffer aliases. This is intentionally not IPC-encoded; cross-process
    // records fall back to the byte copy in `data`.
    RefPtr<SameAgentSerializationSideTable const> same_agent_side_table;

    IPCSerializationRecord() = default;
    explicit IPCSerializationRecord(IPC::MessageDataType data)
        : data(move(data))
    {
    }
};

enum class ValueTag : u8 {
    // These values are part of the stable storage serialization format.
    // Do not reorder or reuse values; leave removed tags reserved.
    Empty = 0, // Unused, for ease of catching bugs.

    UndefinedPrimitive = 1,
    NullPrimitive = 2,
    BooleanPrimitive = 3,
    NumberPrimitive = 4,
    StringPrimitive = 5,
    BigIntPrimitive = 6,

    BooleanObject = 7,
    NumberObject = 8,
    StringObject = 9,
    BigIntObject = 10,
    DateObject = 11,
    RegExpObject = 12,
    MapObject = 13,
    SetObject = 14,
    ArrayObject = 15,
    ErrorObject = 16,
    Object = 17,
    ObjectReference = 18,

    GrowableSharedArrayBuffer = 19,
    SharedArrayBuffer = 20,
    ResizeableArrayBuffer = 21,
    ArrayBuffer = 22,
    ArrayBufferView = 23,

    SerializableObject = 24,

    Int32Primitive = 25,

    // Object/Array property-list terminator, kept outside the value-tag range.
    EndObject = 0xFF,
};

// The on-disk version of the LBSC storage format. Bumping this is a deliberate wire change.
static constexpr u64 storage_format_version = 1;

static constexpr Array<u8, 4> storage_format_magic = { 'L', 'B', 'S', 'C' };

// Flags are for optional envelope features (e.g. compression); changes to the value encoding bump the version instead.
static constexpr u64 storage_format_flags = 0;

struct StorageSerializationRecord {
    AK_ALLOC_WITH_KMALLOC;

    ByteBuffer data;

    StorageSerializationRecord() = default;
    explicit StorageSerializationRecord(ByteBuffer data)
        : data(move(data))
    {
    }

    bool is_empty() const { return data.is_empty(); }

    bool operator==(StorageSerializationRecord const&) const = default;
};

// Writes the magic, version, and flags that every storage serialization record begins with.
WEBCOMMON_API void append_storage_format_prologue(ByteBuffer&);

// NB: StructuredSerializeForStorage(undefined) and StructuredSerializeForStorage(null), for processes without a JS VM.
WEBCOMMON_API StorageSerializationRecord storage_serialization_record_for_undefined();
WEBCOMMON_API StorageSerializationRecord storage_serialization_record_for_null();

class WEBCOMMON_API TransferDataEncoder {
    AK_ALLOC_WITH_KMALLOC;

public:
    explicit TransferDataEncoder();
    explicit TransferDataEncoder(IPC::MessageBuffer&&);

    template<typename T>
    ErrorOr<void> encode(T const& value)
    {
        VERIFY(!m_buffer_has_been_taken);
        return m_encoder.encode(value);
    }

    IPC::MessageBuffer const& buffer() const;
    IPC::MessageBuffer take_buffer() const;

private:
    mutable IPC::MessageBuffer m_buffer;
    mutable bool m_buffer_has_been_taken { false };
    IPC::Encoder m_encoder;
};

// AD-HOC: What keeps the SharedArrayBuffer stores that a message names alive while the message is on its way, for as
//         long as this or a copy of it in any process is open. It gives no access to the stores, whose bytes never leave
//         their process. See shared_backing_store.rs in LibJS.
struct WEBCOMMON_API SharedStoreLifeline {
    static SharedStoreLifeline adopting_fd(int fd);

    int fd() const;

    Optional<IPC::File> file;
};

struct SerializedTransferRecord {
    AK_ALLOC_WITH_KMALLOC;

    IPCSerializationRecord serialized;
    Vector<TransferDataEncoder> transfer_data_holders;
    // AD-HOC: Shared memory that hands the pixels of transferred ImageBitmaps over, by index from the data holders.
    Vector<Core::AnonymousBuffer> shared_buffers;
    SharedStoreLifeline shared_store_lifeline;
};

}

namespace IPC {

template<>
WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::HTML::SharedStoreLifeline const&);

template<>
WEBCOMMON_API ErrorOr<Web::HTML::SharedStoreLifeline> decode(Decoder&);

template<>
WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::HTML::IPCSerializationRecord const&);

template<>
WEBCOMMON_API ErrorOr<Web::HTML::IPCSerializationRecord> decode(Decoder&);

template<>
WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::HTML::StorageSerializationRecord const&);

template<>
WEBCOMMON_API ErrorOr<Web::HTML::StorageSerializationRecord> decode(Decoder&);

template<>
WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::HTML::TransferDataEncoder const&);

template<>
WEBCOMMON_API ErrorOr<Web::HTML::TransferDataEncoder> decode(Decoder&);

template<>
WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::HTML::SerializedTransferRecord const&);

template<>
WEBCOMMON_API ErrorOr<Web::HTML::SerializedTransferRecord> decode(Decoder&);

}

GC_UNTRACED_CAPTURE(Web::HTML::TransferDataEncoder);