Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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(int 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     int 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: %s", DPL::ToUTF8String(it->name).c_str());
81         if (it->rejected) {
82             _W("Api-feature was rejected by ace. (Api-feature name: %s)",
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(int 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(int 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     model.Reset(new PluginModel(handle));
171
172     \r_D("Model Created. Handle: %d, name: %s",
173        handle,
174        model->LibraryName.Get().c_str());
175
176     m_pluginModels.insert(model);
177 }
178
179 void PluginContainerSupport::readRootPluginsList()
180 {
181     _D("Reading root plugins list from so files...");
182     m_rootPluginsList = PluginDAOReadOnly::getRootPluginHandleList();
183 }
184
185 FeatureModelPtr
186 PluginContainerSupport::getFeatureModel(const std::string &name) const
187 {
188     FOREACH(iter, m_featureModels)
189     {
190         if ((*iter)->Name.Get() == name) {
191             return *iter;
192         }
193     }
194
195     return FeatureModelPtr();
196 }
197
198 FeatureModelPtr
199 PluginContainerSupport::getFeatureModel(const FeatureHandle handle) const
200 {
201     FOREACH(iter, m_featureModels)
202     {
203         if ((*iter)->FHandle.Get() == handle) {
204             return *iter;
205         }
206     }
207
208     return FeatureModelPtr();
209 }
210
211 PluginModelPtr
212 PluginContainerSupport::getPluginModelById(DbPluginHandle handle) const
213 {
214     FOREACH(pluginModel, m_pluginModels)
215     {
216         if ((*pluginModel)->Handle.Get() == handle) {
217             return *pluginModel;
218         }
219     }
220
221     return PluginModelPtr();
222 }
223
224 PluginModelPtr
225 PluginContainerSupport::getPluginModel(const FeatureModelPtr &feature) const
226 {
227     if (!feature) {
228         _D("Null Ptr for feature model");
229         return PluginModelPtr();
230     } else {
231         _D("Feature located in plugin: %d", feature->PHandle.Get());
232         return getPluginModelById(feature->PHandle.Get());
233     }
234 }
235
236  PluginContainerSupport::PluginsList
237 PluginContainerSupport::getRootPlugins() const
238 {
239     PluginsList plugins;
240
241     FOREACH(it, m_rootPluginsList) {
242         PluginModelPtr plugin = getPluginModelById(*it);
243         if (!plugin) {
244             _W("PluginModel not found");
245             continue;
246         }
247
248         plugins.push_back(plugin);
249     }
250
251     return plugins;
252 }
253
254 PluginContainerSupport::PluginsList
255 PluginContainerSupport::getPluginsList() const
256 {
257     PluginsList plugins;
258
259     FOREACH(it, m_pluginModels) {
260         plugins.push_back(*it);
261     }
262
263     return plugins;
264 }
265
266 PluginModelPtr
267 PluginContainerSupport::getPluginForFeature(const std::string& featureName)
268 {
269     return getPluginModel(getFeatureModel(featureName));
270 }