2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file task_check.cpp
18 * @author Pawel Sikorski(p.sikorski@samsung.com)
20 * @brief Header file for widget uninstall task check
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>
33 namespace WidgetUninstall {
34 TaskCheck::TaskCheck(UninstallerContext& context) :
35 DPL::TaskDecl<TaskCheck>(this),
38 AddStep(&TaskCheck::StartStep);
39 AddStep(&TaskCheck::StepUninstallPreCheck);
40 AddStep(&TaskCheck::EndStep);
43 TaskCheck::~TaskCheck()
46 void TaskCheck::StartStep()
48 LogDebug("--------- <TaskCheck> : START ----------");
51 void TaskCheck::EndStep()
53 m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
54 "Uninstall pre-checking Finished");
55 LogDebug("--------- <TaskCheck> : END ----------");
58 void TaskCheck::StepUninstallPreCheck()
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");
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!");
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!");
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");
101 if (!isStillRunning) {
105 if (isStillRunning) {
106 LogError("Fail to terminate running application");
107 ThrowMsg(Exceptions::AppIsRunning,
108 "Widget is not stopped. Cannot uninstall!");
110 LogDebug("terminate application");
114 LogDebug("Widget Can be uninstalled. Pkgname : " << m_context.tzAppid);
116 } //namespace WidgetUninstall