Ensure notification manager is not null before first call to setManager in cross-platform code

This commit is contained in:
Ilya Fedin 2022-07-26 06:54:18 +04:00 committed by John Preston
parent 6718d238af
commit 9b8dcec26e
3 changed files with 12 additions and 25 deletions

View file

@ -745,18 +745,15 @@ void Create(Window::Notifications::System *system) {
using ManagerType = Window::Notifications::ManagerType; using ManagerType = Window::Notifications::ManagerType;
if ((Core::App().settings().nativeNotifications() || Enforced()) if ((Core::App().settings().nativeNotifications() || Enforced())
&& Supported()) { && Supported()) {
if (!system->managerType().has_value() if (system->managerType() != ManagerType::Native) {
|| *system->managerType() != ManagerType::Native) {
system->setManager(std::make_unique<Manager>(system)); system->setManager(std::make_unique<Manager>(system));
} }
} else if (Enforced()) { } else if (Enforced()) {
if (!system->managerType().has_value() if (system->managerType() != ManagerType::Dummy) {
|| *system->managerType() != ManagerType::Dummy) {
using DummyManager = Window::Notifications::DummyManager; using DummyManager = Window::Notifications::DummyManager;
system->setManager(std::make_unique<DummyManager>(system)); system->setManager(std::make_unique<DummyManager>(system));
} }
} else if (!system->managerType().has_value() } else if (system->managerType() != ManagerType::Default) {
|| *system->managerType() != ManagerType::Default) {
system->setManager(nullptr); system->setManager(nullptr);
} }
}; };
@ -768,7 +765,8 @@ void Create(Window::Notifications::System *system) {
} }
}; };
const auto serviceActivated = [=] { // snap doesn't allow access when the daemon is not running :(
StartServiceAsync([=] {
ServiceRegistered = GetServiceRegistered(); ServiceRegistered = GetServiceRegistered();
if (!ServiceRegistered) { if (!ServiceRegistered) {
@ -787,17 +785,7 @@ void Create(Window::Notifications::System *system) {
CurrentCapabilities = result; CurrentCapabilities = result;
oneReady(); oneReady();
}); });
}; });
// There are some asserts that manager is not nullptr,
// avoid crashes until some real manager is created
if (!system->managerType().has_value()) {
using DummyManager = Window::Notifications::DummyManager;
system->setManager(std::make_unique<DummyManager>(system));
}
// snap doesn't allow access when the daemon is not running :(
StartServiceAsync(serviceActivated);
} }
class Manager::Private { class Manager::Private {

View file

@ -91,7 +91,8 @@ System::NotificationInHistoryKey::NotificationInHistoryKey(
System::System() System::System()
: _waitTimer([=] { showNext(); }) : _waitTimer([=] { showNext(); })
, _waitForAllGroupedTimer([=] { showGrouped(); }) { , _waitForAllGroupedTimer([=] { showGrouped(); })
, _manager(std::make_unique<DummyManager>(this)) {
settingsChanged( settingsChanged(
) | rpl::start_with_next([=](ChangeType type) { ) | rpl::start_with_next([=](ChangeType type) {
if (type == ChangeType::DesktopEnabled) { if (type == ChangeType::DesktopEnabled) {
@ -116,11 +117,9 @@ void System::setManager(std::unique_ptr<Manager> manager) {
} }
} }
std::optional<ManagerType> System::managerType() const { ManagerType System::managerType() const {
if (_manager) { Expects(_manager != nullptr);
return _manager->type(); return _manager->type();
}
return std::nullopt;
} }
Main::Session *System::findSession(uint64 sessionId) const { Main::Session *System::findSession(uint64 sessionId) const {

View file

@ -85,7 +85,7 @@ public:
void createManager(); void createManager();
void setManager(std::unique_ptr<Manager> manager); void setManager(std::unique_ptr<Manager> manager);
[[nodiscard]] std::optional<ManagerType> managerType() const; [[nodiscard]] ManagerType managerType() const;
void checkDelayed(); void checkDelayed();
void schedule(ItemNotification notification); void schedule(ItemNotification notification);