e10fc173ff17520c5c94954939574b131ac0cccb
[platform/core/appfw/appcore-widget.git] / src / base / widget_base.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <stdbool.h>
19
20 #include <bundle.h>
21 #include <bundle_internal.h>
22 #include <aul.h>
23 #include <aul_widget.h>
24 #include <dlog.h>
25 #include <glib.h>
26 #include <glib-object.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <widget_errno.h>
30 #include <widget_instance.h>
31 #include <aul_app_com.h>
32 #include <Ecore_Wl2.h>
33 #include <system_info.h>
34 #include <vconf.h>
35 #include <vconf-internal-keys.h>
36 #include <screen_connector_provider.h>
37 #include <appcore_multiwindow_base.h>
38
39 #include "widget_base.h"
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #endif
44
45 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
46 #define APP_TYPE_WIDGET "widgetapp"
47 #define STATUS_FOREGROUND "fg"
48 #define STATUS_BACKGROUND "bg"
49
50 static int __app_event_converter[APPCORE_BASE_EVENT_MAX] = {
51         [APP_EVENT_LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
52         [APP_EVENT_LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
53         [APP_EVENT_LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
54         [APP_EVENT_DEVICE_ORIENTATION_CHANGED]
55                         = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
56         [APP_EVENT_REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
57         [APP_EVENT_SUSPENDED_STATE_CHANGED]
58                         = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
59 };
60
61 struct app_event_info {
62         app_event_type_e type;
63         void *value;
64 };
65
66 struct app_event_handler {
67         app_event_type_e type;
68         app_event_cb cb;
69         void *data;
70         void *raw;
71 };
72
73 struct widget_foreach_context {
74         widget_base_instance_cb callback;
75         void *data;
76 };
77
78 typedef struct _widget_base_context {
79         widget_base_ops ops;
80         void *data;
81         int argc;
82         char **argv;
83         GList *classes;
84 } widget_base_context;
85
86 typedef struct _widget_base_instance_data {
87         bundle *args;
88         char *id;
89         char *content;
90         void *tag;
91         double period;
92         guint periodic_timer;
93         bool pending_update;
94         char *pending_content;
95         void *user_data;
96 } widget_base_instance_data;
97
98 static widget_base_context __context;
99 static char *__appid;
100 static char *__package_id;
101 static bool __fg_signal;
102 static char *__viewer_endpoint;
103 static bool __is_permanent;
104 static void __call_update_cb(const char *class_id, const char *id, int force,
105                 const char *content_raw);
106
107 static gboolean __timeout_cb(gpointer user_data)
108 {
109         widget_base_instance_data *data =
110                         (widget_base_instance_data *)user_data;
111         appcore_multiwindow_base_instance_h cxt;
112         const char *class_id;
113
114         cxt = appcore_multiwindow_base_instance_find(data->id);
115
116         if (!cxt) {
117                 LOGE("Can't find the instance");
118                 return G_SOURCE_REMOVE;
119         }
120
121         if (appcore_multiwindow_base_instance_is_resumed(cxt)) {
122                 LOGD("Periodic update!");
123                 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
124                 __call_update_cb(class_id, data->id, 0, NULL);
125         } else {
126                 data->pending_update = true;
127                 if (data->periodic_timer) {
128                         LOGD("Remove timer!");
129                         g_source_remove(data->periodic_timer);
130                         data->periodic_timer = 0;
131                 }
132         }
133
134         return G_SOURCE_CONTINUE;
135 }
136
137 static bool __is_widget_feature_enabled(void)
138 {
139         static bool feature = false;
140         static bool retrieved = false;
141         int ret;
142
143         if (retrieved == true)
144                 return feature;
145
146         ret = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
147         if (ret != SYSTEM_INFO_ERROR_NONE) {
148                 LOGE("failed to get system info"); /* LCOV_EXCL_LINE */
149                 return false; /* LCOV_EXCL_LINE */
150         }
151
152         retrieved = true;
153
154         return feature;
155 }
156
157 /* LCOV_EXCL_START */
158 static void __on_poweroff(keynode_t *key, void *data)
159 {
160         int val;
161
162         val = vconf_keynode_get_int(key);
163         switch (val) {
164         case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
165         case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
166                 LOGI("power off changed: %d", val);
167                 widget_base_exit();
168                 break;
169         case VCONFKEY_SYSMAN_POWER_OFF_NONE:
170         case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
171         default:
172                 /* DO NOTHING */
173                 break;
174         }
175 }
176 /* LCOV_EXCL_STOP */
177
178 static void __check_empty_instance(void)
179 {
180         int cnt = appcore_multiwindow_base_instance_get_cnt();
181
182         if (cnt == 0)
183                 widget_base_exit();
184 }
185
186 static void __instance_drop(appcore_multiwindow_base_instance_h instance_h)
187 {
188         widget_base_instance_data *data;
189
190         data = appcore_multiwindow_base_instance_get_extra(instance_h);
191         appcore_multiwindow_base_instance_drop(instance_h);
192         free(data->pending_content);
193         free(data->content);
194         free(data->id);
195         free(data);
196         __check_empty_instance();
197 }
198
199 static gint __comp_class(gconstpointer a, gconstpointer b)
200 {
201         const widget_base_class *cls = a;
202
203         return strcmp(cls->id, b);
204 }
205
206 static widget_base_class *__get_class(const char *class_id)
207 {
208         widget_base_class *cls;
209         GList *class_node;
210
211         class_node = g_list_find_custom(__context.classes, class_id,
212                         __comp_class);
213         if (class_node == NULL) {
214                 LOGE("empty classes");
215                 return NULL;
216         }
217         cls = (widget_base_class *)class_node->data;
218
219         return cls;
220 }
221
222 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
223         int status)
224 {
225         bundle *b = bundle_create();
226         int ret;
227
228         if (b == NULL) {
229                 LOGE("out of memory"); /* LCOV_EXCL_LINE */
230                 return -1; /* LCOV_EXCL_LINE */
231         }
232
233         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
234         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
235         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
236         bundle_add_str(b, AUL_K_PKGID, __package_id);
237
238         LOGD("send lifecycle %s(%d)", instance_id, status);
239         ret = aul_app_com_send("widget.status", b);
240         if (ret < 0)
241                 LOGE("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
242
243         bundle_free(b);
244
245         return ret;
246 }
247
248 static int __send_update_status(const char *class_id, const char *instance_id,
249         int status, int err, bundle *extra)
250 {
251         bundle *b;
252         int lifecycle = -1;
253         bundle_raw *raw = NULL;
254         int len;
255         char err_str[256];
256
257         b = bundle_create();
258         if (!b) {
259                 LOGE("out of memory"); /* LCOV_EXCL_LINE */
260                 return -1; /* LCOV_EXCL_LINE */
261         }
262
263         if (err < 0) {
264                 snprintf(err_str, sizeof(err_str), "%d", err);
265                 bundle_add_str(b, AUL_K_WIDGET_ERROR_CODE, err_str);
266         }
267
268         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
269         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
270         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
271
272         if (extra) {
273                 bundle_encode(extra, &raw, &len);
274                 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
275                 aul_widget_instance_add(class_id, instance_id);
276         }
277
278         LOGD("send update %s(%d) to %s", instance_id, status, __viewer_endpoint);
279         aul_app_com_send(__viewer_endpoint, b);
280
281         switch (status) {
282         case WIDGET_INSTANCE_EVENT_CREATE:
283                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
284                 break;
285         case WIDGET_INSTANCE_EVENT_DESTROY:
286                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
287                 break;
288         case WIDGET_INSTANCE_EVENT_PAUSE:
289                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
290                 break;
291         case WIDGET_INSTANCE_EVENT_RESUME:
292                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
293                 break;
294         }
295
296         if (lifecycle > -1)
297                 __send_lifecycle_event(class_id, instance_id, lifecycle);
298
299         bundle_free(b);
300         if (raw)
301                 free(raw);
302
303         return 0;
304 }
305
306 static void __control_create(const char *class_id, const char *id, bundle *b)
307 {
308         widget_base_instance_data *data;
309         char *content = NULL;
310
311         data = (widget_base_instance_data *)
312                         calloc(1, sizeof(widget_base_instance_data));
313         if (!data) {
314                 LOGE("Out of memory");
315                 return;
316         }
317
318         data->id = strdup(id);
319         data->args = b;
320
321         /* call stub create */
322         appcore_multiwindow_base_instance_run(class_id, id, data);
323         data->args = NULL;
324         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
325         if (content)
326                 data->content = strdup(content);
327
328 }
329
330 static void __control_resume(const char *class_id, const char *id, bundle *b)
331 {
332         appcore_multiwindow_base_instance_h cxt;
333
334         cxt = appcore_multiwindow_base_instance_find(id);
335         if (!cxt) {
336                 LOGE("context not found: %s", id);
337                 return;
338         }
339
340         /* call stub resume */
341         appcore_multiwindow_base_instance_resume(cxt);
342 }
343
344 static void __control_pause(const char *class_id, const char *id, bundle *b)
345 {
346         appcore_multiwindow_base_instance_h instance_h;
347
348         instance_h = appcore_multiwindow_base_instance_find(id);
349
350         if (!instance_h) {
351                 LOGE("instance not found: %s", id);
352                 return;
353         }
354
355         /* call stub pause */
356         appcore_multiwindow_base_instance_pause(instance_h);
357 }
358
359 static void __control_resize(const char *class_id, const char *id, bundle *b)
360 {
361         appcore_multiwindow_base_instance_h instance_h;
362         char *remain = NULL;
363         char *w_str = NULL;
364         char *h_str = NULL;
365         int w = 0;
366         int h = 0;
367         void *class_data;
368         widget_base_class *cls;
369         const appcore_multiwindow_base_class *raw_cls;
370
371         instance_h = appcore_multiwindow_base_instance_find(id);
372         if (!instance_h) {
373                 LOGE("context not found: %s", id);
374                 return;
375         }
376
377         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
378         if (!raw_cls)
379                 return;
380
381         cls = __get_class(class_id);
382         if (cls == NULL) {
383                 LOGE("class not found: %s", class_id);
384                 return;
385         }
386         class_data = raw_cls->data;
387         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
388         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
389
390         if (w_str)
391                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
392
393         if (h_str)
394                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
395
396         if (cls->ops.resize)
397                 cls->ops.resize(instance_h, w, h, class_data);
398
399         LOGD("%s is resized to %dx%d", id, w, h);
400         __send_update_status(class_id, id,
401                 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, 0, NULL);
402 }
403
404 static void __call_update_cb(const char *class_id, const char *id, int force,
405                 const char *content_raw)
406 {
407         void *class_data;
408         widget_base_class *cls;
409         const appcore_multiwindow_base_class *raw_cls;
410         appcore_multiwindow_base_instance_h instance_h;
411         bundle *content = NULL;
412
413         instance_h = appcore_multiwindow_base_instance_find(id);
414         if (!instance_h) {
415                 LOGE("context not found: %s", id);
416                 return;
417         }
418
419         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
420         if (!raw_cls) {
421                 LOGE("class is NULL");
422                 return;
423         }
424
425         class_data = raw_cls->data;
426         cls = __get_class(class_id);
427         if (cls == NULL) {
428                 LOGE("class not found: %s", class_id);
429                 return;
430         }
431
432         if (!cls->ops.update) {
433                 LOGE("update callback is NULL");
434                 return;
435         }
436
437         if (content_raw) {
438                 content = bundle_decode((const bundle_raw *)content_raw,
439                                 strlen(content_raw));
440         }
441
442         if (cls->ops.update)
443                 cls->ops.update(instance_h, content, force, class_data);
444
445         __send_update_status(class_id, id,
446                 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
447         LOGD("updated:%s", id);
448
449         if (content)
450                 bundle_free(content);
451 }
452
453 static void __update_pending_content(
454                 appcore_multiwindow_base_instance_h instance_h,
455                 const char *content_raw)
456 {
457         widget_base_instance_data *data;
458
459         data = (widget_base_instance_data *)
460                         appcore_multiwindow_base_instance_get_extra(instance_h);
461
462         if (data->pending_content) {
463                 free(data->pending_content);
464                 data->pending_content = NULL;
465         }
466
467         if (content_raw) {
468                 data->pending_content = strdup(content_raw);
469                 if (data->pending_content == NULL)
470                         LOGW("Out of memory");
471         }
472
473         data->pending_update = true;
474 }
475
476 static void __update_process(const char *class_id, const char *id,
477                 appcore_multiwindow_base_instance_h instance_h, void *data)
478 {
479         char *content_raw = NULL;
480         char *force_str = NULL;
481         int force;
482         bundle *b = data;
483
484         if (!b) {
485                 LOGE("bundle is NULL");
486                 return;
487         }
488
489         bundle_get_str(b, WIDGET_K_FORCE, &force_str);
490
491         if (force_str && strcmp(force_str, "true") == 0)
492                 force = 1;
493         else
494                 force = 0;
495
496         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
497         if (!appcore_multiwindow_base_instance_is_resumed(instance_h) && !force)
498                 __update_pending_content(instance_h, content_raw);
499         else
500                 __call_update_cb(class_id, id, force, content_raw);
501 }
502
503 static void __control_update(const char *class_id, const char *id, bundle *b)
504 {
505         appcore_multiwindow_base_instance_h instance_h;
506
507         if (!id) {
508                 appcore_multiwindow_base_instance_foreach(class_id,
509                                 __update_process, b);
510                 return;
511         }
512
513         instance_h = appcore_multiwindow_base_instance_find(id);
514         if (!instance_h) {
515                 LOGE("context not found: %s", id);
516                 return;
517         }
518
519         __update_process(class_id, id, instance_h, b);
520 }
521
522 static void __control_destroy(const char *class_id, const char *id, bundle *b)
523 {
524         appcore_multiwindow_base_instance_h instance_h;
525         widget_base_instance_data *data;
526
527         instance_h = appcore_multiwindow_base_instance_find(id);
528         if (!instance_h) {
529                 LOGE("could not find widget obj: %s, clear amd info", id);
530                 aul_widget_instance_del(class_id, id);
531                 return;
532         }
533
534         data = (widget_base_instance_data *)
535                         appcore_multiwindow_base_instance_get_extra(instance_h);
536         data->args = b;
537
538         /* call stub terminate */
539         appcore_multiwindow_base_instance_exit(instance_h);
540         free(data->pending_content);
541         free(data->content);
542         free(data->id);
543         free(data);
544         __check_empty_instance();
545 }
546
547 static void __control_change_period(const char *class_id, const char *id,
548                 bundle *b)
549 {
550         appcore_multiwindow_base_instance_h instance_h;
551         widget_base_instance_data *data;
552         double *period = NULL;
553         size_t size;
554         int ret;
555
556         instance_h = appcore_multiwindow_base_instance_find(id);
557         if (!instance_h) {
558                 LOGE("context not found: %s", id);
559                 return;
560         }
561
562         data = (widget_base_instance_data *)
563                         appcore_multiwindow_base_instance_get_extra(instance_h);
564
565         if (!data) {
566                 LOGE("could not find instance data: %s", id);
567                 return;
568         }
569
570         if (data->periodic_timer) {
571                 LOGD("Remove timer!");
572                 g_source_remove(data->periodic_timer);
573                 data->periodic_timer = 0;
574         }
575
576         ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
577         if (ret == BUNDLE_ERROR_NONE)
578                 data->period = *period;
579
580         if (data->period > 0) {
581                 LOGD("Restart timer!");
582                 data->periodic_timer = g_timeout_add_seconds(data->period,
583                                 __timeout_cb, data);
584         }
585
586         return;
587 }
588
589 static int __multiwindow_create(void *data)
590 {
591         char pkgid[256] = {0, };
592         int ret = 0;
593
594         appcore_multiwindow_base_on_create();
595         app_get_id(&__appid);
596         if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
597                 __package_id = strdup(pkgid);
598
599         if (!__package_id || !__appid) {
600                 LOGE("__package_id is NULL");
601                 return -1;
602         }
603
604         screen_connector_provider_init();
605         vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
606                         __on_poweroff, NULL);
607
608
609         if (__context.ops.create)
610                 ret = __context.ops.create(data);
611
612         LOGD("widget base is created");
613         return ret;
614 }
615
616 static int __multiwindow_terminate(void *data)
617 {
618         if (__context.ops.terminate)
619                 __context.ops.terminate(data);
620
621         vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
622                         __on_poweroff);
623         screen_connector_provider_fini();
624
625         if (__viewer_endpoint) {
626                 free(__viewer_endpoint);
627                 __viewer_endpoint = NULL;
628         }
629
630         if (__package_id) {
631                 free(__package_id);
632                 __package_id = NULL;
633         }
634
635         if (__appid) {
636                 free(__appid);
637                 __appid = NULL;
638         }
639
640         appcore_multiwindow_base_on_terminate();
641
642         LOGD("widget base is terminated");
643         return 0;
644 }
645
646 static int __multiwindow_control(bundle *b, void *data)
647 {
648         char *class_id = NULL;
649         char *id = NULL;
650         char *operation = NULL;
651
652         appcore_multiwindow_base_on_control(b);
653         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
654         /* for previous version compatibility, use appid for default class id */
655         if (class_id == NULL)
656                 class_id = __appid;
657
658         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
659         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
660
661         if (!operation) {
662                 LOGE("operation is NULL");
663                 return 0;
664         }
665
666         if (strcmp(operation, "create") == 0)
667                 __control_create(class_id, id, b);
668         else if (strcmp(operation, "resize") == 0)
669                 __control_resize(class_id, id, b);
670         else if (strcmp(operation, "update") == 0)
671                 __control_update(class_id, id, b);
672         else if (strcmp(operation, "destroy") == 0)
673                 __control_destroy(class_id, id, b);
674         else if (strcmp(operation, "resume") == 0)
675                 __control_resume(class_id, id, b);
676         else if (strcmp(operation, "pause") == 0)
677                 __control_pause(class_id, id, b);
678         else if (strcmp(operation, "terminate") == 0)
679                 __control_destroy(class_id, id, b);
680         else if (strcmp(operation, "period") == 0)
681                 __control_change_period(class_id, id, b);
682
683         return 0;
684 }
685
686 static void __inst_resume_cb(const char *class_id, const char *id,
687                 appcore_multiwindow_base_instance_h cxt, void *data)
688 {
689         __control_resume(class_id, id, data);
690 }
691
692 static void __get_content(bundle *b)
693 {
694         char *instance_id = NULL;
695         appcore_multiwindow_base_instance_h cxt;
696         widget_base_instance_data * we;
697
698         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
699         if (!instance_id) {
700                 LOGE("instance id is NULL");
701                 return;
702         }
703
704         cxt = appcore_multiwindow_base_instance_find(instance_id);
705         if (!cxt) {
706                 LOGE("could not find widget obj: %s", instance_id);
707                 return;
708         }
709
710         we = appcore_multiwindow_base_instance_get_extra(cxt);
711         if (!we) {
712                 LOGE("widget extra is NULL");
713                 return;
714         }
715
716         if (we->content) {
717                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
718                 LOGD("content info of %s found", instance_id);
719         } else {
720                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
721                 LOGD("empty content info added");
722         }
723 }
724
725 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
726 {
727         appcore_multiwindow_base_on_receive(type, b);
728
729         switch (type) {
730         case AUL_RESUME:
731                 appcore_multiwindow_base_instance_foreach_full(
732                                 __inst_resume_cb, b);
733                 break;
734         case AUL_TERMINATE:
735                 widget_base_exit();
736                 break;
737         case AUL_WIDGET_CONTENT:
738                 __get_content(b);
739                 break;
740         default:
741                 break;
742         }
743
744         return 0;
745 }
746
747 static void __multiwindow_init(int argc, char **argv, void *data)
748 {
749         if (__context.ops.init)
750                 __context.ops.init(argc, argv, data);
751 }
752
753 static void __multiwindow_finish(void)
754 {
755         if (__context.ops.finish) {
756                 __context.ops.finish();
757                 /* Check Loader case */
758                 if (getenv("AUL_LOADER_INIT")) {
759                         unsetenv("AUL_LOADER_INIT");
760                         __context.ops.finish();
761                 }
762         }
763 }
764
765 static void __multiwindow_run(void *data)
766 {
767         if (__context.ops.run)
768                 __context.ops.run(data);
769 }
770
771 static void __multiwindow_exit(void *data)
772 {
773         if (__context.ops.exit)
774                 __context.ops.exit(data);
775 }
776
777 EXPORT_API int widget_base_exit(void)
778 {
779         int ret;
780
781         appcore_multiwindow_base_exit();
782         if (appcore_multiwindow_base_instance_get_cnt() != 0 && __is_permanent) {
783                 ret = aul_notify_exit();
784                 aul_widget_write_log(LOG_TAG, "[%s:%d] permanent exit : %d",
785                         __FUNCTION__, __LINE__, ret);
786         }
787
788         return 0;
789 }
790
791 static gboolean __finish_event_cb(gpointer user_data)
792 {
793         appcore_multiwindow_base_instance_h cxt = user_data;
794         bundle *b;
795         const char *id;
796         const char *class_id;
797
798         if (!cxt) {
799                 LOGE("user_data is NULL");
800                 return FALSE;
801         }
802
803         id = appcore_multiwindow_base_instance_get_id(cxt);
804         class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
805         b = bundle_create();
806
807         if (!b) {
808                 LOGE("Out-of-memory");
809                 return FALSE;
810         }
811
812         bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
813         __control_destroy(class_id, id, b);
814         bundle_free(b);
815
816         return FALSE;
817 }
818
819 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
820 {
821         if (!__is_widget_feature_enabled()) {
822                 LOGE("not supported"); /* LCOV_EXCL_LINE */
823                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
824         }
825
826         if (!context) {
827                 LOGE("context is null");
828                 return WIDGET_ERROR_INVALID_PARAMETER;
829         }
830
831         g_idle_add(__finish_event_cb, context);
832
833         return WIDGET_ERROR_NONE;
834 }
835
836 static void __inst_full_cb(const char *class_id, const char *id,
837                 appcore_multiwindow_base_instance_h cxt, void *data)
838 {
839         struct widget_foreach_context *foreach_context = data;
840
841         if (!data)
842                 return;
843
844         if (foreach_context->callback)
845                 foreach_context->callback(cxt, foreach_context->data);
846 }
847
848 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
849 {
850         struct widget_foreach_context foreach_context;
851
852         if (!__is_widget_feature_enabled()) {
853                 LOGE("not supported"); /* LCOV_EXCL_LINE */
854                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
855         }
856
857         if (!cb) {
858                 LOGE("callback is NULL");
859                 return WIDGET_ERROR_INVALID_PARAMETER;
860         }
861
862         foreach_context.callback = cb;
863         foreach_context.data = data;
864         appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
865
866         return WIDGET_ERROR_NONE;
867 }
868
869 static int __event_cb(void *event, void *data)
870 {
871         app_event_handler_h handler = data;
872
873         struct app_event_info app_event;
874
875         app_event.type = handler->type;
876         app_event.value = event;
877
878         if (handler->cb)
879                 handler->cb(&app_event, handler->data);
880
881         return 0;
882 }
883
884 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
885                                         app_event_type_e event_type,
886                                         app_event_cb callback,
887                                         void *user_data)
888 {
889         int r;
890         bool feature;
891         app_event_handler_h handler;
892
893         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
894         if (r < 0)
895                 return WIDGET_BASE_ERROR_FAULT;
896
897         if (!feature)
898                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
899
900         if (event_handler == NULL || callback == NULL)
901                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
902
903         if (event_type < APP_EVENT_LOW_MEMORY
904             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
905                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
906
907         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
908                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
909
910
911         handler = calloc(1, sizeof(struct app_event_handler));
912         if (!handler)
913                 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
914
915         handler->type = event_type;
916         handler->cb = callback;
917         handler->data = user_data;
918         handler->raw = appcore_base_add_event(
919                         __app_event_converter[event_type], __event_cb, handler);
920         *event_handler = handler;
921
922         return WIDGET_BASE_ERROR_NONE;
923 }
924
925 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
926                                                 event_handler)
927 {
928         int r;
929         bool feature;
930         app_event_type_e type;
931
932         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
933         if (r < 0)
934                 return WIDGET_BASE_ERROR_FAULT;
935
936         if (!feature)
937                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
938
939         if (event_handler == NULL)
940                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
941
942         type = event_handler->type;
943         if (type < APP_EVENT_LOW_MEMORY ||
944                         type > APP_EVENT_REGION_FORMAT_CHANGED)
945                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
946
947         r = appcore_base_remove_event(event_handler->raw);
948         if (r < 0)
949                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
950
951         free(event_handler);
952
953         return WIDGET_BASE_ERROR_NONE;
954 }
955
956 EXPORT_API int widget_base_context_set_content_info(
957                 widget_base_instance_h context,
958                 bundle *content_info)
959 {
960         int ret = 0;
961         bundle_raw *raw = NULL;
962         int len;
963         const char *id;
964         const char *class_id;
965         widget_base_instance_data *data;
966         appcore_multiwindow_base_instance_h instance_h;
967
968         if (!__is_widget_feature_enabled()) {
969                 LOGE("not supported"); /* LCOV_EXCL_LINE */
970                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
971         }
972
973         if (!context || !content_info)
974                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
975
976         instance_h = (appcore_multiwindow_base_instance_h)context;
977         id = appcore_multiwindow_base_instance_get_id(instance_h);
978         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
979         data = appcore_multiwindow_base_instance_get_extra(instance_h);
980
981         if (!class_id || !id || !data)
982                 return WIDGET_BASE_ERROR_FAULT;
983
984         ret = __send_update_status(class_id, id,
985                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
986
987         if (data->content)
988                 free(data->content);
989
990         bundle_encode(content_info, &raw, &len);
991         if (raw)
992                 data->content = strdup((const char *)raw);
993         else
994                 data->content = NULL;
995
996         free(raw);
997         if (ret < 0) {
998                 /* LCOV_EXCL_START */
999                 LOGE("failed to send content info: %s of %s (%d)", id,
1000                                 class_id, ret);
1001                 return WIDGET_BASE_ERROR_IO_ERROR;
1002                 /* LCOV_EXCL_STOP */
1003         }
1004
1005         return WIDGET_BASE_ERROR_NONE;
1006 }
1007
1008 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
1009 {
1010         appcore_multiwindow_base_instance_h instance_h;
1011         widget_base_instance_data *data;
1012
1013         if (!__is_widget_feature_enabled()) {
1014                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1015                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1016         }
1017
1018         if (!context || !tag) {
1019                 LOGE("Invalid parameter");
1020                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1021         }
1022
1023         instance_h = (appcore_multiwindow_base_instance_h)context;
1024         data = (widget_base_instance_data *)
1025                         appcore_multiwindow_base_instance_get_extra(instance_h);
1026
1027         if (!data) {
1028                 LOGE("Invalid parameter");
1029                 return WIDGET_ERROR_INVALID_PARAMETER;
1030         }
1031
1032         *tag = data->tag;
1033
1034         return WIDGET_BASE_ERROR_NONE;
1035 }
1036
1037 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
1038 {
1039         appcore_multiwindow_base_instance_h instance_h;
1040         widget_base_instance_data *data;
1041
1042         if (!__is_widget_feature_enabled()) {
1043                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1044                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1045         }
1046
1047         if (!context) {
1048                 LOGE("Invalid parameter");
1049                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1050         }
1051
1052         instance_h = (appcore_multiwindow_base_instance_h)context;
1053         data = (widget_base_instance_data *)
1054                         appcore_multiwindow_base_instance_get_extra(instance_h);
1055         data->tag = tag;
1056
1057         return WIDGET_BASE_ERROR_NONE;
1058 }
1059
1060 EXPORT_API void *widget_base_context_get_user_data(
1061                 widget_base_instance_h context)
1062 {
1063         appcore_multiwindow_base_instance_h instance_h;
1064         widget_base_instance_data *data;
1065
1066         if (!__is_widget_feature_enabled()) {
1067                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1068                 return NULL; /* LCOV_EXCL_LINE */
1069         }
1070
1071         if (!context) {
1072                 LOGE("Invalid parameter");
1073                 return NULL;
1074         }
1075
1076         instance_h = (appcore_multiwindow_base_instance_h)context;
1077         data = (widget_base_instance_data *)
1078                         appcore_multiwindow_base_instance_get_extra(instance_h);
1079
1080         return data->user_data;
1081 }
1082
1083
1084 EXPORT_API int widget_base_context_set_user_data(
1085                 widget_base_instance_h context, void *user_data)
1086 {
1087         appcore_multiwindow_base_instance_h instance_h;
1088         widget_base_instance_data *data;
1089
1090         if (!__is_widget_feature_enabled()) {
1091                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1092                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1093         }
1094
1095         if (!context) {
1096                 LOGE("Invalid parameter");
1097                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1098         }
1099
1100         instance_h = (appcore_multiwindow_base_instance_h)context;
1101         data = (widget_base_instance_data *)
1102                         appcore_multiwindow_base_instance_get_extra(instance_h);
1103         data->user_data = user_data;
1104
1105         return WIDGET_BASE_ERROR_NONE;
1106 }
1107
1108 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1109 {
1110         appcore_multiwindow_base_instance_h instance_h;
1111
1112         if (!__is_widget_feature_enabled()) {
1113                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1114                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1115         }
1116
1117         instance_h = (appcore_multiwindow_base_instance_h)context;
1118         *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1119
1120         return WIDGET_BASE_ERROR_NONE;
1121 }
1122
1123 EXPORT_API const char *widget_base_get_viewer_endpoint()
1124 {
1125         return __viewer_endpoint;
1126 }
1127
1128 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1129                 void *data)
1130 {
1131         bundle *kb;
1132         char *viewer_endpoint = NULL;
1133         appcore_multiwindow_base_ops raw_ops
1134                         = appcore_multiwindow_base_get_default_ops();
1135
1136         __context.ops = ops;
1137         __context.argc = argc;
1138         __context.argv = argv;
1139         __context.data = data;
1140
1141         /* override methods */
1142         raw_ops.base.create = __multiwindow_create;
1143         raw_ops.base.control = __multiwindow_control;
1144         raw_ops.base.terminate = __multiwindow_terminate;
1145         raw_ops.base.receive = __multiwindow_receive;
1146         raw_ops.base.init = __multiwindow_init;
1147         raw_ops.base.finish = __multiwindow_finish;
1148         raw_ops.base.run = __multiwindow_run;
1149         raw_ops.base.exit = __multiwindow_exit;
1150
1151         if (!__is_widget_feature_enabled()) {
1152                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1153                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1154         }
1155
1156         kb = bundle_import_from_argv(argc, argv);
1157         if (kb) {
1158                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
1159                 if (viewer_endpoint) {
1160                         LOGD("viewer endpoint :%s", viewer_endpoint);
1161                         __viewer_endpoint = strdup(viewer_endpoint);
1162                 } else {
1163                         LOGE("endpoint is missing");
1164                 }
1165
1166                 bundle_free(kb);
1167         } else {
1168                 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1169                 return WIDGET_ERROR_FAULT;
1170         }
1171
1172         if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1173                return WIDGET_ERROR_FAULT;
1174
1175         return WIDGET_ERROR_NONE;
1176 }
1177
1178 static int __on_create(void *data)
1179 {
1180         return widget_base_on_create();
1181 }
1182
1183 static int __on_terminate(void *data)
1184 {
1185         return widget_base_on_terminate();
1186 }
1187
1188 static void __on_init(int argc, char **argv, void *data)
1189 {
1190         widget_base_on_init(argc, argv);
1191 }
1192
1193 static void __on_finish(void)
1194 {
1195         widget_base_on_finish();
1196 }
1197
1198 static void __on_run(void *data)
1199 {
1200         widget_base_on_run();
1201 }
1202
1203 static void __on_exit(void *data)
1204 {
1205         widget_base_on_exit();
1206 }
1207
1208 EXPORT_API int widget_base_on_create(void)
1209 {
1210         appcore_multiwindow_base_on_create();
1211
1212         return 0;
1213 }
1214
1215 EXPORT_API int widget_base_on_terminate(void)
1216 {
1217         appcore_multiwindow_base_on_terminate();
1218
1219         return 0;
1220 }
1221
1222 EXPORT_API int widget_base_on_init(int argc, char **argv)
1223 {
1224         return 0;
1225 }
1226
1227 EXPORT_API void widget_base_on_finish(void)
1228 {
1229 }
1230
1231 EXPORT_API void widget_base_on_run(void)
1232 {
1233 }
1234
1235 EXPORT_API void widget_base_on_exit(void)
1236 {
1237 }
1238
1239 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1240 {
1241         widget_base_ops ops;
1242
1243         /* override methods */
1244         ops.create = __on_create;
1245         ops.terminate = __on_terminate;
1246         ops.init = __on_init;
1247         ops.finish = __on_finish;
1248         ops.run = __on_run;
1249         ops.exit = __on_exit;
1250
1251         return ops;
1252 }
1253
1254 static void __free_class(gpointer data)
1255 {
1256         widget_base_class *cls = data;
1257
1258         free(cls->id);
1259         free(cls);
1260 }
1261
1262 EXPORT_API void widget_base_fini(void)
1263 {
1264         appcore_multiwindow_base_fini();
1265         g_list_free_full(__context.classes, __free_class);
1266         __context.classes = NULL;
1267 }
1268
1269 EXPORT_API int widget_base_context_window_bind(
1270                 widget_base_instance_h instance_h, const char *id,
1271                 Ecore_Wl2_Window *wl_win)
1272 {
1273         struct wl_surface *surface;
1274
1275         surface = ecore_wl2_window_surface_get(wl_win);
1276         if (surface == NULL) {
1277                 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1278                 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1279         }
1280
1281         screen_connector_provider_remote_enable(id, surface);
1282         appcore_multiwindow_base_window_bind(instance_h, wl_win);
1283
1284         return WIDGET_BASE_ERROR_NONE;
1285 }
1286
1287 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1288                 int w, int h, void *class_data)
1289 {
1290         return widget_base_class_on_create(instance_h, content, w, h);
1291 }
1292
1293 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1294 {
1295         return widget_base_class_on_resume(instance_h);
1296 }
1297
1298 static int __class_on_pause(widget_base_instance_h instance_h,
1299                 void *class_data)
1300 {
1301         return widget_base_class_on_pause(instance_h);
1302 }
1303
1304 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1305                 void *class_data)
1306 {
1307         return widget_base_class_on_resize(instance_h, w, h);
1308 }
1309
1310 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1311                 int force, void *class_data)
1312 {
1313         return widget_base_class_on_update(instance_h, content, force);
1314 }
1315
1316 static int __class_on_destroy(widget_base_instance_h instance_h,
1317                 widget_base_destroy_type_e reason, bundle *content,
1318                 void *class_data)
1319 {
1320         return widget_base_class_on_destroy(instance_h, reason, content);
1321 }
1322
1323 static void __multiwindow_instance_create(
1324                 appcore_multiwindow_base_instance_h instance_h,
1325                 void *class_data)
1326 {
1327         widget_base_instance_data *instance_data;
1328         bundle *b;
1329         bundle *content_info = NULL;
1330         char *id = NULL;
1331         char *class_id = NULL;
1332         char *operation = NULL;
1333         char *content = NULL;
1334         char *w_str = NULL;
1335         char *h_str = NULL;
1336         char *remain = NULL;
1337         int w = 0;
1338         int h = 0;
1339         int ret = -1;
1340         widget_base_class *cls;
1341         double *period = NULL;
1342         size_t size;
1343
1344         appcore_multiwindow_base_class_on_create(instance_h);
1345         instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1346         b = instance_data->args;
1347
1348         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1349         /* for previous version compatibility, use appid for default class id */
1350         if (class_id == NULL)
1351                 class_id = __appid;
1352
1353         cls = __get_class(class_id);
1354         if (cls == NULL) {
1355                 LOGE("class not found: %s", class_id);
1356                 return;
1357         }
1358
1359         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1360         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1361
1362         if (!operation) {
1363                 LOGE("no operation provided");
1364                 return;
1365         }
1366
1367         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1368         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1369         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1370
1371         if (w_str)
1372                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1373
1374         if (h_str)
1375                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1376
1377         if (content)
1378                 content_info = bundle_decode((const bundle_raw *)content,
1379                                 strlen(content));
1380
1381         if (cls->ops.create)
1382                 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1383
1384         if (ret < 0) {
1385                 LOGW("Create callback returns error(%d)", ret);
1386                 ret = __send_update_status(class_id, id,
1387                                 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
1388                 if (ret < 0)
1389                         LOGE("Fail to send abort status (%d) ", ret);
1390                 __instance_drop(instance_h);
1391         } else {
1392                 LOGD("%s is created", id);
1393                 ret = __send_update_status(class_id, id,
1394                         WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
1395                 if (ret < 0)
1396                         LOGE("Fail to send create status (%d) ", ret);
1397
1398                 aul_widget_instance_add(class_id, id);
1399
1400                 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period,
1401                                 &size);
1402                 if (ret == BUNDLE_ERROR_NONE && *period > 0) {
1403                         LOGI("set periodic update timer (%lf)", *period);
1404                         instance_data->period = *period;
1405                         instance_data->periodic_timer = g_timeout_add_seconds(
1406                                         instance_data->period,
1407                                         __timeout_cb, instance_data);
1408                 }
1409         }
1410
1411         if (content_info)
1412                 bundle_free(content_info);
1413 }
1414
1415 static void __multiwindow_instance_resume(
1416                 appcore_multiwindow_base_instance_h instance_h,
1417                 void *class_data)
1418 {
1419         const char *id;
1420         const char *class_id;
1421         widget_base_class *cls;
1422         widget_base_instance_data *data;
1423
1424         appcore_multiwindow_base_class_on_resume(instance_h);
1425         id = appcore_multiwindow_base_instance_get_id(instance_h);
1426         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1427         cls = __get_class(class_id);
1428         if (cls == NULL) {
1429                 LOGE("class not found: %s", class_id);
1430                 return;
1431         }
1432
1433         data = (widget_base_instance_data *)
1434                         appcore_multiwindow_base_instance_get_extra(instance_h);
1435
1436         if (data->pending_update) {
1437                 LOGD("pending update!");
1438                 data->pending_update = false;
1439                 __call_update_cb(class_id, data->id, 0, data->pending_content);
1440                 if (data->period > 0) {
1441                         LOGD("Restart timer!");
1442                         data->periodic_timer = g_timeout_add_seconds(
1443                                         data->period,
1444                                         __timeout_cb, data);
1445                 }
1446         }
1447
1448         if (cls->ops.resume)
1449                 cls->ops.resume(instance_h, class_data);
1450
1451         LOGD("%s is resumed", id);
1452         __send_update_status(class_id, id,
1453                 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
1454
1455         if (!__fg_signal) {
1456                 LOGD("Send fg signal to resourceD");
1457                 aul_widget_instance_change_status(class_id, STATUS_FOREGROUND);
1458                 __fg_signal = true;
1459         }
1460 }
1461
1462 static void __multiwindow_instance_pause(
1463                 appcore_multiwindow_base_instance_h instance_h,
1464                 void *class_data)
1465 {
1466         const char *id;
1467         const char *class_id;
1468         widget_base_class *cls;
1469
1470         appcore_multiwindow_base_class_on_pause(instance_h);
1471         id = appcore_multiwindow_base_instance_get_id(instance_h);
1472         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1473         cls = __get_class(class_id);
1474         if (cls == NULL) {
1475                 LOGE("class not found: %s", class_id);
1476                 return;
1477         }
1478
1479         if (cls->ops.pause)
1480                 cls->ops.pause(instance_h, class_data);
1481
1482         LOGD("%s is paused", id);
1483         __send_update_status(class_id, id,
1484                 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
1485
1486         if (__fg_signal) {
1487                 LOGD("Send bg signal to resourceD");
1488                 aul_widget_instance_change_status(class_id, STATUS_BACKGROUND);
1489                 __fg_signal = false;
1490         }
1491 }
1492
1493 static void __multiwindow_instance_terminate(
1494                 appcore_multiwindow_base_instance_h instance_h,
1495                 void *class_data)
1496 {
1497         widget_base_instance_data *data;
1498         bundle *b;
1499         char *operation = NULL;
1500         bundle *content_info;
1501         widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1502         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1503         const char *id;
1504         const char *class_id;
1505         widget_base_class *cls;
1506
1507         id = appcore_multiwindow_base_instance_get_id(instance_h);
1508         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1509         data  = appcore_multiwindow_base_instance_get_extra(
1510                         (appcore_multiwindow_base_instance_h)instance_h);
1511         b = data->args;
1512         cls = __get_class(class_id);
1513         if (cls == NULL) {
1514                 LOGE("class not found: %s", class_id);
1515                 return;
1516         }
1517
1518         if (b) {
1519                 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1520                 if (operation && strcmp(operation, "destroy") == 0)
1521                         reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1522         }
1523
1524         if (data->content)
1525                 content_info = bundle_decode((const bundle_raw *)data->content,
1526                                 strlen(data->content));
1527         else
1528                 content_info = bundle_create();
1529
1530         if (cls->ops.destroy)
1531                 cls->ops.destroy(instance_h, reason, content_info, class_data);
1532
1533         LOGW("%s is destroyed %d", id, reason);
1534         if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1535                 __is_permanent = true;
1536                 event = WIDGET_INSTANCE_EVENT_DESTROY;
1537                 aul_widget_instance_del(class_id, id);
1538         } else {
1539                 __is_permanent = false;
1540                 __send_update_status(class_id, id,
1541                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0,
1542                                 content_info);
1543         }
1544
1545         if (content_info)
1546                 bundle_free(content_info);
1547
1548         if (data->periodic_timer)
1549                 g_source_remove(data->periodic_timer);
1550
1551         __send_update_status(class_id, id, event, 0, NULL);
1552         appcore_multiwindow_base_class_on_terminate(instance_h);
1553 }
1554
1555 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1556                 bundle *content, int w, int h)
1557 {
1558         appcore_multiwindow_base_class_on_create(instance_h);
1559
1560         return 0;
1561 }
1562
1563 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1564 {
1565         appcore_multiwindow_base_class_on_pause(instance_h);
1566
1567         return 0;
1568 }
1569
1570 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1571 {
1572         appcore_multiwindow_base_class_on_resume(instance_h);
1573
1574         return 0;
1575 }
1576
1577 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1578                 int w, int h)
1579 {
1580         return 0;
1581 }
1582
1583 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1584                 bundle *content, int force)
1585 {
1586         return 0;
1587 }
1588
1589 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1590                 widget_base_destroy_type_e reason, bundle *content)
1591 {
1592         appcore_multiwindow_base_class_on_terminate(instance_h);
1593
1594         return 0;
1595 }
1596
1597 EXPORT_API widget_base_class widget_base_class_get_default(void)
1598 {
1599         widget_base_class cls;
1600
1601         cls.ops.create = __class_on_create;
1602         cls.ops.resize = __class_on_resize;
1603         cls.ops.update = __class_on_update;
1604         cls.ops.destroy = __class_on_destroy;
1605         cls.ops.pause = __class_on_pause;
1606         cls.ops.resume = __class_on_resume;
1607         cls.id = NULL;
1608
1609         return cls;
1610 }
1611
1612 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1613                 const char *class_id, void *class_data)
1614 {
1615         widget_base_class *c;
1616         appcore_multiwindow_base_class raw_cls;
1617
1618         if (!__is_widget_feature_enabled()) {
1619                 LOGE("not supported");
1620                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1621                 return NULL;
1622         }
1623
1624         if (!class_id) {
1625                 LOGE("class is is NULL");
1626                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1627                 return NULL;
1628         }
1629
1630         raw_cls.id = strdup(class_id);
1631         raw_cls.data = class_data;
1632         raw_cls.create = __multiwindow_instance_create;
1633         raw_cls.terminate = __multiwindow_instance_terminate;
1634         raw_cls.pause = __multiwindow_instance_pause;
1635         raw_cls.resume = __multiwindow_instance_resume;
1636         appcore_multiwindow_base_class_add(raw_cls);
1637
1638         c = malloc(sizeof(widget_base_class));
1639         if (!c)
1640                 return NULL;
1641
1642         *c = cls;
1643         c->id = strdup(class_id);
1644         __context.classes = g_list_append(__context.classes, c);
1645
1646         return c;
1647 }