tizen 2.4 release
[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 <app_manager_extension.h>
32 #include <pkgmgr/pkgmgr_parser.h>
33 #include <pkgmgr-info.h>
34 #include <dpl/log/secure_log.h>
35 #if USE(WEB_PROVIDER)
36 #include <web_provider_service.h>
37 #endif
38
39 namespace Jobs {
40 namespace WidgetUninstall {
41 TaskCheck::TaskCheck(UninstallerContext& context) :
42     DPL::TaskDecl<TaskCheck>(this),
43     m_context(context)
44 {
45     AddStep(&TaskCheck::StartStep);
46     AddStep(&TaskCheck::StepUninstallPreCheck);
47     AddStep(&TaskCheck::EndStep);
48 }
49
50 TaskCheck::~TaskCheck()
51 {}
52
53 void TaskCheck::StartStep()
54 {
55     LOGI("--------- <TaskCheck> : START ----------");
56 }
57
58 void TaskCheck::EndStep()
59 {
60     m_context.job->UpdateProgress(UninstallerContext::UNINSTALL_PRECHECK,
61                                   "Uninstall pre-checking Finished");
62     LOGI("--------- <TaskCheck> : END ----------");
63 }
64
65 void TaskCheck::StepUninstallPreCheck()
66 {
67     FOREACH( it , m_context.tzAppIdList){
68         bool isRunning = false;
69         std::string tzAppId = DPL::ToUTF8String(*it);
70         int ret = app_manager_is_running(tzAppId.c_str(), &isRunning);
71         if (APP_MANAGER_ERROR_NONE != ret) {
72             _E("Fail to get running state");
73             ThrowMsg(Exceptions::PlatformAPIFailure,
74                  "Fail to get widget state");
75         }
76
77         if (true == isRunning) {
78             // get app_context for running application
79             // app_context must be released with app_context_destroy
80             app_context_h appCtx = NULL;
81             ret = app_manager_get_app_context(tzAppId.c_str(), &appCtx);
82             if (APP_MANAGER_ERROR_NONE != ret) {
83                 _E("Fail to get app_context");
84                 ThrowMsg(Exceptions::AppIsRunning,
85                      "Widget is not stopped. Cannot uninstall!");
86             }
87
88             // terminate app_context_h
89             ret = app_manager_terminate_app(appCtx);
90             if (APP_MANAGER_ERROR_NONE != ret) {
91                 _E("Fail to terminate running application");
92                 app_context_destroy(appCtx);
93                 ThrowMsg(Exceptions::AppIsRunning,
94                      "Widget is not stopped. Cannot uninstall!");
95             } else {
96                 app_context_destroy(appCtx);
97                 // app_manager_terminate_app isn't sync API
98                 // wait until application isn't running (50ms * 100)
99                 bool isStillRunning = true;
100                 int checkingloop = 100;
101                 struct timespec duration = { 0, 50 * 1000 * 1000 };
102                 while (--checkingloop >= 0) {
103                     nanosleep(&duration, NULL);
104                     ret = app_manager_is_running(tzAppId.c_str(), &isStillRunning);
105                     if (APP_MANAGER_ERROR_NONE != ret) {
106                         _E("Fail to get running state");
107                         ThrowMsg(Exceptions::PlatformAPIFailure,
108                              "Fail to get widget state");
109                     }
110                     if (!isStillRunning) {
111                         break;
112                     }
113                 }
114                 if (isStillRunning) {
115                     _E("Fail to terminate running application");
116                     ThrowMsg(Exceptions::AppIsRunning,
117                          "Widget is not stopped. Cannot uninstall!");
118                 }
119                 _D("terminate application");
120             }
121         }
122
123 #if USE(WEB_PROVIDER)
124         // TODO: if web package provides mutli-app in one package, we have to call below API with pkgId
125         web_provider_service_wait_boxes_removed(tzAppId.c_str());
126         _D("web app(%s) and its dynamic boxes can be terminated.", tzAppId.c_str());
127 #endif
128     }
129 }
130
131 } //namespace WidgetUninstall
132 } //namespace Jobs