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::Clear, PKGMGR_INSTALLER_CLEAR_EVENT_STR},
27 {ci::RequestType::Uninstall, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
28 {ci::RequestType::Update, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
29 {ci::RequestType::Delta, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
30 {ci::RequestType::MountInstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
31 {ci::RequestType::MountUpdate, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
32 {ci::RequestType::ManifestDirectInstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
33 {ci::RequestType::ManifestDirectUpdate, PKGMGR_INSTALLER_UPGRADE_EVENT_STR}
38 namespace common_installer {
40 PkgmgrSignal::State PkgmgrSignal::state_ = PkgmgrSignal::State::NOT_SENT;
42 PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi, RequestType req_type)
43 : pi_(pi), request_type_(req_type), error_message_sent_(false) {
46 bool PkgmgrSignal::SendStarted(
47 const std::string& type, const std::string& pkgid) {
48 if (state_ != State::NOT_SENT) {
52 auto key = kEventStr.find(request_type_);
53 if (key == kEventStr.end()) {
56 if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, key->second, type, pkgid)) {
60 state_ = State::STARTED;
62 // workaround for pkgmgr client to know all appids which are uninstalled
63 if (request_type_ == ci::RequestType::Uninstall)
64 if (!SendAppids(type, pkgid))
70 bool PkgmgrSignal::SendProgress(int progress,
71 const std::string& type, const std::string& pkgid) {
72 if (state_ != State::STARTED) {
76 return SendSignal(PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
77 std::to_string(progress).c_str(), type, pkgid);
80 bool PkgmgrSignal::SendFinished(
81 Step::Status result, const std::string& type, const std::string& pkgid) {
82 if (state_ != State::STARTED) {
85 if (result != Step::Status::OK && !error_message_sent_) {
87 PKGMGR_INSTALLER_ERROR_KEY_STR,
88 std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
93 PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
96 state_ = State::FINISHED;
100 bool PkgmgrSignal::SendError(
102 const std::string& error_message,
103 const std::string& type,
104 const std::string& pkgid) {
105 if (state_ != State::STARTED) {
108 std::string error_value = std::to_string(static_cast<int>(result));
109 if (!error_message.empty()) {
110 error_value = error_value + ":" + error_message;
112 LOG(ERROR) << "PkgmgrSignal error_value: (" << error_value << ")";
113 error_message_sent_ = true;
115 PKGMGR_INSTALLER_ERROR_KEY_STR,
121 bool PkgmgrSignal::SendSignal(
124 const std::string& type,
125 const std::string& pkgid) const {
126 // send pkgmgr signal
127 if (pkgmgr_installer_send_signal(
129 !type.empty() ? type.c_str(): "",
130 !pkgid.empty() ? pkgid.c_str() : "",
133 LOG(ERROR) << "Fail to send pkgmgr signal";
137 LOG(DEBUG) << "Success to send pkgmgr signal"
138 << " PKGID=" << pkgid
140 << " VALUE=" << value;
144 const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
146 case Step::Status::OK:
147 return PKGMGR_INSTALLER_OK_EVENT_STR;
149 return PKGMGR_INSTALLER_FAIL_EVENT_STR;
153 bool PkgmgrSignal::SendAppids(const std::string& type,
154 const std::string& pkgid) const {
155 std::vector<std::string> appids;
156 if (!QueryAppidsForPkgId(pkgid, &appids, getuid()))
158 for (auto& appid : appids) {
159 if (pkgmgr_installer_send_app_uninstall_signal(pi_, type.c_str(),
167 } // namespace common_installer