Line data Source code
1 : /* 2 : * Copyright (C) 2022-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Tobias Hildebrandt <tobias.hildebrandt@savoirfairelinux.com> 5 : * 6 : * This program is free software; you can redistribute it and/or modify 7 : * it under the terms of the GNU General Public License as published by 8 : * the Free Software Foundation; either version 3 of the License, or 9 : * (at your option) any later version. 10 : * 11 : * This program is distributed in the hope that it will be useful, 12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : * GNU General Public License for more details. 15 : * 16 : * You should have received a copy of the GNU General Public License 17 : * along with this program; if not, write to the Free Software 18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 : */ 20 : 21 : #include "webviewservicesmanager.h" 22 : #include "client/ring_signal.h" 23 : #include "pluginmanager.h" 24 : #include "logger.h" 25 : #include "manager.h" 26 : #include "jamidht/jamiaccount.h" 27 : #include "fileutils.h" 28 : #include "plugin_manager_interface.h" 29 : #include "pluginsutils.h" 30 : #include "webviewmessage.h" 31 : #include <cstdint> 32 : #include <vector> 33 : 34 : namespace jami { 35 : 36 31 : WebViewServicesManager::WebViewServicesManager(PluginManager& pluginManager) 37 : { 38 31 : registerComponentsLifeCycleManagers(pluginManager); 39 31 : registerWebViewService(pluginManager); 40 31 : } 41 : 42 : WebViewHandler* 43 0 : WebViewServicesManager::getWebViewHandlerPointer(const std::string& pluginId) 44 : { 45 0 : auto it = handlersIdMap.find(pluginId); 46 : // check if handler with specified pluginId does not exist 47 0 : if (it == handlersIdMap.end()) { 48 0 : JAMI_ERR("handler with pluginId %s was not found!", pluginId.c_str()); 49 0 : return nullptr; 50 : } 51 : 52 : // we know that the pointer exists 53 0 : return it->second.get(); 54 : } 55 : 56 : void 57 31 : WebViewServicesManager::registerComponentsLifeCycleManagers(PluginManager& pluginManager) 58 : { 59 : // called by the plugin manager whenever a plugin is loaded 60 0 : auto registerWebViewHandler = [this](void* data, std::mutex& pmMtx_) { 61 0 : std::lock_guard lk(pmMtx_); 62 : 63 0 : WebViewHandlerPtr ptr {(static_cast<WebViewHandler*>(data))}; 64 : 65 : // make sure pointer is valid 66 0 : if (!ptr) { 67 0 : JAMI_ERR("trying to register a webview handler with invalid pointer!"); 68 0 : return -1; 69 : } 70 : 71 : // pointer is valid, get details 72 0 : auto id = ptr->id(); 73 : 74 : // add the handler to our map 75 0 : handlersIdMap[id] = std::move(ptr); 76 : 77 0 : return 0; 78 0 : }; 79 : 80 : // called by the plugin manager whenever a plugin is unloaded 81 0 : auto unregisterWebViewHandler = [this](void* data, std::mutex& pmMtx_) { 82 0 : std::lock_guard pluginManagerLock(pmMtx_); 83 : 84 0 : WebViewHandler* ptr {(static_cast<WebViewHandler*>(data))}; 85 : 86 : // make sure pointer is valid 87 0 : if (!ptr) { 88 0 : JAMI_ERR("trying to unregister a webview handler with invalid pointer!"); 89 0 : return false; 90 : } 91 : 92 : // pointer is valid, get details 93 0 : auto id = ptr->id(); 94 : 95 : // remove from our map, unique_ptr gets destroyed 96 0 : handlersIdMap.erase(id); 97 : 98 0 : return true; 99 0 : }; 100 : 101 : // register the functions 102 31 : pluginManager.registerComponentManager("WebViewHandlerManager", 103 : registerWebViewHandler, 104 : unregisterWebViewHandler); 105 31 : } 106 : 107 : void 108 31 : WebViewServicesManager::registerWebViewService(PluginManager& pluginManager) 109 : { 110 : // NOTE: These are API calls that can be called by the plugin 111 0 : auto pluginWebViewMessage = [](const DLPlugin* plugin, void* data) { 112 : // the plugin must pass data as a WebViewMessage pointer 113 0 : auto* message = static_cast<WebViewMessage*>(data); 114 : 115 : // get datapath for the plugin 116 0 : std::string dataPath = PluginUtils::dataPath(plugin->getPath()).string(); 117 : 118 0 : emitSignal<libjami::PluginSignal::WebViewMessageReceived>(dataPath, 119 0 : message->webViewId, 120 0 : message->messageId, 121 0 : message->payload); 122 : 123 0 : return 0; 124 0 : }; 125 : 126 : // register the service. 127 31 : pluginManager.registerService("pluginWebViewMessage", pluginWebViewMessage); 128 31 : } 129 : 130 : void 131 0 : WebViewServicesManager::sendWebViewMessage(const std::string& pluginId, 132 : const std::string& webViewId, 133 : const std::string& messageId, 134 : const std::string& payload) 135 : { 136 0 : if (auto* handler = getWebViewHandlerPointer(pluginId)) { 137 0 : handler->pluginWebViewMessage(webViewId, messageId, payload); 138 : } 139 0 : } 140 : 141 : std::string 142 0 : WebViewServicesManager::sendWebViewAttach(const std::string& pluginId, 143 : const std::string& accountId, 144 : const std::string& webViewId, 145 : const std::string& action) 146 : { 147 0 : if (auto* handler = getWebViewHandlerPointer(pluginId)) { 148 0 : return handler->pluginWebViewAttach(accountId, webViewId, action); 149 : } 150 0 : return ""; 151 : } 152 : 153 : void 154 0 : WebViewServicesManager::sendWebViewDetach(const std::string& pluginId, const std::string& webViewId) 155 : { 156 0 : if (auto* handler = getWebViewHandlerPointer(pluginId)) { 157 0 : handler->pluginWebViewDetach(webViewId); 158 : } 159 0 : } 160 : 161 : } // namespace jami