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"
7 #include <manifest_parser/utils/logging.h>
10 #include <sys/types.h>
16 #include "common/pkgmgr_registration.h"
20 namespace ci = common_installer;
22 const std::map<ci::RequestType, const char*> kEventStr = {
23 {ci::RequestType::Install, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
24 {ci::RequestType::Recovery, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
25 {ci::RequestType::Reinstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
26 {ci::RequestType::Uninstall, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
27 {ci::RequestType::Update, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
28 {ci::RequestType::Delta, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
29 {ci::RequestType::ManifestDirectInstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
30 {ci::RequestType::ManifestDirectUpdate, PKGMGR_INSTALLER_UPGRADE_EVENT_STR}
35 namespace common_installer {
37 PkgmgrSignal::State PkgmgrSignal::state_ = PkgmgrSignal::State::NOT_SENT;
39 PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi, RequestType req_type)
40 : pi_(pi), request_type_(req_type), error_message_sent_(false) {
43 bool PkgmgrSignal::SendStarted(
44 const std::string& type, const std::string& pkgid) {
45 if (state_ != State::NOT_SENT) {
49 auto key = kEventStr.find(request_type_);
50 if (key == kEventStr.end()) {
53 if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, key->second, type, pkgid)) {
57 state_ = State::STARTED;
59 // workaround for pkgmgr client to know all appids which are uninstalled
60 if (request_type_ == ci::RequestType::Uninstall)
61 if (!SendAppids(type, pkgid))
67 bool PkgmgrSignal::SendProgress(int progress,
68 const std::string& type, const std::string& pkgid) {
69 if (state_ != State::STARTED) {
73 return SendSignal(PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
74 std::to_string(progress).c_str(), type, pkgid);
77 bool PkgmgrSignal::SendFinished(
78 Step::Status result, const std::string& type, const std::string& pkgid) {
79 if (state_ != State::STARTED) {
82 if (result != Step::Status::OK && !error_message_sent_) {
84 PKGMGR_INSTALLER_ERROR_KEY_STR,
85 std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
90 PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
93 state_ = State::FINISHED;
97 bool PkgmgrSignal::SendError(
99 const std::string& error_message,
100 const std::string& type,
101 const std::string& pkgid) {
102 if (state_ != State::STARTED) {
105 std::string error_value = std::to_string(static_cast<int>(result));
106 if (!error_message.empty()) {
107 error_value = error_value + ":" + error_message;
109 LOG(ERROR) << "PkgmgrSignal error_value: (" << error_value << ")";
110 error_message_sent_ = true;
112 PKGMGR_INSTALLER_ERROR_KEY_STR,
118 bool PkgmgrSignal::SendSignal(
121 const std::string& type,
122 const std::string& pkgid) const {
123 // send pkgmgr signal
124 if (pkgmgr_installer_send_signal(
126 !type.empty() ? type.c_str(): "",
127 !pkgid.empty() ? pkgid.c_str() : "",
130 LOG(ERROR) << "Fail to send pkgmgr signal";
134 LOG(DEBUG) << "Success to send pkgmgr signal"
135 << " PKGID=" << pkgid
137 << " VALUE=" << value;
141 const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
143 case Step::Status::OK:
144 return PKGMGR_INSTALLER_OK_EVENT_STR;
146 return PKGMGR_INSTALLER_FAIL_EVENT_STR;
150 bool PkgmgrSignal::SendAppids(const std::string& type,
151 const std::string& pkgid) const {
152 std::vector<std::string> appids;
153 if (!QueryAppidsForPkgId(pkgid, &appids, getuid()))
155 for (auto& appid : appids) {
156 if (!pkgmgr_installer_send_app_uninstall_signal(pi_, type.c_str(),
164 } // namespace common_installer