Merge "Fixed crash when get detail info"
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / task_check.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    task_check.cpp
18  * @author  Pawel Sikorski(p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   Header file for widget uninstall task check
21  */
22 #include <time.h>
23 #include <dpl/sstream.h>
24 #include <widget_uninstall/task_check.h>
25 #include <widget_uninstall/job_widget_uninstall.h>
26 #include <widget_uninstall/uninstaller_context.h>
27 #include <widget_uninstall/widget_uninstall_errors.h>
28 #include <dpl/wrt-dao-ro/global_config.h>
29 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
30 #include <app_manager.h>
31
32 namespace Jobs {
33 namespace WidgetUninstall {
34 TaskCheck::TaskCheck(UninstallerContext& context) :
35     DPL::TaskDecl<TaskCheck>(this),
36     m_context(context)
37 {
38     AddStep(&TaskCheck::StepUninstallPreCheck);
39 }
40
41 TaskCheck::~TaskCheck()
42 {}
43
44 void TaskCheck::StepUninstallPreCheck()
45 {
46     LogInfo("Uninstall check for appid: " << m_context.tzAppid);
47     //check if deferred
48     //TODO if widget to be updated, then remove it from Deferred list?
49
50     bool isRunning = false;
51     int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isRunning);
52     if (APP_MANAGER_ERROR_NONE != ret) {
53         LogError("Fail to get running state");
54         ThrowMsg(Exceptions::PlatformAPIFailure,
55                  "Fail to get widget state");
56     }
57
58     if (true == isRunning) {
59         // get app_context for running application
60         // app_context must be released with app_context_destroy
61         app_context_h appCtx = NULL;
62         ret = app_manager_get_app_context(m_context.tzAppid.c_str(), &appCtx);
63         if (APP_MANAGER_ERROR_NONE != ret) {
64             LogError("Fail to get app_context");
65             ThrowMsg(Exceptions::AppIsRunning,
66                      "Widget is not stopped. Cannot uninstall!");
67         }
68
69         // terminate app_context_h
70         ret = app_manager_terminate_app(appCtx);
71         if (APP_MANAGER_ERROR_NONE != ret) {
72             LogError("Fail to terminate running application");
73             app_context_destroy(appCtx);
74             ThrowMsg(Exceptions::AppIsRunning,
75                      "Widget is not stopped. Cannot uninstall!");
76         } else {
77             app_context_destroy(appCtx);
78             // app_manager_terminate_app isn't sync API
79             // wait until application isn't running (50ms * 100)
80             bool isStillRunning = true;
81             int checkingloop = 100;
82             struct timespec duration = { 0, 50 * 1000 * 1000 };
83             while (--checkingloop >= 0) {
84                 nanosleep(&duration, NULL);
85                 int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isStillRunning);
86                 if (APP_MANAGER_ERROR_NONE != ret) {
87                     LogError("Fail to get running state");
88                     ThrowMsg(Exceptions::PlatformAPIFailure,
89                              "Fail to get widget state");
90                 }
91                 if (!isStillRunning) {
92                     break;
93                 }
94             }
95             if (isStillRunning) {
96                 LogError("Fail to terminate running application");
97                 ThrowMsg(Exceptions::AppIsRunning,
98                          "Widget is not stopped. Cannot uninstall!");
99             }
100             LogInfo("terminate application");
101         }
102     }
103
104     LogInfo("Widget Can be uninstalled. Pkgname : " << m_context.tzAppid);
105     m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
106                                   "Uninstall pre-checking Finished");
107 }
108 } //namespace WidgetUninstall
109 } //namespace Jobs