Use GList in manifest_x structures
[platform/core/appfw/app-installers.git] / src / common / step / step_kill_apps.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/step_kill_apps.h"
6
7 #include <app_manager.h>
8 #include <app_manager_extension.h>
9
10 #include <string>
11
12 #include "common/utils/glist_range.h"
13
14 namespace {
15
16 bool KillApp(const std::string& appid) {
17   bool is_running = false;
18   if (app_manager_is_running(appid.c_str(), &is_running)
19       != APP_MANAGER_ERROR_NONE) {
20     LOG(ERROR) << "app_manager_is_running failed";
21     return false;
22   }
23   if (!is_running) {
24     return false;
25   }
26   app_context_h app_context;
27   if (app_manager_get_app_context(appid.c_str(), &app_context)
28       != APP_MANAGER_ERROR_NONE) {
29     LOG(ERROR) << "app_manager_get_app_context failed";
30     return false;
31   }
32   if (app_manager_terminate_app(app_context)
33       != APP_MANAGER_ERROR_NONE) {
34     LOG(ERROR) << "app_manager_terminate_app failed";
35     app_context_destroy(app_context);
36     return false;
37   }
38   LOG(DEBUG) << "Application '" << appid << "' has been killed";
39   app_context_destroy(app_context);
40   return true;
41 }
42
43 }  // namespace
44
45 namespace common_installer {
46 namespace pkgmgr {
47
48 Step::Status StepKillApps::process() {
49   manifest_x* old_manifest = context_->old_manifest_data.get() ?
50       context_->old_manifest_data.get() : context_->manifest_data.get();
51   for (application_x* app :
52        GListRange<application_x*>(old_manifest->application)) {
53     (void) KillApp(app->appid);
54   }
55   return Status::OK;
56 }
57
58 Step::Status StepKillApps::precheck() {
59   if (!context_->manifest_data.get() && !context_->old_manifest_data.get()) {
60     LOG(ERROR) << "manifest_data is empty";
61     return Status::ERROR;
62   }
63   return Status::OK;
64 }
65
66 }  // namespace pkgmgr
67 }  // namespace common_installer