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