Improve performance in getting root plugins list.
[platform/framework/web/wrt-plugins-common.git] / src / plugin-loading / plugin_container_support.cpp
index 6bc1d31..13dadd0 100644 (file)
 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
 #include <dpl/wrt-dao-ro/global_config.h>
 
-namespace {
-const char *STANDARD_FEATURES_LIST_FILE = "standard-features-list";
-const char *TIZEN_ROOT_FEATURES = "http://tizen.org/privilege/tizen";
-}
-
-using namespace WrtDB;
+ using namespace WrtDB;
 
 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
 
-PluginContainerSupport::PluginContainerSupport(): m_initialized(false)
+PluginContainerSupport::PluginContainerSupport() : m_initialized(false)
 {
-    // Reading standard features list from file
-    readStandardFeaturesList();
+     // Reading root plugins list from so files
+    readRootPluginsList();
 }
 
 PluginContainerSupport::~PluginContainerSupport()
@@ -49,12 +44,18 @@ PluginContainerSupport::~PluginContainerSupport()
 
     // Remove all feature models
     m_featureModels.clear();
+ }
 
-    // Clear standard features list
-    m_standardFeatureList.clear();
+void PluginContainerSupport::Initialize(int widgetHandle)
+{
+    if (isInitialized() == false) {
+        readAllowedPlugins(widgetHandle);
+        readRootPlugins(widgetHandle);
+    }
 }
 
