wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Package / PackageManager.cpp
index 556c460..52553ef 100644 (file)
@@ -15,7 +15,6 @@
 // limitations under the License.
 //
 
-#include <dlog.h>
 #include <glib.h>
 
 #include <JSTizenExceptionFactory.h>
@@ -39,6 +38,7 @@
 #include "PackageManager.h"
 #include "PackageInformation.h"
 #include "JSPackageInformation.h"
+#include <Logger.h>
 
 using namespace WrtDeviceApis::Commons;
 using namespace WrtDeviceApis::CommonsJavaScript;
@@ -93,6 +93,7 @@ PackageInfoEventCallback::PackageInfoEventCallback(CallbackUserData* oninstalled
        m_oninstalled = oninstalled;
        m_onupdated = onupdated;
        m_onuninstalled = onuninstalled;
+       m_handle_p = NULL;
 }
 
 PackageInfoEventCallback::~PackageInfoEventCallback()
@@ -122,6 +123,15 @@ CallbackUserData* PackageInfoEventCallback::getOnUninstalled()
        return m_onuninstalled;
 }
 
+pkgmgr_client** PackageInfoEventCallback::getEventHandler()
+{
+       return m_handle_p;
+}
+
+void PackageInfoEventCallback::setEventHandler(pkgmgr_client **handler)
+{
+       m_handle_p = handler;
+}
 
 PackageInstallEventCallback::PackageInstallEventCallback(JSContextRef globalCtx, CallbackUserData* onprogress, CallbackUserData* oncomplete, CallbackUserData* onerror)
 {
@@ -136,7 +146,7 @@ PackageInstallEventCallback::PackageInstallEventCallback(JSContextRef globalCtx,
 PackageInstallEventCallback::~PackageInstallEventCallback()
 {
        if (m_request_handle != NULL) {
-               LogDebug("destroy client handle");
+               LoggerD("destroy client handle");
                //package_manager_request_destroy(m_request_handle);
                package_manager_client_destroy(m_request_handle);
        }
@@ -183,35 +193,35 @@ package_manager_request_h PackageInstallEventCallback::getHandle()
 
 static int get_current_pkg_id(char** pkg_id)
 {
-       LogDebug("get_current_pkg_id enter");
+       LoggerD("get_current_pkg_id enter");
        int parent_pid = getppid();
-       LogDebug("parent pid : " << parent_pid);
+       LoggerD("parent pid : " << parent_pid);
        char* app_id = NULL;
        app_info_h handle;
 
        int ret = app_manager_get_app_id(parent_pid, &app_id);
        if ((ret != APP_MANAGER_ERROR_NONE) || (app_id == NULL)) {
-               LogDebug("Cannot get app id from pid : " << parent_pid);
+               LoggerD("Cannot get app id from pid : " << parent_pid);
                return ret;
        } else {
-               LogDebug("app_id : " << app_id);
+               LoggerD("app_id : " << app_id);
        }
 
        ret = app_manager_get_app_info(app_id, &handle);
        if (ret != APP_MANAGER_ERROR_NONE) {
-               LogDebug("Fail to get appinfo by " << app_id);
+               LoggerD("Fail to get appinfo by " << app_id);
                return ret;
        }
 
        ret = app_info_get_package(handle, pkg_id);
        if ((ret != APP_MANAGER_ERROR_NONE) || (*pkg_id == NULL)) {
-               LogDebug("Fail to get pkg_id by " << app_id);
+               LoggerD("Fail to get pkg_id by " << app_id);
                return ret;
        }
 
        ret = app_info_destroy(handle);
        if (ret != APP_MANAGER_ERROR_NONE) {
-               LogDebug("Fail to get destory appinfo");
+               LoggerD("Fail to get destory appinfo");
                return ret;
        }
 
@@ -220,7 +230,7 @@ static int get_current_pkg_id(char** pkg_id)
 
 static bool app_callback(package_info_app_component_type_e comp_type, const char *app_id, void *user_data)
 {
-       //LogDebug("app_callback enter : " << app_id);
+       //LoggerD("app_callback enter : " << app_id);
        PackageInformation* pkgInfo = (PackageInformation*)user_data;
        pkgInfo->m_appIds.push_back(app_id);
        return true;
@@ -228,7 +238,7 @@ static bool app_callback(package_info_app_component_type_e comp_type, const char
 
 static PackageInformation* create_pkg_info(pkgmgrinfo_pkginfo_h handle)
 {
-       //LogDebug("enter");
+       //LoggerD("enter");
 
        PackageInformation *pkgInfo = new PackageInformation();
 
@@ -247,7 +257,7 @@ static PackageInformation* create_pkg_info(pkgmgrinfo_pkginfo_h handle)
 
        ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &id);
        if ((ret != PMINFO_R_OK) || (id == NULL)) {
-               LogDebug("Fail to get id. return NULL.");
+               LoggerD("Fail to get id. return NULL.");
                delete pkgInfo;
                return NULL;
        } else {
@@ -256,63 +266,63 @@ static PackageInformation* create_pkg_info(pkgmgrinfo_pkginfo_h handle)
 
        ret = pkgmgrinfo_pkginfo_get_label(handle, &name);
        if ((ret != PMINFO_R_OK) || (name == NULL)) {
-               LogDebug("Fail to get labe");
+               LoggerD("Fail to get labe");
        } else {
                pkgInfo->m_name = name;
        }
 
        ret = pkgmgrinfo_pkginfo_get_icon(handle, &iconPath);
        if ((ret != PMINFO_R_OK) || (iconPath == NULL)) {
-               LogDebug("Fail to get iconPath");
+               LoggerD("Fail to get iconPath");
        } else {
                pkgInfo->m_iconPath = iconPath;
        }
 
        ret = pkgmgrinfo_pkginfo_get_version(handle, &version);
        if ((ret != PMINFO_R_OK) || (version == NULL)) {
-               LogDebug("Fail to get version");
+               LoggerD("Fail to get version");
        } else {
                pkgInfo->m_version = version;
        }
 
        ret = pkgmgrinfo_pkginfo_get_total_size(handle, &totalSize);
        if (ret != PMINFO_R_OK) {
-               LogDebug("Fail to get totalSize");
+               LoggerD("Fail to get totalSize");
        } else {
                pkgInfo->m_totalSize = totalSize;
        }
 
        ret = pkgmgrinfo_pkginfo_get_data_size(handle, &dataSize);
        if (ret != PMINFO_R_OK) {
-               LogDebug("Fail to get dataSize");
+               LoggerD("Fail to get dataSize");
        } else {
                pkgInfo->m_dataSize = dataSize;
        }
 
        ret = pkgmgrinfo_pkginfo_get_installed_time(handle, &lastModified);
        if (ret != PMINFO_R_OK) {
-               LogDebug("Fail to get lastModified");
+               LoggerD("Fail to get lastModified");
        } else {
                pkgInfo->m_lastModified = lastModified;
        }
 
        ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
        if ((ret != PMINFO_R_OK) || (type == NULL)) {
-               LogDebug("Fail to get type");
+               LoggerD("Fail to get type");
        } else {
                pkgInfo->m_type = type;
        }
 
        ret = pkgmgrinfo_pkginfo_get_author_name(handle, &author);
        if ((ret != PMINFO_R_OK) || (author == NULL)) {
-               LogDebug("Fail to get author");
+               LoggerD("Fail to get author");
        } else {
                pkgInfo->m_author = author;
        }
 
        ret = pkgmgrinfo_pkginfo_get_description(handle, &description);
        if ((ret != PMINFO_R_OK) || (description == NULL)) {
-               LogDebug("Fail to get description");
+               LoggerD("Fail to get description");
        } else {
                pkgInfo->m_description = description;
        }
@@ -321,19 +331,19 @@ static PackageInformation* create_pkg_info(pkgmgrinfo_pkginfo_h handle)
        
        ret = package_manager_get_package_info(id, &package_info);
        if (ret != PACKAGE_MANAGER_ERROR_NONE) {
-               LogDebug("Cannot create package info by : " << id);
+               LoggerD("Cannot create package info by : " << id);
                delete pkgInfo;
                return NULL;
        }
 
        ret = package_info_foreach_app_from_package(package_info, PACKAGE_INFO_ALLAPP, app_callback, (void*)pkgInfo);
        if (ret != PACKAGE_MANAGER_ERROR_NONE) {
-               LogDebug("failed while getting appids");
+               LoggerD("failed while getting appids");
        }
 
        ret = package_info_destroy(package_info);
        if (ret != PACKAGE_MANAGER_ERROR_NONE) {
-               LogDebug("Cannot destroy package info");
+               LoggerD("Cannot destroy package info");
        }       
 
        // REMARK: do not destroy handle. because handle is comes from outside!!
@@ -342,7 +352,7 @@ static PackageInformation* create_pkg_info(pkgmgrinfo_pkginfo_h handle)
 
 static gboolean getCompleteCB(void *data)
 {
-       LogDebug("entered");
+       LoggerD("entered");
 
        PackageInfoCallbackData *callback = (PackageInfoCallbackData *)data;
        JSContextRef context = callback->getContext();
@@ -368,7 +378,7 @@ static gboolean getCompleteCB(void *data)
 
                callback->callSuccessCallback(jsResult);
        } else {
-               LogDebug("Fail!!");
+               LoggerD("Fail!!");
                JSValueRef error = JSTizenExceptionFactory::makeErrorObject(callback->getContext(), JSTizenException::UNKNOWN_ERROR, "platform exception");
                callback->callErrorCallback(error);
        }
@@ -381,27 +391,27 @@ static gboolean getCompleteCB(void *data)
 
 static int get_package_list_cb(pkgmgrinfo_pkginfo_h handle, void *user_data)
 {
-       //LogDebug("entered");
+       //LoggerD("entered");
        PackageInfoCallbackData *callback = (PackageInfoCallbackData *)user_data;
        PackageInformation* pkgInfo = create_pkg_info(handle);
        if (pkgInfo != NULL) {
                callback->addPackageInfo(*pkgInfo);
                delete pkgInfo;
        } else {
-               LogError("Fail to get pkgInfo");
+               LoggerE("Fail to get pkgInfo");
        }
        return 0;
 }
 
 static void getThreadCB(void *data, Ecore_Thread *thread)
 {
-       LogDebug("entered");
+       LoggerD("entered");
 
        PackageInfoCallbackData *callback = (PackageInfoCallbackData *)data;
        
        int ret = pkgmgrinfo_pkginfo_get_list(get_package_list_cb, data);
        if (ret != PMINFO_R_OK) {
-               LogDebug("Fail to get package info");
+               LoggerD("Fail to get package info");
                callback->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::PlatformException);
        }
 
@@ -420,7 +430,7 @@ static void package_event_cb(app_manger_event_type_e event_type, const char *pac
                pkgmgrinfo_pkginfo_h handle;
                int ret = pkgmgrinfo_pkginfo_get_pkginfo(package, &handle);
                if (ret != PMINFO_R_OK) {
-                       LogDebug("fail to find pkg info with given pkg : " << package);
+                       LoggerD("fail to find pkg info with given pkg : " << package);
                        // Do not throw exception. No one can handle exception because this code is called from async callback.
                        //throw NotFoundException("Can't find given package");
                        break;
@@ -430,7 +440,7 @@ static void package_event_cb(app_manger_event_type_e event_type, const char *pac
                pkgmgrinfo_appinfo_destroy_appinfo(handle);
        
                if (pkgInfo == NULL) {
-                       LogDebug("Fail to get pkg info. skip callback call for pkgid " << package);
+                       LoggerD("Fail to get pkg info. skip callback call for pkgid " << package);
                } else {
                        CallbackUserData *callback = eventCB->getOnInstalled();
                        if (callback) {
@@ -440,7 +450,7 @@ static void package_event_cb(app_manger_event_type_e event_type, const char *pac
                break;
        }
        case APP_MANAGER_EVENT_UNINSTALLED: {
-               LogDebug("uninstall : " << package);
+               LoggerD("uninstall : " << package);
                CallbackUserData *callback = eventCB->getOnUninstalled();
                if (callback) {
                        Converter converter(callback->getContext());
@@ -461,7 +471,7 @@ static void package_event_cb(app_manger_event_type_e event_type, const char *pac
                pkgmgrinfo_appinfo_destroy_appinfo(handle);
        
                if (pkgInfo == NULL) {
-                       LogDebug("Fail to get pkg info. skip callback call for pkgid " << package);
+                       LoggerD("Fail to get pkg info. skip callback call for pkgid " << package);
                } else {
                        CallbackUserData *callback = eventCB->getOnUpdated();
                        if (callback) {
@@ -471,7 +481,7 @@ static void package_event_cb(app_manger_event_type_e event_type, const char *pac
                break;
        }
        default:
-               LogDebug("Fail!! Unknown event type is entered : " << event_type);
+               LoggerD("Fail!! Unknown event type is entered : " << event_type);
                break;
        }
 
@@ -484,6 +494,7 @@ static int app_list_changed_cb_broker(int id, const char *type, const char *pack
 
        if (!strcasecmp(key, "start"))
        {
+               LoggerD("start ");
                if (!strcasecmp(val, "install"))
                {
                        event_type = APP_MANAGER_EVENT_INSTALLED;
@@ -499,13 +510,24 @@ static int app_list_changed_cb_broker(int id, const char *type, const char *pack
        }
        else if (!strcasecmp(key, "end") && !strcasecmp(val, "ok"))
        {
+               LoggerD("end ");
                if (event_type >= 0)
                {
                        package_event_cb(event_type, package, data);
+
+                       // Check whether handler is freed in the callback function or not.
+                       // if freed, return error code to avoid iteration of callback function. (core platform side)
+                       PackageInfoEventCallback * callback = (PackageInfoEventCallback *)data;
+                       pkgmgr_client ** handler_p = callback->getEventHandler();
+                       if (*handler_p == NULL) {
+                               LoggerD("handler is NULL");
+                               return -1;
+                       }
+                       LoggerD("handler is not NULL");
                }
        }
 
-       return APP_MANAGER_ERROR_NONE;
+       return 0;
 }
 
 
@@ -516,7 +538,7 @@ void install_request_cb(int id, const char *type, const char *package,
                                package_manager_error_e error,
                                void *user_data)
 {
-       LogDebug("install_request_cb ");
+       LoggerD("install_request_cb ");
        PackageInstallEventCallback *callback = (PackageInstallEventCallback *)user_data;
        JSContextRef context = callback->getContext();;
 
@@ -527,7 +549,7 @@ void install_request_cb(int id, const char *type, const char *package,
                        callback->getOnComplete()->callSuccessCallback(converter.toJSValueRef(package));
                }
 
-               LogDebug("destroy client handle");
+               LoggerD("destroy client handle");
                // this api is not supported from platform.
                //package_manager_request_destroy(callback->getHandle());
                package_manager_client_destroy(callback->getHandle());
@@ -550,7 +572,7 @@ void install_request_cb(int id, const char *type, const char *package,
                        callback->getOnError()->callSuccessCallback(jsError);
                }
 
-               LogDebug("destroy client handle");
+               LoggerD("destroy client handle");
                // this api is not supported from platform.
                //package_manager_request_destroy(callback->getHandle());
                package_manager_client_destroy(callback->getHandle());
@@ -575,7 +597,7 @@ void install_request_cb(int id, const char *type, const char *package,
                if (callback->getOnError()) {
                        callback->getOnError()->callSuccessCallback(error);
                }
-               LogDebug("destroy client handle");
+               LoggerD("destroy client handle");
                // this api is not supported from platform.
                //package_manager_request_destroy(callback->getHandle());
                package_manager_client_destroy(callback->getHandle());
@@ -591,7 +613,7 @@ void install_request_cb(int id, const char *type, const char *package,
 
 void PackageManager::install(string pkgPath, PackageInstallEventCallback* callback)
 {
-       LogDebug("install");
+       LoggerD("install");
        int ret = 0;
        int id = 0;
        package_manager_request_h request_h;
@@ -611,7 +633,7 @@ void PackageManager::install(string pkgPath, PackageInstallEventCallback* callba
 
        ret = package_manager_request_install(request_h, pkgPath.c_str(), &id);
        if (ret != PACKAGE_MANAGER_ERROR_NONE) {
-               LogDebug("error : " << pkgPath);
+               LoggerD("error : " << pkgPath);
                if (errCallback) {
                        JSValueRef error = 
                                JSTizenExceptionFactory::makeErrorObject(globalCtx, JSTizenException::NOT_FOUND_ERROR, "Not proper file");
@@ -637,7 +659,7 @@ void PackageManager::install(string pkgPath, PackageInstallEventCallback* callba
 
 void PackageManager::uninstall(string pkgPath, PackageInstallEventCallback* callback)
 {
-       LogDebug("uninstall");
+       LoggerD("uninstall");
        int ret = 0;
        int id = 0;
        package_manager_request_h request_h;
@@ -658,7 +680,7 @@ void PackageManager::uninstall(string pkgPath, PackageInstallEventCallback* call
 
        ret = package_manager_request_uninstall(request_h, pkgPath.c_str(), &id);
        if (ret != PACKAGE_MANAGER_ERROR_NONE) {
-               LogDebug("error : " << pkgPath);
+               LoggerD("error : " << pkgPath);
                if (errCallback) {
                        JSValueRef error = 
                                JSTizenExceptionFactory::makeErrorObject(globalCtx, JSTizenException::NOT_FOUND_ERROR, "Not proper file");
@@ -693,7 +715,7 @@ PackageInformation* PackageManager::getPackageInfo(string pkgId)
 
                int ret = get_current_pkg_id(&pkg_id);
                if((ret != APP_MANAGER_ERROR_NONE) || (pkg_id == NULL)) {
-                       LogError("Can not get app id from current pid (" << ret << ")");
+                       LoggerE("Can not get app id from current pid (" << ret << ")");
                        throw NotFoundException("Can't find given package");
                }
                pkgId = pkg_id;
@@ -719,7 +741,7 @@ PackageInformation* PackageManager::getPackageInfo(string pkgId)
 
 void PackageManager::getPackagesInfo(PackageInfoCallbackData *user_data)
 {
-       LogDebug("entered");
+       LoggerD("entered");
 
        // getting info of all package from other thread. the result callback will be called on main thread
        ecore_thread_run(getThreadCB, NULL, NULL, user_data);
@@ -728,7 +750,7 @@ void PackageManager::getPackagesInfo(PackageInfoCallbackData *user_data)
 
 void PackageManager::setPackageInfoEventListener(PackageInfoEventCallback * eventCB)
 {
-       LogDebug("enter");
+       LoggerD("enter");
 
        if (m_manager_handle == NULL) {
                m_manager_handle = pkgmgr_client_new(PC_LISTENING);
@@ -737,15 +759,17 @@ void PackageManager::setPackageInfoEventListener(PackageInfoEventCallback * even
                }
        }
 
+       eventCB->setEventHandler(&m_manager_handle);
+
        pkgmgr_client_listen_status(m_manager_handle, app_list_changed_cb_broker, eventCB);
 }
 
 void PackageManager::unsetPackageInfoEventListener()
 {
-       LogDebug("enter");
+       LoggerD("enter");
 
        if (m_manager_handle == NULL) {
-               LogDebug("no package manager handle registered");
+               LoggerD("no package manager handle registered");
                return;
        }
 
@@ -754,19 +778,19 @@ void PackageManager::unsetPackageInfoEventListener()
 }
 
 PackageManager* PackageManager::getInstance(){
-       LogDebug("enter to getInstance()");
+       LoggerD("enter to getInstance()");
     static PackageManager instance;
     return &instance;
 }
 
 PackageManager::PackageManager()
 {
-       LogDebug("Create PackageManager");
+       LoggerD("Create PackageManager");
 }
 
 PackageManager::~PackageManager()
 {
-       LogDebug("Destroy PackageManager");
+       LoggerD("Destroy PackageManager");
 
        if (m_manager_handle != NULL) {
                pkgmgr_client_free(m_manager_handle);
@@ -780,7 +804,7 @@ PackageManager::~PackageManager()
 #if 0
 void PackageManager::move(string pkgPath, string target, PackageInstallEventCallback* callback)
 {
-       LogDebug("entered : " << pkgPath);
+       LoggerD("entered : " << pkgPath);
        int ret = 0;
        package_manager_move_type_e type = PACAKGE_MANAGER_REQUEST_MOVE_TO_INTERNAL;
 
@@ -813,13 +837,13 @@ void PackageManager::move(string pkgPath, string target, PackageInstallEventCall
 
 bool PackageManager::isInstalled(string pkgId)
 {
-       LogDebug("pkgId = " << pkgId);
+       LoggerD("pkgId = " << pkgId);
        package_info_h handle;
        int ret = 0;
        
        ret = package_manager_get_package_info(pkgId.c_str(), &handle);
        if (ret == PACKAGE_MANAGER_ERROR_NO_SUCH_PACKAGE) {
-               LogError("No patched package: " << pkgId);
+               LoggerE("No patched package: " << pkgId);
                return false;
        } else if (ret == PACKAGE_MANAGER_ERROR_NONE) {
                // TODO: Should I exception handling while destory handle? what should I do for that time?
@@ -832,14 +856,14 @@ bool PackageManager::isInstalled(string pkgId)
 
 string PackageManager::getPackageIdFromAppId(string appId)
 {
-       LogDebug("appId = " << appId);
+       LoggerD("appId = " << appId);
        char *pkg_id = NULL;
        string pkgId;
        int ret = 0;
        
        ret = package_manager_get_package_id_by_app_id(appId.c_str(), &pkg_id);
        if (ret != PACKAGE_MANAGER_ERROR_NONE) {
-               LogError("Error on get pkgId from appId : " << appId);
+               LoggerE("Error on get pkgId from appId : " << appId);
                ThrowMsg(NotFoundException, "Not found pkg");
        }