Refactor slp-pkgmgr with tidl
[platform/core/appfw/slp-pkgmgr.git] / client / src / api_stub.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 <unistd.h>
18 #include <unzip.h>
19 #include <tzplatform_config.h>
20
21 #include "include/package-manager-types.h"
22 #include "include/package-manager-plugin.h"
23
24 #include "client/include/package-manager.h"
25 #include "client/src/connector.hh"
26 #include "client/src/log.hh"
27 #include "client/src/internal.hh"
28
29 #ifdef API
30 #undef API
31 #endif
32 #define API __attribute__ ((visibility("default")))
33
34 #define CONNECTION_RETRY_MAX 5
35 #define CONNECTION_WAIT_USEC (1000000 / 2) /* 0.5 sec */
36 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
37 #define REGULAR_USER 5000
38
39 using pkgmgr::client::Connector;
40
41 namespace {
42
43 static int GetDelayedResult(pkgmgr_client *pc, const std::string& req_key) {
44   if (pc == nullptr) {
45     _E("invalid parameter");
46     return PKGMGR_R_EINVAL;
47   }
48
49   auto* con = static_cast<Connector*>(pc);
50   if (con->GetPcType() != PC_REQUEST) {
51     _E("client type is not PC_REQUEST");
52     return PKGMGR_R_EINVAL;
53   }
54
55   auto* proxy = con->GetDelayedResultProxy();
56   if (proxy == nullptr)
57     return PKGMGR_R_EIO;
58
59   int retry_cnt = 0;
60   int ret = 0;
61   do {
62     if (proxy->GetResult(req_key, ret) == PKGMGR_R_OK)
63       return ret;
64
65     _E("Failed to get result sleep and retry[%d/%d]",
66         retry_cnt, CONNECTION_RETRY_MAX);
67     usleep(CONNECTION_WAIT_USEC);
68     retry_cnt++;
69   } while (retry_cnt <= CONNECTION_RETRY_MAX);
70
71   _E("Failed to get result");
72   return PKGMGR_R_ERROR;
73 }
74
75 static int ResultGetSizeSync(pkgmgr_client *pc, const std::string& req_key) {
76   if (pc == nullptr) {
77     _E("invalid parameter");
78     return PKGMGR_R_EINVAL;
79   }
80
81   auto* con = static_cast<Connector*>(pc);
82   if (con->GetPcType() != PC_REQUEST) {
83     _E("client type is not PC_REQUEST");
84     return PKGMGR_R_EINVAL;
85   }
86
87   auto* proxy = con->GetInfoProxy();
88   if (proxy == nullptr)
89     return PKGMGR_R_EIO;
90
91   int retry_cnt = 0;
92   int ret = PKGMGR_R_ECOMM;
93   long long size_info = 0;
94   do {
95     if (proxy->GetSizeSyncResult(req_key, ret, size_info) == PKGMGR_R_OK) {
96       if (ret != PKGMGR_R_OK) {
97         _E("request result failed: %d", ret);
98         return ret;
99       }
100
101       if (size_info < 0) {
102         _E("invalid size_info=(%lld)", size_info);
103         return -1;
104       }
105
106       _D("size_info(%lld), return size(%d)", size_info, ret);
107       return size_info;
108     }
109
110     _E("Failed to get result sleep and retry[%d/%d]",
111         retry_cnt, CONNECTION_RETRY_MAX);
112     usleep(CONNECTION_WAIT_USEC);
113     retry_cnt++;
114   } while (retry_cnt <= CONNECTION_RETRY_MAX);
115
116   _E("Failed to get result");
117   return ret;
118 }
119
120 static int GetGenerateLicenseResult(pkgmgr_client *pc,
121     const std::string& req_key, std::string &reqData, std::string &licenseUrl) {
122   if (pc == nullptr) {
123     _E("invalid parameter");
124     return PKGMGR_R_EINVAL;
125   }
126
127   auto* con = static_cast<Connector*>(pc);
128   if (con->GetPcType() != PC_REQUEST) {
129     _E("client type is not PC_REQUEST");
130     return PKGMGR_R_EINVAL;
131   }
132
133   auto* proxy = con->GetAdminProxy();
134   if (proxy == nullptr)
135     return PKGMGR_R_EIO;
136
137   int retry_cnt = 0;
138   int ret = PKGMGR_R_ECOMM;
139   do {
140     if (proxy->GetGenerateLicenseResult(req_key,
141         ret, reqData, licenseUrl) == PKGMGR_R_OK)
142       return ret;
143
144     _E("Failed to get result sleep and retry[%d/%d]",
145         retry_cnt, CONNECTION_RETRY_MAX);
146     usleep(CONNECTION_WAIT_USEC);
147     retry_cnt++;
148   } while (retry_cnt <= CONNECTION_RETRY_MAX);
149
150   _E("Failed to get result");
151   return ret;
152 }
153
154 static inline uid_t GetUid() {
155   uid_t uid = getuid();
156
157   if (uid < REGULAR_USER)
158     return GLOBAL_USER;
159   else
160     return uid;
161 }
162
163 }
164
165 API pkgmgr_client* pkgmgr_client_new(pkgmgr_client_type pc_type) {
166   if (pc_type != PC_REQUEST && pc_type != PC_LISTENING) {
167     _E("invalid parameter(%d)", pc_type);
168     return nullptr;
169   }
170
171   return new Connector(pc_type);
172 }
173
174 API int pkgmgr_client_free(pkgmgr_client* pc) {
175   if (pc == nullptr) {
176     _E("invalid argument");
177     return PKGMGR_R_EINVAL;
178   }
179
180   Connector* con = static_cast<Connector*>(pc);
181   delete con;
182
183   return PKGMGR_R_OK;
184 }
185
186 API int pkgmgr_client_set_tep_path(pkgmgr_client *pc, const char *tep_path,
187     bool tep_move) {
188   if (pc == nullptr || tep_path == nullptr) {
189     _E("invalied parameter");
190     return PKGMGR_R_EINVAL;
191   }
192
193   Connector* con = static_cast<Connector*>(pc);
194   con->SetTep(tep_path, tep_move);
195   return PKGMGR_R_OK;
196 }
197
198 API int pkgmgr_client_usr_install_packages(pkgmgr_client *pc,
199     const char **pkg_paths, int n_pkgs, pkgmgr_handler event_cb,
200     void *data, uid_t uid) {
201   if (pc == nullptr || pkg_paths == nullptr || n_pkgs < 1) {
202     _E("invalid parameter");
203     return PKGMGR_R_EINVAL;
204   }
205
206   auto* con = static_cast<Connector*>(pc);
207   if (con->GetPcType() != PC_REQUEST) {
208     _E("client type is not PC_REQUEST");
209     return PKGMGR_R_EINVAL;
210   }
211
212   auto* proxy = con->GetAdminProxy();
213   if (proxy == nullptr)
214     return PKGMGR_R_EIO;
215
216   std::string req_key;
217   std::vector<std::string> pkgs;
218   for (int i = 0; i < n_pkgs; i++)
219     pkgs.push_back(pkg_paths[i]);
220
221   const auto& receiver = con->GetSignalReceiver();
222   int ret = proxy->InstallPkgs(uid, pkgs, con->GetArgv(),
223       con->GenerateRequestId(), req_key);
224   if (ret != PKGMGR_R_OK)
225     return ret;
226
227   return receiver->AddEventHandler(req_key, event_cb, data);
228 }
229
230 API int pkgmgr_client_install_packages(pkgmgr_client *pc,
231     const char **pkg_paths, int n_pkgs, pkgmgr_handler event_cb,
232     void *data) {
233   return pkgmgr_client_usr_install_packages(pc, pkg_paths, n_pkgs,
234       event_cb, data, getuid());
235 }
236
237 API int pkgmgr_client_install(pkgmgr_client *pc, const char *pkg_type,
238     const char *descriptor_path, const char *pkg_path,
239     const char *optional_data, pkgmgr_mode mode,
240     pkgmgr_handler event_cb, void *data) {
241   return pkgmgr_client_usr_install(pc, pkg_type, descriptor_path, pkg_path,
242       optional_data, mode, event_cb, data, getuid());
243 }
244
245 API int pkgmgr_client_usr_install(pkgmgr_client *pc, const char *pkg_type,
246     const char *descriptor_path, const char *pkg_path,
247     const char *optional_data, pkgmgr_mode mode,
248     pkgmgr_handler event_cb, void *data, uid_t uid) {
249   if (pc == nullptr || pkg_path == nullptr) {
250     _E("invalid parameter");
251     return PKGMGR_R_EINVAL;
252   }
253
254   auto* con = static_cast<Connector*>(pc);
255   if (con->GetPcType() != PC_REQUEST) {
256     _E("client type is not PC_REQUEST");
257     return PKGMGR_R_EINVAL;
258   }
259
260   if (access(pkg_path, F_OK) != 0) {
261     _E("failed to access: %s", pkg_path);
262     return PKGMGR_R_EINVAL;
263   }
264
265   if (!con->GetTepPath().empty()) {
266     if (access(con->GetTepPath().c_str(), F_OK) != 0) {
267       _E("failed to access: %s", con->GetTepPath().c_str());
268       return PKGMGR_R_EINVAL;
269     }
270
271     con->SetTepArgs();
272   }
273
274   auto* proxy = con->GetAdminProxy();
275   if (proxy == nullptr)
276     return PKGMGR_R_EIO;
277
278   const auto& receiver = con->GetSignalReceiver();
279   std::string req_key;
280   int ret = proxy->Install(uid, pkg_type ? pkg_type : "", pkg_path, con->GetArgv(),
281       con->GenerateRequestId(), req_key);
282   if (ret != PKGMGR_R_OK)
283     return ret;
284
285   return receiver->AddEventHandler(req_key, event_cb, data);
286 }
287
288 API int pkgmgr_client_reinstall(pkgmgr_client *pc, const char *pkg_type,
289     const char *pkgid, const char *optional_data, pkgmgr_mode mode,
290     pkgmgr_handler event_cb, void *data) {
291   return pkgmgr_client_usr_reinstall(pc, pkg_type, pkgid, optional_data,
292       mode, event_cb, data, getuid());
293 }
294
295 API int pkgmgr_client_usr_reinstall(pkgmgr_client *pc, const char *pkg_type,
296     const char *pkgid, const char *optional_data, pkgmgr_mode mode,
297     pkgmgr_handler event_cb, void *data, uid_t uid) {
298   if (pc == nullptr || pkgid == nullptr) {
299     _E("invalid parameter");
300     return PKGMGR_R_EINVAL;
301   }
302
303   auto* con = static_cast<Connector*>(pc);
304   if (con->GetPcType() != PC_REQUEST) {
305     _E("client type is not PC_REQUEST");
306     return PKGMGR_R_EINVAL;
307   }
308
309   auto* proxy = con->GetAdminProxy();
310   if (proxy == nullptr)
311     return PKGMGR_R_EIO;
312
313   const auto& receiver = con->GetSignalReceiver();
314   std::string req_key;
315   int ret = proxy->Reinstall(uid, pkgid, req_key);
316   if (ret != PKGMGR_R_OK)
317     return ret;
318
319   return receiver->AddEventHandler(req_key, event_cb, data);
320 }
321
322 API int pkgmgr_client_uninstall_packages(pkgmgr_client *pc,
323     const char **pkgids, int n_pkgs, pkgmgr_handler event_cb,
324     void *data) {
325   return pkgmgr_client_usr_uninstall_packages(pc, pkgids, n_pkgs,
326       event_cb, data, getuid());
327 }
328
329 API int pkgmgr_client_usr_uninstall_packages(pkgmgr_client *pc,
330     const char **pkgids, int n_pkgs, pkgmgr_handler event_cb,
331     void *data, uid_t uid) {
332   if (pc == nullptr || pkgids == nullptr || n_pkgs < 1) {
333     _E("invalid parameter");
334     return PKGMGR_R_EINVAL;
335   }
336
337   auto* con = static_cast<Connector*>(pc);
338   if (con->GetPcType() != PC_REQUEST) {
339     _E("client type is not PC_REQUEST");
340     return PKGMGR_R_EINVAL;
341   }
342
343   auto* proxy = con->GetAdminProxy();
344   if (proxy == nullptr)
345     return PKGMGR_R_EIO;
346
347   std::string req_key;
348   std::vector<std::string> pkgs;
349   for (int i = 0; i < n_pkgs; i++)
350     pkgs.push_back(pkgids[i]);
351
352   const auto& receiver = con->GetSignalReceiver();
353   int ret = proxy->UninstallPkgs(uid, pkgs, req_key);
354   if (ret != PKGMGR_R_OK)
355     return ret;
356
357   return receiver->AddEventHandler(req_key, event_cb, data);
358 }
359
360 API int pkgmgr_client_uninstall(pkgmgr_client *pc, const char *pkg_type,
361     const char *pkgid, pkgmgr_mode mode, pkgmgr_handler event_cb,
362     void *data) {
363   return pkgmgr_client_usr_uninstall(pc, pkg_type, pkgid, mode, event_cb,
364       data, getuid());
365 }
366
367 API int pkgmgr_client_usr_uninstall(pkgmgr_client *pc, const char *pkg_type,
368     const char *pkgid, pkgmgr_mode mode, pkgmgr_handler event_cb,
369     void *data, uid_t uid) {
370   if (pc == nullptr || pkgid == nullptr) {
371     _E("invalid parameter");
372     return PKGMGR_R_EINVAL;
373   }
374
375   auto* con = static_cast<Connector*>(pc);
376   if (con->GetPcType() != PC_REQUEST) {
377     _E("client type is not PC_REQUEST");
378     return PKGMGR_R_EINVAL;
379   }
380
381   auto* proxy = con->GetAdminProxy();
382   if (proxy == nullptr)
383     return PKGMGR_R_EIO;
384
385   const auto& receiver = con->GetSignalReceiver();
386   std::string req_key;
387   int ret = proxy->Uninstall(uid, pkgid, req_key);
388   if (ret != PKGMGR_R_OK)
389     return ret;
390
391   return receiver->AddEventHandler(req_key, event_cb, data);
392 }
393
394 API int pkgmgr_client_listen_res_status(pkgmgr_client *pc,
395     pkgmgr_res_handler event_cb, void *data) {
396   Connector* con = static_cast<Connector*>(pc);
397   if (con->GetPcType() != PC_LISTENING) {
398     _E("client->pc_type is not PC_LISTENING");
399     return PKGMGR_R_EINVAL;
400   }
401
402   try {
403     const auto& receiver = con->GetSignalReceiver();
404     return receiver->AddEventHandler("", event_cb, data);
405   } catch (...) {
406     _E("exception occured");
407     return PKGMGR_R_ERROR;
408   }
409 }
410
411 API int pkgmgr_client_listen_pkg_upgrade_status(pkgmgr_client *pc,
412     pkgmgr_pkg_upgrade_handler event_cb, void *data) {
413   //TODO
414   return PKGMGR_R_OK;
415 }
416
417 API int pkgmgr_client_usr_mount_install_packages(pkgmgr_client *pc,
418     const char **pkg_paths, int n_pkgs, pkgmgr_handler event_cb,
419     void *data, uid_t uid) {
420   if (pc == nullptr || pkg_paths == nullptr || n_pkgs < 1) {
421     _E("invalid parameter");
422     return PKGMGR_R_EINVAL;
423   }
424
425   auto* con = static_cast<Connector*>(pc);
426   if (con->GetPcType() != PC_REQUEST) {
427     _E("client type is not PC_REQUEST");
428     return PKGMGR_R_EINVAL;
429   }
430
431   auto* proxy = con->GetAdminProxy();
432   if (proxy == nullptr)
433     return PKGMGR_R_EIO;
434
435   std::string req_key;
436   std::vector<std::string> pkgs;
437   for (int i = 0; i < n_pkgs; i++) {
438     if (access(pkg_paths[i], F_OK) != 0) {
439       _E("failed to access: %s", pkg_paths[i]);
440       return PKGMGR_R_EINVAL;
441     }
442
443     pkgs.push_back(pkg_paths[i]);
444   }
445
446   const auto& receiver = con->GetSignalReceiver();
447   int ret = proxy->MountInstallPkgs(uid, pkgs, con->GenerateRequestId(),
448       req_key);
449   if (ret != PKGMGR_R_OK)
450     return ret;
451
452   return receiver->AddEventHandler(req_key, event_cb, data);
453 }
454
455 API int pkgmgr_client_mount_install_packages(pkgmgr_client *pc,
456     const char **pkg_paths, int n_pkgs, pkgmgr_handler event_cb,
457     void *data) {
458   return pkgmgr_client_usr_mount_install_packages(pc, pkg_paths, n_pkgs,
459       event_cb, data, getuid());
460 }
461
462 API int pkgmgr_client_mount_install(pkgmgr_client *pc, const char *pkg_type,
463     const char *descriptor_path, const char *pkg_path,
464     const char *optional_data, pkgmgr_mode mode,
465     pkgmgr_handler event_cb, void *data) {
466   return pkgmgr_client_usr_mount_install(pc, pkg_type, descriptor_path,
467       pkg_path, optional_data, mode, event_cb, data,
468       getuid());
469 }
470
471 API int pkgmgr_client_usr_mount_install(pkgmgr_client *pc, const char *pkg_type,
472     const char *descriptor_path, const char *pkg_path,
473     const char *optional_data, pkgmgr_mode mode,
474     pkgmgr_handler event_cb, void *data, uid_t uid) {
475   if (pc == nullptr || pkg_path == nullptr) {
476     _E("invalid parameter");
477     return PKGMGR_R_EINVAL;
478   }
479
480   auto* con = static_cast<Connector*>(pc);
481   if (con->GetPcType() != PC_REQUEST) {
482     _E("client type is not PC_REQUEST");
483     return PKGMGR_R_EINVAL;
484   }
485
486   if (access(pkg_path, F_OK) != 0) {
487     _E("failed to access: %s", pkg_path);
488     return PKGMGR_R_EINVAL;
489   }
490
491   if (!con->GetTepPath().empty()) {
492     if (access(con->GetTepPath().c_str(), F_OK) != 0) {
493       _E("failed to access: %s", con->GetTepPath().c_str());
494       return PKGMGR_R_EINVAL;
495     }
496
497     con->SetTepArgs();
498   }
499
500   auto* proxy = con->GetAdminProxy();
501   if (proxy == nullptr)
502     return PKGMGR_R_EIO;
503
504   const auto& receiver = con->GetSignalReceiver();
505   std::string req_key;
506   int ret = proxy->MountInstall(uid, pkg_type ? pkg_type : "", pkg_path,
507       con->GetArgv(), con->GenerateRequestId(), req_key);
508   if (ret != PKGMGR_R_OK)
509     return ret;
510
511   return receiver->AddEventHandler(req_key, event_cb, data);
512 }
513
514 API int pkgmgr_client_move(pkgmgr_client *pc, const char *pkg_type,
515     const char *pkgid, pkgmgr_move_type move_type,
516     pkgmgr_handler event_cb, void *data) {
517   return pkgmgr_client_usr_move(pc, pkg_type, pkgid, move_type,
518       event_cb, data, getuid());
519 }
520
521 API int pkgmgr_client_usr_move(pkgmgr_client *pc, const char *pkg_type,
522     const char *pkgid, pkgmgr_move_type move_type,
523     pkgmgr_handler event_cb, void *data, uid_t uid) {
524   if (pc == nullptr || pkgid == nullptr) {
525     _E("invalid parameter");
526     return PKGMGR_R_EINVAL;
527   }
528
529   if ((move_type < PM_MOVE_TO_INTERNAL) ||
530       (move_type > PM_MOVE_TO_EXTENDED))
531     return PKGMGR_R_EINVAL;
532
533   auto* con = static_cast<Connector*>(pc);
534   if (con->GetPcType() != PC_REQUEST) {
535     _E("client type is not PC_REQUEST");
536     return PKGMGR_R_EINVAL;
537   }
538
539   auto* proxy = con->GetAdminProxy();
540   if (proxy == nullptr)
541     return PKGMGR_R_EIO;
542
543   const auto& receiver = con->GetSignalReceiver();
544   std::string req_key;
545   int ret = proxy->Move(uid, pkgid, move_type, req_key);
546   if (ret != PKGMGR_R_OK)
547     return ret;
548
549   return receiver->AddEventHandler(req_key, event_cb, data);
550 }
551
552 API int pkgmgr_client_register_pkg_update_info(pkgmgr_client *pc,
553     pkg_update_info_t *update_info) {
554   return pkgmgr_client_usr_register_pkg_update_info(pc, update_info,
555       getuid());
556 }
557
558 API int pkgmgr_client_usr_register_pkg_update_info(pkgmgr_client *pc,
559     pkg_update_info_t *update_info, uid_t uid) {
560   if (pc == nullptr || update_info == nullptr || update_info->pkgid == nullptr) {
561     _E("invalid parameter");
562     return PKGMGR_R_EINVAL;
563   }
564
565   auto* con = static_cast<Connector*>(pc);
566   if (con->GetPcType() != PC_REQUEST) {
567     _E("client type is not PC_REQUEST");
568     return PKGMGR_R_EINVAL;
569   }
570
571   auto* proxy = con->GetAdminProxy();
572   if (proxy == nullptr)
573     return PKGMGR_R_EIO;
574
575   std::string req_key;
576   int ret = proxy->RegisterPkgUpdateInfo(uid, update_info->pkgid,
577       update_info->pkgid, update_info->type, req_key);
578   if (ret != 0) {
579     _E("Request fail");
580     return ret;
581   }
582
583   return GetDelayedResult(pc, req_key);
584 }
585
586 API int pkgmgr_client_usr_unregister_pkg_update_info(pkgmgr_client *pc,
587     const char *pkgid, uid_t uid) {
588   if (pc == nullptr || pkgid == nullptr) {
589     _E("invalid parameter");
590     return PKGMGR_R_EINVAL;
591   }
592
593   auto* con = static_cast<Connector*>(pc);
594   if (con->GetPcType() != PC_REQUEST) {
595     _E("client type is not PC_REQUEST");
596     return PKGMGR_R_EINVAL;
597   }
598
599   auto* proxy = con->GetAdminProxy();
600   if (proxy == nullptr)
601     return PKGMGR_R_EIO;
602
603   std::string req_key;
604   int ret = proxy->UnregisterPkgUpdateInfo(uid, pkgid, req_key);
605
606   if (ret != 0) {
607     _E("Request fail");
608     return ret;
609   }
610
611   return GetDelayedResult(pc, req_key);
612 }
613
614 API int pkgmgr_client_unregister_pkg_update_info(pkgmgr_client *pc,
615     const char *pkgid) {
616   return pkgmgr_client_usr_unregister_pkg_update_info(pc, pkgid, getuid());
617 }
618
619 API int pkgmgr_client_usr_unregister_all_pkg_update_info(pkgmgr_client *pc,
620     uid_t uid) {
621   if (pc == nullptr) {
622     _E("invalid parameter");
623     return PKGMGR_R_EINVAL;
624   }
625
626   auto* con = static_cast<Connector*>(pc);
627   if (con->GetPcType() != PC_REQUEST) {
628     _E("client type is not PC_REQUEST");
629     return PKGMGR_R_EINVAL;
630   }
631
632   auto* proxy = con->GetAdminProxy();
633   if (proxy == nullptr)
634     return PKGMGR_R_EIO;
635
636   std::string req_key;
637   return proxy->UnregisterAllPkgUpdateInfo(uid, req_key);
638 }
639
640 API int pkgmgr_client_unregister_all_pkg_update_info(pkgmgr_client *pc) {
641   return pkgmgr_client_usr_unregister_all_pkg_update_info(pc, getuid());
642 }
643
644 API int pkgmgr_client_usr_activate(pkgmgr_client *pc, const char *pkg_type,
645     const char *pkgid, uid_t uid) {
646   if (pc == nullptr || pkgid == nullptr) {
647     _E("invalid parameter");
648     return PKGMGR_R_EINVAL;
649   }
650
651   auto* con = static_cast<Connector*>(pc);
652   auto* proxy = con->GetAdminProxy();
653   if (proxy == nullptr)
654     return PKGMGR_R_EIO;
655
656   std::string req_key;
657   return proxy->EnablePkgs(uid, { pkgid }, req_key);
658 }
659
660 API int pkgmgr_client_activate(pkgmgr_client *pc, const char *pkg_type,
661     const char *pkgid) {
662   return pkgmgr_client_usr_activate(pc, pkg_type, pkgid, getuid());
663 }
664
665 API int pkgmgr_client_usr_activate_packages(pkgmgr_client *pc,
666     const char *pkg_type, const char **pkgids, int n_pkgs,
667     pkgmgr_handler event_cb, void *data, uid_t uid) {
668   if (pc == nullptr || pkgids == nullptr || n_pkgs < 1) {
669     _E("invalid parameter");
670     return PKGMGR_R_EINVAL;
671   }
672
673   auto* con = static_cast<Connector*>(pc);
674   if (con->GetPcType() != PC_REQUEST) {
675     _E("client type is not PC_REQUEST");
676     return PKGMGR_R_EINVAL;
677   }
678
679   auto* proxy = con->GetAdminProxy();
680   if (proxy == nullptr)
681     return PKGMGR_R_EIO;
682
683   std::string req_key;
684   std::vector<std::string> vec;
685   for (int i = 0; i < n_pkgs; i++) {
686     vec.push_back(pkgids[i]);
687   }
688
689   const auto& receiver = con->GetSignalReceiver();
690   int ret = proxy->EnablePkgs(uid, std::move(vec), req_key);
691   if (ret != PKGMGR_R_OK)
692     return ret;
693
694   return receiver->AddEventHandler(req_key, event_cb, data);
695 }
696
697 API int pkgmgr_client_activate_packages(pkgmgr_client *pc,
698     const char *pkg_type, const char **pkgids, int n_pkgs,
699     pkgmgr_handler event_cb, void *data) {
700   return pkgmgr_client_usr_activate_packages(pc, pkg_type,
701       pkgids, n_pkgs, event_cb, data, getuid());
702 }
703
704 API int pkgmgr_client_usr_deactivate(pkgmgr_client *pc, const char *pkg_type,
705         const char *pkgid, uid_t uid) {
706   if (pc == nullptr || pkgid == nullptr) {
707     _E("invalid parameter");
708     return PKGMGR_R_EINVAL;
709   }
710
711   auto* con = static_cast<Connector*>(pc);
712   auto* proxy = con->GetAdminProxy();
713   if (proxy == nullptr)
714     return PKGMGR_R_EIO;
715
716   std::string req_key;
717   return proxy->DisablePkgs(uid, { pkgid }, req_key);
718 }
719
720 API int pkgmgr_client_deactivate(pkgmgr_client *pc, const char *pkg_type,
721         const char *pkgid) {
722   return pkgmgr_client_usr_deactivate(pc, pkg_type, pkgid, getuid());
723 }
724
725 API int pkgmgr_client_usr_deactivate_packages(pkgmgr_client *pc,
726     const char *pkg_type, const char **pkgids, int n_pkgs,
727     pkgmgr_handler event_cb, void *data, uid_t uid) {
728   if (pc == nullptr || pkgids == nullptr || n_pkgs < 1) {
729     _E("invalid parameter");
730     return PKGMGR_R_EINVAL;
731   }
732
733   auto* con = static_cast<Connector*>(pc);
734   if (con->GetPcType() != PC_REQUEST) {
735     _E("client type is not PC_REQUEST");
736     return PKGMGR_R_EINVAL;
737   }
738
739   auto* proxy = con->GetAdminProxy();
740   if (proxy == nullptr)
741     return PKGMGR_R_EIO;
742
743   std::string req_key;
744   std::vector<std::string> vec;
745   for (int i = 0; i < n_pkgs; i++) {
746     vec.push_back(pkgids[i]);
747   }
748   const auto& receiver = con->GetSignalReceiver();
749   int ret = proxy->DisablePkgs(uid, std::move(vec), req_key);
750   if (ret != PKGMGR_R_OK)
751     return ret;
752
753   return receiver->AddEventHandler(req_key, event_cb, data);
754 }
755
756 API int pkgmgr_client_deactivate_packages(pkgmgr_client *pc,
757     const char *pkg_type, const char **pkgids, int n_pkgs,
758     pkgmgr_handler event_cb, void *data) {
759   return pkgmgr_client_usr_deactivate_packages(pc, pkg_type,
760       pkgids, n_pkgs, event_cb, data, getuid());
761 }
762
763 API int pkgmgr_client_usr_activate_app(pkgmgr_client *pc, const char *appid,
764     pkgmgr_app_handler app_event_cb, void *data, uid_t uid) {
765   if (pc == nullptr || appid == nullptr) {
766     _E("invalid parameter");
767     return PKGMGR_R_EINVAL;
768   }
769
770   auto* con = static_cast<Connector*>(pc);
771   auto* proxy = con->GetAdminProxy();
772   if (proxy == nullptr)
773     return PKGMGR_R_EIO;
774
775   const auto& receiver = con->GetSignalReceiver();
776   std::string req_key;
777   int ret = proxy->EnableApp(uid, appid, req_key);
778   if (ret != PKGMGR_R_OK)
779     return ret;
780
781   return receiver->AddEventHandler(req_key, app_event_cb, data);
782 }
783
784 API int pkgmgr_client_activate_app(pkgmgr_client *pc, const char *appid,
785     pkgmgr_app_handler app_event_cb, void *data) {
786   return pkgmgr_client_usr_activate_app(pc, appid, app_event_cb,
787       data, getuid());
788 }
789
790 API int pkgmgr_client_usr_activate_apps(pkgmgr_client *pc, const char **appids,
791     int n_apps, pkgmgr_app_handler app_event_cb, void *data, uid_t uid) {
792   if (pc == nullptr || appids == nullptr || n_apps < 1) {
793     _E("invalid parameter");
794     return PKGMGR_R_EINVAL;
795   }
796
797   auto* con = static_cast<Connector*>(pc);
798   if (con->GetPcType() != PC_REQUEST) {
799     _E("client type is not PC_REQUEST");
800     return PKGMGR_R_EINVAL;
801   }
802
803   auto* proxy = con->GetAdminProxy();
804   if (proxy == nullptr)
805     return PKGMGR_R_EIO;
806
807   std::string req_key;
808   std::vector<std::string> vec;
809   for (int i = 0; i < n_apps; i++) {
810     vec.push_back(appids[i]);
811   }
812
813   const auto& receiver = con->GetSignalReceiver();
814   int ret = proxy->EnableApps(uid, std::move(vec), req_key);
815   if (ret != PKGMGR_R_OK)
816     return ret;
817
818   return receiver->AddEventHandler(req_key, app_event_cb, data);
819 }
820
821 API int pkgmgr_client_activate_apps(pkgmgr_client *pc, const char **appids,
822     int n_apps, pkgmgr_app_handler app_event_cb, void *data) {
823   return pkgmgr_client_usr_activate_apps(pc, appids, n_apps,
824       app_event_cb, data, getuid());
825 }
826
827 API int pkgmgr_client_activate_global_app_for_uid(pkgmgr_client *pc,
828     const char *appid, pkgmgr_app_handler app_event_cb, void *data, uid_t uid) {
829   if (pc == nullptr || appid == nullptr) {
830     _E("invalid parameter");
831     return PKGMGR_R_EINVAL;
832   }
833
834   auto* con = static_cast<Connector*>(pc);
835   auto* proxy = con->GetAdminProxy();
836   if (proxy == nullptr)
837     return PKGMGR_R_EIO;
838
839   const auto& receiver = con->GetSignalReceiver();
840   std::string req_key;
841   int ret = proxy->EnableGlobalAppForUid(uid, appid, req_key);
842   if (ret != PKGMGR_R_OK)
843     return ret;
844
845   return receiver->AddEventHandler(req_key, app_event_cb, data);
846 }
847
848 API int pkgmgr_client_usr_deactivate_app(pkgmgr_client *pc, const char *appid,
849     pkgmgr_app_handler app_event_cb, void *data, uid_t uid) {
850   if (pc == nullptr || appid == nullptr) {
851     _E("invalid parameter");
852     return PKGMGR_R_EINVAL;
853   }
854
855   auto* con = static_cast<Connector*>(pc);
856   auto* proxy = con->GetAdminProxy();
857   if (proxy == nullptr)
858     return PKGMGR_R_EIO;
859
860   const auto& receiver = con->GetSignalReceiver();
861   std::string req_key;
862   int ret = proxy->DisableApp(uid, appid, req_key);
863   if (ret != PKGMGR_R_OK)
864     return ret;
865
866   return receiver->AddEventHandler(req_key, app_event_cb, data);
867 }
868
869 API int pkgmgr_client_deactivate_app(pkgmgr_client *pc, const char *appid,
870     pkgmgr_app_handler app_event_cb, void *data) {
871   return pkgmgr_client_usr_deactivate_app(pc, appid, app_event_cb, data,
872       getuid());
873 }
874
875 API int pkgmgr_client_usr_deactivate_apps(pkgmgr_client *pc,
876     const char **appids, int n_apps,
877     pkgmgr_app_handler app_event_cb, void *data, uid_t uid) {
878   if (pc == nullptr || appids == nullptr || n_apps < 1) {
879     _E("invalid parameter");
880     return PKGMGR_R_EINVAL;
881   }
882
883   auto* con = static_cast<Connector*>(pc);
884   if (con->GetPcType() != PC_REQUEST) {
885     _E("client type is not PC_REQUEST");
886     return PKGMGR_R_EINVAL;
887   }
888
889   auto* proxy = con->GetAdminProxy();
890   if (proxy == nullptr)
891     return PKGMGR_R_EIO;
892
893   std::string req_key;
894   std::vector<std::string> vec;
895   for (int i = 0; i < n_apps; i++) {
896     vec.push_back(appids[i]);
897   }
898
899   const auto& receiver = con->GetSignalReceiver();
900   int ret = proxy->DisableApps(uid, std::move(vec), req_key);
901   if (ret != PKGMGR_R_OK)
902     return ret;
903
904   return receiver->AddEventHandler(req_key, app_event_cb, data);
905 }
906
907 API int pkgmgr_client_deactivate_apps(pkgmgr_client *pc, const char **appids,
908     int n_apps, pkgmgr_app_handler app_event_cb, void *data) {
909   return pkgmgr_client_usr_deactivate_apps(pc, appids, n_apps,
910       app_event_cb, data, getuid());
911 }
912
913 API int pkgmgr_client_deactivate_global_app_for_uid(pkgmgr_client *pc,
914     const char *appid, pkgmgr_app_handler app_event_cb, void *data, uid_t uid) {
915   if (pc == nullptr || appid == nullptr) {
916     _E("invalid parameter");
917     return PKGMGR_R_EINVAL;
918   }
919
920   auto* con = static_cast<Connector*>(pc);
921   auto* proxy = con->GetAdminProxy();
922   if (proxy == nullptr)
923     return PKGMGR_R_EIO;
924
925   const auto& receiver = con->GetSignalReceiver();
926   std::string req_key;
927   int ret = proxy->DisableGlobalAppForUid(uid, appid, req_key);
928   if (ret != PKGMGR_R_OK)
929     return ret;
930
931   return receiver->AddEventHandler(req_key, app_event_cb, data);
932 }
933
934 API int pkgmgr_client_clear_user_data(pkgmgr_client *pc, const char *pkg_type,
935     const char *pkgid, pkgmgr_mode mode) {
936   return pkgmgr_client_usr_clear_user_data(pc, pkg_type, pkgid, mode,
937       getuid());
938 }
939
940 API int pkgmgr_client_usr_clear_user_data(pkgmgr_client *pc,
941     const char *pkg_type, const char *pkgid, pkgmgr_mode mode,
942     uid_t uid) {
943   if (pc == nullptr || pkgid == nullptr || uid == GLOBAL_USER) {
944     _E("invalid parameter");
945     return PKGMGR_R_EINVAL;
946   }
947
948   auto* con = static_cast<Connector*>(pc);
949   if (con->GetPcType() != PC_REQUEST) {
950     _E("client type is not PC_REQUEST");
951     return PKGMGR_R_EINVAL;
952   }
953
954   auto* proxy = con->GetAdminProxy();
955   if (proxy == nullptr)
956     return PKGMGR_R_EIO;
957
958   return proxy->ClearData(uid, pkgid);
959 }
960
961 API int pkgmgr_client_usr_clear_user_data_with_path(pkgmgr_client *pc,
962     const char *pkg_type, const char *pkgid, const char *file_path,
963     pkgmgr_mode mode, uid_t uid)
964 {
965   if (!pc || !pkgid || !file_path || uid == GLOBAL_USER) {
966     _E("invalid parameter");
967     return PKGMGR_R_EINVAL;
968   }
969
970   auto* con = static_cast<Connector*>(pc);
971   if (con->GetPcType() != PC_REQUEST) {
972     _E("client type is not PC_REQUEST");
973     return PKGMGR_R_EINVAL;
974   }
975
976   auto* proxy = con->GetAdminProxy();
977   if (proxy == nullptr)
978     return PKGMGR_R_EIO;
979
980   return proxy->ClearDataWithPath(uid, pkgid, file_path);
981 }
982
983 API int pkgmgr_client_clear_user_data_with_path(pkgmgr_client *pc, const char *pkg_type,
984     const char *pkgid, const char *file_path, pkgmgr_mode mode)
985 {
986   return pkgmgr_client_usr_clear_user_data_with_path(pc, pkg_type, pkgid,
987       file_path, mode, GetUid());
988 }
989
990 API int pkgmgr_client_set_status_type(pkgmgr_client *pc, int status_type) {
991   //TODO
992   return PKGMGR_R_OK;
993 }
994
995 API int pkgmgr_client_listen_status(pkgmgr_client *pc, pkgmgr_handler event_cb,
996     void *data) {
997   if (pc == nullptr || event_cb == nullptr) {
998     _E("invalid parameter");
999     return PKGMGR_R_EINVAL;
1000   }
1001
1002   auto* con = static_cast<Connector*>(pc);
1003   if (con->GetPcType() != PC_LISTENING) {
1004     _E("client->pc_type is not PC_LISTENING");
1005     return PKGMGR_R_EINVAL;
1006   }
1007
1008   try {
1009     const auto& receiver = con->GetSignalReceiver();
1010     return receiver->AddEventHandler("", event_cb, data);
1011   } catch (...) {
1012     _E("exception occured");
1013     return PKGMGR_R_ERROR;
1014   }
1015 }
1016
1017 API int pkgmgr_client_listen_app_status(pkgmgr_client *pc,
1018     pkgmgr_app_handler app_event_cb, void *data) {
1019   if (pc == nullptr || app_event_cb == nullptr) {
1020     _E("invalid parameter");
1021     return PKGMGR_R_EINVAL;
1022   }
1023
1024   auto* con = static_cast<Connector*>(pc);
1025   if (con->GetPcType() != PC_LISTENING) {
1026     _E("client->pc_type is not PC_LISTENING");
1027     return PKGMGR_R_EINVAL;
1028   }
1029
1030   try {
1031     const auto& receiver = con->GetSignalReceiver();
1032     return receiver->AddEventHandler("", app_event_cb, data);
1033   } catch (...) {
1034     _E("exception occured");
1035     return PKGMGR_R_ERROR;
1036   }
1037 }
1038
1039 API int pkgmgr_client_remove_listen_status(pkgmgr_client *pc) {
1040   //TODO
1041   return PKGMGR_R_OK;
1042 }
1043
1044 API int pkgmgr_client_broadcast_status(pkgmgr_client *pc, const char *pkg_type,
1045     const char *pkgid, const char *key, const char *val) {
1046   /* client cannot broadcast signal */
1047   return PKGMGR_R_OK;
1048 }
1049
1050 /* TODO: deprecate(or remove) */
1051 API int pkgmgr_client_request_service(pkgmgr_request_service_type service_type,
1052     int service_mode, pkgmgr_client *pc, const char *pkg_type,
1053     const char *pkgid, const char *custom_info,
1054     pkgmgr_handler event_cb, void *data) {
1055   return pkgmgr_client_usr_request_service(service_type, service_mode,
1056       pc, pkg_type, pkgid, getuid(), custom_info, event_cb,
1057       data);
1058 }
1059
1060 API int pkgmgr_client_usr_request_service(
1061     pkgmgr_request_service_type service_type, int service_mode,
1062     pkgmgr_client *pc, const char *pkg_type, const char *pkgid,
1063     uid_t uid, const char *custom_info, pkgmgr_handler event_cb,
1064     void *data) {
1065   switch (service_type) {
1066     case PM_REQUEST_MOVE: {
1067       return pkgmgr_client_usr_move(pc, pkg_type, pkgid,
1068           static_cast<pkgmgr_move_type>(service_mode),
1069           event_cb, data, uid);
1070     }
1071     case PM_REQUEST_GET_SIZE: {
1072       auto* con = static_cast<Connector*>(pc);
1073       auto* proxy = con->GetInfoProxy();
1074       std::string req_key;
1075       int ret = proxy->GetSizeSync(uid, pkgid, service_mode, req_key);
1076       if (ret != PKGMGR_R_OK)
1077         return ret;
1078
1079       return ResultGetSizeSync(pc, req_key);
1080     }
1081     case PM_REQUEST_KILL_APP: {
1082       auto* con = static_cast<Connector*>(pc);
1083       auto* proxy = con->GetAdminProxy();
1084       int pid = 0;
1085       int ret = proxy->Kill(uid, pkgid, pid);
1086       *(int *)data = pid;
1087
1088       return ret;
1089     }
1090     case PM_REQUEST_CHECK_APP: {
1091       auto* con = static_cast<Connector*>(pc);
1092       auto* proxy = con->GetInfoProxy();
1093       int pid = 0;
1094       int ret = proxy->Check(uid, pkgid, pid);
1095       *(int *)data = pid;
1096
1097       return ret;
1098     }
1099     default:
1100       _E("Wrong Request");
1101       break;
1102   }
1103   return 0;
1104 }
1105
1106 API int pkgmgr_client_usr_clear_cache_dir(const char *pkgid, uid_t uid) {
1107   if (!pkgid) {
1108     _E("invalid parameter");
1109     return PKGMGR_R_EINVAL;
1110   }
1111
1112   auto* con = new Connector(PC_REQUEST);
1113   auto* proxy = con->GetCacheProxy();
1114   if (proxy == nullptr)
1115     return PKGMGR_R_EIO;
1116
1117   int ret = proxy->ClearCache(uid, pkgid);
1118   delete con;
1119
1120   return ret;
1121 }
1122
1123 API int pkgmgr_client_clear_cache_dir(const char *pkgid) {
1124   return pkgmgr_client_usr_clear_cache_dir(pkgid, getuid());
1125 }
1126
1127 API int pkgmgr_client_clear_usr_all_cache_dir(uid_t uid) {
1128   return pkgmgr_client_usr_clear_cache_dir(PKG_CLEAR_ALL_CACHE, uid);
1129 }
1130
1131 API int pkgmgr_client_clear_all_cache_dir() {
1132   return pkgmgr_client_usr_clear_cache_dir(
1133       PKG_CLEAR_ALL_CACHE, getuid());
1134 }
1135
1136 API int pkgmgr_client_get_size(pkgmgr_client *pc, const char *pkgid,
1137     pkgmgr_getsize_type get_type, pkgmgr_handler event_cb,
1138     void *data) {
1139   return pkgmgr_client_usr_get_size(pc, pkgid, get_type, event_cb, data,
1140       getuid());
1141 }
1142
1143 /* TODO: deprecate(or remove) */
1144 API int pkgmgr_client_usr_get_size(pkgmgr_client *pc, const char *pkgid,
1145     pkgmgr_getsize_type get_type, pkgmgr_handler event_cb,
1146     void *data, uid_t uid) {
1147   if (pc == nullptr || pkgid == nullptr || event_cb == nullptr) {
1148     _E("invalid parameter");
1149     return PKGMGR_R_EINVAL;
1150   }
1151
1152   if (strcmp(pkgid, PKG_SIZE_INFO_TOTAL) == 0)
1153     get_type = PM_GET_TOTAL_PKG_SIZE_INFO;
1154   else
1155     get_type = PM_GET_PKG_SIZE_INFO;
1156
1157   auto* con = static_cast<Connector*>(pc);
1158   if (con->GetPcType() != PC_REQUEST) {
1159     _E("client type is not PC_REQUEST");
1160     return PKGMGR_R_EINVAL;
1161   }
1162
1163   auto* proxy = con->GetInfoProxy();
1164   if (proxy == nullptr)
1165     return PKGMGR_R_EIO;
1166
1167   const auto& receiver = con->GetSignalReceiver();
1168   std::string req_key;
1169   int ret = proxy->GetSize(uid, pkgid, get_type, req_key);
1170   if (ret != PKGMGR_R_OK)
1171     return ret;
1172
1173   return receiver->AddEventHandler(req_key, event_cb, data);
1174 }
1175
1176 API int pkgmgr_client_get_package_size_info(pkgmgr_client *pc,
1177     const char *pkgid, pkgmgr_pkg_size_info_receive_cb event_cb,
1178     void *user_data) {
1179   return pkgmgr_client_usr_get_package_size_info(pc, pkgid, event_cb,
1180       user_data, getuid());
1181 }
1182
1183 API int pkgmgr_client_usr_get_total_package_size_info(pkgmgr_client *pc,
1184     pkgmgr_total_pkg_size_info_receive_cb event_cb,
1185     void *user_data, uid_t uid) {
1186   return pkgmgr_client_usr_get_package_size_info(pc, PKG_SIZE_INFO_TOTAL,
1187       (pkgmgr_pkg_size_info_receive_cb)event_cb,
1188       user_data, uid);
1189 }
1190
1191 API int pkgmgr_client_get_total_package_size_info(pkgmgr_client *pc,
1192     pkgmgr_total_pkg_size_info_receive_cb event_cb, void *user_data) {
1193   return pkgmgr_client_usr_get_package_size_info(pc, PKG_SIZE_INFO_TOTAL,
1194       (pkgmgr_pkg_size_info_receive_cb)event_cb,
1195       user_data, getuid());
1196 }
1197
1198 API int pkgmgr_client_usr_get_package_size_info(pkgmgr_client *pc,
1199     const char *pkgid, pkgmgr_pkg_size_info_receive_cb event_cb,
1200     void *user_data, uid_t uid) {
1201   if (pc == nullptr || pkgid == nullptr || event_cb == nullptr) {
1202     _E("invalid parameter");
1203     return PKGMGR_R_EINVAL;
1204   }
1205
1206   int get_type;
1207   if (strcmp(pkgid, PKG_SIZE_INFO_TOTAL) == 0)
1208     get_type = PM_GET_TOTAL_PKG_SIZE_INFO;
1209   else
1210     get_type = PM_GET_PKG_SIZE_INFO;
1211
1212   auto* con = static_cast<Connector*>(pc);
1213   if (con->GetPcType() != PC_REQUEST) {
1214     _E("client type is not PC_REQUEST");
1215     return PKGMGR_R_EINVAL;
1216   }
1217
1218   auto* proxy = con->GetInfoProxy();
1219   if (proxy == nullptr)
1220     return PKGMGR_R_EIO;
1221
1222   const auto& receiver = con->GetSignalReceiver();
1223   std::string req_key;
1224   int ret = proxy->GetSize(uid, pkgid, get_type, req_key);
1225   if (ret != PKGMGR_R_OK)
1226     return ret;
1227
1228   receiver->AddEventHandler(req_key, event_cb, pc, user_data);
1229
1230   return PKGMGR_R_OK;
1231 }
1232
1233 API int pkgmgr_client_generate_license_request(pkgmgr_client *pc,
1234     const char *resp_data, char **req_data, char **license_url) {
1235   if (pc == nullptr || resp_data == nullptr || req_data == nullptr ||
1236       license_url == nullptr) {
1237     _E("invalid parameter");
1238     return PKGMGR_R_EINVAL;
1239   }
1240
1241   auto* con = static_cast<Connector*>(pc);
1242   if (con->GetPcType() != PC_REQUEST) {
1243     _E("client type is not PC_REQUEST");
1244     return PKGMGR_R_EINVAL;
1245   }
1246
1247   auto* proxy = con->GetAdminProxy();
1248   if (proxy == nullptr)
1249     return PKGMGR_R_EIO;
1250
1251   std::string data;
1252   std::string url;
1253   std::string req_key;
1254   int ret = proxy->GenerateLicenseRequest(resp_data, req_key);
1255   if (ret != PKGMGR_R_OK)
1256     return ret;
1257
1258   ret = GetGenerateLicenseResult(pc, req_key, data, url);
1259   if (ret != PKGMGR_R_OK)
1260     return ret;
1261
1262   *req_data = strdup(data.c_str());
1263   *license_url = strdup(url.c_str());
1264   return PKGMGR_R_OK;
1265 }
1266
1267 API int pkgmgr_client_register_license(pkgmgr_client *pc, const char *resp_data) {
1268   if (pc == nullptr || resp_data == nullptr) {
1269     _E("invalid parameter");
1270     return PKGMGR_R_EINVAL;
1271   }
1272
1273   auto* con = static_cast<Connector*>(pc);
1274   if (con->GetPcType() != PC_REQUEST) {
1275     _E("client type is not PC_REQUEST");
1276     return PKGMGR_R_EINVAL;
1277   }
1278
1279   auto* proxy = con->GetAdminProxy();
1280   if (proxy == nullptr)
1281     return PKGMGR_R_EIO;
1282
1283   std::string req_key;
1284   int ret = proxy->RegisterLicense(resp_data, req_key);
1285
1286   if (ret != 0) {
1287     _E("Request fail");
1288     return ret;
1289   }
1290
1291   return GetDelayedResult(pc, req_key);
1292 }
1293
1294 API int pkgmgr_client_decrypt_package(pkgmgr_client *pc,
1295     const char *drm_file_path, const char *decrypted_file_path) {
1296   if (pc == nullptr || drm_file_path == nullptr ||
1297       decrypted_file_path == nullptr) {
1298     _E("invalid parameter");
1299     return PKGMGR_R_EINVAL;
1300   }
1301
1302   auto* con = static_cast<Connector*>(pc);
1303   if (con->GetPcType() != PC_REQUEST) {
1304     _E("client type is not PC_REQUEST");
1305     return PKGMGR_R_EINVAL;
1306   }
1307
1308   auto* proxy = con->GetAdminProxy();
1309   if (proxy == nullptr)
1310     return PKGMGR_R_EIO;
1311
1312   std::string req_key;
1313   int ret = proxy->DecryptPackage(drm_file_path, decrypted_file_path, req_key);
1314
1315   if (ret != 0) {
1316     _E("Request fail");
1317     return ret;
1318   }
1319
1320   return GetDelayedResult(pc, req_key);
1321 }
1322
1323 API int pkgmgr_client_enable_splash_screen(pkgmgr_client *pc, const char *appid) {
1324   return pkgmgr_client_usr_enable_splash_screen(pc, appid, getuid());
1325 }
1326
1327 API int pkgmgr_client_usr_enable_splash_screen(pkgmgr_client *pc,
1328     const char *appid, uid_t uid) {
1329   if (pc == nullptr || appid == nullptr) {
1330     _E("Invalid parameter");
1331     return PKGMGR_R_EINVAL;
1332   }
1333
1334   auto* con = static_cast<Connector*>(pc);
1335   auto* proxy = con->GetAdminProxy();
1336   if (proxy == nullptr)
1337     return PKGMGR_R_EIO;
1338
1339   return proxy->EnableAppSplashScreen(uid, appid);
1340 }
1341
1342 API int pkgmgr_client_disable_splash_screen(pkgmgr_client *pc,
1343     const char *appid) {
1344   return pkgmgr_client_usr_disable_splash_screen(pc, appid,
1345       getuid());
1346 }
1347
1348 API int pkgmgr_client_usr_disable_splash_screen(pkgmgr_client *pc,
1349     const char *appid, uid_t uid) {
1350   if (pc == nullptr || appid == nullptr) {
1351     _E("Invalid parameter");
1352     return PKGMGR_R_EINVAL;
1353   }
1354
1355   auto* con = static_cast<Connector*>(pc);
1356   auto* proxy = con->GetAdminProxy();
1357   if (proxy == nullptr)
1358     return PKGMGR_R_EIO;
1359
1360   return proxy->DisableAppSplashScreen(uid, appid);
1361 }
1362
1363 API int pkgmgr_client_usr_set_pkg_restriction_mode(pkgmgr_client *pc,
1364     const char *pkgid, int mode, uid_t uid) {
1365   if (pc == nullptr || pkgid == nullptr || strlen(pkgid) == 0 || mode <= 0) {
1366     _E("invalid parameter");
1367     return PKGMGR_R_EINVAL;
1368   }
1369
1370   return PKGMGR_R_OK;
1371 }
1372
1373 API int pkgmgr_client_set_pkg_restriction_mode(pkgmgr_client *pc,
1374     const char *pkgid, int mode) {
1375   return pkgmgr_client_usr_set_pkg_restriction_mode(pc, pkgid, mode,
1376       getuid());
1377 }
1378
1379 API int pkgmgr_client_usr_unset_pkg_restriction_mode(pkgmgr_client *pc,
1380     const char *pkgid, int mode, uid_t uid) {
1381   if (pc == nullptr || pkgid == nullptr || strlen(pkgid) == 0 || mode <= 0) {
1382     _E("invalid parameter");
1383     return PKGMGR_R_EINVAL;
1384   }
1385
1386   return PKGMGR_R_OK;
1387 }
1388
1389 API int pkgmgr_client_unset_pkg_restriction_mode(pkgmgr_client *pc,
1390     const char *pkgid, int mode) {
1391   return pkgmgr_client_usr_unset_pkg_restriction_mode(pc, pkgid, mode,
1392       getuid());
1393 }
1394
1395 API int pkgmgr_client_usr_get_pkg_restriction_mode(pkgmgr_client *pc,
1396     const char *pkgid, int *mode, uid_t uid) {
1397   if (pc == nullptr || pkgid == nullptr || strlen(pkgid) == 0) {
1398     _E("invalid parameter");
1399     return PKGMGR_R_EINVAL;
1400   }
1401
1402   return PKGMGR_R_OK;
1403 }
1404
1405 API int pkgmgr_client_get_pkg_restriction_mode(pkgmgr_client *pc,
1406     const char *pkgid, int *mode) {
1407   return pkgmgr_client_usr_get_pkg_restriction_mode(pc, pkgid, mode,
1408       getuid());
1409 }
1410
1411 API int pkgmgr_client_usr_set_restriction_mode(pkgmgr_client *pc, int mode,
1412     uid_t uid) {
1413   if (pc == nullptr) {
1414     _E("invalid parameter");
1415     return PKGMGR_R_EINVAL;
1416   }
1417
1418   return PKGMGR_R_OK;
1419 }
1420
1421 API int pkgmgr_client_set_restriction_mode(pkgmgr_client *pc, int mode) {
1422   return pkgmgr_client_usr_set_restriction_mode(pc, mode, getuid());
1423 }
1424
1425 API int pkgmgr_client_usr_unset_restriction_mode(pkgmgr_client *pc, int mode,
1426     uid_t uid) {
1427   if (pc == nullptr) {
1428     _E("invalid parameter");
1429     return PKGMGR_R_EINVAL;
1430   }
1431
1432   return PKGMGR_R_OK;
1433 }
1434
1435 API int pkgmgr_client_unset_restriction_mode(pkgmgr_client *pc, int mode) {
1436   return pkgmgr_client_usr_unset_restriction_mode(pc, mode, getuid());
1437 }
1438
1439 API int pkgmgr_client_usr_get_restriction_mode(pkgmgr_client *pc,
1440     int *mode, uid_t uid) {
1441   if (pc == nullptr || mode == nullptr) {
1442     _E("invalid parameter");
1443     return PKGMGR_R_EINVAL;
1444   }
1445
1446   *mode = 0;
1447
1448   return PKGMGR_R_OK;
1449 }
1450
1451 API int pkgmgr_client_get_restriction_mode(pkgmgr_client *pc, int *mode) {
1452   return pkgmgr_client_usr_get_restriction_mode(pc, mode, getuid());
1453 }
1454
1455 API pkgmgr_info *pkgmgr_client_check_pkginfo_from_file(const char *pkg_path) {
1456   if (pkg_path == nullptr) {
1457     _E("invalid parameter");
1458     return nullptr;
1459   }
1460
1461   std::string type = pkgmgr::client::GetTypeFromPath(pkg_path);
1462   if (type.empty()) {
1463     _E("invalid package type");
1464     return nullptr;
1465   }
1466
1467   pkg_plugin_set* plugin_set = pkgmgr::client::LoadPluginSet(type);
1468   if (!plugin_set) {
1469     _E("failed to load library for %s", type.c_str());
1470     return nullptr;
1471   }
1472
1473   package_manager_pkg_detail_info_t* info =
1474       reinterpret_cast<package_manager_pkg_detail_info_t*>(
1475           calloc(1, sizeof(package_manager_pkg_detail_info_t)));
1476   if (!info) {
1477     _E("out of memory");
1478     return nullptr;
1479   }
1480
1481   int r = plugin_set->get_pkg_detail_info_from_package(pkg_path, info);
1482   if (r) {
1483     _E("failed to get package detail info");
1484     free(info);
1485     return nullptr;
1486   }
1487
1488   return info;
1489 }
1490
1491 API int pkgmgr_client_free_pkginfo(pkgmgr_info *info) {
1492   package_manager_pkg_detail_info_t *pkg_info =
1493       reinterpret_cast<package_manager_pkg_detail_info_t*>(info);
1494   if (pkg_info == nullptr) {
1495     _E("invalid parameter");
1496     return PKGMGR_R_EINVAL;
1497   }
1498
1499   g_list_free_full(pkg_info->privilege_list, free);
1500   free(pkg_info->icon_buf);
1501   free(pkg_info);
1502
1503   return PKGMGR_R_OK;
1504 }
1505
1506 API int pkgmgr_client_usr_set_app_label(pkgmgr_client *pc, char *appid,
1507     char *label, uid_t uid) {
1508   if (pc == nullptr || appid == nullptr || label == nullptr) {
1509     _E("Invalid parameter");
1510     return PKGMGR_R_EINVAL;
1511   }
1512
1513   auto* con = static_cast<Connector*>(pc);
1514   auto* proxy = con->GetAdminProxy();
1515   if (proxy == nullptr)
1516     return PKGMGR_R_EIO;
1517
1518   std::string req_key;
1519   int ret = proxy->SetAppLabel(uid, appid, label, req_key);
1520
1521   if (ret != 0) {
1522     _E("Request fail");
1523     return ret;
1524   }
1525
1526   return GetDelayedResult(pc, req_key);
1527 }
1528
1529 API int pkgmgr_client_set_app_label(pkgmgr_client *pc, char *appid, char *label) {
1530   return pkgmgr_client_usr_set_app_label(pc, appid, label, getuid());
1531 }
1532
1533 API int pkgmgr_client_usr_set_app_icon(pkgmgr_client *pc, char *appid,
1534     char *icon_path, uid_t uid) {
1535   if (pc == nullptr || appid == nullptr || icon_path == nullptr) {
1536     _E("Invalid parameter");
1537     return PKGMGR_R_EINVAL;
1538   }
1539
1540   auto* con = static_cast<Connector*>(pc);
1541   auto* proxy = con->GetAdminProxy();
1542   if (proxy == nullptr)
1543     return PKGMGR_R_EIO;
1544
1545   std::string req_key;
1546   int ret = proxy->SetAppIcon(uid, appid, icon_path, req_key);
1547
1548   if (ret != 0) {
1549     _E("Request fail");
1550     return ret;
1551   }
1552
1553   return GetDelayedResult(pc, req_key);
1554 }
1555
1556 API int pkgmgr_client_set_app_icon(pkgmgr_client *pc, char *appid, char *icon_path) {
1557   return pkgmgr_client_usr_set_app_icon(pc, appid, icon_path, getuid());
1558 }
1559
1560 API int pkgmgr_client_set_debug_mode(pkgmgr_client *pc, bool debug_mode) {
1561   if (pc == nullptr) {
1562     _E("invalid parameter");
1563     return PKGMGR_R_EINVAL;
1564   }
1565
1566   auto* con = static_cast<Connector*>(pc);
1567   if (debug_mode)
1568     con->SetDebugMode();
1569
1570   return PKGMGR_R_OK;
1571 }
1572
1573 API int pkgmgr_client_set_skip_optimization(pkgmgr_client *pc, bool skip_optimization) {
1574   if (pc == nullptr) {
1575     _E("invalid parameter");
1576     return PKGMGR_R_EINVAL;
1577   }
1578
1579   auto* con = static_cast<Connector*>(pc);
1580   if (skip_optimization)
1581     con->SetSkipOptimization();
1582
1583   return PKGMGR_R_OK;
1584 }
1585
1586 API int pkgmgr_client_usr_migrate_external_image(pkgmgr_client *pc,
1587     const char *pkgid, uid_t uid) {
1588   if (pc == nullptr) {
1589     _E("invalid parameter");
1590     return PKGMGR_R_EINVAL;
1591   }
1592
1593   auto* con = static_cast<Connector*>(pc);
1594   auto* proxy = con->GetAdminProxy();
1595   if (proxy == nullptr)
1596     return PKGMGR_R_EIO;
1597
1598   return proxy->MigrateExternalImage(uid, pkgid);
1599 }
1600
1601 API int pkgmgr_client_add_res_copy_path(pkgmgr_client *pc,
1602     const char *src_path, const char *dest_path) {
1603   if (pc == nullptr || src_path == nullptr) {
1604     _E("invalid parameter");
1605     return PKGMGR_R_EINVAL;
1606   }
1607
1608   auto* con = static_cast<Connector*>(pc);
1609   con->GetResCopyPath().emplace_back(src_path, dest_path ? dest_path: "");
1610
1611   return PKGMGR_R_OK;
1612 }
1613
1614 API int pkgmgr_client_res_copy(pkgmgr_client *pc,
1615     pkgmgr_res_handler event_cb, void *user_data) {
1616   if (pc == nullptr || event_cb == nullptr) {
1617     _E("invalid parameter");
1618     return PKGMGR_R_EINVAL;
1619   }
1620
1621   auto* con = static_cast<Connector*>(pc);
1622   auto* proxy = con->GetAdminProxy();
1623   if (proxy == nullptr)
1624     return PKGMGR_R_EIO;
1625
1626   const auto& receiver = con->GetSignalReceiver();
1627
1628   std::string req_key;
1629   int ret = proxy->ResCopy(con->GetResCopyPath(), req_key);
1630   if (ret != PKGMGR_R_OK)
1631     return ret;
1632
1633   return receiver->AddEventHandler(req_key, event_cb, user_data);
1634 }
1635
1636 API int pkgmgr_client_add_res_create_dir_path(pkgmgr_client *pc,
1637     const char *dir_path) {
1638   if (pc == nullptr || dir_path == nullptr) {
1639     _E("invalid parameter");
1640     return PKGMGR_R_EINVAL;
1641   }
1642
1643   auto* con = static_cast<Connector*>(pc);
1644   con->GetResCreateDir().emplace_back(dir_path);
1645
1646   return PKGMGR_R_OK;
1647 }
1648
1649 API int pkgmgr_client_res_create_dir(pkgmgr_client *pc,
1650     pkgmgr_res_handler event_cb, void *user_data) {
1651   if (pc == nullptr || event_cb == nullptr) {
1652     _E("invalid parameter");
1653     return PKGMGR_R_EINVAL;
1654   }
1655
1656   auto* con = static_cast<Connector*>(pc);
1657   auto* proxy = con->GetAdminProxy();
1658   if (proxy == nullptr)
1659     return PKGMGR_R_EIO;
1660
1661   const auto& receiver = con->GetSignalReceiver();
1662
1663   std::string req_key;
1664   int ret = proxy->ResCreateDir(con->GetResCreateDir(), req_key);
1665   if (ret != PKGMGR_R_OK)
1666     return ret;
1667
1668   return receiver->AddEventHandler(req_key, event_cb, user_data);
1669 }
1670
1671 API int pkgmgr_client_add_res_remove_path(pkgmgr_client *pc,
1672     const char *res_path) {
1673   if (pc == nullptr || res_path == nullptr) {
1674     _E("invalid parameter");
1675     return PKGMGR_R_EINVAL;
1676   }
1677
1678   auto* con = static_cast<Connector*>(pc);
1679   con->GetResRemovePath().push_back(res_path);
1680
1681   return PKGMGR_R_OK;
1682 }
1683
1684 API int pkgmgr_client_res_remove(pkgmgr_client *pc,
1685     pkgmgr_res_handler event_cb, void *user_data) {
1686   if (pc == nullptr || event_cb == nullptr) {
1687     _E("invalid parameter");
1688     return PKGMGR_R_EINVAL;
1689   }
1690
1691   auto* con = static_cast<Connector*>(pc);
1692   auto* proxy = con->GetAdminProxy();
1693   if (proxy == nullptr)
1694     return PKGMGR_R_EIO;
1695
1696   const auto& receiver = con->GetSignalReceiver();
1697
1698   std::string req_key;
1699   int ret = proxy->ResRemove(con->GetResRemovePath(), req_key);
1700   if (ret != PKGMGR_R_OK)
1701     return ret;
1702
1703   return receiver->AddEventHandler(req_key, event_cb, user_data);
1704 }
1705
1706 API int pkgmgr_client_res_uninstall(pkgmgr_client *pc, const char *pkgid) {
1707   return pkgmgr_client_res_usr_uninstall(pc, pkgid, getuid());
1708 }
1709
1710 API int pkgmgr_client_res_usr_uninstall(pkgmgr_client *pc, const char *pkgid,
1711     uid_t uid) {
1712   if (pc == nullptr || pkgid == nullptr) {
1713     _E("invalid parameter");
1714     return PKGMGR_R_EINVAL;
1715   }
1716
1717   auto* con = static_cast<Connector*>(pc);
1718   auto* proxy = con->GetAdminProxy();
1719   if (proxy == nullptr)
1720     return PKGMGR_R_EIO;
1721
1722   std::string req_key;
1723   return proxy->ResUninstall(uid, pkgid, req_key);
1724 }
1725
1726 API pkgmgr_res_event_info *pkgmgr_res_event_info_new() {
1727   pkgmgr_res_event_info* info =
1728       reinterpret_cast<pkgmgr_res_event_info*>(
1729           new rpc_port::PkgSignal::ExtraData);
1730   if (!info) {
1731     _E("Out of memory");
1732     return nullptr;
1733   }
1734   return info;
1735 }
1736
1737 API int pkgmgr_res_event_info_free(pkgmgr_res_event_info *info) {
1738   if (!info) {
1739     _E("invalid parameter");
1740     return PKGMGR_R_EINVAL;
1741   }
1742   rpc_port::PkgSignal::ExtraData* event_info =
1743       reinterpret_cast<rpc_port::PkgSignal::ExtraData*>(info);
1744   delete event_info;
1745
1746   return PKGMGR_R_OK;
1747 }
1748
1749 API int pkgmgr_res_event_info_set_error_code(pkgmgr_res_event_info *handle, int error_code) {
1750   if (!handle) {
1751     _E("invalid parameter");
1752     return PKGMGR_R_EINVAL;
1753   }
1754
1755   rpc_port::PkgSignal::ExtraData* event_info =
1756       reinterpret_cast<rpc_port::PkgSignal::ExtraData*>(handle);
1757   event_info->SetErrCode(error_code);
1758
1759   return PKGMGR_R_OK;
1760 }
1761
1762 API int pkgmgr_res_event_info_get_error_code(pkgmgr_res_event_info *handle, int *error_code) {
1763   if (!handle) {
1764     _E("invalid parameter");
1765     return PKGMGR_R_EINVAL;
1766   }
1767
1768   rpc_port::PkgSignal::ExtraData* event_info =
1769       reinterpret_cast<rpc_port::PkgSignal::ExtraData*>(handle);
1770   *error_code = event_info->GetErrCode();
1771
1772   return PKGMGR_R_OK;
1773 }
1774
1775 API int pkgmgr_res_event_info_add_path_state(pkgmgr_res_event_info *handle,
1776     const char *path, pkgmgr_res_event_path_state state) {
1777   if (!handle || !path) {
1778     _E("invalid parameter");
1779     return PKGMGR_R_EINVAL;
1780   }
1781
1782   rpc_port::PkgSignal::ExtraData* event_info =
1783       reinterpret_cast<rpc_port::PkgSignal::ExtraData*>(handle);
1784
1785   auto paths = event_info->GetPaths();
1786   paths.emplace_back(path, state);
1787   event_info->SetPaths(std::move(paths));
1788
1789   return PKGMGR_R_OK;
1790 }
1791
1792 API int pkgmgr_res_event_info_foreach_path(pkgmgr_res_event_info *handle,
1793     pkgmgr_res_event_path_cb callback, void *user_data) {
1794   if (!handle || !callback) {
1795     _E("invalid parameter");
1796     return PKGMGR_R_EINVAL;
1797   }
1798
1799   rpc_port::PkgSignal::ExtraData* event_info =
1800       reinterpret_cast<rpc_port::PkgSignal::ExtraData*>(handle);
1801
1802   for (const auto& p : event_info->GetPaths()) {
1803     if (callback(p.GetPath().c_str(),
1804         static_cast<pkgmgr_res_event_path_state>(p.GetState()), user_data) < 0)
1805       return PKGMGR_R_OK;
1806   }
1807
1808   return PKGMGR_R_OK;
1809 }