merge with master
[platform/framework/web/wrt-plugins-common.git] / src / plugin-loading / js_page_session.cpp
index 1534d24..91f8cec 100644 (file)
@@ -141,26 +141,26 @@ class JSPageSession::Impl
                              const char* theme);
 
     void dispatchJavaScriptEvent(CustomEventType eventType, void* data);
-
 };
 
-
 JSPageSession::Impl::Impl(const PluginContainerSupportPtr& support) :
-    m_sessionStarted(false)
+        m_widgetHandle(0),
+        m_context(NULL),
+        m_sessionStarted(false),
+        m_objectExplorer(NULL)
 {
-//    DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
+    //    DPL::Log::LogSystemSingleton::Instance().SetTag("WRT_PLUGINS");
     LogDebug("Initializing Page Session");
     m_pluginsSupport = support;
 
     // explicit call to keep singleton's lifetime until calling destructor.
-//    JsFunctionManagerSingleton::Instance();
-//    JavaScriptInterfaceSingleton::Instance();
+    //    JsFunctionManagerSingleton::Instance();
+    //    JavaScriptInterfaceSingleton::Instance();
 }
 
 JSPageSession::Impl::~Impl()
 {
-    if(m_sessionStarted)
-    {
+    if (m_sessionStarted) {
         LogError("Must stop widget session before exit!");
         stopSession();
     }
@@ -207,9 +207,7 @@ bool JSPageSession::Impl::installPlugin(PluginModelPtr plugin)
 
     LogInfo("Install Plugin '" << library->GetFileName());
 
-
-    if(!library)
-    {
+    if (!library) {
         LogError("Loading library failed");
         return false;
     }
@@ -231,7 +229,7 @@ void JSPageSession::Impl::installRequestedFeatures()
     LogInfo("Installing requested widget features...");
 
     std::list<std::string> allowedFeatures =
-            m_pluginsSupport->getAllowedFeatures(m_widgetHandle);
+        m_pluginsSupport->getAllowedFeatures(m_widgetHandle);
 
     PluginContainerSupport::PluginsList allowedPlugins;
 
@@ -242,14 +240,14 @@ void JSPageSession::Impl::installRequestedFeatures()
         auto plugin = m_pluginsSupport->getPluginForFeature(*feature);
         ImplementedObjectsList implObjs =
             PluginDAOReadOnly::
-            getImplementedObjectsForPluginHandle(plugin->Handle.Get());
+                getImplementedObjectsForPluginHandle(plugin->Handle.Get());
 
         FOREACH(obj, implObjs)
         {
             LogDebug("Processing object: " << *obj);
             /* This can be optimalized, but would need extra data in database.
-               There should be a list of features that are allowed to be
-               installed at widget start */
+             * There should be a list of features that are allowed to be
+             * installed at widget start */
             if (obj->find(".") == obj->rfind(".")) {
                 allowedPlugins.push_back(plugin);
                 LogWarning("Plugin will be added: "
@@ -283,50 +281,49 @@ bool JSPageSession::Impl::loadPluginOnDemand(
         LogError("Session not started");
         return false;
     }
-//    //TODO here may be a bug. if plugin contains feature rejected and accepted
-//    LogInfo("Installing feature : " << widgetFeature.name);
-//    if (widgetFeature.rejected) {
-//        LogWarning("This api-feature was rejected");
-//        return;
-//    }
-//
-//    auto plugin = m_pluginsSupport->getPluginModelById(pluginHandle);
-//    if (!plugin) {
-//        LogError("Failed to load plugin. plugin handle: " << pluginHandle);
-//        return false;
-//    }
+    //    //TODO here may be a bug. if plugin contains feature rejected and
+    // accepted
+    //    LogInfo("Installing feature : " << widgetFeature.name);
+    //    if (widgetFeature.rejected) {
+    //        LogWarning("This api-feature was rejected");
+    //        return;
+    //    }
+    //
+    //    auto plugin = m_pluginsSupport->getPluginModelById(pluginHandle);
+    //    if (!plugin) {
+    //        LogError("Failed to load plugin. plugin handle: " <<
+    // pluginHandle);
+    //        return false;
+    //    }
     m_pluginsSupport->registerPluginModel(pluginHandle);
     return installPluginOnDemand(
-        m_pluginsSupport->getPluginModelById(pluginHandle),
-        parentObject,
-        context);
+               m_pluginsSupport->getPluginModelById(pluginHandle),
+               parentObject,
+               context);
 }
 
 bool JSPageSession::Impl::installPluginOnDemand(PluginModelPtr plugin,
-                                              JavaScriptObject& parentObject,
-                                              JSGlobalContextRef context)
+                                                JavaScriptObject& parentObject,
+                                                JSGlobalContextRef context)
 {
     Assert(plugin && "Plugin Model is NULL");
     auto library = loadLibrary(plugin);
 
     LogInfo("Install Plugin '" << library->GetFileName());
 
-    if(!library)
-    {
+    if (!library) {
         LogError("Loading library failed");
         return false;
     }
 
-    if(!(parentObject.instance))
-    {
+    if (!(parentObject.instance)) {
         LogError("NULL pointer value");
         return false;
     }
 
     JSObjectPtr parent(new JSObject(parentObject.instance));
 
-    if (!parent->getObject())
-    {
+    if (!parent->getObject()) {
         LogError("NULL pointer value");
         assert(false);
         return false;
@@ -340,10 +337,11 @@ bool JSPageSession::Impl::installPluginOnDemand(PluginModelPtr plugin,
                                              parent,
                                              context);
 
-        if(!installationStatus)
-        {
-            LogError("Object Registration failed : " << (*it)->getName()
-                     << "; Parent object name: " << parentObject.name);
+        if (!installationStatus) {
+            LogError(
+                "Object Registration failed : " << (*it)->getName()
+                                                <<
+                "; Parent object name: " << parentObject.name);
             return false;
         }
     }
@@ -353,11 +351,13 @@ bool JSPageSession::Impl::installPluginOnDemand(PluginModelPtr plugin,
 }
 
 void JSPageSession::Impl::setCustomProperties(double scaleFactor,
-                                            const char* encodedBundle,
-                                            const char* theme)
+                                              const char* encodedBundle,
+                                              const char* theme)
 {
-    LogInfo("set properties of window object " << scaleFactor << ", "
-            << encodedBundle << ", " << theme);
+    LogInfo(
+        "set properties of window object " << scaleFactor << ", "
+                                           << encodedBundle << ", " <<
+        theme);
 
     m_objectExplorer->getWindowPropertySupport()
         ->setScaleToNavigatorProperty(scaleFactor);
@@ -367,7 +367,8 @@ void JSPageSession::Impl::setCustomProperties(double scaleFactor,
         ->setThemeToNavigatorProperty(theme);
 }
 
-void JSPageSession::Impl::dispatchJavaScriptEvent(CustomEventType eventType, void* data)
+void JSPageSession::Impl::dispatchJavaScriptEvent(CustomEventType eventType,
+                                                  void* data)
 {
     // Check if session is already started
     if (!m_sessionStarted) {
@@ -383,37 +384,40 @@ void JSPageSession::Impl::loadInjectedJavaScript()
 {
     LogInfo("Entered");
 
-    std::string DIR_PATH     = "/usr/etc/wrt/injected-javascript";
+    std::string DIR_PATH = "/usr/etc/wrt/injected-javascript";
     std::string JS_EXTENSION = ".js";
 
     DIR *dir = opendir(DIR_PATH.c_str());
 
     if (!dir) {
-        LogError("opendir(\"" << DIR_PATH << "\") error!" );
+        LogError("opendir(\"" << DIR_PATH << "\") error!");
         return;
     }
 
-    struct dirent* libdir;
+    int return_code;
+    struct dirent libdir;
+    struct dirent* result;
     std::list<std::string> jsFiles;
 
     // make file list from DIR_PATH
-    while ((libdir = readdir(dir)) != 0)
-    {
-        if (strncmp(libdir->d_name, ".", 2) == 0 ||
-            strncmp(libdir->d_name, "..", 3) == 0)
+    for (return_code = readdir_r(dir, &libdir, &result);
+            result != NULL && return_code == 0;
+            return_code = readdir_r(dir, &libdir, &result)) {
+        if (strncmp(libdir.d_name, ".", 2) == 0 ||
+            strncmp(libdir.d_name, "..", 3) == 0)
         {
             continue;
         }
 
         std::string filepath = DIR_PATH;
         filepath += "/";
-        filepath += libdir->d_name;
+        filepath += libdir.d_name;
 
         std::string lowercase = filepath;
         std::transform(lowercase.begin(), lowercase.end(), lowercase.begin(),
                        towlower);
 
-        iflowercase.rfind(JS_EXTENSION) == std::string::npos ||
+        if (lowercase.rfind(JS_EXTENSION) == std::string::npos ||
             lowercase.length() !=
             lowercase.rfind(JS_EXTENSION) + JS_EXTENSION.length() )
         {
@@ -436,10 +440,11 @@ void JSPageSession::Impl::loadInjectedJavaScript()
         LogInfo("Added : " << filepath);
         jsFiles.push_back(filepath);
     }
+    if (0 != return_code)
+        LogError("Error while reading directory.");
 
     closedir(dir);
 
-
     FOREACH(it, jsFiles)
     {
         LogDebug("load file : " << (*it));
@@ -447,8 +452,7 @@ void JSPageSession::Impl::loadInjectedJavaScript()
         std::string content;
         std::ifstream fin(it->c_str());
 
-        while(fin.good())
-        {
+        while (fin.good()) {
             string line;
             std::getline(fin, line);
             content += line + "\n";
@@ -456,8 +460,7 @@ void JSPageSession::Impl::loadInjectedJavaScript()
 
         fin.close();
         // execute
-        if(!content.empty())
-        {
+        if (!content.empty()) {
             JSValueRef exception = NULL;
             JSStringRef script =
                 JSStringCreateWithUTF8CString(content.c_str());
@@ -466,10 +469,9 @@ void JSPageSession::Impl::loadInjectedJavaScript()
 
             JSStringRelease(script);
 
-            if(exception)
-            {
+            if (exception) {
                 LogDebug("Exception Occured while injecting javascript "
-                    "file. : " << *it);
+                         "file. : " << *it);
 
                 JSStringRef exceptionJSString =
                     JSValueToStringCopy(m_context, exception, NULL);
@@ -480,18 +482,18 @@ void JSPageSession::Impl::loadInjectedJavaScript()
                                        exceptionString, size);
                 LogDebug("Exception : " << exceptionString);
 
-                delete [] exceptionString;
+                delete[] exceptionString;
                 JSStringRelease(exceptionJSString);
-             }
+            }
         }
     }
 }
 
 void JSPageSession::Impl::startSession(int widgetHandle,
-                                     JSGlobalContextRef context,
-                                     double scaleFactor,
-                                     const char* encodedBundle,
-                                     const char* theme)
+                                       JSGlobalContextRef context,
+                                       double scaleFactor,
+                                       const char* encodedBundle,
+                                       const char* theme)
 {
     LogInfo("Starting widget session...");
 
@@ -543,7 +545,6 @@ void JSPageSession::Impl::stopSession()
     LogInfo("Widget session stopped.");
 }
 
-
 void JSPageSession::Impl::unloadPluginsFromSession()
 {
     LogDebug("Unload plugins from session");
@@ -580,9 +581,6 @@ void JSPageSession::Impl::unloadPluginsFromSession()
     m_objectExplorer = NULL;
 }
 
-
-
-
 void JSPageSession::Impl::performLibrariesUnload()
 {
 #if 0
@@ -610,19 +608,16 @@ void JSPageSession::Impl::performLibrariesUnload()
 PluginPtr JSPageSession::Impl::loadLibrary(PluginModelPtr& pluginModel)
 {
     PluginPtr pluginLib = pluginModel->LibraryInstance.Get();
-    if (!pluginLib)
-    {
+    if (!pluginLib) {
         std::string path = pluginModel->LibraryPath.Get() +
-                           std::string(LIBRARY_PATH_SEPARATOR) +
-                           pluginModel->LibraryName.Get();
+            std::string(LIBRARY_PATH_SEPARATOR) +
+            pluginModel->LibraryName.Get();
 
         pluginLib = Plugin::LoadFromFile(path);
 
-        if (!pluginLib)
-        {
+        if (!pluginLib) {
             LogError("Loading library failed");
-        } else
-        {
+        } else {
             pluginModel->LibraryInstance.Set(pluginLib);
 
             LogDebug("On widget start");
@@ -636,9 +631,7 @@ PluginPtr JSPageSession::Impl::loadLibrary(PluginModelPtr& pluginModel)
                 pluginLib->OnFrameLoad(*context);
             }
         }
-    }
-    else
-    {
+    } else {
         LogDebug("Get from LibraryInstance");
         LogDebug("On widget start");
         // This is first time for this plugin, start widget Session
@@ -655,7 +648,6 @@ PluginPtr JSPageSession::Impl::loadLibrary(PluginModelPtr& pluginModel)
     return pluginLib;
 }
 
-
 void JSPageSession::Impl::loadFrame(JSGlobalContextRef context)
 {
     LogDebug("Load a frame");
@@ -698,12 +690,11 @@ void JSPageSession::Impl::unloadFrame(JSGlobalContextRef context)
     m_objectExplorer->unloadFrame(context);
 }
 
-
 void JSPageSession::startSession(int widgetHandle,
-                               JSGlobalContextRef ctx,
-                               double scaleFactor,
-                               const char* encodedBundle,
-                               const char* theme)
+                                 JSGlobalContextRef ctx,
+                                 double scaleFactor,
+                                 const char* encodedBundle,
+                                 const char* theme)
 {
     m_impl->startSession(widgetHandle, ctx, scaleFactor, encodedBundle, theme);
 }
@@ -727,13 +718,14 @@ bool JSPageSession::loadPluginOnDemand(
 }
 
 void JSPageSession::setCustomProperties(double scaleFactor,
-                                      const char* encodedBundle,
-                                      const char* theme)
+                                        const char* encodedBundle,
+                                        const char* theme)
 {
     m_impl->setCustomProperties(scaleFactor, encodedBundle, theme);
 }
 
-void JSPageSession::dispatchJavaScriptEvent(CustomEventType eventType, void* data)
+void JSPageSession::dispatchJavaScriptEvent(CustomEventType eventType,
+                                            void* data)
 {
     m_impl->dispatchJavaScriptEvent(eventType, data);
 }
@@ -749,10 +741,9 @@ void JSPageSession::unloadFrame(JSGlobalContextRef context)
 }
 
 JSPageSession::JSPageSession(const PluginContainerSupportPtr& containerSupport)
-    : m_impl(new JSPageSession::Impl(containerSupport))
-{
-}
+    :
+    m_impl(new JSPageSession::Impl(containerSupport))
+{}
 
 JSPageSession::~JSPageSession()
-{
-}
+{}