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

Userland: Add try_* IPC handlers

This enables calling auto-generated IPC methods in a way that doesn't
crash the client if the peer disconnects.
This commit is contained in:
Gunnar Beutner 2021-05-03 16:51:42 +02:00 committed by Andreas Kling
parent 34cf5cf07f
commit 8a6db55e79
4 changed files with 62 additions and 27 deletions

View file

@ -59,8 +59,8 @@ static LaunchServerConnection& connection()
bool Launcher::add_allowed_url(const URL& url)
{
auto response = connection().send_sync_but_allow_failure<Messages::LaunchServer::AddAllowedUrl>(url);
if (!response) {
auto response_or_error = connection().try_add_allowed_url(url);
if (response_or_error.is_error()) {
dbgln("Launcher::add_allowed_url: Failed");
return false;
}
@ -69,8 +69,8 @@ bool Launcher::add_allowed_url(const URL& url)
bool Launcher::add_allowed_handler_with_any_url(const String& handler)
{
auto response = connection().send_sync_but_allow_failure<Messages::LaunchServer::AddAllowedHandlerWithAnyUrl>(handler);
if (!response) {
auto response_or_error = connection().try_add_allowed_handler_with_any_url(handler);
if (response_or_error.is_error()) {
dbgln("Launcher::add_allowed_handler_with_any_url: Failed");
return false;
}
@ -79,8 +79,8 @@ bool Launcher::add_allowed_handler_with_any_url(const String& handler)
bool Launcher::add_allowed_handler_with_only_specific_urls(const String& handler, const Vector<URL>& urls)
{
auto response = connection().send_sync_but_allow_failure<Messages::LaunchServer::AddAllowedHandlerWithOnlySpecificUrls>(handler, urls);
if (!response) {
auto response_or_error = connection().try_add_allowed_handler_with_only_specific_urls(handler, urls);
if (response_or_error.is_error()) {
dbgln("Launcher::add_allowed_handler_with_only_specific_urls: Failed");
return false;
}
@ -89,8 +89,8 @@ bool Launcher::add_allowed_handler_with_only_specific_urls(const String& handler
bool Launcher::seal_allowlist()
{
auto response = connection().send_sync_but_allow_failure<Messages::LaunchServer::SealAllowlist>();
if (!response) {
auto response_or_error = connection().try_seal_allowlist();
if (response_or_error.is_error()) {
dbgln("Launcher::seal_allowlist: Failed");
return false;
}

View file

@ -37,6 +37,10 @@ struct MessageBuffer {
Vector<RefPtr<AutoCloseFileDescriptor>> fds;
};
enum class ErrorCode : u32 {
PeerDisconnected
};
class Message {
public:
virtual ~Message();

View file

@ -42,21 +42,23 @@ Optional<DecodedImage> Client::decode_image(const ByteBuffer& encoded_data)
}
memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
auto response = send_sync_but_allow_failure<Messages::ImageDecoderServer::DecodeImage>(move(encoded_buffer));
auto response_or_error = try_decode_image(move(encoded_buffer));
if (!response) {
if (response_or_error.is_error()) {
dbgln("ImageDecoder died heroically");
return {};
}
auto& response = response_or_error.value();
DecodedImage image;
image.is_animated = response->is_animated();
image.loop_count = response->loop_count();
image.frames.resize(response->bitmaps().size());
image.is_animated = response.is_animated();
image.loop_count = response.loop_count();
image.frames.resize(response.bitmaps().size());
for (size_t i = 0; i < image.frames.size(); ++i) {
auto& frame = image.frames[i];
frame.bitmap = response->bitmaps()[i].bitmap();
frame.duration = response->durations()[i];
frame.bitmap = response.bitmaps()[i].bitmap();
frame.duration = response.durations()[i];
}
return image;
}