tizen 2.4 release
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_status_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_status_check.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task  install osp service
21  */
22 #include "task_status_check.h"
23
24 #include <unistd.h>
25 #include <string>
26
27 #include <dpl/platform.h>
28 #include <app_manager.h>
29 #if USE(WEB_PROVIDER)
30 #include <web_provider_service.h>
31 #endif
32 #include <dpl/errno_string.h>
33 #include <dpl/utils/bash_utils.h>
34
35 #include <widget_install/widget_install_context.h>
36 #include <widget_install/widget_install_errors.h>
37
38 #include <dpl/log/secure_log.h>
39 #include <app_manager_extension.h>
40
41 namespace Jobs {
42 namespace WidgetInstall {
43 TaskStatusCheck::TaskStatusCheck(JobWidgetInstall * const &jobContext) :
44     DPL::TaskDecl<TaskStatusCheck>(this),
45     m_jobContext(jobContext)
46 {
47     AddStep(&TaskStatusCheck::StartStep);
48     AddStep(&TaskStatusCheck::CheckAppRunningStateStep);
49 #if USE(WEB_PROVIDER)
50     AddStep(&TaskStatusCheck::WidgetRemoveStep);
51 #endif
52     AddStep(&TaskStatusCheck::EndStep);
53 }
54
55 void TaskStatusCheck::CheckAppRunningStateStep()
56 {
57     _D("Step: CheckAppRunningStateStep");
58     std::string webAppPkgId = DPL::ToUTF8String(m_jobContext->m_installerContext.widgetConfig.tzPkgid);
59
60     int ret = 0;
61     pkgmgrinfo_pkginfo_h handle;
62     ret = pkgmgrinfo_pkginfo_get_pkginfo(webAppPkgId.c_str(), &handle);
63     if (ret != PMINFO_R_OK) {
64         _E("Can't find [%s] in package manager", webAppPkgId.c_str());
65         ThrowMsg(Jobs::WidgetInstall::Exceptions::GetInfoPkgMgrFailed,
66                 "package id can't find from package manager");
67     }
68     ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_ALL_APP,
69             terminateRunningApp, NULL);
70     if (ret != PMINFO_R_OK) {
71         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
72         ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
73                 "widget is running");
74     }
75     pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
76 }
77
78 int TaskStatusCheck::terminateRunningApp(pkgmgrinfo_appinfo_h handle,
79         void* /*user_data*/)
80 {
81     char *appId = NULL;
82     pkgmgrinfo_appinfo_get_appid(handle, &appId);
83     _D("# Terminating App : [%s]", appId);
84
85     bool isRunning = false;
86     int ret = app_manager_is_running(appId, &isRunning);
87     if (APP_MANAGER_ERROR_NONE != ret) {
88         _E("Fail to get running state");
89         ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
90                 "widget is running");
91     }
92
93     if (true == isRunning) {
94         // get app_context for running application
95         // app_context must be released with app_context_destroy
96         app_context_h appCtx = NULL;
97         ret = app_manager_get_app_context(appId, &appCtx);
98         if (APP_MANAGER_ERROR_NONE != ret) {
99             _E("Fail to get app_context");
100             ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
101                     "widget is running");
102         }
103
104         // terminate app_context_h
105         ret = app_manager_terminate_app(appCtx);
106         if (APP_MANAGER_ERROR_NONE != ret) {
107             _E("Fail to terminate running application");
108             app_context_destroy(appCtx);
109             ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
110                     "widget is running");
111         } else {
112             app_context_destroy(appCtx);
113             // app_manager_terminate_app isn't sync API
114             // wait until application isn't running (50ms * 100)
115             bool isStillRunning = true;
116             int checkingloop = 100;
117             struct timespec duration = { 0, 50 * 1000 * 1000 };
118             while (--checkingloop >= 0) {
119                 nanosleep(&duration, NULL);
120                 ret = app_manager_is_running(appId, &isStillRunning);
121                 if (APP_MANAGER_ERROR_NONE != ret) {
122                     _E("Fail to get running state");
123                     ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
124                             "widget is running");
125                 }
126                 if (!isStillRunning) {
127                     break;
128                 }
129             }
130             if (isStillRunning) {
131                 _E("Fail to terminate running application");
132                 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
133                         "widget is running");
134             }
135             _D("terminate application");
136         }
137     }
138     return 0;
139 }
140
141 #if USE(WEB_PROVIDER)
142 void TaskStatusCheck::WidgetRemoveStep()
143 {
144     _D("Step: WidgetRemoveStep");
145
146     // check if this webapp has dynaimc boxes, and request to remove them
147     web_provider_service_wait_boxes_removed(
148             DPL::ToUTF8String(m_jobContext->m_installerContext.widgetConfig.tzAppid).c_str());
149
150     _D("Finished to handle this web app's dynamic box");
151 }
152 #endif
153
154 void TaskStatusCheck::StartStep()
155 {
156     LOGI("--------- <TaskStatusCheck> : START ----------");
157 }
158
159 void TaskStatusCheck::EndStep()
160 {
161     LOGI("--------- <TaskStatusCheck> : END ----------");
162 }
163 } //namespace WidgetInstall
164 } //namespace Jobs