2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file install_one_task.cpp
18 * @author Pawel Sikorski (p.sikorski@samgsung.com)
19 * @author Grzegorz Krawczyk (g.krawczyk@samgsung.com)
25 #include <sys/types.h>
30 #include <dpl/log/log.h>
31 #include <dpl/foreach.h>
33 #include "plugin_install_task.h"
34 #include "job_plugin_install.h"
35 #include "plugin_installer_errors.h"
36 #include "plugin_metafile_reader.h"
37 #include <dpl/wrt-dao-ro/global_config.h>
39 #include <wrt_common_types.h>
40 #include <dpl/wrt-dao-rw/feature_dao.h>
41 #include <dpl/wrt-dao-rw/plugin_dao.h>
42 #include "plugin_objects.h"
43 #include <wrt_plugin_export.h>
45 using namespace WrtDB;
48 const std::string DIRECTORY_SEPARATOR = std::string("/");
51 #define SET_PLUGIN_INSTALL_PROGRESS(step, desc) \
52 m_context->installerTask->UpdateProgress( \
53 PluginInstallerContext::step, desc);
55 #define DISABLE_IF_PLUGIN_WITHOUT_LIB() \
56 if (m_pluginInfo.m_libraryName.empty()) \
58 LogWarning("Plugin without library."); \
63 namespace PluginInstall {
64 PluginInstallTask::PluginInstallTask(PluginInstallerContext *inCont) :
65 DPL::TaskDecl<PluginInstallTask>(this),
68 m_dataFromConfigXML(true)
70 AddStep(&PluginInstallTask::stepCheckPluginPath);
71 AddStep(&PluginInstallTask::stepParseConfigFile);
72 AddStep(&PluginInstallTask::stepFindPluginLibrary);
73 AddStep(&PluginInstallTask::stepCheckIfAlreadyInstalled);
74 AddStep(&PluginInstallTask::stepLoadPluginLibrary);
75 AddStep(&PluginInstallTask::stepRegisterPlugin);
76 AddStep(&PluginInstallTask::stepRegisterFeatures);
77 AddStep(&PluginInstallTask::stepRegisterPluginObjects);
78 AddStep(&PluginInstallTask::stepResolvePluginDependencies);
80 SET_PLUGIN_INSTALL_PROGRESS(START, "Installation initialized");
83 PluginInstallTask::~PluginInstallTask()
86 void PluginInstallTask::stepCheckPluginPath()
88 LogInfo("Plugin installation: step CheckPluginPath");
90 if(!m_context->pluginFilePath.Exists()){
91 ThrowMsg(Exceptions::PluginPathFailed,
95 SET_PLUGIN_INSTALL_PROGRESS(PLUGIN_PATH, "Path to plugin verified");
98 void PluginInstallTask::stepParseConfigFile()
100 LogInfo("Plugin installation: step parse config file");
102 DPL::Utils::Path filename = m_context->pluginFilePath;
103 filename /= GlobalConfig::GetPluginMetafileName();
105 if(!filename.Exists()){
106 m_dataFromConfigXML = false;
110 LogInfo("Plugin Config file::" << filename.Filename());
114 PluginMetafileReader reader;
115 reader.initialize(filename);
116 reader.read(m_pluginInfo);
118 FOREACH(it, m_pluginInfo.m_featureContainer)
120 LogDebug("Parsed feature : " << it->m_name);
121 FOREACH(devCap, it->m_deviceCapabilities) {
122 LogDebug(" | DevCap : " << *devCap);
126 SET_PLUGIN_INSTALL_PROGRESS(PLUGIN_PATH, "Config file analyzed");
128 Catch(ValidationCore::ParserSchemaException::Base)
130 LogError("Error during file processing " << filename.Filename());
131 ThrowMsg(Exceptions::PluginMetafileFailed,
136 void PluginInstallTask::stepFindPluginLibrary()
138 if (m_dataFromConfigXML) {
141 LogDebug("Plugin installation: step find plugin library");
142 DPL::Utils::Path pluginPath = m_context->pluginFilePath;
143 size_t indexpos = pluginPath.Fullpath().find_last_of('/');
145 if (std::string::npos == indexpos) {
148 indexpos += 1; // move after '/'
151 std::string libName = pluginPath.Fullpath().substr(indexpos);
152 libName = GlobalConfig::GetPluginPrefix() + libName +
153 GlobalConfig::GetPluginSuffix();
154 LogDebug("Plugin .so: " << libName);
155 m_pluginInfo.m_libraryName = libName;
158 void PluginInstallTask::stepCheckIfAlreadyInstalled()
160 if (PluginDAO::isPluginInstalled(m_pluginInfo.m_libraryName)) {
161 ThrowMsg(Exceptions::PluginAlreadyInstalled,
162 "Plugin already installed");
165 SET_PLUGIN_INSTALL_PROGRESS(PLUGIN_EXISTS_CHECK, "Check if plugin exist");
168 void PluginInstallTask::stepLoadPluginLibrary()
170 LogInfo("Plugin installation: step load library");
172 DISABLE_IF_PLUGIN_WITHOUT_LIB()
174 DPL::Utils::Path filename = m_context->pluginFilePath;
175 filename /= m_pluginInfo.m_libraryName;
177 LogDebug("Loading plugin: " << filename.Filename());
179 void *dlHandle = dlopen(filename.Fullpath().c_str(), RTLD_LAZY);
180 if (dlHandle == NULL) {
181 const char* error = (const char*)dlerror();
183 "Failed to load plugin: " << filename.Filename() <<
184 ". Reason: " << (error != NULL ? error : "unknown"));
185 ThrowMsg(Exceptions::PluginLibraryError, "Library error");
188 const js_entity_definition_t *rawEntityList = NULL;
189 get_widget_entity_map_proc *getWidgetEntityMapProcPtr = NULL;
191 getWidgetEntityMapProcPtr =
192 reinterpret_cast<get_widget_entity_map_proc *>(dlsym(dlHandle,
193 PLUGIN_GET_CLASS_MAP_PROC_NAME));
195 if (getWidgetEntityMapProcPtr) {
196 rawEntityList = (*getWidgetEntityMapProcPtr)();
199 static_cast<const js_entity_definition_t *>(dlsym(dlHandle,
200 PLUGIN_CLASS_MAP_NAME));
203 if (rawEntityList == NULL) {
205 LogError("Failed to read class name" << filename.Filename());
206 ThrowMsg(Exceptions::PluginLibraryError, "Library error");
209 if (!m_dataFromConfigXML) {
210 on_widget_init_proc *onWidgetInitProc =
211 reinterpret_cast<on_widget_init_proc *>(
212 dlsym(dlHandle, PLUGIN_WIDGET_INIT_PROC_NAME));
214 if (NULL == onWidgetInitProc) {
216 LogError("Failed to read onWidgetInit symbol" << filename.Filename());
217 ThrowMsg(Exceptions::PluginLibraryError, "Library error");
220 // obtain feature -> dev-cap mapping
221 feature_mapping_interface_t mappingInterface = { NULL, NULL, NULL };
222 (*onWidgetInitProc)(&mappingInterface);
224 if (!mappingInterface.featGetter || !mappingInterface.release ||
225 !mappingInterface.dcGetter)
227 LogError("Failed to obtain mapping interface from .so");
228 ThrowMsg(Exceptions::PluginLibraryError, "Library error");
231 feature_mapping_t* devcapMapping = mappingInterface.featGetter();
233 LogDebug("Getting mapping from features to device capabilities");
235 for (size_t i = 0; i < devcapMapping->featuresCount; ++i) {
236 PluginMetafileData::Feature feature;
237 feature.m_name = devcapMapping->features[i].feature_name;
239 LogDebug("Feature: " << feature.m_name);
241 const devcaps_t* dc =
242 mappingInterface.dcGetter(
244 devcapMapping->features[i].
247 LogDebug("device=cap: " << dc);
250 LogDebug("devcaps count: " << dc->devCapsCount);
252 for (size_t j = 0; j < dc->devCapsCount; ++j) {
253 LogDebug("devcap: " << dc->deviceCaps[j]);
254 feature.m_deviceCapabilities.insert(dc->deviceCaps[j]);
258 m_pluginInfo.m_featureContainer.insert(feature);
261 mappingInterface.release(devcapMapping);
264 m_libraryObjects = PluginObjectsPtr(new PluginObjects());
265 const js_entity_definition_t *rawEntityListIterator = rawEntityList;
268 LogInfo("##### Plugin: " << filename.Filename() << " supports new plugin API");
271 while (rawEntityListIterator->parent_name != NULL &&
272 rawEntityListIterator->object_name != NULL)
274 LogInfo("##### [" << rawEntityListIterator->object_name << "]: ");
275 LogInfo("##### Parent: " << rawEntityListIterator->parent_name);
278 m_libraryObjects->addObjects(rawEntityListIterator->parent_name,
279 rawEntityListIterator->object_name);
281 ++rawEntityListIterator;
285 if (dlclose(dlHandle) != 0) {
286 LogError("Cannot close plugin handle");
288 LogDebug("Library is unloaded");
292 LogDebug("Library successfuly loaded and parsed");
294 SET_PLUGIN_INSTALL_PROGRESS(LOADING_LIBRARY, "Library loaded and analyzed");
297 void PluginInstallTask::stepRegisterPlugin()
299 LogInfo("Plugin installation: step register Plugin");
302 PluginDAO::registerPlugin(m_pluginInfo, m_context->pluginFilePath.Fullpath());
304 SET_PLUGIN_INSTALL_PROGRESS(REGISTER_PLUGIN, "Plugin registered");
307 void PluginInstallTask::stepRegisterFeatures()
309 LogInfo("Plugin installation: step register features");
311 FOREACH(it, m_pluginInfo.m_featureContainer)
313 LogError("PluginHandle: " << m_pluginHandle);
314 FeatureDAO::RegisterFeature(*it, m_pluginHandle);
316 SET_PLUGIN_INSTALL_PROGRESS(REGISTER_FEATURES, "Features registered");
319 void PluginInstallTask::stepRegisterPluginObjects()
321 LogInfo("Plugin installation: step register objects");
323 DISABLE_IF_PLUGIN_WITHOUT_LIB()
325 //register implemented objects
326 PluginObjects::ObjectsPtr objects =
327 m_libraryObjects->getImplementedObject();
329 FOREACH(it, *objects)
331 PluginDAO::registerPluginImplementedObject(*it, m_pluginHandle);
334 //register requiredObjects
335 objects = m_libraryObjects->getDependentObjects();
337 FOREACH(it, *objects)
339 if (m_libraryObjects->hasObject(*it)) {
340 LogDebug("Dependency from the same library. ignored");
344 PluginDAO::registerPluginRequiredObject(*it, m_pluginHandle);
347 SET_PLUGIN_INSTALL_PROGRESS(REGISTER_OBJECTS, "Plugin Objects registered");
350 void PluginInstallTask::stepResolvePluginDependencies()
352 LogInfo("Plugin installation: step resolve dependencies ");
354 //DISABLE_IF_PLUGIN_WITHOUT_LIB
355 if (m_pluginInfo.m_libraryName.empty()) {
356 PluginDAO::setPluginInstallationStatus(
359 INSTALLATION_COMPLETED);
360 //Installation completed
361 m_context->pluginHandle = m_pluginHandle;
362 m_context->installationCompleted = true;
363 LogWarning("Plugin without library.");
367 PluginHandleSetPtr handles = PluginHandleSetPtr(new PluginHandleSet);
369 DbPluginHandle handle = INVALID_PLUGIN_HANDLE;
371 //register requiredObjects
372 FOREACH(it, *(m_libraryObjects->getDependentObjects()))
374 if (m_libraryObjects->hasObject(*it)) {
375 LogDebug("Dependency from the same library. ignored");
379 handle = PluginDAO::getPluginHandleForImplementedObject(*it);
380 if (handle == INVALID_PLUGIN_HANDLE) {
381 LogError("Library implementing: " << *it << " NOT FOUND");
382 PluginDAO::setPluginInstallationStatus(
384 PluginDAO::INSTALLATION_WAITING);
388 handles->insert(handle);
391 PluginDAO::registerPluginLibrariesDependencies(m_pluginHandle, handles);
393 PluginDAO::setPluginInstallationStatus(m_pluginHandle,
394 PluginDAO::INSTALLATION_COMPLETED);
396 //Installation completed
397 m_context->pluginHandle = m_pluginHandle;
398 m_context->installationCompleted = true;
400 SET_PLUGIN_INSTALL_PROGRESS(RESOLVE_DEPENDENCIES, "Dependencies resolved");
403 #undef SET_PLUGIN_INSTALL_PROGRESS
405 } //namespace PluginInstall