| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "helperids.h" |
| 7 | #include "ipmonmsgdispatcher.h" |
| 8 | #include <QObject> |
| 9 | #include <memory> |
| 10 | |
| 11 | class IAppRegistry; |
| 12 | class ICryptoLayer; |
| 13 | class QByteArray; |
| 14 | |
| 15 | // Methods of PMonMsgDispatcher class will be called in the ProcessMonitor |
| 16 | // thread. However D-Bus signals must be emitted in the thread of the object |
| 17 | // that registered the D-Bus object. For this reason, signals are forwarded |
| 18 | // to PMonDBusSignalDispatcher through QObject signal / slot mechanism. |
| 19 | // PMonDBusSignalDispatcher, as the object that registered the D-Bus object, |
| 20 | // will re-emit the forwaded signals through D-Bus in the event loop. |
| 21 | |
| 22 | class DBusSignalDispatcher final : public QObject |
| 23 | { |
| 24 | Q_OBJECT |
| 25 | Q_CLASSINFO("D-Bus Interface", DBUS_HELPER_PMON_INTERFACE) |
| 26 | |
| 27 | public: |
| 28 | DBusSignalDispatcher(std::shared_ptr<ICryptoLayer> cryptoLayer, |
| 29 | std::shared_ptr<IAppRegistry> appRegistry, |
| 30 | QObject *parent = nullptr); |
| 31 | ~DBusSignalDispatcher() override; |
| 32 | |
| 33 | public slots: |
| 34 | void sendExec(QByteArray const &data); |
| 35 | void sendExit(QByteArray const &data); |
| 36 | |
| 37 | // D-Bus interface methods |
| 38 | Q_SCRIPTABLE void watchApp(QByteArray const &data, QByteArray const &signature); |
| 39 | Q_SCRIPTABLE void forgetApp(QByteArray const &data, |
| 40 | QByteArray const &signature); |
| 41 | |
| 42 | signals: |
| 43 | // D-Bus interface signals |
| 44 | Q_SCRIPTABLE void appExec(QByteArray const &data, QByteArray const &signature); |
| 45 | Q_SCRIPTABLE void appExit(QByteArray const &data, QByteArray const &signature); |
| 46 | |
| 47 | private: |
| 48 | std::shared_ptr<ICryptoLayer> cryptoLayer_; |
| 49 | std::shared_ptr<IAppRegistry> appRegistry_; |
| 50 | }; |
| 51 | |
| 52 | class MsgDispatcher final |
| 53 | : public QObject |
| 54 | , public IPMonMsgDispatcher |
| 55 | { |
| 56 | Q_OBJECT |
| 57 | |
| 58 | public: |
| 59 | MsgDispatcher(std::shared_ptr<ICryptoLayer> cryptoLayer, |
| 60 | std::shared_ptr<IAppRegistry> appRegistry, |
| 61 | QObject *parent = nullptr); |
| 62 | |
| 63 | void sendExec(std::string const &app) override; |
| 64 | void sendExit(std::string const &app) override; |
| 65 | |
| 66 | signals: |
| 67 | void appExec(QByteArray const &data); |
| 68 | void appExit(QByteArray const &data); |
| 69 | |
| 70 | private: |
| 71 | DBusSignalDispatcher dbusSignalDispatcher_; |
| 72 | }; |