LCOV - code coverage report
Current view: top level - src/plugin - webviewservicesmanager.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 10 57 17.5 %
Date: 2024-12-21 08:56:24 Functions: 3 10 30.0 %

          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             : 
      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          33 : WebViewServicesManager::WebViewServicesManager(PluginManager& pluginManager)
      34             : {
      35          33 :     registerComponentsLifeCycleManagers(pluginManager);
      36          33 :     registerWebViewService(pluginManager);
      37          33 : }
      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          33 : 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          33 :     pluginManager.registerComponentManager("WebViewHandlerManager",
     100             :                                            registerWebViewHandler,
     101             :                                            unregisterWebViewHandler);
     102          33 : }
     103             : 
     104             : void
     105          33 : WebViewServicesManager::registerWebViewService(PluginManager& pluginManager)
     106             : {
     107             :     // NOTE: These are API calls that can be called by the plugin
     108           0 :     auto pluginWebViewMessage = [](const DLPlugin* plugin, void* data) {
     109             :         // the plugin must pass data as a WebViewMessage pointer
     110           0 :         auto* message = static_cast<WebViewMessage*>(data);
     111             : 
     112             :         // get datapath for the plugin
     113           0 :         std::string dataPath = PluginUtils::dataPath(plugin->getPath()).string();
     114             : 
     115           0 :         emitSignal<libjami::PluginSignal::WebViewMessageReceived>(dataPath,
     116           0 :                                                                 message->webViewId,
     117           0 :                                                                 message->messageId,
     118           0 :                                                                 message->payload);
     119             : 
     120           0 :         return 0;
     121           0 :     };
     122             : 
     123             :     // register the service.
     124          33 :     pluginManager.registerService("pluginWebViewMessage", pluginWebViewMessage);
     125          33 : }
     126             : 
     127             : void
     128           0 : WebViewServicesManager::sendWebViewMessage(const std::string& pluginId,
     129             :                                            const std::string& webViewId,
     130             :                                            const std::string& messageId,
     131             :                                            const std::string& payload)
     132             : {
     133           0 :     if (auto* handler = getWebViewHandlerPointer(pluginId)) {
     134           0 :         handler->pluginWebViewMessage(webViewId, messageId, payload);
     135             :     }
     136           0 : }
     137             : 
     138             : std::string
     139           0 : WebViewServicesManager::sendWebViewAttach(const std::string& pluginId,
     140             :                                           const std::string& accountId,
     141             :                                           const std::string& webViewId,
     142             :                                           const std::string& action)
     143             : {
     144           0 :     if (auto* handler = getWebViewHandlerPointer(pluginId)) {
     145           0 :         return handler->pluginWebViewAttach(accountId, webViewId, action);
     146             :     }
     147           0 :     return "";
     148             : }
     149             : 
     150             : void
     151           0 : WebViewServicesManager::sendWebViewDetach(const std::string& pluginId, const std::string& webViewId)
     152             : {
     153           0 :     if (auto* handler = getWebViewHandlerPointer(pluginId)) {
     154           0 :         handler->pluginWebViewDetach(webViewId);
     155             :     }
     156           0 : }
     157             : 
     158             : } // namespace jami

Generated by: LCOV version 1.14