Ring Daemon 16.0.0
Loading...
Searching...
No Matches
pluginmanager.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "noncopyable.h"
20#include "jamiplugin.h"
21#include "pluginloader.h"
22
23#include <functional>
24#include <map>
25#include <mutex>
26#include <vector>
27#include <list>
28
29#include <inttypes.h>
30
31namespace jami {
32
42{
43public:
46
47private:
48 using ObjectDeleter = std::function<void(void*)>;
49
50 // Function that may be called from plugin implementation
51 using ServiceFunction = std::function<int32_t(const DLPlugin*, void*)>;
52
53 // A Component is either a MediaHandler or a ChatHandler.
54 // A ComponentFunction is a function that may start or end a component life.
55 using ComponentFunction = std::function<int32_t(void*, std::mutex&)>;
56
57 // A list of component type (MediaHandler or ChatHandler), and component pointer pairs
58 using ComponentPtrList = std::list<std::pair<std::string, void*>>;
59
60 struct ObjectFactory
61 {
63 ObjectDeleter deleter;
64 };
65
70 struct ComponentLifeCycleManager
71 {
72 // Register component to servicesmanager
73 ComponentFunction takeComponentOwnership;
74
75 // Destroys component in servicesmanager
76 ComponentFunction destroyComponent;
77 };
78
79 // Map between plugin's library path and loader pointer
80 using PluginMap = std::map<std::string, std::pair<std::shared_ptr<Plugin>, bool>>;
81
82 // Map between plugins' library path and their components list
83 using PluginComponentsMap = std::map<std::string, ComponentPtrList>;
84
85 // Map with plugins' destruction functions
86 using ExitFuncMap = std::map<std::string, JAMI_PluginExitFunc>;
87 using ObjectFactoryVec = std::vector<ObjectFactory>;
88 using ObjectFactoryMap = std::map<std::string, ObjectFactory>;
89
90public:
96 bool load(const std::string& path);
97
103 bool unload(const std::string& path);
104
108 std::vector<std::string> getLoadedPlugins() const;
109
113 bool checkLoadedPlugin(const std::string& rootPath) const;
114
121 bool registerService(const std::string& name, ServiceFunction&& func);
122
127 void unRegisterService(const std::string& name);
128
138 bool registerObjectFactory(const char* type, const JAMI_PluginObjectFactory& factory);
139
148 bool registerComponentManager(const std::string& name,
149 ComponentFunction&& takeOwnership,
150 ComponentFunction&& destroyComponent);
151
152private:
154
159 void destroyPluginComponents(const std::string& path);
160
165 bool callPluginInitFunction(const std::string& path);
166
171 bool registerPlugin(std::unique_ptr<Plugin>& plugin);
172
178 std::unique_ptr<void, ObjectDeleter> createObject(const std::string& type);
179
188 static int32_t registerObjectFactory_(const JAMI_PluginAPI* api, const char* type, void* data);
189
194 int32_t invokeService(const DLPlugin* plugin, const std::string& name, void* data);
195
201 int32_t manageComponent(const DLPlugin* plugin, const std::string& name, void* data);
202
203 std::mutex mutex_ {};
205 nullptr, // set by PluginManager constructor
206 registerObjectFactory_,
207 nullptr,
208 nullptr};
209 // Keeps a map between plugin library path and a Plugin instance
210 // for dynamically loaded plugins.
211 PluginMap dynPluginMap_ {};
212
213 // Should keep reference to plugins' destruction functions read during library loading.
214 ExitFuncMap exitFunc_ {};
215
216 ObjectFactoryMap exactMatchMap_ {};
217 ObjectFactoryVec wildCardVec_ {};
218
219 // Keeps a map between services names and service functions.
220 std::map<std::string, ServiceFunction> services_ {};
221
222 // Keeps a ComponentsLifeCycleManager for each available Handler API.
223 std::map<std::string, ComponentLifeCycleManager> componentsLifeCycleManagers_ {};
224
225 // Keeps a map between plugins' library path and their components list.
226 PluginComponentsMap pluginComponentsMap_ {};
227
228 std::mutex mtx_;
229};
230} // namespace jami
This class is used after a plugin library is successfully loaded.
This class manages plugin (un)loading.
bool registerComponentManager(const std::string &name, ComponentFunction &&takeOwnership, ComponentFunction &&destroyComponent)
Registers a component manager that will have two functions, one to take ownership of the component an...
std::vector< std::string > getLoadedPlugins() const
Returns vector with loaded plugins' libraries paths.
bool unload(const std::string &path)
Unloads the plugin.
void unRegisterService(const std::string &name)
Unregister a service from the Plugin System.
bool checkLoadedPlugin(const std::string &rootPath) const
Returns True if plugin is loaded.
bool registerObjectFactory(const char *type, const JAMI_PluginObjectFactory &factory)
Function called from plugin implementation register a new object factory.
bool registerService(const std::string &name, ServiceFunction &&func)
Register a new service in the Plugin System.
bool load(const std::string &path)
Load a dynamic plugin by filename.
#define JAMI_PLUGIN_ABI_VERSION
Definition jamiplugin.h:33
#define JAMI_PLUGIN_API_VERSION
Definition jamiplugin.h:37
void emitSignal(Args... args)
Definition ring_signal.h:64
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
This structure is filled by the PluginManager.
Definition jamiplugin.h:93
This structure is filled by plugin.
Definition jamiplugin.h:78