Add to set res_fn_ to nullptr
[platform/core/appfw/aul-1.git] / src / aul_svc.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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 <bundle.h>
18 #include <bundle_cpp.h>
19 #include <bundle_internal.h>
20 #include <glib.h>
21 #include <iniparser.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 #include <memory>
30 #include <string>
31 #include <vector>
32
33 #include "aul/app_control/resolve_info.hh"
34 #include "include/aul.h"
35 #include "include/aul_app_group.h"
36 #include "include/aul_error.h"
37 #include "include/aul_sock.h"
38 #include "include/aul_svc.h"
39 #include "include/aul_svc_internal.h"
40 #include "src/aul_api.h"
41 #include "src/aul_svc_priv_key.h"
42 #include "src/aul_util.h"
43 #include "src/launch.h"
44
45 #undef MAX_MIME_STR_SIZE
46 #define MAX_MIME_STR_SIZE 256
47
48 #undef MAX_SCHEME_STR_SIZE
49 #define MAX_SCHEME_STR_SIZE 256
50
51 #undef MAX_HOST_STR_SIZE
52 #define MAX_HOST_STR_SIZE 256
53
54 #undef DEPRECATION_WARNING
55 #define DEPRECATION_WARNING() do {                                             \
56     dlog_print(DLOG_WARN, LOG_TAG,                                             \
57         "DEPRECATION WARNING: %s() is deprecated and "                         \
58         "will be removed from next release.", __FUNCTION__);                   \
59 } while (0)
60
61 namespace {
62
63 class CbInfo {
64  public:
65   CbInfo(int request_code, aul_svc_res_fn res_fn,
66       aul_svc_err_cb err_cb, void* user_data)
67       : request_code_(request_code),
68         res_fn_(res_fn),
69         err_cb_(err_cb),
70         user_data_(user_data) {
71   }
72
73   int request_code_;
74   aul_svc_res_fn res_fn_;
75   aul_svc_err_cb err_cb_;
76   void* user_data_;
77 };
78
79 int SetBundle(bundle* b, const char* key, const char* value) {
80   if (bundle_get_type(b, key) != BUNDLE_TYPE_NONE) {
81     if (bundle_del(b, key) != BUNDLE_ERROR_NONE)
82       return AUL_SVC_RET_ERROR;
83   }
84
85   if (value == nullptr)
86     return AUL_SVC_RET_EINVAL;
87
88   if (bundle_add(b, key, value) != BUNDLE_ERROR_NONE)
89     return AUL_SVC_RET_ERROR;
90
91   SECURE_LOGD("key(%s), value(%s)", key, value);
92   return AUL_SVC_RET_OK;
93 }
94
95 int SetBundleArray(bundle* b, const char* key, const char** value,
96     int len) {
97   int is_array = aul_svc_data_is_array(b, key);
98   if (is_array) {
99     if (bundle_del(b, key) != BUNDLE_ERROR_NONE)
100       return AUL_SVC_RET_ERROR;
101   }
102
103   if (value == nullptr)
104     return AUL_SVC_RET_EINVAL;
105
106   if (bundle_add_str_array(b, key, value, len) != BUNDLE_ERROR_NONE)
107     return AUL_SVC_RET_ERROR;
108
109   SECURE_LOGD("key(%s), length(%d)", key, len);
110   return AUL_SVC_RET_OK;
111 }
112
113 std::string GetAliasAppId(const char* appid) {
114   dictionary* dic = iniparser_load("/usr/share/appsvc/alias.ini");
115   if (dic == nullptr)
116     return {};
117
118   auto dic_ptr = std::unique_ptr<dictionary, decltype(iniparser_freedict)*>(
119       dic, iniparser_freedict);
120
121   std::string key = std::string("Alias:") + appid;
122   const char* value = iniparser_getstring(dic, key.c_str(), nullptr);
123   SECURE_LOGD("appid(%s), alias_id(%s)", appid, value);
124   if (value == nullptr)
125     return {};
126
127   return std::string(value);
128 }
129
130 bool IsSpecialApp(const char* appid) {
131   if (!strcmp(appid, APP_SELECTOR) || !strcmp(appid, SHARE_PANEL))
132     return true;
133
134   return false;
135 }
136
137 bool IsSpecialOperation(const char* operation) {
138   if (operation == nullptr)
139     return false;
140
141   int ret = strcmp(operation,
142       "http://tizen.org/appcontrol/operation/guide_privacy_setting");
143   if (ret == 0)
144     return true;
145
146   return false;
147 }
148
149 std::string GetAppId(bundle* request) {
150   const char* appid = aul_svc_get_pkgname(request);
151   if (appid == nullptr) {
152     if (aul_svc_get_operation(request) == nullptr) {
153       _E("Invalid request");
154       return {};
155     }
156
157     appid = "@UNKNOWN";
158   }
159
160   int ret = bundle_get_type(request, AUL_SVC_K_SELECTOR_EXTRA_LIST);
161   if (ret != BUNDLE_TYPE_NONE) {
162     if (appid == nullptr || !strcmp(appid, "@UNKNOWN"))
163       appid = APP_SELECTOR;
164   }
165
166   ret = bundle_get_type(request, AUL_K_FORCE_LAUNCH_APP_SELECTOR);
167   if (ret != BUNDLE_TYPE_NONE)
168     appid = APP_SELECTOR;
169
170   return std::string(appid);
171 }
172
173 void SetLaunchData(bundle* request, const std::string& appid) {
174   const char* operation = aul_svc_get_operation(request);
175   if (operation == nullptr)
176     aul_svc_set_operation(request, AUL_SVC_OPERATION_DEFAULT);
177
178   if (IsSpecialApp(appid.c_str()) || IsSpecialOperation(operation)) {
179     SetBundle(request, AUL_SVC_K_CAN_BE_LEADER, "true");
180     SetBundle(request, AUL_SVC_K_REROUTE, "true");
181     SetBundle(request, AUL_SVC_K_RECYCLE, "true");
182   }
183
184   const char* launch_mode = aul_svc_get_launch_mode(request);
185   if (launch_mode && !strcmp(launch_mode, "group")) {
186     int ret = bundle_get_type(request, AUL_K_INSTANCE_ID);
187     if (ret != BUNDLE_TYPE_NONE)
188       aul_set_instance_info(appid.c_str(), request);
189   }
190 }
191
192 int AulErrorConvert(int res) {
193   switch (res) {
194   case AUL_R_EILLACC:
195     return AUL_SVC_RET_EILLACC;
196   case AUL_R_EINVAL:
197     return AUL_SVC_RET_EINVAL;
198   case AUL_R_ETERMINATING:
199     return AUL_SVC_RET_ETERMINATING;
200   case AUL_R_EREJECTED:
201     return AUL_SVC_RET_EREJECTED;
202   case AUL_R_ENOAPP:
203     return AUL_SVC_RET_ENOMATCH;
204   case AUL_R_ECANCELED:
205     return AUL_SVC_RET_ECANCELED;
206   default:
207     return AUL_SVC_RET_ELAUNCH;
208   }
209 }
210
211 void LaunchWithResultCb(bundle* b, int is_cancel, void* data) {
212   int res;
213   if (is_cancel) {
214     res = AUL_SVC_RES_CANCEL;
215   } else {
216     const char* val = bundle_get_val(b, AUL_SVC_K_RES_VAL);
217     res = (val == nullptr) ? AUL_SVC_RES_NOT_OK : atoi(val);
218   }
219
220   bundle_del(b, AUL_SVC_K_RES_VAL);
221   auto* cb_info = static_cast<CbInfo*>(data);
222   if (cb_info == nullptr) {
223     _E("Invalid parameter");
224     return;
225   }
226
227   if (cb_info->res_fn_) {
228     cb_info->res_fn_(b, cb_info->request_code_,
229         static_cast<aul_svc_result_val>(res), cb_info->user_data_);
230     cb_info->res_fn_ = nullptr;
231   }
232
233   if (cb_info->err_cb_ != nullptr)
234     return;
235
236   delete cb_info;
237 }
238
239 void ErrorCb(int error, void* data) {
240   if (error < 0)
241     error = AulErrorConvert(error);
242
243   auto* cb_info = static_cast<CbInfo*>(data);
244   if (cb_info == nullptr) {
245     _E("Invalid parameter");
246     return;
247   }
248
249   if (cb_info->err_cb_) {
250     cb_info->err_cb_(cb_info->request_code_, error, cb_info->user_data_);
251     cb_info->err_cb_ = nullptr;
252   }
253
254   if (cb_info->res_fn_)
255     return;
256
257   delete cb_info;
258 }
259
260 using SendLaunchRequestCb =
261     int (*)(const std::string&, bundle*, uid_t, CbInfo*);
262 using SendLaunchRequestSyncCb =
263     int (*)(const std::string&, bundle*, uid_t, bundle**);
264
265 template <typename T, typename A>
266 int SendLaunchRequest(T cb, bundle* request, uid_t uid, A arg) {
267   if (request == nullptr) {
268     _E("Invalid parameter");
269     return AUL_SVC_RET_EINVAL;
270   }
271
272   std::string appid = GetAppId(request);
273   if (appid.empty()) {
274     _E("GetAppId() is failed");
275     return AUL_SVC_RET_EINVAL;
276   }
277
278   SetLaunchData(request, appid);
279   return cb(appid, request, uid, arg);
280 }
281
282 int SendAndReceive(int cmd, uid_t uid, bundle* request, bundle** response) {
283   int fd = aul_sock_send_bundle(AUL_UTIL_PID, uid, cmd, request,
284       AUL_SOCK_ASYNC);
285   if (fd < 0)
286     return AUL_SVC_RET_ERROR;
287
288   app_pkt_t* pkt = nullptr;
289   int ret = aul_sock_recv_reply_pkt(fd, &pkt);
290   if (ret < 0) {
291     _E("Failed to receive reply packet. error(%d)", ret);
292     return AUL_SVC_RET_ERROR;
293   }
294
295   auto ptr = std::unique_ptr<app_pkt_t, decltype(std::free)*>(pkt, std::free);
296   if (pkt->cmd != APP_GET_INFO_OK && pkt->cmd != cmd) {
297     if (pkt->cmd == APP_GET_INFO_ERROR)
298       return AUL_SVC_RET_ERROR;
299
300     return AulErrorConvert(aul_error_convert(pkt->cmd));
301   }
302
303   bundle* b = nullptr;
304   if (pkt->opt & AUL_SOCK_BUNDLE) {
305     b = bundle_decode(pkt->data, pkt->len);
306     if (b == nullptr) {
307       _E("bundle_decode() is failed");
308       return AUL_SVC_RET_ENOMEM;
309     }
310   } else {
311     _E("Invalid packet");
312     return AUL_SVC_RET_ERROR;
313   }
314
315   *response = b;
316   return AUL_SVC_RET_OK;
317 }
318
319 }  // namespace
320
321 extern "C" API int aul_svc_set_operation(bundle* b, const char* operation) {
322   if (b == nullptr) {
323     _E("Invalid parameter");
324     return AUL_SVC_RET_EINVAL;
325   }
326
327   return ::SetBundle(b, AUL_SVC_K_OPERATION, operation);
328 }
329
330 extern "C" API int aul_svc_set_uri(bundle* b, const char* uri) {
331   if (b == nullptr) {
332     _E("Invalid parameter");
333     return AUL_SVC_RET_EINVAL;
334   }
335
336   return ::SetBundle(b, AUL_SVC_K_URI, uri);
337 }
338
339 extern "C" API int aul_svc_set_mime(bundle* b, const char* mime) {
340   if (b == nullptr) {
341     _E("Invalid parameter");
342     return AUL_SVC_RET_EINVAL;
343   }
344
345   return ::SetBundle(b, AUL_SVC_K_MIME, mime);
346 }
347
348 extern "C" API int aul_svc_add_data(bundle* b, const char* key,
349     const char* value) {
350   if (b == nullptr || key == nullptr)
351     return AUL_SVC_RET_EINVAL;
352
353   return ::SetBundle(b, key, value);
354 }
355
356 extern "C" API int aul_svc_add_data_array(bundle* b, const char* key,
357     const char** value, int len) {
358   if (b == nullptr || key == nullptr)
359     return AUL_SVC_RET_EINVAL;
360
361   return ::SetBundleArray(b, key, value, len);
362 }
363
364 extern "C" API int aul_svc_set_pkgname(bundle* b, const char* pkg_name) {
365   if (b == nullptr) {
366     _E("Invalid parameter");
367     return AUL_SVC_RET_EINVAL;
368   }
369
370   return ::SetBundle(b, AUL_SVC_K_PKG_NAME, pkg_name);
371 }
372
373 extern "C" API int aul_svc_set_appid(bundle* b, const char* appid) {
374   if (b == nullptr || appid == nullptr) {
375     _E("Invalid parameter");
376     return AUL_SVC_RET_EINVAL;
377   }
378
379   std::string alias_id = ::GetAliasAppId(appid);
380   if (!alias_id.empty())
381     appid = alias_id.c_str();
382
383   return ::SetBundle(b, AUL_SVC_K_PKG_NAME, appid);
384 }
385
386 extern "C" API int aul_svc_set_category(bundle* b, const char* category) {
387   if (b == nullptr) {
388     _E("Invalid parameter");
389     return AUL_SVC_RET_EINVAL;
390   }
391
392   return ::SetBundle(b, AUL_SVC_K_CATEGORY, category);
393 }
394
395 extern "C" API int aul_svc_set_launch_mode(bundle* b, const char* mode) {
396   if (b == nullptr) {
397     _E("Invalid parameter");
398     return AUL_SVC_RET_EINVAL;
399   }
400
401   return ::SetBundle(b, AUL_SVC_K_LAUNCH_MODE, mode);
402 }
403
404 extern "C" API int aul_svc_resolve(bundle* b, uid_t uid, char*** appid_array,
405     unsigned int* len) {
406   return aul_svc_get_appid_array(b, uid, appid_array, len);
407 }
408
409 extern "C" API int aul_svc_run_service(bundle* b, int request_code,
410     aul_svc_res_fn cbfunc, void* data) {
411   return aul_svc_run_service_for_uid(b, request_code, cbfunc, data, getuid());
412 }
413
414 extern "C" API int aul_svc_run_service_for_uid(bundle* b, int request_code,
415   aul_svc_res_fn cbfunc, void* data, uid_t uid) {
416   CbInfo* cb_info = nullptr;
417   if (cbfunc)
418     cb_info = new (std::nothrow) CbInfo(request_code, cbfunc, nullptr, data);
419   if (cb_info == nullptr)
420     LOGE("Out of memory");
421
422   std::tuple<aul_svc_res_fn, void*> param { cbfunc, data };
423   int ret = ::SendLaunchRequest<::SendLaunchRequestCb, ::CbInfo*>(
424       [](const std::string& appid, bundle* request, uid_t uid,
425           CbInfo* cb_info) -> int {
426         int ret;
427         if (cb_info) {
428           ret = aul_launch_app_with_result_for_uid(appid.c_str(), request,
429               LaunchWithResultCb, cb_info, uid);
430          } else {
431           ret = aul_launch_app_for_uid(appid.c_str(), request, uid);
432         }
433
434         return ret;
435       }, b, uid, cb_info);
436   if (ret < 0) {
437     if (cb_info)
438       delete cb_info;
439
440     ret = AulErrorConvert(ret);
441   }
442
443   return ret;
444 }
445
446 extern "C" API int aul_svc_get_list(bundle* b, aul_svc_info_iter_fn iter_fn,
447     void* data) {
448   return aul_svc_get_list_for_uid(b, iter_fn, data, getuid());
449 }
450
451 extern "C" API int aul_svc_get_list_for_uid(bundle* b,
452     aul_svc_info_iter_fn iter_fn, void* data, uid_t uid) {
453   if (b == nullptr || iter_fn == nullptr) {
454     _E("Invalid parameter");
455     return AUL_SVC_RET_EINVAL;
456   }
457
458   char** appid_array = nullptr;
459   unsigned int len = 0;
460   int ret = aul_svc_get_appid_array(b, uid, &appid_array, &len);
461   if (ret != AUL_SVC_RET_OK)
462     return ret;
463
464   if (len == 0) {
465     _E("Failed to find associated application");
466     aul_svc_free_appid_array(appid_array, len);
467     return AUL_SVC_RET_ENOMATCH;
468   }
469
470   for (unsigned int i = 0; i < len; ++i) {
471     SECURE_LOGD("APPID: %s", appid_array[i]);
472     if (iter_fn(appid_array[i], data) != 0)
473       break;
474   }
475
476   aul_svc_free_appid_array(appid_array, len);
477   return AUL_SVC_RET_OK;
478 }
479
480 extern "C" API int aul_svc_get_all_defapps(aul_svc_info_iter_fn iter_fn,
481     void* data) {
482   return aul_svc_get_all_defapps_for_uid(iter_fn, data, getuid());
483 }
484
485 extern "C" API int aul_svc_get_all_defapps_for_uid(aul_svc_info_iter_fn iter_fn,
486     void* data, uid_t uid) {
487   if (iter_fn == nullptr) {
488     _E("Invalid parameter");
489     return AUL_SVC_RET_EINVAL;
490   }
491
492   bundle* response;
493   tizen_base::Bundle request;
494   int ret = ::SendAndReceive(APP_GET_APP_CONTROL_DEFAULT_APPS, uid,
495       request.GetHandle(), &response);
496   if (ret != AUL_SVC_RET_OK)
497     return ret;
498
499   tizen_base::Bundle res(response, false, true);
500   auto appid_array = res.GetStringArray(AUL_K_APPID_LIST);
501   for (auto& appid : appid_array) {
502     if (iter_fn(appid.c_str(), data) != 0)
503       break;
504   }
505
506   return AUL_SVC_RET_OK;
507 }
508
509 extern "C" API const char* aul_svc_get_operation(bundle* b) {
510   return bundle_get_val(b, AUL_SVC_K_OPERATION);
511 }
512
513 extern "C" API const char* aul_svc_get_uri(bundle* b) {
514   return bundle_get_val(b, AUL_SVC_K_URI);
515 }
516
517 extern "C" API const char* aul_svc_get_mime(bundle* b) {
518   return bundle_get_val(b, AUL_SVC_K_MIME);
519 }
520
521 extern "C" API const char* aul_svc_get_data(bundle* b, const char* key) {
522   return bundle_get_val(b, key);
523 }
524
525 extern "C" API const char** aul_svc_get_data_array(bundle* b, const char* key,
526     int* len) {
527   return bundle_get_str_array(b, key, len);
528 }
529
530 extern "C" API const char* aul_svc_get_pkgname(bundle* b) {
531   return bundle_get_val(b, AUL_SVC_K_PKG_NAME);
532 }
533
534 extern "C" API const char* aul_svc_get_appid(bundle* b) {
535   return bundle_get_val(b, AUL_SVC_K_PKG_NAME);
536 }
537
538 extern "C" API const char* aul_svc_get_category(bundle* b) {
539   return bundle_get_val(b, AUL_SVC_K_CATEGORY);
540 }
541
542 extern "C" API const char* aul_svc_get_launch_mode(bundle* b) {
543   return bundle_get_val(b, AUL_SVC_K_LAUNCH_MODE);
544 }
545
546 extern "C" API int aul_svc_create_result_bundle(bundle* inb, bundle** outb) {
547   if (inb == nullptr || outb == nullptr) {
548     _E("Invalid parameter");
549     return AUL_SVC_RET_EINVAL;
550   }
551
552   int ret = aul_create_result_bundle(inb, outb);
553   if (ret != AUL_R_OK)
554     return AulErrorConvert(ret);
555
556   return AUL_SVC_RET_OK;
557 }
558
559 extern "C" API int aul_svc_send_result(bundle* b, aul_svc_result_val result) {
560   if (b == nullptr) {
561     _E("Invalid parameter");
562     return AUL_SVC_RET_EINVAL;
563   }
564
565   int ret = ::SetBundle(b, AUL_SVC_K_RES_VAL, std::to_string(result).c_str());
566   if (ret < 0)
567     return AUL_SVC_RET_ERROR;
568
569   if (result == AUL_SVC_RES_CANCEL)
570     ret = aul_send_result(b, 1);
571   else
572     ret = aul_send_result(b, 0);
573
574   bundle_del(b, AUL_SVC_K_RES_VAL);
575   return ret;
576 }
577
578 extern "C" API int aul_svc_data_is_array(bundle* b, const char* key) {
579   int type = bundle_get_type(b, key);
580   if (type <= 0)
581     return 0;
582
583   if (type & BUNDLE_TYPE_ARRAY)
584     return 1;
585
586   return 0;
587 }
588
589 extern "C" API int aul_svc_allow_transient_app(bundle* b, int wid) {
590   if (b == nullptr) {
591     _E("Invalid parameter");
592     return AUL_SVC_RET_EINVAL;
593   }
594
595   return ::SetBundle(b, AUL_SVC_K_WIN_ID, std::to_string(wid).c_str());
596 }
597
598 extern "C" API int aul_svc_request_transient_app(bundle* b, int callee_wid,
599     aul_svc_host_res_fn cbfunc, void* data) {
600   return 0;
601 }
602
603 extern "C" API int aul_svc_subapp_terminate_request_pid(int pid) {
604   int cpid = getpid();
605   int lcnt;
606   int* lpids = nullptr;
607   aul_app_group_get_leader_pids(&lcnt, &lpids);
608   for (int i = 0; i < lcnt; ++i) {
609     if (lpids[i] == cpid) {
610       int cnt;
611       int* pids = nullptr;
612       aul_app_group_get_group_pids(cpid, &cnt, &pids);
613       if (cnt == 0) {
614         free(lpids);
615         if (pids)
616           free(pids);
617
618         return aul_subapp_terminate_request_pid(pid);
619       }
620
621       if (pids != nullptr)
622         free(pids);
623       break;
624     }
625   }
626
627   if (lpids != nullptr)
628     free(lpids);
629
630   return aul_app_group_clear_top();
631 }
632
633 extern "C" API int aul_send_service_result(bundle* b) {
634   return aul_send_result(b, 0);
635 }
636
637 extern "C" API int aul_svc_subscribe_launch_result(bundle* b,
638     const char* result) {
639   if (b == nullptr) {
640     _E("Invalid parameter");
641     return AUL_SVC_RET_EINVAL;
642   }
643
644   return ::SetBundle(b, result, "1");
645 }
646
647 extern "C" API int aul_svc_set_loader_id(bundle* b, int loader_id) {
648   if (b == nullptr || loader_id <= 0) {
649     _E("Invalid parameter");
650     return AUL_SVC_RET_EINVAL;
651   }
652
653   return ::SetBundle(b, AUL_K_LOADER_ID, std::to_string(loader_id).c_str());
654 }
655
656 extern "C" API int aul_svc_set_loader_name(bundle* b, const char* loader_name) {
657   if (b == nullptr || loader_name == nullptr) {
658     _E("Invalid parameter");
659     return AUL_SVC_RET_EINVAL;
660   }
661
662   return ::SetBundle(b, AUL_K_LOADER_NAME, loader_name);
663 }
664
665 extern "C" API int aul_svc_set_background_launch(bundle* b, int enabled) {
666   if (b == nullptr) {
667     _E("Invalid parameter");
668     return AUL_SVC_RET_EINVAL;
669   }
670
671   return ::SetBundle(b, AUL_SVC_K_BG_LAUNCH, enabled ? "enable" : nullptr);
672 }
673
674 extern "C" API int aul_svc_get_appid_by_alias_appid(const char* alias_appid,
675     char** appid) {
676   return aul_svc_get_appid_by_alias_appid_for_uid(alias_appid, appid, getuid());
677 }
678
679 extern "C" API int aul_svc_get_appid_by_alias_appid_for_uid(
680     const char* alias_appid, char** appid, uid_t uid) {
681   if (alias_appid == nullptr || appid == nullptr) {
682     _E("Invalid parameter");
683     return AUL_SVC_RET_EINVAL;
684   }
685
686   bundle* response;
687   tizen_base::Bundle request;
688   request.Add(AUL_K_ALIAS_APPID, alias_appid);
689   int ret = ::SendAndReceive(APP_GET_APPID_BY_ALIAS_APPID, uid,
690       request.GetHandle(), &response);
691   if (ret != AUL_SVC_RET_OK)
692     return ret;
693
694   tizen_base::Bundle res(response, false, true);
695   auto val = res.GetString(AUL_K_APPID);
696   if (val.empty())
697     return AUL_SVC_RET_ERROR;
698
699   *appid = strdup(val.c_str());
700   if (*appid == nullptr) {
701     _E("Out of memory");
702     return AUL_SVC_RET_ENOMEM;
703   }
704
705   return AUL_SVC_RET_OK;
706 }
707
708 extern "C" API const char *aul_svc_get_instance_id(bundle* b) {
709   return bundle_get_val(b, AUL_K_INSTANCE_ID);
710 }
711
712 extern "C" API int aul_svc_set_instance_id(bundle* b, const char* instance_id) {
713   if (b == nullptr) {
714     _E("Invalid parameter");
715     return AUL_SVC_RET_EINVAL;
716   }
717
718   return ::SetBundle(b, AUL_K_INSTANCE_ID, instance_id);
719 }
720
721 extern "C" API int aul_svc_run_service_async(bundle* b, int request_code,
722     aul_svc_res_fn cbfunc, void* data) {
723   return aul_svc_run_service_async_for_uid(b, request_code, cbfunc, data,
724       getuid());
725 }
726
727 extern "C" API int aul_svc_run_service_async_for_uid(bundle* b,
728     int request_code, aul_svc_res_fn cbfunc, void* data, uid_t uid) {
729   CbInfo* cb_info = nullptr;
730   if (cbfunc)
731     cb_info = new (std::nothrow) CbInfo(request_code, cbfunc, nullptr, data);
732
733   int ret = ::SendLaunchRequest<::SendLaunchRequestCb, ::CbInfo*>(
734       [](const std::string& appid, bundle* request, uid_t uid,
735           CbInfo* info) -> int {
736         if (info) {
737           return aul_launch_app_with_result_async_for_uid(appid.c_str(),
738               request, LaunchWithResultCb, info, uid);
739         }
740
741         return aul_launch_app_async_for_uid(appid.c_str(), request, uid);
742       }, b, uid, cb_info);
743   if (ret < 0) {
744     if (cb_info)
745       delete cb_info;
746
747     ret = AulErrorConvert(ret);
748   }
749
750   return ret;
751 }
752
753 extern "C" API int aul_svc_send_launch_request(bundle* b, int request_code,
754     aul_svc_res_fn cbfunc, aul_svc_err_cb err_cb, void* user_data) {
755   return aul_svc_send_launch_request_for_uid(b, request_code,
756       cbfunc, err_cb, user_data, getuid());
757 }
758
759 extern "C" API int aul_svc_send_launch_request_for_uid(bundle* b,
760     int request_code, aul_svc_res_fn cbfunc, aul_svc_err_cb err_cb,
761     void* user_data, uid_t uid) {
762   if (b == nullptr || err_cb == nullptr) {
763     _E("Invalid parameter");
764     return AUL_SVC_RET_EINVAL;
765   }
766
767   CbInfo* cb_info = new (std::nothrow) CbInfo(request_code, cbfunc, err_cb,
768       user_data);
769   if (cb_info == nullptr)
770     _E("Out of memory");
771
772   int ret = ::SendLaunchRequest<::SendLaunchRequestCb, ::CbInfo*>(
773       [](const std::string& appid, bundle* request, uid_t uid,
774         CbInfo* info) -> int {
775         return aul_send_launch_request_for_uid(appid.c_str(), request, uid,
776             info->res_fn_ ? ::LaunchWithResultCb : nullptr, ::ErrorCb, info);
777       }, b, uid, cb_info);
778   if (ret < 0) {
779     delete cb_info;
780     ret = ::AulErrorConvert(ret);
781   }
782
783   return ret;
784 }
785
786 extern "C" API int aul_svc_send_launch_request_sync_for_uid(bundle* b,
787     int request_code, bundle** res_b, aul_svc_result_val* res, uid_t uid) {
788   if (b == nullptr || res_b == nullptr || res == nullptr) {
789     _E("Invalid parameter");
790     return AUL_SVC_RET_EINVAL;
791   }
792
793   int ret = ::SendLaunchRequest<::SendLaunchRequestSyncCb, bundle**>(
794       [](const std::string& appid, bundle* request, uid_t uid,
795           bundle** response) -> int {
796         return aul_send_launch_request_sync_for_uid(appid.c_str(), request,
797             uid, response);
798       }, b, uid, res_b);
799   if (ret > 0) {
800     auto* val = bundle_get_val(*res_b, AUL_SVC_K_RES_VAL);
801     *res = static_cast<aul_svc_result_val>(
802         (val == nullptr) ? AUL_SVC_RES_NOT_OK : atoi(val));
803   } else {
804     ret = ::AulErrorConvert(ret);
805     *res = AUL_SVC_RES_CANCEL;
806   }
807
808   return ret;
809 }
810
811 extern "C" API int aul_svc_info_create(bundle* b, aul_svc_info_h* h) {
812   if (b == nullptr || h == nullptr) {
813     _E("Invalid parameter");
814     return AUL_SVC_RET_EINVAL;
815   }
816
817   aul::ResolveInfo* resolve_info = nullptr;
818   try {
819     tizen_base::Bundle kb(b, false, false);
820     resolve_info = aul::ResolveInfo::Manager::Create(kb);
821   } catch (aul::Exception& e) {
822     return AUL_SVC_RET_ERROR;
823   }
824
825   *h = static_cast<aul_svc_info_h>(resolve_info);
826   return AUL_SVC_RET_OK;
827 }
828
829 extern "C" API int aul_svc_info_get_operation(aul_svc_info_h h,
830     char** operation) {
831   if (h == nullptr || operation == nullptr) {
832     _E("Invalid parameter");
833     return AUL_SVC_RET_EINVAL;
834   }
835
836   auto* info = static_cast<aul::ResolveInfo*>(h);
837   auto& value = info->GetOperation();
838   *operation = strdup(value.empty() ? "NULL" : value.c_str());
839   if (*operation == nullptr) {
840     _E("Failed to duplicate operation");
841     return AUL_SVC_RET_ERROR;
842   }
843
844   return AUL_SVC_RET_OK;
845 }
846
847 extern "C" API int aul_svc_info_get_uri(aul_svc_info_h h, char** uri) {
848   if (h == nullptr || uri == nullptr) {
849     _E("Invalid parameter");
850     return AUL_SVC_RET_EINVAL;
851   }
852
853   auto* info = static_cast<aul::ResolveInfo*>(h);
854   auto& value = info->GetUri();
855   *uri = strdup(value.empty() ? "NULL" : value.c_str());
856   if (*uri == nullptr) {
857     _E("Failed to duplicate URI");
858     return AUL_SVC_RET_ERROR;
859   }
860
861   return AUL_SVC_RET_OK;
862 }
863
864 extern "C" API int aul_svc_info_get_uri_scheme(aul_svc_info_h h,
865     char** uri_scheme) {
866   if (h == nullptr || uri_scheme == nullptr) {
867     _E("Invalid parameter");
868     return AUL_SVC_RET_EINVAL;
869   }
870
871   auto* info = static_cast<aul::ResolveInfo*>(h);
872   auto& value = info->GetScheme();
873   *uri_scheme = strdup(value.empty() ? "NULL" : value.c_str());
874   if (*uri_scheme == nullptr) {
875     _E("Failed to duplicate URI scheme");
876     return AUL_SVC_RET_ERROR;
877   }
878
879   return AUL_SVC_RET_OK;
880 }
881
882 extern "C" API int aul_svc_info_get_uri_host(aul_svc_info_h h,
883     char** uri_host) {
884   if (h == nullptr || uri_host == nullptr) {
885     _E("Invalid parameter");
886     return AUL_SVC_RET_EINVAL;
887   }
888
889   auto* info = static_cast<aul::ResolveInfo*>(h);
890   auto& value = info->GetHost();
891   *uri_host = strdup(value.empty() ? "NULL" : value.c_str());
892   if (*uri_host == nullptr) {
893     _E("Failed to duplicate URI host");
894     return AUL_SVC_RET_ERROR;
895   }
896
897   return AUL_SVC_RET_OK;
898 }
899
900 extern "C" API int aul_svc_info_get_mime(aul_svc_info_h h, char** mime) {
901   if (h == nullptr || mime == nullptr) {
902     _E("Invalid parameter");
903     return AUL_SVC_RET_EINVAL;
904   }
905
906   auto* info = static_cast<aul::ResolveInfo*>(h);
907   auto& value = info->GetMime();
908   *mime = strdup(value.empty() ? "NULL" : value.c_str());
909   if (*mime == nullptr) {
910     _E("Failed to duplicate MIME-Type");
911     return AUL_SVC_RET_ERROR;
912   }
913
914   return AUL_SVC_RET_OK;
915 }
916
917 extern "C" API int aul_svc_info_get_mime_type(aul_svc_info_h h,
918     char** mime_type) {
919   if (h == nullptr || mime_type == nullptr) {
920     _E("Invalid parameter");
921     return AUL_SVC_RET_EINVAL;
922   }
923
924   auto* info = static_cast<aul::ResolveInfo*>(h);
925   auto& value = info->GetMType();
926   *mime_type = strdup(value.empty() ? "NULL" : value.c_str());
927   if (*mime_type == nullptr) {
928     _E("Failed to duplicate the type of MIME-Type");
929     return AUL_SVC_RET_ERROR;
930   }
931
932   return AUL_SVC_RET_OK;
933 }
934
935 extern "C" API int aul_svc_info_get_mime_subtype(aul_svc_info_h h,
936     char** mime_subtype) {
937   if (h == nullptr || mime_subtype == nullptr) {
938     _E("Invalid parameter");
939     return AUL_SVC_RET_EINVAL;
940   }
941
942   auto* info = static_cast<aul::ResolveInfo*>(h);
943   auto& value = info->GetSType();
944   *mime_subtype = strdup(value.empty() ? "NULL" : value.c_str());
945   if (*mime_subtype == nullptr) {
946     _E("Failed to duplicate the subtype of MIME-Type");
947     return AUL_SVC_RET_ERROR;
948   }
949
950   return AUL_SVC_RET_OK;
951 }
952
953 extern "C" API int aul_svc_info_destroy(aul_svc_info_h h) {
954   if (h == nullptr) {
955     _E("Invalid parameter");
956     return AUL_SVC_RET_EINVAL;
957   }
958
959   auto* info = static_cast<aul::ResolveInfo*>(h);
960   delete info;
961   return AUL_SVC_RET_OK;
962 }
963
964 extern "C" API int aul_svc_set_caller_instance_id(bundle* b,
965     const char* instance_id) {
966   if (b == nullptr) {
967     _E("Invalid parameter");
968     return AUL_SVC_RET_EINVAL;
969   }
970
971   return ::SetBundle(b, AUL_K_CALLER_INSTANCE_ID, instance_id);
972 }
973
974 extern "C" API int aul_svc_set_comp_id(bundle* b, const char* comp_id) {
975   if (b == nullptr) {
976     _E("Invalid parameter");
977     return AUL_SVC_RET_EINVAL;
978   }
979
980   return ::SetBundle(b, AUL_K_COMPONENT_ID, comp_id);
981 }
982
983 extern "C" API const char *aul_svc_get_comp_id(bundle* b) {
984   return bundle_get_val(b, AUL_K_COMPONENT_ID);
985 }
986
987 extern "C" API int aul_svc_subapp_terminate_request(bundle* b, int pid) {
988   if (b == nullptr || pid < 0) {
989     _E("Invalid parameter");
990     return AUL_SVC_RET_EINVAL;
991   }
992
993   const char* inst_id = bundle_get_val(b, AUL_K_INSTANCE_ID);
994   if (inst_id == nullptr) {
995     _E("Invalid parameter");
996     return AUL_SVC_RET_EINVAL;
997   }
998
999   char buf[512] = { 0, };
1000   const char* caller_inst_id = bundle_get_val(b, AUL_K_CALLER_INSTANCE_ID);
1001   if (caller_inst_id == nullptr) {
1002     int ret = aul_app_get_instance_id_bypid(getpid(), buf, sizeof(buf));
1003     if (ret != AUL_R_OK) {
1004       _E("aul_app_get_instance_id_bypid() is failed. error(%d)", ret);
1005       return AUL_SVC_RET_ERROR;
1006     }
1007
1008     caller_inst_id = buf;
1009   }
1010
1011   int cnt = 0;
1012   aul_app_group_foreach_group_info(caller_inst_id,
1013       [](aul_app_group_info_h info, void* data) {
1014       int* count = static_cast<int*>(data);
1015       (*count)++;
1016       }, static_cast<void*>(&cnt));
1017   if (cnt == 0)
1018     return aul_subapp_terminate_request(inst_id, pid);
1019
1020   return aul_app_group_clear_top();
1021 }
1022
1023 extern "C" API int aul_svc_send_resume_request(bundle* b, int request_code,
1024     aul_svc_err_cb err_cb, void *user_data) {
1025   return aul_svc_send_resume_request_for_uid(b, request_code, err_cb, user_data,
1026       getuid());
1027 }
1028
1029 extern "C" API int aul_svc_send_resume_request_for_uid(bundle* b,
1030     int request_code, aul_svc_err_cb err_cb, void* user_data, uid_t uid) {
1031   if (b == nullptr || err_cb == nullptr) {
1032     _E("Invalid parameter");
1033     return AUL_SVC_RET_EINVAL;
1034   }
1035
1036   auto* cb_info = new (std::nothrow) CbInfo(request_code, nullptr, err_cb,
1037       user_data);
1038   if (cb_info == nullptr)
1039     _E("Out of memory");
1040
1041   int ret = ::SendLaunchRequest<::SendLaunchRequestCb, ::CbInfo*>(
1042       [](const std::string& appid, bundle* request, uid_t uid,
1043           CbInfo* info) -> int {
1044         return aul_send_resume_request_for_uid(appid.c_str(), request, uid,
1045             ErrorCb, info);
1046       }, b, uid, cb_info);
1047   if (ret < 0) {
1048     delete cb_info;
1049     ret = ::AulErrorConvert(ret);
1050   }
1051
1052   return ret;
1053 }
1054
1055 extern "C" API int aul_svc_get_appid_array(bundle* b, uid_t uid,
1056     char*** appid_array, unsigned int* len) {
1057   if (b == nullptr || appid_array == nullptr || len == nullptr) {
1058     _E("Invalid parameter");
1059     return AUL_SVC_RET_EINVAL;
1060   }
1061
1062   bundle_del(b, AUL_K_APPID);
1063   bundle_add(b, AUL_K_APPID, "@UNKNOWN");
1064
1065   bundle* response;
1066   int ret = ::SendAndReceive(APP_GET_APPID_LIST, uid, b, &response);
1067   if (ret != AUL_SVC_RET_OK)
1068     return ret;
1069
1070   tizen_base::Bundle res(response, false, true);
1071   auto str = res.GetString(AUL_SVC_K_URI_R_INFO);
1072   if (!str.empty())
1073     ::SetBundle(b, AUL_SVC_K_URI_R_INFO, str.c_str());
1074
1075   auto str_arr = res.GetStringArray(AUL_K_APPID_LIST);
1076   if (str_arr.empty()) {
1077     _E("Failed to get appid list");
1078     return AUL_SVC_RET_ERROR;
1079   }
1080
1081   *appid_array = reinterpret_cast<char**>(
1082       calloc(str_arr.size(), sizeof(char*)));
1083   if (*appid_array == nullptr) {
1084     _E("Out of memory");
1085     return AUL_SVC_RET_ENOMEM;
1086   }
1087
1088   *len = str_arr.size();
1089
1090   int i = 0;
1091   for (auto& appid : str_arr)
1092     (*appid_array)[i++] = strdup(appid.c_str());
1093
1094   return AUL_SVC_RET_OK;
1095 }
1096
1097 extern "C" API void aul_svc_free_appid_array(char** appid_array,
1098     unsigned int len) {
1099   if (appid_array == nullptr)
1100     return;
1101
1102   for (unsigned int i = 0; i < len; ++i)
1103     free(appid_array[i]);
1104
1105   free(appid_array);
1106 }
1107
1108 extern "C" API int aul_svc_set_defapp(const char* op, const char* mime_type,
1109     const char* uri, const char* defapp) {
1110   DEPRECATION_WARNING();
1111   return AUL_SVC_RET_OK;
1112 }
1113
1114 extern "C" API int aul_svc_set_defapp_for_uid(const char* op,
1115     const char* mime_type, const char* uri, const char* defapp, uid_t uid) {
1116   DEPRECATION_WARNING();
1117   return AUL_SVC_RET_OK;
1118 }
1119
1120 extern "C" API int aul_svc_unset_defapp(const char* defapp) {
1121   DEPRECATION_WARNING();
1122   return AUL_SVC_RET_OK;
1123 }
1124
1125 extern "C" API int aul_svc_unset_defapp_for_uid(const char* defapp, uid_t uid) {
1126   DEPRECATION_WARNING();
1127   return AUL_SVC_RET_OK;
1128 }
1129
1130 extern "C" API int aul_svc_unset_all_defapps(void) {
1131   DEPRECATION_WARNING();
1132   return AUL_SVC_RET_OK;
1133 }
1134
1135 extern "C" API int aul_svc_unset_all_defapps_for_uid(uid_t uid) {
1136   DEPRECATION_WARNING();
1137   return AUL_SVC_RET_OK;
1138 }
1139
1140 extern "C" API int aul_svc_is_defapp(const char* pkg_name) {
1141   DEPRECATION_WARNING();
1142   return AUL_SVC_RET_OK;
1143 }
1144
1145 extern "C" API int aul_svc_is_defapp_for_uid(const char* pkg_name, uid_t uid) {
1146   DEPRECATION_WARNING();
1147   return AUL_SVC_RET_OK;
1148 }
1149
1150 extern "C" API int aul_svc_set_alias_appid(const char* alias_appid,
1151     const char* appid) {
1152   DEPRECATION_WARNING();
1153   return AUL_SVC_RET_OK;
1154 }
1155
1156 extern "C" API int aul_svc_set_alias_appid_for_uid(const char* alias_appid,
1157     const char* appid, uid_t uid) {
1158   DEPRECATION_WARNING();
1159   return AUL_SVC_RET_OK;
1160 }
1161
1162 extern "C" API int aul_svc_unset_alias_appid(const char* alias_appid) {
1163   DEPRECATION_WARNING();
1164   return AUL_SVC_RET_OK;
1165 }
1166
1167 extern "C" API int aul_svc_unset_alias_appid_for_uid(const char* alias_appid,
1168     uid_t uid) {
1169   DEPRECATION_WARNING();
1170   return AUL_SVC_RET_OK;
1171 }
1172
1173 extern "C" API int aul_svc_foreach_alias_info(
1174     void (*callback)(const char *, const char *, void *),
1175     void* user_data) {
1176   DEPRECATION_WARNING();
1177   return AUL_SVC_RET_OK;
1178 }
1179
1180 extern "C" API int aul_svc_foreach_alias_info_for_uid(
1181     void (*callback)(const char *, const char *, void *),
1182     uid_t uid, void* user_data) {
1183   DEPRECATION_WARNING();
1184   return AUL_SVC_RET_OK;
1185 }
1186
1187 extern "C" API int aul_svc_enable_alias_info(const char* appid) {
1188   DEPRECATION_WARNING();
1189   return AUL_SVC_RET_OK;
1190 }
1191
1192 extern "C" API int aul_svc_enable_alias_info_for_uid(const char* appid,
1193     uid_t uid) {
1194   DEPRECATION_WARNING();
1195   return AUL_SVC_RET_OK;
1196 }
1197
1198 extern "C" API int aul_svc_disable_alias_info(const char* appid) {
1199   DEPRECATION_WARNING();
1200   return AUL_SVC_RET_OK;
1201 }
1202
1203 extern "C" API int aul_svc_disable_alias_info_for_uid(const char* appid,
1204     uid_t uid) {
1205   DEPRECATION_WARNING();
1206   return AUL_SVC_RET_OK;
1207 }
1208
1209 extern "C" API int aul_svc_foreach_alias_info_by_appid(
1210     int (*callback)(const char *, const char *, void *),
1211     const char* appid, void* user_data) {
1212   DEPRECATION_WARNING();
1213   return AUL_SVC_RET_OK;
1214 }
1215
1216 extern "C" API int aul_svc_foreach_alias_info_by_appid_for_uid(
1217     int (*callback)(const char *, const char *, void *),
1218     const char* appid, uid_t uid, void* user_data) {
1219   DEPRECATION_WARNING();
1220   return AUL_SVC_RET_OK;
1221 }
1222
1223 extern "C" API int aul_svc_foreach_allowed_info(
1224     int (*callback)(const char *, const char *, void *),
1225     void* user_data) {
1226   DEPRECATION_WARNING();
1227   return AUL_SVC_RET_OK;
1228 }
1229
1230 extern "C" API int aul_svc_foreach_allowed_info_for_uid(
1231     int (*callback)(const char *, const char *, void *),
1232     uid_t uid, void* user_data) {
1233   DEPRECATION_WARNING();
1234   return AUL_SVC_RET_OK;
1235 }
1236
1237 extern "C" API int aul_svc_foreach_allowed_info_by_appid(
1238     int (*callback)(const char *, const char *, void *),
1239     const char* appid, void* user_data) {
1240   DEPRECATION_WARNING();
1241   return AUL_SVC_RET_OK;
1242 }
1243
1244 extern "C" API int aul_svc_foreach_allowed_info_by_appid_for_uid(
1245     int (*callback)(const char *, const char *, void *),
1246     const char* appid, uid_t uid, void* user_data) {
1247   DEPRECATION_WARNING();
1248   return AUL_SVC_RET_OK;
1249 }