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 wrt_installer_api.cpp
18 * @author Chung Jihoon (jihoon.chung@samsung.com)
20 * @brief This file contains definitions of wrt installer api
25 #include <sys/types.h>
28 #include <dpl/exception.h>
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
31 #include <dpl/semaphore.h>
32 #include <dpl/sstream.h>
33 #include <dpl/errno_string.h>
34 #include <libxml/parser.h>
37 #include <wrt_installer_api.h>
38 #include <installer_callbacks_translate.h>
39 #include <installer_controller.h>
40 #include <language_subtag_rst_tree.h>
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/utils/widget_version.h>
44 #include <dpl/localization/w3c_file_localization.h>
45 #include <dpl/wrt-dao-ro/WrtDatabase.h>
46 #include <vcore/VCore.h>
47 #include <installer_main_thread.h>
49 using namespace WrtDB;
58 #define EXPORT_API __attribute__((visibility("default")))
62 inline WidgetUpdateMode::Type translateWidgetUpdateMode(
63 wrt_widget_update_mode_t updateMode)
65 WidgetUpdateMode::Type result = WidgetUpdateMode::Zero;
67 if (updateMode & WRT_WIM_NOT_INSTALLED) {
68 result = result | WidgetUpdateMode::NotInstalled;
71 if (updateMode & WRT_WIM_INCOMING_VERSION_NOT_STD) {
72 result = result | WidgetUpdateMode::IncomingVersionNotStd;
75 if (updateMode & WRT_WIM_EXISTING_VERSION_NOT_STD) {
76 result = result | WidgetUpdateMode::ExistingVersionNotStd;
79 if (updateMode & WRT_WIM_BOTH_VERSIONS_NOT_STD) {
80 result = result | WidgetUpdateMode::BothVersionsNotStd;
83 if (updateMode & WRT_WIM_EXISTING_VERSION_OLDER) {
84 result = result | WidgetUpdateMode::ExistingVersionOlder;
87 if (updateMode & WRT_WIM_EXISTING_VERSION_EQUAL) {
88 result = result | WidgetUpdateMode::ExistingVersionEqual;
91 if (updateMode & WRT_WIM_EXISTING_VERSION_NEWER) {
92 result = result | WidgetUpdateMode::ExistingVersionNewer;
98 const char PLUGIN_INSTALL_SEMAPHORE[] = "/.wrt_plugin_install_lock";
99 static int wrt_count_plugin;
101 static std::string cutOffFileName(const std::string& path)
103 size_t found = path.find_last_of("/");
104 if (found == std::string::npos) {
107 return path.substr(0, found);
111 static bool checkPath(const std::string& path)
114 if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
117 LogError("Cannot access directory [ " << path << " ]");
121 static bool checkPaths()
124 if_ok &= (checkPath(cutOffFileName(
125 GlobalConfig::GetWrtDatabaseFilePath())));
128 "Path <" << GlobalConfig::GetWrtDatabaseFilePath() <<
129 "> does not exist.");
132 if_ok &= (checkPath(GlobalConfig::GetDevicePluginPath()));
135 "Path <" << GlobalConfig::GetDevicePluginPath() <<
136 "> does not exist.");
139 if_ok &= (checkPath(GlobalConfig::GetFactoryInstalledWidgetPath()));
142 "Path <" << GlobalConfig::GetFactoryInstalledWidgetPath() <<
143 "> does not exist.");
146 if_ok &= (checkPath(GlobalConfig::GetUserInstalledWidgetPath()));
149 "Path <" << GlobalConfig::GetUserInstalledWidgetPath() <<
150 "> does not exist.");
155 void plugin_install_status_cb(WrtErrStatus status,
160 wrt_plugin_data *plugin_data = static_cast<wrt_plugin_data*>(userparam);
162 if (--wrt_count_plugin < 1) {
163 LogDebug("All plugins installation completed");
165 LogDebug("Call SetAllinstallpluginsCallback");
166 plugin_data->plugin_installed_cb(plugin_data->user_data);
169 if (status == WRT_SUCCESS) {
171 "plugin installation is successful: " <<
172 plugin_data->plugin_path);
176 LogError("Fail to install plugin : " << plugin_data->plugin_path);
179 case WRT_PLUGIN_INSTALLER_ERROR_WRONG_PATH:
180 LogError("Failed : Plugin install path is wrong");
182 case WRT_PLUGIN_INSTALLER_ERROR_METAFILE:
183 LogError("Failed : Plugin Metafile Error");
185 case WRT_PLUGIN_INSTALLER_ERROR_ALREADY_INSTALLED:
186 LogError("Failed : This Plugin is already installed");
188 case WRT_PLUGIN_INSTALLER_ERROR_LIBRARY_ERROR:
189 LogError("Failed : Library Error. Missing symbol or structures");
191 case WRT_PLUGIN_INSTALLER_ERROR_WAITING:
192 LogError("Failed : Waiting for plugin dependencies");
194 case WRT_PLUGIN_INSTALLER_ERROR_LOCK:
195 LogError("Failed : Lock Error");
197 case WRT_PLUGIN_INSTALLER_ERROR_UNKNOWN:
198 LogError("Failed : Unkown Error");
205 void plugin_install_progress_cb(float percent,
206 const char* description,
209 char *plugin_path = static_cast<char*>(userdata);
211 LogInfo("Install plugin : " << plugin_path <<
212 ", Progress : " << percent <<
213 ", Description : " << description);
216 EXPORT_API int wrt_installer_init(void *userdata,
217 WrtInstallerInitCallback callback)
220 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
224 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
225 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
227 // Touch InstallerController Singleton
228 InstallerMainThreadSingleton::Instance().TouchArchitecture();
233 callback(WRT_ERROR_NO_PATH, userdata);
238 // Initialize ValidationCore - this must be done before AttachDatabases
239 ValidationCore::VCoreInit(
240 std::string(GlobalConfig::GetFingerprintListFile()),
241 std::string(GlobalConfig::GetFingerprintListSchema()),
242 std::string(GlobalConfig::GetVCoreDatabaseFilePath()));
244 InstallerMainThreadSingleton::Instance().AttachDatabases();
246 //checking for correct DB version
247 // if (!WrtDB::WrtDatabase::CheckTableExist(DB_CHECKSUM_STR)) {
248 // LogError("WRONG VERSION OF WRT DATABASE");
249 // Assert(false && "WRONG VERSION OF WRT DATABASE");
252 LogWarning("Database check not implemented!");
254 LogInfo("Prepare libxml2 to work in multithreaded program.");
257 // Initialize Language Subtag registry
258 LanguageSubtagRstTreeSingleton::Instance().Initialize();
261 CONTROLLER_POST_SYNC_EVENT(
262 Logic::InstallerController,
263 InstallerControllerEvents::
266 // Install deferred widget packages
267 CONTROLLER_POST_EVENT(
268 Logic::InstallerController,
269 InstallerControllerEvents::
270 InstallDeferredWidgetPackagesEvent());
273 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
274 callback(WRT_SUCCESS, userdata);
277 catch (const DPL::Exception& ex)
279 LogError("Internal Error during Init:");
280 DPL::Exception::DisplayKnownException(ex);
282 callback(WRT_ERROR_INTERNAL, userdata);
290 EXPORT_API void wrt_installer_shutdown()
294 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
296 // Installer termination
297 CONTROLLER_POST_SYNC_EVENT(
298 Logic::InstallerController,
299 InstallerControllerEvents::
302 InstallerMainThreadSingleton::Instance().DetachDatabases();
304 // This must be done after DetachDatabase
305 ValidationCore::VCoreDeinit();
307 // Global deinit check
308 LogInfo("Cleanup libxml2 global values.");
312 catch (const DPL::Exception& ex)
314 LogError("Internal Error during Shutdown:");
315 DPL::Exception::DisplayKnownException(ex);
319 EXPORT_API void wrt_install_widget(const char *path,
321 WrtInstallerStatusCallback status_cb,
322 WrtProgressCallback progress_cb,
323 wrt_widget_update_mode_t update_mode,
326 UNHANDLED_EXCEPTION_HANDLER_BEGIN
328 LogInfo("[WRT-API] INSTALL WIDGET: " << path);
329 // Post installation event
330 CONTROLLER_POST_EVENT(
331 Logic::InstallerController,
332 InstallerControllerEvents::InstallWidgetEvent(
333 path, WidgetInstallationStruct(
334 InstallerCallbacksTranslate::installFinishedCallback,
335 InstallerCallbacksTranslate::installProgressCallback,
336 new InstallerCallbacksTranslate::StatusCallbackStruct(
337 userdata, status_cb, progress_cb),
338 translateWidgetUpdateMode(update_mode),
341 UNHANDLED_EXCEPTION_HANDLER_END
344 EXPORT_API void wrt_uninstall_widget(const char * const pkg_name,
346 WrtInstallerStatusCallback status_cb,
347 WrtProgressCallback progress_cb)
349 UNHANDLED_EXCEPTION_HANDLER_BEGIN
351 std::string widgetPkgName(pkg_name);
352 LogInfo("[WRT-API] UNINSTALL WIDGET: " << widgetPkgName);
353 // Post uninstallation event
354 CONTROLLER_POST_EVENT(
355 Logic::InstallerController,
356 InstallerControllerEvents::UninstallWidgetEvent(
358 WidgetUninstallationStruct(
359 InstallerCallbacksTranslate::uninstallFinishedCallback,
360 InstallerCallbacksTranslate::installProgressCallback,
361 new InstallerCallbacksTranslate::StatusCallbackStruct(
362 userdata, status_cb, progress_cb))));
364 UNHANDLED_EXCEPTION_HANDLER_END
367 EXPORT_API void wrt_install_plugin(
368 const char *pluginDir,
370 WrtPluginInstallerStatusCallback status_cb,
371 WrtProgressCallback progress_cb)
373 UNHANDLED_EXCEPTION_HANDLER_BEGIN
375 LogInfo("[WRT-API] INSTALL PLUGIN: " << pluginDir);
376 //Private data for status callback
377 //Resource is free in pluginInstallFinishedCallback
378 InstallerCallbacksTranslate::PluginStatusCallbackStruct*
380 new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
381 user_param, status_cb, progress_cb);
383 CONTROLLER_POST_EVENT(
384 Logic::InstallerController,
385 InstallerControllerEvents::InstallPluginEvent(
386 std::string(pluginDir),
387 PluginInstallerStruct(
388 InstallerCallbacksTranslate::
389 pluginInstallFinishedCallback,
390 InstallerCallbacksTranslate::
391 installProgressCallback, callbackStruct)));
393 UNHANDLED_EXCEPTION_HANDLER_END
396 EXPORT_API void wrt_install_all_plugins(
397 WrtAllPluginInstalledCallback installed_cb,
400 UNHANDLED_EXCEPTION_HANDLER_BEGIN
402 std::string installRequest =
403 std::string(GlobalConfig::GetPluginInstallInitializerName());
405 LogDebug("Install new plugins");
408 DPL::Semaphore lock(PLUGIN_INSTALL_SEMAPHORE);
410 Catch(DPL::Semaphore::Exception::Base){
411 LogError("Failed to create installation lock");
417 if (-1 == stat(installRequest.c_str(), &tmp) ||
418 !S_ISREG(tmp.st_mode))
420 if (ENOENT == errno) {
421 LogDebug("Plugin installation not required");
423 LogDebug("Call SetAllinstallPluginCallback");
424 installed_cb(user_param);
426 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
429 LogWarning("Opening installation request file failed");
432 std::string PLUGIN_PATH =
433 std::string(GlobalConfig::GetDevicePluginPath());
436 dir = opendir(PLUGIN_PATH.c_str());
438 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
442 LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
443 struct dirent* libdir;
447 std::list<std::string> pluginsPaths;
449 while ((libdir = readdir(dir)) != 0) {
450 if (strcmp(libdir->d_name, ".") == 0 ||
451 strcmp(libdir->d_name, "..") == 0)
456 std::string path = PLUGIN_PATH;
458 path += libdir->d_name;
462 if (stat(path.c_str(), &tmp) == -1) {
463 LogError("Failed to open file" << path);
467 if (!S_ISDIR(tmp.st_mode)) {
468 LogError("Not a directory" << path);
472 pluginsPaths.push_back(path);
475 wrt_count_plugin = pluginsPaths.size();
477 FOREACH(it, pluginsPaths) {
478 wrt_plugin_data *plugin_data = new wrt_plugin_data;
480 plugin_data->plugin_installed_cb = installed_cb;
481 plugin_data->plugin_path = const_cast<char*>(it->c_str());
482 plugin_data->user_data = user_param;
485 it->c_str(), static_cast<void*>(plugin_data),
486 plugin_install_status_cb,
487 plugin_install_progress_cb);
490 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
491 LogError("Failed to close dir: " << PLUGIN_PATH << " with error: "
492 << DPL::GetErrnoString());
495 if (0 != unlink(installRequest.c_str())) {
496 LogError("Failed to remove file initializing plugin "
501 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
503 Catch(DPL::Semaphore::Exception::Base){
504 LogInfo("Failed to remove installation lock");
507 UNHANDLED_EXCEPTION_HANDLER_END
510 EXPORT_API int wrt_installer_init_for_tests(void *userdata,
511 WrtInstallerInitCallback callback)
514 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
518 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
519 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
521 // Touch InstallerController Singleton
522 InstallerMainThreadSingleton::Instance().
523 TouchArchitectureOnlyInstaller();
528 callback(WRT_ERROR_NO_PATH, userdata);
533 CONTROLLER_POST_SYNC_EVENT(
534 Logic::InstallerController,
535 InstallerControllerEvents::
539 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
540 callback(WRT_SUCCESS, userdata);
543 catch (const DPL::Exception& ex)
545 LogError("Internal Error during Init:");
546 DPL::Exception::DisplayKnownException(ex);
548 callback(WRT_ERROR_INTERNAL, userdata);
557 EXPORT_API void wrt_installer_shutdown_for_tests()
561 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
563 // Installer termination
564 CONTROLLER_POST_SYNC_EVENT(
565 Logic::InstallerController,
566 InstallerControllerEvents::
569 // Global deinit check
570 LogInfo("Cleanup libxml2 global values.");
573 catch (const DPL::Exception& ex)
575 LogError("Internal Error during Shutdown:");
576 DPL::Exception::DisplayKnownException(ex);
580 EXPORT_API WrtErrStatus wrt_get_widget_by_guid(std::string & pkgname,
581 const std::string guid)
585 LogInfo("[WRT-API] GETTING WIDGET PACKAGE NAME BY WidgetID : "
588 WidgetGUID widget_guid = DPL::FromUTF8String(guid);
589 WrtDB::WidgetDAOReadOnly dao(widget_guid);
590 pkgname = DPL::ToUTF8String(*dao.getPkgname());
593 catch (WidgetDAOReadOnly::Exception::WidgetNotExist)
595 LogError("Error package name is not found");
596 return WRT_ERROR_PKGNAME_NOT_FOUND;
598 catch (const DPL::Exception& ex)
600 LogError("Internal Error during get widget id by package name");
601 DPL::Exception::DisplayKnownException(ex);
602 return WRT_ERROR_INTERNAL;