1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:37:36 +00:00

QuickShow: Replace posix_spawn with LibDesktop::Launcher

When multiple images are dragged and dropped onto the image widget,
QuickShow will use LibDesktop::Launcher to launch a new instance
of QuickShow for each item, rather than spawn a child QuickShow
process for each item with posix_spawn.

This allows `proc` and `exec` pledges to be removed :^)
This commit is contained in:
Brendan Coles 2021-03-12 12:25:38 +00:00 committed by Andreas Kling
parent 612a5225fa
commit 8c0723960b
2 changed files with 24 additions and 20 deletions

View file

@ -4,4 +4,4 @@ set(SOURCES
) )
serenity_app(QuickShow ICON filetype-image) serenity_app(QuickShow ICON filetype-image)
target_link_libraries(QuickShow LibGUI LibGfx) target_link_libraries(QuickShow LibDesktop LibGUI LibGfx)

View file

@ -28,6 +28,7 @@
#include <AK/URL.h> #include <AK/URL.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/MimeData.h> #include <LibCore/MimeData.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
@ -45,24 +46,33 @@
#include <LibGfx/Palette.h> #include <LibGfx/Palette.h>
#include <LibGfx/Rect.h> #include <LibGfx/Rect.h>
#include <serenity.h> #include <serenity.h>
#include <spawn.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (pledge("stdio recvfd sendfd accept cpath rpath wpath unix cpath fattr proc exec thread", nullptr) < 0) { if (pledge("stdio recvfd sendfd accept rpath wpath cpath unix fattr thread", nullptr) < 0) {
perror("pledge"); perror("pledge");
return 1; return 1;
} }
auto app = GUI::Application::construct(argc, argv); auto app = GUI::Application::construct(argc, argv);
if (pledge("stdio recvfd sendfd accept cpath rpath wpath proc exec thread", nullptr) < 0) { if (pledge("stdio recvfd sendfd accept cpath rpath wpath unix thread", nullptr) < 0) {
perror("pledge"); perror("pledge");
return 1; return 1;
} }
if (!Desktop::Launcher::add_allowed_handler_with_any_url("/bin/QuickShow")) {
warnln("Failed to set up allowed launch URLs");
return 1;
}
if (!Desktop::Launcher::seal_allowlist()) {
warnln("Failed to seal allowed launch URLs");
return 1;
}
auto app_icon = GUI::Icon::default_icon("filetype-image"); auto app_icon = GUI::Icon::default_icon("filetype-image");
const char* path = nullptr; const char* path = nullptr;
@ -106,24 +116,18 @@ int main(int argc, char** argv)
widget.on_drop = [&](auto& event) { widget.on_drop = [&](auto& event) {
window->move_to_front(); window->move_to_front();
if (event.mime_data().has_urls()) { if (!event.mime_data().has_urls())
auto urls = event.mime_data().urls(); return;
if (!urls.is_empty()) { auto urls = event.mime_data().urls();
auto url = urls.first();
widget.load_from_file(url.path());
}
pid_t child; if (urls.is_empty())
for (size_t i = 1; i < urls.size(); ++i) { return;
const char* argv[] = { "/bin/QuickShow", urls[i].path().characters(), nullptr };
if ((errno = posix_spawn(&child, "/bin/QuickShow", nullptr, nullptr, const_cast<char**>(argv), environ))) { widget.load_from_file(urls.first().path());
perror("posix_spawn");
} else { for (size_t i = 1; i < urls.size(); ++i) {
if (disown(child) < 0) Desktop::Launcher::open(URL::create_with_file_protocol(urls[i].path().characters()), "/bin/QuickShow");
perror("disown");
}
}
} }
}; };
widget.on_doubleclick = [&] { widget.on_doubleclick = [&] {