1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:07:45 +00:00

SpiceAgent: Use ErrorOr<T> for on_message_received

This commit is contained in:
Caoimhe 2023-05-12 18:27:24 +01:00 committed by Andreas Kling
parent 0d82cc8a67
commit 7e3fd73410
2 changed files with 21 additions and 14 deletions

View file

@ -21,7 +21,10 @@ SpiceAgent::SpiceAgent(int fd, ConnectionToClipboardServer& connection)
{ {
m_notifier = Core::Notifier::construct(fd, Core::Notifier::Type::Read); m_notifier = Core::Notifier::construct(fd, Core::Notifier::Type::Read);
m_notifier->on_activation = [this] { m_notifier->on_activation = [this] {
on_message_received(); auto result = on_message_received();
if (result.is_error()) {
warnln("Failed to handle message: {}", result.release_error());
}
}; };
m_clipboard_connection.on_data_changed = [this] { m_clipboard_connection.on_data_changed = [this] {
if (m_just_set_clip) { if (m_just_set_clip) {
@ -54,12 +57,14 @@ Optional<SpiceAgent::ClipboardType> SpiceAgent::mime_type_to_clipboard_type(Depr
return {}; return {};
} }
void SpiceAgent::on_message_received() ErrorOr<void> SpiceAgent::on_message_received()
{ {
ChunkHeader header {}; ChunkHeader header {};
read_n(&header, sizeof(header)); read_n(&header, sizeof(header));
auto buffer = ByteBuffer::create_uninitialized(header.size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
auto buffer = TRY(ByteBuffer::create_uninitialized(header.size));
read_n(buffer.data(), buffer.size()); read_n(buffer.data(), buffer.size());
auto* message = reinterpret_cast<Message*>(buffer.data()); auto* message = reinterpret_cast<Message*>(buffer.data());
switch (message->type) { switch (message->type) {
case (u32)MessageType::AnnounceCapabilities: { case (u32)MessageType::AnnounceCapabilities: {
@ -108,7 +113,7 @@ void SpiceAgent::on_message_received()
} }
} }
if (found_type == ClipboardType::None) if (found_type == ClipboardType::None)
return; return {};
auto request_buffer = ClipboardRequest::make_buffer(found_type); auto request_buffer = ClipboardRequest::make_buffer(found_type);
send_message(request_buffer); send_message(request_buffer);
@ -117,7 +122,7 @@ void SpiceAgent::on_message_received()
case (u32)MessageType::Clipboard: { case (u32)MessageType::Clipboard: {
auto* clipboard_message = reinterpret_cast<Clipboard*>(message->data); auto* clipboard_message = reinterpret_cast<Clipboard*>(message->data);
auto type = (ClipboardType)clipboard_message->type; auto type = (ClipboardType)clipboard_message->type;
auto data_buffer = ByteBuffer::create_uninitialized(message->size - sizeof(u32)).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation. auto data_buffer = TRY(ByteBuffer::create_uninitialized(message->size - sizeof(u32)));
auto const total_bytes = message->size - sizeof(Clipboard); auto const total_bytes = message->size - sizeof(Clipboard);
auto bytes_copied = header.size - sizeof(Message) - sizeof(Clipboard); auto bytes_copied = header.size - sizeof(Message) - sizeof(Clipboard);
@ -135,37 +140,36 @@ void SpiceAgent::on_message_received()
if (data_buffer.is_empty()) { if (data_buffer.is_empty()) {
m_clipboard_connection.async_set_clipboard_data({}, "text/plain", {}); m_clipboard_connection.async_set_clipboard_data({}, "text/plain", {});
} else { } else {
auto anon_buffer_or_error = Core::AnonymousBuffer::create_with_size(data_buffer.size()); auto anon_buffer = TRY(Core::AnonymousBuffer::create_with_size(data_buffer.size()));
VERIFY(!anon_buffer_or_error.is_error());
auto anon_buffer = anon_buffer_or_error.release_value();
memcpy(anon_buffer.data<void>(), data_buffer.data(), data_buffer.size()); memcpy(anon_buffer.data<void>(), data_buffer.data(), data_buffer.size());
m_clipboard_connection.async_set_clipboard_data(anon_buffer, "text/plain", {}); m_clipboard_connection.async_set_clipboard_data(anon_buffer, "text/plain", {});
} }
return; return {};
} else { } else {
ErrorOr<Gfx::ImageFrameDescriptor> frame_or_error = Gfx::ImageFrameDescriptor {}; ErrorOr<Gfx::ImageFrameDescriptor> frame_or_error = Gfx::ImageFrameDescriptor {};
if (type == ClipboardType::PNG) { if (type == ClipboardType::PNG) {
if (Gfx::PNGImageDecoderPlugin::sniff({ data_buffer.data(), data_buffer.size() })) { if (Gfx::PNGImageDecoderPlugin::sniff({ data_buffer.data(), data_buffer.size() })) {
auto png_decoder = Gfx::PNGImageDecoderPlugin::create({ data_buffer.data(), data_buffer.size() }).release_value_but_fixme_should_propagate_errors(); auto png_decoder = TRY(Gfx::PNGImageDecoderPlugin::create({ data_buffer.data(), data_buffer.size() }));
if (!png_decoder->initialize().is_error()) if (!png_decoder->initialize().is_error())
frame_or_error = png_decoder->frame(0); frame_or_error = png_decoder->frame(0);
} }
} else if (type == ClipboardType::BMP) { } else if (type == ClipboardType::BMP) {
if (Gfx::BMPImageDecoderPlugin::sniff({ data_buffer.data(), data_buffer.size() })) { if (Gfx::BMPImageDecoderPlugin::sniff({ data_buffer.data(), data_buffer.size() })) {
auto bmp_decoder = Gfx::BMPImageDecoderPlugin::create({ data_buffer.data(), data_buffer.size() }).release_value_but_fixme_should_propagate_errors(); auto bmp_decoder = TRY(Gfx::BMPImageDecoderPlugin::create({ data_buffer.data(), data_buffer.size() }));
if (!bmp_decoder->initialize().is_error()) if (!bmp_decoder->initialize().is_error())
frame_or_error = bmp_decoder->frame(0); frame_or_error = bmp_decoder->frame(0);
} }
} else if (type == ClipboardType::JPEG) { } else if (type == ClipboardType::JPEG) {
if (Gfx::JPEGImageDecoderPlugin::sniff({ data_buffer.data(), data_buffer.size() })) { if (Gfx::JPEGImageDecoderPlugin::sniff({ data_buffer.data(), data_buffer.size() })) {
auto jpeg_decoder = Gfx::JPEGImageDecoderPlugin::create({ data_buffer.data(), data_buffer.size() }).release_value_but_fixme_should_propagate_errors(); auto jpeg_decoder = TRY(Gfx::JPEGImageDecoderPlugin::create({ data_buffer.data(), data_buffer.size() }));
if (!jpeg_decoder->initialize().is_error()) if (!jpeg_decoder->initialize().is_error())
frame_or_error = jpeg_decoder->frame(0); frame_or_error = jpeg_decoder->frame(0);
} }
} else { } else {
dbgln("Unknown clipboard type: {}", (u32)type); dbgln("Unknown clipboard type: {}", (u32)type);
return; return {};
} }
auto const& bitmap = frame_or_error.value().image; auto const& bitmap = frame_or_error.value().image;
m_clipboard_connection.set_bitmap(*bitmap); m_clipboard_connection.set_bitmap(*bitmap);
} }
@ -174,6 +178,8 @@ void SpiceAgent::on_message_received()
default: default:
dbgln("Unhandled message type {}", message->type); dbgln("Unhandled message type {}", message->type);
} }
return {};
} }
void SpiceAgent::read_n(void* dest, size_t n) void SpiceAgent::read_n(void* dest, size_t n)

View file

@ -119,7 +119,8 @@ private:
RefPtr<Core::Notifier> m_notifier; RefPtr<Core::Notifier> m_notifier;
ConnectionToClipboardServer& m_clipboard_connection; ConnectionToClipboardServer& m_clipboard_connection;
void on_message_received(); ErrorOr<void> on_message_received();
void send_message(ByteBuffer const& buffer); void send_message(ByteBuffer const& buffer);
bool m_just_set_clip { false }; bool m_just_set_clip { false };
void read_n(void* dest, size_t n); void read_n(void* dest, size_t n);