2 * Copyright (c) 2015 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
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.
17 #include "include/aul_app_group.h"
19 #include <bundle_cpp.h>
25 #include "app_request.h"
28 #include "include/aul.h"
29 #include "include/aul_error.h"
30 #include "include/aul_sock.h"
33 #include "aul/app_group/app_group_info.hh"
34 #include "aul/common/exception.hh"
37 using namespace aul::internal;
39 struct aul_app_group_info_s {
45 constexpr const int kMaxPacketLength = AUL_SOCK_MAXBUFF - AUL_PKT_HEADER_SIZE;
47 AppGroupInfo* CreateAppGroupInfo(const tizen_base::Bundle& b) {
48 return AppGroupInfo::Builder()
58 std::vector<std::shared_ptr<AppGroupInfo>> ReceiveAppGroupInfos(int fd) {
59 std::vector<std::shared_ptr<AppGroupInfo>> infos;
60 int ret = aul_sock_recv_pkt_with_cb(fd,
61 [](app_pkt_t* pkt, void* user_data) {
62 if (pkt->cmd == APP_GET_INFO_ERROR || !(pkt->opt & AUL_SOCK_BUNDLE))
65 bundle* kb = bundle_decode(pkt->data, pkt->len);
67 _E("bundle_decode() is failed");
71 tizen_base::Bundle b(kb, false, true);
73 auto* info = CreateAppGroupInfo(b);
74 if (info == nullptr) {
80 static_cast<std::vector<std::shared_ptr<AppGroupInfo>>*>(
82 info_array->emplace_back(info);
83 } catch (const Exception& e) {
84 _E("Exception occurs. error(%s)", e.what());
88 THROW(aul_error_convert(ret));
93 std::vector<std::string> ReceiveLeaderIds(int fd) {
94 app_pkt_t* pkt = nullptr;
95 int ret = aul_sock_recv_reply_pkt(fd, &pkt);
96 if (ret < 0 || pkt == nullptr)
99 auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
102 bundle* kb = bundle_decode(pkt->data, pkt->len);
104 _E("bundle_decode() is failed. data(%s:%d)", pkt->data, pkt->len);
108 tizen_base::Bundle b(kb, false, true);
109 return b.GetStringArray(AUL_K_LEADER_IDS);
112 void ReceivePids(int fd, int* cnt, int** pids) {
113 app_pkt_t* pkt = nullptr;
114 int ret = aul_sock_recv_reply_pkt(fd, &pkt);
115 if (ret < 0 || pkt == nullptr)
118 auto pkt_auto = std::unique_ptr<app_pkt_t, decltype(std::free)*>(
121 int count = pkt->len / sizeof(int);
122 if (count > 0 && pkt->len <= kMaxPacketLength) {
123 *pids = reinterpret_cast<int*>(calloc(1, pkt->len));
124 if (*pids == nullptr) {
129 memcpy(*pids, pkt->data, pkt->len);
136 extern "C" API int aul_app_group_get_window(int pid) {
138 _E("Invalid parameter");
142 return AppRequest(APP_GROUP_GET_WINDOW, getuid())
147 extern "C" API int aul_app_group_set_window(int wid) {
149 _E("Invalid parameter");
153 tizen_base::Bundle b { { AUL_K_WID, std::to_string(wid) } };
154 return AppRequest(APP_GROUP_SET_WINDOW, getuid())
156 .SendSimply(AUL_SOCK_NOREPLY);
159 extern "C" API void aul_app_group_get_leader_pids(int* cnt, int** pids) {
160 if (cnt == nullptr || pids == nullptr) {
161 _E("Invalid parameter");
168 int fd = AppRequest(APP_GROUP_GET_LEADER_PIDS, getuid())
169 .SendCmdOnly(AUL_SOCK_ASYNC);
174 ReceivePids(fd, cnt, pids);
175 } catch (const Exception& e) {
176 _E("Exception occurs. error(%s)", e.what());
180 extern "C" API void aul_app_group_get_group_pids(int leader_pid, int* cnt,
182 if (leader_pid < 1 || cnt == nullptr || pids == nullptr) {
183 _E("Invalid parameter");
190 tizen_base::Bundle b { { AUL_K_LEADER_PID, std::to_string(leader_pid) } };
191 int fd = AppRequest(APP_GROUP_GET_GROUP_PIDS, getuid())
193 .SendSimply(AUL_SOCK_ASYNC);
198 ReceivePids(fd, cnt, pids);
199 } catch (const Exception& e) {
200 _E("Exception occurs. error(%s)", e.what());
204 extern "C" API int aul_app_group_get_leader_pid(int pid) {
206 _E("Invalid parameter");
210 return AppRequest(APP_GROUP_GET_LEADER_PID, getuid())
215 extern "C" API int aul_app_group_clear_top(void) {
216 return AppRequest(APP_GROUP_CLEAR_TOP, getuid())
220 extern "C" API int aul_app_group_is_top(void) {
221 int leader_pid = aul_app_group_get_leader_pid(getpid());
227 aul_app_group_get_group_pids(leader_pid, &cnt, &pids);
228 auto pids_auto = std::unique_ptr<int, decltype(std::free)*>(pids, std::free);
229 if (cnt > 0 && pids != nullptr) {
230 if (pids[cnt - 1] == getpid())
239 extern "C" API int aul_app_group_get_fg_flag(int pid) {
241 _E("Invalid parameter");
245 return AppRequest(APP_GROUP_GET_FG, getuid())
250 extern "C" API void aul_app_group_lower(int* exit) {
251 if (exit == nullptr) {
252 _E("Invalid parameter");
256 int ret = AppRequest(APP_GROUP_LOWER, getuid())
264 extern "C" API void aul_app_group_get_idle_pids(int* cnt, int** pids) {
265 if (cnt == nullptr || pids == nullptr) {
266 _E("Invalid parameter");
272 int fd = AppRequest(APP_GROUP_GET_IDLE_PIDS, getuid())
273 .SendCmdOnly(AUL_SOCK_ASYNC);
278 ReceivePids(fd, cnt, pids);
279 } catch (const Exception& e) {
280 _E("Exception occurs. error(%s)", e.what());
284 extern "C" API int aul_app_group_activate_below(const char* below_appid) {
285 if (below_appid == nullptr) {
286 _E("Invalid parameter");
290 return AppRequest(APP_GROUP_ACTIVATE_BELOW, getuid())
291 .SetAppId(below_appid)
295 extern "C" API int aul_app_group_activate_above(const char* above_appid) {
296 if (above_appid == nullptr) {
297 _E("Invalid parameter");
301 return AppRequest(APP_GROUP_ACTIVATE_ABOVE, getuid())
302 .SetAppId(above_appid)
306 extern "C" API int aul_app_group_set_window_v2(const char* inst_id, int wid) {
307 if (inst_id == nullptr || wid < 1) {
308 _E("Invalid parameter");
312 tizen_base::Bundle b {
313 { AUL_K_INSTANCE_ID, inst_id },
314 { AUL_K_WID, std::to_string(wid) }
317 return AppRequest(APP_GROUP_SET_WINDOW_V2, getuid())
319 .SendSimply(AUL_SOCK_NOREPLY);
322 extern "C" API int aul_app_group_lower_v2(const char* inst_id, bool* exit) {
323 if (inst_id == nullptr || exit == nullptr) {
324 _E("Invalid parameter");
328 int ret = AppRequest(APP_GROUP_LOWER_V2, getuid())
334 *exit = (ret == 0) ? false : true;
338 extern "C" API int aul_app_group_foreach_leader_ids(
339 aul_app_group_leader_id_cb callback, void* user_data) {
340 if (callback == nullptr) {
341 _E("Invalid parameter");
345 int fd = AppRequest(APP_GROUP_GET_LEADER_IDS, getuid())
346 .SendCmdOnly(AUL_SOCK_ASYNC);
348 return aul_error_convert(fd);
351 for (auto const& id : ReceiveLeaderIds(fd))
352 callback(id.c_str(), user_data);
353 } catch (const Exception& e) {
354 _E("Exception occurs. error(%s)", e.what());
355 return e.GetErrorCode();
361 extern "C" API int aul_app_group_foreach_group_info(const char* leader_id,
362 aul_app_group_info_cb callback, void* user_data) {
363 if (leader_id == nullptr || callback == nullptr) {
364 _E("Invalid parameter");
368 tizen_base::Bundle b { { AUL_K_LEADER_ID, leader_id } };
369 int fd = AppRequest(APP_GROUP_GET_GROUP_INFO, getuid())
371 .SendSimply(AUL_SOCK_ASYNC);
373 return aul_error_convert(fd);
376 for (auto const& info : ReceiveAppGroupInfos(fd))
377 callback(reinterpret_cast<aul_app_group_info_h>(info.get()), user_data);
378 } catch (const Exception& e) {
379 _E("Exception occurs. error(%s)", e.what());
385 extern "C" API int aul_app_group_foreach_idle_info(
386 aul_app_group_info_cb callback, void* user_data) {
387 if (callback == nullptr) {
388 _E("Invalid parameter");
392 int fd = AppRequest(APP_GROUP_GET_IDLE_INFO, getuid())
393 .SendCmdOnly(AUL_SOCK_ASYNC);
395 return aul_error_convert(fd);
398 for (auto const& info : ReceiveAppGroupInfos(fd))
399 callback(reinterpret_cast<aul_app_group_info_h>(info.get()), user_data);
400 } catch (const Exception& e) {
401 _E("Exception occurs. error(%s)", e.what());
407 extern "C" API int aul_app_group_info_get_id(aul_app_group_info_h h,
409 if (h == nullptr || id == nullptr) {
410 _E("Invalid parameter");
414 auto* info = reinterpret_cast<AppGroupInfo*>(h);
415 *id = info->GetId().c_str();
419 extern "C" API int aul_app_group_info_get_pid(aul_app_group_info_h h,
421 if (h == nullptr || pid == nullptr) {
422 _E("Invalid parameter");
426 auto* info = reinterpret_cast<AppGroupInfo*>(h);
427 *pid = info->GetPid();
431 extern "C" API int aul_app_group_info_get_appid(aul_app_group_info_h h,
432 const char** appid) {
433 if (h == nullptr || appid == nullptr) {
434 _E("Invalid parameter");
438 auto* info = reinterpret_cast<AppGroupInfo*>(h);
439 *appid = info->GetAppId().c_str();
443 extern "C" API int aul_app_group_info_get_pkgid(aul_app_group_info_h h,
444 const char** pkgid) {
445 if (h == nullptr || pkgid == nullptr) {
446 _E("Invalid parameter");
450 auto* info = reinterpret_cast<AppGroupInfo*>(h);
451 *pkgid = info->GetPkgId().c_str();
455 extern "C" API int aul_app_group_info_get_window(aul_app_group_info_h h,
457 if (h == nullptr || wid == nullptr) {
458 _E("Invalid parameter");
462 auto* info = reinterpret_cast<AppGroupInfo*>(h);
463 *wid = info->GetWid();
467 extern "C" API int aul_app_group_info_get_fg_flag(aul_app_group_info_h h,
469 if (h == nullptr || fg_flag == nullptr) {
470 _E("Invalid parameter");
474 auto* info = reinterpret_cast<AppGroupInfo*>(h);
475 *fg_flag = info->IsFg();
479 extern "C" API int aul_app_group_info_get_status(aul_app_group_info_h h,
481 if (h == nullptr || status == nullptr) {
482 _E("Invalid parameter");
486 auto* info = reinterpret_cast<AppGroupInfo*>(h);
487 *status = info->GetStatus();
491 extern "C" API int aul_app_group_add(int wid) {
493 _E("Invalid parameter");
497 tizen_base::Bundle b { { AUL_K_WID, std::to_string(wid) } };
498 return AppRequest(APP_GROUP_ADD, getuid())
500 .SendSimply(AUL_SOCK_NOREPLY);
503 extern "C" API int aul_app_group_remove(int wid) {
505 _E("Invalid parameter");
509 tizen_base::Bundle b { { AUL_K_WID, std::to_string(wid) } };
510 return AppRequest(APP_GROUP_REMOVE, getuid())
512 .SendSimply(AUL_SOCK_NOREPLY);