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 &oldOwner,
const Glib::ustring &newOwner) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
if (activatable && newOwner.empty()) {
crl::on_main([] {
Core::App().notifications().clearAll();
});
return;
}
crl::on_main([] {
} else {
Core::App().notifications().createManager();
}
});
});
} catch (...) {
@ -117,6 +114,7 @@ void StartServiceAsync(Fn<void()> callback) {
connection,
kService,
[=](Fn<base::Platform::DBus::StartReply()> result) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Noexcept([&] {
try {
result(); // get the error if any
@ -136,14 +134,15 @@ void StartServiceAsync(Fn<void()> callback) {
}
});
crl::on_main(callback);
callback();
});
});
return;
} catch (...) {
}
crl::on_main(callback);
callback();
}
bool GetServiceRegistered() {
@ -191,6 +190,7 @@ void GetServerInformation(
"GetServerInformation",
{},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Noexcept([&] {
const auto reply = connection->call_finish(result);
@ -210,7 +210,6 @@ void GetServerInformation(
3
).get_dynamic<Glib::ustring>();
crl::on_main([=] {
callback(ServerInformation{
QString::fromStdString(name),
QString::fromStdString(vendor),
@ -219,14 +218,14 @@ void GetServerInformation(
QVersionNumber::fromString(
QString::fromStdString(specVersion)),
});
});
}, [&] {
crl::on_main([=] { callback(std::nullopt); });
callback(std::nullopt);
});
});
},
kService);
}, [&] {
crl::on_main([=] { callback(std::nullopt); });
callback(std::nullopt);
});
}
@ -241,6 +240,7 @@ void GetCapabilities(Fn<void(const QStringList &)> callback) {
"GetCapabilities",
{},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Noexcept([&] {
QStringList value;
ranges::transform(
@ -252,14 +252,15 @@ void GetCapabilities(Fn<void(const QStringList &)> callback) {
ranges::back_inserter(value),
QString::fromStdString);
crl::on_main([=] { callback(value); });
callback(value);
}, [&] {
crl::on_main([=] { callback({}); });
callback({});
});
});
},
kService);
}, [&] {
crl::on_main([=] { callback({}); });
callback({});
});
}
@ -277,6 +278,7 @@ void GetInhibited(Fn<void(bool)> callback) {
Glib::ustring("Inhibited"),
}),
[=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Noexcept([&] {
const auto value = connection->call_finish(
result
@ -285,14 +287,15 @@ void GetInhibited(Fn<void(bool)> callback) {
).get_dynamic<Glib::Variant<bool>>(
).get();
crl::on_main([=] { callback(value); });
callback(value);
}, [&] {
crl::on_main([=] { callback(false); });
callback(false);
});
});
},
kService);
}, [&] {
crl::on_main([=] { callback(false); });
callback(false);
});
}
@ -465,13 +468,14 @@ 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<Gio::DBus::Connection> &connection,
const Glib::ustring &sender_name,
const Glib::ustring &object_path,
const Glib::ustring &interface_name,
const Glib::ustring &signal_name,
const Glib::VariantContainerBase &parameters) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Noexcept([&] {
if (signal_name == "ActionInvoked") {
const auto id = parameters.get_child(0).get_dynamic<uint>();
@ -480,7 +484,7 @@ bool NotificationData::init(
1
).get_dynamic<Glib::ustring>();
crl::on_main(weak, [=] { actionInvoked(id, actionName); });
actionInvoked(id, actionName);
} else if (signal_name == "ActivationToken") {
const auto id = parameters.get_child(0).get_dynamic<uint>();
@ -488,7 +492,7 @@ bool NotificationData::init(
1
).get_dynamic<Glib::ustring>();
crl::on_main(weak, [=] { activationToken(id, token); });
activationToken(id, token);
} else if (signal_name == "NotificationReplied") {
const auto id = parameters.get_child(0).get_dynamic<uint>();
@ -496,7 +500,7 @@ bool NotificationData::init(
1
).get_dynamic<Glib::ustring>();
crl::on_main(weak, [=] { notificationReplied(id, text); });
notificationReplied(id, text);
} else if (signal_name == "NotificationClosed") {
const auto id = parameters.get_child(0).get_dynamic<uint>();
@ -504,10 +508,11 @@ bool NotificationData::init(
1
).get_dynamic<uint>();
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<Gio::AsyncResult> &result) {
crl::guard(weak, [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Noexcept([&] {
const auto notificationId = connection->call_finish(
_notificationId = connection->call_finish(
result
).get_child(
0
).get_dynamic<uint>();
crl::on_main(weak, [=] {
_notificationId = notificationId;
});
}, [&] {
crl::on_main(weak, [=] {
_manager->clearNotification(_id);
});
});
},
}),
kService);
}));
}
@ -966,13 +967,14 @@ Manager::Private::Private(not_null<Manager*> manager)
}));
_inhibitedSignalId = _dbusConnection->signal_subscribe(
[=](
crl::guard(weak, [=](
const Glib::RefPtr<Gio::DBus::Connection> &connection,
const Glib::ustring &sender_name,
const Glib::ustring &object_path,
const Glib::ustring &interface_name,
const Glib::ustring &signal_name,
const Glib::VariantContainerBase &parameters) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Noexcept([&] {
const auto interface = parameters.get_child(
0
@ -982,18 +984,15 @@ Manager::Private::Private(not_null<Manager*> manager)
return;
}
const auto inhibited = parameters.get_child(
_inhibited = parameters.get_child(
1
).get_dynamic<std::map<Glib::ustring, Glib::VariantBase>>(
).at(
"Inhibited"
).get_dynamic<bool>();
crl::on_main(weak, [=] {
_inhibited = inhibited;
});
});
},
}),
kService,
kPropertiesInterface,
"PropertiesChanged",