22a7e911042a7b39cb9fc3e7ba48a4fd626530ed
[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 #include <pkgmgr/pkgmgr_parser.h>
32 #include <pkgmgr-info.h>
33 #include <installer_log.h>
34
35 namespace Jobs {
36 namespace WidgetUninstall {
37 TaskCheck::TaskCheck(UninstallerContext& context) :
38     DPL::TaskDecl<TaskCheck>(this),
39     m_context(context)
40 {
41     AddStep(&TaskCheck::StartStep);
42     AddStep(&TaskCheck::StepUninstallPreCheck);
43     AddStep(&TaskCheck::StepCheckMDM);
44     AddStep(&TaskCheck::EndStep);
45 }
46
47 TaskCheck::~TaskCheck()
48 {}
49
50 void TaskCheck::StartStep()
51 {
52     _D("--------- <TaskCheck> : START ----------");
53 }
54
55 void TaskCheck::EndStep()
56 {
57     m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
58                                   "Uninstall pre-checking Finished");
59     _D("--------- <TaskCheck> : END ----------");
60 }
61
62 void TaskCheck::StepUninstallPreCheck()
63 {
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");
70     }
71
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!");
81         }
82
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!");
90         } else {
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");
104                 }
105                 if (!isStillRunning) {
106                     break;
107                 }
108             }
109             if (isStillRunning) {
110                 _E("Fail to terminate running application");
111                 ThrowMsg(Exceptions::AppIsRunning,
112                          "Widget is not stopped. Cannot uninstall!");
113             }
114             _D("terminate application");
115         }
116     }
117
118     _D("Widget Can be uninstalled, Pkgname : %s", m_context.tzAppid.c_str());
119 }
120
121 void TaskCheck::StepCheckMDM()
122 {
123     _D("StepCheckMDM");
124
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");
129     }
130 }
131 } //namespace WidgetUninstall
132 } //namespace Jobs