Fixed problem with running tests
[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/global_config.h>
28
29 namespace {
30 const char *STANDARD_FEATURES_LIST_FILE = "standard-features-list";
31 }
32
33 using namespace WrtDB;
34
35 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
36
37 PluginContainerSupport::PluginContainerSupport(): m_initialized(false)
38 {
39     // Reading standard features list from file
40     readStandardFeaturesList();
41 }
42
43 PluginContainerSupport::~PluginContainerSupport()
44 {
45     // Remove all plugin models
46     m_pluginModels.clear();
47
48     // Remove all feature models
49     m_featureModels.clear();
50
51     // Clear standard features list
52     m_standardFeatureList.clear();
53 }
54
55 std::list<std::string> PluginContainerSupport::getAllowedFeatures(int widgetHandle) const
56 {
57     //TODO it has to return LIST NOT SET!!!
58     WidgetDAOReadOnly widgetDao(widgetHandle);
59     DbWidgetFeatureSet features = widgetDao.getFeaturesList();
60
61     std::list<std::string> allowedFeatures;
62     FOREACH(it, features) {
63         LogInfo("Loading api-feature: " << it->name);
64         if (it->rejected) {
65             LogWarning("Api-feature was rejected by ace. (Api-feature name: "
66                        << it->name << ")" );
67             continue;
68         }
69
70         allowedFeatures.push_back(DPL::ToUTF8String(it->name));
71     }
72     return allowedFeatures;
73 }
74
75 void PluginContainerSupport::readAllowedPlugins(int widgetHandle)
76 {
77     std::list<std::string> allowedFeatures(m_standardFeatureList);
78     auto requested = getAllowedFeatures(widgetHandle);
79     FOREACH(f, requested)
80     {
81         allowedFeatures.push_back(*f);
82     }
83
84     FeatureData* dt = NULL;
85     std::map<FeatureHandle, FeatureData> featureDataList = FeatureDAOReadOnly::GetFeatures(allowedFeatures);
86     DeviceCapList deviceCapabilities = FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
87     FOREACH(data, featureDataList) {
88         dt = &(data->second);
89         registerPluginModel(dt->pluginHandle);
90         registerFeatureModel(data->first, dt, deviceCapabilities);
91     }
92
93     m_initialized = true;
94 }
95
96 void PluginContainerSupport::registerFeatureModel(FeatureHandle handle, FeatureData* data, DeviceCapList deviceCapabilities)
97 {
98     LogDebug("Analyzing feature: " << handle);
99     FeatureModelPtr model = getFeatureModel(handle);
100     if (model) {
101         LogDebug("Model for feature:" << handle << " already created");
102         return;
103     }
104
105     LogDebug("Creating Model for feature:" << handle);
106
107     model.reset(new FeatureModel(handle));
108
109     std::set<std::string> devCapList;
110     auto ret = deviceCapabilities.equal_range(handle);
111     for (auto devCapIt = ret.first; devCapIt != ret.second; devCapIt++) {
112         devCapList.insert((*devCapIt).second);
113     }
114     model->SetData(data->featureName, devCapList, data->pluginHandle);
115     m_featureModels.insert(model);
116 }
117
118 void PluginContainerSupport::registerPluginModel(DbPluginHandle handle)
119 {
120     PluginModelPtr model = getPluginModelById(handle);
121
122     if (model) {
123         LogDebug("Model for plugin:" << handle << " already registered");
124         return;
125     }
126
127     LogDebug("Creating Model for plugin: " << handle);
128
129     if (PluginDAOReadOnly::INSTALLATION_COMPLETED !=
130         PluginDAOReadOnly::getInstallationStateForHandle(handle))
131     {
132         LogWarning("Failed To CreateModel for handle " << handle);
133         return;
134     }
135
136     model.Reset(new PluginModel(handle));
137
138     LogInfo("Model Created. Handle: " <<
139             handle << ", name: " << model->LibraryName.Get());
140
141     m_pluginModels.insert(model);
142 }
143
144 void PluginContainerSupport::readStandardFeaturesList()
145 {
146     LogDebug("Reading standard features list from file...");
147
148     std::string path = GlobalConfig::GetDevicePluginPath();
149     path += "/";
150     path += STANDARD_FEATURES_LIST_FILE;
151
152     std::ifstream standardFeatureFile;
153     standardFeatureFile.open(path.c_str(), std::ifstream::in);
154
155     if (!standardFeatureFile.is_open()) {
156         LogError("Reading standard features list from file FAILED.");
157         return;
158     }
159
160     char buffer[1024];
161
162     while (!standardFeatureFile.eof()) {
163         standardFeatureFile.getline(buffer, sizeof(buffer));
164
165         if (buffer[0] == '\0') {
166             break;
167         }
168
169         LogDebug("Standard Feature: <" << buffer << ">");
170         m_standardFeatureList.push_back(std::string(buffer));
171     }
172
173     standardFeatureFile.close();
174 }
175
176 FeatureModelPtr
177 PluginContainerSupport::getFeatureModel(const std::string &name) const
178 {
179     FOREACH(iter, m_featureModels)
180     {
181         if ((*iter)->Name.Get() == name) {
182             return *iter;
183         }
184     }
185
186     return FeatureModelPtr();
187 }
188
189 FeatureModelPtr
190 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
191 {
192     FOREACH(iter, m_featureModels)
193     {
194         if ((*iter)->FHandle.Get() == handle) {
195             return *iter;
196         }
197     }
198
199     return FeatureModelPtr();
200 }
201
202 PluginModelPtr
203 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
204 {
205     FOREACH(pluginModel, m_pluginModels)
206     {
207         if ((*pluginModel)->Handle.Get() == handle) {
208             return *pluginModel;
209         }
210     }
211
212     return PluginModelPtr();
213 }
214
215 PluginModelPtr
216 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
217 {
218     LogDebug("");
219     Assert(feature && "Null Ptr for feature model");
220     LogDebug("Feature located in plugin: " << feature->PHandle.Get());
221
222     return getPluginModelById(feature->PHandle.Get());
223 }
224
225 PluginContainerSupport::FeaturesList
226 PluginContainerSupport::getStandardFeatures() const
227 {
228     //TODO use move
229     FeaturesList standardFeatures;
230
231     FOREACH(it, m_standardFeatureList)
232     {
233         FeatureModelPtr feature = getFeatureModel(*it);
234         if (!feature) {
235             LogWarning("Feature does not exist in DB " << *it);
236             continue;
237         }
238
239         //TODO maybe it should be sorted
240         standardFeatures.push_back(feature);
241     }
242
243     return standardFeatures;
244 }
245
246 PluginContainerSupport::PluginsList
247 PluginContainerSupport::getStandardPlugins() const
248 {
249     PluginsList plugins;
250
251     auto features = getStandardFeatures();
252
253     FOREACH(it, features)
254     {
255         auto plugin = getPluginModel(*it);
256         if(!plugin)
257         {
258             LogError("PluginModel not found");
259             continue;
260         }
261
262         plugins.push_back(plugin);
263     }
264
265     return plugins;
266 }
267
268 PluginModelPtr
269 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
270 {
271    return getPluginModel(getFeatureModel(featureName));
272 }