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::GetUserInstalledWidgetPath()));
142 "Path <" << GlobalConfig::GetUserInstalledWidgetPath() <<
143 "> does not exist.");
148 void plugin_install_status_cb(WrtErrStatus status,
153 wrt_plugin_data *plugin_data = static_cast<wrt_plugin_data*>(userparam);
155 if (--wrt_count_plugin < 1) {
156 LogDebug("All plugins installation completed");
158 LogDebug("Call SetAllinstallpluginsCallback");
159 plugin_data->plugin_installed_cb(plugin_data->user_data);
162 if (status == WRT_SUCCESS) {
164 "plugin installation is successful: " <<
165 plugin_data->plugin_path);
169 LogError("Fail to install plugin : " << plugin_data->plugin_path);
172 case WRT_PLUGIN_INSTALLER_ERROR_WRONG_PATH:
173 LogError("Failed : Plugin install path is wrong");
175 case WRT_PLUGIN_INSTALLER_ERROR_METAFILE:
176 LogError("Failed : Plugin Metafile Error");
178 case WRT_PLUGIN_INSTALLER_ERROR_ALREADY_INSTALLED:
179 LogError("Failed : This Plugin is already installed");
181 case WRT_PLUGIN_INSTALLER_ERROR_LIBRARY_ERROR:
182 LogError("Failed : Library Error. Missing symbol or structures");
184 case WRT_PLUGIN_INSTALLER_ERROR_WAITING:
185 LogError("Failed : Waiting for plugin dependencies");
187 case WRT_PLUGIN_INSTALLER_ERROR_LOCK:
188 LogError("Failed : Lock Error");
190 case WRT_PLUGIN_INSTALLER_ERROR_UNKNOWN:
191 LogError("Failed : Unkown Error");
198 void plugin_install_progress_cb(float percent,
199 const char* description,
202 char *plugin_path = static_cast<char*>(userdata);
204 LogInfo("Install plugin : " << plugin_path <<
205 ", Progress : " << percent <<
206 ", Description : " << description);
209 EXPORT_API int wrt_installer_init(void *userdata,
210 WrtInstallerInitCallback callback)
213 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
217 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
218 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
220 // Touch InstallerController Singleton
221 InstallerMainThreadSingleton::Instance().TouchArchitecture();
226 callback(WRT_ERROR_NO_PATH, userdata);
231 // Initialize ValidationCore - this must be done before AttachDatabases
232 ValidationCore::VCoreInit(
233 std::string(GlobalConfig::GetFingerprintListFile()),
234 std::string(GlobalConfig::GetFingerprintListSchema()),
235 std::string(GlobalConfig::GetVCoreDatabaseFilePath()));
237 InstallerMainThreadSingleton::Instance().AttachDatabases();
239 //checking for correct DB version
240 // if (!WrtDB::WrtDatabase::CheckTableExist(DB_CHECKSUM_STR)) {
241 // LogError("WRONG VERSION OF WRT DATABASE");
242 // Assert(false && "WRONG VERSION OF WRT DATABASE");
245 LogWarning("Database check not implemented!");
247 LogInfo("Prepare libxml2 to work in multithreaded program.");
250 // Initialize Language Subtag registry
251 LanguageSubtagRstTreeSingleton::Instance().Initialize();
254 CONTROLLER_POST_SYNC_EVENT(
255 Logic::InstallerController,
256 InstallerControllerEvents::
259 // Install deferred widget packages
260 CONTROLLER_POST_EVENT(
261 Logic::InstallerController,
262 InstallerControllerEvents::
263 InstallDeferredWidgetPackagesEvent());
266 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
267 callback(WRT_SUCCESS, userdata);
270 catch (const DPL::Exception& ex)
272 LogError("Internal Error during Init:");
273 DPL::Exception::DisplayKnownException(ex);
275 callback(WRT_ERROR_INTERNAL, userdata);
283 EXPORT_API void wrt_installer_shutdown()
287 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
289 // Installer termination
290 CONTROLLER_POST_SYNC_EVENT(
291 Logic::InstallerController,
292 InstallerControllerEvents::
295 InstallerMainThreadSingleton::Instance().DetachDatabases();
297 // This must be done after DetachDatabase
298 ValidationCore::VCoreDeinit();
300 // Global deinit check
301 LogInfo("Cleanup libxml2 global values.");
305 catch (const DPL::Exception& ex)
307 LogError("Internal Error during Shutdown:");
308 DPL::Exception::DisplayKnownException(ex);
312 EXPORT_API void wrt_install_widget(const char *path,
314 WrtInstallerStatusCallback status_cb,
315 WrtProgressCallback progress_cb,
316 wrt_widget_update_mode_t update_mode,
319 UNHANDLED_EXCEPTION_HANDLER_BEGIN
321 LogInfo("[WRT-API] INSTALL WIDGET: " << path);
322 // Post installation event
323 CONTROLLER_POST_EVENT(
324 Logic::InstallerController,
325 InstallerControllerEvents::InstallWidgetEvent(
326 path, WidgetInstallationStruct(
327 InstallerCallbacksTranslate::installFinishedCallback,
328 InstallerCallbacksTranslate::installProgressCallback,
329 new InstallerCallbacksTranslate::StatusCallbackStruct(
330 userdata, status_cb, progress_cb),
331 translateWidgetUpdateMode(update_mode),
334 UNHANDLED_EXCEPTION_HANDLER_END
337 EXPORT_API void wrt_uninstall_widget(const char * const pkg_name,
339 WrtInstallerStatusCallback status_cb,
340 WrtProgressCallback progress_cb)
342 UNHANDLED_EXCEPTION_HANDLER_BEGIN
344 std::string widgetPkgName(pkg_name);
345 LogInfo("[WRT-API] UNINSTALL WIDGET: " << widgetPkgName);
346 // Post uninstallation event
347 CONTROLLER_POST_EVENT(
348 Logic::InstallerController,
349 InstallerControllerEvents::UninstallWidgetEvent(
351 WidgetUninstallationStruct(
352 InstallerCallbacksTranslate::uninstallFinishedCallback,
353 InstallerCallbacksTranslate::installProgressCallback,
354 new InstallerCallbacksTranslate::StatusCallbackStruct(
355 userdata, status_cb, progress_cb))));
357 UNHANDLED_EXCEPTION_HANDLER_END
360 EXPORT_API void wrt_install_plugin(
361 const char *pluginDir,
363 WrtPluginInstallerStatusCallback status_cb,
364 WrtProgressCallback progress_cb)
366 UNHANDLED_EXCEPTION_HANDLER_BEGIN
368 LogInfo("[WRT-API] INSTALL PLUGIN: " << pluginDir);
369 //Private data for status callback
370 //Resource is free in pluginInstallFinishedCallback
371 InstallerCallbacksTranslate::PluginStatusCallbackStruct*
373 new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
374 user_param, status_cb, progress_cb);
376 CONTROLLER_POST_EVENT(
377 Logic::InstallerController,
378 InstallerControllerEvents::InstallPluginEvent(
379 std::string(pluginDir),
380 PluginInstallerStruct(
381 InstallerCallbacksTranslate::
382 pluginInstallFinishedCallback,
383 InstallerCallbacksTranslate::
384 installProgressCallback, callbackStruct)));
386 UNHANDLED_EXCEPTION_HANDLER_END
389 EXPORT_API void wrt_install_all_plugins(
390 WrtAllPluginInstalledCallback installed_cb,
393 UNHANDLED_EXCEPTION_HANDLER_BEGIN
395 std::string installRequest =
396 std::string(GlobalConfig::GetPluginInstallInitializerName());
398 LogDebug("Install new plugins");
401 DPL::Semaphore lock(PLUGIN_INSTALL_SEMAPHORE);
403 Catch(DPL::Semaphore::Exception::Base){
404 LogError("Failed to create installation lock");
410 if (-1 == stat(installRequest.c_str(), &tmp) ||
411 !S_ISREG(tmp.st_mode))
413 if (ENOENT == errno) {
414 LogDebug("Plugin installation not required");
416 LogDebug("Call SetAllinstallPluginCallback");
417 installed_cb(user_param);
419 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
422 LogWarning("Opening installation request file failed");
425 std::string PLUGIN_PATH =
426 std::string(GlobalConfig::GetDevicePluginPath());
429 dir = opendir(PLUGIN_PATH.c_str());
431 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
435 LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
436 struct dirent* libdir;
440 std::list<std::string> pluginsPaths;
442 while ((libdir = readdir(dir)) != 0) {
443 if (strcmp(libdir->d_name, ".") == 0 ||
444 strcmp(libdir->d_name, "..") == 0)
449 std::string path = PLUGIN_PATH;
451 path += libdir->d_name;
455 if (stat(path.c_str(), &tmp) == -1) {
456 LogError("Failed to open file" << path);
460 if (!S_ISDIR(tmp.st_mode)) {
461 LogError("Not a directory" << path);
465 pluginsPaths.push_back(path);
468 wrt_count_plugin = pluginsPaths.size();
470 FOREACH(it, pluginsPaths) {
471 wrt_plugin_data *plugin_data = new wrt_plugin_data;
473 plugin_data->plugin_installed_cb = installed_cb;
474 plugin_data->plugin_path = const_cast<char*>(it->c_str());
475 plugin_data->user_data = user_param;
478 it->c_str(), static_cast<void*>(plugin_data),
479 plugin_install_status_cb,
480 plugin_install_progress_cb);
483 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
484 LogError("Failed to close dir: " << PLUGIN_PATH << " with error: "
485 << DPL::GetErrnoString());
488 if (0 != unlink(installRequest.c_str())) {
489 LogError("Failed to remove file initializing plugin "
494 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
496 Catch(DPL::Semaphore::Exception::Base){
497 LogInfo("Failed to remove installation lock");
500 UNHANDLED_EXCEPTION_HANDLER_END
503 EXPORT_API int wrt_installer_init_for_tests(void *userdata,
504 WrtInstallerInitCallback callback)
507 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
511 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
512 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
514 // Touch InstallerController Singleton
515 InstallerMainThreadSingleton::Instance().
516 TouchArchitectureOnlyInstaller();
521 callback(WRT_ERROR_NO_PATH, userdata);
526 CONTROLLER_POST_SYNC_EVENT(
527 Logic::InstallerController,
528 InstallerControllerEvents::
532 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
533 callback(WRT_SUCCESS, userdata);
536 catch (const DPL::Exception& ex)
538 LogError("Internal Error during Init:");
539 DPL::Exception::DisplayKnownException(ex);
541 callback(WRT_ERROR_INTERNAL, userdata);
550 EXPORT_API void wrt_installer_shutdown_for_tests()
554 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
556 // Installer termination
557 CONTROLLER_POST_SYNC_EVENT(
558 Logic::InstallerController,
559 InstallerControllerEvents::
562 // Global deinit check
563 LogInfo("Cleanup libxml2 global values.");
566 catch (const DPL::Exception& ex)
568 LogError("Internal Error during Shutdown:");
569 DPL::Exception::DisplayKnownException(ex);
573 EXPORT_API WrtErrStatus wrt_get_widget_by_guid(std::string & pkgname,
574 const std::string guid)
578 LogInfo("[WRT-API] GETTING WIDGET PACKAGE NAME BY WidgetID : "
581 WidgetGUID widget_guid = DPL::FromUTF8String(guid);
582 WrtDB::WidgetDAOReadOnly dao(widget_guid);
583 pkgname = DPL::ToUTF8String(*dao.getPkgname());
586 catch (WidgetDAOReadOnly::Exception::WidgetNotExist)
588 LogError("Error package name is not found");
589 return WRT_ERROR_PKGNAME_NOT_FOUND;
591 catch (const DPL::Exception& ex)
593 LogError("Internal Error during get widget id by package name");
594 DPL::Exception::DisplayKnownException(ex);
595 return WRT_ERROR_INTERNAL;