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