From bf255c0e0043db6338b98d79c351a6e62d1aa18b Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Mon, 4 Sep 2023 00:30:48 +0400 Subject: [PATCH] Don't use crl::on_main unnecessarily in Linux native notifications code All those dbus methods call callbacks on the same thread --- .../linux/notifications_manager_linux.cpp | 255 +++++++++--------- 1 file changed, 127 insertions(+), 128 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 7448d690e..20c26143b 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -91,15 +91,12 @@ std::unique_ptr CreateServiceWatcher() { const Glib::ustring &service, const Glib::ustring &oldOwner, const Glib::ustring &newOwner) { - if (activatable && newOwner.empty()) { - crl::on_main([] { + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + if (activatable && newOwner.empty()) { Core::App().notifications().clearAll(); - }); - return; - } - - crl::on_main([] { - Core::App().notifications().createManager(); + } else { + Core::App().notifications().createManager(); + } }); }); } catch (...) { @@ -117,33 +114,35 @@ void StartServiceAsync(Fn callback) { connection, kService, [=](Fn result) { - Noexcept([&] { - try { - result(); // get the error if any - } catch (const Glib::Error &e) { - static const auto NotSupportedErrors = { - "org.freedesktop.DBus.Error.ServiceUnknown", - }; + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + Noexcept([&] { + try { + result(); // get the error if any + } catch (const Glib::Error &e) { + static const auto NotSupportedErrors = { + "org.freedesktop.DBus.Error.ServiceUnknown", + }; - const auto errorName = - Gio::DBus::ErrorUtils::get_remote_error(e).raw(); + const auto errorName = + Gio::DBus::ErrorUtils::get_remote_error(e).raw(); - if (!ranges::contains( - NotSupportedErrors, - errorName)) { - throw e; + if (!ranges::contains( + NotSupportedErrors, + errorName)) { + throw e; + } } - } - }); + }); - crl::on_main(callback); + callback(); + }); }); return; } catch (...) { } - crl::on_main(callback); + callback(); } bool GetServiceRegistered() { @@ -191,26 +190,26 @@ void GetServerInformation( "GetServerInformation", {}, [=](const Glib::RefPtr &result) { - Noexcept([&] { - const auto reply = connection->call_finish(result); + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + Noexcept([&] { + const auto reply = connection->call_finish(result); - const auto name = reply.get_child( - 0 - ).get_dynamic(); + const auto name = reply.get_child( + 0 + ).get_dynamic(); - const auto vendor = reply.get_child( - 1 - ).get_dynamic(); + const auto vendor = reply.get_child( + 1 + ).get_dynamic(); - const auto version = reply.get_child( - 2 - ).get_dynamic(); + const auto version = reply.get_child( + 2 + ).get_dynamic(); - const auto specVersion = reply.get_child( - 3 - ).get_dynamic(); + const auto specVersion = reply.get_child( + 3 + ).get_dynamic(); - crl::on_main([=] { callback(ServerInformation{ QString::fromStdString(name), QString::fromStdString(vendor), @@ -219,14 +218,14 @@ void GetServerInformation( QVersionNumber::fromString( QString::fromStdString(specVersion)), }); + }, [&] { + callback(std::nullopt); }); - }, [&] { - crl::on_main([=] { callback(std::nullopt); }); }); }, kService); }, [&] { - crl::on_main([=] { callback(std::nullopt); }); + callback(std::nullopt); }); } @@ -241,25 +240,27 @@ void GetCapabilities(Fn callback) { "GetCapabilities", {}, [=](const Glib::RefPtr &result) { - Noexcept([&] { - QStringList value; - ranges::transform( - connection->call_finish( - result - ).get_child( - 0 - ).get_dynamic>(), - ranges::back_inserter(value), - QString::fromStdString); + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + Noexcept([&] { + QStringList value; + ranges::transform( + connection->call_finish( + result + ).get_child( + 0 + ).get_dynamic>(), + ranges::back_inserter(value), + QString::fromStdString); - crl::on_main([=] { callback(value); }); - }, [&] { - crl::on_main([=] { callback({}); }); + callback(value); + }, [&] { + callback({}); + }); }); }, kService); }, [&] { - crl::on_main([=] { callback({}); }); + callback({}); }); } @@ -277,22 +278,24 @@ void GetInhibited(Fn callback) { Glib::ustring("Inhibited"), }), [=](const Glib::RefPtr &result) { - Noexcept([&] { - const auto value = connection->call_finish( - result - ).get_child( - 0 - ).get_dynamic>( - ).get(); + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + Noexcept([&] { + const auto value = connection->call_finish( + result + ).get_child( + 0 + ).get_dynamic>( + ).get(); - crl::on_main([=] { callback(value); }); - }, [&] { - crl::on_main([=] { callback(false); }); + callback(value); + }, [&] { + callback(false); + }); }); }, kService); }, [&] { - crl::on_main([=] { callback(false); }); + callback(false); }); } @@ -465,49 +468,51 @@ bool NotificationData::init( const auto weak = base::make_weak(this); const auto capabilities = CurrentCapabilities; - const auto signalEmitted = [=]( + const auto signalEmitted = crl::guard(weak, [=]( const Glib::RefPtr &connection, const Glib::ustring &sender_name, const Glib::ustring &object_path, const Glib::ustring &interface_name, const Glib::ustring &signal_name, const Glib::VariantContainerBase ¶meters) { - Noexcept([&] { - if (signal_name == "ActionInvoked") { - const auto id = parameters.get_child(0).get_dynamic(); + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + Noexcept([&] { + if (signal_name == "ActionInvoked") { + const auto id = parameters.get_child(0).get_dynamic(); - const auto actionName = parameters.get_child( - 1 - ).get_dynamic(); + const auto actionName = parameters.get_child( + 1 + ).get_dynamic(); - crl::on_main(weak, [=] { actionInvoked(id, actionName); }); - } else if (signal_name == "ActivationToken") { - const auto id = parameters.get_child(0).get_dynamic(); + actionInvoked(id, actionName); + } else if (signal_name == "ActivationToken") { + const auto id = parameters.get_child(0).get_dynamic(); - const auto token = parameters.get_child( - 1 - ).get_dynamic(); + const auto token = parameters.get_child( + 1 + ).get_dynamic(); - crl::on_main(weak, [=] { activationToken(id, token); }); - } else if (signal_name == "NotificationReplied") { - const auto id = parameters.get_child(0).get_dynamic(); + activationToken(id, token); + } else if (signal_name == "NotificationReplied") { + const auto id = parameters.get_child(0).get_dynamic(); - const auto text = parameters.get_child( - 1 - ).get_dynamic(); + const auto text = parameters.get_child( + 1 + ).get_dynamic(); - crl::on_main(weak, [=] { notificationReplied(id, text); }); - } else if (signal_name == "NotificationClosed") { - const auto id = parameters.get_child(0).get_dynamic(); + notificationReplied(id, text); + } else if (signal_name == "NotificationClosed") { + const auto id = parameters.get_child(0).get_dynamic(); - const auto reason = parameters.get_child( - 1 - ).get_dynamic(); + const auto reason = parameters.get_child( + 1 + ).get_dynamic(); - crl::on_main(weak, [=] { notificationClosed(id, reason); }); - } + notificationClosed(id, reason); + } + }); }); - }; + }); _imageKey = GetImageKey(CurrentServerInformationValue().specVersion); @@ -652,23 +657,19 @@ void NotificationData::show() { _hints, -1, }), - [=](const Glib::RefPtr &result) { - Noexcept([&] { - const auto notificationId = connection->call_finish( - result - ).get_child( - 0 - ).get_dynamic(); - - crl::on_main(weak, [=] { - _notificationId = notificationId; - }); - }, [&] { - crl::on_main(weak, [=] { + crl::guard(weak, [=](const Glib::RefPtr &result) { + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + Noexcept([&] { + _notificationId = connection->call_finish( + result + ).get_child( + 0 + ).get_dynamic(); + }, [&] { _manager->clearNotification(_id); }); }); - }, + }), kService); })); } @@ -966,34 +967,32 @@ Manager::Private::Private(not_null manager) })); _inhibitedSignalId = _dbusConnection->signal_subscribe( - [=]( + crl::guard(weak, [=]( const Glib::RefPtr &connection, const Glib::ustring &sender_name, const Glib::ustring &object_path, const Glib::ustring &interface_name, const Glib::ustring &signal_name, const Glib::VariantContainerBase ¶meters) { - Noexcept([&] { - const auto interface = parameters.get_child( - 0 - ).get_dynamic(); + Core::Sandbox::Instance().customEnterFromEventLoop([&] { + Noexcept([&] { + const auto interface = parameters.get_child( + 0 + ).get_dynamic(); - if (interface != kInterface) { - return; - } + if (interface != kInterface) { + return; + } - const auto inhibited = parameters.get_child( - 1 - ).get_dynamic>( - ).at( - "Inhibited" - ).get_dynamic(); - - crl::on_main(weak, [=] { - _inhibited = inhibited; + _inhibited = parameters.get_child( + 1 + ).get_dynamic>( + ).at( + "Inhibited" + ).get_dynamic(); }); }); - }, + }), kService, kPropertiesInterface, "PropertiesChanged",