game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
notification_service.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// notification_service.cpp
4// ==============================================================================
5//
6// Thread-safety contract:
7//
8// `instance_` is a `std::shared_ptr` accessed concurrently:
9// - main.cpp `setInstance()` at startup, and again at shutdown via the
10// QApplication destructor releasing its capturing closures
11// - background threads (SteamUpdateMonitor 300s polling, GameProcessManager
12// detection thread, library scanner workers, download workers) calling
13// info/warn/error
14//
15// Per [shared_ptr] ยง20.10.2.6, `shared_ptr` is NOT thread-safe for
16// concurrent read+write of the same instance without external
17// synchronisation; `atomic_load`/`atomic_store` were the C++17 path
18// (deprecated in C++20 in favour of `std::atomic<std::shared_ptr<T>>`).
19// This codebase is C++17 with libstdc++ so a `std::mutex` is the
20// simplest portable fix and matches the pattern already used in
21// settings_controller / store_integration_controller.
22
24
25#include <mutex>
26
27namespace core
28{
29
30namespace
31{
33}
34
35// Static instance โ€” only mutated under `g_notificationInstanceMutex`.
36std::shared_ptr<INotificationService> NotificationService::instance_ = nullptr;
37
38// Snapshot the shared_ptr under the mutex so the caller's copy keeps the
39// underlying object alive for the duration of the call โ€” even if another
40// thread races to `setInstance(nullptr)` between our null-check and the
41// virtual call. Defined as a static class member so it can read the
42// private `instance_` without resorting to friend declarations.
43std::shared_ptr<INotificationService> NotificationService::snapshotInstance()
44{
45 std::lock_guard<std::mutex> lock(g_notificationInstanceMutex);
46 return instance_;
47}
48
49std::shared_ptr<INotificationService> NotificationService::instance()
50{
51 // Return a shared_ptr (not a raw pointer) so the caller's copy
52 // keeps the underlying service alive for the duration of their
53 // call โ€” even if another thread races setInstance(nullptr) at
54 // QApplication shutdown. Returning a raw pointer would have a UAF
55 // window between `instance_.get()` and the caller's deref.
56 std::lock_guard<std::mutex> lock(g_notificationInstanceMutex);
57 return instance_;
58}
59
60void NotificationService::setInstance(std::shared_ptr<INotificationService> service)
61{
62 std::lock_guard<std::mutex> lock(g_notificationInstanceMutex);
63 instance_ = std::move(service);
64}
65
66void NotificationService::info(const std::string& message, float duration)
67{
68 if (auto inst = snapshotInstance())
69 {
70 inst->info(message, duration);
71 }
72}
73
74void NotificationService::success(const std::string& message, float duration)
75{
76 if (auto inst = snapshotInstance())
77 {
78 inst->success(message, duration);
79 }
80}
81
82void NotificationService::warning(const std::string& message, float duration)
83{
84 if (auto inst = snapshotInstance())
85 {
86 inst->warning(message, duration);
87 }
88}
89
90void NotificationService::error(const std::string& message, float duration)
91{
92 if (auto inst = snapshotInstance())
93 {
94 inst->error(message, duration);
95 }
96}
97
98} // namespace core
static void setInstance(std::shared_ptr< INotificationService > service)
Set the notification service implementation UI layer calls this at startup with its implementation.
static void success(const std::string &message, float duration=3.0f)
static void error(const std::string &message, float duration=5.0f)
static std::shared_ptr< INotificationService > instance()
Get the current notification service instance.
static void warning(const std::string &message, float duration=5.0f)
static void info(const std::string &message, float duration=3.0f)
Convenience methods that safely handle null instance.