Locking launching of widget during reinstallation/uninstallation
[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 <dpl/file_lock.h>
31 #include <app_manager.h>
32 #include <pkgmgr/pkgmgr_parser.h>
33 #include <pkgmgr-info.h>
34 #include <installer_log.h>
35
36 namespace Jobs {
37 namespace WidgetUninstall {
38 TaskCheck::TaskCheck(UninstallerContext& context) :
39     DPL::TaskDecl<TaskCheck>(this),
40     m_context(context)
41 {
42     AddStep(&TaskCheck::StartStep);
43     AddStep(&TaskCheck::SetUninstallationLockStep);
44     AddStep(&TaskCheck::StepUninstallPreCheck);
45     AddStep(&TaskCheck::StepCheckMDM);
46     AddStep(&TaskCheck::EndStep);
47 }
48
49 TaskCheck::~TaskCheck()
50 {}
51
52 void TaskCheck::StartStep()
53 {
54     _D("--------- <TaskCheck> : START ----------");
55 }
56
57 void TaskCheck::EndStep()
58 {
59     m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
60                                   "Uninstall pre-checking Finished");
61     _D("--------- <TaskCheck> : END ----------");
62 }
63
64 void TaskCheck::SetUninstallationLockStep()
65 {
66     std::string lockString = m_context.tzAppid;
67     _D("Locking uninstallation on file '%s'", lockString.c_str());
68     m_context.installationLock.reset(new DPL::FileBasedMutex(lockString));
69     _D("Uninstallation locked");
70 }
71
72 void TaskCheck::StepUninstallPreCheck()
73 {
74     bool isRunning = false;
75     int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isRunning);
76     if (APP_MANAGER_ERROR_NONE != ret) {
77         _E("Fail to get running state");
78         ThrowMsg(Exceptions::PlatformAPIFailure,
79                  "Fail to get widget state");
80     }
81
82     if (true == isRunning) {
83         // get app_context for running application
84         // app_context must be released with app_context_destroy
85         app_context_h appCtx = NULL;
86         ret = app_manager_get_app_context(m_context.tzAppid.c_str(), &appCtx);
87         if (APP_MANAGER_ERROR_NONE != ret) {
88             _E("Fail to get app_context");
89             ThrowMsg(Exceptions::AppIsRunning,
90                      "Widget is not stopped. Cannot uninstall!");
91         }
92
93         // terminate app_context_h
94         ret = app_manager_terminate_app(appCtx);
95         if (APP_MANAGER_ERROR_NONE != ret) {
96             _E("Fail to terminate running application");
97             app_context_destroy(appCtx);
98             ThrowMsg(Exceptions::AppIsRunning,
99                      "Widget is not stopped. Cannot uninstall!");
100         } else {
101             app_context_destroy(appCtx);
102             // app_manager_terminate_app isn't sync API
103             // wait until application isn't running (50ms * 100)
104             bool isStillRunning = true;
105             int checkingloop = 100;
106             struct timespec duration = { 0, 50 * 1000 * 1000 };
107             while (--checkingloop >= 0) {
108                 nanosleep(&duration, NULL);
109                 int ret = app_manager_is_running(m_context.tzAppid.c_str(), &isStillRunning);
110                 if (APP_MANAGER_ERROR_NONE != ret) {
111                     _E("Fail to get running state");
112                     ThrowMsg(Exceptions::PlatformAPIFailure,
113                              "Fail to get widget state");
114                 }
115                 if (!isStillRunning) {
116                     break;
117                 }
118             }
119             if (isStillRunning) {
120                 _E("Fail to terminate running application");
121                 ThrowMsg(Exceptions::AppIsRunning,
122                          "Widget is not stopped. Cannot uninstall!");
123             }
124             _D("terminate application");
125         }
126     }
127
128     _D("Widget Can be uninstalled, Pkgname : %s", m_context.tzAppid.c_str());
129 }
130
131 void TaskCheck::StepCheckMDM()
132 {
133     _D("StepCheckMDM");
134
135     if (PMINFO_R_OK !=  pkgmgr_parser_check_mdm_policy_for_uninstallation(
136                 m_context.manifestFile.Fullpath().c_str())) {
137         _E("Failed to check mdm policy");
138         ThrowMsg(Exceptions::CheckMDMPolicyFailure, "Can't uninstall! Because of MDM policy");
139     }
140 }
141 } //namespace WidgetUninstall
142 } //namespace Jobs