Replace LogProvider to Dlog
[platform/framework/web/wrt-plugins-common.git] / src / plugin-loading / plugin_logic.cpp
index e97b593..d3df0da 100644 (file)
@@ -28,7 +28,7 @@
 
 #include <dpl/assert.h>
 #include <dpl/scoped_array.h>
-#include <dpl/log/log.h>
+#include <dpl/log/secure_log.h>
 #include <dpl/foreach.h>
 #include <dpl/singleton_impl.h>
 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
@@ -67,7 +67,6 @@ using namespace WrtPlugins::W3C;
 
 namespace {
 const char *LIBRARY_PATH_SEPARATOR = "/";
-const char* FEATURE_WAC20_DEVICAPIS_NAME = "http://wacapps.net/api/deviceapis";
 }
 
 class PluginLogic::Impl
@@ -76,13 +75,14 @@ class PluginLogic::Impl
 
     typedef std::map<JSContextRef, JSPageSessionPtr> PagesSessionsSet;
     PagesSessionsSet m_sessions;
-
+    unsigned int m_windowHandle;
 
   public:
     Impl();
     ~Impl();
 
     // Widget session
+    void initSession(int widgetHandle);
     void startSession(int widgetHandle,
                       JSGlobalContextRef context,
                       double scaleFactor,
@@ -106,16 +106,33 @@ class PluginLogic::Impl
                              const char* theme);
 
     void dispatchJavaScriptEvent(JSGlobalContextRef ctx,
-                                 CustomEventType eventType);
+                                 CustomEventType eventType,
+                                 void* data);
+
+    unsigned int windowHandle() const;
+    void setWindowHandle(unsigned int handle);
 
+    static bool s_sanityCheck;
 };
 
 IMPLEMENT_SINGLETON(PluginLogic);
 
