Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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 "jamiplugin.h" 20 : #include <dlfcn.h> 21 : #include <string> 22 : #include <memory> 23 : 24 : namespace jami { 25 : 26 : /** 27 : * @class Plugin 28 : * @brief This class is used to attempt loading a plugin library. 29 : */ 30 : class Plugin 31 : { 32 : public: 33 8 : virtual ~Plugin() = default; 34 : 35 : /** 36 : * @brief Load plugin's library. 37 : * @return DLPlugin if success. 38 : */ 39 : static Plugin* load(const std::string& path, std::string& error); 40 : virtual void* getSymbol(const char* name) const = 0; 41 : 42 : /** 43 : * @brief Search loaded library for its initialization function 44 : * @return Plugin's initialization function. 45 : */ 46 16 : virtual JAMI_PluginInitFunc getInitFunction() const 47 : { 48 16 : return reinterpret_cast<JAMI_PluginInitFunc>(getSymbol(JAMI_DYN_INIT_FUNC_NAME)); 49 : } 50 : 51 : protected: 52 8 : Plugin() = default; 53 : }; 54 : 55 : /** 56 : * @class DLPlugin 57 : * @brief This class is used after a plugin library is successfully loaded. 58 : */ 59 : class DLPlugin : public Plugin 60 : { 61 : public: 62 8 : DLPlugin(void* handle, const std::string& path) 63 24 : : handle_(handle, ::dlclose) 64 8 : , path_ {path} 65 : { 66 8 : api_.context = this; 67 8 : } 68 : 69 16 : virtual ~DLPlugin() { unload(); } 70 : 71 : /** 72 : * @brief Unload plugin's library. 73 : * @return True if success. 74 : */ 75 8 : bool unload() 76 : { 77 8 : if (!handle_) { 78 0 : return false; 79 : } 80 8 : return !(::dlclose(handle_.release())); 81 : } 82 : 83 : /** 84 : * @brief Searchs for symbol in library. 85 : * @param name 86 : * @return symbol. 87 : */ 88 16 : void* getSymbol(const char* name) const 89 : { 90 16 : if (!handle_) 91 0 : return nullptr; 92 : 93 16 : return ::dlsym(handle_.get(), name); 94 : } 95 : 96 40 : const std::string& getPath() const { return path_; } 97 : 98 : public: 99 : void* apiContext_; 100 : JAMI_PluginAPI api_; 101 : 102 : private: 103 : // Pointer to the loaded library returned by dlopen 104 : std::unique_ptr<void, int (*)(void*)> handle_; 105 : // Plugin's data path 106 : const std::string path_; 107 : }; 108 : 109 : } // namespace jami