Launcher.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if (auto icons_value = obj.get_ptr("icons")) {
  52. icons_value->as_object().for_each_member([&](auto& name, auto& value) {
  53. details->icons.set(name, value.to_string());
  54. });
  55. }
  56. return details;
  57. }
  58. class LaunchServerConnection : public IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>
  59. , public LaunchClientEndpoint {
  60. C_OBJECT(LaunchServerConnection)
  61. public:
  62. virtual void handshake() override
  63. {
  64. auto response = send_sync<Messages::LaunchServer::Greet>();
  65. set_my_client_id(response->client_id());
  66. }
  67. private:
  68. LaunchServerConnection()
  69. : IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>(*this, "/tmp/portal/launch")
  70. {
  71. }
  72. virtual void handle(const Messages::LaunchClient::Dummy&) override { }
  73. };
  74. bool Launcher::open(const URL& url, const String& handler_name)
  75. {
  76. auto connection = LaunchServerConnection::construct();
  77. return connection->send_sync<Messages::LaunchServer::OpenURL>(url, handler_name)->response();
  78. }
  79. bool Launcher::open(const URL& url, const Details& details)
  80. {
  81. ASSERT(details.launcher_type != LauncherType::Application); // Launcher should not be used to execute arbitrary applications
  82. return open(url, details.executable);
  83. }
  84. Vector<String> Launcher::get_handlers_for_url(const URL& url)
  85. {
  86. auto connection = LaunchServerConnection::construct();
  87. return connection->send_sync<Messages::LaunchServer::GetHandlersForURL>(url.to_string())->handlers();
  88. }
  89. auto Launcher::get_handlers_with_details_for_url(const URL& url) -> NonnullRefPtrVector<Details>
  90. {
  91. auto connection = LaunchServerConnection::construct();
  92. auto details = connection->send_sync<Messages::LaunchServer::GetHandlersWithDetailsForURL>(url.to_string())->handlers_details();
  93. NonnullRefPtrVector<Details> handlers_with_details;
  94. for (auto& value : details) {
  95. handlers_with_details.append(Details::from_details_str(value));
  96. }
  97. return handlers_with_details;
  98. }
  99. }