upload tizen1.0 source
[platform/framework/web/wrt.git] / src / plugin-service / 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     FOREACH(pluginIt, m_pluginModels)
99     {
100         PluginModelPtr model = *pluginIt;
101         LogDebug("Analyzing Plugin model: " << model->Handle.Get());
102
103         FeatureHandleListPtr featureHandles = model->FeatureHandles.Get();
104         FOREACH(featureIt, *featureHandles)
105         {
106             LogDebug("Analyzing feature: " << *featureIt);
107             registerFeatureModel(*featureIt, model->Handle.Get());
108         }
109     }
110 }
111
112 void PluginContainerSupport::readStandardFeaturesList()
113 {
114     LogDebug("Reading standard features list from file...");
115
116     std::string path = GlobalConfig::GetDevicePluginPath();
117     path += "/";
118     path += STANDARD_FEATURES_LIST_FILE;
119
120     std::ifstream standardFeatureFile;
121     standardFeatureFile.open(path.c_str(), std::ifstream::in);
122
123     if (!standardFeatureFile.is_open()) {
124         LogError("Reading standard features list from file FAILED.");
125         return;
126     }
127
128     char buffer[1024];
129
130     while (!standardFeatureFile.eof()) {
131         standardFeatureFile.getline(buffer, sizeof(buffer));
132
133         if (buffer[0] == '\0') {
134             break;
135         }
136
137         LogDebug("Standard Feature: <" << buffer << ">");
138         m_standardFeatureList.push_back(std::string(buffer));
139     }
140
141     standardFeatureFile.close();
142 }
143
144 FeatureModelPtr
145 PluginContainerSupport::getFeatureModel(const std::string &name) const
146 {
147     FOREACH(iter, m_featureModels)
148     {
149         if ((*iter)->Name.Get() == name) {
150             return *iter;
151         }
152     }
153
154     return FeatureModelPtr();
155 }
156
157 FeatureModelPtr
158 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
159 {
160     FOREACH(iter, m_featureModels)
161     {
162         if ((*iter)->FHandle.Get() == handle) {
163             return *iter;
164         }
165     }
166
167     return FeatureModelPtr();
168 }
169
170 PluginModelPtr
171 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
172 {
173     FOREACH(pluginModel, m_pluginModels)
174     {
175         if ((*pluginModel)->Handle.Get() == handle) {
176             return *pluginModel;
177         }
178     }
179
180     return PluginModelPtr();
181 }
182
183 void PluginContainerSupport::registerFeatureModel(FeatureHandle featureHandle,
184                                              WrtDB::DbPluginHandle pluginHandle)
185 {
186     FeatureModelPtr model = getFeatureModel(featureHandle);
187
188     if (model) {
189         LogDebug("Model for feature:" << featureHandle << " already created");
190         return;
191     }
192
193     LogDebug("Creating Model for feature:" << featureHandle);
194
195     // Create new model
196     model.Reset(new FeatureModel(featureHandle));
197
198     // Read DAO data
199     FeatureDAOReadOnly dao(featureHandle);
200     //    PluginHandle pluginHandle = dao.GetPluginHandle();
201
202     model->Name.Set(dao.GetName());
203     model->DeviceCapabilities.Set(dao.GetDeviceCapabilities());
204     model->PHandle.Set(pluginHandle);
205
206     // Insert new model into map
207     m_featureModels.insert(model);
208 }
209
210 PluginModelPtr
211 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
212 {
213     LogDebug("");
214     Assert(feature && "Null Ptr for feature model");
215     LogDebug("Feature located in plugin: " << feature->PHandle.Get());
216
217     return getPluginModelById(feature->PHandle.Get());
218 }
219
220 PluginContainerSupport::FeaturesList
221 PluginContainerSupport::getStandardFeatures() const
222 {
223     //TODO use move
224     FeaturesList standardFeatures;
225
226     FOREACH(it, m_standardFeatureList)
227     {
228         FeatureModelPtr feature = getFeatureModel(*it);
229         if (!feature) {
230             LogWarning("Feature does not exist in DB" << *it);
231             continue;
232         }
233
234         //TODO maybe it should be sorted
235         standardFeatures.push_back(feature);
236     }
237
238     return standardFeatures;
239 }
240
241 PluginContainerSupport::PluginsList
242 PluginContainerSupport::getStandardPlugins() const
243 {
244     PluginsList plugins;
245
246     auto features = getStandardFeatures();
247
248     FOREACH(it, features)
249     {
250         auto plugin = getPluginModel(*it);
251         if(!plugin)
252         {
253             LogError("PluginModel not found");
254             continue;
255         }
256
257         plugins.push_back(plugin);
258     }
259
260     return plugins;
261 }
262
263 PluginModelPtr
264 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
265 {
266    return getPluginModel(getFeatureModel(featureName));
267 }