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>
32 #include "plugin_install_task.h"
33 #include "job_plugin_install.h"
34 #include "plugin_installer_errors.h"
35 #include "plugin_metafile_reader.h"
36 #include <dpl/wrt-dao-ro/global_config.h>
38 #include <wrt_common_types.h>
39 #include <dpl/wrt-dao-rw/feature_dao.h>
40 #include <dpl/wrt-dao-rw/plugin_dao.h>
41 #include "plugin_objects.h"
43 using namespace WrtDB;
46 const std::string DIRECTORY_SEPARATOR = std::string("/");
49 #define SET_PLUGIN_INSTALL_PROGRESS(step, desc) \
50 m_context->installerTask->UpdateProgress( \
51 PluginInstallerContext::step, desc);
54 namespace PluginInstall {
55 PluginInstallTask::PluginInstallTask(PluginInstallerContext *inCont) :
56 DPL::TaskDecl<PluginInstallTask>(this),
59 AddStep(&PluginInstallTask::stepCheckPluginPath);
60 AddStep(&PluginInstallTask::stepParseConfigFile);
61 AddStep(&PluginInstallTask::stepCheckIfAlreadyInstalled);
62 AddStep(&PluginInstallTask::stepLoadPluginLibrary);
63 AddStep(&PluginInstallTask::stepRegisterPlugin);
64 AddStep(&PluginInstallTask::stepRegisterFeatures);
65 AddStep(&PluginInstallTask::stepRegisterPluginObjects);
66 AddStep(&PluginInstallTask::stepResolvePluginDependencies);
68 SET_PLUGIN_INSTALL_PROGRESS(START, "Installation initialized");
71 PluginInstallTask::~PluginInstallTask()
75 void PluginInstallTask::stepCheckPluginPath()
77 LogInfo("Plugin installation: step CheckPluginPath");
81 if (-1 == stat(m_context->pluginFilePath.c_str(), &tmp)) {
82 ThrowMsg(Exceptions::PluginPathFailed,
83 "Stat function failed");
86 if (!S_ISDIR(tmp.st_mode)) {
87 ThrowMsg(Exceptions::PluginPathFailed,
91 SET_PLUGIN_INSTALL_PROGRESS(PLUGIN_PATH, "Path to plugin verified");
94 void PluginInstallTask::stepParseConfigFile()
96 LogInfo("Plugin installation: step parse config file");
98 std::string filename = m_context->pluginFilePath + DIRECTORY_SEPARATOR +
99 std::string(GlobalConfig::GetPluginMetafileName());
101 LogInfo("Plugin Config file::" << filename);
105 PluginMetafileReader reader;
106 reader.initialize(filename);
107 reader.read(m_pluginMetafile);
109 SET_PLUGIN_INSTALL_PROGRESS(PLUGIN_PATH, "Config file analyzed");
111 Catch(ValidationCore::ParserSchemaException::Base)
113 LogError("Error during file processing " << filename);
114 ThrowMsg(Exceptions::PluginMetafileFailed,
119 void PluginInstallTask::stepCheckIfAlreadyInstalled()
121 if (PluginDAO::isPluginInstalled(m_pluginMetafile.m_libraryName)) {
122 ThrowMsg(Exceptions::PluginAlreadyInstalled,
123 "Plugin already installed");
126 SET_PLUGIN_INSTALL_PROGRESS(PLUGIN_EXISTS_CHECK, "Check if plugin exist");
129 void PluginInstallTask::stepLoadPluginLibrary()
131 LogInfo("Plugin installation: step load library");
133 std::string filename = m_context->pluginFilePath + DIRECTORY_SEPARATOR +
134 m_pluginMetafile.m_libraryName;
136 LogDebug("Loading plugin: " << filename);
138 void *dlHandle = dlopen(filename.c_str(), RTLD_LAZY);
139 if (dlHandle == NULL ) {
141 "Failed to load plugin: " << filename <<
142 ". Reason: " << dlerror());
143 ThrowMsg(Exceptions::PluginLibraryError, "Library error");
146 const class_definition_t *rawClassList = NULL;
147 get_widget_class_map_proc *getWidgetClassMapProcPtr = NULL;
149 getWidgetClassMapProcPtr =
150 reinterpret_cast<get_widget_class_map_proc *>(dlsym(dlHandle,
151 PLUGIN_GET_CLASS_MAP_PROC_NAME));
153 if (getWidgetClassMapProcPtr) {
154 rawClassList = (*getWidgetClassMapProcPtr)();
157 static_cast<const class_definition_t *>(dlsym(dlHandle,
158 PLUGIN_CLASS_MAP_NAME));
161 if (rawClassList == NULL) {
163 LogError("Failed to read class name" << filename);
164 ThrowMsg(Exceptions::PluginLibraryError, "Library error");
167 m_libraryObjects = PluginObjectsPtr(new PluginObjects());
168 const class_definition_t *rawClassListIterator = rawClassList;
171 LogInfo("##### Plugin: " << filename << " supports new plugin API");
174 while (rawClassListIterator->parent_name != NULL &&
175 rawClassListIterator->object_name != NULL &&
176 rawClassListIterator->js_class_template != NULL) {
177 LogInfo("##### [" << rawClassListIterator->object_name << "]: ");
178 LogInfo("##### Parent: " << rawClassListIterator->parent_name);
181 m_libraryObjects->addObjects(rawClassListIterator->parent_name,
182 rawClassListIterator->object_name);
184 ++rawClassListIterator;
188 if (dlclose(dlHandle) != 0) {
189 LogError("Cannot close plugin handle");
191 LogDebug("Library is unloaded");
195 LogDebug("Library successfuly loaded and parsed");
197 SET_PLUGIN_INSTALL_PROGRESS(LOADING_LIBRARY, "Library loaded and analyzed");
198 //TODO unload library;
201 void PluginInstallTask::stepRegisterPlugin()
203 LogInfo("Plugin installation: step register Plugin");
206 PluginDAO::registerPlugin(m_pluginMetafile, m_context->pluginFilePath);
208 SET_PLUGIN_INSTALL_PROGRESS(REGISTER_PLUGIN, "Plugin registered");
211 void PluginInstallTask::stepRegisterFeatures()
213 LogInfo("Plugin installation: step register features");
215 FOREACH(it, m_pluginMetafile.m_featureContainer)
217 LogError("PluginHandle: " << m_pluginHandle);
218 FeatureDAO::RegisterFeature(*it, m_pluginHandle);
220 SET_PLUGIN_INSTALL_PROGRESS(REGISTER_FEATURES, "Features registered");
223 void PluginInstallTask::stepRegisterPluginObjects()
225 LogInfo("Plugin installation: step register objects");
227 //register implemented objects
228 PluginObjects::ObjectsPtr objects =
229 m_libraryObjects->getImplementedObject();
231 FOREACH(it, *objects)
233 PluginDAO::registerPluginImplementedObject(*it, m_pluginHandle);
236 //register requiredObjects
237 objects = m_libraryObjects->getDependentObjects();
239 FOREACH(it, *objects)
241 if (m_libraryObjects->hasObject(*it)) {
242 LogDebug("Dependency from the same library. ignored");
246 PluginDAO::registerPluginRequiredObject(*it, m_pluginHandle);
249 SET_PLUGIN_INSTALL_PROGRESS(REGISTER_OBJECTS, "Plugin Objects registered");
252 void PluginInstallTask::stepResolvePluginDependencies()
254 LogInfo("Plugin installation: step resolve dependencies ");
256 PluginHandleSetPtr handles = PluginHandleSetPtr(new PluginHandleSet);
258 DbPluginHandle handle = INVALID_PLUGIN_HANDLE;
260 //register requiredObjects
261 FOREACH(it, *(m_libraryObjects->getDependentObjects()))
263 if (m_libraryObjects->hasObject(*it)) {
264 LogDebug("Dependency from the same library. ignored");
268 handle = PluginDAO::getPluginHandleForImplementedObject(*it);
269 if (handle == INVALID_PLUGIN_HANDLE) {
270 LogError("Library implementing: " << *it << " NOT FOUND");
271 PluginDAO::setPluginInstallationStatus(
273 PluginDAO::INSTALLATION_WAITING);
277 handles->insert(handle);
280 PluginDAO::registerPluginLibrariesDependencies(m_pluginHandle, handles);
282 PluginDAO::setPluginInstallationStatus(m_pluginHandle,
283 PluginDAO::INSTALLATION_COMPLETED);
285 //Installation completed
286 m_context->pluginHandle = m_pluginHandle;
287 m_context->installationCompleted = true;
289 SET_PLUGIN_INSTALL_PROGRESS(RESOLVE_DEPENDENCIES, "Dependencies resolved");
292 #undef SET_PLUGIN_INSTALL_PROGRESS
294 } //namespace PluginInstall