[Signal] Send 'error' code to pkgcmd
[platform/core/appfw/app-installers.git] / src / common / pkgmgr_signal.cc
1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/pkgmgr_signal.h"
6
7 #include <cassert>
8
9 #include "utils/logging.h"
10
11 // Redefine this value as it is not exported by pkgmgr
12 // this should probably be in public interface because of
13 // otherwise there is no way to return errorcode
14 #define PKGMGR_INSTALLER_ERROR_KEY_STR "error"
15
16 namespace common_installer {
17
18 PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi)
19     : pi_(pi),
20       state_(State::NOT_SENT) {
21 }
22
23 bool PkgmgrSignal::SendStarted(
24     const std::string& type, const std::string& pkgid) {
25   if (state_ != State::NOT_SENT) {
26     return false;
27   }
28
29   if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR,
30       (pkgmgr_installer_get_request_type(pi_) != PKGMGR_REQ_UNINSTALL)
31       ? PKGMGR_INSTALLER_INSTALL_EVENT_STR
32       : PKGMGR_INSTALLER_UNINSTALL_EVENT_STR,
33       type, pkgid)) {
34     return false;
35   }
36   state_ = State::STARTED;
37   return true;
38 }
39
40 bool PkgmgrSignal::SendFinished(
41     Step::Status result, const std::string& type, const std::string& pkgid) {
42   if (state_ != State::STARTED) {
43     return false;
44   }
45   if (result != Step::Status::OK) {
46     if (!SendSignal(
47         PKGMGR_INSTALLER_ERROR_KEY_STR,
48         std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
49       return false;
50     }
51   }
52   if (!SendSignal(
53       PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
54     return false;
55   }
56   state_ = State::FINISHED;
57   return true;
58 }
59
60 bool PkgmgrSignal::IsFinished() const {
61   return state_ == State::FINISHED;
62 }
63
64 bool PkgmgrSignal::SendSignal(
65     const char* key,
66     const char* value,
67     const std::string& type,
68     const std::string& pkgid) const {
69   // send pkgmgr signal
70   if (pkgmgr_installer_send_signal(
71         pi_,
72         !type.empty() ?  type.c_str(): "",
73         !pkgid.empty() ? pkgid.c_str() : "",
74         key,
75         value)) {
76     LOG(ERROR) << "Fail to send pkgmgr signal";
77     return false;
78   }
79
80   LOG(DEBUG) << "Success to send pkgmgr signal"
81              << " PKGID=" << pkgid
82              << " KEY=" << key
83              << " VALUE=" << value;
84   return true;
85 }
86
87 const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
88   switch (result) {
89     case Step::Status::OK:
90       return PKGMGR_INSTALLER_OK_EVENT_STR;
91     default:
92       return PKGMGR_INSTALLER_FAIL_EVENT_STR;
93   }
94 }
95
96 }  // namespace common_installer