Ring Daemon
Loading...
Searching...
No Matches
pluginmanager.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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
29namespace jami {
30
40{
41public:
44
45private:
46 using ObjectDeleter = std::function<void(void*)>;
47
48 // Function that may be called from plugin implementation
49 using ServiceFunction = std::function<int32_t(const DLPlugin*, void*)>;
50
51 // A Component is either a MediaHandler or a ChatHandler.
52 // A ComponentFunction is a function that may start or end a component life.
53 using ComponentFunction = std::function<int32_t(void*, std::mutex&)>;
54
55 // A list of component type (MediaHandler or ChatHandler), and component pointer pairs
56 using ComponentPtrList = std::list<std::pair<std::string, void*>>;
57
58 struct ObjectFactory
59 {
61 ObjectDeleter deleter;
62 };
63
68 struct ComponentLifeCycleManager
69 {
70 // Register component to servicesmanager
71 ComponentFunction takeComponentOwnership;
72
73 // Destroys component in servicesmanager
74 ComponentFunction destroyComponent;
75 };
76
77 // Map between plugin's library path and loader pointer
78 using PluginMap = std::map<std::string, std::pair<std::shared_ptr<Plugin>, bool>>;
79
80 // Map between plugins' library path and their components list
81 using PluginComponentsMap = std::map<std::string, ComponentPtrList>;
82
83 // Map with plugins' destruction functions
84 using ExitFuncMap = std::map<std::string, JAMI_PluginExitFunc>;
85 using ObjectFactoryVec = std::vector<ObjectFactory>;
86 using ObjectFactoryMap = std::map<std::string, ObjectFactory>;
87
88public:
94 bool load(const std::string& path);
95
101 bool unload(const std::string& path);
102
106 std::vector<std::string> getLoadedPlugins() const;
107
111 bool checkLoadedPlugin(const std::string& rootPath) const;
112
119 bool registerService(const std::string& name, ServiceFunction&& func);
120
125 void unRegisterService(const std::string& name);
126
136 bool registerObjectFactory(const char* type, const JAMI_PluginObjectFactory& factory);
137
146 bool registerComponentManager(const std::string& name,
147 ComponentFunction&& takeOwnership,
148 ComponentFunction&& destroyComponent);
149
150private:
152
157 void destroyPluginComponents(const std::string& path);
158
163 bool callPluginInitFunction(const std::string& path);
164
169 bool registerPlugin(std::unique_ptr<Plugin>& plugin);
170
176 std::unique_ptr<void, ObjectDeleter> createObject(const std::string& type);
177
186 static int32_t registerObjectFactory_(const JAMI_PluginAPI* api, const char* type, void* data);
187
192 int32_t invokeService(const DLPlugin* plugin, const std::string& name, void* data);
193
199 int32_t manageComponent(const DLPlugin* plugin, const std::string& name, void* data);
200
201 std::mutex mutex_ {};
203 nullptr, // set by PluginManager constructor
204 registerObjectFactory_,
205 nullptr,
206 nullptr};
207 // Keeps a map between plugin library path and a Plugin instance
208 // for dynamically loaded plugins.
209 PluginMap dynPluginMap_ {};
210
211 // Should keep reference to plugins' destruction functions read during library loading.
212 ExitFuncMap exitFunc_ {};
213
214 ObjectFactoryMap exactMatchMap_ {};
215 ObjectFactoryVec wildCardVec_ {};
216
217 // Keeps a map between services names and service functions.
218 std::map<std::string, ServiceFunction> services_ {};
219
220 // Keeps a ComponentsLifeCycleManager for each available Handler API.
221 std::map<std::string, ComponentLifeCycleManager> componentsLifeCycleManagers_ {};
222
223 // Keeps a map between plugins' library path and their components list.
224 PluginComponentsMap pluginComponentsMap_ {};
225
226 std::mutex mtx_;
227};
228} // 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:32
#define JAMI_PLUGIN_API_VERSION
Definition jamiplugin.h:36
void emitSignal(Args... args)
Definition jami_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:92
This structure is filled by plugin.
Definition jamiplugin.h:77