Impelement pkgmgr_client_remove_listen_status
[platform/core/appfw/slp-pkgmgr.git] / client / src / signal_receiver.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <sstream>
18
19 #include "signal_receiver.hh"
20
21 #include "log.hh"
22 #include "../../installer/src/pkgmgr_installer.h"
23
24 namespace pkgmgr {
25 namespace client {
26
27 #define AGENT_APPID "signal_agent"
28
29 SignalReceiver::SignalReceiver(bool is_system, int status_type)
30     : PkgSignal(is_system ? "" : AGENT_APPID, is_system),
31       status_type_(status_type) {
32 }
33
34 SignalReceiver::~SignalReceiver() {
35 }
36
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}
63 };
64
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());
72       return;
73     }
74
75     if ((it->second & status_type_) == 0) {
76       return;
77     }
78   }
79
80   HandleHandler(targetUid, reqId, pkgs, key, val);
81   HandleGlobalHandler(targetUid, reqId, pkgs, key, val);
82   HandleSizeInfoHandler(reqId, pkgs, key, val);
83 }
84
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())
90     return;
91   const auto& [id, cb, app_cb, data] = it->second;
92
93   for (auto& i : pkgs) {
94     if (cb) {
95       cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
96           key.c_str(), val.c_str(), nullptr, data);
97     } else {
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);
100     }
101   }
102 }
103
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_) {
109       if (cb) {
110         cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
111             key.c_str(), val.c_str(), nullptr, data);
112       } else {
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);
115       }
116     }
117   }
118 }
119
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())
125     return;
126   const auto& [id, cb, pc, data] = it->second;
127
128   std::vector<std::string> tokens;
129   std::istringstream ss(val);
130   std::string token;
131
132   while (std::getline(ss, token, ':')) {
133       tokens.push_back(token);
134   }
135
136   if (tokens.size() != 6) {
137         return;
138     }
139
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]),
147   };
148
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);
154     } else {
155       cb(pc, i.GetPkgid().c_str(), &size_info, data);
156     }
157   }
158 }
159
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());
167       return;
168     }
169
170     if ((it->second & status_type_) == 0) {
171       return;
172     }
173   }
174
175   HandleResHandler(signal, targetUid, reqId, pkgid, status, extra);
176   HandleGlobalResHandler(signal, targetUid, reqId, pkgid, status, extra);
177 }
178
179 void SignalReceiver::OnAsyncResultForPkgUpgrade(
180     std::string signal, int progress) {
181   for (const auto& [id, cb, data] : global_pkg_upgrade_handlers_) {
182     cb(progress, data);
183   }
184 }
185
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())
191     return;
192   const auto& [id, cb, data] = it->second;
193
194   cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
195       static_cast<void*>(&extra), data);
196 }
197
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);
204   }
205 }
206
207
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);
211 }
212
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);
216 }
217
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();
221   if (req_key.empty())
222     global_size_info_handlers_.emplace_back(sId, event_cb, pc, data);
223   else
224     size_info_handlers_[req_key] = { sId, event_cb, pc, data };
225   return sId;
226 }
227
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();
231   if (req_key.empty())
232     global_handlers_.emplace_back(sId, event_cb, app_event_cb, data);
233   else
234     handlers_[req_key] = { sId, event_cb, app_event_cb, data };
235   return sId;
236 }
237
238 int SignalReceiver::AddEventHandler(std::string req_key,
239     pkgmgr_res_handler event_cb, void* data) {
240   int sId = SignalReceiver::GetRequestId();
241   if (req_key.empty())
242     global_res_handlers_.emplace_back(sId, event_cb, data);
243   else
244     res_handlers_[req_key] = { sId, event_cb, data };
245   return sId;
246 }
247
248 int SignalReceiver::AddEventHandler(pkgmgr_pkg_upgrade_handler event_cb,
249     void* data) {
250   int sId = SignalReceiver::GetRequestId();
251   global_pkg_upgrade_handlers_.emplace_back(sId, event_cb, data);
252   return sId;
253 }
254
255 void SignalReceiver::SetStatusType(int status_type) {
256   status_type_ = status_type;
257 }
258
259
260 void SignalReceiver::RemoveEventHandler() {
261   handlers_.clear();
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();
268 }
269
270
271 int SignalReceiver::GetRequestId() {
272   return ++request_id_;
273 }
274
275 }  // namespace client
276 }  // namespace pkgmgr
277