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);
113 IMPLEMENT_SINGLETON(PluginLogic);
115 PluginLogic::Impl::Impl()
117 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
118 LogDebug("Initializing Plugin Logic...");
119 m_pluginsSupport = PluginContainerSupportPtr(new PluginContainerSupport());
121 // explicit call to keep singleton's lifetime until calling destructor.
122 JsFunctionManagerSingleton::Instance();
123 JavaScriptInterfaceSingleton::Instance();
126 PluginLogic::Impl::~Impl()
128 FOREACH(it, m_sessions)
130 LogError("Must stop widget session before exit!");
131 it->second->stopSession();
134 LogDebug("Deinitializing plugin Logic...");
138 void PluginLogic::startSession(int widgetHandle,
139 JSGlobalContextRef ctx,
141 const char* encodedBundle,
144 m_impl->startSession(widgetHandle, ctx, scaleFactor, encodedBundle, theme);
147 void PluginLogic::stopSession(JSGlobalContextRef context)
149 m_impl->stopSession(context);
152 void PluginLogic::performLibrariesUnload()
154 LogError("Libraries unload TURNED OFF");
155 // m_impl->performLibrariesUnload();
158 bool PluginLogic::loadPluginOnDemand(
159 const WrtDB::DbPluginHandle &pluginHandle,
160 JavaScriptObject& parentObject,
161 JSGlobalContextRef context)
163 return m_impl->loadPluginOnDemand(pluginHandle, parentObject, context);
166 void PluginLogic::loadPluginsIntoIframes(JSGlobalContextRef context)
168 LogError("This function is Deprecated");
171 void PluginLogic::setCustomProperties(double scaleFactor,
172 const char* encodedBundle,
175 LogError("This function is DEPRECATED");
178 void PluginLogic::setCustomProperties(JSGlobalContextRef context,
180 const char* encodedBundle,
183 m_impl->setCustomProperties(context, scaleFactor, encodedBundle, theme);
186 void PluginLogic::dispatchJavaScriptEvent(CustomEventType eventType)
188 LogError("This function is DEPRECATED");
191 void PluginLogic::dispatchJavaScriptEvent(JSGlobalContextRef context,
192 CustomEventType eventType)
194 m_impl->dispatchJavaScriptEvent(context, eventType);
197 void PluginLogic::loadFrame(JSGlobalContextRef context)
199 m_impl->loadFrame(context);
202 void PluginLogic::unloadFrame(JSGlobalContextRef context)
204 m_impl->unloadFrame(context);
207 PluginLogic::PluginLogic() : m_impl(new PluginLogic::Impl())
211 PluginLogic::~PluginLogic()
215 void PluginLogic::Impl::startSession(int widgetHandle,
216 JSGlobalContextRef context,
218 const char* encodedBundle,
221 LogInfo("Starting widget session...");
223 if (!m_pluginsSupport->isInitialized())
224 m_pluginsSupport->readAllowedPlugins(widgetHandle);
226 auto sessionIt = m_sessions.find(context);
228 // Check if corresponding session if not already created
229 if (sessionIt != m_sessions.end())
231 LogWarning("Session already started!");
235 auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
236 newSession->startSession(widgetHandle,
242 m_sessions[context] = newSession;
245 void PluginLogic::Impl::stopSession(JSGlobalContextRef context)
247 LogInfo("Stopping widget session...");
249 auto sessionIt = m_sessions.find(context);
250 if (sessionIt == m_sessions.end())
252 LogError("Session not exist!");
256 sessionIt->second->stopSession();
257 m_sessions.erase(sessionIt);
259 LogInfo("Widget session stopped.");
262 bool PluginLogic::Impl::loadPluginOnDemand(
263 const WrtDB::DbPluginHandle &pluginHandle,
264 JavaScriptObject& parentObject,
265 JSGlobalContextRef context
268 LogInfo("Load plugin on demand");
270 auto sessionIt = m_sessions.find(context);
271 if (sessionIt == m_sessions.end())
273 LogWarning("Session not exist!");
277 return sessionIt->second->loadPluginOnDemand(pluginHandle,
282 void PluginLogic::Impl::loadFrame(JSGlobalContextRef context)
284 LogDebug("Load a frame");
285 auto sessionIt = m_sessions.find(context);
286 if (sessionIt == m_sessions.end())
288 LogWarning("Session not exist!");
292 sessionIt->second->loadFrame(context);
295 void PluginLogic::Impl::unloadFrame(JSGlobalContextRef context)
297 LogDebug("Unload a frame");
299 auto sessionIt = m_sessions.find(context);
300 if (sessionIt == m_sessions.end())
302 LogWarning("Session not exist!");
306 sessionIt->second->unloadFrame(context);
309 void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
311 const char* encodedBundle,
314 LogInfo("set properties of window object " << scaleFactor << ", "
315 << encodedBundle << ", " << theme);
317 auto sessionIt = m_sessions.find(context);
318 if (sessionIt == m_sessions.end())
320 LogWarning("Session not exist!");
324 sessionIt->second->setCustomProperties(scaleFactor,
330 void PluginLogic::Impl::dispatchJavaScriptEvent(JSGlobalContextRef context,
331 CustomEventType eventType)
333 LogDebug("Dispatch event");
335 auto sessionIt = m_sessions.find(context);
336 if (sessionIt == m_sessions.end())
338 LogWarning("Session not exist!");
342 sessionIt->second->dispatchJavaScriptEvent(eventType);