Tizen 2.1 base
[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     {
61         readAllowedPlugins(widgetHandle);
62         readRootPlugins(widgetHandle);
63     }
64 }
65
66 std::list<std::string> PluginContainerSupport::getAllowedFeatures(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, FeatureData> featureDataList = FeatureDAOReadOnly::GetFeatures(allowedFeatures);
97     DeviceCapList deviceCapabilities = FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
98     FOREACH(data, featureDataList) {
99         dt = &(data->second);
100         registerPluginModel(dt->pluginHandle);
101         registerFeatureModel(data->first, dt, deviceCapabilities);
102     }
103
104     m_initialized = true;
105 }
106
107 void PluginContainerSupport::readRootPlugins(int widgetHandle)
108 {
109     WidgetDAOReadOnly dao(widgetHandle);
110     WidgetType appType = dao.getWidgetType();
111     if (appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
112         WrtDB::FeatureDAOReadOnly dao(TIZEN_ROOT_FEATURES);
113         registerPluginModel(dao.GetPluginHandle());
114     } else {
115         LogDebug("Not defined app type");
116     }
117     m_initialized = true;
118 }
119
120 void PluginContainerSupport::registerFeatureModel(FeatureHandle handle, FeatureData* data, DeviceCapList deviceCapabilities)
121 {
122     LogDebug("Analyzing feature: " << handle);
123     FeatureModelPtr model = getFeatureModel(handle);
124     if (model) {
125         LogDebug("Model for feature:" << handle << " already created");
126         return;
127     }
128
129     LogDebug("Creating Model for feature:" << handle);
130
131     model.reset(new FeatureModel(handle));
132
133     std::set<std::string> devCapList;
134     auto ret = deviceCapabilities.equal_range(handle);
135     for (auto devCapIt = ret.first; devCapIt != ret.second; devCapIt++) {
136         devCapList.insert((*devCapIt).second);
137     }
138     model->SetData(data->featureName, devCapList, data->pluginHandle);
139     m_featureModels.insert(model);
140 }
141
142 void PluginContainerSupport::registerPluginModel(DbPluginHandle handle)
143 {
144     PluginModelPtr model = getPluginModelById(handle);
145
146     if (model) {
147         LogDebug("Model for plugin:" << handle << " already registered");
148         return;
149     }
150
151     LogDebug("Creating Model for plugin: " << handle);
152
153     if (PluginDAOReadOnly::INSTALLATION_COMPLETED !=
154         PluginDAOReadOnly::getInstallationStateForHandle(handle))
155     {
156         LogWarning("Failed To CreateModel for handle " << handle);
157         return;
158     }
159
160     model.Reset(new PluginModel(handle));
161
162     LogInfo("Model Created. Handle: " <<
163             handle << ", name: " << model->LibraryName.Get());
164
165     m_pluginModels.insert(model);
166 }
167
168 void PluginContainerSupport::readStandardFeaturesList()
169 {
170     LogDebug("Reading standard features list from file...");
171
172     std::string path = GlobalConfig::GetDevicePluginPath();
173     path += "/";
174     path += STANDARD_FEATURES_LIST_FILE;
175
176     std::ifstream standardFeatureFile;
177     standardFeatureFile.open(path.c_str(), std::ifstream::in);
178
179     if (!standardFeatureFile.is_open()) {
180         LogError("Reading standard features list from file FAILED.");
181         return;
182     }
183
184     char buffer[1024];
185
186     while (!standardFeatureFile.eof()) {
187         standardFeatureFile.getline(buffer, sizeof(buffer));
188
189         if (buffer[0] == '\0') {
190             break;
191         }
192
193         LogDebug("Standard Feature: <" << buffer << ">");
194         m_standardFeatureList.push_back(std::string(buffer));
195     }
196
197     standardFeatureFile.close();
198 }
199
200 FeatureModelPtr
201 PluginContainerSupport::getFeatureModel(const std::string &name) const
202 {
203     FOREACH(iter, m_featureModels)
204     {
205         if ((*iter)->Name.Get() == name) {
206             return *iter;
207         }
208     }
209
210     return FeatureModelPtr();
211 }
212
213 FeatureModelPtr
214 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
215 {
216     FOREACH(iter, m_featureModels)
217     {
218         if ((*iter)->FHandle.Get() == handle) {
219             return *iter;
220         }
221     }
222
223     return FeatureModelPtr();
224 }
225
226 PluginModelPtr
227 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
228 {
229     FOREACH(pluginModel, m_pluginModels)
230     {
231         if ((*pluginModel)->Handle.Get() == handle) {
232             return *pluginModel;
233         }
234     }
235
236     return PluginModelPtr();
237 }
238
239 PluginModelPtr
240 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
241 {
242     LogDebug("");
243     Assert(feature && "Null Ptr for feature model");
244     LogDebug("Feature located in plugin: " << feature->PHandle.Get());
245
246     return getPluginModelById(feature->PHandle.Get());
247 }
248
249 PluginContainerSupport::FeaturesList
250 PluginContainerSupport::getStandardFeatures() const
251 {
252     //TODO use move
253     FeaturesList standardFeatures;
254
255     FOREACH(it, m_standardFeatureList)
256     {
257         FeatureModelPtr feature = getFeatureModel(*it);
258         if (!feature) {
259             LogWarning("Feature does not exist in DB " << *it);
260             continue;
261         }
262
263         //TODO maybe it should be sorted
264         standardFeatures.push_back(feature);
265     }
266
267     return standardFeatures;
268 }
269
270 PluginContainerSupport::PluginsList
271 PluginContainerSupport::getStandardPlugins() const
272 {
273     PluginsList plugins;
274
275     auto features = getStandardFeatures();
276
277     FOREACH(it, features)
278     {
279         auto plugin = getPluginModel(*it);
280         if(!plugin)
281         {
282             LogError("PluginModel not found");
283             continue;
284         }
285
286         plugins.push_back(plugin);
287     }
288
289     return plugins;
290 }
291
292 PluginContainerSupport::PluginsList
293 PluginContainerSupport::getPluginsList() const
294 {
295     LogDebug("");
296
297     PluginsList plugins;
298
299     FOREACH(it, m_pluginModels)
300     {
301         plugins.push_back(*it);
302     }
303
304     return plugins;
305 }
306
307 PluginModelPtr
308 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
309 {
310    return getPluginModel(getFeatureModel(featureName));
311 }