Improve performance in getting root plugins list.
[platform/framework/web/wrt-plugins-common.git] / src / plugin-loading / plugin_container_support.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        plugin_container_support.h
18  * @author      Grzegorz Krawczyk (g.krawczyk@samsung.com)
19  * @version     1.0
20  */
21
22 #include "plugin_container_support.h"
23
24 #include <fstream>
25
26 #include <dpl/foreach.h>
27 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
28 #include <dpl/wrt-dao-ro/global_config.h>
29
30  using namespace WrtDB;
31
32 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
33
34 PluginContainerSupport::PluginContainerSupport() : m_initialized(false)
35 {
36      // Reading root plugins list from so files
37     readRootPluginsList();
38 }
39
40 PluginContainerSupport::~PluginContainerSupport()
41 {
42     // Remove all plugin models
43     m_pluginModels.clear();
44
45     // Remove all feature models
46     m_featureModels.clear();
47  }
48
49 void PluginContainerSupport::Initialize(int widgetHandle)
50 {
51     if (isInitialized() == false) {
52         readAllowedPlugins(widgetHandle);
53         readRootPlugins(widgetHandle);
54     }
55 }
56
57 std::list<std::string> PluginContainerSupport::getAllowedFeatures(
58     int widgetHandle) const
59 {
60     //TODO it has to return LIST NOT SET!!!
61     WidgetDAOReadOnly widgetDao(widgetHandle);
62     DbWidgetFeatureSet features = widgetDao.getFeaturesList();
63
64     std::list<std::string> allowedFeatures;
65     FOREACH(it, features) {
66         LogInfo("Loading api-feature: " << it->name);
67         if (it->rejected) {
68             LogWarning("Api-feature was rejected by ace. (Api-feature name: "
69                        << it->name << ")");
70             continue;
71         }
72
73         allowedFeatures.push_back(DPL::ToUTF8String(it->name));
74     }
75     return allowedFeatures;
76 }
77
78 void PluginContainerSupport::readAllowedPlugins(int widgetHandle)
79 {
80     std::list<std::string> allowedFeatures;
81     auto requested = getAllowedFeatures(widgetHandle);
82     FOREACH(f, requested)
83     {
84         allowedFeatures.push_back(*f);
85     }
86
87     FeatureData* dt = NULL;
88     std::map<FeatureHandle,
89              FeatureData> featureDataList = FeatureDAOReadOnly::GetFeatures(
90             allowedFeatures);
91     DeviceCapList deviceCapabilities =
92         FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
93     FOREACH(data, featureDataList) {
94         dt = &(data->second);
95         registerPluginModel(dt->pluginHandle);
96         registerFeatureModel(data->first, dt, deviceCapabilities);
97     }
98
99     m_initialized = true;
100 }
101
102 void PluginContainerSupport::readRootPlugins(int widgetHandle)
103 {
104     WidgetDAOReadOnly dao(widgetHandle);
105     WidgetType appType = dao.getWidgetType();
106     if (appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
107          FOREACH(it_rootPluginHandle, m_rootPluginsList)
108         {
109             LogDebug("*it_rootPluginHandle: " << *it_rootPluginHandle);
110             registerPluginModel(*it_rootPluginHandle);
111         }
112     } else {
113         LogDebug("Not defined app type");
114     }
115     m_initialized = true;
116 }
117
118 void PluginContainerSupport::registerFeatureModel(
119     FeatureHandle handle,
120     FeatureData* data,
121     DeviceCapList
122     deviceCapabilities)
123 {
124     LogDebug("Analyzing feature: " << handle);
125     FeatureModelPtr model = getFeatureModel(handle);
126     if (model) {
127         LogDebug("Model for feature:" << handle << " already created");
128         return;
129     }
130
131     LogDebug("Creating Model for feature:" << handle);
132
133     model.reset(new FeatureModel(handle));
134
135     std::set<std::string> devCapList;
136     auto ret = deviceCapabilities.equal_range(handle);
137     for (auto devCapIt = ret.first; devCapIt != ret.second; devCapIt++) {
138         devCapList.insert((*devCapIt).second);
139     }
140     model->SetData(data->featureName, devCapList, data->pluginHandle);
141     m_featureModels.insert(model);
142 }
143
144 void PluginContainerSupport::registerPluginModel(DbPluginHandle handle)
145 {
146     PluginModelPtr model = getPluginModelById(handle);
147
148     if (model) {
149         LogDebug("Model for plugin:" << handle << " already registered");
150         return;
151     }
152
153     LogDebug("Creating Model for plugin: " << handle);
154
155     if (PluginDAOReadOnly::INSTALLATION_COMPLETED !=
156         PluginDAOReadOnly::getInstallationStateForHandle(handle))
157     {
158         LogWarning("Failed To CreateModel for handle " << handle);
159         return;
160     }
161
162     model.Reset(new PluginModel(handle));
163
164     LogInfo("Model Created. Handle: " <<
165             handle << ", name: " << model->LibraryName.Get());
166
167     m_pluginModels.insert(model);
168 }
169
170 void PluginContainerSupport::readRootPluginsList()
171 {
172     LogDebug("Reading root plugins list from so files...");
173
174     m_rootPluginsList = PluginDAOReadOnly::getRootPluginHandleList();
175 }
176
177 FeatureModelPtr
178 PluginContainerSupport::getFeatureModel(const std::string &name) const
179 {
180     FOREACH(iter, m_featureModels)
181     {
182         if ((*iter)->Name.Get() == name) {
183             return *iter;
184         }
185     }
186
187     return FeatureModelPtr();
188 }
189
190 FeatureModelPtr
191 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
192 {
193     FOREACH(iter, m_featureModels)
194     {
195         if ((*iter)->FHandle.Get() == handle) {
196             return *iter;
197         }
198     }
199
200     return FeatureModelPtr();
201 }
202
203 PluginModelPtr
204 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
205 {
206     FOREACH(pluginModel, m_pluginModels)
207     {
208         if ((*pluginModel)->Handle.Get() == handle) {
209             return *pluginModel;
210         }
211     }
212
213     return PluginModelPtr();
214 }
215
216 PluginModelPtr
217 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
218 {
219     if (!feature)
220     {
221         LogDebug("Null Ptr for feature model");
222         return PluginModelPtr();
223     }
224     else
225     {
226         LogDebug("Feature located in plugin: " << feature->PHandle.Get());
227         return getPluginModelById(feature->PHandle.Get());
228     }
229 }
230
231  PluginContainerSupport::PluginsList
232 PluginContainerSupport::getRootPlugins() const
233 {
234     PluginsList plugins;
235
236     FOREACH(it, m_rootPluginsList)
237     {
238         PluginModelPtr plugin = getPluginModelById(*it);
239         if (!plugin) {
240             LogError("PluginModel not found");
241             continue;
242         }
243
244         plugins.push_back(plugin);
245     }
246
247     return plugins;
248 }
249
250 PluginContainerSupport::PluginsList
251 PluginContainerSupport::getPluginsList() const
252 {
253     LogDebug("");
254
255     PluginsList plugins;
256
257     FOREACH(it, m_pluginModels)
258     {
259         plugins.push_back(*it);
260     }
261
262     return plugins;
263 }
264
265 PluginModelPtr
266 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
267 {
268     return getPluginModel(getFeatureModel(featureName));
269 }