Remove unused codes
[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 <screen_connector_provider.h>
35 #include <appcore_multiwindow_base.h>
36
37 #include "widget_base.h"
38
39 #ifdef LOG_TAG
40 #undef LOG_TAG
41 #endif
42
43 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
44 #define APP_TYPE_WIDGET "widgetapp"
45 #define STATUS_FOREGROUND "fg"
46 #define STATUS_BACKGROUND "bg"
47
48 static int __app_event_converter[APPCORE_BASE_EVENT_MAX] = {
49         [APP_EVENT_LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
50         [APP_EVENT_LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
51         [APP_EVENT_LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
52         [APP_EVENT_DEVICE_ORIENTATION_CHANGED]
53                         = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
54         [APP_EVENT_REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
55         [APP_EVENT_SUSPENDED_STATE_CHANGED]
56                         = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
57 };
58
59 struct app_event_info {
60         app_event_type_e type;
61         void *value;
62 };
63
64 struct app_event_handler {
65         app_event_type_e type;
66         app_event_cb cb;
67         void *data;
68         void *raw;
69 };
70
71 struct widget_foreach_context {
72         widget_base_instance_cb callback;
73         void *data;
74 };
75
76 typedef struct _widget_base_context {
77         widget_base_ops ops;
78         void *data;
79         int argc;
80         char **argv;
81         GList *classes;
82 } widget_base_context;
83
84 typedef struct _widget_base_instance_data {
85         bundle *args;
86         char *id;
87         char *content;
88         void *tag;
89         double period;
90         guint periodic_timer;
91         bool pending_update;
92         char *pending_content;
93         void *user_data;
94 } widget_base_instance_data;
95
96 static widget_base_context __context;
97 static char *__appid;
98 static char *__package_id;
99 static bool __fg_signal;
100 static char *__viewer_endpoint;
101 static bool __is_permanent;
102 static void __call_update_cb(const char *class_id, const char *id, int force,
103                 const char *content_raw);
104
105 static gboolean __timeout_cb(gpointer user_data)
106 {
107         widget_base_instance_data *data =
108                         (widget_base_instance_data *)user_data;
109         appcore_multiwindow_base_instance_h cxt;
110         const char *class_id;
111
112         cxt = appcore_multiwindow_base_instance_find(data->id);
113
114         if (!cxt) {
115                 LOGE("Can't find the instance");
116                 return G_SOURCE_REMOVE;
117         }
118
119         if (appcore_multiwindow_base_instance_is_resumed(cxt)) {
120                 LOGD("Periodic update!");
121                 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
122                 __call_update_cb(class_id, data->id, 0, NULL);
123         } else {
124                 data->pending_update = true;
125                 if (data->periodic_timer) {
126                         LOGD("Remove timer!");
127                         g_source_remove(data->periodic_timer);
128                         data->periodic_timer = 0;
129                 }
130         }
131
132         return G_SOURCE_CONTINUE;
133 }
134
135 static bool __is_widget_feature_enabled(void)
136 {
137         static bool feature = false;
138         static bool retrieved = false;
139         int ret;
140
141         if (retrieved == true)
142                 return feature;
143
144         ret = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
145         if (ret != SYSTEM_INFO_ERROR_NONE) {
146                 LOGE("failed to get system info"); /* LCOV_EXCL_LINE */
147                 return false; /* LCOV_EXCL_LINE */
148         }
149
150         retrieved = true;
151
152         return feature;
153 }
154
155 static void __check_empty_instance(void)
156 {
157         int cnt = appcore_multiwindow_base_instance_get_cnt();
158
159         if (cnt == 0)
160                 widget_base_exit();
161 }
162
163 static void __instance_drop(appcore_multiwindow_base_instance_h instance_h)
164 {
165         widget_base_instance_data *data;
166
167         data = appcore_multiwindow_base_instance_get_extra(instance_h);
168         appcore_multiwindow_base_instance_drop(instance_h);
169         free(data->pending_content);
170         free(data->content);
171         free(data->id);
172         free(data);
173         __check_empty_instance();
174 }
175
176 static gint __comp_class(gconstpointer a, gconstpointer b)
177 {
178         const widget_base_class *cls = a;
179
180         return strcmp(cls->id, b);
181 }
182
183 static widget_base_class *__get_class(const char *class_id)
184 {
185         widget_base_class *cls;
186         GList *class_node;
187
188         class_node = g_list_find_custom(__context.classes, class_id,
189                         __comp_class);
190         if (class_node == NULL) {
191                 LOGE("empty classes");
192                 return NULL;
193         }
194         cls = (widget_base_class *)class_node->data;
195
196         return cls;
197 }
198
199 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
200         int status)
201 {
202         bundle *b = bundle_create();
203         int ret;
204
205         if (b == NULL) {
206                 LOGE("out of memory"); /* LCOV_EXCL_LINE */
207                 return -1; /* LCOV_EXCL_LINE */
208         }
209
210         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
211         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
212         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
213         bundle_add_str(b, AUL_K_PKGID, __package_id);
214
215         LOGD("send lifecycle %s(%d)", instance_id, status);
216         ret = aul_app_com_send("widget.status", b);
217         if (ret < 0)
218                 LOGE("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
219
220         bundle_free(b);
221
222         return ret;
223 }
224
225 static int __send_update_status(const char *class_id, const char *instance_id,
226         int status, int err, bundle *extra)
227 {
228         bundle *b;
229         int lifecycle = -1;
230         bundle_raw *raw = NULL;
231         int len;
232         char err_str[256];
233
234         b = bundle_create();
235         if (!b) {
236                 LOGE("out of memory"); /* LCOV_EXCL_LINE */
237                 return -1; /* LCOV_EXCL_LINE */
238         }
239
240         if (err < 0) {
241                 snprintf(err_str, sizeof(err_str), "%d", err);
242                 bundle_add_str(b, AUL_K_WIDGET_ERROR_CODE, err_str);
243         }
244
245         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
246         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
247         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
248
249         if (extra) {
250                 bundle_encode(extra, &raw, &len);
251                 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
252                 aul_widget_instance_add(class_id, instance_id);
253         }
254
255         LOGD("send update %s(%d) to %s", instance_id, status, __viewer_endpoint);
256         aul_app_com_send(__viewer_endpoint, b);
257
258         switch (status) {
259         case WIDGET_INSTANCE_EVENT_CREATE:
260                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
261                 break;
262         case WIDGET_INSTANCE_EVENT_DESTROY:
263                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
264                 break;
265         case WIDGET_INSTANCE_EVENT_PAUSE:
266                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
267                 break;
268         case WIDGET_INSTANCE_EVENT_RESUME:
269                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
270                 break;
271         }
272
273         if (lifecycle > -1)
274                 __send_lifecycle_event(class_id, instance_id, lifecycle);
275
276         bundle_free(b);
277         if (raw)
278                 free(raw);
279
280         return 0;
281 }
282
283 static void __control_create(const char *class_id, const char *id, bundle *b)
284 {
285         widget_base_instance_data *data;
286         char *content = NULL;
287
288         if (appcore_multiwindow_base_instance_find(id)) {
289                 LOGE("Already exist id (%s)", id);
290                 return;
291         }
292
293         data = (widget_base_instance_data *)
294                         calloc(1, sizeof(widget_base_instance_data));
295         if (!data) {
296                 LOGE("Out of memory");
297                 return;
298         }
299
300         data->id = strdup(id);
301         data->args = b;
302
303         /* call stub create */
304         appcore_multiwindow_base_instance_run(class_id, id, data);
305         data->args = NULL;
306         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
307         if (content)
308                 data->content = strdup(content);
309
310 }
311
312 static void __control_resume(const char *class_id, const char *id, bundle *b)
313 {
314         appcore_multiwindow_base_instance_h cxt;
315
316         cxt = appcore_multiwindow_base_instance_find(id);
317         if (!cxt) {
318                 LOGE("context not found: %s", id);
319                 return;
320         }
321
322         /* call stub resume */
323         appcore_multiwindow_base_instance_resume(cxt);
324 }
325
326 static void __control_pause(const char *class_id, const char *id, bundle *b)
327 {
328         appcore_multiwindow_base_instance_h instance_h;
329
330         instance_h = appcore_multiwindow_base_instance_find(id);
331
332         if (!instance_h) {
333                 LOGE("instance not found: %s", id);
334                 return;
335         }
336
337         /* call stub pause */
338         appcore_multiwindow_base_instance_pause(instance_h);
339 }
340
341 static void __control_resize(const char *class_id, const char *id, bundle *b)
342 {
343         appcore_multiwindow_base_instance_h instance_h;
344         char *remain = NULL;
345         char *w_str = NULL;
346         char *h_str = NULL;
347         int w = 0;
348         int h = 0;
349         void *class_data;
350         widget_base_class *cls;
351         const appcore_multiwindow_base_class *raw_cls;
352
353         instance_h = appcore_multiwindow_base_instance_find(id);
354         if (!instance_h) {
355                 LOGE("context not found: %s", id);
356                 return;
357         }
358
359         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
360         if (!raw_cls)
361                 return;
362
363         cls = __get_class(class_id);
364         if (cls == NULL) {
365                 LOGE("class not found: %s", class_id);
366                 return;
367         }
368         class_data = raw_cls->data;
369         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
370         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
371
372         if (w_str)
373                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
374
375         if (h_str)
376                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
377
378         if (cls->ops.resize)
379                 cls->ops.resize(instance_h, w, h, class_data);
380
381         LOGD("%s is resized to %dx%d", id, w, h);
382         __send_update_status(class_id, id,
383                 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, 0, NULL);
384 }
385
386 static void __call_update_cb(const char *class_id, const char *id, int force,
387                 const char *content_raw)
388 {
389         void *class_data;
390         widget_base_class *cls;
391         const appcore_multiwindow_base_class *raw_cls;
392         appcore_multiwindow_base_instance_h instance_h;
393         bundle *content = NULL;
394
395         instance_h = appcore_multiwindow_base_instance_find(id);
396         if (!instance_h) {
397                 LOGE("context not found: %s", id);
398                 return;
399         }
400
401         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
402         if (!raw_cls) {
403                 LOGE("class is NULL");
404                 return;
405         }
406
407         class_data = raw_cls->data;
408         cls = __get_class(class_id);
409         if (cls == NULL) {
410                 LOGE("class not found: %s", class_id);
411                 return;
412         }
413
414         if (!cls->ops.update) {
415                 LOGE("update callback is NULL");
416                 return;
417         }
418
419         if (content_raw) {
420                 content = bundle_decode((const bundle_raw *)content_raw,
421                                 strlen(content_raw));
422         }
423
424         if (cls->ops.update)
425                 cls->ops.update(instance_h, content, force, class_data);
426
427         __send_update_status(class_id, id,
428                 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
429         LOGD("updated:%s", id);
430
431         if (content)
432                 bundle_free(content);
433 }
434
435 static void __update_pending_content(
436                 appcore_multiwindow_base_instance_h instance_h,
437                 const char *content_raw)
438 {
439         widget_base_instance_data *data;
440
441         data = (widget_base_instance_data *)
442                         appcore_multiwindow_base_instance_get_extra(instance_h);
443
444         if (data->pending_content) {
445                 free(data->pending_content);
446                 data->pending_content = NULL;
447         }
448
449         if (content_raw) {
450                 data->pending_content = strdup(content_raw);
451                 if (data->pending_content == NULL)
452                         LOGW("Out of memory");
453         }
454
455         data->pending_update = true;
456 }
457
458 static void __update_process(const char *class_id, const char *id,
459                 appcore_multiwindow_base_instance_h instance_h, void *data)
460 {
461         char *content_raw = NULL;
462         char *force_str = NULL;
463         int force;
464         bundle *b = data;
465
466         if (!b) {
467                 LOGE("bundle is NULL");
468                 return;
469         }
470
471         bundle_get_str(b, WIDGET_K_FORCE, &force_str);
472
473         if (force_str && strcmp(force_str, "true") == 0)
474                 force = 1;
475         else
476                 force = 0;
477
478         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
479         if (!appcore_multiwindow_base_instance_is_resumed(instance_h) && !force)
480                 __update_pending_content(instance_h, content_raw);
481         else
482                 __call_update_cb(class_id, id, force, content_raw);
483 }
484
485 static void __control_update(const char *class_id, const char *id, bundle *b)
486 {
487         appcore_multiwindow_base_instance_h instance_h;
488
489         if (!id) {
490                 appcore_multiwindow_base_instance_foreach(class_id,
491                                 __update_process, b);
492                 return;
493         }
494
495         instance_h = appcore_multiwindow_base_instance_find(id);
496         if (!instance_h) {
497                 LOGE("context not found: %s", id);
498                 return;
499         }
500
501         __update_process(class_id, id, instance_h, b);
502 }
503
504 static void __control_destroy(const char *class_id, const char *id, bundle *b)
505 {
506         appcore_multiwindow_base_instance_h instance_h;
507         widget_base_instance_data *data;
508
509         instance_h = appcore_multiwindow_base_instance_find(id);
510         if (!instance_h) {
511                 LOGE("could not find widget obj: %s, clear amd info", id);
512                 aul_widget_instance_del(class_id, id);
513                 return;
514         }
515
516         data = (widget_base_instance_data *)
517                         appcore_multiwindow_base_instance_get_extra(instance_h);
518         data->args = b;
519
520         /* call stub terminate */
521         appcore_multiwindow_base_instance_exit(instance_h);
522         free(data->pending_content);
523         free(data->content);
524         free(data->id);
525         free(data);
526         __check_empty_instance();
527 }
528
529 static void __control_change_period(const char *class_id, const char *id,
530                 bundle *b)
531 {
532         appcore_multiwindow_base_instance_h instance_h;
533         widget_base_instance_data *data;
534         double *period = NULL;
535         size_t size;
536         int ret;
537
538         instance_h = appcore_multiwindow_base_instance_find(id);
539         if (!instance_h) {
540                 LOGE("context not found: %s", id);
541                 return;
542         }
543
544         data = (widget_base_instance_data *)
545                         appcore_multiwindow_base_instance_get_extra(instance_h);
546
547         if (!data) {
548                 LOGE("could not find instance data: %s", id);
549                 return;
550         }
551
552         if (data->periodic_timer) {
553                 LOGD("Remove timer!");
554                 g_source_remove(data->periodic_timer);
555                 data->periodic_timer = 0;
556         }
557
558         ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
559         if (ret == BUNDLE_ERROR_NONE)
560                 data->period = *period;
561
562         if (data->period > 0) {
563                 LOGD("Restart timer!");
564                 data->periodic_timer = g_timeout_add_seconds(data->period,
565                                 __timeout_cb, data);
566         }
567
568         return;
569 }
570
571 static int __multiwindow_create(void *data)
572 {
573         char pkgid[256] = {0, };
574         int ret = 0;
575
576         appcore_multiwindow_base_on_create();
577         app_get_id(&__appid);
578         if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
579                 __package_id = strdup(pkgid);
580
581         if (!__package_id || !__appid) {
582                 LOGE("__package_id is NULL");
583                 return -1;
584         }
585
586         screen_connector_provider_init();
587         if (__context.ops.create)
588                 ret = __context.ops.create(data);
589
590         LOGD("widget base is created");
591         return ret;
592 }
593
594 static int __multiwindow_terminate(void *data)
595 {
596         if (__context.ops.terminate)
597                 __context.ops.terminate(data);
598         screen_connector_provider_fini();
599
600         if (__viewer_endpoint) {
601                 free(__viewer_endpoint);
602                 __viewer_endpoint = NULL;
603         }
604
605         if (__package_id) {
606                 free(__package_id);
607                 __package_id = NULL;
608         }
609
610         if (__appid) {
611                 free(__appid);
612                 __appid = NULL;
613         }
614
615         appcore_multiwindow_base_on_terminate();
616
617         LOGD("widget base is terminated");
618         return 0;
619 }
620
621 static int __multiwindow_control(bundle *b, void *data)
622 {
623         char *class_id = NULL;
624         char *id = NULL;
625         char *operation = NULL;
626
627         appcore_multiwindow_base_on_control(b);
628         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
629         /* for previous version compatibility, use appid for default class id */
630         if (class_id == NULL)
631                 class_id = __appid;
632
633         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
634         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
635
636         if (!operation) {
637                 LOGE("operation is NULL");
638                 return 0;
639         }
640
641         if (strcmp(operation, "create") == 0)
642                 __control_create(class_id, id, b);
643         else if (strcmp(operation, "resize") == 0)
644                 __control_resize(class_id, id, b);
645         else if (strcmp(operation, "update") == 0)
646                 __control_update(class_id, id, b);
647         else if (strcmp(operation, "destroy") == 0)
648                 __control_destroy(class_id, id, b);
649         else if (strcmp(operation, "resume") == 0)
650                 __control_resume(class_id, id, b);
651         else if (strcmp(operation, "pause") == 0)
652                 __control_pause(class_id, id, b);
653         else if (strcmp(operation, "terminate") == 0)
654                 __control_destroy(class_id, id, b);
655         else if (strcmp(operation, "period") == 0)
656                 __control_change_period(class_id, id, b);
657
658         return 0;
659 }
660
661 static void __inst_resume_cb(const char *class_id, const char *id,
662                 appcore_multiwindow_base_instance_h cxt, void *data)
663 {
664         __control_resume(class_id, id, data);
665 }
666
667 static void __get_content(bundle *b)
668 {
669         char *instance_id = NULL;
670         appcore_multiwindow_base_instance_h cxt;
671         widget_base_instance_data * we;
672
673         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
674         if (!instance_id) {
675                 LOGE("instance id is NULL");
676                 return;
677         }
678
679         cxt = appcore_multiwindow_base_instance_find(instance_id);
680         if (!cxt) {
681                 LOGE("could not find widget obj: %s", instance_id);
682                 return;
683         }
684
685         we = appcore_multiwindow_base_instance_get_extra(cxt);
686         if (!we) {
687                 LOGE("widget extra is NULL");
688                 return;
689         }
690
691         if (we->content) {
692                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
693                 LOGD("content info of %s found", instance_id);
694         } else {
695                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
696                 LOGD("empty content info added");
697         }
698 }
699
700 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
701 {
702         appcore_multiwindow_base_on_receive(type, b);
703
704         switch (type) {
705         case AUL_RESUME:
706                 appcore_multiwindow_base_instance_foreach_full(
707                                 __inst_resume_cb, b);
708                 break;
709         case AUL_TERMINATE:
710                 widget_base_exit();
711                 break;
712         case AUL_WIDGET_CONTENT:
713                 __get_content(b);
714                 break;
715         default:
716                 break;
717         }
718
719         return 0;
720 }
721
722 static void __multiwindow_init(int argc, char **argv, void *data)
723 {
724         if (__context.ops.init)
725                 __context.ops.init(argc, argv, data);
726 }
727
728 static void __multiwindow_finish(void)
729 {
730         if (__context.ops.finish) {
731                 __context.ops.finish();
732                 /* Check Loader case */
733                 if (getenv("AUL_LOADER_INIT")) {
734                         unsetenv("AUL_LOADER_INIT");
735                         __context.ops.finish();
736                 }
737         }
738 }
739
740 static void __multiwindow_run(void *data)
741 {
742         if (__context.ops.run)
743                 __context.ops.run(data);
744 }
745
746 static void __multiwindow_exit(void *data)
747 {
748         if (__context.ops.exit)
749                 __context.ops.exit(data);
750 }
751
752 static void __multiwindow_trim_memory(void *data)
753 {
754         if (__context.ops.trim_memory)
755                 __context.ops.trim_memory(data);
756 }
757
758 EXPORT_API int widget_base_exit(void)
759 {
760         int ret;
761
762         appcore_multiwindow_base_exit();
763         if (appcore_multiwindow_base_instance_get_cnt() == 0 && __is_permanent) {
764                 ret = aul_notify_exit();
765                 aul_widget_write_log(LOG_TAG, "[%s:%d] permanent exit : %d",
766                         __FUNCTION__, __LINE__, ret);
767         }
768
769         return 0;
770 }
771
772 static gboolean __finish_event_cb(gpointer user_data)
773 {
774         appcore_multiwindow_base_instance_h cxt = user_data;
775         bundle *b;
776         const char *id;
777         const char *class_id;
778
779         if (!cxt) {
780                 LOGE("user_data is NULL");
781                 return FALSE;
782         }
783
784         id = appcore_multiwindow_base_instance_get_id(cxt);
785         class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
786         b = bundle_create();
787
788         if (!b) {
789                 LOGE("Out-of-memory");
790                 return FALSE;
791         }
792
793         bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
794         __control_destroy(class_id, id, b);
795         bundle_free(b);
796
797         return FALSE;
798 }
799
800 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
801 {
802         if (!__is_widget_feature_enabled()) {
803                 LOGE("not supported"); /* LCOV_EXCL_LINE */
804                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
805         }
806
807         if (!context) {
808                 LOGE("context is null");
809                 return WIDGET_ERROR_INVALID_PARAMETER;
810         }
811
812         g_idle_add(__finish_event_cb, context);
813
814         return WIDGET_ERROR_NONE;
815 }
816
817 static void __inst_full_cb(const char *class_id, const char *id,
818                 appcore_multiwindow_base_instance_h cxt, void *data)
819 {
820         struct widget_foreach_context *foreach_context = data;
821
822         if (!data)
823                 return;
824
825         if (foreach_context->callback)
826                 foreach_context->callback(cxt, foreach_context->data);
827 }
828
829 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
830 {
831         struct widget_foreach_context foreach_context;
832
833         if (!__is_widget_feature_enabled()) {
834                 LOGE("not supported"); /* LCOV_EXCL_LINE */
835                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
836         }
837
838         if (!cb) {
839                 LOGE("callback is NULL");
840                 return WIDGET_ERROR_INVALID_PARAMETER;
841         }
842
843         foreach_context.callback = cb;
844         foreach_context.data = data;
845         appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
846
847         return WIDGET_ERROR_NONE;
848 }
849
850 static int __event_cb(void *event, void *data)
851 {
852         app_event_handler_h handler = data;
853
854         struct app_event_info app_event;
855
856         app_event.type = handler->type;
857         app_event.value = event;
858
859         if (handler->cb)
860                 handler->cb(&app_event, handler->data);
861
862         return 0;
863 }
864
865 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
866                                         app_event_type_e event_type,
867                                         app_event_cb callback,
868                                         void *user_data)
869 {
870         int r;
871         bool feature;
872         app_event_handler_h handler;
873
874         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
875         if (r < 0)
876                 return WIDGET_BASE_ERROR_FAULT;
877
878         if (!feature)
879                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
880
881         if (event_handler == NULL || callback == NULL)
882                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
883
884         if (event_type < APP_EVENT_LOW_MEMORY
885             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
886                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
887
888         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
889                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
890
891
892         handler = calloc(1, sizeof(struct app_event_handler));
893         if (!handler)
894                 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
895
896         handler->type = event_type;
897         handler->cb = callback;
898         handler->data = user_data;
899         handler->raw = appcore_base_add_event(
900                         __app_event_converter[event_type], __event_cb, handler);
901         *event_handler = handler;
902
903         return WIDGET_BASE_ERROR_NONE;
904 }
905
906 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
907                                                 event_handler)
908 {
909         int r;
910         bool feature;
911         app_event_type_e type;
912
913         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
914         if (r < 0)
915                 return WIDGET_BASE_ERROR_FAULT;
916
917         if (!feature)
918                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
919
920         if (event_handler == NULL)
921                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
922
923         type = event_handler->type;
924         if (type < APP_EVENT_LOW_MEMORY ||
925                         type > APP_EVENT_REGION_FORMAT_CHANGED)
926                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
927
928         r = appcore_base_remove_event(event_handler->raw);
929         if (r < 0)
930                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
931
932         free(event_handler);
933
934         return WIDGET_BASE_ERROR_NONE;
935 }
936
937 EXPORT_API int widget_base_context_set_content_info(
938                 widget_base_instance_h context,
939                 bundle *content_info)
940 {
941         int ret = 0;
942         bundle_raw *raw = NULL;
943         int len;
944         const char *id;
945         const char *class_id;
946         widget_base_instance_data *data;
947         appcore_multiwindow_base_instance_h instance_h;
948
949         if (!__is_widget_feature_enabled()) {
950                 LOGE("not supported"); /* LCOV_EXCL_LINE */
951                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
952         }
953
954         if (!context || !content_info)
955                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
956
957         instance_h = (appcore_multiwindow_base_instance_h)context;
958         id = appcore_multiwindow_base_instance_get_id(instance_h);
959         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
960         data = appcore_multiwindow_base_instance_get_extra(instance_h);
961
962         if (!class_id || !id || !data)
963                 return WIDGET_BASE_ERROR_FAULT;
964
965         ret = __send_update_status(class_id, id,
966                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
967
968         if (data->content)
969                 free(data->content);
970
971         bundle_encode(content_info, &raw, &len);
972         if (raw)
973                 data->content = strdup((const char *)raw);
974         else
975                 data->content = NULL;
976
977         free(raw);
978         if (ret < 0) {
979                 /* LCOV_EXCL_START */
980                 LOGE("failed to send content info: %s of %s (%d)", id,
981                                 class_id, ret);
982                 return WIDGET_BASE_ERROR_IO_ERROR;
983                 /* LCOV_EXCL_STOP */
984         }
985
986         return WIDGET_BASE_ERROR_NONE;
987 }
988
989 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
990 {
991         appcore_multiwindow_base_instance_h instance_h;
992         widget_base_instance_data *data;
993
994         if (!__is_widget_feature_enabled()) {
995                 LOGE("not supported"); /* LCOV_EXCL_LINE */
996                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
997         }
998
999         if (!context || !tag) {
1000                 LOGE("Invalid parameter");
1001                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1002         }
1003
1004         instance_h = (appcore_multiwindow_base_instance_h)context;
1005         data = (widget_base_instance_data *)
1006                         appcore_multiwindow_base_instance_get_extra(instance_h);
1007
1008         if (!data) {
1009                 LOGE("Invalid parameter");
1010                 return WIDGET_ERROR_INVALID_PARAMETER;
1011         }
1012
1013         *tag = data->tag;
1014
1015         return WIDGET_BASE_ERROR_NONE;
1016 }
1017
1018 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
1019 {
1020         appcore_multiwindow_base_instance_h instance_h;
1021         widget_base_instance_data *data;
1022
1023         if (!__is_widget_feature_enabled()) {
1024                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1025                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1026         }
1027
1028         if (!context) {
1029                 LOGE("Invalid parameter");
1030                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1031         }
1032
1033         instance_h = (appcore_multiwindow_base_instance_h)context;
1034         data = (widget_base_instance_data *)
1035                         appcore_multiwindow_base_instance_get_extra(instance_h);
1036         data->tag = tag;
1037
1038         return WIDGET_BASE_ERROR_NONE;
1039 }
1040
1041 EXPORT_API void *widget_base_context_get_user_data(
1042                 widget_base_instance_h context)
1043 {
1044         appcore_multiwindow_base_instance_h instance_h;
1045         widget_base_instance_data *data;
1046
1047         if (!__is_widget_feature_enabled()) {
1048                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1049                 return NULL; /* LCOV_EXCL_LINE */
1050         }
1051
1052         if (!context) {
1053                 LOGE("Invalid parameter");
1054                 return NULL;
1055         }
1056
1057         instance_h = (appcore_multiwindow_base_instance_h)context;
1058         data = (widget_base_instance_data *)
1059                         appcore_multiwindow_base_instance_get_extra(instance_h);
1060
1061         return data->user_data;
1062 }
1063
1064
1065 EXPORT_API int widget_base_context_set_user_data(
1066                 widget_base_instance_h context, void *user_data)
1067 {
1068         appcore_multiwindow_base_instance_h instance_h;
1069         widget_base_instance_data *data;
1070
1071         if (!__is_widget_feature_enabled()) {
1072                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1073                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1074         }
1075
1076         if (!context) {
1077                 LOGE("Invalid parameter");
1078                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1079         }
1080
1081         instance_h = (appcore_multiwindow_base_instance_h)context;
1082         data = (widget_base_instance_data *)
1083                         appcore_multiwindow_base_instance_get_extra(instance_h);
1084         data->user_data = user_data;
1085
1086         return WIDGET_BASE_ERROR_NONE;
1087 }
1088
1089 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1090 {
1091         appcore_multiwindow_base_instance_h instance_h;
1092
1093         if (!__is_widget_feature_enabled()) {
1094                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1095                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1096         }
1097
1098         instance_h = (appcore_multiwindow_base_instance_h)context;
1099         *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1100
1101         return WIDGET_BASE_ERROR_NONE;
1102 }
1103
1104 EXPORT_API const char *widget_base_get_viewer_endpoint()
1105 {
1106         return __viewer_endpoint;
1107 }
1108
1109 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1110                 void *data)
1111 {
1112         bundle *kb;
1113         char *viewer_endpoint = NULL;
1114         appcore_multiwindow_base_ops raw_ops
1115                         = appcore_multiwindow_base_get_default_ops();
1116
1117         __context.ops = ops;
1118         __context.argc = argc;
1119         __context.argv = argv;
1120         __context.data = data;
1121
1122         /* override methods */
1123         raw_ops.base.create = __multiwindow_create;
1124         raw_ops.base.control = __multiwindow_control;
1125         raw_ops.base.terminate = __multiwindow_terminate;
1126         raw_ops.base.receive = __multiwindow_receive;
1127         raw_ops.base.init = __multiwindow_init;
1128         raw_ops.base.finish = __multiwindow_finish;
1129         raw_ops.base.run = __multiwindow_run;
1130         raw_ops.base.exit = __multiwindow_exit;
1131         raw_ops.base.trim_memory = __multiwindow_trim_memory;
1132
1133         if (!__is_widget_feature_enabled()) {
1134                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1135                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1136         }
1137
1138         kb = bundle_import_from_argv(argc, argv);
1139         if (kb) {
1140                 bundle_get_str(kb, AUL_K_WIDGET_VIEWER, &viewer_endpoint);
1141                 if (viewer_endpoint) {
1142                         LOGD("viewer endpoint :%s", viewer_endpoint);
1143                         __viewer_endpoint = strdup(viewer_endpoint);
1144                 } else {
1145                         LOGE("endpoint is missing");
1146                 }
1147
1148                 bundle_free(kb);
1149         } else {
1150                 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1151                 return WIDGET_ERROR_FAULT;
1152         }
1153
1154         if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1155                return WIDGET_ERROR_FAULT;
1156
1157         return WIDGET_ERROR_NONE;
1158 }
1159
1160 static int __on_create(void *data)
1161 {
1162         return widget_base_on_create();
1163 }
1164
1165 static int __on_terminate(void *data)
1166 {
1167         return widget_base_on_terminate();
1168 }
1169
1170 static void __on_init(int argc, char **argv, void *data)
1171 {
1172         widget_base_on_init(argc, argv);
1173 }
1174
1175 static void __on_finish(void)
1176 {
1177         widget_base_on_finish();
1178 }
1179
1180 static void __on_run(void *data)
1181 {
1182         widget_base_on_run();
1183 }
1184
1185 static void __on_exit(void *data)
1186 {
1187         widget_base_on_exit();
1188 }
1189
1190 static void __on_trim_memory(void *data)
1191 {
1192         widget_base_on_trim_memory();
1193 }
1194
1195 EXPORT_API int widget_base_on_create(void)
1196 {
1197         appcore_multiwindow_base_on_create();
1198
1199         return 0;
1200 }
1201
1202 EXPORT_API int widget_base_on_terminate(void)
1203 {
1204         appcore_multiwindow_base_on_terminate();
1205
1206         return 0;
1207 }
1208
1209 EXPORT_API int widget_base_on_init(int argc, char **argv)
1210 {
1211         return 0;
1212 }
1213
1214 EXPORT_API void widget_base_on_finish(void)
1215 {
1216 }
1217
1218 EXPORT_API void widget_base_on_run(void)
1219 {
1220 }
1221
1222 EXPORT_API void widget_base_on_exit(void)
1223 {
1224 }
1225
1226 EXPORT_API int widget_base_on_trim_memory(void)
1227 {
1228         appcore_multiwindow_base_on_trim_memory();
1229
1230         return 0;
1231 }
1232
1233 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1234 {
1235         widget_base_ops ops;
1236
1237         /* override methods */
1238         ops.create = __on_create;
1239         ops.terminate = __on_terminate;
1240         ops.init = __on_init;
1241         ops.finish = __on_finish;
1242         ops.run = __on_run;
1243         ops.exit = __on_exit;
1244         ops.trim_memory = __on_trim_memory;
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_Wl2_Window *wl_win)
1267 {
1268         struct wl_surface *surface;
1269
1270         surface = ecore_wl2_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         LOGW("%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 }