2020-04-26 20:30:01 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-26 20:30:01 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-07-13 18:55:09 -06:00
|
|
|
#include <AK/JsonObject.h>
|
2020-04-26 20:30:01 +01:00
|
|
|
#include <AK/URL.h>
|
|
|
|
|
#include <LaunchServer/LaunchClientEndpoint.h>
|
|
|
|
|
#include <LaunchServer/LaunchServerEndpoint.h>
|
|
|
|
|
#include <LibDesktop/Launcher.h>
|
|
|
|
|
#include <LibIPC/ServerConnection.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
namespace Desktop {
|
|
|
|
|
|
2020-07-13 18:55:09 -06:00
|
|
|
auto Launcher::Details::from_details_str(const String& details_str) -> NonnullRefPtr<Details>
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
auto details = adopt_ref(*new Details);
|
2020-07-13 18:55:09 -06:00
|
|
|
auto json = JsonValue::from_string(details_str);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(json.has_value());
|
2020-07-13 18:55:09 -06:00
|
|
|
auto obj = json.value().as_object();
|
|
|
|
|
details->executable = obj.get("executable").to_string();
|
|
|
|
|
details->name = obj.get("name").to_string();
|
|
|
|
|
if (auto type_value = obj.get_ptr("type")) {
|
|
|
|
|
auto type_str = type_value->to_string();
|
2020-07-14 09:36:00 -06:00
|
|
|
if (type_str == "app")
|
|
|
|
|
details->launcher_type = LauncherType::Application;
|
|
|
|
|
else if (type_str == "userpreferred")
|
2020-07-13 18:55:09 -06:00
|
|
|
details->launcher_type = LauncherType::UserPreferred;
|
|
|
|
|
else if (type_str == "userdefault")
|
|
|
|
|
details->launcher_type = LauncherType::UserDefault;
|
|
|
|
|
}
|
|
|
|
|
return details;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 20:30:01 +01:00
|
|
|
class LaunchServerConnection : public IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>
|
|
|
|
|
, public LaunchClientEndpoint {
|
|
|
|
|
C_OBJECT(LaunchServerConnection)
|
|
|
|
|
private:
|
|
|
|
|
LaunchServerConnection()
|
|
|
|
|
: IPC::ServerConnection<LaunchClientEndpoint, LaunchServerEndpoint>(*this, "/tmp/portal/launch")
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-05-02 19:54:34 +02:00
|
|
|
virtual void dummy() override { }
|
2020-04-26 20:30:01 +01:00
|
|
|
};
|
|
|
|
|
|
2021-01-03 11:39:33 +01:00
|
|
|
static LaunchServerConnection& connection()
|
|
|
|
|
{
|
|
|
|
|
static auto connection = LaunchServerConnection::construct();
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 12:03:13 +01:00
|
|
|
bool Launcher::add_allowed_url(const URL& url)
|
|
|
|
|
{
|
2021-05-03 16:51:42 +02:00
|
|
|
auto response_or_error = connection().try_add_allowed_url(url);
|
|
|
|
|
if (response_or_error.is_error()) {
|
2021-01-03 12:03:13 +01:00
|
|
|
dbgln("Launcher::add_allowed_url: Failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 11:39:33 +01:00
|
|
|
bool Launcher::add_allowed_handler_with_any_url(const String& handler)
|
|
|
|
|
{
|
2021-05-03 16:51:42 +02:00
|
|
|
auto response_or_error = connection().try_add_allowed_handler_with_any_url(handler);
|
|
|
|
|
if (response_or_error.is_error()) {
|
2021-01-03 11:39:33 +01:00
|
|
|
dbgln("Launcher::add_allowed_handler_with_any_url: Failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Launcher::add_allowed_handler_with_only_specific_urls(const String& handler, const Vector<URL>& urls)
|
|
|
|
|
{
|
2021-05-03 16:51:42 +02:00
|
|
|
auto response_or_error = connection().try_add_allowed_handler_with_only_specific_urls(handler, urls);
|
|
|
|
|
if (response_or_error.is_error()) {
|
2021-01-03 11:39:33 +01:00
|
|
|
dbgln("Launcher::add_allowed_handler_with_only_specific_urls: Failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 12:03:13 +01:00
|
|
|
bool Launcher::seal_allowlist()
|
2021-01-03 11:39:33 +01:00
|
|
|
{
|
2021-05-03 16:51:42 +02:00
|
|
|
auto response_or_error = connection().try_seal_allowlist();
|
|
|
|
|
if (response_or_error.is_error()) {
|
2021-01-03 12:03:13 +01:00
|
|
|
dbgln("Launcher::seal_allowlist: Failed");
|
2021-01-03 11:39:33 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 14:16:43 +01:00
|
|
|
bool Launcher::open(const URL& url, const String& handler_name)
|
2020-04-26 20:30:01 +01:00
|
|
|
{
|
2021-05-03 13:55:29 +02:00
|
|
|
return connection().open_url(url, handler_name);
|
2020-04-26 20:30:01 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 18:55:09 -06:00
|
|
|
bool Launcher::open(const URL& url, const Details& details)
|
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(details.launcher_type != LauncherType::Application); // Launcher should not be used to execute arbitrary applications
|
2020-07-13 18:55:09 -06:00
|
|
|
return open(url, details.executable);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 18:43:55 +02:00
|
|
|
Vector<String> Launcher::get_handlers_for_url(const URL& url)
|
|
|
|
|
{
|
2021-05-03 13:55:29 +02:00
|
|
|
return connection().get_handlers_for_url(url.to_string());
|
2020-05-12 18:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 18:55:09 -06:00
|
|
|
auto Launcher::get_handlers_with_details_for_url(const URL& url) -> NonnullRefPtrVector<Details>
|
|
|
|
|
{
|
2021-05-03 13:55:29 +02:00
|
|
|
auto details = connection().get_handlers_with_details_for_url(url.to_string());
|
2020-07-13 18:55:09 -06:00
|
|
|
NonnullRefPtrVector<Details> handlers_with_details;
|
|
|
|
|
for (auto& value : details) {
|
|
|
|
|
handlers_with_details.append(Details::from_details_str(value));
|
|
|
|
|
}
|
|
|
|
|
return handlers_with_details;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 20:30:01 +01:00
|
|
|
}
|