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