-PluginLogic::Impl::Impl()
+bool PluginLogic::Impl::s_sanityCheck = false;
+
+#define PLUGIN_LOGIC_SANITY_CHECK \
+    if (!s_sanityCheck) \
+    { \
+        _E("Object is not available. Wrong flow occured"); \
+        return; \
+    }
+
+PluginLogic::Impl::Impl() :
+    m_windowHandle(0)
 {
+    s_sanityCheck = true;
+
     DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
-    LogDebug("Initializing Plugin Logic...");
+    _D("Initializing Plugin Logic...");
     m_pluginsSupport = PluginContainerSupportPtr(new PluginContainerSupport());
 
     // explicit call to keep singleton's lifetime until calling destructor.
@@ -125,15 +142,21 @@ PluginLogic::Impl::Impl()
 
 PluginLogic::Impl::~Impl()
 {
+    _D("called");
+
+    s_sanityCheck = false;
+
     FOREACH(it, m_sessions)
     {
-        LogError("Must stop widget session before exit!");
+        _W("Must stop widget session before exit!");
         it->second->stopSession();
     }
-
-    LogDebug("Deinitializing plugin Logic...");
 }
 
+void PluginLogic::initSession(int widgetHandle)
+{
+    m_impl->initSession(widgetHandle);
+}
 
 void PluginLogic::startSession(int widgetHandle,
                                JSGlobalContextRef ctx,
@@ -151,8 +174,8 @@ void PluginLogic::stopSession(JSGlobalContextRef context)
 
 void PluginLogic::performLibrariesUnload()
 {
-    LogError("Libraries unload TURNED OFF");
-//    m_impl->performLibrariesUnload();
+    _W("This function is DEPRECATED");
+    //    m_impl->performLibrariesUnload();
 }
 
 bool PluginLogic::loadPluginOnDemand(
@@ -163,16 +186,16 @@ bool PluginLogic::loadPluginOnDemand(
     return m_impl->loadPluginOnDemand(pluginHandle, parentObject, context);
 }
 
-void PluginLogic::loadPluginsIntoIframes(JSGlobalContextRef context)
+void PluginLogic::loadPluginsIntoIframes(JSGlobalContextRef /*context*/)
 {
-    LogError("This function is Deprecated");
+    _W("This function is DEPRECATED");
 }
 
-void PluginLogic::setCustomProperties(double scaleFactor,
-                                      const char* encodedBundle,
-                                      const char* theme)
+void PluginLogic::setCustomProperties(double /*scaleFactor*/,
+                                      const char* /*encodedBundle*/,
+                                      const char* /*theme*/)
 {
-    LogError("This function is DEPRECATED");
+    _W("This function is DEPRECATED");
 }
 
 void PluginLogic::setCustomProperties(JSGlobalContextRef context,
@@ -183,15 +206,16 @@ void PluginLogic::setCustomProperties(JSGlobalContextRef context,
     m_impl->setCustomProperties(context, scaleFactor, encodedBundle, theme);
 }
 
-void PluginLogic::dispatchJavaScriptEvent(CustomEventType eventType)
+void PluginLogic::dispatchJavaScriptEvent(CustomEventType /*eventType*/)
 {
-    LogError("This function is DEPRECATED");
+    _W("This function is DEPRECATED");
 }
 
 void PluginLogic::dispatchJavaScriptEvent(JSGlobalContextRef context,
-                                          CustomEventType eventType)
+                                          CustomEventType eventType,
+                                          void *data)
 {
-    m_impl->dispatchJavaScriptEvent(context, eventType);
+    m_impl->dispatchJavaScriptEvent(context, eventType, data);
 }
 
 void PluginLogic::loadFrame(JSGlobalContextRef context)
@@ -204,12 +228,54 @@ void PluginLogic::unloadFrame(JSGlobalContextRef context)
     m_impl->unloadFrame(context);
 }
 
-PluginLogic::PluginLogic() : m_impl(new PluginLogic::Impl())
+unsigned int PluginLogic::windowHandle() const
 {
+    return m_impl->windowHandle();
 }
 
+void PluginLogic::setWindowHandle(unsigned int handle)
+{
+    m_impl->setWindowHandle(handle);
+}
+
+PluginLogic::PluginLogic() : m_impl(new PluginLogic::Impl())
+{}
+
 PluginLogic::~PluginLogic()
+{}
+
+void PluginLogic::Impl::initSession(int widgetHandle)
 {
+    _D(">---------------------[init session START]---------------------<");
+
+    m_pluginsSupport->Initialize(widgetHandle);
+    PluginContainerSupport::PluginsList rootPluginList =
+        m_pluginsSupport->getRootPlugins();
+
+    FOREACH(it, rootPluginList)
+    {
+        PluginModelPtr& pluginModel = *it;
+        PluginPtr pluginLib = pluginModel->LibraryInstance.Get();
+
+        if (!pluginLib) {
+            std::string path = pluginModel->LibraryPath.Get() +
+                std::string(LIBRARY_PATH_SEPARATOR) +
+                pluginModel->LibraryName.Get();
+
+            pluginLib = Plugin::LoadFromFile(path);
+
+            if (!pluginLib) {
+                _W("Loading library failed");
+            } else {
+                pluginModel->LibraryInstance.Set(pluginLib);
+                _D("pluginModel->LibraryInstance.Set() : %s",
+                   pluginLib->GetFileName().c_str());
+            }
+        } else {
+            _D("Already loaded");
+        }
+    }
+    _D("========== init session END ==========");
 }
 
 void PluginLogic::Impl::startSession(int widgetHandle,
@@ -218,45 +284,41 @@ void PluginLogic::Impl::startSession(int widgetHandle,
                                      const char* encodedBundle,
                                      const char* theme)
 {
-    LogInfo("Starting widget session...");
-
-    if (!m_pluginsSupport->isInitialized())
-        m_pluginsSupport->readAllowedPlugins(widgetHandle);
+    _D("========== start session START ==========");
 
+    if (!m_pluginsSupport->isInitialized()) {
+        m_pluginsSupport->Initialize(widgetHandle);
+    }
     auto sessionIt = m_sessions.find(context);
 
     // Check if corresponding session if not already created
-    if (sessionIt != m_sessions.end())
-    {
-        LogWarning("Session already started!");
-        return;
+    if (sessionIt != m_sessions.end()) {
+        _W("Session already started!");
+    } else {
+        auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
+        newSession->startSession(widgetHandle,
+                                 context,
+                                 scaleFactor,
+                                 encodedBundle,
+                                 theme);
+
+        m_sessions[context] = newSession;
     }
-
-    auto newSession = JSPageSessionPtr(new JSPageSession(m_pluginsSupport));
-    newSession->startSession(widgetHandle,
-                             context,
-                             scaleFactor,
-                             encodedBundle,
-                             theme);
-
-    m_sessions[context] = newSession;
+    _D("========== start session END ==========");
 }
 
 void PluginLogic::Impl::stopSession(JSGlobalContextRef context)
 {
-    LogInfo("Stopping widget session...");
+    _D("========== stop session START ==========");
 
     auto sessionIt = m_sessions.find(context);
-    if (sessionIt == m_sessions.end())
-    {
-        LogError("Session not exist!");
-        return;
+    if (sessionIt == m_sessions.end()) {
+        _W("Session not exist!");
+    } else {
+        sessionIt->second->stopSession();
+        m_sessions.erase(sessionIt);
     }
-
-    sessionIt->second->stopSession();
-    m_sessions.erase(sessionIt);
-
-    LogInfo("Widget session stopped.");
+    _D("========== stop session END ==========");
 }
 
 bool PluginLogic::Impl::loadPluginOnDemand(
@@ -265,12 +327,11 @@ bool PluginLogic::Impl::loadPluginOnDemand(
     JSGlobalContextRef context
     )
 {
-    LogInfo("Load plugin on demand");
+    _D("========== load ondemand plugin ==========");
 
     auto sessionIt = m_sessions.find(context);
-    if (sessionIt == m_sessions.end())
-    {
-        LogWarning("Session not exist!");
+    if (sessionIt == m_sessions.end()) {
+        _W("Session not exist!");
         return false;
     }
 
@@ -281,29 +342,34 @@ bool PluginLogic::Impl::loadPluginOnDemand(
 
 void PluginLogic::Impl::loadFrame(JSGlobalContextRef context)
 {
-    LogDebug("Load a frame");
+    _D("========== load frame START ==========");
+    PLUGIN_LOGIC_SANITY_CHECK
+
     auto sessionIt = m_sessions.find(context);
-    if (sessionIt == m_sessions.end())
-    {
-        LogWarning("Session not exist!");
-        return;
+    if (sessionIt == m_sessions.end()) {
+        _W("Session not exist!");
+    } else {
+        sessionIt->second->loadFrame(context);
     }
-
-    sessionIt->second->loadFrame(context);
+    _D("========== load frame END ==========");
 }
 
 void PluginLogic::Impl::unloadFrame(JSGlobalContextRef context)
 {
-    LogDebug("Unload a frame");
+    _D("========== unload frame START ==========");
+    PLUGIN_LOGIC_SANITY_CHECK
 
     auto sessionIt = m_sessions.find(context);
-    if (sessionIt == m_sessions.end())
-    {
+    if (sessionIt == m_sessions.end()) {
         LogWarning("Session not exist!");
-        return;
-    }
-
-    sessionIt->second->unloadFrame(context);
+    } else {
+        sessionIt->second->unloadFrame(context);
+
+        // I don't know why this session should be removed here.
+        // session list is removed also from stopSession().
+        //m_sessions.erase(sessionIt);
+        }
+    _D("========== unload frame END ==========");
 }
 
 void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
@@ -311,35 +377,44 @@ void PluginLogic::Impl::setCustomProperties(JSGlobalContextRef context,
                                             const char* encodedBundle,
                                             const char* theme)
 {
-    LogInfo("set properties of window object " << scaleFactor << ", "
-            << encodedBundle << ", " << theme);
+    PLUGIN_LOGIC_SANITY_CHECK
 
     auto sessionIt = m_sessions.find(context);
-    if (sessionIt == m_sessions.end())
-    {
-        LogWarning("Session not exist!");
+    if (sessionIt == m_sessions.end()) {
+        _W("Session not exist!");
         return;
     }
 
     sessionIt->second->setCustomProperties(scaleFactor,
                                            encodedBundle,
                                            theme);
-
 }
 
 void PluginLogic::Impl::dispatchJavaScriptEvent(JSGlobalContextRef context,
-                                                CustomEventType eventType)
+                                                CustomEventType eventType,
+                                                void* data)
 {
-    LogDebug("Dispatch event");
+    PLUGIN_LOGIC_SANITY_CHECK
 
     auto sessionIt = m_sessions.find(context);
-    if (sessionIt == m_sessions.end())
-    {
-        LogWarning("Session not exist!");
+    if (sessionIt == m_sessions.end()) {
+        _W("Session not exist!");
         return;
     }
 
-    sessionIt->second->dispatchJavaScriptEvent(eventType);
+    sessionIt->second->dispatchJavaScriptEvent(eventType, data);
+}
 
+unsigned int PluginLogic::Impl::windowHandle() const
+{
+    if (!s_sanityCheck) {
+        LogError("Object is not available. Wrong flow occured");
+    }
+    return m_windowHandle;
+}
 
+void PluginLogic::Impl::setWindowHandle(unsigned int handle)
+{
+    PLUGIN_LOGIC_SANITY_CHECK
+    m_windowHandle = handle;
 }