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"
24 #define AGENT_APPID "signal_agent"
26 SignalReceiver::SignalReceiver(bool is_system)
27 : PkgSignal(is_system ? "" : AGENT_APPID, is_system) {
30 SignalReceiver::~SignalReceiver() {
33 void SignalReceiver::OnAsyncResult(std::string signal, int targetUid,
34 std::string reqId, std::vector<pkg_signal::PkgInfo> pkgs,
35 std::string key, std::string val) {
36 HandleHandler(targetUid, reqId, pkgs, key, val);
37 HandleGlobalHandler(targetUid, reqId, pkgs, key, val);
38 HandleSizeInfoHandler(reqId, pkgs, key, val);
41 void SignalReceiver::HandleHandler(int targetUid,
42 const std::string& reqId, const std::vector<pkg_signal::PkgInfo>& pkgs,
43 const std::string& key, const std::string& val) const {
44 const auto it = handlers_.find(reqId);
45 if (it == handlers_.end())
47 const auto& [id, cb, app_cb, data] = it->second;
49 for (auto& i : pkgs) {
51 cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
52 key.c_str(), val.c_str(), nullptr, data);
54 app_cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
55 i.GetAppid().c_str(), key.c_str(), val.c_str(), nullptr, data);
60 void SignalReceiver::HandleGlobalHandler(int targetUid,
61 const std::string& reqId, const std::vector<pkg_signal::PkgInfo>& pkgs,
62 const std::string& key, const std::string& val) const {
63 for (auto& i : pkgs) {
64 for (const auto& [id, cb, app_cb, data] : global_handlers_) {
66 cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
67 key.c_str(), val.c_str(), nullptr, data);
69 app_cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
70 i.GetAppid().c_str(), key.c_str(), val.c_str(), nullptr, data);
76 void SignalReceiver::HandleSizeInfoHandler(
77 const std::string& reqId, const std::vector<pkg_signal::PkgInfo>& pkgs,
78 const std::string& key, const std::string& val) const {
79 auto it = size_info_handlers_.find(reqId);
80 if (it == size_info_handlers_.end())
82 const auto& [id, cb, pc, data] = it->second;
84 std::vector<std::string> tokens;
85 std::istringstream ss(val);
88 while (std::getline(ss, token, ':')) {
89 tokens.push_back(token);
92 if (tokens.size() != 6) {
96 pkg_size_info_t size_info{
97 std::stoll(tokens[0]),
98 std::stoll(tokens[1]),
99 std::stoll(tokens[2]),
100 std::stoll(tokens[3]),
101 std::stoll(tokens[4]),
102 std::stoll(tokens[5]),
105 for (auto& i : pkgs) {
106 if (i.GetPkgid() == std::string(PKG_SIZE_INFO_TOTAL)) {
107 pkgmgr_total_pkg_size_info_receive_cb callback;
108 callback = (pkgmgr_total_pkg_size_info_receive_cb)cb;
109 callback(pc, &size_info, data);
111 cb(pc, i.GetPkgid().c_str(), &size_info, data);
116 void SignalReceiver::OnAsyncResultForResource(std::string signal,
117 int targetUid, std::string reqId, std::string pkgid,
118 std::string status, pkg_signal::ExtraData extra) {
119 HandleResHandler(signal, targetUid, reqId, pkgid, status, extra);
120 HandleGlobalResHandler(signal, targetUid, reqId, pkgid, status, extra);
123 void SignalReceiver::OnAsyncResultForPkgUpgrade(
124 std::string signal, int progress) {
125 for (const auto& [id, cb, data] : global_pkg_upgrade_handlers_) {
130 void SignalReceiver::HandleResHandler(const std::string& signal,
131 int targetUid, const std::string& reqId, const std::string& pkgid,
132 const std::string& status, pkg_signal::ExtraData& extra) const {
133 auto it = res_handlers_.find(reqId);
134 if (it == res_handlers_.end())
136 const auto& [id, cb, data] = it->second;
138 cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
139 static_cast<void*>(&extra), data);
142 void SignalReceiver::HandleGlobalResHandler(const std::string& signal,
143 int targetUid, const std::string& reqId, const std::string& pkgid,
144 const std::string& status, pkg_signal::ExtraData& extra) const {
145 for (const auto& [id, cb, data] : global_res_handlers_) {
146 cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
147 static_cast<void*>(&extra), data);
152 int SignalReceiver::AddEventHandler(std::string req_key,
153 pkgmgr_handler event_cb, void* data) {
154 return AddEventHandler(std::move(req_key), event_cb, nullptr, data);
157 int SignalReceiver::AddEventHandler(std::string req_key,
158 pkgmgr_app_handler app_event_cb, void* data) {
159 return AddEventHandler(std::move(req_key), nullptr, app_event_cb, data);
162 int SignalReceiver::AddEventHandler(std::string req_key,
163 pkgmgr_pkg_size_info_receive_cb event_cb, void* pc, void* data) {
164 int sId = SignalReceiver::GetRequestId();
166 global_size_info_handlers_.emplace_back(sId, event_cb, pc, data);
168 size_info_handlers_[req_key] = { sId, event_cb, pc, data };
172 int SignalReceiver::AddEventHandler(std::string req_key,
173 pkgmgr_handler event_cb, pkgmgr_app_handler app_event_cb, void* data) {
174 int sId = SignalReceiver::GetRequestId();
176 global_handlers_.emplace_back(sId, event_cb, app_event_cb, data);
178 handlers_[req_key] = { sId, event_cb, app_event_cb, data };
182 int SignalReceiver::AddEventHandler(std::string req_key,
183 pkgmgr_res_handler event_cb, void* data) {
184 int sId = SignalReceiver::GetRequestId();
186 global_res_handlers_.emplace_back(sId, event_cb, data);
188 res_handlers_[req_key] = { sId, event_cb, data };
192 int SignalReceiver::AddEventHandler(pkgmgr_pkg_upgrade_handler event_cb,
194 int sId = SignalReceiver::GetRequestId();
195 global_pkg_upgrade_handlers_.emplace_back(sId, event_cb, data);
200 int SignalReceiver::GetRequestId() {
201 return ++request_id_;
204 } // namespace client
205 } // namespace pkgmgr