Tizen 2.4 SDK Rev6 Release
[framework/appfw/aul-1.git] / am_daemon / amd_appinfo.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <assert.h>
7 #include <glib.h>
8 #include <dirent.h>
9
10 #include <pkgmgr-info.h>
11 #include <vconf.h>
12 #include "amd_config.h"
13 #include "simple_util.h"
14 #include "amd_appinfo.h"
15 #include "amd_launch.h"
16 #include "amd_request.h"
17 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
18 #include <pkgmgr-info.h>
19 #include <pkgmgrinfo_type.h>
20 #endif
21
22
23 #define SERVICE_GROUP "Service"
24
25 struct appinfomgr {
26         GHashTable *tbl; /* key is filename, value is struct appinfo */
27 };
28
29 enum _appinfo_idx {
30         _AI_FILE = 0, /* service filename */
31         _AI_NAME,
32         _AI_EXEC,
33         _AI_TYPE,
34         _AI_ONBOOT,
35         _AI_RESTART,
36         _AI_MULTI,
37         _AI_HWACC,
38         _AI_PERM,
39         _AI_PKGID,
40         _AI_TASKMANAGE,
41         _AI_PRELOAD,
42         _AI_INDICATORDISP,
43         _AI_EFFECTIMAGEPORT,
44         _AI_EFFECTIMAGELAND,
45 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
46         _AI_EFFECTTYPE,
47 #endif
48         _AI_STATUS,
49 #ifdef _APPFW_FEATURE_PROCESS_POOL
50         _AI_POOL,
51 #endif
52         _AI_COMPTYPE,
53 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
54         _AI_MULTI_INSTANCE,
55         _AI_MULTI_INSTANCE_MAINID,
56         _AI_TOGGLE_ORDER,
57 #endif
58 #ifdef _APPFW_FEATURE_TTS_MODE
59         _AI_TTS,
60 #endif
61 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
62         _AI_UPS,
63 #endif
64 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
65         _AI_COOLDOWN,
66 #endif
67 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
68         _AI_TEP,
69         _AI_STORAGE_TYPE,
70 #endif
71         _AI_ALLOWED_BG,
72         _AI_API_VER,
73         _AI_LAUNCH_MODE,
74 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
75         _AI_EFFECTIVE_APPID,
76 #endif
77 #ifdef _APPFW_FEATURE_PRIVATE_SERVICE
78         _AI_VISIBILITY,
79 #endif
80         _AI_MAX,
81 };
82 #define _AI_START _AI_NAME /* start index */
83
84 struct appinfo_t {
85         char *name;
86         enum appinfo_type type;
87 };
88
89 static struct appinfo_t _appinfos[] = {
90         [_AI_NAME] = { "Name", AIT_NAME, },
91         [_AI_EXEC] = { "Exec", AIT_EXEC, },
92         [_AI_TYPE] = { "PkgType", AIT_TYPE, },
93         [_AI_ONBOOT] = { "StartOnBoot", AIT_ONBOOT, },
94         [_AI_RESTART] = { "AutoRestart", AIT_RESTART, },
95         [_AI_MULTI] = { "Multiple", AIT_MULTI, },
96         [_AI_HWACC] = { "Hwacceleration", AIT_HWACC, },
97         [_AI_PERM] = { "PermissionType", AIT_PERM, },
98         [_AI_PKGID] = { "PackageId", AIT_PKGID, },
99         [_AI_TASKMANAGE] = { "Taskmanage", AIT_TASKMANAGE, },
100         [_AI_PRELOAD] = { "Preload", AIT_PRELOAD, },
101         [_AI_INDICATORDISP] = { "indicatordisplay", AIT_INDICATOR_DISP, },
102         [_AI_EFFECTIMAGEPORT] = { "portaitimg", AIT_EFFECTIMAGEPORT, },
103         [_AI_EFFECTIMAGELAND] = { "landscapeimg", AIT_EFFECTIMAGELAND, },
104 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
105         [_AI_EFFECTTYPE] = { "EffectType", AIT_EFFECTTYPE, },
106 #endif
107         [_AI_STATUS] = { "status", AIT_STATUS, },
108 #ifdef _APPFW_FEATURE_PROCESS_POOL
109         [_AI_POOL] = { "ProcessPool", AIT_POOL, },
110 #endif
111         [_AI_COMPTYPE] = { "ComponentType", AIT_COMPTYPE, },
112 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
113         [_AI_MULTI_INSTANCE] = { "multi-instance", AIT_MULTI_INSTANCE, },
114         [_AI_MULTI_INSTANCE_MAINID] = { "multi-instance-mainid", AIT_MULTI_INSTANCE_MAINID, },
115         [_AI_TOGGLE_ORDER] = { "toggleOrder", AIT_TOGGLE_ORDER, },
116 #endif
117 #ifdef _APPFW_FEATURE_TTS_MODE
118         [_AI_TTS] = { "TTS", AIT_TTS, },
119 #endif
120 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
121         [_AI_UPS] = { "UPS", AIT_UPS, },
122 #endif
123 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
124         [_AI_COOLDOWN] = { "CoolDown", AIT_COOLDOWN, },
125 #endif
126 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
127         [_AI_TEP] = {"Tep", AIT_TEP},
128         [_AI_STORAGE_TYPE] = {"StorageType", AIT_STORAGE_TYPE},
129 #endif
130         [_AI_ALLOWED_BG] = {"AllowedBackground", AIT_ALLOWED_BG },
131         [_AI_API_VER] = {"ApiVersion", AIT_API_VER },
132         [_AI_LAUNCH_MODE] = {"launch_mode", AIT_LAUNCH_MODE },
133 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
134         [_AI_EFFECTIVE_APPID] = {"effective-appid", AIT_EFFECTIVE_APPID },
135 #endif
136 #ifdef _APPFW_FEATURE_PRIVATE_SERVICE
137         [_AI_VISIBILITY] = {"visibility", AIT_VISIBILITY },
138 #endif
139
140 };
141
142 struct appinfo {
143         char *val[_AI_MAX];
144 };
145
146 struct pkginfo {
147         char *pkgid;
148         char *op;
149 };
150
151 int gles = 1;
152
153 GHashTable *pkg_tbl;
154
155 static void _free_appinfo(gpointer data)
156 {
157         struct appinfo *c = data;
158         int i;
159
160         if (!c)
161                 return;
162
163         for (i = 0; i < sizeof(c->val)/sizeof(c->val[0]); i++) {
164                 if(c->val[i] != NULL)
165                         free(c->val[i]);
166         }
167
168         free(c);
169 }
170
171 static void _fini(struct appinfomgr *cf)
172 {
173         assert(cf);
174
175         g_hash_table_destroy(cf->tbl);
176         g_hash_table_destroy(pkg_tbl);
177         free(cf);
178 }
179
180 static int __set_allowed_bg(const char *category_name, void *user_data)
181 {
182         /* assume any of background category declared means that background running is allowed */
183         bool *allowed_bg = (bool *)user_data;
184
185         if (category_name && strcmp(category_name, "enable") == 0) {
186                 return 0;
187         }
188
189         if (category_name && strcmp(category_name, "disable") == 0) {
190                 *allowed_bg = false;
191                 return -1;
192         }
193
194         _D("background category:%s", category_name);
195         *allowed_bg = true;
196         return -1;
197 }
198
199 static int __app_info_insert_handler(const pkgmgrinfo_appinfo_h handle, void *data)
200 {
201         struct appinfo *c;
202         struct appinfomgr *cf = (struct appinfomgr *)data;
203         gboolean r;
204         char *exec;
205         char *portraitimg;
206         char *landscapeimg;
207 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
208         char *effectimg_type;
209 #endif
210         char *type;
211         char *appid;
212         char *pkgid;
213         char *component_type;
214         char *multi_mainid;
215         bool multiple;
216 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
217         bool multi_instance;
218 #endif
219         bool onboot;
220         bool restart;
221         pkgmgrinfo_app_hwacceleration hwacc;
222         pkgmgrinfo_permission_type permission;
223         bool indicator_display;
224         int ret = -1;
225         bool taskmanage;
226         bool preload;
227 #ifdef _APPFW_FEATURE_PROCESS_POOL
228         bool process_pool = 0;
229 #endif
230         int support_mode = 0;
231
232 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
233         char *tep_name = NULL;
234         pkgmgrinfo_installed_storage installed_storage;
235 #endif
236         bool allowed_bg = false;
237 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
238         char *effective_appid = NULL;
239 #endif
240         char *api_ver = NULL;
241         char *mode = NULL;
242
243         if (!handle) {
244                 _E("null app handle");
245                 return -1;
246         }
247
248         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
249         if (ret < 0) {
250                 _E("fail to get appinfo");
251                 return -1;
252         }
253
254         g_hash_table_remove(cf->tbl, appid);
255
256         c = calloc(1, sizeof(*c));
257         if (!c) {
258                 _E("create appinfo: %s", strerror(errno));
259                 return -1;
260         }
261
262         memset(c, 0, sizeof(struct appinfo));
263
264         c->val[_AI_FILE] = strdup(appid);
265         if (!c->val[_AI_FILE]) {
266                 _E("create appinfo: %s", strerror(errno));
267                 _free_appinfo(c);
268                 return -1;
269         }
270
271         c->val[_AI_NAME] = strdup(appid);
272
273         r = pkgmgrinfo_appinfo_is_multiple(handle, &multiple);
274         if(multiple == true)
275                 c->val[_AI_MULTI] = strdup("true");
276         else c->val[_AI_MULTI] = strdup("false");
277
278         r = pkgmgrinfo_appinfo_is_taskmanage(handle, &taskmanage);
279         if (taskmanage == false) {
280                 c->val[_AI_TASKMANAGE] = strdup("false");
281         } else {
282                 c->val[_AI_TASKMANAGE] = strdup("true");
283         }
284
285         r = pkgmgrinfo_appinfo_is_preload(handle, &preload);
286         if (preload == false) {
287                 c->val[_AI_PRELOAD] = strdup("false");
288         } else {
289                 c->val[_AI_PRELOAD] = strdup("true");
290         }
291
292         if(gles == 0) {
293                 c->val[_AI_HWACC] = strdup("NOT_USE");
294         } else {
295                 r = pkgmgrinfo_appinfo_get_hwacceleration(handle, &hwacc);
296                 if (r < 0){
297                         _E("ERROR IN FETCHING HWACCELERATION INFO\n");
298                         c->val[_AI_HWACC] = NULL;
299                 } else {
300                         if (hwacc == PMINFO_HWACCELERATION_ON) {
301                                 c->val[_AI_HWACC] = strdup("USE");
302                         } else if (hwacc == PMINFO_HWACCELERATION_OFF) {
303                                 c->val[_AI_HWACC] = strdup("NOT_USE");
304                         } else {
305                                 c->val[_AI_HWACC] = strdup("SYS");
306                         }
307                 }
308         }
309
310         r = pkgmgrinfo_appinfo_is_indicator_display_allowed(handle, &indicator_display);
311         if (r <0 ){
312                 _E("ERROR IN FETCHING INDICATOR DISP INFO\n");
313                 c->val[_AI_INDICATORDISP] = strdup("true");
314         }else{
315                 if (indicator_display == true){
316                         c->val[_AI_INDICATORDISP] = strdup("true");
317                 }else{
318                         c->val[_AI_INDICATORDISP] = strdup("false");
319                 }
320         }
321
322         r = pkgmgrinfo_appinfo_get_effectimage(handle, &portraitimg, &landscapeimg);
323         if (r < 0){
324                 _E("ERROR IN FETCHING EFFECT IMAGES\n");
325                 c->val[_AI_EFFECTIMAGEPORT] = NULL;
326                 c->val[_AI_EFFECTIMAGELAND] = NULL;
327         }else{
328                 if (portraitimg)
329                         c->val[_AI_EFFECTIMAGEPORT] = strdup(portraitimg);
330                 else
331                         c->val[_AI_EFFECTIMAGEPORT] = NULL;
332                 if (landscapeimg)
333                         c->val[_AI_EFFECTIMAGELAND] = strdup(landscapeimg);
334                 else
335                         c->val[_AI_EFFECTIMAGELAND] = NULL;
336         }
337 #ifdef _APPFW_FEATURE_CHANGEABLE_COLOR
338         r = pkgmgrinfo_appinfo_get_effectimage_type(handle, &effectimg_type);
339         c->val[_AI_EFFECTTYPE] = strdup(effectimg_type);
340 #endif
341
342 #ifdef _APPFW_FEATURE_PROCESS_POOL
343         r = pkgmgrinfo_appinfo_is_process_pool(handle, &process_pool);
344         if (process_pool == false) {
345                 c->val[_AI_POOL] = strdup("false");
346         } else {
347                 c->val[_AI_POOL] = strdup("true");
348         }
349 #endif
350         r = pkgmgrinfo_appinfo_get_component_type(handle, &component_type);
351         c->val[_AI_COMPTYPE] = strdup(component_type);
352
353         r = pkgmgrinfo_appinfo_is_onboot(handle, &onboot);
354         if(onboot == true)
355                 c->val[_AI_ONBOOT] = strdup("true");
356         else c->val[_AI_ONBOOT] = strdup("false");
357
358         r = pkgmgrinfo_appinfo_is_autorestart(handle, &restart);
359         if(restart == true)
360                 c->val[_AI_RESTART] = strdup("true");
361         else c->val[_AI_RESTART] = strdup("false");
362
363 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
364         r = pkgmgrinfo_appinfo_is_multi_instance(handle, &multi_instance);
365         if(multi_instance == true)
366                 c->val[_AI_MULTI_INSTANCE] = strdup("true");
367         else
368                 c->val[_AI_MULTI_INSTANCE] = strdup("false");
369
370         r = pkgmgrinfo_appinfo_get_multi_instance_mainid(handle, &multi_mainid);
371         c->val[_AI_MULTI_INSTANCE_MAINID] = strdup(multi_mainid);
372
373         // Toggle order
374         c->val[_AI_TOGGLE_ORDER] = strdup("0");
375 #endif
376
377         r = pkgmgrinfo_appinfo_get_exec(handle, &exec);
378         c->val[_AI_EXEC] = strdup(exec);
379
380         r = pkgmgrinfo_appinfo_get_apptype(handle, &type);
381         if(strncmp(type, "capp", 4) == 0 ) {
382                 c->val[_AI_TYPE] = strdup("rpm");
383         } else if (strncmp(type, "c++app", 6) == 0 || strncmp(type, "ospapp", 6) == 0) {
384                 c->val[_AI_TYPE] = strdup("tpk");
385         } else if (strncmp(type, "webapp", 6) == 0) {
386                 c->val[_AI_TYPE] = strdup("wgt");
387         }
388
389         r = pkgmgrinfo_appinfo_get_permission_type(handle, &permission);
390         if (permission == PMINFO_PERMISSION_SIGNATURE) {
391                 c->val[_AI_PERM] = strdup("signature");
392         } else if (permission == PMINFO_PERMISSION_PRIVILEGE) {
393                 c->val[_AI_PERM] = strdup("privilege");
394         } else {
395                 c->val[_AI_PERM] = strdup("normal");
396         }
397
398         pkgmgrinfo_appinfo_get_support_mode(handle, &support_mode);
399 #ifdef _APPFW_FEATURE_TTS_MODE
400         if(support_mode & PMINFO_MODE_PROP_SCREEN_READER) {
401                 c->val[_AI_TTS] = strdup("true");
402         } else {
403                 c->val[_AI_TTS] = strdup("false");
404         }
405 #endif
406 #ifdef _APPFW_FEATURE_ULTRA_POWER_SAVING_MODE
407         if(support_mode & PMINFO_MODE_PROP_ULTRA_POWER_SAVING) {
408                 c->val[_AI_UPS] = strdup("true");
409         } else {
410                 c->val[_AI_UPS] = strdup("false");
411         }
412 #endif
413 #ifdef _APPFW_FEATURE_COOLDOWN_MODE_SUPPORT
414         if(support_mode & PMINFO_SUPPORT_MODE_COOL_DOWN) {
415                 c->val[_AI_COOLDOWN] = strdup("true");
416         } else {
417                 c->val[_AI_COOLDOWN] = strdup("false");
418         }
419 #endif
420
421         r = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
422         c->val[_AI_PKGID] = strdup(pkgid);
423
424         c->val[_AI_STATUS] = strdup("installed");
425
426 #ifdef _APPFW_FEATURE_EXPANSION_PKG_INSTALL
427
428         ret = pkgmgrinfo_appinfo_get_tep_name(handle, &tep_name);
429         if(ret == PMINFO_R_OK) {
430                 c->val[_AI_TEP] = strdup(tep_name);
431         }
432
433         ret = pkgmgrinfo_appinfo_get_installed_storage_location(handle, &installed_storage);
434         if(ret == PMINFO_R_OK) {
435                 if(installed_storage == PMINFO_INTERNAL_STORAGE)
436                 {
437                         c->val[_AI_STORAGE_TYPE] = strdup("internal");
438                 }
439                 else if(installed_storage == PMINFO_EXTERNAL_STORAGE)
440                 {
441                         c->val[_AI_STORAGE_TYPE] = strdup("external");
442                 }
443         }
444 #endif
445
446 #ifdef _APPFW_FEATURE_EFFECTIVE_APPID
447         ret = pkgmgrinfo_appinfo_get_effective_appid(handle, &effective_appid);
448         if (ret == PMINFO_R_OK) {
449                 if (effective_appid && strlen(effective_appid) > 0)
450                         c->val[_AI_EFFECTIVE_APPID] = strdup(effective_appid);
451         }
452 #endif
453
454         ret = pkgmgrinfo_appinfo_get_api_version(handle, &api_ver);
455         if (ret != PMINFO_R_OK) {
456                 _E("Failed to get api version");
457                 _free_appinfo(c);
458                 return -1;
459         }
460
461         if (api_ver)
462                 c->val[_AI_API_VER] = strdup(api_ver);
463         else
464                 c->val[_AI_API_VER] = strdup("NONE");
465
466         ret = pkgmgrinfo_appinfo_get_launch_mode(handle, &mode);
467         if (ret == PMINFO_R_OK && mode)
468                 c->val[_AI_LAUNCH_MODE] = strdup(mode);
469         else
470                 c->val[_AI_LAUNCH_MODE] = strdup("single");
471
472         /* TODO each background category may have different background management policies,
473          *      so should have real background category value than allowed_background.  */
474         pkgmgrinfo_appinfo_foreach_background_category(handle, __set_allowed_bg, &allowed_bg);
475         if (ret != PMINFO_R_OK) {
476                 _E("Failed to check allowed background");
477         }
478
479         if (allowed_bg) {
480                 SECURE_LOGD("[__SUSPEND__] allowed background, appid: %s", appid);
481                 c->val[_AI_ALLOWED_BG] = strdup("ALLOWED_BG");
482         } else {
483                 c->val[_AI_ALLOWED_BG] = strdup("NONE");
484         }
485 #ifdef _APPFW_FEATURE_PRIVATE_SERVICE
486         c->val[_AI_VISIBILITY] = NULL;
487 #endif
488         SECURE_LOGD("appinfo file:%s, type:%s", c->val[_AI_FILE], c->val[_AI_TYPE]);
489
490         g_hash_table_insert(cf->tbl, c->val[_AI_FILE], c);
491
492         return 0;
493 }
494
495 static int __app_info_delete_handler(const pkgmgrinfo_appinfo_h handle, void *data)
496 {
497         struct appinfomgr *cf = (struct appinfomgr *)data;
498         char *appid = NULL;
499
500         pkgmgrinfo_appinfo_get_appid(handle, &appid);
501
502         if (appid)
503                 g_hash_table_remove(cf->tbl, appid);
504
505         return 0;
506 }
507
508 static int _read_pkg_info(struct appinfomgr *cf)
509 {
510         int ret;
511
512         ret = pkgmgrinfo_appinfo_get_install_list(__app_info_insert_handler, cf);
513         assert(ret == PMINFO_R_OK);
514
515         return ret;
516 }
517
518 static struct appinfomgr *_init()
519 {
520         struct appinfomgr *cf;
521
522         cf = calloc(1, sizeof(*cf));
523         if (!cf) {
524                 _E("appinfo init: %s", strerror(errno));
525                 return NULL;
526         }
527
528         cf->tbl = g_hash_table_new_full(g_str_hash, g_str_equal,
529                         NULL, _free_appinfo);
530
531         pkg_tbl = g_hash_table_new(g_str_hash, g_str_equal);
532         _E("pkg_tbl : %x", pkg_tbl);
533         return cf;
534 }
535
536 static void __amd_pkgmgrinfo_start_handler (gpointer key, gpointer value, gpointer user_data)
537 {
538         struct appinfo *c = value;
539         char *pkgid = (char *)user_data;
540
541         if (c != NULL && strcmp(c->val[_AI_PKGID], pkgid) == 0) {
542                 free(c->val[_AI_STATUS]);
543                 c->val[_AI_STATUS] = strdup("blocking");
544                 SECURE_LOGD("pkgmgr working for this application(%s)", c->val[_AI_NAME]);
545         }
546 }
547
548 static void __amd_pkgmgrinfo_fail_handler (gpointer key, gpointer value, gpointer user_data)
549 {
550         struct appinfo *c = value;
551         char *pkgid = (char *)user_data;
552
553         if (c != NULL && strcmp(c->val[_AI_PKGID], pkgid) == 0) {
554                 free(c->val[_AI_STATUS]);
555                 c->val[_AI_STATUS] = strdup("installed");
556                 SECURE_LOGD("pkgmgr fail(%s)", c->val[_AI_NAME]);
557         }
558 }
559
560 static int __amd_pkgmgrinfo_install_end_handler(pkgmgrinfo_appinfo_h handle, void *user_data)
561 {
562         char *appid = NULL;
563         struct appinfomgr *cf = (struct appinfomgr *)user_data;
564         struct appinfo *c;
565         const char *componet;
566         int r;
567
568         pkgmgrinfo_appinfo_get_appid(handle, &appid);
569
570         __app_info_insert_handler(handle, user_data);
571         c = g_hash_table_lookup(cf->tbl, appid);
572
573         componet = appinfo_get_value(c, AIT_COMPTYPE);
574         r = appinfo_get_boolean(c, AIT_ONBOOT);
575
576         if (r == 1 && componet && strncmp(componet, "svcapp", 6) == 0)
577         {
578                 SECURE_LOGW("start service - %s", appid);
579                 _start_srv(c);
580         }
581
582         return 0;
583 }
584
585 static int __amd_pkgmgrinfo_update_end_handler(pkgmgrinfo_appinfo_h handle, void *user_data)
586 {
587         char *appid = NULL;
588         struct appinfomgr *cf = (struct appinfomgr *)user_data;
589         struct appinfo *c;
590         const char *componet;
591         int r;
592
593         pkgmgrinfo_appinfo_get_appid(handle, &appid);
594         c = g_hash_table_lookup(cf->tbl, appid);
595
596         if (c != NULL && strncmp(c->val[_AI_STATUS], "restart", 7) == 0) {
597                 __app_info_insert_handler(handle, user_data);
598                 _release_srv(appid);
599         } else {
600                 __app_info_insert_handler(handle, user_data);
601                 c = g_hash_table_lookup(cf->tbl, appid);
602
603                 componet = appinfo_get_value(c, AIT_COMPTYPE);
604                 r = appinfo_get_boolean(c, AIT_ONBOOT);
605
606                 if (r == 1 && componet && strncmp(componet, "svcapp", 6) == 0)
607                 {
608                         SECURE_LOGW("start service - %s", appid);
609                         _start_srv(c);
610                 }
611         }
612
613         return 0;
614 }
615
616 static gboolean __amd_pkgmgrinfo_uninstall_end_handler (gpointer key, gpointer value, gpointer user_data)
617 {
618         struct appinfo *c = value;
619         char *pkgid = (char *)user_data;
620
621         if (strcmp(c->val[_AI_PKGID], pkgid) == 0) {
622                 SECURE_LOGD("appid(%s), pkgid(%s)", c->val[_AI_NAME], pkgid);
623                 return TRUE;
624         }
625         return FALSE;
626 }
627
628 static int __amd_pkgmgrinfo_status_cb(int req_id, const char *pkg_type,
629                        const char *pkgid, const char *key, const char *val,
630                        const void *pmsg, void *user_data)
631 {
632         int ret = 0;
633         pkgmgrinfo_pkginfo_h handle;
634         char *op = NULL;
635         struct pkginfo *p = NULL;
636         struct appinfomgr *cf = (struct appinfomgr *)user_data;
637
638         SECURE_LOGD("pkgid(%s), key(%s), value(%s)", pkgid, key, val);
639
640         if(strncmp(key,"start", 5) == 0) {
641                 g_hash_table_remove(pkg_tbl, pkgid);
642                 p = calloc(1, sizeof(*p));
643                 if (p == NULL) {
644                         _E("out of memory");
645                         return -1;
646                 }
647
648                 p->pkgid = strdup(pkgid);
649                 p->op = strdup(val);
650                 g_hash_table_insert(pkg_tbl, p->pkgid, p);
651                 if (strncmp(val, "install", 7) == 0) {
652
653                 }
654                 else if ((strncmp(val, "update", 6) == 0) || (strncmp(val, "uninstall", 9) == 0)) {
655                         g_hash_table_foreach(cf->tbl, __amd_pkgmgrinfo_start_handler, (gpointer)pkgid);
656                         _D("__amd_pkgmgrinfo_start_handler");
657                         ret = 0;
658                 }
659         }
660         else if (strncmp(key,"end", 3) == 0) {
661                 p = g_hash_table_lookup(pkg_tbl, pkgid);
662                 if(p) {
663                         op = p->op;
664                         SECURE_LOGW("op(%s), value(%s)", op, val);
665                         if (strncmp(val, "ok", 2) == 0) {
666                                 if (op && strncmp(op, "install", 7)== 0) {
667                                         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
668                                         if (ret != PMINFO_R_OK)
669                                                 return -1;
670                                         ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __amd_pkgmgrinfo_install_end_handler, user_data);
671                                         if (ret != PMINFO_R_OK) {
672                                                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
673                                                 return -1;
674                                         }
675                                         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
676                                 }
677                                 else if (op && strncmp(op, "update", 6) == 0){
678                                         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
679                                         if (ret != PMINFO_R_OK)
680                                                 return -1;
681                                         ret = pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __amd_pkgmgrinfo_update_end_handler, user_data);
682                                         if (ret != PMINFO_R_OK) {
683                                                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
684                                                 return -1;
685                                         }
686                                         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
687                                 }
688                                 else if (op && strncmp(op, "uninstall", 9) == 0) {
689                                         ret = g_hash_table_foreach_remove(cf->tbl, __amd_pkgmgrinfo_uninstall_end_handler, (gpointer)pkgid);
690                                         _D("g_hash_table_foreach_remove, %d", ret);
691                                         ret = 0;
692                                 }
693                         }
694                         else if (strncmp(val, "fail", 4) == 0) {
695                                 if ((op && strncmp(op, "update", 6) == 0) || (op && strncmp(op, "uninstall", 9) ==  0) ) {
696                                         g_hash_table_foreach(cf->tbl, __amd_pkgmgrinfo_fail_handler, (gpointer)pkgid);
697                                         _D("__amd_pkgmgrinfo_fail_handler");
698                                         ret = 0;
699                                 }
700                         }
701                         g_hash_table_remove(pkg_tbl, p->pkgid);
702                         free(p->pkgid);
703                         free(p->op);
704                         free(p);
705                 }
706         }
707         return ret;
708 }
709
710 static int __unmounted_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
711 {
712         char *appid = NULL;
713         struct appinfomgr *cf = (struct appinfomgr *)user_data;
714         struct appinfo *c = NULL;
715
716         pkgmgrinfo_appinfo_get_appid(handle, &appid);
717         if (appid) {
718                 c = g_hash_table_lookup(cf->tbl, appid);
719                 SECURE_LOGD("%s : %s", c->val[_AI_FILE], c->val[_AI_STATUS]);
720                 free(c->val[_AI_STATUS]);
721                 c->val[_AI_STATUS] = strdup("unmounted");
722                 SECURE_LOGD("unmounted(%s)", appid);
723         } else {
724                 _E("pkgmgrinfo_appinfo_get_appid() failed");
725         }
726
727         return 0;
728 }
729
730 static int __mounted_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
731 {
732         char *appid = NULL;
733         struct appinfomgr *cf = (struct appinfomgr *)user_data;
734         struct appinfo *c = NULL;
735
736         pkgmgrinfo_appinfo_get_appid(handle, &appid);
737         if (appid) {
738                 c = g_hash_table_lookup(cf->tbl, appid);
739                 SECURE_LOGD("%s : %s", c->val[_AI_FILE], c->val[_AI_STATUS]);
740                 if(strncmp(c->val[_AI_STATUS], "unmounted", 9) ==0 ) {
741                         free(c->val[_AI_STATUS]);
742                         c->val[_AI_STATUS] = strdup("installed");
743                 }
744                 SECURE_LOGD("mounted(%s)", appid);
745         } else {
746                 _E("pkgmgrinfo_appinfo_get_appid() failed");
747         }
748
749         return 0;
750 }
751
752 #ifdef _APPFW_FEATURE_MMC_SUPPORT
753 static void __amd_mmc_vconf_cb(keynode_t *key, void *data)
754 {
755         int status;
756         int ret = 0;
757
758         status = vconf_keynode_get_int(key);
759         if( status < 0 ) {
760                 return;
761         }
762
763         if(status == VCONFKEY_SYSMAN_MMC_REMOVED || status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
764                 ret = pkgmgrinfo_appinfo_get_unmounted_list(__unmounted_list_cb, data);
765                 if (ret != PMINFO_R_OK){
766                         _E("pkgmgrinfo_appinfo_get_unmounted_list failed: %d", ret);
767                 }
768         } else if(status == VCONFKEY_SYSMAN_MMC_MOUNTED){
769                 ret = pkgmgrinfo_appinfo_get_mounted_list(__mounted_list_cb, data);
770                 if (ret != PMINFO_R_OK){
771                         _E("pkgmgrinfo_appinfo_get_mounted_list failed: %d", ret);
772                 }
773         }
774 }
775 #endif
776
777 int appinfo_init(struct appinfomgr **cf)
778 {
779         struct appinfomgr *_cf;
780         int r;
781         char *cmdline;
782         FILE *fp = NULL;
783         char buf[4096] = {0,};
784         char *tmp = NULL;
785
786         if (!cf) {
787                 errno = EINVAL;
788                 _E("appinfo init: %s", strerror(errno));
789                 return -1;
790         }
791
792         fp = fopen("/proc/cmdline", "r");
793         if (fp == NULL){
794                 _E("appinfo init failed: %s", strerror(errno));
795                 return -1;
796         }
797         cmdline = fgets(buf, sizeof(buf), fp);
798         if (cmdline != NULL) {
799                 tmp = strstr(buf, "gles");
800                 if(tmp != NULL) {
801                         sscanf(tmp,"gles=%d", &gles);
802                 }
803         }
804         fclose(fp);
805
806         _cf = _init();
807         if (!_cf) {
808                 assert(_cf);
809                 return -1;
810         }
811
812         r = _read_pkg_info(_cf);
813         if (r != PMINFO_R_OK) {
814                 _fini(_cf);
815                 return -1;
816         }
817
818 #ifdef _APPFW_FEATURE_MMC_SUPPORT
819         r = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, __amd_mmc_vconf_cb, _cf);
820         if (r < 0)
821                 _E("Unable to register vconf notification callback for VCONFKEY_SYSMAN_MMC_STATUS\n");
822 #endif
823
824         int event_type = PMINFO_CLIENT_STATUS_UPGRADE | PMINFO_CLIENT_STATUS_UNINSTALL | PMINFO_CLIENT_STATUS_INSTALL;
825         pkgmgrinfo_client *pc = NULL;
826         pc = pkgmgrinfo_client_new(PMINFO_LISTENING);
827         pkgmgrinfo_client_set_status_type(pc, event_type);
828         pkgmgrinfo_client_listen_status(pc, __amd_pkgmgrinfo_status_cb , _cf);
829
830         *cf = _cf;
831
832         return 0;
833 }
834
835 void appinfo_fini(struct appinfomgr **cf)
836 {
837         if (!cf || !*cf)
838                 return;
839
840         _fini(*cf);
841         *cf = NULL;
842 }
843
844 const struct appinfomgr *appinfo_insert(struct appinfomgr *cf, const char *pkg_name)
845 {
846         pkgmgrinfo_pkginfo_h handle;
847         if (pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle) == PMINFO_R_OK){
848                 pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_insert_handler, cf);
849                 pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_insert_handler, cf);
850                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
851         }
852         return cf;
853 }
854
855 void appinfo_delete(struct appinfomgr *cf, const char *pkg_name)
856 {
857         pkgmgrinfo_pkginfo_h handle;
858         if (pkgmgrinfo_pkginfo_get_pkginfo(pkg_name, &handle) != PMINFO_R_OK)
859                 return;
860         pkgmgrinfo_appinfo_get_list(handle, PMINFO_SVC_APP, __app_info_delete_handler, cf);
861         pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __app_info_delete_handler, cf);
862         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
863 }
864
865 const struct appinfo *appinfo_find(struct appinfomgr *cf, const char *filename)
866 {
867         if (!cf || !filename || !*filename) {
868                 errno = EINVAL;
869                 return NULL;
870         }
871
872         return g_hash_table_lookup(cf->tbl, FILENAME(filename));
873 }
874
875 const char *appinfo_get_value(const struct appinfo *c, enum appinfo_type type)
876 {
877         enum _appinfo_idx i;
878
879         if (!c) {
880                 errno = EINVAL;
881                 _E("appinfo get value: %s, %d", strerror(errno), type);
882                 return NULL;
883         }
884
885         for (i = _AI_START; i < sizeof(_appinfos)/sizeof(_appinfos[0]); i++) {
886                 if (type == _appinfos[i].type)
887                         return c->val[i];
888         }
889
890         errno = ENOENT;
891         _E("appinfo get value: %s", strerror(errno));
892
893         return NULL;
894 }
895
896 const char *appinfo_set_value(struct appinfo *c, enum appinfo_type type, const char* val)
897 {
898         enum _appinfo_idx i;
899         if (!c) {
900                 SECURE_LOGE("appinfo is NULL, type: %d, val: %s", type, val);
901                 return NULL;
902         }
903         for (i = _AI_START; i < sizeof(_appinfos)/sizeof(_appinfos[0]); i++) {
904                 if (type == _appinfos[i].type) {
905                         SECURE_LOGD("%s : %s : %s", c->val[_AI_FILE], c->val[i], val);
906                         free(c->val[i]);
907                         c->val[i] = strdup(val);
908                 }
909         }
910
911         return NULL;
912 }
913
914 const char *appinfo_get_filename(const struct appinfo *c)
915 {
916         if (!c) {
917                 errno = EINVAL;
918                 SECURE_LOGE("appinfo get filename: %s", strerror(errno));
919                 return NULL;
920         }
921
922         return c->val[_AI_FILE];
923 }
924
925 struct _cbinfo {
926         appinfo_iter_callback cb;
927         void *cb_data;
928 };
929
930 static void _iter_cb(gpointer key, gpointer value, gpointer user_data)
931 {
932         struct _cbinfo *cbi = user_data;
933
934         assert(cbi);
935
936         cbi->cb(cbi->cb_data, key, value);
937 }
938
939 void appinfo_foreach(struct appinfomgr *cf, appinfo_iter_callback cb, void *user_data)
940 {
941         struct _cbinfo cbi;
942
943         if (!cf || !cb) {
944                 errno = EINVAL;
945                 _E("appinfo foreach: %s", strerror(errno));
946                 return;
947         }
948
949         cbi.cb = cb;
950         cbi.cb_data = user_data;
951
952         g_hash_table_foreach(cf->tbl, _iter_cb, &cbi);
953 }
954
955 int appinfo_get_boolean(const struct appinfo *c, enum appinfo_type type)
956 {
957         const char *v;
958
959         v = appinfo_get_value(c, type);
960         if (!v)
961                 return -1;
962
963         if (!strcmp(v, "1") || !strcasecmp(v, "true"))
964                 return 1;
965
966         if (!strcmp(v, "0") || !strcasecmp(v, "false"))
967                 return 0;
968
969         errno = EFAULT;
970
971         return -1;
972 }
973