2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file plugin_logic.cpp
18 * @author Piotr Fatyga (p.fatyga@samsung.com)
19 * @author Grzegorz Krawczyk (g.krawczyk@samsung.com)
20 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
22 * @brief This file is the implementation file of plugin and
23 * feature loading routines
24 * @brief This code is intended to work behind view controller
27 #include "plugin_logic.h"
29 #include <dpl/assert.h>
30 #include <dpl/scoped_array.h>
31 #include <dpl/log/log.h>
32 #include <dpl/foreach.h>
33 #include <dpl/singleton_impl.h>
34 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
35 #include <dpl/wrt-dao-ro/common_dao_types.h>
36 #include <dpl/wrt-dao-ro/global_config.h>
38 #include <JavaScriptCore/JavaScript.h>
41 #include <sys/types.h>
52 #include <wrt_plugin_export.h>
53 #include <js_overlay_types.h>
57 #include "plugin_model.h"
58 #include "javascript_interface.h"
59 #include "js_function_manager.h"
60 #include "plugin_container_support.h"
62 #include "js_page_session.h"
65 using namespace WrtDB;
66 using namespace WrtPlugins::W3C;
69 const char *LIBRARY_PATH_SEPARATOR = "/";
70 const char* FEATURE_WAC20_DEVICAPIS_NAME = "http://wacapps.net/api/deviceapis";
73 class PluginLogic::Impl
75 PluginContainerSupportPtr m_pluginsSupport;
77 typedef std::map<JSContextRef, JSPageSessionPtr> PagesSessionsSet;
78 PagesSessionsSet m_sessions;
86 void startSession(int widgetHandle,
87 JSGlobalContextRef context,
89 const char* encodedBundle,
92 void stopSession(JSGlobalContextRef context);
94 void performLibrariesUnload();
96 bool loadPluginOnDemand(const WrtDB::DbPluginHandle &pluginHandle,
97 JavaScriptObject& parentObject,
98 JSGlobalContextRef context);
100 void loadFrame(JSGlobalContextRef context);
101 void unloadFrame(JSGlobalContextRef context);
103 void setCustomProperties(JSGlobalContextRef ctx,
105 const char* encodedBundle,
108 void dispatchJavaScriptEvent(JSGlobalContextRef ctx,
109 CustomEventType eventType,
114 IMPLEMENT_SINGLETON(PluginLogic);
116 PluginLogic::Impl::Impl()
118 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
119 LogDebug("Initializing Plugin Logic...");
120 m_pluginsSupport = PluginContainerSupportPtr(new PluginContainerSupport());
122 // explicit call to keep singleton's lifetime until calling destructor.
123 JsFunctionManagerSingleton::Instance();
124 JavaScriptInterfaceSingleton::Instance();
127 PluginLogic::Impl::~Impl()
129 FOREACH(it, m_sessions)
131 LogError("Must stop widget session before exit!");
132 it->second->stopSession();
135 LogDebug("Deinitializing plugin Logic...");
139 void PluginLogic::startSession(int widgetHandle,
140 JSGlobalContextRef ctx,
142 const char* encodedBundle,
145 m_impl->startSession(widgetHandle, ctx, scaleFactor, encodedBundle, theme);
148 void PluginLogic::stopSession(JSGlobalContextRef context)
150 m_impl->stopSession(context);
153 void PluginLogic::performLibrariesUnload()
155 LogError("Libraries unload TURNED OFF");
156 // m_impl->performLibrariesUnload();
159 bool PluginLogic::loadPluginOnDemand(
160 const WrtDB::DbPluginHandle &pluginHandle,
161 JavaScriptObject& parentObject,
162 JSGlobalContextRef context)
164 return m_impl->loadPluginOnDemand(pluginHandle, parentObject, context);
167 void PluginLogic::loadPluginsIntoIframes(JSGlobalContextRef context)
169 LogError("This function is Deprecated");
172 void PluginLogic::setCustomProperties(double scaleFactor,
173 const char* encodedBundle,
176 LogError("This function is DEPRECATED");
179 void PluginLogic::setCustomProperties(JSGlobalContextRef context,
181 const char* encodedBundle,
184 m_impl->setCustomProperties(context, scaleFactor, encodedBundle, theme);
187 void PluginLogic::dispatchJavaScriptEvent(CustomEventType eventType)
189 LogError("This function is DEPRECATED");
192 void PluginLogic::dispatchJavaScriptEvent(JSGlobalContextRef context,
193 CustomEventType eventType,
196 m_impl->dispatchJavaScriptEvent(context, eventType, data);
199 void PluginLogic::loadFrame(JSGlobalContextRef context)
201 m_impl->loadFrame(context);
204 void PluginLogic::unloadFrame(JSGlobalContextRef context)
206 m_impl->unloadFrame(context);
209 PluginLogic::PluginLogic() : m_impl(new PluginLogic::Impl())
213 PluginLogic::~PluginLogic()
217 void PluginLogic::Impl::startSession(int widgetHandle,
218 JSGlobalContextRef context,
220 const char* encodedBundle,
223 LogInfo("Starting widget session...");
225 if (!m_pluginsSupport->isInitialized())
226 m_pluginsSupport->readAllowedPlugins(widgetHandle);
228 auto sessionIt = m_sessions.find(context);
230 // Check if corresponding session if not already created
231 if (sessionIt != m_sessions.end())
233 LogWarning("Session already started!");
237 auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
238 newSession->startSession(widgetHandle,
244 m_sessions[context] = newSession;
247 void PluginLogic::Impl::stopSession(JSGlobalContextRef context)
249 LogInfo("Stopping widget session...");
251 auto sessionIt = m_sessions.find(context);
252 if (sessionIt == m_sessions.end())
254 LogError("Session not exist!");
258 sessionIt->second->stopSession();
259 m_sessions.erase(sessionIt);
261 LogInfo("Widget session stopped.");
264 bool PluginLogic::Impl::loadPluginOnDemand(
265 const WrtDB::DbPluginHandle &pluginHandle,
266 JavaScriptObject& parentObject,
267 JSGlobalContextRef context
270 LogInfo("Load plugin on demand");
272 auto sessionIt = m_sessions.find(context);
273 if (sessionIt == m_sessions.end())
275 LogWarning("Session not exist!");
279 return sessionIt->second->loadPluginOnDemand(pluginHandle,
284 void PluginLogic::Impl::loadFrame(JSGlobalContextRef context)
286 LogDebug("Load a frame");
287 auto sessionIt = m_sessions.find(context);
288 if (sessionIt == m_sessions.end())
290 LogWarning("Session not exist!");
294 sessionIt->second->loadFrame(context);
297 void PluginLogic::Impl::unloadFrame(JSGlobalContextRef context)
299 LogDebug("Unload a frame");
301 auto sessionIt = m_sessions.find(context);
302 if (sessionIt == m_sessions.end())
304 LogWarning("Session not exist!");
308 sessionIt->second->unloadFrame(context);
311 void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
313 const char* encodedBundle,
316 LogInfo("set properties of window object " << scaleFactor << ", "
317 << encodedBundle << ", " << theme);
319 auto sessionIt = m_sessions.find(context);
320 if (sessionIt == m_sessions.end())
322 LogWarning("Session not exist!");
326 sessionIt->second->setCustomProperties(scaleFactor,
332 void PluginLogic::Impl::dispatchJavaScriptEvent(JSGlobalContextRef context,
333 CustomEventType eventType,
336 LogDebug("Dispatch event");
338 auto sessionIt = m_sessions.find(context);
339 if (sessionIt == m_sessions.end())
341 LogWarning("Session not exist!");
345 sessionIt->second->dispatchJavaScriptEvent(eventType, data);