[Release] wrt-installer_0.1.37
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / task_check.cpp
index e1eb8d4..bb6a73b 100644 (file)
  * @version 1.0
  * @brief   Header file for widget uninstall task check
  */
+#include <ctime>
 #include <dpl/sstream.h>
 #include <widget_uninstall/task_check.h>
 #include <widget_uninstall/job_widget_uninstall.h>
 #include <widget_uninstall/uninstaller_context.h>
+#include <widget_uninstall/widget_uninstall_errors.h>
 #include <dpl/wrt-dao-ro/global_config.h>
-#include <aul.h>
+#include <dpl/wrt-dao-ro/widget_dao_read_only.h>
+#include <app_manager.h>
 
 namespace Jobs {
 namespace WidgetUninstall {
@@ -36,31 +39,71 @@ TaskCheck::TaskCheck(UninstallerContext& context) :
 }
 
 TaskCheck::~TaskCheck()
-{
-}
+{}
 
 void TaskCheck::StepUninstallPreCheck()
 {
-    LogInfo("Uninstall check for widget Handle: " << m_context.widgetHandle);
+    LogInfo("Uninstall check for appid: " << m_context.tzAppid);
     //check if deferred
     //TODO if widget to be updated, then remove it from Deferred list?
 
-    DPL::OptionalString pkgName =
-            WrtDB::WidgetDAO(m_context.widgetHandle).getPkgname();
+    bool isRunning = false;
+    int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isRunning);
+    if (APP_MANAGER_ERROR_NONE != ret) {
+        LogError("Fail to get running state");
+        ThrowMsg(Exceptions::PlatformAPIFailure,
+                 "Fail to get widget state");
+    }
 
-    LogInfo("Widget model exists. Pkg name: " << pkgName);
-    if (aul_app_is_running(DPL::ToUTF8String(*pkgName).c_str())) {
-        LogError("Widget is not stopped. Cannot uninstall!");
-        //TODO different error
-        ThrowMsg(Exceptions::AlreadyUninstalling,
-                 "Widget is not stopped. Cannot uninstall!");
-        //TODO or defer uninstall?
+    if (true == isRunning) {
+        // get app_context for running application
+        // app_context must be released with app_context_destroy
+        app_context_h appCtx = NULL;
+        ret = app_manager_get_app_context(m_context.tzAppid.c_str(), &appCtx);
+        if (APP_MANAGER_ERROR_NONE != ret) {
+            LogError("Fail to get app_context");
+            ThrowMsg(Exceptions::AppIsRunning,
+                     "Widget is not stopped. Cannot uninstall!");
+        }
+
+        // terminate app_context_h
+        ret = app_manager_terminate_app(appCtx);
+        if (APP_MANAGER_ERROR_NONE != ret) {
+            LogError("Fail to terminate running application");
+            app_context_destroy(appCtx);
+            ThrowMsg(Exceptions::AppIsRunning,
+                     "Widget is not stopped. Cannot uninstall!");
+        } else {
+            app_context_destroy(appCtx);
+            // app_manager_terminate_app isn't sync API
+            // wait until application isn't running (50ms * 100)
+            bool isStillRunning = true;
+            int checkingloop = 100;
+            struct timespec duration = { 0, 50 * 1000 * 1000 };
+            while (--checkingloop >= 0) {
+                nanosleep(&duration, NULL);
+                int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isStillRunning);
+                if (APP_MANAGER_ERROR_NONE != ret) {
+                    LogError("Fail to get running state");
+                    ThrowMsg(Exceptions::PlatformAPIFailure,
+                             "Fail to get widget state");
+                }
+                if (!isStillRunning) {
+                    break;
+                }
+            }
+            if (isStillRunning) {
+                LogError("Fail to terminate running application");
+                ThrowMsg(Exceptions::AppIsRunning,
+                         "Widget is not stopped. Cannot uninstall!");
+            }
+            LogInfo("terminate application");
+        }
     }
 
-    LogInfo("Widget Can be uninstalled. Handle : " << m_context.widgetHandle);
-    m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_CHECK,
+    LogInfo("Widget Can be uninstalled. Pkgname : " << m_context.tzAppid);
+    m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
                                   "Uninstall pre-checking Finished");
 }
-
 } //namespace WidgetUninstall
 } //namespace Jobs