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