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