Launcher.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonObject.h>
  27. #include <AK/URL.h>
  28. #include <LaunchServer/LaunchClientEndpoint.h>
  29. #include <LaunchServer/LaunchServerEndpoint.h>
  30. #include <LibDesktop/Launcher.h>
  31. #include <LibIPC/ServerConnection.h>
  32. #include <stdlib.h>
  33. namespace Desktop {
  34. auto Launcher::Details::from_details_str(const String& details_str) -> NonnullRefPtr<Details>
  35. {
  36. auto details = adopt(*new Details);
  37. auto json = JsonValue::from_string(details_str);
  38. ASSERT(json.has_value());
  39. auto obj = json.value().as_object();
  40. details->executable = obj.get("executable").to_string();
  41. details->name = obj.get("name").to_string();
  42. if (auto type_value = obj.get_ptr("type")) {
  43. auto type_str = type_value->to_string();
  44. if (type_str == "app")
  45. details->launcher_type = LauncherType::Application;
  46. else if (type_str == "userpreferred")
  47. details->launcher_type = LauncherType::UserPreferred;
  48. else if (type_str == "userdefault")
  49. details->launcher_type = LauncherType::UserDefault;
  50. }
  51. return details;
  52. }
  53. class LaunchServerConnection : public IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>
  54. , public LaunchClientEndpoint {
  55. C_OBJECT(LaunchServerConnection)
  56. public:
  57. virtual void handshake() override
  58. {
  59. send_sync<Messages::LaunchServer::Greet>();
  60. }
  61. private:
  62. LaunchServerConnection()
  63. : IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>(*this, "/tmp/portal/launch")
  64. {
  65. }
  66. virtual void handle(const Messages::LaunchClient::Dummy&) override { }
  67. };
  68. static LaunchServerConnection& connection()
  69. {
  70. static auto connection = LaunchServerConnection::construct();
  71. return connection;
  72. }
  73. bool Launcher::add_allowed_url(const URL& url)
  74. {
  75. auto response = connection().send_sync<Messages::LaunchServer::AddAllowedURL>(url);
  76. if (!response) {
  77. dbgln("Launcher::add_allowed_url: Failed");
  78. return false;
  79. }
  80. return true;
  81. }
  82. bool Launcher::add_allowed_handler_with_any_url(const String& handler)
  83. {
  84. auto response = connection().send_sync<Messages::LaunchServer::AddAllowedHandlerWithAnyURL>(handler);
  85. if (!response) {
  86. dbgln("Launcher::add_allowed_handler_with_any_url: Failed");
  87. return false;
  88. }
  89. return true;
  90. }
  91. bool Launcher::add_allowed_handler_with_only_specific_urls(const String& handler, const Vector<URL>& urls)
  92. {
  93. auto response = connection().send_sync<Messages::LaunchServer::AddAllowedHandlerWithOnlySpecificURLs>(handler, urls);
  94. if (!response) {
  95. dbgln("Launcher::add_allowed_handler_with_only_specific_urls: Failed");
  96. return false;
  97. }
  98. return true;
  99. }
  100. bool Launcher::seal_allowlist()
  101. {
  102. auto response = connection().send_sync<Messages::LaunchServer::SealAllowlist>();
  103. if (!response) {
  104. dbgln("Launcher::seal_allowlist: Failed");
  105. return false;
  106. }
  107. return true;
  108. }
  109. bool Launcher::open(const URL& url, const String& handler_name)
  110. {
  111. return connection().send_sync<Messages::LaunchServer::OpenURL>(url, handler_name)->response();
  112. }
  113. bool Launcher::open(const URL& url, const Details& details)
  114. {
  115. ASSERT(details.launcher_type != LauncherType::Application); // Launcher should not be used to execute arbitrary applications
  116. return open(url, details.executable);
  117. }
  118. Vector<String> Launcher::get_handlers_for_url(const URL& url)
  119. {
  120. return connection().send_sync<Messages::LaunchServer::GetHandlersForURL>(url.to_string())->handlers();
  121. }
  122. auto Launcher::get_handlers_with_details_for_url(const URL& url) -> NonnullRefPtrVector<Details>
  123. {
  124. auto details = connection().send_sync<Messages::LaunchServer::GetHandlersWithDetailsForURL>(url.to_string())->handlers_details();
  125. NonnullRefPtrVector<Details> handlers_with_details;
  126. for (auto& value : details) {
  127. handlers_with_details.append(Details::from_details_str(value));
  128. }
  129. return handlers_with_details;
  130. }
  131. }