c7325769f2817d312ef7e2127d6b46fdd7a14d24
[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_Wayland.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         appcore_multiwindow_base_exit();
780         if (appcore_multiwindow_base_instance_get_cnt() != 0 && __is_permanent)
781                 aul_notify_exit();
782
783         return 0;
784 }
785
786 static gboolean __finish_event_cb(gpointer user_data)
787 {
788         appcore_multiwindow_base_instance_h cxt = user_data;
789         bundle *b;
790         const char *id;
791         const char *class_id;
792
793         if (!cxt) {
794                 LOGE("user_data is NULL");
795                 return FALSE;
796         }
797
798         id = appcore_multiwindow_base_instance_get_id(cxt);
799         class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
800         b = bundle_create();
801
802         if (!b) {
803                 LOGE("Out-of-memory");
804                 return FALSE;
805         }
806
807         bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
808         __control_destroy(class_id, id, b);
809         bundle_free(b);
810
811         return FALSE;
812 }
813
814 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
815 {
816         if (!__is_widget_feature_enabled()) {
817                 LOGE("not supported"); /* LCOV_EXCL_LINE */
818                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
819         }
820
821         if (!context) {
822                 LOGE("context is null");
823                 return WIDGET_ERROR_INVALID_PARAMETER;
824         }
825
826         g_idle_add(__finish_event_cb, context);
827
828         return WIDGET_ERROR_NONE;
829 }
830
831 static void __inst_full_cb(const char *class_id, const char *id,
832                 appcore_multiwindow_base_instance_h cxt, void *data)
833 {
834         struct widget_foreach_context *foreach_context = data;
835
836         if (!data)
837                 return;
838
839         if (foreach_context->callback)
840                 foreach_context->callback(cxt, foreach_context->data);
841 }
842
843 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
844 {
845         struct widget_foreach_context foreach_context;
846
847         if (!__is_widget_feature_enabled()) {
848                 LOGE("not supported"); /* LCOV_EXCL_LINE */
849                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
850         }
851
852         if (!cb) {
853                 LOGE("callback is NULL");
854                 return WIDGET_ERROR_INVALID_PARAMETER;
855         }
856
857         foreach_context.callback = cb;
858         foreach_context.data = data;
859         appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
860
861         return WIDGET_ERROR_NONE;
862 }
863
864 static int __event_cb(void *event, void *data)
865 {
866         app_event_handler_h handler = data;
867
868         struct app_event_info app_event;
869
870         app_event.type = handler->type;
871         app_event.value = event;
872
873         if (handler->cb)
874                 handler->cb(&app_event, handler->data);
875
876         return 0;
877 }
878
879 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
880                                         app_event_type_e event_type,
881                                         app_event_cb callback,
882                                         void *user_data)
883 {
884         int r;
885         bool feature;
886         app_event_handler_h handler;
887
888         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
889         if (r < 0)
890                 return WIDGET_BASE_ERROR_FAULT;
891
892         if (!feature)
893                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
894
895         if (event_handler == NULL || callback == NULL)
896                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
897
898         if (event_type < APP_EVENT_LOW_MEMORY
899             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
900                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
901
902         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
903                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
904
905
906         handler = calloc(1, sizeof(struct app_event_handler));
907         if (!handler)
908                 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
909
910         handler->type = event_type;
911         handler->cb = callback;
912         handler->data = user_data;
913         handler->raw = appcore_base_add_event(
914                         __app_event_converter[event_type], __event_cb, handler);
915         *event_handler = handler;
916
917         return WIDGET_BASE_ERROR_NONE;
918 }
919
920 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
921                                                 event_handler)
922 {
923         int r;
924         bool feature;
925         app_event_type_e type;
926
927         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
928         if (r < 0)
929                 return WIDGET_BASE_ERROR_FAULT;
930
931         if (!feature)
932                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
933
934         if (event_handler == NULL)
935                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
936
937         type = event_handler->type;
938         if (type < APP_EVENT_LOW_MEMORY ||
939                         type > APP_EVENT_REGION_FORMAT_CHANGED)
940                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
941
942         r = appcore_base_remove_event(event_handler->raw);
943         if (r < 0)
944                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
945
946         free(event_handler);
947
948         return WIDGET_BASE_ERROR_NONE;
949 }
950
951 EXPORT_API int widget_base_context_set_content_info(
952                 widget_base_instance_h context,
953                 bundle *content_info)
954 {
955         int ret = 0;
956         bundle_raw *raw = NULL;
957         int len;
958         const char *id;
959         const char *class_id;
960         widget_base_instance_data *data;
961         appcore_multiwindow_base_instance_h instance_h;
962
963         if (!__is_widget_feature_enabled()) {
964                 LOGE("not supported"); /* LCOV_EXCL_LINE */
965                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
966         }
967
968         if (!context || !content_info)
969                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
970
971         instance_h = (appcore_multiwindow_base_instance_h)context;
972         id = appcore_multiwindow_base_instance_get_id(instance_h);
973         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
974         data = appcore_multiwindow_base_instance_get_extra(instance_h);
975
976         if (!class_id || !id || !data)
977                 return WIDGET_BASE_ERROR_FAULT;
978
979         ret = __send_update_status(class_id, id,
980                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
981
982         if (data->content)
983                 free(data->content);
984
985         bundle_encode(content_info, &raw, &len);
986         if (raw)
987                 data->content = strdup((const char *)raw);
988         else
989                 data->content = NULL;
990
991         free(raw);
992         if (ret < 0) {
993                 /* LCOV_EXCL_START */
994                 LOGE("failed to send content info: %s of %s (%d)", id,
995                                 class_id, ret);
996                 return WIDGET_BASE_ERROR_IO_ERROR;
997                 /* LCOV_EXCL_STOP */
998         }
999
1000         return WIDGET_BASE_ERROR_NONE;
1001 }
1002
1003 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
1004 {
1005         appcore_multiwindow_base_instance_h instance_h;
1006         widget_base_instance_data *data;
1007
1008         if (!__is_widget_feature_enabled()) {
1009                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1010                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1011         }
1012
1013         if (!context || !tag) {
1014                 LOGE("Invalid parameter");
1015                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1016         }
1017
1018         instance_h = (appcore_multiwindow_base_instance_h)context;
1019         data = (widget_base_instance_data *)
1020                         appcore_multiwindow_base_instance_get_extra(instance_h);
1021
1022         if (!data) {
1023                 LOGE("Invalid parameter");
1024                 return WIDGET_ERROR_INVALID_PARAMETER;
1025         }
1026
1027         *tag = data->tag;
1028
1029         return WIDGET_BASE_ERROR_NONE;
1030 }
1031
1032 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
1033 {
1034         appcore_multiwindow_base_instance_h instance_h;
1035         widget_base_instance_data *data;
1036
1037         if (!__is_widget_feature_enabled()) {
1038                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1039                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1040         }
1041
1042         if (!context) {
1043                 LOGE("Invalid parameter");
1044                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1045         }
1046
1047         instance_h = (appcore_multiwindow_base_instance_h)context;
1048         data = (widget_base_instance_data *)
1049                         appcore_multiwindow_base_instance_get_extra(instance_h);
1050         data->tag = tag;
1051
1052         return WIDGET_BASE_ERROR_NONE;
1053 }
1054
1055 EXPORT_API void *widget_base_context_get_user_data(
1056                 widget_base_instance_h context)
1057 {
1058         appcore_multiwindow_base_instance_h instance_h;
1059         widget_base_instance_data *data;
1060
1061         if (!__is_widget_feature_enabled()) {
1062                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1063                 return NULL; /* LCOV_EXCL_LINE */
1064         }
1065
1066         if (!context) {
1067                 LOGE("Invalid parameter");
1068                 return NULL;
1069         }
1070
1071         instance_h = (appcore_multiwindow_base_instance_h)context;
1072         data = (widget_base_instance_data *)
1073                         appcore_multiwindow_base_instance_get_extra(instance_h);
1074
1075         return data->user_data;
1076 }
1077
1078
1079 EXPORT_API int widget_base_context_set_user_data(
1080                 widget_base_instance_h context, void *user_data)
1081 {
1082         appcore_multiwindow_base_instance_h instance_h;
1083         widget_base_instance_data *data;
1084
1085         if (!__is_widget_feature_enabled()) {
1086                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1087                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1088         }
1089
1090         if (!context) {
1091                 LOGE("Invalid parameter");
1092                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1093         }
1094
1095         instance_h = (appcore_multiwindow_base_instance_h)context;
1096         data = (widget_base_instance_data *)
1097                         appcore_multiwindow_base_instance_get_extra(instance_h);
1098         data->user_data = user_data;
1099
1100         return WIDGET_BASE_ERROR_NONE;
1101 }
1102
1103 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1104 {
1105         appcore_multiwindow_base_instance_h instance_h;
1106
1107         if (!__is_widget_feature_enabled()) {
1108                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1109                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1110         }
1111
1112         instance_h = (appcore_multiwindow_base_instance_h)context;
1113         *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1114
1115         return WIDGET_BASE_ERROR_NONE;
1116 }
1117
1118 EXPORT_API const char *widget_base_get_viewer_endpoint()
1119 {
1120         return __viewer_endpoint;
1121 }
1122
1123 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1124                 void *data)
1125 {
1126         bundle *kb;
1127         char *viewer_endpoint = NULL;
1128         appcore_multiwindow_base_ops raw_ops
1129                         = appcore_multiwindow_base_get_default_ops();
1130
1131         __context.ops = ops;
1132         __context.argc = argc;
1133         __context.argv = argv;
1134         __context.data = data;
1135
1136         /* override methods */
1137         raw_ops.base.create = __multiwindow_create;
1138         raw_ops.base.control = __multiwindow_control;
1139         raw_ops.base.terminate = __multiwindow_terminate;
1140         raw_ops.base.receive = __multiwindow_receive;
1141         raw_ops.base.init = __multiwindow_init;
1142         raw_ops.base.finish = __multiwindow_finish;
1143         raw_ops.base.run = __multiwindow_run;
1144         raw_ops.base.exit = __multiwindow_exit;
1145
1146         if (!__is_widget_feature_enabled()) {
1147                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1148                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1149         }
1150
1151         kb = bundle_import_from_argv(argc, argv);
1152         if (kb) {
1153                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
1154                 if (viewer_endpoint) {
1155                         LOGD("viewer endpoint :%s", viewer_endpoint);
1156                         __viewer_endpoint = strdup(viewer_endpoint);
1157                 } else {
1158                         LOGE("endpoint is missing");
1159                 }
1160
1161                 bundle_free(kb);
1162         } else {
1163                 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1164                 return WIDGET_ERROR_FAULT;
1165         }
1166
1167         if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1168                return WIDGET_ERROR_FAULT;
1169
1170         return WIDGET_ERROR_NONE;
1171 }
1172
1173 static int __on_create(void *data)
1174 {
1175         return widget_base_on_create();
1176 }
1177
1178 static int __on_terminate(void *data)
1179 {
1180         return widget_base_on_terminate();
1181 }
1182
1183 static void __on_init(int argc, char **argv, void *data)
1184 {
1185         widget_base_on_init(argc, argv);
1186 }
1187
1188 static void __on_finish(void)
1189 {
1190         widget_base_on_finish();
1191 }
1192
1193 static void __on_run(void *data)
1194 {
1195         widget_base_on_run();
1196 }
1197
1198 static void __on_exit(void *data)
1199 {
1200         widget_base_on_exit();
1201 }
1202
1203 EXPORT_API int widget_base_on_create(void)
1204 {
1205         appcore_multiwindow_base_on_create();
1206
1207         return 0;
1208 }
1209
1210 EXPORT_API int widget_base_on_terminate(void)
1211 {
1212         appcore_multiwindow_base_on_terminate();
1213
1214         return 0;
1215 }
1216
1217 EXPORT_API int widget_base_on_init(int argc, char **argv)
1218 {
1219         return 0;
1220 }
1221
1222 EXPORT_API void widget_base_on_finish(void)
1223 {
1224 }
1225
1226 EXPORT_API void widget_base_on_run(void)
1227 {
1228 }
1229
1230 EXPORT_API void widget_base_on_exit(void)
1231 {
1232 }
1233
1234 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1235 {
1236         widget_base_ops ops;
1237
1238         /* override methods */
1239         ops.create = __on_create;
1240         ops.terminate = __on_terminate;
1241         ops.init = __on_init;
1242         ops.finish = __on_finish;
1243         ops.run = __on_run;
1244         ops.exit = __on_exit;
1245
1246         return ops;
1247 }
1248
1249 static void __free_class(gpointer data)
1250 {
1251         widget_base_class *cls = data;
1252
1253         free(cls->id);
1254         free(cls);
1255 }
1256
1257 EXPORT_API void widget_base_fini(void)
1258 {
1259         appcore_multiwindow_base_fini();
1260         g_list_free_full(__context.classes, __free_class);
1261         __context.classes = NULL;
1262 }
1263
1264 EXPORT_API int widget_base_context_window_bind(
1265                 widget_base_instance_h instance_h, const char *id,
1266                 Ecore_Wl_Window *wl_win)
1267 {
1268         struct wl_surface *surface;
1269
1270         surface = ecore_wl_window_surface_get(wl_win);
1271         if (surface == NULL) {
1272                 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1273                 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1274         }
1275
1276         screen_connector_provider_remote_enable(id, surface);
1277         appcore_multiwindow_base_window_bind(instance_h, wl_win);
1278
1279         return WIDGET_BASE_ERROR_NONE;
1280 }
1281
1282 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1283                 int w, int h, void *class_data)
1284 {
1285         return widget_base_class_on_create(instance_h, content, w, h);
1286 }
1287
1288 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1289 {
1290         return widget_base_class_on_resume(instance_h);
1291 }
1292
1293 static int __class_on_pause(widget_base_instance_h instance_h,
1294                 void *class_data)
1295 {
1296         return widget_base_class_on_pause(instance_h);
1297 }
1298
1299 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1300                 void *class_data)
1301 {
1302         return widget_base_class_on_resize(instance_h, w, h);
1303 }
1304
1305 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1306                 int force, void *class_data)
1307 {
1308         return widget_base_class_on_update(instance_h, content, force);
1309 }
1310
1311 static int __class_on_destroy(widget_base_instance_h instance_h,
1312                 widget_base_destroy_type_e reason, bundle *content,
1313                 void *class_data)
1314 {
1315         return widget_base_class_on_destroy(instance_h, reason, content);
1316 }
1317
1318 static void __multiwindow_instance_create(
1319                 appcore_multiwindow_base_instance_h instance_h,
1320                 void *class_data)
1321 {
1322         widget_base_instance_data *instance_data;
1323         bundle *b;
1324         bundle *content_info = NULL;
1325         char *id = NULL;
1326         char *class_id = NULL;
1327         char *operation = NULL;
1328         char *content = NULL;
1329         char *w_str = NULL;
1330         char *h_str = NULL;
1331         char *remain = NULL;
1332         int w = 0;
1333         int h = 0;
1334         int ret = -1;
1335         widget_base_class *cls;
1336         double *period = NULL;
1337         size_t size;
1338
1339         appcore_multiwindow_base_class_on_create(instance_h);
1340         instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1341         b = instance_data->args;
1342
1343         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1344         /* for previous version compatibility, use appid for default class id */
1345         if (class_id == NULL)
1346                 class_id = __appid;
1347
1348         cls = __get_class(class_id);
1349         if (cls == NULL) {
1350                 LOGE("class not found: %s", class_id);
1351                 return;
1352         }
1353
1354         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1355         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1356
1357         if (!operation) {
1358                 LOGE("no operation provided");
1359                 return;
1360         }
1361
1362         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1363         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1364         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1365
1366         if (w_str)
1367                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1368
1369         if (h_str)
1370                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1371
1372         if (content)
1373                 content_info = bundle_decode((const bundle_raw *)content,
1374                                 strlen(content));
1375
1376         if (cls->ops.create)
1377                 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1378
1379         if (ret < 0) {
1380                 LOGW("Create callback returns error(%d)", ret);
1381                 ret = __send_update_status(class_id, id,
1382                                 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
1383                 if (ret < 0)
1384                         LOGE("Fail to send abort status (%d) ", ret);
1385                 __instance_drop(instance_h);
1386         } else {
1387                 LOGD("%s is created", id);
1388                 ret = __send_update_status(class_id, id,
1389                         WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
1390                 if (ret < 0)
1391                         LOGE("Fail to send create status (%d) ", ret);
1392
1393                 aul_widget_instance_add(class_id, id);
1394
1395                 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period,
1396                                 &size);
1397                 if (ret == BUNDLE_ERROR_NONE && *period > 0) {
1398                         LOGI("set periodic update timer (%lf)", *period);
1399                         instance_data->period = *period;
1400                         instance_data->periodic_timer = g_timeout_add_seconds(
1401                                         instance_data->period,
1402                                         __timeout_cb, instance_data);
1403                 }
1404         }
1405
1406         if (content_info)
1407                 bundle_free(content_info);
1408 }
1409
1410 static void __multiwindow_instance_resume(
1411                 appcore_multiwindow_base_instance_h instance_h,
1412                 void *class_data)
1413 {
1414         const char *id;
1415         const char *class_id;
1416         widget_base_class *cls;
1417         widget_base_instance_data *data;
1418
1419         appcore_multiwindow_base_class_on_resume(instance_h);
1420         id = appcore_multiwindow_base_instance_get_id(instance_h);
1421         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1422         cls = __get_class(class_id);
1423         if (cls == NULL) {
1424                 LOGE("class not found: %s", class_id);
1425                 return;
1426         }
1427
1428         data = (widget_base_instance_data *)
1429                         appcore_multiwindow_base_instance_get_extra(instance_h);
1430
1431         if (data->pending_update) {
1432                 LOGD("pending update!");
1433                 data->pending_update = false;
1434                 __call_update_cb(class_id, data->id, 0, data->pending_content);
1435                 if (data->period > 0) {
1436                         LOGD("Restart timer!");
1437                         data->periodic_timer = g_timeout_add_seconds(
1438                                         data->period,
1439                                         __timeout_cb, data);
1440                 }
1441         }
1442
1443         if (cls->ops.resume)
1444                 cls->ops.resume(instance_h, class_data);
1445
1446         LOGD("%s is resumed", id);
1447         __send_update_status(class_id, id,
1448                 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
1449
1450         if (!__fg_signal) {
1451                 LOGD("Send fg signal to resourceD");
1452                 aul_widget_instance_change_status(class_id, STATUS_FOREGROUND);
1453                 __fg_signal = true;
1454         }
1455 }
1456
1457 static void __multiwindow_instance_pause(
1458                 appcore_multiwindow_base_instance_h instance_h,
1459                 void *class_data)
1460 {
1461         const char *id;
1462         const char *class_id;
1463         widget_base_class *cls;
1464
1465         appcore_multiwindow_base_class_on_pause(instance_h);
1466         id = appcore_multiwindow_base_instance_get_id(instance_h);
1467         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1468         cls = __get_class(class_id);
1469         if (cls == NULL) {
1470                 LOGE("class not found: %s", class_id);
1471                 return;
1472         }
1473
1474         if (cls->ops.pause)
1475                 cls->ops.pause(instance_h, class_data);
1476
1477         LOGD("%s is paused", id);
1478         __send_update_status(class_id, id,
1479                 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
1480
1481         if (__fg_signal) {
1482                 LOGD("Send bg signal to resourceD");
1483                 aul_widget_instance_change_status(class_id, STATUS_BACKGROUND);
1484                 __fg_signal = false;
1485         }
1486 }
1487
1488 static void __multiwindow_instance_terminate(
1489                 appcore_multiwindow_base_instance_h instance_h,
1490                 void *class_data)
1491 {
1492         widget_base_instance_data *data;
1493         bundle *b;
1494         char *operation = NULL;
1495         bundle *content_info;
1496         widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1497         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1498         const char *id;
1499         const char *class_id;
1500         widget_base_class *cls;
1501
1502         id = appcore_multiwindow_base_instance_get_id(instance_h);
1503         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1504         data  = appcore_multiwindow_base_instance_get_extra(
1505                         (appcore_multiwindow_base_instance_h)instance_h);
1506         b = data->args;
1507         cls = __get_class(class_id);
1508         if (cls == NULL) {
1509                 LOGE("class not found: %s", class_id);
1510                 return;
1511         }
1512
1513         if (b) {
1514                 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1515                 if (operation && strcmp(operation, "destroy") == 0)
1516                         reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1517         }
1518
1519         if (data->content)
1520                 content_info = bundle_decode((const bundle_raw *)data->content,
1521                                 strlen(data->content));
1522         else
1523                 content_info = bundle_create();
1524
1525         if (cls->ops.destroy)
1526                 cls->ops.destroy(instance_h, reason, content_info, class_data);
1527
1528         LOGD("%s is destroyed %d", id, reason);
1529         if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1530                 __is_permanent = true;
1531                 event = WIDGET_INSTANCE_EVENT_DESTROY;
1532                 aul_widget_instance_del(class_id, id);
1533         } else {
1534                 __is_permanent = false;
1535                 __send_update_status(class_id, id,
1536                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0,
1537                                 content_info);
1538         }
1539
1540         if (content_info)
1541                 bundle_free(content_info);
1542
1543         if (data->periodic_timer)
1544                 g_source_remove(data->periodic_timer);
1545
1546         __send_update_status(class_id, id, event, 0, NULL);
1547         appcore_multiwindow_base_class_on_terminate(instance_h);
1548 }
1549
1550 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1551                 bundle *content, int w, int h)
1552 {
1553         appcore_multiwindow_base_class_on_create(instance_h);
1554
1555         return 0;
1556 }
1557
1558 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1559 {
1560         appcore_multiwindow_base_class_on_pause(instance_h);
1561
1562         return 0;
1563 }
1564
1565 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1566 {
1567         appcore_multiwindow_base_class_on_resume(instance_h);
1568
1569         return 0;
1570 }
1571
1572 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1573                 int w, int h)
1574 {
1575         return 0;
1576 }
1577
1578 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1579                 bundle *content, int force)
1580 {
1581         return 0;
1582 }
1583
1584 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1585                 widget_base_destroy_type_e reason, bundle *content)
1586 {
1587         appcore_multiwindow_base_class_on_terminate(instance_h);
1588
1589         return 0;
1590 }
1591
1592 EXPORT_API widget_base_class widget_base_class_get_default(void)
1593 {
1594         widget_base_class cls;
1595
1596         cls.ops.create = __class_on_create;
1597         cls.ops.resize = __class_on_resize;
1598         cls.ops.update = __class_on_update;
1599         cls.ops.destroy = __class_on_destroy;
1600         cls.ops.pause = __class_on_pause;
1601         cls.ops.resume = __class_on_resume;
1602         cls.id = NULL;
1603
1604         return cls;
1605 }
1606
1607 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1608                 const char *class_id, void *class_data)
1609 {
1610         widget_base_class *c;
1611         appcore_multiwindow_base_class raw_cls;
1612
1613         if (!__is_widget_feature_enabled()) {
1614                 LOGE("not supported");
1615                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1616                 return NULL;
1617         }
1618
1619         if (!class_id) {
1620                 LOGE("class is is NULL");
1621                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1622                 return NULL;
1623         }
1624
1625         raw_cls.id = strdup(class_id);
1626         raw_cls.data = class_data;
1627         raw_cls.create = __multiwindow_instance_create;
1628         raw_cls.terminate = __multiwindow_instance_terminate;
1629         raw_cls.pause = __multiwindow_instance_pause;
1630         raw_cls.resume = __multiwindow_instance_resume;
1631         appcore_multiwindow_base_class_add(raw_cls);
1632
1633         c = malloc(sizeof(widget_base_class));
1634         if (!c)
1635                 return NULL;
1636
1637         *c = cls;
1638         c->id = strdup(class_id);
1639         __context.classes = g_list_append(__context.classes, c);
1640
1641         return c;
1642 }