2 * Copyright 2015 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "widget_instance.h"
20 #include <uuid/uuid.h>
27 #include <sys/types.h>
31 #include <aul_app_com.h>
32 #include <aul_widget.h>
33 #include <aul_debug_info.h>
34 #include <widget_service.h>
35 #include <widget_service_internal.h>
36 #include <bundle_internal.h>
37 #include "widget_errno.h"
39 #define USER_UID_MIN 5000
40 #define MAX_INSTANCE_ID_LEN 256
41 #define WIDGET_CLASS_DELIMITER "@"
42 #define QUERY_MAX_LEN 8192
44 #define DLP_K_DEBUG_ARG "__DLP_DEBUG_ARG__"
45 #define DLP_K_GDBSERVER_PATH "__DLP_GDBSERVER_PATH__"
46 #define DLP_K_VALGRIND_ARG "__DLP_VALGRIND_ARG__"
47 #define DLP_K_VALGRIND_PATH "__DLP_VALGRIND_PATH__"
48 #define SDK_DEBUG "DEBUG"
49 #define SDK_VALGRIND "VALGRIND"
51 #ifndef TIZEN_PATH_MAX
52 #define TIZEN_PATH_MAX 1024
56 WIDGET_INSTANCE_CREATED,
57 WIDGET_INSTANCE_RUNNING,
58 WIDGET_INSTANCE_TERMINATED,
59 WIDGET_INSTANCE_DELETED
62 struct _widget_instance {
87 static GList *_widget_instances = NULL;
88 static GList *_widget_apps = NULL;
90 static char *viewer_appid = NULL;
91 static aul_app_com_connection_h conn_viewer;
92 static aul_app_com_connection_h conn_status;
94 static GHashTable *lifecycle_tbl;
95 static int is_status_handler_connected;
97 static struct _sdk_util sdk_util;
99 static char *package_id;
101 struct lifecycle_local_s {
103 widget_instance_event_cb cb;
107 static GList *event_cbs;
109 widget_instance_event_cb cb;
113 static int __fault_handler(int pid);
115 extern int widget_service_get_update_period(const char *widget_id, double *period);
117 static struct _widget_instance *__pick_instance(const char *instance_id)
119 GList *instances = _widget_instances;
120 struct _widget_instance *instance = NULL;
123 instance = (struct _widget_instance *)instances->data;
124 if (instance && g_strcmp0(instance_id, instance->id) == 0)
127 instances = instances->next;
133 static struct widget_app *__pick_app(const char *widget_id)
135 GList *apps = _widget_apps;
136 struct widget_app *app = NULL;
140 if (app && app->widget_id && g_strcmp0(widget_id, app->widget_id) == 0)
149 static struct widget_app *__add_app(const char *widget_id, const char *viewer_id)
151 struct widget_app *app = NULL;
153 app = (struct widget_app *)malloc(sizeof(struct widget_app));
159 app->viewer_id = strdup(viewer_id);
160 app->widget_id = strdup(widget_id);
161 app->instances = NULL;
163 _widget_apps = g_list_append(_widget_apps, app);
168 static char *__create_instance_id(const char *widget_id)
171 char instance_id[MAX_INSTANCE_ID_LEN];
173 struct widget_app *app = NULL;
175 if (widget_id == NULL)
178 app = __pick_app(widget_id);
181 app = __add_app(widget_id, viewer_appid);
187 uuid_unparse(u, uuid);
189 snprintf(instance_id, MAX_INSTANCE_ID_LEN, "%s:%s", uuid, widget_id);
191 _D("new instance: %s", instance_id);
193 return strdup(instance_id);
196 static struct _widget_instance *__add_instance(const char *id, const char *widget_id)
198 struct _widget_instance *instance = NULL;
199 struct widget_app *app = NULL;
202 if (widget_service_get_update_period(widget_id, &period) != WIDGET_ERROR_NONE) {
203 _E("Can't get update-period");
207 instance = (struct _widget_instance *)malloc(sizeof(struct _widget_instance));
208 if (instance == NULL) {
213 instance->status = WIDGET_INSTANCE_CREATED;
214 instance->id = strdup(id);
216 instance->stored = 0;
217 instance->widget_id = strdup(widget_id);
218 instance->content_info = NULL;
220 instance->period = period;
223 _widget_instances = g_list_append(_widget_instances, instance);
225 app = __pick_app(widget_id);
227 app = __add_app(widget_id, viewer_appid);
230 app->instances = g_list_append(app->instances, instance);
232 _E("failed to find app: %s", widget_id);
237 static void __remove_instance(struct _widget_instance *instance)
239 struct widget_app *app = NULL;
241 if (instance == NULL) {
242 _E("invalid argument");
246 if (instance->widget_id)
247 app = __pick_app(instance->widget_id);
250 app->instances = g_list_remove(app->instances, instance);
251 if (app->instances == NULL) {
252 _widget_apps = g_list_remove(_widget_apps, app);
254 free(app->widget_id);
256 free(app->viewer_id);
261 _widget_instances = g_list_remove(_widget_instances, instance);
263 if (instance->widget_id) {
264 free(instance->widget_id);
265 instance->widget_id = NULL;
273 if (instance->content_info) {
274 free(instance->content_info);
275 instance->content_info = NULL;
281 EAPI int widget_instance_create(const char *widget_id, char **instance_id)
284 struct _widget_instance *instance = NULL;
286 _D("create: %s", widget_id);
288 id = __create_instance_id(widget_id);
290 _E("failed to get instance id for %s", widget_id);
294 instance = __add_instance(id, widget_id);
299 *instance_id = instance->id;
309 static int __convert_aul_error(int res)
314 case AUL_R_EREJECTED:
316 ret = WIDGET_ERROR_NOT_EXIST;
319 ret = WIDGET_ERROR_PERMISSION_DENIED;
322 ret = WIDGET_ERROR_TIMED_OUT;
324 case AUL_R_ECANCELED:
325 ret = WIDGET_ERROR_CANCELED;
328 ret = WIDGET_ERROR_INVALID_PARAMETER;
331 ret = WIDGET_ERROR_FAULT;
338 static int __launch(const char *widget_id, const char *instance_id, bundle *extra)
342 const char *classid = widget_id;
345 appid = g_strstr_len(widget_id, strlen(widget_id), WIDGET_CLASS_DELIMITER) + 1;
346 if (appid != (const char *)1) { /* move pointer next to delimiter */
347 if (appid > widget_id + (sizeof(char) * strlen(widget_id)))
348 return -1; /* bad ptr */
357 bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
359 bundle_add_str(b, WIDGET_K_CLASS, classid);
360 bundle_add_str(b, AUL_K_WIDGET_VIEWER, viewer_appid);
362 aul_svc_set_operation(b, AUL_SVC_OPERATION_LAUNCH_WIDGET);
364 ret = aul_launch_app_async(appid, b);
366 ret = __convert_aul_error(ret);
376 static int __send_aul_cmd(widget_instance_h instance, const char *command, bundle *extra)
381 if (!instance || !command) {
382 _E("inavlid parameter");
394 bundle_del(b, WIDGET_K_OPERATION);
395 bundle_add_str(b, WIDGET_K_OPERATION, command);
397 ret = __launch(instance->widget_id, instance->id, b);
405 _E("failed to launch %s (%s:%d)", instance->id, command, ret);
407 } else if (ret != instance->pid) {
408 _E("pid has been changed. new process detected. %s (%d to %d)",
409 instance->id, instance->pid, ret);
416 static int __set_width(bundle *content_info, int w)
419 if (content_info == NULL || w < 0)
422 snprintf(wbuf, 11, "%d", w);
423 bundle_add_str(content_info, WIDGET_K_WIDTH, wbuf);
428 static int __set_height(bundle *content_info, int h)
431 if (content_info == NULL || h < 0)
434 snprintf(hbuf, 11, "%d", h);
435 bundle_add_str(content_info, WIDGET_K_HEIGHT, hbuf);
440 EAPI int widget_instance_launch(const char *instance_id, char *content_info, int w, int h)
445 widget_instance_h instance;
447 _D("launch: %s", instance_id);
449 if (instance_id == NULL) {
450 _E("wrong arguments");
454 instance = __pick_instance(instance_id);
456 if (instance == NULL) {
457 _E("unknown instance: %s", instance_id);
472 snprintf(pid_buf, sizeof(pid_buf), "%d", getpid());
473 bundle_add_str(b, WIDGET_K_CALLER, pid_buf);
474 bundle_add_str(b, WIDGET_K_OPERATION, "create");
475 bundle_add_byte(b, WIDGET_K_PERIOD, &(instance->period), sizeof(double));
478 bundle_add_str(b, WIDGET_K_CONTENT_INFO, content_info);
479 instance->content_info = strdup(content_info);
485 ret = __launch(instance->widget_id, instance_id, b);
486 aul_widget_write_log(LOG_TAG, "[%s:%d] %s, launched : %d",
487 __FUNCTION__, __LINE__, instance_id, ret);
497 EAPI int widget_instance_terminate(const char *instance_id)
500 struct _widget_instance *instance;
502 if (instance_id == NULL)
505 instance = __pick_instance(instance_id);
507 _E("illegal operation: termiante (instance not yet initialized: %s)", instance_id);
511 ret = __send_aul_cmd(instance, "terminate", NULL);
516 EAPI int widget_instance_terminate_app(const char *instance_id)
519 struct _widget_instance *instance;
521 if (instance_id == NULL)
524 instance = __pick_instance(instance_id);
526 _E("illegal operation: termiante app (instance not yet initialized: %s)", instance_id);
530 if (instance->pid <= 0) {
531 _E("Invalid pid (%d)", instance->pid);
535 ret = aul_terminate_pid(instance->pid);
537 _E("Fail to terminate (%d)", instance->pid);
544 EAPI int widget_instance_destroy(const char *instance_id)
547 struct _widget_instance *instance;
549 if (instance_id == NULL)
552 instance = __pick_instance(instance_id);
554 _E("illegal operation: destroy (instance not yet initialized: %s)", instance_id);
558 if (instance->pid > 0) {
559 ret = __send_aul_cmd(instance, "destroy", NULL);
560 } else { /* uninitialized */
561 __remove_instance(instance);
567 EAPI int widget_instance_resume(const char *instance_id)
570 struct _widget_instance *instance;
572 if (instance_id == NULL)
575 instance = __pick_instance(instance_id);
577 _E("illegal operation: resume (instance not yet initialized: %s)", instance_id);
581 ret = __send_aul_cmd(instance, "resume", NULL);
586 EAPI int widget_instance_pause(const char *instance_id)
589 struct _widget_instance *instance;
591 if (instance_id == NULL)
594 instance = __pick_instance(instance_id);
596 _E("illegal operation: pause (instance not yet initialized: %s)", instance_id);
600 ret = __send_aul_cmd(instance, "pause", NULL);
605 EAPI int widget_instance_resize(const char *instance_id, int w, int h)
609 struct _widget_instance *instance;
611 if (instance_id == NULL)
614 instance = __pick_instance(instance_id);
616 _E("illegal operation: resize (instance not yet initialized: %s)", instance_id);
627 ret = __send_aul_cmd(instance, "resize", b);
634 EAPI int widget_instance_foreach(const char *widget_id, widget_instance_foreach_cb cb, void *data)
636 GList *apps = _widget_apps;
637 GList *instances = NULL;
638 struct widget_app *app = NULL;
639 struct _widget_instance *instance = NULL;
641 if (widget_id == NULL || cb == NULL)
646 if (app && g_strcmp0(app->widget_id, widget_id) == 0) {
647 instances = app->instances;
649 instance = instances->data;
650 if (instance && cb(instance, data) < 0)
653 instances = instances->next;
662 static void __notify_event(int event, const char *widget_id, const char *instance_id)
664 struct event_cb_s *cb_info;
665 GList *iter = event_cbs;
668 cb_info = (struct event_cb_s *)iter->data;
669 if (cb_info && cb_info->cb)
670 cb_info->cb(widget_id, instance_id, event, cb_info->data);
676 static int __check_valid_sender(char *widget_id, int pid)
678 struct widget_app *app = NULL;
679 widget_instance_h instance;
682 app = __pick_app(widget_id);
684 head = app->instances;
686 instance = (widget_instance_h)head->data;
687 if (instance->pid == -1 || instance->pid == pid)
689 _E("instance->pid (%d), pid (%d)", instance->pid, pid);
695 static int __check_valid_sender_v2(const char *sender_pkgid)
697 char pkgid[256] = {0, };
700 if (sender_pkgid == NULL)
703 if (package_id == NULL) {
704 ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
705 if (ret != AUL_R_OK) {
706 _E("Failed to get pkgid");
710 package_id = strdup(pkgid);
711 if (package_id == NULL) {
717 _D("sender_pkgid: %s, pkgid: %s", sender_pkgid, package_id);
718 if (strcmp(package_id, sender_pkgid) == 0)
724 static int __status_handler(const char *endpoint, aul_app_com_result_e e, bundle *envelope, void *user_data)
726 char *widget_id = NULL;
727 char *instance_id = NULL;
728 char *sender_pkgid = NULL;
729 char *is_faulted = NULL;
731 size_t status_sz = 0;
732 struct lifecycle_local_s *cb_info;
733 char *sender_pid_str;
736 bundle_get_str(envelope, AUL_K_WIDGET_ID, &widget_id);
737 bundle_get_str(envelope, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
738 bundle_get_byte(envelope, AUL_K_WIDGET_STATUS, (void **)&status, &status_sz);
739 bundle_get_str(envelope, AUL_K_COM_SENDER_PID, &sender_pid_str);
740 bundle_get_str(envelope, AUL_K_PKGID, &sender_pkgid);
741 bundle_get_str(envelope, AUL_K_IS_FAULT, &is_faulted);
743 if (widget_id == NULL || status == NULL) {
744 _E("undefined class or instance %s of %s", instance_id, widget_id);
746 _E("status: %d", *status);
751 sender_pid = atoi(sender_pid_str);
752 if (__check_valid_sender(widget_id, sender_pid) == -1) {
753 bundle_get_str(envelope, AUL_K_PKGID, &sender_pkgid);
754 LOGW("It's not my widget(%s), let's check package", widget_id);
755 if (__check_valid_sender_v2(sender_pkgid) < 0) {
756 LOGW("My package do not have widget_id %s", widget_id);
761 if (*status == AUL_WIDGET_LIFE_CYCLE_EVENT_APP_DEAD &&
762 is_faulted && !strcmp(is_faulted, "true")) {
763 LOGW("handle dead widget(%s) instances", widget_id);
764 aul_widget_write_log(LOG_TAG, "[%s:%d] %s sender(%d)",
765 __FUNCTION__, __LINE__, widget_id, sender_pid);
766 __fault_handler(sender_pid);
769 cb_info = (struct lifecycle_local_s *)g_hash_table_lookup(lifecycle_tbl, "NULL");
771 cb_info->cb(widget_id, instance_id, *status, cb_info->data);
773 cb_info = (struct lifecycle_local_s *)g_hash_table_lookup(lifecycle_tbl, widget_id);
775 cb_info->cb(widget_id, instance_id, *status, cb_info->data);
780 static int __connect_status_handler()
782 if (is_status_handler_connected)
785 if (aul_app_com_create("widget.status", NULL, __status_handler, NULL, &conn_status) < 0) {
786 _E("failed to create status");
790 is_status_handler_connected = 1;
795 static int __widget_instance_handler(int status, char *widget_id,
796 char *instance_id, char *content_info, int err)
799 struct _widget_instance *instance;
801 if (instance_id == NULL) {
802 _E("undefined class or instance of %s", widget_id);
806 _D("update status %s on %d", instance_id, status);
808 instance = __pick_instance(instance_id);
810 if (instance == NULL) {
811 _E("undefined instance id: %s of %s", instance_id, widget_id);
816 case WIDGET_INSTANCE_EVENT_CREATE:
817 instance->status = WIDGET_INSTANCE_RUNNING;
819 case WIDGET_INSTANCE_EVENT_TERMINATE:
820 instance->status = WIDGET_INSTANCE_TERMINATED;
822 case WIDGET_INSTANCE_EVENT_DESTROY:
823 instance->status = WIDGET_INSTANCE_DELETED;
825 case WIDGET_INSTANCE_EVENT_PAUSE:
827 case WIDGET_INSTANCE_EVENT_RESUME:
829 case WIDGET_INSTANCE_EVENT_UPDATE:
831 case WIDGET_INSTANCE_EVENT_EXTRA_UPDATED:
833 if (instance->content_info)
834 free(instance->content_info);
836 instance->content_info = strdup(content_info);
839 case WIDGET_INSTANCE_EVENT_FAULT:
841 case WIDGET_INSTANCE_EVENT_CREATE_ABORTED:
846 _E("unknown status: %d", status);
850 __notify_event(status, widget_id, instance_id);
852 if (instance->status == WIDGET_INSTANCE_DELETED)
853 widget_instance_unref(instance);
858 static int __widget_handler(const char *viewer_id, aul_app_com_result_e e, bundle *envelope, void *user_data)
860 char *widget_id = NULL;
861 char *instance_id = NULL;
862 char *error_code = NULL;
864 size_t status_sz = 0;
865 char *content_info = NULL;
866 char *sender_pid_str = NULL;
871 bundle_get_str(envelope, AUL_K_WIDGET_ID, &widget_id);
872 bundle_get_byte(envelope, AUL_K_WIDGET_STATUS, (void **)&status, &status_sz);
874 if (widget_id == NULL) {
875 LOGE("undefined widget_id");
879 if (*status == WIDGET_INSTANCE_EVENT_APP_RESTART_REQUEST) {
880 LOGW("WIDGET_INSTANCE_EVENT_APP_RESTART_REQUEST notify");
881 bundle_get_str(envelope, AUL_K_COM_SENDER_PID, &sender_pid_str);
882 if (sender_pid_str == NULL) {
883 LOGE("Failed to get sender pid of %s", widget_id);
886 sender_pid = atoi(sender_pid_str);
887 ret = __check_valid_sender(widget_id, sender_pid);
889 __notify_event(*status, widget_id, "");
891 bundle_get_str(envelope, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
892 bundle_get_str(envelope, WIDGET_K_CONTENT_INFO, &content_info);
893 bundle_get_str(envelope, AUL_K_WIDGET_ERROR_CODE, &error_code);
895 err = atoi(error_code);
897 __widget_instance_handler(*status, widget_id, instance_id,
904 /* LCOV_EXCL_START */
905 static int __fault_handler(int pid)
908 struct _widget_instance *instance;
910 iter = _widget_instances;
913 instance = (struct _widget_instance *)iter->data;
914 if (instance && instance->pid == pid) {
916 instance->status = WIDGET_INSTANCE_TERMINATED;
917 __notify_event(WIDGET_INSTANCE_EVENT_FAULT, instance->widget_id, instance->id);
926 EAPI int widget_instance_init(const char *viewer_id)
928 if (viewer_id == NULL)
931 viewer_appid = strdup(viewer_id);
933 __connect_status_handler();
935 _D("widget_instance_init %s", viewer_id);
936 if (aul_app_com_create(viewer_id, NULL, __widget_handler, NULL, &conn_viewer) < 0) {
937 _E("failed to create app com endpoint");
944 EAPI int widget_instance_fini()
946 if (sdk_util.initialized) {
947 aul_debug_info_fini();
948 sdk_util.initialized = false;
952 bundle_free(sdk_util.b);
957 g_hash_table_destroy(lifecycle_tbl);
958 lifecycle_tbl = NULL;
962 if (aul_app_com_leave(conn_viewer) < 0)
963 _E("failed to leave app com endpoint viewer");
968 if (aul_app_com_leave(conn_status) < 0)
969 _E("failed to leave app com endpoint status");
986 EAPI int widget_instance_get_error_code(widget_instance_h instance, int *err)
988 if (instance == NULL || err == NULL)
991 *err = instance->err;
996 EAPI int widget_instance_get_id(widget_instance_h instance, char **id)
998 if (instance == NULL || id == NULL)
1001 *id = (char *)instance->id;
1005 EAPI int widget_instance_get_content(widget_instance_h instance, char **content)
1007 if (instance == NULL || content == NULL)
1010 *content = instance->content_info;
1014 EAPI int widget_instance_get_width(widget_instance_h instance, int *w)
1016 if (instance == NULL || w == NULL)
1023 EAPI int widget_instance_get_height(widget_instance_h instance, int *h)
1025 if (instance == NULL || h == NULL)
1032 EAPI int widget_instance_set_period(widget_instance_h instance, double period)
1034 if (instance == NULL || period < 0)
1037 instance->period = period;
1041 EAPI int widget_instance_get_period(widget_instance_h instance, double *period)
1043 if (instance == NULL || period == NULL)
1046 *period = instance->period;
1050 EAPI int widget_instance_change_period(const char *widget_id, const char *instance_id, double period)
1055 b = bundle_create();
1057 _E("out of memory"); /* LCOV_EXCL_LINE */
1058 return -1; /* LCOV_EXCL_LINE */
1061 bundle_add_byte(b, WIDGET_K_PERIOD, &period, sizeof(double));
1062 bundle_add_str(b, WIDGET_K_OPERATION, "period");
1063 ret = aul_widget_instance_update(widget_id, instance_id, b);
1069 EAPI int widget_instance_trigger_update(widget_instance_h instance, const char *content_info, int force)
1074 return widget_instance_trigger_update_v2(instance->widget_id, instance->id, content_info, force);
1077 EAPI int widget_instance_trigger_update_v2(const char *widget_id,
1078 const char *instance_id, const char *content_info, int force)
1084 kb = bundle_create();
1086 _E("out of memory"); /* LCOV_EXCL_LINE */
1087 return -1; /* LCOV_EXCL_LINE */
1090 bundle_add_str(kb, WIDGET_K_OPERATION, "update");
1093 bundle_add_str(kb, WIDGET_K_FORCE, "true");
1096 bundle_add_str(kb, WIDGET_K_CONTENT_INFO, content_info);
1098 ret = aul_widget_instance_update(widget_id, instance_id, kb);
1103 return WIDGET_ERROR_NONE;
1107 ret = WIDGET_ERROR_NOT_EXIST;
1110 ret = WIDGET_ERROR_PERMISSION_DENIED;
1113 ret = WIDGET_ERROR_INVALID_PARAMETER;
1115 case AUL_R_ECANCELED:
1116 ret = WIDGET_ERROR_CANCELED;
1119 ret = WIDGET_ERROR_FAULT;
1125 EAPI widget_instance_h widget_instance_get_instance(const char *widget_id, const char *instance_id)
1127 widget_instance_h instance;
1129 if (widget_id == NULL || instance_id == NULL)
1132 if (_widget_apps && _widget_instances) {
1133 instance = __pick_instance(instance_id);
1134 return widget_instance_ref(instance);
1140 EAPI int widget_instance_get_instance_list(const char *widget_id, widget_instance_list_cb cb, void *data)
1142 widget_instance_h instance;
1143 struct widget_app *app;
1146 if (widget_id == NULL)
1150 app = __pick_app(widget_id);
1155 head = app->instances;
1158 instance = (widget_instance_h)head->data;
1159 if (cb(instance->widget_id, instance->id, data) != 0)
1169 EAPI void widget_instance_unref(widget_instance_h instance)
1171 if (instance == NULL)
1176 if (instance->ref > -1)
1179 __remove_instance(instance);
1182 EAPI widget_instance_h widget_instance_ref(widget_instance_h instance)
1190 EAPI int widget_instance_listen_event(widget_instance_event_cb cb, void *data)
1192 struct event_cb_s *cb_info;
1194 cb_info = (struct event_cb_s *)malloc(sizeof(struct event_cb_s));
1196 _E("out of memory"); /* LCOV_EXCL_LINE */
1197 return -1; /* LCOV_EXCL_LINE */
1201 cb_info->data = data;
1203 event_cbs = g_list_append(event_cbs, cb_info);
1205 _E("out of memory"); /* LCOV_EXCL_LINE */
1206 return -1; /* LCOV_EXCL_LINE */
1212 EAPI int widget_instance_unlisten_event(widget_instance_event_cb cb)
1214 struct event_cb_s *cb_info;
1215 GList *iter = event_cbs;
1218 _E("invalid parameter");
1223 cb_info = (struct event_cb_s *)iter->data;
1224 if (cb_info && cb_info->cb == cb) {
1225 event_cbs = g_list_remove_link(event_cbs, iter);
1233 _E("wrong argument");
1237 static void __destroy_lifecycle_local(gpointer data)
1239 struct lifecycle_local_s *cb_info = (struct lifecycle_local_s *)data;
1241 if (cb_info == NULL)
1244 if (cb_info->widget_id)
1245 free(cb_info->widget_id);
1249 /* within package only */
1250 EAPI int widget_instance_listen_status(const char *widget_id, widget_instance_event_cb cb, void *data)
1252 struct lifecycle_local_s *cb_info;
1255 widget_id = "NULL"; /* listen all widget */
1257 cb_info = (struct lifecycle_local_s *)malloc(sizeof(struct lifecycle_local_s));
1259 _E("out of memory"); /* LCOV_EXCL_LINE */
1260 return -1; /* LCOV_EXCL_LINE */
1263 cb_info->widget_id = strdup(widget_id);
1265 cb_info->data = data;
1266 if (!lifecycle_tbl) {
1267 lifecycle_tbl = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
1268 __destroy_lifecycle_local);
1269 if (!lifecycle_tbl) {
1270 free(cb_info->widget_id);
1276 g_hash_table_insert(lifecycle_tbl, cb_info->widget_id, cb_info);
1278 __connect_status_handler();
1283 EAPI int widget_instance_unlisten_status(const char *widget_id)
1285 struct lifecycle_local_s *cb_info;
1290 cb_info = (struct lifecycle_local_s *)g_hash_table_lookup(lifecycle_tbl, widget_id);
1294 g_hash_table_remove(lifecycle_tbl, widget_id);
1299 EAPI int widget_service_set_sdk_util(bundle *data)
1304 val = bundle_get_val(data, AUL_K_SDK);
1308 if (!sdk_util.initialized) {
1309 r = aul_debug_info_init();
1310 if (r != AUL_R_OK) {
1311 _E("Failed to initialize debug info");
1317 bundle_free(sdk_util.b);
1319 sdk_util.b = bundle_create();
1320 if (sdk_util.b == NULL) {
1321 _E("Failed to create bundle");
1325 r = aul_debug_info_set(data, sdk_util.b);
1326 if (r != AUL_R_OK) {
1327 _E("Failed to set debug info");
1328 bundle_free(sdk_util.b);
1336 EAPI int widget_instance_convert_event_to_lifecycle_status(int event)
1339 case WIDGET_INSTANCE_EVENT_CREATE:
1340 return WIDGET_LIFE_CYCLE_EVENT_CREATE;
1341 case WIDGET_INSTANCE_EVENT_DESTROY:
1342 return WIDGET_LIFE_CYCLE_EVENT_DESTROY;
1343 case WIDGET_INSTANCE_EVENT_PAUSE:
1344 return WIDGET_LIFE_CYCLE_EVENT_PAUSE;
1345 case WIDGET_INSTANCE_EVENT_RESUME:
1346 return WIDGET_LIFE_CYCLE_EVENT_RESUME;
1352 EAPI int widget_instance_delete(const char *widget_id, const char *instance_id)
1356 ret = aul_widget_instance_del(widget_id, instance_id);
1357 if (ret == AUL_R_OK)
1358 return WIDGET_ERROR_NONE;
1362 ret = WIDGET_ERROR_OUT_OF_MEMORY;
1365 ret = WIDGET_ERROR_INVALID_PARAMETER;
1368 ret = WIDGET_ERROR_FAULT;