[Signal] Sending progess to pkgmgr
[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::SendProgress(int progress,
41     const std::string& type, const std::string& pkgid) {
42   if (state_ != State::STARTED) {
43     return false;
44   }
45
46   return SendSignal(PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
47       std::to_string(progress).c_str(), type, pkgid);
48 }
49
50 bool PkgmgrSignal::SendFinished(
51     Step::Status result, const std::string& type, const std::string& pkgid) {
52   if (state_ != State::STARTED) {
53     return false;
54   }
55   if (result != Step::Status::OK) {
56     if (!SendSignal(
57         PKGMGR_INSTALLER_ERROR_KEY_STR,
58         std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
59       return false;
60     }
61   }
62   if (!SendSignal(
63       PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
64     return false;
65   }
66   state_ = State::FINISHED;
67   return true;
68 }
69
70 bool PkgmgrSignal::IsFinished() const {
71   return state_ == State::FINISHED;
72 }
73
74 bool PkgmgrSignal::SendSignal(
75     const char* key,
76     const char* value,
77     const std::string& type,
78     const std::string& pkgid) const {
79   // send pkgmgr signal
80   if (pkgmgr_installer_send_signal(
81         pi_,
82         !type.empty() ?  type.c_str(): "",
83         !pkgid.empty() ? pkgid.c_str() : "",
84         key,
85         value)) {
86     LOG(ERROR) << "Fail to send pkgmgr signal";
87     return false;
88   }
89
90   LOG(DEBUG) << "Success to send pkgmgr signal"
91              << " PKGID=" << pkgid
92              << " KEY=" << key
93              << " VALUE=" << value;
94   return true;
95 }
96
97 const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
98   switch (result) {
99     case Step::Status::OK:
100       return PKGMGR_INSTALLER_OK_EVENT_STR;
101     default:
102       return PKGMGR_INSTALLER_FAIL_EVENT_STR;
103   }
104 }
105
106 }  // namespace common_installer