ladybird/Libraries/LibWeb/HTML/SelectedFile.cpp
Timothy Flynn f0d7e28f55 LibWeb+LibWebView+UI: Move SelectedFile factory to LibWebView
This factory is only used by the UI. An upcoming commit will make it
depend on LibFileSystem. LibWeb currently does not link LibFileSystem,
and doing so would push LibWeb in the wrong direction (we should be
doing less file IO in LibWeb, not more).
2026-05-20 21:09:22 +02:00

58 lines
1.4 KiB
C++

/*
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/File.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibWeb/HTML/SelectedFile.h>
namespace Web::HTML {
SelectedFile::SelectedFile(ByteString name, ByteBuffer contents)
: m_name(move(name))
, m_file_or_contents(move(contents))
{
}
SelectedFile::SelectedFile(ByteString name, IPC::File file)
: m_name(move(name))
, m_file_or_contents(move(file))
{
}
ByteBuffer SelectedFile::take_contents()
{
VERIFY(m_file_or_contents.has<ByteBuffer>());
return move(m_file_or_contents.get<ByteBuffer>());
}
}
template<>
ErrorOr<void> IPC::encode(Encoder& encoder, Web::HTML::SelectedFile const& file)
{
TRY(encoder.encode(file.name()));
TRY(encoder.encode(file.file_or_contents()));
return {};
}
template<>
ErrorOr<Web::HTML::SelectedFile> IPC::decode(Decoder& decoder)
{
auto name = TRY(decoder.decode<ByteString>());
auto file_or_contents = TRY((decoder.decode<Variant<IPC::File, ByteBuffer>>()));
ByteBuffer contents;
if (file_or_contents.has<IPC::File>()) {
auto file = TRY(Core::File::adopt_fd(file_or_contents.get<IPC::File>().take_fd(), Core::File::OpenMode::Read));
contents = TRY(file->read_until_eof());
} else {
contents = move(file_or_contents.get<ByteBuffer>());
}
return Web::HTML::SelectedFile { move(name), move(contents) };
}