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>
31 #include <pkgmgr/pkgmgr_parser.h>
32 #include <pkgmgr-info.h>
33 #include <installer_log.h>
36 namespace WidgetUninstall {
37 TaskCheck::TaskCheck(UninstallerContext& context) :
38 DPL::TaskDecl<TaskCheck>(this),
41 AddStep(&TaskCheck::StartStep);
42 AddStep(&TaskCheck::StepUninstallPreCheck);
43 AddStep(&TaskCheck::StepCheckMDM);
44 AddStep(&TaskCheck::EndStep);
47 TaskCheck::~TaskCheck()
50 void TaskCheck::StartStep()
52 _D("--------- <TaskCheck> : START ----------");
55 void TaskCheck::EndStep()
57 m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
58 "Uninstall pre-checking Finished");
59 _D("--------- <TaskCheck> : END ----------");
62 void TaskCheck::StepUninstallPreCheck()
64 bool isRunning = false;
65 int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isRunning);
66 if (APP_MANAGER_ERROR_NONE != ret) {
67 _E("Fail to get running state");
68 ThrowMsg(Exceptions::PlatformAPIFailure,
69 "Fail to get widget state");
72 if (true == isRunning) {
73 // get app_context for running application
74 // app_context must be released with app_context_destroy
75 app_context_h appCtx = NULL;
76 ret = app_manager_get_app_context(m_context.tzAppid.c_str(), &appCtx);
77 if (APP_MANAGER_ERROR_NONE != ret) {
78 _E("Fail to get app_context");
79 ThrowMsg(Exceptions::AppIsRunning,
80 "Widget is not stopped. Cannot uninstall!");
83 // terminate app_context_h
84 ret = app_manager_terminate_app(appCtx);
85 if (APP_MANAGER_ERROR_NONE != ret) {
86 _E("Fail to terminate running application");
87 app_context_destroy(appCtx);
88 ThrowMsg(Exceptions::AppIsRunning,
89 "Widget is not stopped. Cannot uninstall!");
91 app_context_destroy(appCtx);
92 // app_manager_terminate_app isn't sync API
93 // wait until application isn't running (50ms * 100)
94 bool isStillRunning = true;
95 int checkingloop = 100;
96 struct timespec duration = { 0, 50 * 1000 * 1000 };
97 while (--checkingloop >= 0) {
98 nanosleep(&duration, NULL);
99 int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isStillRunning);
100 if (APP_MANAGER_ERROR_NONE != ret) {
101 _E("Fail to get running state");
102 ThrowMsg(Exceptions::PlatformAPIFailure,
103 "Fail to get widget state");
105 if (!isStillRunning) {
109 if (isStillRunning) {
110 _E("Fail to terminate running application");
111 ThrowMsg(Exceptions::AppIsRunning,
112 "Widget is not stopped. Cannot uninstall!");
114 _D("terminate application");
118 _D("Widget Can be uninstalled, Pkgname : %s", m_context.tzAppid.c_str());
121 void TaskCheck::StepCheckMDM()
125 if (PMINFO_R_OK != pkgmgr_parser_check_mdm_policy_for_uninstallation(
126 m_context.manifestFile.Fullpath().c_str())) {
127 _E("Failed to check mdm policy");
128 ThrowMsg(Exceptions::CheckMDMPolicyFailure, "Can't uninstall! Because of MDM policy");
131 } //namespace WidgetUninstall