tizen 2.4 release
[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/log/secure_log.h>
27 #include <dpl/foreach.h>
28 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
29 #include <dpl/wrt-dao-ro/global_config.h>
30
31  using namespace WrtDB;
32
33 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
34
35 PluginContainerSupport::PluginContainerSupport() :
36     m_initialized(false),
37     m_widgetHandle(0)
38 {
39      // Reading root plugins list from so files
40     readRootPluginsList();
41 }
42
43 PluginContainerSupport::~PluginContainerSupport()
44 {
45     // Remove all plugin models
46     m_pluginModels.clear();
47
48     // Remove all feature models
49     m_featureModels.clear();
50  }
51
52 void PluginContainerSupport::Initialize(WidgetHandle widgetHandle)
53 {
54     if (isInitialized() == true && m_widgetHandle != widgetHandle) {
55         // re-initialize required
56         m_initialized = false;
57
58         m_pluginModels.clear();
59         m_featureModels.clear();
60     }
61
62     if (isInitialized() == false) {
63         m_initialized = true;
64         m_widgetHandle = widgetHandle;
65
66         readAllowedPlugins(widgetHandle);
67         readRootPlugins(widgetHandle);
68     }
69 }
70
71 std::list<std::string> PluginContainerSupport::getAllowedFeatures(
72     WidgetHandle widgetHandle) const
73 {
74     //TODO it has to return LIST NOT SET!!!
75     WidgetDAOReadOnly widgetDao(widgetHandle);
76     DbWidgetFeatureSet features = widgetDao.getFeaturesList();
77
78     std::list<std::string> allowedFeatures;
79     FOREACH(it, features) {
80         _D("Loading api-feature: %ls", it->name.c_str());
81         if (it->rejected) {
82             _W("Api-feature was rejected by ace. (Api-feature name: %ls)",
83                it->name.c_str());
84             continue;
85         }
86
87         allowedFeatures.push_back(DPL::ToUTF8String(it->name));
88     }
89     return allowedFeatures;
90 }
91
92 void PluginContainerSupport::readAllowedPlugins(WidgetHandle widgetHandle)
93 {
94     std::list<std::string> allowedFeatures;
95     auto requested = getAllowedFeatures(widgetHandle);
96     FOREACH(f, requested)
97     {
98         allowedFeatures.push_back(*f);
99     }
100
101     FeatureData* dt = NULL;
102     std::map<FeatureHandle,
103              FeatureData> featureDataList = FeatureDAOReadOnly::GetFeatures(
104             allowedFeatures);
105     DeviceCapList deviceCapabilities =
106         FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
107     FOREACH(data, featureDataList) {
108         dt = &(data->second);
109         registerPluginModel(dt->pluginHandle);
110         registerFeatureModel(data->first, dt, deviceCapabilities);
111     }
112 }
113
114 void PluginContainerSupport::readRootPlugins(WidgetHandle widgetHandle)
115 {
116     WidgetDAOReadOnly dao(widgetHandle);
117     WidgetType appType = dao.getWidgetType();
118     if (appType == WrtDB::APP_TYPE_TIZENWEBAPP) {
119          FOREACH(it_rootPluginHandle, m_rootPluginsList)
120         {
121             _D("*it_rootPluginHandle: %d", *it_rootPluginHandle);
122             registerPluginModel(*it_rootPluginHandle);
123         }
124     } else {
125         _D("Not defined app type");
126     }
127 }
128
129 void PluginContainerSupport::registerFeatureModel(
130     FeatureHandle handle,
131     FeatureData* data,
132     DeviceCapList
133     deviceCapabilities)
134 {
135     FeatureModelPtr model = getFeatureModel(handle);
136     if (model) {
137         _D("Model for feature: %d already created", handle);
138         return;
139     }
140
141     _D("Creating Model for feature: %d", handle);
142
143     model.reset(new FeatureModel(handle));
144
145     std::set<std::string> devCapList;
146     auto ret = deviceCapabilities.equal_range(handle);
147     for (auto devCapIt = ret.first; devCapIt != ret.second; devCapIt++) {
148         devCapList.insert((*devCapIt).second);
149     }
150     model->SetData(data->featureName, devCapList, data->pluginHandle);
151     m_featureModels.insert(model);
152 }
153
154 void PluginContainerSupport::registerPluginModel(DbPluginHandle handle)
155 {
156     PluginModelPtr model = getPluginModelById(handle);
157
158     if (model) {
159         _D("Model for plugin: %d already registered", handle);
160         return;
161     }
162
163     if (PluginDAOReadOnly::INSTALLATION_COMPLETED !=
164         PluginDAOReadOnly::getInstallationStateForHandle(handle))
165     {
166         _W("Failed To CreateModel for handle %d", handle);
167         return;
168     }
169
170     Try {
171         model.reset(new PluginModel(handle));
172     } Catch(WrtDB::PluginDAOReadOnly::Exception::PluginNotExist) {
173         // database changed between receiving plugin handle and creating PluginModel
174         _E("Failed to create PluginModel - database may be changed");
175         return;
176     }
177     \r_D("Model Created. Handle: %d, name: %s",
178        handle,
179        model->LibraryName.Get().c_str());
180
181     m_pluginModels.insert(model);
182 }
183
184 void PluginContainerSupport::readRootPluginsList()
185 {
186     _D("Reading root plugins list from so files...");
187     m_rootPluginsList = PluginDAOReadOnly::getRootPluginHandleList();
188 }
189
190 FeatureModelPtr
191 PluginContainerSupport::getFeatureModel(const std::string &name) const
192 {
193     FOREACH(iter, m_featureModels)
194     {
195         if ((*iter)->Name.Get() == name) {
196             return *iter;
197         }
198     }
199
200     return FeatureModelPtr();
201 }
202
203 FeatureModelPtr
204 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
205 {
206     FOREACH(iter, m_featureModels)
207     {
208         if ((*iter)->FHandle.Get() == handle) {
209             return *iter;
210         }
211     }
212
213     return FeatureModelPtr();
214 }
215
216 PluginModelPtr
217 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
218 {
219     FOREACH(pluginModel, m_pluginModels)
220     {
221         if ((*pluginModel)->Handle.Get() == handle) {
222             return *pluginModel;
223         }
224     }
225
226     return PluginModelPtr();
227 }
228
229 PluginModelPtr
230 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
231 {
232     if (!feature) {
233         _D("Null Ptr for feature model");
234         return PluginModelPtr();
235     } else {
236         _D("Feature located in plugin: %d", feature->PHandle.Get());
237         return getPluginModelById(feature->PHandle.Get());
238     }
239 }
240
241  PluginContainerSupport::PluginsList
242 PluginContainerSupport::getRootPlugins() const
243 {
244     PluginsList plugins;
245
246     FOREACH(it, m_rootPluginsList) {
247         PluginModelPtr plugin = getPluginModelById(*it);
248         if (!plugin) {
249             _W("PluginModel not found");
250             continue;
251         }
252
253         plugins.push_back(plugin);
254     }
255
256     return plugins;
257 }
258
259 PluginContainerSupport::PluginsList
260 PluginContainerSupport::getPluginsList() const
261 {
262     PluginsList plugins;
263
264     FOREACH(it, m_pluginModels) {
265         plugins.push_back(*it);
266     }
267
268     return plugins;
269 }
270
271 PluginModelPtr
272 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
273 {
274     return getPluginModel(getFeatureModel(featureName));
275 }