Browse Source

LibDesktop: Make allowlist APIs return ErrorOr<void>

This makes it very smooth to use TRY() when setting up these lists,
as you can see in the rest of this commit. :^)
Andreas Kling 3 năm trước cách đây
mục cha
commit
b6f49924be

+ 2 - 5
Userland/Applications/Browser/main.cpp

@@ -54,11 +54,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     // Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
     // the user's downloads directory.
     // FIXME: This should go away with a standalone download manager at some point.
-    if (!Desktop::Launcher::add_allowed_url(URL::create_with_file_protocol(Core::StandardPaths::downloads_directory()))
-        || !Desktop::Launcher::seal_allowlist()) {
-        warnln("Failed to set up allowed launch URLs");
-        return 1;
-    }
+    TRY(Desktop::Launcher::add_allowed_url(URL::create_with_file_protocol(Core::StandardPaths::downloads_directory())));
+    TRY(Desktop::Launcher::seal_allowlist());
 
     TRY(Core::System::unveil("/home", "rwc"));
     TRY(Core::System::unveil("/res", "r"));

+ 2 - 7
Userland/Applications/FontEditor/main.cpp

@@ -24,13 +24,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
 
     auto app = TRY(GUI::Application::try_create(arguments));
 
-    if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
-            "/bin/Help",
-            { URL::create_with_file_protocol("/usr/share/man/man1/FontEditor.md") })
-        || !Desktop::Launcher::seal_allowlist()) {
-        warnln("Failed to set up allowed launch URLs");
-        return 1;
-    }
+    TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/FontEditor.md") }));
+    TRY(Desktop::Launcher::seal_allowlist());
 
     TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath", nullptr));
 

+ 3 - 15
Userland/Applications/ImageViewer/main.cpp

@@ -38,21 +38,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
 
     auto app = TRY(GUI::Application::try_create(arguments));
 
-    if (!Desktop::Launcher::add_allowed_handler_with_any_url("/bin/ImageViewer")) {
-        warnln("Failed to set up allowed launch URLs");
-        return 1;
-    }
-
-    if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
-            "/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/ImageViewer.md") })) {
-        warnln("Failed to set up allowed launch URLs");
-        return 1;
-    }
-
-    if (!Desktop::Launcher::seal_allowlist()) {
-        warnln("Failed to seal allowed launch URLs");
-        return 1;
-    }
+    TRY(Desktop::Launcher::add_allowed_handler_with_any_url("/bin/ImageViewer"));
+    TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/ImageViewer.md") }));
+    TRY(Desktop::Launcher::seal_allowlist());
 
     auto app_icon = GUI::Icon::default_icon("filetype-image");
 

+ 2 - 7
Userland/DevTools/Inspector/main.cpp

@@ -79,13 +79,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
         }
     }
 
-    if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
-            "/bin/Help",
-            { URL::create_with_file_protocol("/usr/share/man/man1/Inspector.md") })
-        || !Desktop::Launcher::seal_allowlist()) {
-        warnln("Failed to set up allowed launch URLs");
-        return 1;
-    }
+    TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/Inspector.md") }));
+    TRY(Desktop::Launcher::seal_allowlist());
 
     window->set_title("Inspector");
     window->resize(685, 500);

+ 3 - 7
Userland/DevTools/Playground/main.cpp

@@ -66,13 +66,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     auto app = TRY(GUI::Application::try_create(arguments));
 
     TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath unix", nullptr));
-    if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
-            "/bin/Help",
-            { URL::create_with_file_protocol("/usr/share/man/man1/Playground.md") })
-        || !Desktop::Launcher::seal_allowlist()) {
-        warnln("Failed to set up allowed launch URLs");
-        return 1;
-    }
+
+    TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/Playground.md") }));
+    TRY(Desktop::Launcher::seal_allowlist());
 
     TRY(Core::System::pledge("stdio thread recvfd sendfd rpath cpath wpath", nullptr));
 

+ 2 - 7
Userland/DevTools/Profiler/main.cpp

@@ -78,13 +78,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
 
     auto window = TRY(GUI::Window::try_create());
 
-    if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
-            "/bin/Help",
-            { URL::create_with_file_protocol("/usr/share/man/man1/Profiler.md") })
-        || !Desktop::Launcher::seal_allowlist()) {
-        warnln("Failed to set up allowed launch URLs");
-        return 1;
-    }
+    TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/Profiler.md") }));
+    TRY(Desktop::Launcher::seal_allowlist());
 
     window->set_title("Profiler");
     window->set_icon(app_icon.bitmap_for_size(16));

+ 16 - 24
Userland/Libraries/LibDesktop/Launcher.cpp

@@ -50,44 +50,36 @@ static LaunchServerConnection& connection()
     return connection;
 }
 
-bool Launcher::add_allowed_url(const URL& url)
+ErrorOr<void> Launcher::add_allowed_url(URL const& url)
 {
     auto response_or_error = connection().try_add_allowed_url(url);
-    if (response_or_error.is_error()) {
-        dbgln("Launcher::add_allowed_url: Failed");
-        return false;
-    }
-    return true;
+    if (response_or_error.is_error())
+        return Error::from_string_literal("Launcher::add_allowed_url: Failed"sv);
+    return {};
 }
 
-bool Launcher::add_allowed_handler_with_any_url(const String& handler)
+ErrorOr<void> Launcher::add_allowed_handler_with_any_url(String const& handler)
 {
     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;
-    }
-    return true;
+    if (response_or_error.is_error())
+        return Error::from_string_literal("Launcher::add_allowed_handler_with_any_url: Failed"sv);
+    return {};
 }
 
-bool Launcher::add_allowed_handler_with_only_specific_urls(const String& handler, const Vector<URL>& urls)
+ErrorOr<void> Launcher::add_allowed_handler_with_only_specific_urls(String const& handler, Vector<URL> const& urls)
 {
     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;
-    }
-    return true;
+    if (response_or_error.is_error())
+        return Error::from_string_literal("Launcher::add_allowed_handler_with_only_specific_urls: Failed"sv);
+    return {};
 }
 
-bool Launcher::seal_allowlist()
+ErrorOr<void> Launcher::seal_allowlist()
 {
     auto response_or_error = connection().try_seal_allowlist();
-    if (response_or_error.is_error()) {
-        dbgln("Launcher::seal_allowlist: Failed");
-        return false;
-    }
-    return true;
+    if (response_or_error.is_error())
+        return Error::from_string_literal("Launcher::seal_allowlist: Failed"sv);
+    return {};
 }
 
 bool Launcher::open(const URL& url, const String& handler_name)

+ 4 - 4
Userland/Libraries/LibDesktop/Launcher.h

@@ -31,10 +31,10 @@ public:
         static NonnullRefPtr<Details> from_details_str(const String&);
     };
 
-    [[nodiscard]] static bool add_allowed_url(const URL&);
-    [[nodiscard]] static bool add_allowed_handler_with_any_url(const String& handler);
-    [[nodiscard]] static bool add_allowed_handler_with_only_specific_urls(const String& handler, const Vector<URL>&);
-    [[nodiscard]] static bool seal_allowlist();
+    static ErrorOr<void> add_allowed_url(URL const&);
+    static ErrorOr<void> add_allowed_handler_with_any_url(String const& handler);
+    static ErrorOr<void> add_allowed_handler_with_only_specific_urls(String const& handler, Vector<URL> const&);
+    static ErrorOr<void> seal_allowlist();
     static bool open(const URL&, const String& handler_name = {});
     static bool open(const URL&, const Details& details);
     static Vector<String> get_handlers_for_url(const URL&);