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 = "/";
72 class PluginLogic::Impl
74 PluginContainerSupportPtr m_pluginsSupport;
76 typedef std::map<JSContextRef, JSPageSessionPtr> PagesSessionsSet;
77 PagesSessionsSet m_sessions;
78 unsigned int m_windowHandle;
85 void initSession(int widgetHandle);
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,
112 unsigned int windowHandle() const;
113 void setWindowHandle(unsigned int handle);
115 static bool s_sanityCheck;
118 IMPLEMENT_SINGLETON(PluginLogic);
120 bool PluginLogic::Impl::s_sanityCheck = false;
122 #define PLUGIN_LOGIC_SANITY_CHECK \
123 if (!s_sanityCheck) \
125 LogError("Object is not available. Wrong flow occured"); \
129 PluginLogic::Impl::Impl() :
132 s_sanityCheck = true;
134 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
135 LogDebug("Initializing Plugin Logic...");
136 m_pluginsSupport = PluginContainerSupportPtr(new PluginContainerSupport());
138 // explicit call to keep singleton's lifetime until calling destructor.
139 JsFunctionManagerSingleton::Instance();
140 JavaScriptInterfaceSingleton::Instance();
143 PluginLogic::Impl::~Impl()
147 s_sanityCheck = false;
149 FOREACH(it, m_sessions)
151 LogError("Must stop widget session before exit!");
152 it->second->stopSession();
155 LogDebug("Deinitializing plugin Logic...");
158 void PluginLogic::initSession(int widgetHandle)
160 m_impl->initSession(widgetHandle);
163 void PluginLogic::startSession(int widgetHandle,
164 JSGlobalContextRef ctx,
166 const char* encodedBundle,
169 m_impl->startSession(widgetHandle, ctx, scaleFactor, encodedBundle, theme);
172 void PluginLogic::stopSession(JSGlobalContextRef context)
174 m_impl->stopSession(context);
177 void PluginLogic::performLibrariesUnload()
179 LogError("Libraries unload TURNED OFF");
180 // m_impl->performLibrariesUnload();
183 bool PluginLogic::loadPluginOnDemand(
184 const WrtDB::DbPluginHandle &pluginHandle,
185 JavaScriptObject& parentObject,
186 JSGlobalContextRef context)
188 return m_impl->loadPluginOnDemand(pluginHandle, parentObject, context);
191 void PluginLogic::loadPluginsIntoIframes(JSGlobalContextRef /*context*/)
193 LogError("This function is Deprecated");
196 void PluginLogic::setCustomProperties(double /*scaleFactor*/,
197 const char* /*encodedBundle*/,
198 const char* /*theme*/)
200 LogError("This function is DEPRECATED");
203 void PluginLogic::setCustomProperties(JSGlobalContextRef context,
205 const char* encodedBundle,
208 m_impl->setCustomProperties(context, scaleFactor, encodedBundle, theme);
211 void PluginLogic::dispatchJavaScriptEvent(CustomEventType /*eventType*/)
213 LogError("This function is DEPRECATED");
216 void PluginLogic::dispatchJavaScriptEvent(JSGlobalContextRef context,
217 CustomEventType eventType,
220 m_impl->dispatchJavaScriptEvent(context, eventType, data);
223 void PluginLogic::loadFrame(JSGlobalContextRef context)
225 m_impl->loadFrame(context);
228 void PluginLogic::unloadFrame(JSGlobalContextRef context)
230 m_impl->unloadFrame(context);
233 unsigned int PluginLogic::windowHandle() const
235 return m_impl->windowHandle();
238 void PluginLogic::setWindowHandle(unsigned int handle)
240 m_impl->setWindowHandle(handle);
243 PluginLogic::PluginLogic() : m_impl(new PluginLogic::Impl())
246 PluginLogic::~PluginLogic()
249 void PluginLogic::Impl::initSession(int widgetHandle)
251 LogInfo("init pluginLogic...");
253 m_pluginsSupport->Initialize(widgetHandle);
255 //add standard objects
256 LogDebug("Preload plugins so file");
258 PluginContainerSupport::PluginsList pluginList =
259 m_pluginsSupport->getPluginsList();
261 FOREACH(it, pluginList)
263 PluginModelPtr& pluginModel = *it;
265 if (!pluginModel->LibraryDependencies.Get()->empty()) {
270 PluginPtr pluginLib = pluginModel->LibraryInstance.Get();
273 std::string path = pluginModel->LibraryPath.Get() +
274 std::string(LIBRARY_PATH_SEPARATOR) +
275 pluginModel->LibraryName.Get();
277 pluginLib = Plugin::LoadFromFile(path);
280 LogError("Loading library failed");
282 pluginModel->LibraryInstance.Set(pluginLib);
285 "pluginModel->LibraryInstance.Set() : " <<
286 pluginLib->GetFileName());
289 LogDebug("Already loaded");
293 LogDebug("Preload plugins so file_done");
296 void PluginLogic::Impl::startSession(int widgetHandle,
297 JSGlobalContextRef context,
299 const char* encodedBundle,
302 LogInfo("Starting widget session...");
304 if (!m_pluginsSupport->isInitialized()) {
305 m_pluginsSupport->Initialize(widgetHandle);
307 auto sessionIt = m_sessions.find(context);
309 // Check if corresponding session if not already created
310 if (sessionIt != m_sessions.end()) {
311 LogWarning("Session already started!");
315 auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
316 newSession->startSession(widgetHandle,
322 m_sessions[context] = newSession;
325 void PluginLogic::Impl::stopSession(JSGlobalContextRef context)
327 LogInfo("Stopping widget session...");
329 auto sessionIt = m_sessions.find(context);
330 if (sessionIt == m_sessions.end()) {
331 LogError("Session not exist!");
335 sessionIt->second->stopSession();
336 m_sessions.erase(sessionIt);
338 LogInfo("Widget session stopped.");
341 bool PluginLogic::Impl::loadPluginOnDemand(
342 const WrtDB::DbPluginHandle &pluginHandle,
343 JavaScriptObject& parentObject,
344 JSGlobalContextRef context
347 LogInfo("Load plugin on demand");
349 auto sessionIt = m_sessions.find(context);
350 if (sessionIt == m_sessions.end()) {
351 LogWarning("Session not exist!");
355 return sessionIt->second->loadPluginOnDemand(pluginHandle,
360 void PluginLogic::Impl::loadFrame(JSGlobalContextRef context)
362 LogDebug("Load a frame");
364 PLUGIN_LOGIC_SANITY_CHECK
366 auto sessionIt = m_sessions.find(context);
367 if (sessionIt == m_sessions.end()) {
368 LogWarning("Session not exist!");
372 sessionIt->second->loadFrame(context);
375 void PluginLogic::Impl::unloadFrame(JSGlobalContextRef context)
377 LogDebug("Unload a frame");
379 PLUGIN_LOGIC_SANITY_CHECK
381 auto sessionIt = m_sessions.find(context);
382 if (sessionIt == m_sessions.end()) {
383 LogWarning("Session not exist!");
387 sessionIt->second->unloadFrame(context);
389 // I don't know why this session should be removed here.
390 // session list is removed also from stopSession().
391 //m_sessions.erase(sessionIt);
394 void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
396 const char* encodedBundle,
400 "set properties of window object " << scaleFactor << ", "
401 << encodedBundle << ", " <<
404 PLUGIN_LOGIC_SANITY_CHECK
406 auto sessionIt = m_sessions.find(context);
407 if (sessionIt == m_sessions.end()) {
408 LogWarning("Session not exist!");
412 sessionIt->second->setCustomProperties(scaleFactor,
417 void PluginLogic::Impl::dispatchJavaScriptEvent(JSGlobalContextRef context,
418 CustomEventType eventType,
421 LogDebug("Dispatch event");
423 PLUGIN_LOGIC_SANITY_CHECK
425 auto sessionIt = m_sessions.find(context);
426 if (sessionIt == m_sessions.end()) {
427 LogWarning("Session not exist!");
431 sessionIt->second->dispatchJavaScriptEvent(eventType, data);
434 unsigned int PluginLogic::Impl::windowHandle() const
436 PLUGIN_LOGIC_SANITY_CHECK
437 return m_windowHandle;
440 void PluginLogic::Impl::setWindowHandle(unsigned int handle)
442 PLUGIN_LOGIC_SANITY_CHECK
443 LogDebug("XWindow handle " << handle);
444 m_windowHandle = handle;