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