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 initSession(int widgetHandle);
87 void startSession(int widgetHandle,
88 JSGlobalContextRef context,
90 const char* encodedBundle,
93 void stopSession(JSGlobalContextRef context);
95 void performLibrariesUnload();
97 bool loadPluginOnDemand(const WrtDB::DbPluginHandle &pluginHandle,
98 JavaScriptObject& parentObject,
99 JSGlobalContextRef context);
101 void loadFrame(JSGlobalContextRef context);
102 void unloadFrame(JSGlobalContextRef context);
104 void setCustomProperties(JSGlobalContextRef ctx,
106 const char* encodedBundle,
109 void dispatchJavaScriptEvent(JSGlobalContextRef ctx,
110 CustomEventType eventType,
113 static bool s_sanityCheck;
116 IMPLEMENT_SINGLETON(PluginLogic);
118 bool PluginLogic::Impl::s_sanityCheck = false;
120 #define PLUGIN_LOGIC_SANITY_CHECK \
123 LogError("Object is not available. Wrong flow occured");\
127 PluginLogic::Impl::Impl()
129 s_sanityCheck = true;
131 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
132 LogDebug("Initializing Plugin Logic...");
133 m_pluginsSupport = PluginContainerSupportPtr(new PluginContainerSupport());
135 // explicit call to keep singleton's lifetime until calling destructor.
136 JsFunctionManagerSingleton::Instance();
137 JavaScriptInterfaceSingleton::Instance();
140 PluginLogic::Impl::~Impl()
144 s_sanityCheck = false;
146 FOREACH(it, m_sessions)
148 LogError("Must stop widget session before exit!");
149 it->second->stopSession();
152 LogDebug("Deinitializing plugin Logic...");
155 void PluginLogic::initSession(int widgetHandle)
157 m_impl->initSession(widgetHandle);
160 void PluginLogic::startSession(int widgetHandle,
161 JSGlobalContextRef ctx,
163 const char* encodedBundle,
166 m_impl->startSession(widgetHandle, ctx, scaleFactor, encodedBundle, theme);
169 void PluginLogic::stopSession(JSGlobalContextRef context)
171 m_impl->stopSession(context);
174 void PluginLogic::performLibrariesUnload()
176 LogError("Libraries unload TURNED OFF");
177 // m_impl->performLibrariesUnload();
180 bool PluginLogic::loadPluginOnDemand(
181 const WrtDB::DbPluginHandle &pluginHandle,
182 JavaScriptObject& parentObject,
183 JSGlobalContextRef context)
185 return m_impl->loadPluginOnDemand(pluginHandle, parentObject, context);
188 void PluginLogic::loadPluginsIntoIframes(JSGlobalContextRef context)
190 LogError("This function is Deprecated");
193 void PluginLogic::setCustomProperties(double scaleFactor,
194 const char* encodedBundle,
197 LogError("This function is DEPRECATED");
200 void PluginLogic::setCustomProperties(JSGlobalContextRef context,
202 const char* encodedBundle,
205 m_impl->setCustomProperties(context, scaleFactor, encodedBundle, theme);
208 void PluginLogic::dispatchJavaScriptEvent(CustomEventType eventType)
210 LogError("This function is DEPRECATED");
213 void PluginLogic::dispatchJavaScriptEvent(JSGlobalContextRef context,
214 CustomEventType eventType,
217 m_impl->dispatchJavaScriptEvent(context, eventType, data);
220 void PluginLogic::loadFrame(JSGlobalContextRef context)
222 m_impl->loadFrame(context);
225 void PluginLogic::unloadFrame(JSGlobalContextRef context)
227 m_impl->unloadFrame(context);
230 PluginLogic::PluginLogic() : m_impl(new PluginLogic::Impl())
234 PluginLogic::~PluginLogic()
238 void PluginLogic::Impl::initSession(int widgetHandle)
240 LogInfo("init pluginLogic...");
242 m_pluginsSupport->Initialize(widgetHandle);
244 //add standard objects
245 LogDebug("Preload plugins so file");
247 PluginContainerSupport::PluginsList pluginList =
248 m_pluginsSupport->getPluginsList();
250 FOREACH(it, pluginList)
252 PluginModelPtr& pluginModel = *it;
253 PluginPtr pluginLib = pluginModel->LibraryInstance.Get();
257 std::string path = pluginModel->LibraryPath.Get() +
258 std::string(LIBRARY_PATH_SEPARATOR) +
259 pluginModel->LibraryName.Get();
261 pluginLib = Plugin::LoadFromFile(path);
265 LogError("Loading library failed");
268 pluginModel->LibraryInstance.Set(pluginLib);
270 LogDebug("pluginModel->LibraryInstance.Set() : " << pluginLib->GetFileName());
275 LogDebug("Already loaded");
279 LogDebug("Preload plugins so file_done");
282 void PluginLogic::Impl::startSession(int widgetHandle,
283 JSGlobalContextRef context,
285 const char* encodedBundle,
288 LogInfo("Starting widget session...");
290 if (!m_pluginsSupport->isInitialized())
292 m_pluginsSupport->Initialize(widgetHandle);
294 auto sessionIt = m_sessions.find(context);
296 // Check if corresponding session if not already created
297 if (sessionIt != m_sessions.end())
299 LogWarning("Session already started!");
303 auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
304 newSession->startSession(widgetHandle,
310 m_sessions[context] = newSession;
313 void PluginLogic::Impl::stopSession(JSGlobalContextRef context)
315 LogInfo("Stopping widget session...");
317 auto sessionIt = m_sessions.find(context);
318 if (sessionIt == m_sessions.end())
320 LogError("Session not exist!");
324 sessionIt->second->stopSession();
325 m_sessions.erase(sessionIt);
327 LogInfo("Widget session stopped.");
330 bool PluginLogic::Impl::loadPluginOnDemand(
331 const WrtDB::DbPluginHandle &pluginHandle,
332 JavaScriptObject& parentObject,
333 JSGlobalContextRef context
336 LogInfo("Load plugin on demand");
338 auto sessionIt = m_sessions.find(context);
339 if (sessionIt == m_sessions.end())
341 LogWarning("Session not exist!");
345 return sessionIt->second->loadPluginOnDemand(pluginHandle,
350 void PluginLogic::Impl::loadFrame(JSGlobalContextRef context)
352 LogDebug("Load a frame");
354 PLUGIN_LOGIC_SANITY_CHECK
356 auto sessionIt = m_sessions.find(context);
357 if (sessionIt == m_sessions.end())
359 LogWarning("Session not exist!");
363 sessionIt->second->loadFrame(context);
366 void PluginLogic::Impl::unloadFrame(JSGlobalContextRef context)
368 LogDebug("Unload a frame");
370 PLUGIN_LOGIC_SANITY_CHECK
372 auto sessionIt = m_sessions.find(context);
373 if (sessionIt == m_sessions.end())
375 LogWarning("Session not exist!");
379 sessionIt->second->unloadFrame(context);
380 m_sessions.erase(sessionIt);
383 void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
385 const char* encodedBundle,
388 LogInfo("set properties of window object " << scaleFactor << ", "
389 << encodedBundle << ", " << theme);
391 PLUGIN_LOGIC_SANITY_CHECK
393 auto sessionIt = m_sessions.find(context);
394 if (sessionIt == m_sessions.end())
396 LogWarning("Session not exist!");
400 sessionIt->second->setCustomProperties(scaleFactor,
406 void PluginLogic::Impl::dispatchJavaScriptEvent(JSGlobalContextRef context,
407 CustomEventType eventType,
410 LogDebug("Dispatch event");
412 PLUGIN_LOGIC_SANITY_CHECK
414 auto sessionIt = m_sessions.find(context);
415 if (sessionIt == m_sessions.end())
417 LogWarning("Session not exist!");
421 sessionIt->second->dispatchJavaScriptEvent(eventType, data);