| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "processregistry.h" |
| 5 | |
| 6 | #include "iappregistry.h" |
| 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
| 10 | class ProcessRegistry::AppRegistryObserver : public IAppRegistry::Observer |
| 11 | { |
| 12 | public: |
| 13 | AppRegistryObserver(ProcessRegistry &outer) noexcept; |
| 14 | void registeredApp(std::string const &app) override; |
| 15 | void unregisteredApp(std::string const &app) override; |
| 16 | |
| 17 | private: |
| 18 | ProcessRegistry &outer_; |
| 19 | }; |
| 20 | |
| 21 | ProcessRegistry::AppRegistryObserver::AppRegistryObserver( |
| 22 | ProcessRegistry &outer) noexcept |
| 23 | : outer_(outer) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | void ProcessRegistry::AppRegistryObserver::registeredApp(std::string const &app) |
| 28 | { |
| 29 | outer_.registeredApp(app); |
| 30 | } |
| 31 | |
| 32 | void ProcessRegistry::AppRegistryObserver::unregisteredApp(std::string const &app) |
| 33 | { |
| 34 | outer_.unregisteredApp(app); |
| 35 | } |
| 36 | |
| 37 | ProcessRegistry::ProcessRegistry(std::shared_ptr<IAppRegistry> appRegistry) noexcept |
| 38 | : appRegObserver_(std::make_shared<ProcessRegistry::AppRegistryObserver>(*this)) |
| 39 | { |
| 40 | appRegistry->addObserver(appRegObserver_); |
| 41 | } |
| 42 | |
| 43 | void ProcessRegistry::add(int PID, std::string const &app) |
| 44 | { |
| 45 | std::lock_guard<std::mutex> lock(mutex_); |
| 46 | |
| 47 | auto const pidIt = pidReg_.find(PID); |
| 48 | if (pidIt == pidReg_.cend()) { |
| 49 | pidReg_.insert({PID, app}); |
| 50 | |
| 51 | // increment counter |
| 52 | auto const appIt = appCounterReg_.find(app); |
| 53 | if (appIt != appCounterReg_.cend()) |
| 54 | ++appIt->second; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void ProcessRegistry::remove(int PID) |
| 59 | { |
| 60 | |
| 61 | std::lock_guard<std::mutex> lock(mutex_); |
| 62 | |
| 63 | auto const pidIt = pidReg_.find(PID); |
| 64 | if (pidIt != pidReg_.cend()) { |
| 65 | auto const app = std::move(pidIt->second); |
| 66 | pidReg_.erase(pidIt); |
| 67 | |
| 68 | // decrement counter |
| 69 | auto appIt = appCounterReg_.find(app); |
| 70 | if (appIt != appCounterReg_.end()) |
| 71 | --appIt->second; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool ProcessRegistry::running(std::string const &app) |
| 76 | { |
| 77 | std::lock_guard<std::mutex> lock(mutex_); |
| 78 | |
| 79 | auto const it = appCounterReg_.find(app); |
| 80 | if (it != appCounterReg_.cend()) |
| 81 | return it->second > 0; |
| 82 | |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | std::optional<std::string> ProcessRegistry::findApp(int PID) |
| 87 | { |
| 88 | std::lock_guard<std::mutex> lock(mutex_); |
| 89 | |
| 90 | auto const it = pidReg_.find(PID); |
| 91 | if (it != pidReg_.end()) |
| 92 | return it->second; |
| 93 | |
| 94 | return {}; |
| 95 | } |
| 96 | |
| 97 | void ProcessRegistry::registeredApp(std::string const &app) |
| 98 | { |
| 99 | std::lock_guard<std::mutex> lock(mutex_); |
| 100 | appCounterReg_.insert({app, 0}); |
| 101 | } |
| 102 | |
| 103 | void ProcessRegistry::unregisteredApp(std::string const &app) |
| 104 | { |
| 105 | std::lock_guard<std::mutex> lock(mutex_); |
| 106 | |
| 107 | for (auto it = pidReg_.begin(); it != pidReg_.end();) { |
| 108 | if (it->second == app) |
| 109 | it = pidReg_.erase(it); |
| 110 | else |
| 111 | ++it; |
| 112 | } |
| 113 | |
| 114 | appCounterReg_.erase(app); |
| 115 | } |