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 if (updateMode == WRT_WIM_POLICY_DIRECTORY_FORCE_INSTALL)
66 return WidgetUpdateMode::PolicyDirectoryForceInstall;
68 WidgetUpdateMode::Type result = WidgetUpdateMode::Zero;
69 if (updateMode & WRT_WIM_NOT_INSTALLED) {
70 result = result | WidgetUpdateMode::NotInstalled;
73 if (updateMode & WRT_WIM_INCOMING_VERSION_NOT_STD) {
74 result = result | WidgetUpdateMode::IncomingVersionNotStd;
77 if (updateMode & WRT_WIM_EXISTING_VERSION_NOT_STD) {
78 result = result | WidgetUpdateMode::ExistingVersionNotStd;
81 if (updateMode & WRT_WIM_BOTH_VERSIONS_NOT_STD) {
82 result = result | WidgetUpdateMode::BothVersionsNotStd;
85 if (updateMode & WRT_WIM_EXISTING_VERSION_OLDER) {
86 result = result | WidgetUpdateMode::ExistingVersionOlder;
89 if (updateMode & WRT_WIM_EXISTING_VERSION_EQUAL) {
90 result = result | WidgetUpdateMode::ExistingVersionEqual;
93 if (updateMode & WRT_WIM_EXISTING_VERSION_NEWER) {
94 result = result | WidgetUpdateMode::ExistingVersionNewer;
100 const char PLUGIN_INSTALL_SEMAPHORE[] = "/.wrt_plugin_install_lock";
101 static int wrt_count_plugin;
103 static std::string cutOffFileName(const std::string& path)
105 size_t found = path.find_last_of("/");
106 if (found == std::string::npos) {
109 return path.substr(0, found);
113 static bool checkPath(const std::string& path)
116 if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
119 LogError("Cannot access directory [ " << path << " ]");
123 static bool checkPaths()
126 if_ok &= (checkPath(cutOffFileName(
127 GlobalConfig::GetWrtDatabaseFilePath())));
130 "Path <" << GlobalConfig::GetWrtDatabaseFilePath() <<
131 "> does not exist.");
134 if_ok &= (checkPath(GlobalConfig::GetDevicePluginPath()));
137 "Path <" << GlobalConfig::GetDevicePluginPath() <<
138 "> does not exist.");
141 if_ok &= (checkPath(GlobalConfig::GetUserInstalledWidgetPath()));
144 "Path <" << GlobalConfig::GetUserInstalledWidgetPath() <<
145 "> does not exist.");
148 if_ok &= (checkPath(GlobalConfig::GetUserPreloadedWidgetPath()));
151 "Path <" << GlobalConfig::GetUserPreloadedWidgetPath() <<
152 "> does not exist.");
157 void plugin_install_status_cb(WrtErrStatus status,
162 wrt_plugin_data *plugin_data = static_cast<wrt_plugin_data*>(userparam);
164 if (--wrt_count_plugin < 1) {
165 LogDebug("All plugins installation completed");
167 LogDebug("Call SetAllinstallpluginsCallback");
168 plugin_data->plugin_installed_cb(plugin_data->user_data);
171 if (status == WRT_SUCCESS) {
173 "plugin installation is successful: " <<
174 plugin_data->plugin_path);
178 LogError("Fail to install plugin : " << plugin_data->plugin_path);
181 case WRT_PLUGIN_INSTALLER_ERROR_WRONG_PATH:
182 LogError("Failed : Plugin install path is wrong");
184 case WRT_PLUGIN_INSTALLER_ERROR_METAFILE:
185 LogError("Failed : Plugin Metafile Error");
187 case WRT_PLUGIN_INSTALLER_ERROR_ALREADY_INSTALLED:
188 LogError("Failed : This Plugin is already installed");
190 case WRT_PLUGIN_INSTALLER_ERROR_LIBRARY_ERROR:
191 LogError("Failed : Library Error. Missing symbol or structures");
193 case WRT_PLUGIN_INSTALLER_ERROR_WAITING:
194 LogError("Failed : Waiting for plugin dependencies");
196 case WRT_PLUGIN_INSTALLER_ERROR_LOCK:
197 LogError("Failed : Lock Error");
199 case WRT_PLUGIN_INSTALLER_ERROR_UNKNOWN:
200 LogError("Failed : Unkown Error");
207 void plugin_install_progress_cb(float percent,
208 const char* description,
211 char *plugin_path = static_cast<char*>(userdata);
213 LogInfo("Install plugin : " << plugin_path <<
214 ", Progress : " << percent <<
215 ", Description : " << description);
218 EXPORT_API int wrt_installer_init(void *userdata,
219 WrtInstallerInitCallback callback)
222 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
226 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
227 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
229 // Touch InstallerController Singleton
230 InstallerMainThreadSingleton::Instance().TouchArchitecture();
235 callback(WRT_ERROR_NO_PATH, userdata);
240 // Initialize ValidationCore - this must be done before AttachDatabases
241 ValidationCore::VCoreInit(
242 std::string(GlobalConfig::GetFingerprintListFile()),
243 std::string(GlobalConfig::GetFingerprintListSchema()),
244 std::string(GlobalConfig::GetVCoreDatabaseFilePath()));
246 InstallerMainThreadSingleton::Instance().AttachDatabases();
248 //checking for correct DB version
249 // if (!WrtDB::WrtDatabase::CheckTableExist(DB_CHECKSUM_STR)) {
250 // LogError("WRONG VERSION OF WRT DATABASE");
251 // Assert(false && "WRONG VERSION OF WRT DATABASE");
254 LogWarning("Database check not implemented!");
256 LogInfo("Prepare libxml2 to work in multithreaded program.");
259 // Initialize Language Subtag registry
260 LanguageSubtagRstTreeSingleton::Instance().Initialize();
263 CONTROLLER_POST_SYNC_EVENT(
264 Logic::InstallerController,
265 InstallerControllerEvents::
268 // Install deferred widget packages
269 CONTROLLER_POST_EVENT(
270 Logic::InstallerController,
271 InstallerControllerEvents::
272 InstallDeferredWidgetPackagesEvent());
275 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
276 callback(WRT_SUCCESS, userdata);
279 catch (const DPL::Exception& ex)
281 LogError("Internal Error during Init:");
282 DPL::Exception::DisplayKnownException(ex);
284 callback(WRT_ERROR_INTERNAL, userdata);
292 EXPORT_API void wrt_installer_shutdown()
296 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
298 // Installer termination
299 CONTROLLER_POST_SYNC_EVENT(
300 Logic::InstallerController,
301 InstallerControllerEvents::
304 InstallerMainThreadSingleton::Instance().DetachDatabases();
306 // This must be done after DetachDatabase
307 ValidationCore::VCoreDeinit();
309 // Global deinit check
310 LogInfo("Cleanup libxml2 global values.");
314 catch (const DPL::Exception& ex)
316 LogError("Internal Error during Shutdown:");
317 DPL::Exception::DisplayKnownException(ex);
321 EXPORT_API void wrt_install_widget(const char *path,
323 WrtInstallerStatusCallback status_cb,
324 WrtProgressCallback progress_cb,
325 wrt_widget_update_mode_t update_mode,
329 UNHANDLED_EXCEPTION_HANDLER_BEGIN
331 LogInfo("[WRT-API] INSTALL WIDGET: " << path);
332 // Post installation event
333 CONTROLLER_POST_EVENT(
334 Logic::InstallerController,
335 InstallerControllerEvents::InstallWidgetEvent(
336 path, WidgetInstallationStruct(
337 InstallerCallbacksTranslate::installFinishedCallback,
338 InstallerCallbacksTranslate::installProgressCallback,
339 new InstallerCallbacksTranslate::StatusCallbackStruct(
340 userdata, status_cb, progress_cb),
341 translateWidgetUpdateMode(update_mode),
345 UNHANDLED_EXCEPTION_HANDLER_END
348 EXPORT_API void wrt_uninstall_widget(const char * const pkg_name,
350 WrtInstallerStatusCallback status_cb,
351 WrtProgressCallback progress_cb)
353 UNHANDLED_EXCEPTION_HANDLER_BEGIN
355 std::string widgetPkgName(pkg_name);
356 LogInfo("[WRT-API] UNINSTALL WIDGET: " << widgetPkgName);
357 // Post uninstallation event
358 CONTROLLER_POST_EVENT(
359 Logic::InstallerController,
360 InstallerControllerEvents::UninstallWidgetEvent(
362 WidgetUninstallationStruct(
363 InstallerCallbacksTranslate::uninstallFinishedCallback,
364 InstallerCallbacksTranslate::installProgressCallback,
365 new InstallerCallbacksTranslate::StatusCallbackStruct(
366 userdata, status_cb, progress_cb))));
368 UNHANDLED_EXCEPTION_HANDLER_END
371 EXPORT_API void wrt_install_plugin(
372 const char *pluginDir,
374 WrtPluginInstallerStatusCallback status_cb,
375 WrtProgressCallback progress_cb)
377 UNHANDLED_EXCEPTION_HANDLER_BEGIN
379 LogInfo("[WRT-API] INSTALL PLUGIN: " << pluginDir);
380 //Private data for status callback
381 //Resource is free in pluginInstallFinishedCallback
382 InstallerCallbacksTranslate::PluginStatusCallbackStruct*
384 new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
385 user_param, status_cb, progress_cb);
387 CONTROLLER_POST_EVENT(
388 Logic::InstallerController,
389 InstallerControllerEvents::InstallPluginEvent(
390 std::string(pluginDir),
391 PluginInstallerStruct(
392 InstallerCallbacksTranslate::
393 pluginInstallFinishedCallback,
394 InstallerCallbacksTranslate::
395 installProgressCallback, callbackStruct)));
397 UNHANDLED_EXCEPTION_HANDLER_END
400 EXPORT_API void wrt_install_all_plugins(
401 WrtAllPluginInstalledCallback installed_cb,
404 UNHANDLED_EXCEPTION_HANDLER_BEGIN
406 std::string installRequest =
407 std::string(GlobalConfig::GetPluginInstallInitializerName());
409 LogDebug("Install new plugins");
412 DPL::Semaphore lock(PLUGIN_INSTALL_SEMAPHORE);
414 Catch(DPL::Semaphore::Exception::Base){
415 LogError("Failed to create installation lock");
421 if (-1 == stat(installRequest.c_str(), &tmp) ||
422 !S_ISREG(tmp.st_mode))
424 if (ENOENT == errno) {
425 LogDebug("Plugin installation not required");
427 LogDebug("Call SetAllinstallPluginCallback");
428 installed_cb(user_param);
430 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
433 LogWarning("Opening installation request file failed");
436 std::string PLUGIN_PATH =
437 std::string(GlobalConfig::GetDevicePluginPath());
440 dir = opendir(PLUGIN_PATH.c_str());
442 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
446 LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
447 struct dirent* libdir;
451 std::list<std::string> pluginsPaths;
453 while ((libdir = readdir(dir)) != 0) {
454 if (strcmp(libdir->d_name, ".") == 0 ||
455 strcmp(libdir->d_name, "..") == 0)
460 std::string path = PLUGIN_PATH;
462 path += libdir->d_name;
466 if (stat(path.c_str(), &tmp) == -1) {
467 LogError("Failed to open file" << path);
471 if (!S_ISDIR(tmp.st_mode)) {
472 LogError("Not a directory" << path);
476 pluginsPaths.push_back(path);
479 wrt_count_plugin = pluginsPaths.size();
481 FOREACH(it, pluginsPaths) {
482 wrt_plugin_data *plugin_data = new wrt_plugin_data;
484 plugin_data->plugin_installed_cb = installed_cb;
485 plugin_data->plugin_path = const_cast<char*>(it->c_str());
486 plugin_data->user_data = user_param;
489 it->c_str(), static_cast<void*>(plugin_data),
490 plugin_install_status_cb,
491 plugin_install_progress_cb);
494 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
495 LogError("Failed to close dir: " << PLUGIN_PATH << " with error: "
496 << DPL::GetErrnoString());
499 if (0 != unlink(installRequest.c_str())) {
500 LogError("Failed to remove file initializing plugin "
505 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
507 Catch(DPL::Semaphore::Exception::Base){
508 LogInfo("Failed to remove installation lock");
511 UNHANDLED_EXCEPTION_HANDLER_END
514 EXPORT_API int wrt_installer_init_for_tests(void *userdata,
515 WrtInstallerInitCallback callback)
518 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
522 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
523 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
525 // Touch InstallerController Singleton
526 InstallerMainThreadSingleton::Instance().
527 TouchArchitectureOnlyInstaller();
532 callback(WRT_ERROR_NO_PATH, userdata);
537 CONTROLLER_POST_SYNC_EVENT(
538 Logic::InstallerController,
539 InstallerControllerEvents::
543 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
544 callback(WRT_SUCCESS, userdata);
547 catch (const DPL::Exception& ex)
549 LogError("Internal Error during Init:");
550 DPL::Exception::DisplayKnownException(ex);
552 callback(WRT_ERROR_INTERNAL, userdata);
561 EXPORT_API void wrt_installer_shutdown_for_tests()
565 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
567 // Installer termination
568 CONTROLLER_POST_SYNC_EVENT(
569 Logic::InstallerController,
570 InstallerControllerEvents::
573 // Global deinit check
574 LogInfo("Cleanup libxml2 global values.");
577 catch (const DPL::Exception& ex)
579 LogError("Internal Error during Shutdown:");
580 DPL::Exception::DisplayKnownException(ex);
584 EXPORT_API WrtErrStatus wrt_get_widget_by_guid(std::string & pkgname,
585 const std::string guid)
589 LogInfo("[WRT-API] GETTING WIDGET PACKAGE NAME BY WidgetID : "
592 WidgetGUID widget_guid = DPL::FromUTF8String(guid);
593 WrtDB::WidgetDAOReadOnly dao(widget_guid);
594 pkgname = DPL::ToUTF8String(*dao.getPkgname());
597 catch (WidgetDAOReadOnly::Exception::WidgetNotExist)
599 LogError("Error package name is not found");
600 return WRT_ERROR_PKGNAME_NOT_FOUND;
602 catch (const DPL::Exception& ex)
604 LogError("Internal Error during get widget id by package name");
605 DPL::Exception::DisplayKnownException(ex);
606 return WRT_ERROR_INTERNAL;