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.
5 #include "common/pkgmgr_signal.h"
14 #include "common/pkgmgr_registration.h"
15 #include "common/utils/logging.h"
19 namespace ci = common_installer;
21 const std::map<ci::RequestType, const char*> kEventStr = {
22 {ci::RequestType::Install, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
23 {ci::RequestType::Recovery, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
24 {ci::RequestType::Reinstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
25 {ci::RequestType::Uninstall, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
26 {ci::RequestType::Unknown, PKGMGR_INSTALLER_INSTALL_EVENT_STR}, // not needed
27 {ci::RequestType::Update, PKGMGR_INSTALLER_UPGRADE_EVENT_STR}
32 namespace common_installer {
34 PkgmgrSignal::State PkgmgrSignal::state_ = PkgmgrSignal::State::NOT_SENT;
36 PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi, RequestType req_type)
37 : pi_(pi), request_type_(req_type) {
40 bool PkgmgrSignal::SendStarted(
41 const std::string& type, const std::string& pkgid) {
42 if (state_ != State::NOT_SENT) {
46 auto key = kEventStr.find(request_type_);
47 if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, key->second, type, pkgid)) {
51 state_ = State::STARTED;
53 // workaround for pkgmgr client to know all appids which are uninstalled
54 if (request_type_ == ci::RequestType::Uninstall)
55 if (!SendAppids(type, pkgid))
61 bool PkgmgrSignal::SendProgress(int progress,
62 const std::string& type, const std::string& pkgid) {
63 if (state_ != State::STARTED) {
67 return SendSignal(PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
68 std::to_string(progress).c_str(), type, pkgid);
71 bool PkgmgrSignal::SendFinished(
72 Step::Status result, const std::string& type, const std::string& pkgid) {
73 if (state_ != State::STARTED) {
76 if (result != Step::Status::OK) {
78 PKGMGR_INSTALLER_ERROR_KEY_STR,
79 std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
84 PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
87 state_ = State::FINISHED;
91 bool PkgmgrSignal::SendSignal(
94 const std::string& type,
95 const std::string& pkgid) const {
97 if (pkgmgr_installer_send_signal(
99 !type.empty() ? type.c_str(): "",
100 !pkgid.empty() ? pkgid.c_str() : "",
103 LOG(ERROR) << "Fail to send pkgmgr signal";
107 LOG(DEBUG) << "Success to send pkgmgr signal"
108 << " PKGID=" << pkgid
110 << " VALUE=" << value;
114 const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
116 case Step::Status::OK:
117 return PKGMGR_INSTALLER_OK_EVENT_STR;
119 return PKGMGR_INSTALLER_FAIL_EVENT_STR;
123 bool PkgmgrSignal::SendAppids(const std::string& type,
124 const std::string& pkgid) const {
125 std::vector<std::string> appids;
126 if (!QueryAppidsForPkgId(pkgid, &appids, getuid()))
128 for (auto& appid : appids) {
129 if (!pkgmgr_installer_send_app_uninstall_signal(pi_, type.c_str(),
137 } // namespace common_installer