Refactor slp-pkgmgr with tidl
[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 namespace pkgmgr {
22 namespace client {
23
24 #define AGENT_APPID "signal_agent"
25
26 SignalReceiver::SignalReceiver(bool is_system)
27     : PkgSignal(is_system ? "" : AGENT_APPID, is_system) {
28 }
29
30 SignalReceiver::~SignalReceiver() {
31 }
32
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);
39 }
40
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())
46     return;
47   const auto& [id, cb, app_cb, data] = it->second;
48
49   for (auto& i : pkgs) {
50     if (cb) {
51       cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
52           key.c_str(), val.c_str(), nullptr, data);
53     } else {
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);
56     }
57   }
58 }
59
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_) {
65       if (cb) {
66         cb(targetUid, id, i.GetPkgType().c_str(), i.GetPkgid().c_str(),
67             key.c_str(), val.c_str(), nullptr, data);
68       } else {
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);
71       }
72     }
73   }
74 }
75
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())
81     return;
82   const auto& [id, cb, pc, data] = it->second;
83
84   std::vector<std::string> tokens;
85   std::istringstream ss(val);
86   std::string token;
87
88   while (std::getline(ss, token, ':')) {
89       tokens.push_back(token);
90   }
91
92   if (tokens.size() != 6) {
93         return;
94     }
95
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]),
103   };
104
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);
110     } else {
111       cb(pc, i.GetPkgid().c_str(), &size_info, data);
112     }
113   }
114 }
115
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);
121 }
122
123 void SignalReceiver::HandleResHandler(const std::string& signal,
124     int targetUid, const std::string& reqId, const std::string& pkgid,
125     const std::string& status, pkg_signal::ExtraData& extra) const {
126   auto it = res_handlers_.find(reqId);
127   if (it == res_handlers_.end())
128     return;
129   const auto& [id, cb, data] = it->second;
130
131   cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
132       static_cast<void*>(&extra), data);
133 }
134
135 void SignalReceiver::HandleGlobalResHandler(const std::string& signal,
136     int targetUid, const std::string& reqId, const std::string& pkgid,
137     const std::string& status, pkg_signal::ExtraData& extra) const {
138   for (const auto& [id, cb, data] : global_res_handlers_) {
139     cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
140         static_cast<void*>(&extra), data);
141   }
142 }
143
144
145 int SignalReceiver::AddEventHandler(std::string req_key,
146     pkgmgr_handler event_cb, void* data) {
147   return AddEventHandler(std::move(req_key), event_cb, nullptr, data);
148 }
149
150 int SignalReceiver::AddEventHandler(std::string req_key,
151     pkgmgr_app_handler app_event_cb, void* data) {
152   return AddEventHandler(std::move(req_key), nullptr, app_event_cb, data);
153 }
154
155 int SignalReceiver::AddEventHandler(std::string req_key,
156       pkgmgr_pkg_size_info_receive_cb event_cb, void* pc, void* data) {
157   int sId = SignalReceiver::GetRequestId();
158   if (req_key.empty())
159     global_size_info_handlers_.emplace_back(sId, event_cb, pc, data);
160   else
161     size_info_handlers_[req_key] = { sId, event_cb, pc, data };
162   return sId;
163 }
164
165 int SignalReceiver::AddEventHandler(std::string req_key,
166     pkgmgr_handler event_cb, pkgmgr_app_handler app_event_cb, void* data) {
167   int sId = SignalReceiver::GetRequestId();
168   if (req_key.empty())
169     global_handlers_.emplace_back(sId, event_cb, app_event_cb, data);
170   else
171     handlers_[req_key] = { sId, event_cb, app_event_cb, data };
172   return sId;
173 }
174
175 int SignalReceiver::AddEventHandler(std::string req_key,
176     pkgmgr_res_handler event_cb, void* data) {
177   int sId = SignalReceiver::GetRequestId();
178   if (req_key.empty())
179     global_res_handlers_.emplace_back(sId, event_cb, data);
180   else
181     res_handlers_[req_key] = { sId, event_cb, data };
182   return sId;
183 }
184
185
186 int SignalReceiver::GetRequestId() {
187   return ++request_id_;
188 }
189
190 }  // namespace client
191 }  // namespace pkgmgr
192