[Release] wrt-plugins-common_0.3.76
[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 namespace {
31 const char *STANDARD_FEATURES_LIST_FILE = "standard-features-list";
32 }
33
34 using namespace WrtDB;
35
36 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
37
38 PluginContainerSupport::PluginContainerSupport() : m_initialized(false)
39 {
40     // Reading standard features list from file
41     readStandardFeaturesList();
42
43     // Reading root plugins list from so files
44     readRootPluginsList();
45 }
46
47 PluginContainerSupport::~PluginContainerSupport()
48 {
49     // Remove all plugin models
50     m_pluginModels.clear();
51
52     // Remove all feature models
53     m_featureModels.clear();
54
55     // Clear standard features list
56     m_standardFeatureList.clear();
57 }
58
59 void PluginContainerSupport::Initialize(int widgetHandle)
60 {
61     if (isInitialized() == false) {
62         readAllowedPlugins(widgetHandle);
63         readRootPlugins(widgetHandle);
64     }
65 }
66
67 std::list<std::string> PluginContainerSupport::getAllowedFeatures(
68     int widgetHandle) const
69 {
70     //TODO it has to return LIST NOT SET!!!
71     WidgetDAOReadOnly widgetDao(widgetHandle);
72     DbWidgetFeatureSet features = widgetDao.getFeaturesList();
73
74     std::list<std::string> allowedFeatures;
75     FOREACH(it, features) {
76         LogInfo("Loading api-feature: " << it->name);
77         if (it->rejected) {
78             LogWarning("Api-feature was rejected by ace. (Api-feature name: "
79                        << it->name << ")");
80             continue;
81         }
82
83         allowedFeatures.push_back(DPL::ToUTF8String(it->name));
84     }
85     return allowedFeatures;
86 }
87
88 void PluginContainerSupport::readAllowedPlugins(int widgetHandle)
89 {
90     std::list<std::string> allowedFeatures(m_standardFeatureList);
91     auto requested = getAllowedFeatures(widgetHandle);
92     FOREACH(f, requested)
93     {
94         allowedFeatures.push_back(*f);
95     }
96
97     FeatureData* dt = NULL;
98     std::map<FeatureHandle,
99              FeatureData> featureDataList = FeatureDAOReadOnly::GetFeatures(
100             allowedFeatures);
101     DeviceCapList deviceCapabilities =
102         FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
103     FOREACH(data, featureDataList) {
104         dt = &(data->second);
105         registerPluginModel(dt->pluginHandle);
106         registerFeatureModel(data->first, dt, deviceCapabilities);
107     }
108
109     m_initialized = true;
110 }
111
112 void PluginContainerSupport::readRootPlugins(int widgetHandle)
113 {
114     WidgetDAOReadOnly dao(widgetHandle);
115     WidgetType appType = dao.getWidgetType();
116     if (appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
117          FOREACH(it_rootPluginHandle, m_rootPluginsList)
118         {
119             LogDebug("*it_rootPluginHandle: " << *it_rootPluginHandle);
120             registerPluginModel(*it_rootPluginHandle);
121         }
122     } else {
123         LogDebug("Not defined app type");
124     }
125     m_initialized = true;
126 }
127
128 void PluginContainerSupport::registerFeatureModel(
129     FeatureHandle handle,
130     FeatureData* data,
131     DeviceCapList
132     deviceCapabilities)
133 {
134     LogDebug("Analyzing feature: " << handle);
135     FeatureModelPtr model = getFeatureModel(handle);
136     if (model) {
137         LogDebug("Model for feature:" << handle << " already created");
138         return;
139     }
140
141     LogDebug("Creating Model for feature:" << handle);
142
143     model.reset(new FeatureModel(handle));
144
145     std::set<std::string> devCapList;
146     auto ret = deviceCapabilities.equal_range(handle);
147     for (auto devCapIt = ret.first; devCapIt != ret.second; devCapIt++) {
148         devCapList.insert((*devCapIt).second);
149     }
150     model->SetData(data->featureName, devCapList, data->pluginHandle);
151     m_featureModels.insert(model);
152 }
153
154 void PluginContainerSupport::registerPluginModel(DbPluginHandle handle)
155 {
156     PluginModelPtr model = getPluginModelById(handle);
157
158     if (model) {
159         LogDebug("Model for plugin:" << handle << " already registered");
160         return;
161     }
162
163     LogDebug("Creating Model for plugin: " << handle);
164
165     if (PluginDAOReadOnly::INSTALLATION_COMPLETED !=
166         PluginDAOReadOnly::getInstallationStateForHandle(handle))
167     {
168         LogWarning("Failed To CreateModel for handle " << handle);
169         return;
170     }
171
172     model.Reset(new PluginModel(handle));
173
174     LogInfo("Model Created. Handle: " <<
175             handle << ", name: " << model->LibraryName.Get());
176
177     m_pluginModels.insert(model);
178 }
179
180 void PluginContainerSupport::readStandardFeaturesList()
181 {
182     LogDebug("Reading standard features list from file...");
183
184     std::string path = GlobalConfig::GetDevicePluginPath();
185     path += "/";
186     path += STANDARD_FEATURES_LIST_FILE;
187
188     std::ifstream standardFeatureFile;
189     standardFeatureFile.open(path.c_str(), std::ifstream::in);
190
191     if (!standardFeatureFile.is_open()) {
192         LogError("Reading standard features list from file FAILED.");
193         return;
194     }
195
196     char buffer[1024];
197
198     while (!standardFeatureFile.eof()) {
199         standardFeatureFile.getline(buffer, sizeof(buffer));
200
201         if (buffer[0] == '\0') {
202             break;
203         }
204
205         LogDebug("Standard Feature: <" << buffer << ">");
206         m_standardFeatureList.push_back(std::string(buffer));
207     }
208
209     standardFeatureFile.close();
210 }
211
212 void PluginContainerSupport::readRootPluginsList()
213 {
214     LogDebug("Reading root plugins list from so files...");
215
216     PluginHandleList pluginHandleList =
217         PluginDAOReadOnly::getPluginHandleList();
218
219     FOREACH(it_pluginHandle, pluginHandleList)
220     {
221         PluginDAOReadOnly pluginDao(*it_pluginHandle);
222         PluginHandleSetPtr retDependencies;
223         retDependencies = pluginDao.getLibraryDependencies();
224         if (retDependencies->empty()) {
225             LogDebug("Root plugin Handle: " << *it_pluginHandle);
226             m_rootPluginsList.push_back(*it_pluginHandle);
227         }
228     }
229 }
230
231 FeatureModelPtr
232 PluginContainerSupport::getFeatureModel(const std::string &name) const
233 {
234     FOREACH(iter, m_featureModels)
235     {
236         if ((*iter)->Name.Get() == name) {
237             return *iter;
238         }
239     }
240
241     return FeatureModelPtr();
242 }
243
244 FeatureModelPtr
245 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
246 {
247     FOREACH(iter, m_featureModels)
248     {
249         if ((*iter)->FHandle.Get() == handle) {
250             return *iter;
251         }
252     }
253
254     return FeatureModelPtr();
255 }
256
257 PluginModelPtr
258 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
259 {
260     FOREACH(pluginModel, m_pluginModels)
261     {
262         if ((*pluginModel)->Handle.Get() == handle) {
263             return *pluginModel;
264         }
265     }
266
267     return PluginModelPtr();
268 }
269
270 PluginModelPtr
271 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
272 {
273     LogDebug("");
274     Assert(feature && "Null Ptr for feature model");
275     LogDebug("Feature located in plugin: " << feature->PHandle.Get());
276
277     return getPluginModelById(feature->PHandle.Get());
278 }
279
280 PluginContainerSupport::FeaturesList
281 PluginContainerSupport::getStandardFeatures() const
282 {
283     //TODO use move
284     FeaturesList standardFeatures;
285
286     FOREACH(it, m_standardFeatureList)
287     {
288         FeatureModelPtr feature = getFeatureModel(*it);
289         if (!feature) {
290             LogWarning("Feature does not exist in DB " << *it);
291             continue;
292         }
293
294         //TODO maybe it should be sorted
295         standardFeatures.push_back(feature);
296     }
297
298     return standardFeatures;
299 }
300
301 PluginContainerSupport::PluginsList
302 PluginContainerSupport::getStandardPlugins() const
303 {
304     PluginsList plugins;
305
306     auto features = getStandardFeatures();
307
308     FOREACH(it, features)
309     {
310         auto plugin = getPluginModel(*it);
311         if (!plugin) {
312             LogError("PluginModel not found");
313             continue;
314         }
315
316         plugins.push_back(plugin);
317     }
318
319     return plugins;
320 }
321
322 PluginContainerSupport::PluginsList
323 PluginContainerSupport::getRootPlugins() const
324 {
325     PluginsList plugins;
326
327     FOREACH(it, m_rootPluginsList)
328     {
329         PluginModelPtr plugin = getPluginModelById(*it);
330         if (!plugin) {
331             LogError("PluginModel not found");
332             continue;
333         }
334
335         plugins.push_back(plugin);
336     }
337
338     return plugins;
339 }
340
341 PluginContainerSupport::PluginsList
342 PluginContainerSupport::getPluginsList() const
343 {
344     LogDebug("");
345
346     PluginsList plugins;
347
348     FOREACH(it, m_pluginModels)
349     {
350         plugins.push_back(*it);
351     }
352
353     return plugins;
354 }
355
356 PluginModelPtr
357 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
358 {
359     return getPluginModel(getFeatureModel(featureName));
360 }