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