service/NetworkLifecycleCoordinator.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QObject> | ||
| 4 | #include <QSet> | ||
| 5 | #include <QString> | ||
| 6 | #include <QTimer> | ||
| 7 | #include <QHash> | ||
| 8 | #include <QDBusConnection> | ||
| 9 | #include <QVariantMap> | ||
| 10 | #include <functional> | ||
| 11 | |||
| 12 | struct NetworkIdentity { | ||
| 13 | QString backend; | ||
| 14 | QString interfaceName; | ||
| 15 | QByteArray activeAccessPointHash; | ||
| 16 | QSet<QString> sourceAddresses; | ||
| 17 | QSet<QString> endpointAddresses; | ||
| 18 | |||
| 19 | 71 | [[nodiscard]] bool isValid() const { return !interfaceName.isEmpty(); } | |
| 20 | friend bool operator==(const NetworkIdentity &, | ||
| 21 | const NetworkIdentity &) = default; | ||
| 22 | }; | ||
| 23 | |||
| 24 | class NetworkLifecycleCoordinator : public QObject { | ||
| 25 | Q_OBJECT | ||
| 26 | |||
| 27 | public: | ||
| 28 | enum class Trigger { | ||
| 29 | NativeResume, | ||
| 30 | QtResume, | ||
| 31 | TimerSkew, | ||
| 32 | NetworkIdentity, | ||
| 33 | Reachability, | ||
| 34 | SocketFailure, | ||
| 35 | }; | ||
| 36 | 1 | Q_ENUM(Trigger) | |
| 37 | |||
| 38 | explicit NetworkLifecycleCoordinator(QObject *parent = nullptr, | ||
| 39 | bool installNativeHooks = true); | ||
| 40 | static NetworkLifecycleCoordinator *instance(); | ||
| 41 | |||
| 42 | 29 | [[nodiscard]] quint64 generation() const { return m_generation; } | |
| 43 | 3315 | [[nodiscard]] bool isSuspending() const { return m_suspending; } | |
| 44 | |||
| 45 | // The coordinator is process-wide, but the connections it serves may live | ||
| 46 | // in other threads (the settings-sync worker has its own IMAP connection | ||
| 47 | // and health monitor). Calls from a foreign thread are queued into this | ||
| 48 | // object's thread — its state and timers are only touched there (F93-17: | ||
| 49 | // "Timers cannot be started from another thread"). requestRecovery() | ||
| 50 | // then returns 0 (the generation is assigned when the call runs). | ||
| 51 | quint64 requestRecovery(Trigger trigger); | ||
| 52 | void markStable(quint64 generation); | ||
| 53 | bool updateNetworkIdentity(const NetworkIdentity &identity); | ||
| 54 | void observeEndpoint(const QString &host); | ||
| 55 | void refreshNetworkIdentity(); | ||
| 56 | |||
| 57 | // Test/diagnostic state contract (Sprint 92 S92.0.2 replaced the former | ||
| 58 | // test friend declarations, Sprint 91 rule 8). No product caller uses it; | ||
| 59 | // tests inject platform identities, endpoint resolutions and the D-Bus | ||
| 60 | // connection without a live NetworkManager. | ||
| 61 | 5 | quint64 &stableGeneration() { return m_stableGeneration; } | |
| 62 | 63 | bool &coalescing() { return m_coalescing; } | |
| 63 | 13 | QTimer &coalesceTimer() { return m_coalesceTimer; } | |
| 64 | 58 | bool &identityReading() { return m_identityReading; } | |
| 65 | 11 | QDBusConnection &platformBus() { return m_platformBus; } | |
| 66 | 21 | NetworkIdentity &networkIdentity() { return m_networkIdentity; } | |
| 67 | 2 | QSet<QString> &endpointHosts() { return m_endpointHosts; } | |
| 68 | 2 | QSet<QString> &endpointLookups() { return m_endpointLookups; } | |
| 69 | 3 | QHash<QString, QSet<QString>> &resolvedEndpoints() { return m_resolvedEndpoints; } | |
| 70 | void refreshEndpointAddresses(); | ||
| 71 | void applyEndpointResolution(const QString &host, | ||
| 72 | const QSet<QString> &addresses); | ||
| 73 | |||
| 74 | public slots: | ||
| 75 | void prepareForSleep(bool sleeping); | ||
| 76 | |||
| 77 | signals: | ||
| 78 | void suspendingChanged(bool suspending); | ||
| 79 | void recoveryRequested(quint64 generation, | ||
| 80 | NetworkLifecycleCoordinator::Trigger trigger); | ||
| 81 | |||
| 82 | private: | ||
| 83 | bool forwardToOwnThread(std::function<void()> call); | ||
| 84 | void installNativeSleepHook(); | ||
| 85 | void readPlatformIdentity(); | ||
| 86 | void readNetworkManagerProperties(const QString &path, const QString &interface, | ||
| 87 | std::function<void(QVariantMap)> callback); | ||
| 88 | NetworkIdentity completePlatformIdentity(NetworkIdentity identity) const; | ||
| 89 | void publishPlatformIdentity(NetworkIdentity identity); | ||
| 90 | |||
| 91 | quint64 m_generation = 0; | ||
| 92 | quint64 m_stableGeneration = 0; | ||
| 93 | bool m_suspending = false; | ||
| 94 | bool m_coalescing = false; | ||
| 95 | QTimer m_coalesceTimer; | ||
| 96 | QTimer m_identityTimer; | ||
| 97 | bool m_identityReading = false; | ||
| 98 | bool m_identityRefreshPending = false; | ||
| 99 | QDBusConnection m_platformBus{QString()}; | ||
| 100 | NetworkIdentity m_networkIdentity; | ||
| 101 | QSet<QString> m_endpointHosts; | ||
| 102 | QSet<QString> m_endpointLookups; | ||
| 103 | QHash<QString, QSet<QString>> m_resolvedEndpoints; | ||
| 104 | }; | ||
| 105 |