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