Don't use crl::on_main unnecessarily in Linux native notifications code

All those dbus methods call callbacks on the same thread
This commit is contained in:
Ilya Fedin 2023-09-04 00:30:48 +04:00 committed by John Preston
parent 55fb3405e5
commit bf255c0e00

View File

@ -91,15 +91,12 @@ std::unique_ptr<base::Platform::DBus::ServiceWatcher> CreateServiceWatcher() {
const Glib::ustring &service, const Glib::ustring &service,
const Glib::ustring &oldOwner, const Glib::ustring &oldOwner,
const Glib::ustring &newOwner) { const Glib::ustring &newOwner) {
if (activatable && newOwner.empty()) { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
crl::on_main([] { if (activatable && newOwner.empty()) {
Core::App().notifications().clearAll(); Core::App().notifications().clearAll();
}); } else {
return; Core::App().notifications().createManager();
} }
crl::on_main([] {
Core::App().notifications().createManager();
}); });
}); });
} catch (...) { } catch (...) {
@ -117,33 +114,35 @@ void StartServiceAsync(Fn<void()> callback) {
connection, connection,
kService, kService,
[=](Fn<base::Platform::DBus::StartReply()> result) { [=](Fn<base::Platform::DBus::StartReply()> result) {
Noexcept([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
try { Noexcept([&] {
result(); // get the error if any try {
} catch (const Glib::Error &e) { result(); // get the error if any
static const auto NotSupportedErrors = { } catch (const Glib::Error &e) {
"org.freedesktop.DBus.Error.ServiceUnknown", static const auto NotSupportedErrors = {
}; "org.freedesktop.DBus.Error.ServiceUnknown",
};
const auto errorName = const auto errorName =
Gio::DBus::ErrorUtils::get_remote_error(e).raw(); Gio::DBus::ErrorUtils::get_remote_error(e).raw();
if (!ranges::contains( if (!ranges::contains(
NotSupportedErrors, NotSupportedErrors,
errorName)) { errorName)) {
throw e; throw e;
}
} }
} });
});
crl::on_main(callback); callback();
});
}); });
return; return;
} catch (...) { } catch (...) {
} }
crl::on_main(callback); callback();
} }
bool GetServiceRegistered() { bool GetServiceRegistered() {
@ -191,26 +190,26 @@ void GetServerInformation(
"GetServerInformation", "GetServerInformation",
{}, {},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Noexcept([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
const auto reply = connection->call_finish(result); Noexcept([&] {
const auto reply = connection->call_finish(result);
const auto name = reply.get_child( const auto name = reply.get_child(
0 0
).get_dynamic<Glib::ustring>(); ).get_dynamic<Glib::ustring>();
const auto vendor = reply.get_child( const auto vendor = reply.get_child(
1 1
).get_dynamic<Glib::ustring>(); ).get_dynamic<Glib::ustring>();
const auto version = reply.get_child( const auto version = reply.get_child(
2 2
).get_dynamic<Glib::ustring>(); ).get_dynamic<Glib::ustring>();
const auto specVersion = reply.get_child( const auto specVersion = reply.get_child(
3 3
).get_dynamic<Glib::ustring>(); ).get_dynamic<Glib::ustring>();
crl::on_main([=] {
callback(ServerInformation{ callback(ServerInformation{
QString::fromStdString(name), QString::fromStdString(name),
QString::fromStdString(vendor), QString::fromStdString(vendor),
@ -219,14 +218,14 @@ void GetServerInformation(
QVersionNumber::fromString( QVersionNumber::fromString(
QString::fromStdString(specVersion)), QString::fromStdString(specVersion)),
}); });
}, [&] {
callback(std::nullopt);
}); });
}, [&] {
crl::on_main([=] { callback(std::nullopt); });
}); });
}, },
kService); kService);
}, [&] { }, [&] {
crl::on_main([=] { callback(std::nullopt); }); callback(std::nullopt);
}); });
} }
@ -241,25 +240,27 @@ void GetCapabilities(Fn<void(const QStringList &)> callback) {
"GetCapabilities", "GetCapabilities",
{}, {},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Noexcept([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
QStringList value; Noexcept([&] {
ranges::transform( QStringList value;
connection->call_finish( ranges::transform(
result connection->call_finish(
).get_child( result
0 ).get_child(
).get_dynamic<std::vector<Glib::ustring>>(), 0
ranges::back_inserter(value), ).get_dynamic<std::vector<Glib::ustring>>(),
QString::fromStdString); ranges::back_inserter(value),
QString::fromStdString);
crl::on_main([=] { callback(value); }); callback(value);
}, [&] { }, [&] {
crl::on_main([=] { callback({}); }); callback({});
});
}); });
}, },
kService); kService);
}, [&] { }, [&] {
crl::on_main([=] { callback({}); }); callback({});
}); });
} }
@ -277,22 +278,24 @@ void GetInhibited(Fn<void(bool)> callback) {
Glib::ustring("Inhibited"), Glib::ustring("Inhibited"),
}), }),
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Noexcept([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
const auto value = connection->call_finish( Noexcept([&] {
result const auto value = connection->call_finish(
).get_child( result
0 ).get_child(
).get_dynamic<Glib::Variant<bool>>( 0
).get(); ).get_dynamic<Glib::Variant<bool>>(
).get();
crl::on_main([=] { callback(value); }); callback(value);
}, [&] { }, [&] {
crl::on_main([=] { callback(false); }); callback(false);
});
}); });
}, },
kService); kService);
}, [&] { }, [&] {
crl::on_main([=] { callback(false); }); callback(false);
}); });
} }
@ -465,49 +468,51 @@ bool NotificationData::init(
const auto weak = base::make_weak(this); const auto weak = base::make_weak(this);
const auto capabilities = CurrentCapabilities; const auto capabilities = CurrentCapabilities;
const auto signalEmitted = [=]( const auto signalEmitted = crl::guard(weak, [=](
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) {
Noexcept([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
if (signal_name == "ActionInvoked") { Noexcept([&] {
const auto id = parameters.get_child(0).get_dynamic<uint>(); if (signal_name == "ActionInvoked") {
const auto id = parameters.get_child(0).get_dynamic<uint>();
const auto actionName = parameters.get_child( const auto actionName = parameters.get_child(
1 1
).get_dynamic<Glib::ustring>(); ).get_dynamic<Glib::ustring>();
crl::on_main(weak, [=] { actionInvoked(id, actionName); }); actionInvoked(id, actionName);
} else if (signal_name == "ActivationToken") { } else if (signal_name == "ActivationToken") {
const auto id = parameters.get_child(0).get_dynamic<uint>(); const auto id = parameters.get_child(0).get_dynamic<uint>();
const auto token = parameters.get_child( const auto token = parameters.get_child(
1 1
).get_dynamic<Glib::ustring>(); ).get_dynamic<Glib::ustring>();
crl::on_main(weak, [=] { activationToken(id, token); }); activationToken(id, token);
} else if (signal_name == "NotificationReplied") { } else if (signal_name == "NotificationReplied") {
const auto id = parameters.get_child(0).get_dynamic<uint>(); const auto id = parameters.get_child(0).get_dynamic<uint>();
const auto text = parameters.get_child( const auto text = parameters.get_child(
1 1
).get_dynamic<Glib::ustring>(); ).get_dynamic<Glib::ustring>();
crl::on_main(weak, [=] { notificationReplied(id, text); }); notificationReplied(id, text);
} else if (signal_name == "NotificationClosed") { } else if (signal_name == "NotificationClosed") {
const auto id = parameters.get_child(0).get_dynamic<uint>(); const auto id = parameters.get_child(0).get_dynamic<uint>();
const auto reason = parameters.get_child( const auto reason = parameters.get_child(
1 1
).get_dynamic<uint>(); ).get_dynamic<uint>();
crl::on_main(weak, [=] { notificationClosed(id, reason); }); notificationClosed(id, reason);
} }
});
}); });
}; });
_imageKey = GetImageKey(CurrentServerInformationValue().specVersion); _imageKey = GetImageKey(CurrentServerInformationValue().specVersion);
@ -652,23 +657,19 @@ void NotificationData::show() {
_hints, _hints,
-1, -1,
}), }),
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { crl::guard(weak, [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Noexcept([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
const auto notificationId = connection->call_finish( Noexcept([&] {
result _notificationId = connection->call_finish(
).get_child( result
0 ).get_child(
).get_dynamic<uint>(); 0
).get_dynamic<uint>();
crl::on_main(weak, [=] { }, [&] {
_notificationId = notificationId;
});
}, [&] {
crl::on_main(weak, [=] {
_manager->clearNotification(_id); _manager->clearNotification(_id);
}); });
}); });
}, }),
kService); kService);
})); }));
} }
@ -966,34 +967,32 @@ Manager::Private::Private(not_null<Manager*> manager)
})); }));
_inhibitedSignalId = _dbusConnection->signal_subscribe( _inhibitedSignalId = _dbusConnection->signal_subscribe(
[=]( crl::guard(weak, [=](
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) {
Noexcept([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
const auto interface = parameters.get_child( Noexcept([&] {
0 const auto interface = parameters.get_child(
).get_dynamic<Glib::ustring>(); 0
).get_dynamic<Glib::ustring>();
if (interface != kInterface) { if (interface != kInterface) {
return; return;
} }
const auto inhibited = parameters.get_child( _inhibited = parameters.get_child(
1 1
).get_dynamic<std::map<Glib::ustring, Glib::VariantBase>>( ).get_dynamic<std::map<Glib::ustring, Glib::VariantBase>>(
).at( ).at(
"Inhibited" "Inhibited"
).get_dynamic<bool>(); ).get_dynamic<bool>();
crl::on_main(weak, [=] {
_inhibited = inhibited;
}); });
}); });
}, }),
kService, kService,
kPropertiesInterface, kPropertiesInterface,
"PropertiesChanged", "PropertiesChanged",