From 9b8dcec26e792cd19a0825d2edd01aa152c31857 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Tue, 26 Jul 2022 06:54:18 +0400 Subject: [PATCH] Ensure notification manager is not null before first call to setManager in cross-platform code --- .../linux/notifications_manager_linux.cpp | 24 +++++-------------- .../window/notifications_manager.cpp | 11 ++++----- .../window/notifications_manager.h | 2 +- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 7228a00dd..97b695c26 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -745,18 +745,15 @@ void Create(Window::Notifications::System *system) { using ManagerType = Window::Notifications::ManagerType; if ((Core::App().settings().nativeNotifications() || Enforced()) && Supported()) { - if (!system->managerType().has_value() - || *system->managerType() != ManagerType::Native) { + if (system->managerType() != ManagerType::Native) { system->setManager(std::make_unique(system)); } } else if (Enforced()) { - if (!system->managerType().has_value() - || *system->managerType() != ManagerType::Dummy) { + if (system->managerType() != ManagerType::Dummy) { using DummyManager = Window::Notifications::DummyManager; system->setManager(std::make_unique(system)); } - } else if (!system->managerType().has_value() - || *system->managerType() != ManagerType::Default) { + } else if (system->managerType() != ManagerType::Default) { 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(); if (!ServiceRegistered) { @@ -787,17 +785,7 @@ void Create(Window::Notifications::System *system) { CurrentCapabilities = result; 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(system)); - } - - // snap doesn't allow access when the daemon is not running :( - StartServiceAsync(serviceActivated); + }); } class Manager::Private { diff --git a/Telegram/SourceFiles/window/notifications_manager.cpp b/Telegram/SourceFiles/window/notifications_manager.cpp index b83fc2b28..0b253f868 100644 --- a/Telegram/SourceFiles/window/notifications_manager.cpp +++ b/Telegram/SourceFiles/window/notifications_manager.cpp @@ -91,7 +91,8 @@ System::NotificationInHistoryKey::NotificationInHistoryKey( System::System() : _waitTimer([=] { showNext(); }) -, _waitForAllGroupedTimer([=] { showGrouped(); }) { +, _waitForAllGroupedTimer([=] { showGrouped(); }) +, _manager(std::make_unique(this)) { settingsChanged( ) | rpl::start_with_next([=](ChangeType type) { if (type == ChangeType::DesktopEnabled) { @@ -116,11 +117,9 @@ void System::setManager(std::unique_ptr manager) { } } -std::optional System::managerType() const { - if (_manager) { - return _manager->type(); - } - return std::nullopt; +ManagerType System::managerType() const { + Expects(_manager != nullptr); + return _manager->type(); } Main::Session *System::findSession(uint64 sessionId) const { diff --git a/Telegram/SourceFiles/window/notifications_manager.h b/Telegram/SourceFiles/window/notifications_manager.h index 09f0ae419..6e9483626 100644 --- a/Telegram/SourceFiles/window/notifications_manager.h +++ b/Telegram/SourceFiles/window/notifications_manager.h @@ -85,7 +85,7 @@ public: void createManager(); void setManager(std::unique_ptr manager); - [[nodiscard]] std::optional managerType() const; + [[nodiscard]] ManagerType managerType() const; void checkDelayed(); void schedule(ItemNotification notification);