Implement unimplemented APIs
[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::OnAsyncResultForPkgUpgrade(
124     std::string signal, int progress) {
125   for (const auto& [id, cb, data] : global_pkg_upgrade_handlers_) {
126     cb(progress, data);
127   }
128 }
129
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())
135     return;
136   const auto& [id, cb, data] = it->second;
137
138   cb(targetUid, id, pkgid.c_str(), signal.c_str(), status.c_str(),
139       static_cast<void*>(&extra), data);
140 }
141
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);
148   }
149 }
150
151
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);
155 }
156
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);
160 }
161
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();
165   if (req_key.empty())
166     global_size_info_handlers_.emplace_back(sId, event_cb, pc, data);
167   else
168     size_info_handlers_[req_key] = { sId, event_cb, pc, data };
169   return sId;
170 }
171
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();
175   if (req_key.empty())
176     global_handlers_.emplace_back(sId, event_cb, app_event_cb, data);
177   else
178     handlers_[req_key] = { sId, event_cb, app_event_cb, data };
179   return sId;
180 }
181
182 int SignalReceiver::AddEventHandler(std::string req_key,
183     pkgmgr_res_handler event_cb, void* data) {
184   int sId = SignalReceiver::GetRequestId();
185   if (req_key.empty())
186     global_res_handlers_.emplace_back(sId, event_cb, data);
187   else
188     res_handlers_[req_key] = { sId, event_cb, data };
189   return sId;
190 }
191
192 int SignalReceiver::AddEventHandler(pkgmgr_pkg_upgrade_handler event_cb,
193     void* data) {
194   int sId = SignalReceiver::GetRequestId();
195   global_pkg_upgrade_handlers_.emplace_back(sId, event_cb, data);
196   return sId;
197 }
198
199
200 int SignalReceiver::GetRequestId() {
201   return ++request_id_;
202 }
203
204 }  // namespace client
205 }  // namespace pkgmgr
206