Line data Source code
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 : 18 : #include "webviewservicesmanager.h" 19 : #include "client/ring_signal.h" 20 : #include "pluginmanager.h" 21 : #include "logger.h" 22 : #include "manager.h" 23 : #include "jamidht/jamiaccount.h" 24 : #include "fileutils.h" 25 : #include "plugin_manager_interface.h" 26 : #include "pluginsutils.h" 27 : #include "webviewmessage.h" 28 : #include <cstdint> 29 : #include <vector> 30 : 31 : namespace jami { 32 : 33 32 : WebViewServicesManager::WebViewServicesManager(PluginManager& pluginManager) 34 : { 35 32 : registerComponentsLifeCycleManagers(pluginManager); 36 32 : registerWebViewService(pluginManager); 37 32 : } 38 : 39 : WebViewHandler* 40 0 : WebViewServicesManager::getWebViewHandlerPointer(const std::string& pluginId) 41 : { 42 0 : auto it = handlersIdMap.find(pluginId); 43 : // check if handler with specified pluginId does not exist 44 0 : if (it == handlersIdMap.end()) { 45 0 : JAMI_ERR("handler with pluginId %s was not found!", pluginId.c_str()); 46 0 : return nullptr; 47 : } 48 : 49 : // we know that the pointer exists 50 0 : return it->second.get(); 51 : } 52 : 53 : void 54 32 : WebViewServicesManager::registerComponentsLifeCycleManagers(PluginManager& pluginManager) 55 : { 56 : // called by the plugin manager whenever a plugin is loaded 57 0 : auto registerWebViewHandler = [this](void* data, std::mutex& pmMtx_) { 58 0 : std::lock_guard lk(pmMtx_); 59 : 60 0 : WebViewHandlerPtr ptr {(static_cast<WebViewHandler*>(data))}; 61 : 62 : // make sure pointer is valid 63 0 : if (!ptr) { 64 0 : JAMI_ERR("Attempting to register a webview handler with invalid pointer!"); 65 0 : return -1; 66 : } 67 : 68 : // pointer is valid, get details 69 0 : auto id = ptr->id(); 70 : 71 : // add the handler to our map 72 0 : handlersIdMap[id] = std::move(ptr); 73 : 74 0 : return 0; 75 0 : }; 76 : 77 : // called by the plugin manager whenever a plugin is unloaded 78 0 : auto unregisterWebViewHandler = [this](void* data, std::mutex& pmMtx_) { 79 0 : std::lock_guard pluginManagerLock(pmMtx_); 80 : 81 0 : WebViewHandler* ptr {(static_cast<WebViewHandler*>(data))}; 82 : 83 : // make sure pointer is valid 84 0 : if (!ptr) { 85 0 : JAMI_ERR("Attempting to unregister a webview handler with invalid pointer!"); 86 0 : return false; 87 : } 88 : 89 : // pointer is valid, get details 90 0 : auto id = ptr->id(); 91 : 92 : // remove from our map, unique_ptr gets destroyed 93 0 : handlersIdMap.erase(id); 94 : 95 0 : return true; 96 0 : }; 97 : 98 : // register the functions 99 32 : pluginManager.registerComponentManager("WebViewHandlerManager", registerWebViewHandler, unregisterWebViewHandler); 100 32 : } 101 : 102 : void 103 32 : WebViewServicesManager::registerWebViewService(PluginManager& pluginManager) 104 : { 105 : // NOTE: These are API calls that can be called by the plugin 106 0 : auto pluginWebViewMessage = [](const DLPlugin* plugin, void* data) { 107 : // the plugin must pass data as a WebViewMessage pointer 108 0 : auto* message = static_cast<WebViewMessage*>(data); 109 : 110 : // get datapath for the plugin 111 0 : std::string dataPath = PluginUtils::dataPath(plugin->getPath()).string(); 112 : 113 0 : emitSignal<libjami::PluginSignal::WebViewMessageReceived>(dataPath, 114 0 : message->webViewId, 115 0 : message->messageId, 116 0 : message->payload); 117 : 118 0 : return 0; 119 0 : }; 120 : 121 : // register the service. 122 32 : pluginManager.registerService("pluginWebViewMessage", pluginWebViewMessage); 123 32 : } 124 : 125 : void 126 0 : WebViewServicesManager::sendWebViewMessage(const std::string& pluginId, 127 : const std::string& webViewId, 128 : const std::string& messageId, 129 : const std::string& payload) 130 : { 131 0 : if (auto* handler = getWebViewHandlerPointer(pluginId)) { 132 0 : handler->pluginWebViewMessage(webViewId, messageId, payload); 133 : } 134 0 : } 135 : 136 : std::string 137 0 : WebViewServicesManager::sendWebViewAttach(const std::string& pluginId, 138 : const std::string& accountId, 139 : const std::string& webViewId, 140 : const std::string& action) 141 : { 142 0 : if (auto* handler = getWebViewHandlerPointer(pluginId)) { 143 0 : return handler->pluginWebViewAttach(accountId, webViewId, action); 144 : } 145 0 : return ""; 146 : } 147 : 148 : void 149 0 : WebViewServicesManager::sendWebViewDetach(const std::string& pluginId, const std::string& webViewId) 150 : { 151 0 : if (auto* handler = getWebViewHandlerPointer(pluginId)) { 152 0 : handler->pluginWebViewDetach(webViewId); 153 : } 154 0 : } 155 : 156 : } // namespace jami