Launcher.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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).release_value_but_fixme_should_propagate_errors();
  18. auto const& obj = json.as_object();
  19. details->executable = obj.get("executable").to_string();
  20. details->name = obj.get("name").to_string();
  21. if (auto type_value = obj.get_ptr("type")) {
  22. auto type_str = type_value->to_string();
  23. if (type_str == "app")
  24. details->launcher_type = LauncherType::Application;
  25. else if (type_str == "userpreferred")
  26. details->launcher_type = LauncherType::UserPreferred;
  27. else if (type_str == "userdefault")
  28. details->launcher_type = LauncherType::UserDefault;
  29. }
  30. return details;
  31. }
  32. class LaunchServerConnection final
  33. : public IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>
  34. , public LaunchClientEndpoint {
  35. C_OBJECT(LaunchServerConnection)
  36. private:
  37. LaunchServerConnection()
  38. : IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>(*this, "/tmp/portal/launch")
  39. {
  40. }
  41. };
  42. static LaunchServerConnection& connection()
  43. {
  44. static auto connection = LaunchServerConnection::construct();
  45. return connection;
  46. }
  47. bool Launcher::add_allowed_url(const URL& url)
  48. {
  49. auto response_or_error = connection().try_add_allowed_url(url);
  50. if (response_or_error.is_error()) {
  51. dbgln("Launcher::add_allowed_url: Failed");
  52. return false;
  53. }
  54. return true;
  55. }
  56. bool Launcher::add_allowed_handler_with_any_url(const String& handler)
  57. {
  58. auto response_or_error = connection().try_add_allowed_handler_with_any_url(handler);
  59. if (response_or_error.is_error()) {
  60. dbgln("Launcher::add_allowed_handler_with_any_url: Failed");
  61. return false;
  62. }
  63. return true;
  64. }
  65. bool Launcher::add_allowed_handler_with_only_specific_urls(const String& handler, const Vector<URL>& urls)
  66. {
  67. auto response_or_error = connection().try_add_allowed_handler_with_only_specific_urls(handler, urls);
  68. if (response_or_error.is_error()) {
  69. dbgln("Launcher::add_allowed_handler_with_only_specific_urls: Failed");
  70. return false;
  71. }
  72. return true;
  73. }
  74. bool Launcher::seal_allowlist()
  75. {
  76. auto response_or_error = connection().try_seal_allowlist();
  77. if (response_or_error.is_error()) {
  78. dbgln("Launcher::seal_allowlist: Failed");
  79. return false;
  80. }
  81. return true;
  82. }
  83. bool Launcher::open(const URL& url, const String& handler_name)
  84. {
  85. return connection().open_url(url, handler_name);
  86. }
  87. bool Launcher::open(const URL& url, const Details& details)
  88. {
  89. VERIFY(details.launcher_type != LauncherType::Application); // Launcher should not be used to execute arbitrary applications
  90. return open(url, details.executable);
  91. }
  92. Vector<String> Launcher::get_handlers_for_url(const URL& url)
  93. {
  94. return connection().get_handlers_for_url(url.to_string());
  95. }
  96. auto Launcher::get_handlers_with_details_for_url(const URL& url) -> NonnullRefPtrVector<Details>
  97. {
  98. auto details = connection().get_handlers_with_details_for_url(url.to_string());
  99. NonnullRefPtrVector<Details> handlers_with_details;
  100. for (auto& value : details) {
  101. handlers_with_details.append(Details::from_details_str(value));
  102. }
  103. return handlers_with_details;
  104. }
  105. }