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_status_check.cpp
18 * @author Soyoung Kim (sy037.kim@samsung.com)
20 * @brief Implementation file for installer task install osp service
22 #include "task_status_check.h"
27 #include <dpl/platform.h>
28 #include <app_manager.h>
30 #include <web_provider_service.h>
32 #include <dpl/errno_string.h>
33 #include <dpl/utils/bash_utils.h>
35 #include <widget_install/widget_install_context.h>
36 #include <widget_install/widget_install_errors.h>
38 #include <dpl/log/secure_log.h>
39 #include <app_manager_extension.h>
42 namespace WidgetInstall {
43 TaskStatusCheck::TaskStatusCheck(JobWidgetInstall * const &jobContext) :
44 DPL::TaskDecl<TaskStatusCheck>(this),
45 m_jobContext(jobContext)
47 AddStep(&TaskStatusCheck::StartStep);
48 AddStep(&TaskStatusCheck::CheckAppRunningStateStep);
50 AddStep(&TaskStatusCheck::WidgetRemoveStep);
52 AddStep(&TaskStatusCheck::EndStep);
55 void TaskStatusCheck::CheckAppRunningStateStep()
57 _D("Step: CheckAppRunningStateStep");
58 std::string webAppPkgId = DPL::ToUTF8String(m_jobContext->m_installerContext.widgetConfig.tzPkgid);
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");
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,
75 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
78 int TaskStatusCheck::terminateRunningApp(pkgmgrinfo_appinfo_h handle,
82 pkgmgrinfo_appinfo_get_appid(handle, &appId);
83 _D("# Terminating App : [%s]", appId);
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,
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");
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");
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");
126 if (!isStillRunning) {
130 if (isStillRunning) {
131 _E("Fail to terminate running application");
132 ThrowMsg(Jobs::WidgetInstall::Exceptions::WidgetRunningError,
133 "widget is running");
135 _D("terminate application");
141 #if USE(WEB_PROVIDER)
142 void TaskStatusCheck::WidgetRemoveStep()
144 _D("Step: WidgetRemoveStep");
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());
150 _D("Finished to handle this web app's dynamic box");
154 void TaskStatusCheck::StartStep()
156 LOGI("--------- <TaskStatusCheck> : START ----------");
159 void TaskStatusCheck::EndStep()
161 LOGI("--------- <TaskStatusCheck> : END ----------");
163 } //namespace WidgetInstall