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 rootPluginList =
259 m_pluginsSupport->getRootPlugins();
261 FOREACH(it, rootPluginList)
263 PluginModelPtr& pluginModel = *it;
264 PluginPtr pluginLib = pluginModel->LibraryInstance.Get();
267 std::string path = pluginModel->LibraryPath.Get() +
268 std::string(LIBRARY_PATH_SEPARATOR) +
269 pluginModel->LibraryName.Get();
271 pluginLib = Plugin::LoadFromFile(path);
274 LogError("Loading library failed");
276 pluginModel->LibraryInstance.Set(pluginLib);
279 "pluginModel->LibraryInstance.Set() : " <<
280 pluginLib->GetFileName());
283 LogDebug("Already loaded");
287 LogDebug("Preload plugins so file_done");
290 void PluginLogic::Impl::startSession(int widgetHandle,
291 JSGlobalContextRef context,
293 const char* encodedBundle,
296 LogInfo("Starting widget session...");
298 if (!m_pluginsSupport->isInitialized()) {
299 m_pluginsSupport->Initialize(widgetHandle);
301 auto sessionIt = m_sessions.find(context);
303 // Check if corresponding session if not already created
304 if (sessionIt != m_sessions.end()) {
305 LogWarning("Session already started!");
309 auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
310 newSession->startSession(widgetHandle,
316 m_sessions[context] = newSession;
319 void PluginLogic::Impl::stopSession(JSGlobalContextRef context)
321 LogInfo("Stopping widget session...");
323 auto sessionIt = m_sessions.find(context);
324 if (sessionIt == m_sessions.end()) {
325 LogError("Session not exist!");
329 sessionIt->second->stopSession();
330 m_sessions.erase(sessionIt);
332 LogInfo("Widget session stopped.");
335 bool PluginLogic::Impl::loadPluginOnDemand(
336 const WrtDB::DbPluginHandle &pluginHandle,
337 JavaScriptObject& parentObject,
338 JSGlobalContextRef context
341 LogInfo("Load plugin on demand");
343 auto sessionIt = m_sessions.find(context);
344 if (sessionIt == m_sessions.end()) {
345 LogWarning("Session not exist!");
349 return sessionIt->second->loadPluginOnDemand(pluginHandle,
354 void PluginLogic::Impl::loadFrame(JSGlobalContextRef context)
356 LogDebug("Load a frame");
358 PLUGIN_LOGIC_SANITY_CHECK
360 auto sessionIt = m_sessions.find(context);
361 if (sessionIt == m_sessions.end()) {
362 LogWarning("Session not exist!");
366 sessionIt->second->loadFrame(context);
369 void PluginLogic::Impl::unloadFrame(JSGlobalContextRef context)
371 LogDebug("Unload a frame");
373 PLUGIN_LOGIC_SANITY_CHECK
375 auto sessionIt = m_sessions.find(context);
376 if (sessionIt == m_sessions.end()) {
377 LogWarning("Session not exist!");
381 sessionIt->second->unloadFrame(context);
383 // I don't know why this session should be removed here.
384 // session list is removed also from stopSession().
385 //m_sessions.erase(sessionIt);
388 void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
390 const char* encodedBundle,
394 "set properties of window object " << scaleFactor << ", "
395 << encodedBundle << ", " <<
398 PLUGIN_LOGIC_SANITY_CHECK
400 auto sessionIt = m_sessions.find(context);
401 if (sessionIt == m_sessions.end()) {
402 LogWarning("Session not exist!");
406 sessionIt->second->setCustomProperties(scaleFactor,
411 void PluginLogic::Impl::dispatchJavaScriptEvent(JSGlobalContextRef context,
412 CustomEventType eventType,
415 LogDebug("Dispatch event");
417 PLUGIN_LOGIC_SANITY_CHECK
419 auto sessionIt = m_sessions.find(context);
420 if (sessionIt == m_sessions.end()) {
421 LogWarning("Session not exist!");
425 sessionIt->second->dispatchJavaScriptEvent(eventType, data);
428 unsigned int PluginLogic::Impl::windowHandle() const
432 LogError("Object is not available. Wrong flow occured");
434 return m_windowHandle;
437 void PluginLogic::Impl::setWindowHandle(unsigned int handle)
439 PLUGIN_LOGIC_SANITY_CHECK
440 LogDebug("XWindow handle " << handle);
441 m_windowHandle = handle;