Merge branch 'tizen_5.5' into tizen
[platform/core/uifw/multi-assistant-service.git] / src / service_main.cpp
index a617088..656a91c 100644 (file)
@@ -728,7 +728,7 @@ pid_t CServiceMain::get_client_pid_by_appid(const char *appid)
                ret = mClientManager.find_client_pid_by_appid(std::string{appid});
        }
 
-       if (-1 != ret && !mApplicationManager.is_application_running(ret)) {
+       if (-1 != ret && !mApplicationManager.is_application_running(appid)) {
                MAS_LOGE("The PID for %s was %d, but it seems to be terminated", appid, ret);
                on_deinitialize(ret);
                ret = -1;
@@ -950,7 +950,8 @@ int CServiceMain::prelaunch_default_assistant()
        if (prelaunch_mode && *prelaunch_mode) {
                const char *default_assistant = NULL;
                if (0 == mServicePlugin.get_default_assistant(&default_assistant)) {
-                       if (!(mApplicationManager.is_application_running(default_assistant))) {
+                       if (default_assistant &&
+                               !(mApplicationManager.is_application_running(default_assistant))) {
                                MAS_LOGD("prelaunching default_assistant_appid : %s", default_assistant);
                                launch_client_by_appid(default_assistant, CLIENT_LAUNCH_MODE_PRELAUNCH);
                        }
@@ -1231,6 +1232,7 @@ static void _package_manager_event_cb(const char *type, const char *package, pac
        pkgmgrinfo_pkginfo_h handle = NULL;
        static bool in_progress = false;
        bool should_exit = false;
+       bool pkginfo_found = true;
 
        if (!package || !type)
                return;
@@ -1241,7 +1243,8 @@ static void _package_manager_event_cb(const char *type, const char *package, pac
                return;
 
        if (PACKAGE_MANAGER_EVENT_STATE_STARTED != event_state &&
-               PACKAGE_MANAGER_EVENT_STATE_COMPLETED != event_state)
+               PACKAGE_MANAGER_EVENT_STATE_COMPLETED != event_state &&
+               PACKAGE_MANAGER_EVENT_STATE_FAILED != event_state)
                return;
 
        bool user = false;
@@ -1254,17 +1257,29 @@ static void _package_manager_event_cb(const char *type, const char *package, pac
                user = true;
                ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package, uid, &handle);
                if (ret != PMINFO_R_OK || NULL == handle) {
-                       MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid());
-                       return;
+                       MAS_LOGW("Failed to call pkgmgrinfo_pkginfo_get_pkginfo & get_usr_pkginfo(\"%s\",~) returned %d, uid : %d", package, ret, getuid ());
+                       pkginfo_found = false;
                }
        }
 
-       if (user) {
-               /* Try to get in user packages */
-               pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret, uid);
-       }  else {
-               /* Try to get in global packages */
-               pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret);
+       if (pkginfo_found) {
+               if (user) {
+                       /* Try to get in user packages */
+                       pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret, uid);
+               }  else {
+                       /* Try to get in global packages */
+                       pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP, pkg_app_list_cb, (void *)&ret);
+               }
+       } else {
+               /* Even if we failed acquiring the pkginfo, proceed if we're uninstalling
+                       since at the time of uninstall completion, pkginfo would not exist */
+               if (in_progress) {
+                       if (PACKAGE_MANAGER_EVENT_TYPE_UNINSTALL == event_type &&
+                               (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state ||
+                               PACKAGE_MANAGER_EVENT_STATE_FAILED == event_state)) {
+                               ret = 1;
+                       }
+               }
        }
        if (1 == ret) {
                if (PACKAGE_MANAGER_EVENT_STATE_STARTED == event_state) {
@@ -1277,8 +1292,10 @@ static void _package_manager_event_cb(const char *type, const char *package, pac
                                        MAS_LOGE("service_main is NULL");
                                }
                        }
-               } else if (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state) {
-                       MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_COMPLETED event : %d", event_type);
+               } else if (PACKAGE_MANAGER_EVENT_STATE_COMPLETED == event_state ||
+                       PACKAGE_MANAGER_EVENT_STATE_FAILED == event_state) {
+                       MAS_LOGI("processing PACKAGE_MANAGER_EVENT_STATE_COMPLETED/FAILED event : %d %d",
+                               event_state, event_type);
                        if (false == in_progress) {
                                if (service_main) {
                                        service_main->deinitialize_service_plugin();
@@ -1299,7 +1316,7 @@ static void _package_manager_event_cb(const char *type, const char *package, pac
                }
        }
 
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       if (handle) pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
 
        if (should_exit) {
                LOGI("Now restarting multi-assistant-service for reloading updated modules");
@@ -1312,7 +1329,7 @@ static void _package_manager_event_cb(const char *type, const char *package, pac
 bool CServiceMain::app_create(void *data)
 {
        // Todo: add your code here.
-       MAS_LOGD("[Enter] Service app create");
+       MAS_LOGI("[ENTER] Service app create");
 
        g_service_main = this;
 
@@ -1359,6 +1376,7 @@ bool CServiceMain::app_create(void *data)
                }
        }
 
+       MAS_LOGI("[END] Service app create");
        return true;
 }