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 <libxml/parser.h>
36 #include <wrt_installer_api.h>
37 #include <installer_callbacks_translate.h>
38 #include <installer_controller.h>
39 #include <language_subtag_rst_tree.h>
40 #include <dpl/localization/localization_utils.h>
41 #include <dpl/wrt-dao-ro/global_config.h>
42 #include <dpl/utils/widget_version.h>
43 #include <dpl/popup/popup_manager.h>
44 #include <dpl/popup/popup_controller.h>
46 #include <dpl/localization/w3c_file_localization.h>
47 #include <dpl/wrt-dao-ro/WrtDatabase.h>
48 #include <vcore/VCore.h>
49 #include <installer_main_thread.h>
51 using namespace WrtDB;
60 #define EXPORT_API __attribute__((visibility("default")))
64 // inline WidgetUpdateMode::Type translateWidgetUpdateMode(
65 // wrt_widget_update_mode_t updateMode)
67 // 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 // void localizationSetting()
102 // char* lang = vconf_get_str(VCONFKEY_LANGSET);
104 // LogError("Cannot get locale settings from vconf");
106 // LogDebug("Language set to: " << lang);
108 // using namespace LocalizationUtils;
110 // LanguageTagsList list;
111 // list.push_back(DPL::FromUTF8String(lang));
112 // SetSystemLanguageTags(list);
114 // LogDebug("LanguageChanged to " << lang);
118 const char PLUGIN_INSTALL_SEMAPHORE[] = "/.wrt_plugin_install_lock";
119 static int wrt_count_plugin;
121 static std::string cutOffFileName(const std::string& path)
123 size_t found = path.find_last_of("/");
124 if (found == std::string::npos) {
127 return path.substr(0, found);
131 static bool checkPath(const std::string& path)
134 if (0 == stat(path.c_str(), &st) && S_ISDIR(st.st_mode)) {
137 LogError("Cannot access directory [ " << path << " ]");
141 static bool checkPaths()
144 if_ok &= (checkPath(cutOffFileName(
145 GlobalConfig::GetWrtDatabaseFilePath())));
148 "Path <" << GlobalConfig::GetWrtDatabaseFilePath() <<
149 "> does not exist.");
152 if_ok &= (checkPath(GlobalConfig::GetDevicePluginPath()));
155 "Path <" << GlobalConfig::GetDevicePluginPath() <<
156 "> does not exist.");
159 if_ok &= (checkPath(GlobalConfig::GetUserInstalledWidgetPath()));
162 "Path <" << GlobalConfig::GetUserInstalledWidgetPath() <<
163 "> does not exist.");
168 void plugin_install_status_cb(WrtErrStatus status,
173 wrt_plugin_data *plugin_data = static_cast<wrt_plugin_data*>(userparam);
175 if (--wrt_count_plugin < 1) {
176 LogDebug("All plugins installation completed");
178 LogDebug("Call SetAllinstallpluginsCallback");
179 plugin_data->plugin_installed_cb(plugin_data->user_data);
182 if (status == WRT_SUCCESS) {
184 "plugin installation is successful: " <<
185 plugin_data->plugin_path);
189 LogError("Fail to install plugin : " << plugin_data->plugin_path);
192 case WRT_PLUGIN_INSTALLER_ERROR_WRONG_PATH:
193 LogError("Failed : Plugin install path is wrong");
195 case WRT_PLUGIN_INSTALLER_ERROR_METAFILE:
196 LogError("Failed : Plugin Metafile Error");
198 case WRT_PLUGIN_INSTALLER_ERROR_ALREADY_INSTALLED:
199 LogError("Failed : This Plugin is already installed");
201 case WRT_PLUGIN_INSTALLER_ERROR_LIBRARY_ERROR:
202 LogError("Failed : Library Error. Missing symbol or structures");
204 case WRT_PLUGIN_INSTALLER_ERROR_WAITING:
205 LogError("Failed : Waiting for plugin dependencies");
207 case WRT_PLUGIN_INSTALLER_ERROR_LOCK:
208 LogError("Failed : Lock Error");
210 case WRT_PLUGIN_INSTALLER_ERROR_UNKNOWN:
211 LogError("Failed : Unkown Error");
218 void plugin_install_progress_cb(float percent,
219 const char* description,
222 char *plugin_path = static_cast<char*>(userdata);
224 LogInfo("Install plugin : " << plugin_path <<
225 ", Progress : " << percent <<
226 ", Description : " << description);
229 EXPORT_API int wrt_installer_init(void *userdata,
230 WrtInstallerInitCallback callback)
233 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
237 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
238 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
240 // Touch InstallerController Singleton
241 InstallerMainThreadSingleton::Instance().TouchArchitecture();
246 callback(WRT_ERROR_NO_PATH, userdata);
251 // Initialize ValidationCore - this must be done before AttachDatabases
252 ValidationCore::VCoreInit(
253 std::string(GlobalConfig::GetFingerprintListFile()),
254 std::string(GlobalConfig::GetFingerprintListSchema()),
255 std::string(GlobalConfig::GetVCoreDatabaseFilePath()));
257 InstallerMainThreadSingleton::Instance().AttachDatabases();
259 //checking for correct DB version
260 // if (!WrtDB::WrtDatabase::CheckTableExist(DB_CHECKSUM_STR)) {
261 // LogError("WRONG VERSION OF WRT DATABASE");
262 // Assert(false && "WRONG VERSION OF WRT DATABASE");
265 LogWarning("Database check not implemented!");
267 LogInfo("Prepare libxml2 to work in multithreaded program.");
270 // Initialize Language Subtag registry
271 LanguageSubtagRstTreeSingleton::Instance().Initialize();
272 // localizationSetting();
275 CONTROLLER_POST_SYNC_EVENT(
276 Logic::InstallerController,
277 InstallerControllerEvents::
280 // Install deferred widget packages
281 // CONTROLLER_POST_EVENT(
282 // Logic::InstallerController,
283 // InstallerControllerEvents::
284 // InstallDeferredWidgetPackagesEvent());
287 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
288 callback(WRT_SUCCESS, userdata);
291 catch (const DPL::Exception& ex)
293 LogError("Internal Error during Init:");
294 DPL::Exception::DisplayKnownException(ex);
296 callback(WRT_ERROR_INTERNAL, userdata);
304 EXPORT_API void wrt_installer_shutdown()
308 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
310 // Installer termination
311 CONTROLLER_POST_SYNC_EVENT(
312 Logic::InstallerController,
313 InstallerControllerEvents::
316 InstallerMainThreadSingleton::Instance().DetachDatabases();
318 // This must be done after DetachDatabase
319 ValidationCore::VCoreDeinit();
321 // Global deinit check
322 LogInfo("Cleanup libxml2 global values.");
326 catch (const DPL::Exception& ex)
328 LogError("Internal Error during Shutdown:");
329 DPL::Exception::DisplayKnownException(ex);
333 // EXPORT_API void wrt_install_widget(const char *path,
335 // WrtInstallerStatusCallback status_cb,
336 // WrtProgressCallback progress_cb,
337 // wrt_widget_update_mode_t update_mode,
340 // UNHANDLED_EXCEPTION_HANDLER_BEGIN
342 // LogInfo("[WRT-API] INSTALL WIDGET: " << path);
343 // // Post installation event
344 // CONTROLLER_POST_EVENT(
345 // Logic::InstallerController,
346 // InstallerControllerEvents::InstallWidgetEvent(
347 // path, WidgetInstallationStruct(
348 // InstallerCallbacksTranslate::installFinishedCallback,
349 // InstallerCallbacksTranslate::installProgressCallback,
350 // new InstallerCallbacksTranslate::StatusCallbackStruct(
351 // userdata, status_cb, progress_cb),
352 // translateWidgetUpdateMode(update_mode),
355 // UNHANDLED_EXCEPTION_HANDLER_END
358 // EXPORT_API void wrt_uninstall_widget(int widget_handle,
360 // WrtInstallerStatusCallback status_cb,
361 // WrtProgressCallback progress_cb)
363 // UNHANDLED_EXCEPTION_HANDLER_BEGIN
365 // LogInfo("[WRT-API] UNINSTALL WIDGET: " << widget_handle);
366 // // Post uninstallation event
367 // CONTROLLER_POST_EVENT(
368 // Logic::InstallerController,
369 // InstallerControllerEvents::UninstallWidgetEvent(
371 // WidgetUninstallationStruct(
372 // InstallerCallbacksTranslate::uninstallFinishedCallback,
373 // InstallerCallbacksTranslate::installProgressCallback,
374 // new InstallerCallbacksTranslate::StatusCallbackStruct(
375 // userdata, status_cb, progress_cb))));
377 // UNHANDLED_EXCEPTION_HANDLER_END
380 EXPORT_API void wrt_install_plugin(
381 const char *pluginDir,
383 WrtPluginInstallerStatusCallback status_cb,
384 WrtProgressCallback progress_cb)
386 UNHANDLED_EXCEPTION_HANDLER_BEGIN
388 LogInfo("[WRT-API] INSTALL PLUGIN: " << pluginDir);
389 //Private data for status callback
390 //Resource is free in pluginInstallFinishedCallback
391 InstallerCallbacksTranslate::PluginStatusCallbackStruct*
393 new InstallerCallbacksTranslate::PluginStatusCallbackStruct(
394 user_param, status_cb, progress_cb);
396 CONTROLLER_POST_EVENT(
397 Logic::InstallerController,
398 InstallerControllerEvents::InstallPluginEvent(
399 std::string(pluginDir),
400 PluginInstallerStruct(
401 InstallerCallbacksTranslate::
402 pluginInstallFinishedCallback,
403 InstallerCallbacksTranslate::
404 installProgressCallback, callbackStruct)));
406 UNHANDLED_EXCEPTION_HANDLER_END
409 EXPORT_API void wrt_install_all_plugins(
410 WrtAllPluginInstalledCallback installed_cb,
413 UNHANDLED_EXCEPTION_HANDLER_BEGIN
415 std::string installRequest =
416 std::string(GlobalConfig::GetPluginInstallInitializerName());
418 LogDebug("Install new plugins");
421 DPL::Semaphore lock(PLUGIN_INSTALL_SEMAPHORE);
423 Catch(DPL::Semaphore::Exception::Base){
424 LogError("Failed to create installation lock");
430 if (-1 == stat(installRequest.c_str(), &tmp) ||
431 !S_ISREG(tmp.st_mode))
433 if (ENOENT == errno) {
434 LogDebug("Plugin installation not required");
436 LogDebug("Call SetAllinstallPluginCallback");
437 installed_cb(user_param);
439 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
442 LogWarning("Opening installation request file failed");
445 std::string PLUGIN_PATH =
446 std::string(GlobalConfig::GetDevicePluginPath());
449 dir = opendir(PLUGIN_PATH.c_str());
451 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
455 LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
456 struct dirent* libdir;
460 std::list<std::string> pluginsPaths;
462 while ((libdir = readdir(dir)) != 0) {
463 if (strcmp(libdir->d_name, ".") == 0 ||
464 strcmp(libdir->d_name, "..") == 0)
469 std::string path = PLUGIN_PATH;
471 path += libdir->d_name;
475 if (stat(path.c_str(), &tmp) == -1) {
476 LogError("Failed to open file" << path);
480 if (!S_ISDIR(tmp.st_mode)) {
481 LogError("Not a directory" << path);
485 pluginsPaths.push_back(path);
488 wrt_count_plugin = pluginsPaths.size();
490 FOREACH(it, pluginsPaths) {
491 wrt_plugin_data *plugin_data = new wrt_plugin_data;
493 plugin_data->plugin_installed_cb = installed_cb;
494 plugin_data->plugin_path = const_cast<char*>(it->c_str());
495 plugin_data->user_data = user_param;
498 it->c_str(), static_cast<void*>(plugin_data),
499 plugin_install_status_cb,
500 plugin_install_progress_cb);
503 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
504 LogError("Failed to close dir: " << PLUGIN_PATH);
507 if (0 != unlink(installRequest.c_str())) {
508 LogError("Failed to remove file initializing plugin "
513 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
515 Catch(DPL::Semaphore::Exception::Base){
516 LogInfo("Failed to remove installation lock");
519 UNHANDLED_EXCEPTION_HANDLER_END
522 EXPORT_API int wrt_installer_init_for_tests(void *userdata,
523 WrtInstallerInitCallback callback)
526 DPL::Log::LogSystemSingleton::Instance().SetTag("WRT");
530 LogInfo("[WRT-API] INITIALIZING WRT INSTALLER...");
531 LogInfo("[WRT-API] BUILD: " << __TIMESTAMP__);
533 // Touch InstallerController Singleton
534 InstallerMainThreadSingleton::Instance().
535 TouchArchitectureOnlyInstaller();
540 callback(WRT_ERROR_NO_PATH, userdata);
545 CONTROLLER_POST_SYNC_EVENT(
546 Logic::InstallerController,
547 InstallerControllerEvents::
551 LogInfo("[WRT-API] WRT INSTALLER INITIALIZATION CALLBACK");
552 callback(WRT_SUCCESS, userdata);
555 catch (const DPL::Exception& ex)
557 LogError("Internal Error during Init:");
558 DPL::Exception::DisplayKnownException(ex);
560 callback(WRT_ERROR_INTERNAL, userdata);
569 EXPORT_API void wrt_installer_shutdown_for_tests()
573 LogInfo("[WRT-API] DEINITIALIZING WRT INSTALLER...");
575 // Installer termination
576 CONTROLLER_POST_SYNC_EVENT(
577 Logic::InstallerController,
578 InstallerControllerEvents::
581 // Global deinit check
582 LogInfo("Cleanup libxml2 global values.");
585 catch (const DPL::Exception& ex)
587 LogError("Internal Error during Shutdown:");
588 DPL::Exception::DisplayKnownException(ex);
592 EXPORT_API WrtErrStatus wrt_get_widget_by_pkgname(const std::string pkgname,
597 LogInfo("[WRT-API] GETTING WIDGET HANDLE BY PKG NAME : "
600 WidgetHandle handle = WidgetDAOReadOnly::getHandle(
601 DPL::FromASCIIString(pkgname));
602 *widget_handle = static_cast<int>(handle);
605 catch (WidgetDAOReadOnly::Exception::WidgetNotExist)
607 LogError("Error package name is not found");
608 return WRT_ERROR_PKGNAME_NOT_FOUND;
610 catch (const DPL::Exception& ex)
612 LogError("Internal Error during get widget id by package name");
613 DPL::Exception::DisplayKnownException(ex);
614 return WRT_ERROR_INTERNAL;
618 EXPORT_API WrtErrStatus wrt_get_widget_by_guid(const std::string guid,
623 LogInfo("[WRT-API] GETTING WIDGET HANDLE BY WidgetID : "
626 WidgetGUID widget_guid = DPL::FromUTF8String(guid);
627 WidgetHandle handle = WidgetDAOReadOnly::getHandle(widget_guid);
628 *widget_handle = static_cast<int>(handle);
631 catch (WidgetDAOReadOnly::Exception::WidgetNotExist)
633 LogError("Error package name is not found");
634 return WRT_ERROR_PKGNAME_NOT_FOUND;
636 catch (const DPL::Exception& ex)
638 LogError("Internal Error during get widget id by package name");
639 DPL::Exception::DisplayKnownException(ex);
640 return WRT_ERROR_INTERNAL;