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;
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 static bool s_sanityCheck;
115 IMPLEMENT_SINGLETON(PluginLogic);
117 bool PluginLogic::Impl::s_sanityCheck = false;
119 #define PLUGIN_LOGIC_SANITY_CHECK \
120 if (!s_sanityCheck) \
122 LogError("Object is not available. Wrong flow occured"); \
126 PluginLogic::Impl::Impl()
128 s_sanityCheck = true;
130 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
131 LogDebug("Initializing Plugin Logic...");
132 m_pluginsSupport = PluginContainerSupportPtr(new PluginContainerSupport());
134 // explicit call to keep singleton's lifetime until calling destructor.
135 JsFunctionManagerSingleton::Instance();
136 JavaScriptInterfaceSingleton::Instance();
139 PluginLogic::Impl::~Impl()
143 s_sanityCheck = false;
145 FOREACH(it, m_sessions)
147 LogError("Must stop widget session before exit!");
148 it->second->stopSession();
151 LogDebug("Deinitializing plugin Logic...");
154 void PluginLogic::initSession(int widgetHandle)
156 m_impl->initSession(widgetHandle);
159 void PluginLogic::startSession(int widgetHandle,
160 JSGlobalContextRef ctx,
162 const char* encodedBundle,
165 m_impl->startSession(widgetHandle, ctx, scaleFactor, encodedBundle, theme);
168 void PluginLogic::stopSession(JSGlobalContextRef context)
170 m_impl->stopSession(context);
173 void PluginLogic::performLibrariesUnload()
175 LogError("Libraries unload TURNED OFF");
176 // m_impl->performLibrariesUnload();
179 bool PluginLogic::loadPluginOnDemand(
180 const WrtDB::DbPluginHandle &pluginHandle,
181 JavaScriptObject& parentObject,
182 JSGlobalContextRef context)
184 return m_impl->loadPluginOnDemand(pluginHandle, parentObject, context);
187 void PluginLogic::loadPluginsIntoIframes(JSGlobalContextRef context)
189 LogError("This function is Deprecated");
192 void PluginLogic::setCustomProperties(double scaleFactor,
193 const char* encodedBundle,
196 LogError("This function is DEPRECATED");
199 void PluginLogic::setCustomProperties(JSGlobalContextRef context,
201 const char* encodedBundle,
204 m_impl->setCustomProperties(context, scaleFactor, encodedBundle, theme);
207 void PluginLogic::dispatchJavaScriptEvent(CustomEventType eventType)
209 LogError("This function is DEPRECATED");
212 void PluginLogic::dispatchJavaScriptEvent(JSGlobalContextRef context,
213 CustomEventType eventType,
216 m_impl->dispatchJavaScriptEvent(context, eventType, data);
219 void PluginLogic::loadFrame(JSGlobalContextRef context)
221 m_impl->loadFrame(context);
224 void PluginLogic::unloadFrame(JSGlobalContextRef context)
226 m_impl->unloadFrame(context);
229 PluginLogic::PluginLogic() : m_impl(new PluginLogic::Impl())
232 PluginLogic::~PluginLogic()
235 void PluginLogic::Impl::initSession(int widgetHandle)
237 LogInfo("init pluginLogic...");
239 m_pluginsSupport->Initialize(widgetHandle);
241 //add standard objects
242 LogDebug("Preload plugins so file");
244 PluginContainerSupport::PluginsList pluginList =
245 m_pluginsSupport->getPluginsList();
247 FOREACH(it, pluginList)
249 PluginModelPtr& pluginModel = *it;
251 if (!pluginModel->LibraryDependencies.Get()->empty()) {
256 PluginPtr pluginLib = pluginModel->LibraryInstance.Get();
259 std::string path = pluginModel->LibraryPath.Get() +
260 std::string(LIBRARY_PATH_SEPARATOR) +
261 pluginModel->LibraryName.Get();
263 pluginLib = Plugin::LoadFromFile(path);
266 LogError("Loading library failed");
268 pluginModel->LibraryInstance.Set(pluginLib);
271 "pluginModel->LibraryInstance.Set() : " <<
272 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()) {
291 m_pluginsSupport->Initialize(widgetHandle);
293 auto sessionIt = m_sessions.find(context);
295 // Check if corresponding session if not already created
296 if (sessionIt != m_sessions.end()) {
297 LogWarning("Session already started!");
301 auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
302 newSession->startSession(widgetHandle,
308 m_sessions[context] = newSession;
311 void PluginLogic::Impl::stopSession(JSGlobalContextRef context)
313 LogInfo("Stopping widget session...");
315 auto sessionIt = m_sessions.find(context);
316 if (sessionIt == m_sessions.end()) {
317 LogError("Session not exist!");
321 sessionIt->second->stopSession();
322 m_sessions.erase(sessionIt);
324 LogInfo("Widget session stopped.");
327 bool PluginLogic::Impl::loadPluginOnDemand(
328 const WrtDB::DbPluginHandle &pluginHandle,
329 JavaScriptObject& parentObject,
330 JSGlobalContextRef context
333 LogInfo("Load plugin on demand");
335 auto sessionIt = m_sessions.find(context);
336 if (sessionIt == m_sessions.end()) {
337 LogWarning("Session not exist!");
341 return sessionIt->second->loadPluginOnDemand(pluginHandle,
346 void PluginLogic::Impl::loadFrame(JSGlobalContextRef context)
348 LogDebug("Load a frame");
350 PLUGIN_LOGIC_SANITY_CHECK
352 auto sessionIt = m_sessions.find(context);
353 if (sessionIt == m_sessions.end()) {
354 LogWarning("Session not exist!");
358 sessionIt->second->loadFrame(context);
361 void PluginLogic::Impl::unloadFrame(JSGlobalContextRef context)
363 LogDebug("Unload a frame");
365 PLUGIN_LOGIC_SANITY_CHECK
367 auto sessionIt = m_sessions.find(context);
368 if (sessionIt == m_sessions.end()) {
369 LogWarning("Session not exist!");
373 sessionIt->second->unloadFrame(context);
374 m_sessions.erase(sessionIt);
377 void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
379 const char* encodedBundle,
383 "set properties of window object " << scaleFactor << ", "
384 << encodedBundle << ", " <<
387 PLUGIN_LOGIC_SANITY_CHECK
389 auto sessionIt = m_sessions.find(context);
390 if (sessionIt == m_sessions.end()) {
391 LogWarning("Session not exist!");
395 sessionIt->second->setCustomProperties(scaleFactor,
400 void PluginLogic::Impl::dispatchJavaScriptEvent(JSGlobalContextRef context,
401 CustomEventType eventType,
404 LogDebug("Dispatch event");
406 PLUGIN_LOGIC_SANITY_CHECK
408 auto sessionIt = m_sessions.find(context);
409 if (sessionIt == m_sessions.end()) {
410 LogWarning("Session not exist!");
414 sessionIt->second->dispatchJavaScriptEvent(eventType, data);