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