Debian Codegraph @

src/helper/helpermonitor.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "helpermonitor.h"
5
6 #include "common/icryptolayer.h"
7 #include "helperids.h"
8 #include <QDBusConnection>
9 #include <QDBusInterface>
10 #include <QString>
11 #include <algorithm>
12 #include <format>
13 #include <spdlog/spdlog.h>
14 #include <stdexcept>
15 #include <utility>
16
17 HelperMonitor::HelperMonitor(std::shared_ptr<ICryptoLayer> cryptoLayer,
18 QObject *parent) noexcept
19 : QObject(parent)
20 , cryptoLayer_(std::move(cryptoLayer))
21 {
22 }
23
24 void HelperMonitor::addObserver(std::shared_ptr<IHelperMonitor::Observer> observer)
25 {
26 std::lock_guard<std::mutex> lock(mutex_);
27
28 auto const it = std::find(observers_.begin(), observers_.end(), observer);
29 if (it == observers_.end())
30 observers_.emplace_back(std::move(observer));
31 }
32
33 void HelperMonitor::removeObserver(
34 std::shared_ptr<IHelperMonitor::Observer> const &observer)
35 {
36 std::lock_guard<std::mutex> lock(mutex_);
37 std::erase(observers_, observer);
38 }
39
40 void HelperMonitor::init()
41 {
42 monitorInterface_ = std::make_unique<QDBusInterface>(
43 QStringLiteral(DBUS_HELPER_SERVICE), QStringLiteral(DBUS_HELPER_PMON_PATH),
44 QStringLiteral(DBUS_HELPER_PMON_INTERFACE), QDBusConnection::systemBus());
45
46 if (!monitorInterface_->isValid())
47 throw std::runtime_error(
48 std::format("Cannot connect to D-Bus interface {} (path: {})",
49 DBUS_HELPER_PMON_INTERFACE, DBUS_HELPER_PMON_PATH));
50
51 if (!QDBusConnection::systemBus().connect(
52 QStringLiteral(DBUS_HELPER_SERVICE),
53 QStringLiteral(DBUS_HELPER_PMON_PATH),
54 QStringLiteral(DBUS_HELPER_PMON_INTERFACE), QStringLiteral("appExec"),
55 this, SLOT(notifyAppExec(QByteArray const &, QByteArray const &))))
56 throw std::runtime_error(std::format(
57 "Cannot connect to 'appExec' in D-Bus interface {} (path: {})",
58 DBUS_HELPER_PMON_INTERFACE, DBUS_HELPER_PMON_PATH));
59
60 if (!QDBusConnection::systemBus().connect(
61 QStringLiteral(DBUS_HELPER_SERVICE),
62 QStringLiteral(DBUS_HELPER_PMON_PATH),
63 QStringLiteral(DBUS_HELPER_PMON_INTERFACE), QStringLiteral("appExit"),
64 this, SLOT(notifyAppExit(QByteArray const &, QByteArray const &))))
65 throw std::runtime_error(std::format(
66 "Cannot connect to 'appExit' in D-Bus interface {} (path: {})",
67 DBUS_HELPER_PMON_INTERFACE, DBUS_HELPER_PMON_PATH));
68 }
69
70 void HelperMonitor::watchApp(std::string const &app)
71 {
72 QByteArray data(app.c_str());
73 auto signature = cryptoLayer_->signature(data);
74 monitorInterface_->asyncCall(QStringLiteral("watchApp"), data, signature);
75 }
76
77 void HelperMonitor::forgetApp(std::string const &app)
78 {
79 QByteArray data(app.c_str());
80 auto signature = cryptoLayer_->signature(data);
81 monitorInterface_->asyncCall(QStringLiteral("forgetApp"), data, signature);
82 }
83
84 void HelperMonitor::notifyAppExec(QByteArray const &data,
85 QByteArray const &signature)
86 {
87 if (!cryptoLayer_->verify(data, signature)) {
88 SPDLOG_DEBUG("Failed to verify received data from D-Bus");
89 return;
90 }
91
92 std::lock_guard<std::mutex> lock(mutex_);
93 auto const app = data.toStdString();
94 for (auto &o : observers_)
95 o->appExec(app);
96 }
97
98 void HelperMonitor::notifyAppExit(QByteArray const &data,
99 QByteArray const &signature)
100 {
101 if (!cryptoLayer_->verify(data, signature)) {
102 SPDLOG_DEBUG("Failed to verify received data from D-Bus");
103 return;
104 }
105
106 std::lock_guard<std::mutex> lock(mutex_);
107 auto const app = data.toStdString();
108 for (auto &o : observers_)
109 o->appExit(app);
110 }