GCC Code Coverage Report


Directory: ./
File: service/backendloader.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 0 50 0.0%
Branches: 0 36 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2014 Daniel Vratil <dvratil@redhat.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19 #include "backendloader.h"
20 #include "backend.h"
21 #include "backenddbuswrapper.h"
22 #include "backendloaderadaptor.h"
23 #include "backendmanager_p.h"
24 #include "disman_backend_launcher_debug.h"
25
26 #include <QCoreApplication>
27 #include <QDBusConnectionInterface>
28 #include <QDir>
29 #include <QPluginLoader>
30
31 #include <memory>
32
33 #include <QDBusConnection>
34 #include <QDBusInterface>
35
36 void pluginDeleter(QPluginLoader* p)
37 {
38 if (p) {
39 qCDebug(DISMAN_BACKEND_LAUNCHER) << "Unloading" << p->fileName();
40 p->unload();
41 delete p;
42 }
43 }
44
45 BackendLoader::BackendLoader()
46 : QObject()
47 , QDBusContext()
48 , mLoader(nullptr)
49 , mBackend(nullptr)
50 {
51 }
52
53 BackendLoader::~BackendLoader()
54 {
55 delete mBackend;
56 pluginDeleter(mLoader);
57 qCDebug(DISMAN_BACKEND_LAUNCHER) << "Backend loader destroyed";
58 }
59
60 bool BackendLoader::init()
61 {
62 QDBusConnection dbus = QDBusConnection::sessionBus();
63 new BackendLoaderAdaptor(this);
64 if (!dbus.registerObject(QStringLiteral("/"), this, QDBusConnection::ExportAdaptors)) {
65 qCWarning(DISMAN_BACKEND_LAUNCHER)
66 << "Failed to export backend to DBus: another launcher already running?";
67 qCWarning(DISMAN_BACKEND_LAUNCHER) << dbus.lastError().message();
68 return false;
69 }
70
71 return true;
72 }
73
74 QString BackendLoader::backend() const
75 {
76 if (mBackend) {
77 return mBackend->backend()->name();
78 }
79
80 return QString();
81 }
82
83 bool BackendLoader::requestBackend(const QString& backendName, const QVariantMap& arguments)
84 {
85 if (mBackend) {
86 // If an backend is already loaded, but it's not the same as the one
87 // requested, then it's an error
88 if (!backendName.isEmpty() && mBackend->backend()->name() != backendName) {
89 sendErrorReply(QDBusError::Failed, QStringLiteral("Another backend is already active"));
90 return false;
91 } else {
92 // If caller requested the same one as already loaded, or did not
93 // request a specific backend, hapilly reuse the existing one
94 return true;
95 }
96 }
97
98 auto backend = loadBackend(backendName, arguments);
99 if (!backend) {
100 return false;
101 }
102
103 mBackend = new BackendDBusWrapper(backend);
104 if (!mBackend->init()) {
105 delete mBackend;
106 mBackend = nullptr;
107 pluginDeleter(mLoader);
108 mLoader = nullptr;
109 return false;
110 }
111 return true;
112 }
113
114 Disman::Backend* BackendLoader::loadBackend(const QString& name, const QVariantMap& arguments)
115 {
116 if (mLoader == nullptr) {
117 std::unique_ptr<QPluginLoader, void (*)(QPluginLoader*)> loader(new QPluginLoader(),
118 pluginDeleter);
119 mLoader = loader.release();
120 }
121 return Disman::BackendManager::load_backend_plugin(mLoader, name, arguments);
122 }
123
124 void BackendLoader::quit()
125 {
126 qApp->quit();
127 }
128