Make PortalAutostart asynchronous

This commit is contained in:
Ilya Fedin 2023-09-03 18:12:33 +04:00 committed by John Preston
parent bf255c0e00
commit e52e1672e8

View file

@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mainwindow.h" #include "mainwindow.h"
#include "storage/localstorage.h" #include "storage/localstorage.h"
#include "core/launcher.h" #include "core/launcher.h"
#include "core/sandbox.h"
#include "core/core_settings.h" #include "core/core_settings.h"
#include "core/update_checker.h" #include "core/update_checker.h"
#include "webview/platform/linux/webview_linux_webkitgtk.h" #include "webview/platform/linux/webview_linux_webkitgtk.h"
@ -52,16 +53,33 @@ using Platform::internal::WaylandIntegration;
namespace Platform { namespace Platform {
namespace { namespace {
bool PortalAutostart(bool start, bool silent) { void PortalAutostart(bool enabled, Fn<void(bool)> done) {
if (cExeName().isEmpty()) { if (cExeName().isEmpty()) {
return false; if (done) {
done(false);
}
return;
} }
auto error = false; const auto connection = [&] {
try { try {
const auto connection = Gio::DBus::Connection::get_sync( return Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::SESSION); Gio::DBus::BusType::SESSION);
} catch (const std::exception &e) {
if (done) {
LOG(("Portal Autostart Error: %1").arg(
QString::fromStdString(e.what())));
}
return Glib::RefPtr<Gio::DBus::Connection>();
}
}();
if (!connection) {
if (done) {
done(false);
}
return;
}
const auto handleToken = Glib::ustring("tdesktop") const auto handleToken = Glib::ustring("tdesktop")
+ std::to_string(base::RandomValue<uint>()); + std::to_string(base::RandomValue<uint>());
@ -79,7 +97,7 @@ bool PortalAutostart(bool start, bool silent) {
options["reason"] = Glib::create_variant( options["reason"] = Glib::create_variant(
Glib::ustring( Glib::ustring(
tr::lng_settings_auto_start(tr::now).toStdString())); tr::lng_settings_auto_start(tr::now).toStdString()));
options["autostart"] = Glib::create_variant(start); options["autostart"] = Glib::create_variant(enabled);
options["commandline"] = Glib::create_variant(commandline); options["commandline"] = Glib::create_variant(commandline);
options["dbus-activatable"] = Glib::create_variant(false); options["dbus-activatable"] = Glib::create_variant(false);
@ -93,49 +111,55 @@ bool PortalAutostart(bool start, bool silent) {
+ '/' + '/'
+ handleToken; + handleToken;
const auto loop = Glib::MainLoop::create(); const auto window = std::make_shared<QWidget>();
window->setAttribute(Qt::WA_DontShowOnScreen);
window->setWindowModality(Qt::ApplicationModal);
window->show();
const auto signalId = connection->signal_subscribe( const auto signalId = std::make_shared<uint>();
[&]( *signalId = connection->signal_subscribe(
[=](
const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::RefPtr<Gio::DBus::Connection> &connection,
const Glib::ustring &sender_name, const Glib::ustring &sender_name,
const Glib::ustring &object_path, const Glib::ustring &object_path,
const Glib::ustring &interface_name, const Glib::ustring &interface_name,
const Glib::ustring &signal_name, const Glib::ustring &signal_name,
const Glib::VariantContainerBase &parameters) { const Glib::VariantContainerBase &parameters) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
(void)window; // don't destroy until finish
try { try {
const auto response = parameters.get_child( const auto response = parameters.get_child(
0 0
).get_dynamic<uint>(); ).get_dynamic<uint>();
if (response) { if (response) {
if (!silent) { if (done) {
LOG(("Portal Autostart Error: Request denied")); LOG(("Portal Autostart Error: Request denied"));
done(false);
} }
error = true; } else if (done) {
done(enabled);
} }
} catch (const std::exception &e) { } catch (const std::exception &e) {
if (!silent) { if (done) {
LOG(("Portal Autostart Error: %1").arg( LOG(("Portal Autostart Error: %1").arg(
QString::fromStdString(e.what()))); QString::fromStdString(e.what())));
done(false);
} }
error = true;
} }
loop->quit(); if (*signalId) {
connection->signal_unsubscribe(*signalId);
}
});
}, },
base::Platform::XDP::kService, base::Platform::XDP::kService,
base::Platform::XDP::kRequestInterface, base::Platform::XDP::kRequestInterface,
"Response", "Response",
requestPath); requestPath);
const auto signalGuard = gsl::finally([&] { connection->call(
if (signalId != 0) {
connection->signal_unsubscribe(signalId);
}
});
connection->call_sync(
base::Platform::XDP::kObjectPath, base::Platform::XDP::kObjectPath,
"org.freedesktop.portal.Background", "org.freedesktop.portal.Background",
"RequestBackground", "RequestBackground",
@ -143,24 +167,24 @@ bool PortalAutostart(bool start, bool silent) {
base::Platform::XDP::ParentWindowID(), base::Platform::XDP::ParentWindowID(),
options, options,
}), }),
base::Platform::XDP::kService); [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
if (signalId != 0) { try {
QWidget window; connection->call_finish(result);
window.setAttribute(Qt::WA_DontShowOnScreen);
window.setWindowModality(Qt::ApplicationModal);
window.show();
loop->run();
}
} catch (const std::exception &e) { } catch (const std::exception &e) {
if (!silent) { if (done) {
LOG(("Portal Autostart Error: %1").arg( LOG(("Portal Autostart Error: %1").arg(
QString::fromStdString(e.what()))); QString::fromStdString(e.what())));
} done(false);
error = true;
} }
return !error; if (*signalId) {
connection->signal_unsubscribe(*signalId);
}
}
});
},
base::Platform::XDP::kService);
} }
bool GenerateDesktopFile( bool GenerateDesktopFile(
@ -457,13 +481,12 @@ bool AutostartSupported() {
} }
void AutostartToggle(bool enabled, Fn<void(bool)> done) { void AutostartToggle(bool enabled, Fn<void(bool)> done) {
const auto success = [&] {
const auto silent = !done;
if (KSandbox::isFlatpak()) { if (KSandbox::isFlatpak()) {
return PortalAutostart(enabled, silent); PortalAutostart(enabled, done);
return;
} }
const auto success = [&] {
const auto autostart = QStandardPaths::writableLocation( const auto autostart = QStandardPaths::writableLocation(
QStandardPaths::GenericConfigLocation) QStandardPaths::GenericConfigLocation)
+ u"/autostart/"_q; + u"/autostart/"_q;
@ -479,7 +502,7 @@ void AutostartToggle(bool enabled, Fn<void(bool)> done) {
autostart, autostart,
{ u"-autostart"_q }, { u"-autostart"_q },
true, true,
silent); !done);
}(); }();
if (done) { if (done) {