[Release] wrt-installer_0.1.114
[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 <ctime>
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::StartStep);
39     AddStep(&TaskCheck::StepUninstallPreCheck);
40     AddStep(&TaskCheck::EndStep);
41 }
42
43 TaskCheck::~TaskCheck()
44 {}
45
46 void TaskCheck::StartStep()
47 {
48     LogDebug("--------- <TaskCheck> : START ----------");
49 }
50
51 void TaskCheck::EndStep()
52 {
53     m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
54                                   "Uninstall pre-checking Finished");
55     LogDebug("--------- <TaskCheck> : END ----------");
56 }
57
58 void TaskCheck::StepUninstallPreCheck()
59 {
60     bool isRunning = false;
61     int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isRunning);
62     if (APP_MANAGER_ERROR_NONE != ret) {
63         LogError("Fail to get running state");
64         ThrowMsg(Exceptions::PlatformAPIFailure,
65                  "Fail to get widget state");
66     }
67
68     if (true == isRunning) {
69         // get app_context for running application
70         // app_context must be released with app_context_destroy
71         app_context_h appCtx = NULL;
72         ret = app_manager_get_app_context(m_context.tzAppid.c_str(), &appCtx);
73         if (APP_MANAGER_ERROR_NONE != ret) {
74             LogError("Fail to get app_context");
75             ThrowMsg(Exceptions::AppIsRunning,
76                      "Widget is not stopped. Cannot uninstall!");
77         }
78
79         // terminate app_context_h
80         ret = app_manager_terminate_app(appCtx);
81         if (APP_MANAGER_ERROR_NONE != ret) {
82             LogError("Fail to terminate running application");
83             app_context_destroy(appCtx);
84             ThrowMsg(Exceptions::AppIsRunning,
85                      "Widget is not stopped. Cannot uninstall!");
86         } else {
87             app_context_destroy(appCtx);
88             // app_manager_terminate_app isn't sync API
89             // wait until application isn't running (50ms * 100)
90             bool isStillRunning = true;
91             int checkingloop = 100;
92             struct timespec duration = { 0, 50 * 1000 * 1000 };
93             while (--checkingloop >= 0) {
94                 nanosleep(&duration, NULL);
95                 int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isStillRunning);
96                 if (APP_MANAGER_ERROR_NONE != ret) {
97                     LogError("Fail to get running state");
98                     ThrowMsg(Exceptions::PlatformAPIFailure,
99                              "Fail to get widget state");
100                 }
101                 if (!isStillRunning) {
102                     break;
103                 }
104             }
105             if (isStillRunning) {
106                 LogError("Fail to terminate running application");
107                 ThrowMsg(Exceptions::AppIsRunning,
108                          "Widget is not stopped. Cannot uninstall!");
109             }
110             LogDebug("terminate application");
111         }
112     }
113
114     LogDebug("Widget Can be uninstalled. Pkgname : " << m_context.tzAppid);
115 }
116 } //namespace WidgetUninstall
117 } //namespace Jobs