-std::list<std::string> PluginContainerSupport::getAllowedFeatures(int widgetHandle) const
+std::list<std::string> PluginContainerSupport::getAllowedFeatures(
+    int widgetHandle) const
 {
     //TODO it has to return LIST NOT SET!!!
     WidgetDAOReadOnly widgetDao(widgetHandle);
@@ -65,7 +66,7 @@ std::list<std::string> PluginContainerSupport::getAllowedFeatures(int widgetHand
         LogInfo("Loading api-feature: " << it->name);
         if (it->rejected) {
             LogWarning("Api-feature was rejected by ace. (Api-feature name: "
-                       << it->name << ")" );
+                       << it->name << ")");
             continue;
         }
 
@@ -76,7 +77,7 @@ std::list<std::string> PluginContainerSupport::getAllowedFeatures(int widgetHand
 
 void PluginContainerSupport::readAllowedPlugins(int widgetHandle)
 {
-    std::list<std::string> allowedFeatures(m_standardFeatureList);
+    std::list<std::string> allowedFeatures;
     auto requested = getAllowedFeatures(widgetHandle);
     FOREACH(f, requested)
     {
@@ -84,8 +85,11 @@ void PluginContainerSupport::readAllowedPlugins(int widgetHandle)
     }
 
     FeatureData* dt = NULL;
-    std::map<FeatureHandle, FeatureData> featureDataList = FeatureDAOReadOnly::GetFeatures(allowedFeatures);
-    DeviceCapList deviceCapabilities = FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
+    std::map<FeatureHandle,
+             FeatureData> featureDataList = FeatureDAOReadOnly::GetFeatures(
+            allowedFeatures);
+    DeviceCapList deviceCapabilities =
+        FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
     FOREACH(data, featureDataList) {
         dt = &(data->second);
         registerPluginModel(dt->pluginHandle);
@@ -100,15 +104,22 @@ void PluginContainerSupport::readRootPlugins(int widgetHandle)
     WidgetDAOReadOnly dao(widgetHandle);
     WidgetType appType = dao.getWidgetType();
     if (appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
-        WrtDB::FeatureDAOReadOnly dao(TIZEN_ROOT_FEATURES);
-        registerPluginModel(dao.GetPluginHandle());
+         FOREACH(it_rootPluginHandle, m_rootPluginsList)
+        {
+            LogDebug("*it_rootPluginHandle: " << *it_rootPluginHandle);
+            registerPluginModel(*it_rootPluginHandle);
+        }
     } else {
         LogDebug("Not defined app type");
     }
     m_initialized = true;
 }
 
-void PluginContainerSupport::registerFeatureModel(FeatureHandle handle, FeatureData* data, DeviceCapList deviceCapabilities)
+void PluginContainerSupport::registerFeatureModel(
+    FeatureHandle handle,
+    FeatureData* data,
+    DeviceCapList
+    deviceCapabilities)
 {
     LogDebug("Analyzing feature: " << handle);
     FeatureModelPtr model = getFeatureModel(handle);
@@ -156,36 +167,11 @@ void PluginContainerSupport::registerPluginModel(DbPluginHandle handle)
     m_pluginModels.insert(model);
 }
 
-void PluginContainerSupport::readStandardFeaturesList()
+void PluginContainerSupport::readRootPluginsList()
 {
-    LogDebug("Reading standard features list from file...");
-
-    std::string path = GlobalConfig::GetDevicePluginPath();
-    path += "/";
-    path += STANDARD_FEATURES_LIST_FILE;
+    LogDebug("Reading root plugins list from so files...");
 
-    std::ifstream standardFeatureFile;
-    standardFeatureFile.open(path.c_str(), std::ifstream::in);
-
-    if (!standardFeatureFile.is_open()) {
-        LogError("Reading standard features list from file FAILED.");
-        return;
-    }
-
-    char buffer[1024];
-
-    while (!standardFeatureFile.eof()) {
-        standardFeatureFile.getline(buffer, sizeof(buffer));
-
-        if (buffer[0] == '\0') {
-            break;
-        }
-
-        LogDebug("Standard Feature: <" << buffer << ">");
-        m_standardFeatureList.push_back(std::string(buffer));
-    }
-
-    standardFeatureFile.close();
+    m_rootPluginsList = PluginDAOReadOnly::getRootPluginHandleList();
 }
 
 FeatureModelPtr
@@ -230,51 +216,47 @@ PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
 PluginModelPtr
 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
 {
-    LogDebug("");
-    Assert(feature && "Null Ptr for feature model");
-    LogDebug("Feature located in plugin: " << feature->PHandle.Get());
-
-    return getPluginModelById(feature->PHandle.Get());
+    if (!feature)
+    {
+        LogDebug("Null Ptr for feature model");
+        return PluginModelPtr();
+    }
+    else
+    {
+        LogDebug("Feature located in plugin: " << feature->PHandle.Get());
+        return getPluginModelById(feature->PHandle.Get());
+    }
 }
 
-PluginContainerSupport::FeaturesList
-PluginContainerSupport::getStandardFeatures() const
+ PluginContainerSupport::PluginsList
+PluginContainerSupport::getRootPlugins() const
 {
-    //TODO use move
-    FeaturesList standardFeatures;
+    PluginsList plugins;
 
-    FOREACH(it, m_standardFeatureList)
+    FOREACH(it, m_rootPluginsList)
     {
-        FeatureModelPtr feature = getFeatureModel(*it);
-        if (!feature) {
-            LogWarning("Feature does not exist in DB " << *it);
+        PluginModelPtr plugin = getPluginModelById(*it);
+        if (!plugin) {
+            LogError("PluginModel not found");
             continue;
         }
 
-        //TODO maybe it should be sorted
-        standardFeatures.push_back(feature);
+        plugins.push_back(plugin);
     }
 
-    return standardFeatures;
+    return plugins;
 }
 
 PluginContainerSupport::PluginsList
-PluginContainerSupport::getStandardPlugins() const
+PluginContainerSupport::getPluginsList() const
 {
-    PluginsList plugins;
+    LogDebug("");
 
-    auto features = getStandardFeatures();
+    PluginsList plugins;
 
-    FOREACH(it, features)
+    FOREACH(it, m_pluginModels)
     {
-        auto plugin = getPluginModel(*it);
-        if(!plugin)
-        {
-            LogError("PluginModel not found");
-            continue;
-        }
-
-        plugins.push_back(plugin);
+        plugins.push_back(*it);
     }
 
     return plugins;
@@ -283,5 +265,5 @@ PluginContainerSupport::getStandardPlugins() const
 PluginModelPtr
 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
 {
-   return getPluginModel(getFeatureModel(featureName));
+    return getPluginModel(getFeatureModel(featureName));
 }