2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "signal_receiver.hh"
22 #include "../../installer/src/pkgmgr_installer.h"
27 #define AGENT_APPID "signal_agent"
29 SignalReceiver::SignalReceiver(bool is_system, int status_type)
30 : PkgSignal(is_system ? "" : AGENT_APPID, is_system),
31 status_type_(status_type) {
34 SignalReceiver::~SignalReceiver() {
37 const std::map<std::string, int> signal_map = {
38 {PKGMGR_INSTALLER_INSTALL_EVENT_STR, PKGMGR_CLIENT_STATUS_INSTALL},
39 {PKGMGR_INSTALLER_UNINSTALL_EVENT_STR, PKGMGR_CLIENT_STATUS_UNINSTALL},
40 {PKGMGR_INSTALLER_UPGRADE_EVENT_STR, PKGMGR_CLIENT_STATUS_UPGRADE},
41 {PKGMGR_INSTALLER_CLEAR_EVENT_STR, PKGMGR_CLIENT_STATUS_CLEAR_DATA},
42 {PKGMGR_INSTALLER_MOVE_EVENT_STR, PKGMGR_CLIENT_STATUS_MOVE},
43 {PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
44 PKGMGR_CLIENT_STATUS_INSTALL_PROGRESS},
45 {PKGMGR_INSTALLER_GET_SIZE_KEY_STR, PKGMGR_CLIENT_STATUS_GET_SIZE},
46 {PKGMGR_INSTALLER_CLEAR_CACHE_KEY_STR,
47 PKGMGR_CLIENT_STATUS_CLEAR_CACHE},
48 {PKGMGR_INSTALLER_APP_ENABLE_EVENT_STR,
49 PKGMGR_CLIENT_STATUS_ENABLE_APP},
50 {PKGMGR_INSTALLER_APP_DISABLE_EVENT_STR,
51 PKGMGR_CLIENT_STATUS_DISABLE_APP},
52 {PKGMGR_INSTALLER_APP_ENABLE_SPLASH_SCREEN_EVENT_STR,
53 PKGMGR_CLIENT_STATUS_ENABLE_APP_SPLASH_SCREEN},
54 {PKGMGR_INSTALLER_APP_DISABLE_SPLASH_SCREEN_EVENT_STR,
55 PKGMGR_CLIENT_STATUS_DISABLE_APP_SPLASH_SCREEN},
56 {PKGMGR_INSTALLER_RES_COPY_EVENT_STR, PKGMGR_CLIENT_STATUS_RES_COPY},
57 {PKGMGR_INSTALLER_RES_CREATE_DIR_EVENT_STR,
58 PKGMGR_CLIENT_STATUS_RES_CREATE_DIR},
59 {PKGMGR_INSTALLER_RES_REMOVE_EVENT_STR,
60 PKGMGR_CLIENT_STATUS_RES_REMOVE},
61 {PKGMGR_INSTALLER_RES_UNINSTALL_EVENT_STR,
62 PKGMGR_CLIENT_STATUS_RES_UNINSTALL}
65 void SignalReceiver::OnAsyncResult(std::string signal, int targetUid,
66 std::string reqId, std::vector<pkg_signal::PkgInfo> pkgs,
67 std::string key, std::string val) {
68 if (status_type_ != PKGMGR_CLIENT_STATUS_ALL) {
69 auto it = signal_map.find(signal);
70 if (it == signal_map.end()) {
71 _E("Unexpected signal : %s", signal.c_str());
75 if ((it->second & status_type_) == 0) {
80 HandleHandler(targetUid, reqId, pkgs, key, val);
81 HandleGlobalHandler(targetUid, reqId, pkgs, key, val);
82 HandleSizeInfoHandler(reqId, pkgs, key, val);
85 void SignalReceiver::HandleHandler(int targetUid,
86 const std::string& reqId, const std::vector<pkg_signal::PkgInfo>& pkgs,
87 const std::string& key, const std::string& val) const {
88 const auto it = handlers_.find(reqId);
89 if (it == handlers_.end())
91 const auto& [id, cb, app_cb, data] = it->second;
93 for (auto& i : pkgs) {
95 cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
96 key.c_str(), val.c_str(), nullptr, data);
98 app_cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
99 i.GetAppid().c_str(), key.c_str(), val.c_str(), nullptr, data);
104 void SignalReceiver::HandleGlobalHandler(int targetUid,
105 const std::string& reqId, const std::vector<pkg_signal::PkgInfo>& pkgs,
106 const std::string& key, const std::string& val) const {
107 for (auto& i : pkgs) {
108 for (const auto& [id, cb, app_cb, data] : global_handlers_) {
110 cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
111 key.c_str(), val.c_str(), nullptr, data);
113 app_cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
114 i.GetAppid().c_str(), key.c_str(), val.c_str(), nullptr, data);
120 void SignalReceiver::HandleSizeInfoHandler(
121 const std::string& reqId, const std::vector<pkg_signal::PkgInfo>& pkgs,
122 const std::string& key, const std::string& val) const {
123 auto it = size_info_handlers_.find(reqId);
124 if (it == size_info_handlers_.end())
126 const auto& [id, cb, pc, data] = it->second;
128 std::vector<std::string> tokens;
129 std::istringstream ss(val);
132 while (std::getline(ss, token, ':')) {
133 tokens.push_back(token);
136 if (tokens.size() != 6) {
140 pkg_size_info_t size_info{
141 std::stoll(tokens[0]),
142 std::stoll(tokens[1]),
143 std::stoll(tokens[2]),
144 std::stoll(tokens[3]),
145 std::stoll(tokens[4]),
146 std::stoll(tokens[5]),
149 for (auto& i : pkgs) {
150 if (i.GetPkgid() == std::string(PKG_SIZE_INFO_TOTAL)) {
151 pkgmgr_total_pkg_size_info_receive_cb callback;
152 callback = (pkgmgr_total_pkg_size_info_receive_cb)cb;
153 callback(pc, &size_info, data);
155 cb(pc, i.GetPkgid().c_str(), &size_info, data);
160 void SignalReceiver::OnAsyncResultForResource(std::string signal,
161 int targetUid, std::string reqId, std::string pkgid,
162 std::string status, pkg_signal::ExtraData extra) {
163 if (status_type_ != PKGMGR_CLIENT_STATUS_ALL) {
164 auto it = signal_map.find(signal);
165 if (it == signal_map.end()) {
166 _E("Unexpected signal : %s", signal.c_str());
170 if ((it->second & status_type_) == 0) {
175 HandleResHandler(signal, targetUid, reqId, pkgid, status, extra);
176 HandleGlobalResHandler(signal, targetUid, reqId, pkgid, status, extra);
179 void SignalReceiver::OnAsyncResultForPkgUpgrade(
180 std::string signal, int progress) {
181 for (const auto& [id, cb, data] : global_pkg_upgrade_handlers_) {
186 void SignalReceiver::HandleResHandler(const std::string& signal,
187 int targetUid, const std::string& reqId, const std::string& pkgid,
188 const std::string& status, pkg_signal::ExtraData& extra) const {
189 auto it = res_handlers_.find(reqId);
190 if (it == res_handlers_.end())
192 const auto& [id, cb, data] = it->second;
194 cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
195 static_cast<void*>(&extra), data);
198 void SignalReceiver::HandleGlobalResHandler(const std::string& signal,
199 int targetUid, const std::string& reqId, const std::string& pkgid,
200 const std::string& status, pkg_signal::ExtraData& extra) const {
201 for (const auto& [id, cb, data] : global_res_handlers_) {
202 cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
203 static_cast<void*>(&extra), data);
208 int SignalReceiver::AddEventHandler(std::string req_key,
209 pkgmgr_handler event_cb, void* data) {
210 return AddEventHandler(std::move(req_key), event_cb, nullptr, data);
213 int SignalReceiver::AddEventHandler(std::string req_key,
214 pkgmgr_app_handler app_event_cb, void* data) {
215 return AddEventHandler(std::move(req_key), nullptr, app_event_cb, data);
218 int SignalReceiver::AddEventHandler(std::string req_key,
219 pkgmgr_pkg_size_info_receive_cb event_cb, void* pc, void* data) {
220 int sId = SignalReceiver::GetRequestId();
222 global_size_info_handlers_.emplace_back(sId, event_cb, pc, data);
224 size_info_handlers_[req_key] = { sId, event_cb, pc, data };
228 int SignalReceiver::AddEventHandler(std::string req_key,
229 pkgmgr_handler event_cb, pkgmgr_app_handler app_event_cb, void* data) {
230 int sId = SignalReceiver::GetRequestId();
232 global_handlers_.emplace_back(sId, event_cb, app_event_cb, data);
234 handlers_[req_key] = { sId, event_cb, app_event_cb, data };
238 int SignalReceiver::AddEventHandler(std::string req_key,
239 pkgmgr_res_handler event_cb, void* data) {
240 int sId = SignalReceiver::GetRequestId();
242 global_res_handlers_.emplace_back(sId, event_cb, data);
244 res_handlers_[req_key] = { sId, event_cb, data };
248 int SignalReceiver::AddEventHandler(pkgmgr_pkg_upgrade_handler event_cb,
250 int sId = SignalReceiver::GetRequestId();
251 global_pkg_upgrade_handlers_.emplace_back(sId, event_cb, data);
255 void SignalReceiver::SetStatusType(int status_type) {
256 status_type_ = status_type;
260 void SignalReceiver::RemoveEventHandler() {
262 global_handlers_.clear();
263 res_handlers_.clear();
264 global_res_handlers_.clear();
265 size_info_handlers_.clear();
266 global_size_info_handlers_.clear();
267 global_pkg_upgrade_handlers_.clear();
271 int SignalReceiver::GetRequestId() {
272 return ++request_id_;
275 } // namespace client
276 } // namespace pkgmgr