Launcher.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObject.h>
  7. #include <AK/URL.h>
  8. #include <LaunchServer/LaunchClientEndpoint.h>
  9. #include <LaunchServer/LaunchServerEndpoint.h>
  10. #include <LibDesktop/Launcher.h>
  11. #include <LibIPC/ServerConnection.h>
  12. #include <stdlib.h>
  13. namespace Desktop {
  14. auto Launcher::Details::from_details_str(const String& details_str) -> NonnullRefPtr<Details>
  15. {
  16. auto details = adopt_ref(*new Details);
  17. auto json = JsonValue::from_string(details_str);
  18. VERIFY(json.has_value());
  19. auto obj = json.value().as_object();
  20. details->executable = obj.get("executable").to_string();
  21. details->name = obj.get("name").to_string();
  22. if (auto type_value = obj.get_ptr("type")) {
  23. auto type_str = type_value->to_string();
  24. if (type_str == "app")
  25. details->launcher_type = LauncherType::Application;
  26. else if (type_str == "userpreferred")
  27. details->launcher_type = LauncherType::UserPreferred;
  28. else if (type_str == "userdefault")
  29. details->launcher_type = LauncherType::UserDefault;
  30. }
  31. return details;
  32. }
  33. class LaunchServerConnection final
  34. : public IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>
  35. , public LaunchClientEndpoint {
  36. C_OBJECT(LaunchServerConnection)
  37. private:
  38. LaunchServerConnection()
  39. : IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>(*this, "/tmp/portal/launch")
  40. {
  41. }
  42. };
  43. static LaunchServerConnection& connection()
  44. {
  45. static auto connection = LaunchServerConnection::construct();
  46. return connection;
  47. }
  48. bool Launcher::add_allowed_url(const URL& url)
  49. {
  50. auto response_or_error = connection().try_add_allowed_url(url);
  51. if (response_or_error.is_error()) {
  52. dbgln("Launcher::add_allowed_url: Failed");
  53. return false;
  54. }
  55. return true;
  56. }
  57. bool Launcher::add_allowed_handler_with_any_url(const String& handler)
  58. {
  59. auto response_or_error = connection().try_add_allowed_handler_with_any_url(handler);
  60. if (response_or_error.is_error()) {
  61. dbgln("Launcher::add_allowed_handler_with_any_url: Failed");
  62. return false;
  63. }
  64. return true;
  65. }
  66. bool Launcher::add_allowed_handler_with_only_specific_urls(const String& handler, const Vector<URL>& urls)
  67. {
  68. auto response_or_error = connection().try_add_allowed_handler_with_only_specific_urls(handler, urls);
  69. if (response_or_error.is_error()) {
  70. dbgln("Launcher::add_allowed_handler_with_only_specific_urls: Failed");
  71. return false;
  72. }
  73. return true;
  74. }
  75. bool Launcher::seal_allowlist()
  76. {
  77. auto response_or_error = connection().try_seal_allowlist();
  78. if (response_or_error.is_error()) {
  79. dbgln("Launcher::seal_allowlist: Failed");
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool Launcher::open(const URL& url, const String& handler_name)
  85. {
  86. return connection().open_url(url, handler_name);
  87. }
  88. bool Launcher::open(const URL& url, const Details& details)
  89. {
  90. VERIFY(details.launcher_type != LauncherType::Application); // Launcher should not be used to execute arbitrary applications
  91. return open(url, details.executable);
  92. }
  93. Vector<String> Launcher::get_handlers_for_url(const URL& url)
  94. {
  95. return connection().get_handlers_for_url(url.to_string());
  96. }
  97. auto Launcher::get_handlers_with_details_for_url(const URL& url) -> NonnullRefPtrVector<Details>
  98. {
  99. auto details = connection().get_handlers_with_details_for_url(url.to_string());
  100. NonnullRefPtrVector<Details> handlers_with_details;
  101. for (auto& value : details) {
  102. handlers_with_details.append(Details::from_details_str(value));
  103. }
  104. return handlers_with_details;
  105. }
  106. }