Writing a file log for widget app exit
[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         if (appcore_multiwindow_base_instance_find(id)) {
306                 data->args = NULL;
307                 bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
308                 if (content)
309                         data->content = strdup(content);
310         }
311 }
312
313 static void __control_resume(const char *class_id, const char *id, bundle *b)
314 {
315         appcore_multiwindow_base_instance_h cxt;
316
317         cxt = appcore_multiwindow_base_instance_find(id);
318         if (!cxt) {
319                 LOGE("context not found: %s", id);
320                 return;
321         }
322
323         /* call stub resume */
324         appcore_multiwindow_base_instance_resume(cxt);
325 }
326
327 static void __control_pause(const char *class_id, const char *id, bundle *b)
328 {
329         appcore_multiwindow_base_instance_h instance_h;
330
331         instance_h = appcore_multiwindow_base_instance_find(id);
332
333         if (!instance_h) {
334                 LOGE("instance not found: %s", id);
335                 return;
336         }
337
338         /* call stub pause */
339         appcore_multiwindow_base_instance_pause(instance_h);
340 }
341
342 static void __control_resize(const char *class_id, const char *id, bundle *b)
343 {
344         appcore_multiwindow_base_instance_h instance_h;
345         char *remain = NULL;
346         char *w_str = NULL;
347         char *h_str = NULL;
348         int w = 0;
349         int h = 0;
350         void *class_data;
351         widget_base_class *cls;
352         const appcore_multiwindow_base_class *raw_cls;
353
354         instance_h = appcore_multiwindow_base_instance_find(id);
355         if (!instance_h) {
356                 LOGE("context not found: %s", id);
357                 return;
358         }
359
360         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
361         if (!raw_cls)
362                 return;
363
364         cls = __get_class(class_id);
365         if (cls == NULL) {
366                 LOGE("class not found: %s", class_id);
367                 return;
368         }
369         class_data = raw_cls->data;
370         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
371         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
372
373         if (w_str)
374                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
375
376         if (h_str)
377                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
378
379         if (cls->ops.resize)
380                 cls->ops.resize(instance_h, w, h, class_data);
381
382         LOGD("%s is resized to %dx%d", id, w, h);
383         __send_update_status(class_id, id,
384                 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, 0, NULL);
385 }
386
387 static void __call_update_cb(const char *class_id, const char *id, int force,
388                 const char *content_raw)
389 {
390         void *class_data;
391         widget_base_class *cls;
392         const appcore_multiwindow_base_class *raw_cls;
393         appcore_multiwindow_base_instance_h instance_h;
394         bundle *content = NULL;
395
396         instance_h = appcore_multiwindow_base_instance_find(id);
397         if (!instance_h) {
398                 LOGE("context not found: %s", id);
399                 return;
400         }
401
402         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
403         if (!raw_cls) {
404                 LOGE("class is NULL");
405                 return;
406         }
407
408         class_data = raw_cls->data;
409         cls = __get_class(class_id);
410         if (cls == NULL) {
411                 LOGE("class not found: %s", class_id);
412                 return;
413         }
414
415         if (!cls->ops.update) {
416                 LOGE("update callback is NULL");
417                 return;
418         }
419
420         if (content_raw) {
421                 content = bundle_decode((const bundle_raw *)content_raw,
422                                 strlen(content_raw));
423         }
424
425         if (cls->ops.update)
426                 cls->ops.update(instance_h, content, force, class_data);
427
428         __send_update_status(class_id, id,
429                 WIDGET_INSTANCE_EVENT_UPDATE, 0, NULL);
430         LOGD("updated:%s", id);
431
432         if (content)
433                 bundle_free(content);
434 }
435
436 static void __update_pending_content(
437                 appcore_multiwindow_base_instance_h instance_h,
438                 const char *content_raw)
439 {
440         widget_base_instance_data *data;
441
442         data = (widget_base_instance_data *)
443                         appcore_multiwindow_base_instance_get_extra(instance_h);
444
445         if (data->pending_content) {
446                 free(data->pending_content);
447                 data->pending_content = NULL;
448         }
449
450         if (content_raw) {
451                 data->pending_content = strdup(content_raw);
452                 if (data->pending_content == NULL)
453                         LOGW("Out of memory");
454         }
455
456         data->pending_update = true;
457 }
458
459 static void __update_process(const char *class_id, const char *id,
460                 appcore_multiwindow_base_instance_h instance_h, void *data)
461 {
462         char *content_raw = NULL;
463         char *force_str = NULL;
464         int force;
465         bundle *b = data;
466
467         if (!b) {
468                 LOGE("bundle is NULL");
469                 return;
470         }
471
472         bundle_get_str(b, WIDGET_K_FORCE, &force_str);
473
474         if (force_str && strcmp(force_str, "true") == 0)
475                 force = 1;
476         else
477                 force = 0;
478
479         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
480         if (!appcore_multiwindow_base_instance_is_resumed(instance_h) && !force)
481                 __update_pending_content(instance_h, content_raw);
482         else
483                 __call_update_cb(class_id, id, force, content_raw);
484 }
485
486 static void __control_update(const char *class_id, const char *id, bundle *b)
487 {
488         appcore_multiwindow_base_instance_h instance_h;
489
490         if (!id) {
491                 appcore_multiwindow_base_instance_foreach(class_id,
492                                 __update_process, b);
493                 return;
494         }
495
496         instance_h = appcore_multiwindow_base_instance_find(id);
497         if (!instance_h) {
498                 LOGE("context not found: %s", id);
499                 return;
500         }
501
502         __update_process(class_id, id, instance_h, b);
503 }
504
505 static void __control_destroy(const char *class_id, const char *id, bundle *b)
506 {
507         appcore_multiwindow_base_instance_h instance_h;
508         widget_base_instance_data *data;
509
510         instance_h = appcore_multiwindow_base_instance_find(id);
511         if (!instance_h) {
512                 LOGE("could not find widget obj: %s, clear amd info", id);
513                 aul_widget_instance_del(class_id, id);
514                 return;
515         }
516
517         data = (widget_base_instance_data *)
518                         appcore_multiwindow_base_instance_get_extra(instance_h);
519         data->args = b;
520
521         /* call stub terminate */
522         appcore_multiwindow_base_instance_exit(instance_h);
523         free(data->pending_content);
524         free(data->content);
525         free(data->id);
526         free(data);
527         __check_empty_instance();
528 }
529
530 static void __control_change_period(const char *class_id, const char *id,
531                 bundle *b)
532 {
533         appcore_multiwindow_base_instance_h instance_h;
534         widget_base_instance_data *data;
535         double *period = NULL;
536         size_t size;
537         int ret;
538
539         instance_h = appcore_multiwindow_base_instance_find(id);
540         if (!instance_h) {
541                 LOGE("context not found: %s", id);
542                 return;
543         }
544
545         data = (widget_base_instance_data *)
546                         appcore_multiwindow_base_instance_get_extra(instance_h);
547
548         if (!data) {
549                 LOGE("could not find instance data: %s", id);
550                 return;
551         }
552
553         if (data->periodic_timer) {
554                 LOGD("Remove timer!");
555                 g_source_remove(data->periodic_timer);
556                 data->periodic_timer = 0;
557         }
558
559         ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
560         if (ret == BUNDLE_ERROR_NONE)
561                 data->period = *period;
562
563         if (data->period > 0) {
564                 LOGD("Restart timer!");
565                 data->periodic_timer = g_timeout_add_seconds(data->period,
566                                 __timeout_cb, data);
567         }
568
569         return;
570 }
571
572 static int __multiwindow_create(void *data)
573 {
574         char pkgid[256] = {0, };
575         int ret = 0;
576
577         appcore_multiwindow_base_on_create();
578         app_get_id(&__appid);
579         if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
580                 __package_id = strdup(pkgid);
581
582         if (!__package_id || !__appid) {
583                 LOGE("__package_id is NULL");
584                 return -1;
585         }
586
587         screen_connector_provider_init();
588         if (__context.ops.create)
589                 ret = __context.ops.create(data);
590
591         LOGD("widget base is created");
592         return ret;
593 }
594
595 static int __multiwindow_terminate(void *data)
596 {
597         if (__context.ops.terminate)
598                 __context.ops.terminate(data);
599         screen_connector_provider_fini();
600
601         if (__viewer_endpoint) {
602                 free(__viewer_endpoint);
603                 __viewer_endpoint = NULL;
604         }
605
606         if (__package_id) {
607                 free(__package_id);
608                 __package_id = NULL;
609         }
610
611         if (__appid) {
612                 free(__appid);
613                 __appid = NULL;
614         }
615
616         appcore_multiwindow_base_on_terminate();
617
618         LOGD("widget base is terminated");
619         return 0;
620 }
621
622 static int __multiwindow_control(bundle *b, void *data)
623 {
624         char *class_id = NULL;
625         char *id = NULL;
626         char *operation = NULL;
627
628         appcore_multiwindow_base_on_control(b);
629         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
630         /* for previous version compatibility, use appid for default class id */
631         if (class_id == NULL)
632                 class_id = __appid;
633
634         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
635         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
636
637         if (!operation) {
638                 LOGE("operation is NULL");
639                 return 0;
640         }
641
642         if (strcmp(operation, "create") == 0)
643                 __control_create(class_id, id, b);
644         else if (strcmp(operation, "resize") == 0)
645                 __control_resize(class_id, id, b);
646         else if (strcmp(operation, "update") == 0)
647                 __control_update(class_id, id, b);
648         else if (strcmp(operation, "destroy") == 0)
649                 __control_destroy(class_id, id, b);
650         else if (strcmp(operation, "resume") == 0)
651                 __control_resume(class_id, id, b);
652         else if (strcmp(operation, "pause") == 0)
653                 __control_pause(class_id, id, b);
654         else if (strcmp(operation, "terminate") == 0)
655                 __control_destroy(class_id, id, b);
656         else if (strcmp(operation, "period") == 0)
657                 __control_change_period(class_id, id, b);
658
659         return 0;
660 }
661
662 static void __inst_resume_cb(const char *class_id, const char *id,
663                 appcore_multiwindow_base_instance_h cxt, void *data)
664 {
665         __control_resume(class_id, id, data);
666 }
667
668 static void __get_content(bundle *b)
669 {
670         char *instance_id = NULL;
671         appcore_multiwindow_base_instance_h cxt;
672         widget_base_instance_data * we;
673
674         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
675         if (!instance_id) {
676                 LOGE("instance id is NULL");
677                 return;
678         }
679
680         cxt = appcore_multiwindow_base_instance_find(instance_id);
681         if (!cxt) {
682                 LOGE("could not find widget obj: %s", instance_id);
683                 return;
684         }
685
686         we = appcore_multiwindow_base_instance_get_extra(cxt);
687         if (!we) {
688                 LOGE("widget extra is NULL");
689                 return;
690         }
691
692         if (we->content) {
693                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
694                 LOGD("content info of %s found", instance_id);
695         } else {
696                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
697                 LOGD("empty content info added");
698         }
699 }
700
701 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
702 {
703         appcore_multiwindow_base_on_receive(type, b);
704
705         switch (type) {
706         case AUL_RESUME:
707                 appcore_multiwindow_base_instance_foreach_full(
708                                 __inst_resume_cb, b);
709                 break;
710         case AUL_TERMINATE:
711                 widget_base_exit();
712                 break;
713         case AUL_WIDGET_CONTENT:
714                 __get_content(b);
715                 break;
716         default:
717                 break;
718         }
719
720         return 0;
721 }
722
723 static void __multiwindow_init(int argc, char **argv, void *data)
724 {
725         if (__context.ops.init)
726                 __context.ops.init(argc, argv, data);
727 }
728
729 static void __multiwindow_finish(void)
730 {
731         if (__context.ops.finish) {
732                 __context.ops.finish();
733                 /* Check Loader case */
734                 if (getenv("AUL_LOADER_INIT")) {
735                         unsetenv("AUL_LOADER_INIT");
736                         __context.ops.finish();
737                 }
738         }
739 }
740
741 static void __multiwindow_run(void *data)
742 {
743         if (__context.ops.run)
744                 __context.ops.run(data);
745 }
746
747 static void __multiwindow_exit(void *data)
748 {
749         if (__context.ops.exit)
750                 __context.ops.exit(data);
751 }
752
753 static void __multiwindow_trim_memory(void *data)
754 {
755         if (__context.ops.trim_memory)
756                 __context.ops.trim_memory(data);
757 }
758
759 EXPORT_API int widget_base_exit(void)
760 {
761         int ret = 0;
762         int cnt;
763
764         appcore_multiwindow_base_exit();
765         cnt = appcore_multiwindow_base_instance_get_cnt();
766         if (cnt == 0 && __is_permanent)
767                 ret = aul_notify_exit();
768
769         aul_widget_write_log(LOG_TAG,
770                 "[%s:%d] exit : ret(%d), cnt(%d), permanent(%d)",
771                         __FUNCTION__, __LINE__, ret, cnt, __is_permanent);
772
773         return 0;
774 }
775
776 static gboolean __finish_event_cb(gpointer user_data)
777 {
778         appcore_multiwindow_base_instance_h cxt = user_data;
779         bundle *b;
780         const char *id;
781         const char *class_id;
782
783         if (!cxt) {
784                 LOGE("user_data is NULL");
785                 return FALSE;
786         }
787
788         id = appcore_multiwindow_base_instance_get_id(cxt);
789         class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
790         b = bundle_create();
791
792         if (!b) {
793                 LOGE("Out-of-memory");
794                 return FALSE;
795         }
796
797         bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
798         __control_destroy(class_id, id, b);
799         bundle_free(b);
800
801         return FALSE;
802 }
803
804 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
805 {
806         if (!__is_widget_feature_enabled()) {
807                 LOGE("not supported"); /* LCOV_EXCL_LINE */
808                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
809         }
810
811         if (!context) {
812                 LOGE("context is null");
813                 return WIDGET_ERROR_INVALID_PARAMETER;
814         }
815
816         g_idle_add(__finish_event_cb, context);
817
818         return WIDGET_ERROR_NONE;
819 }
820
821 static void __inst_full_cb(const char *class_id, const char *id,
822                 appcore_multiwindow_base_instance_h cxt, void *data)
823 {
824         struct widget_foreach_context *foreach_context = data;
825
826         if (!data)
827                 return;
828
829         if (foreach_context->callback)
830                 foreach_context->callback(cxt, foreach_context->data);
831 }
832
833 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
834 {
835         struct widget_foreach_context foreach_context;
836
837         if (!__is_widget_feature_enabled()) {
838                 LOGE("not supported"); /* LCOV_EXCL_LINE */
839                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
840         }
841
842         if (!cb) {
843                 LOGE("callback is NULL");
844                 return WIDGET_ERROR_INVALID_PARAMETER;
845         }
846
847         foreach_context.callback = cb;
848         foreach_context.data = data;
849         appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
850
851         return WIDGET_ERROR_NONE;
852 }
853
854 static int __event_cb(void *event, void *data)
855 {
856         app_event_handler_h handler = data;
857
858         struct app_event_info app_event;
859
860         app_event.type = handler->type;
861         app_event.value = event;
862
863         if (handler->cb)
864                 handler->cb(&app_event, handler->data);
865
866         return 0;
867 }
868
869 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
870                                         app_event_type_e event_type,
871                                         app_event_cb callback,
872                                         void *user_data)
873 {
874         int r;
875         bool feature;
876         app_event_handler_h handler;
877
878         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
879         if (r < 0)
880                 return WIDGET_BASE_ERROR_FAULT;
881
882         if (!feature)
883                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
884
885         if (event_handler == NULL || callback == NULL)
886                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
887
888         if (event_type < APP_EVENT_LOW_MEMORY
889             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
890                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
891
892         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
893                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
894
895
896         handler = calloc(1, sizeof(struct app_event_handler));
897         if (!handler)
898                 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
899
900         handler->type = event_type;
901         handler->cb = callback;
902         handler->data = user_data;
903         handler->raw = appcore_base_add_event(
904                         __app_event_converter[event_type], __event_cb, handler);
905         *event_handler = handler;
906
907         return WIDGET_BASE_ERROR_NONE;
908 }
909
910 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
911                                                 event_handler)
912 {
913         int r;
914         bool feature;
915         app_event_type_e type;
916
917         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
918         if (r < 0)
919                 return WIDGET_BASE_ERROR_FAULT;
920
921         if (!feature)
922                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
923
924         if (event_handler == NULL)
925                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
926
927         type = event_handler->type;
928         if (type < APP_EVENT_LOW_MEMORY ||
929                         type > APP_EVENT_REGION_FORMAT_CHANGED)
930                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
931
932         r = appcore_base_remove_event(event_handler->raw);
933         if (r < 0)
934                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
935
936         free(event_handler);
937
938         return WIDGET_BASE_ERROR_NONE;
939 }
940
941 EXPORT_API int widget_base_context_set_content_info(
942                 widget_base_instance_h context,
943                 bundle *content_info)
944 {
945         int ret = 0;
946         bundle_raw *raw = NULL;
947         int len;
948         const char *id;
949         const char *class_id;
950         widget_base_instance_data *data;
951         appcore_multiwindow_base_instance_h instance_h;
952
953         if (!__is_widget_feature_enabled()) {
954                 LOGE("not supported"); /* LCOV_EXCL_LINE */
955                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
956         }
957
958         if (!context || !content_info)
959                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
960
961         instance_h = (appcore_multiwindow_base_instance_h)context;
962         id = appcore_multiwindow_base_instance_get_id(instance_h);
963         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
964         data = appcore_multiwindow_base_instance_get_extra(instance_h);
965
966         if (!class_id || !id || !data)
967                 return WIDGET_BASE_ERROR_FAULT;
968
969         ret = __send_update_status(class_id, id,
970                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
971
972         if (data->content)
973                 free(data->content);
974
975         bundle_encode(content_info, &raw, &len);
976         if (raw)
977                 data->content = strdup((const char *)raw);
978         else
979                 data->content = NULL;
980
981         free(raw);
982         if (ret < 0) {
983                 /* LCOV_EXCL_START */
984                 LOGE("failed to send content info: %s of %s (%d)", id,
985                                 class_id, ret);
986                 return WIDGET_BASE_ERROR_IO_ERROR;
987                 /* LCOV_EXCL_STOP */
988         }
989
990         return WIDGET_BASE_ERROR_NONE;
991 }
992
993 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
994 {
995         appcore_multiwindow_base_instance_h instance_h;
996         widget_base_instance_data *data;
997
998         if (!__is_widget_feature_enabled()) {
999                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1000                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1001         }
1002
1003         if (!context || !tag) {
1004                 LOGE("Invalid parameter");
1005                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1006         }
1007
1008         instance_h = (appcore_multiwindow_base_instance_h)context;
1009         data = (widget_base_instance_data *)
1010                         appcore_multiwindow_base_instance_get_extra(instance_h);
1011
1012         if (!data) {
1013                 LOGE("Invalid parameter");
1014                 return WIDGET_ERROR_INVALID_PARAMETER;
1015         }
1016
1017         *tag = data->tag;
1018
1019         return WIDGET_BASE_ERROR_NONE;
1020 }
1021
1022 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
1023 {
1024         appcore_multiwindow_base_instance_h instance_h;
1025         widget_base_instance_data *data;
1026
1027         if (!__is_widget_feature_enabled()) {
1028                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1029                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1030         }
1031
1032         if (!context) {
1033                 LOGE("Invalid parameter");
1034                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1035         }
1036
1037         instance_h = (appcore_multiwindow_base_instance_h)context;
1038         data = (widget_base_instance_data *)
1039                         appcore_multiwindow_base_instance_get_extra(instance_h);
1040         data->tag = tag;
1041
1042         return WIDGET_BASE_ERROR_NONE;
1043 }
1044
1045 EXPORT_API void *widget_base_context_get_user_data(
1046                 widget_base_instance_h context)
1047 {
1048         appcore_multiwindow_base_instance_h instance_h;
1049         widget_base_instance_data *data;
1050
1051         if (!__is_widget_feature_enabled()) {
1052                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1053                 return NULL; /* LCOV_EXCL_LINE */
1054         }
1055
1056         if (!context) {
1057                 LOGE("Invalid parameter");
1058                 return NULL;
1059         }
1060
1061         instance_h = (appcore_multiwindow_base_instance_h)context;
1062         data = (widget_base_instance_data *)
1063                         appcore_multiwindow_base_instance_get_extra(instance_h);
1064
1065         return data->user_data;
1066 }
1067
1068
1069 EXPORT_API int widget_base_context_set_user_data(
1070                 widget_base_instance_h context, void *user_data)
1071 {
1072         appcore_multiwindow_base_instance_h instance_h;
1073         widget_base_instance_data *data;
1074
1075         if (!__is_widget_feature_enabled()) {
1076                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1077                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1078         }
1079
1080         if (!context) {
1081                 LOGE("Invalid parameter");
1082                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1083         }
1084
1085         instance_h = (appcore_multiwindow_base_instance_h)context;
1086         data = (widget_base_instance_data *)
1087                         appcore_multiwindow_base_instance_get_extra(instance_h);
1088         data->user_data = user_data;
1089
1090         return WIDGET_BASE_ERROR_NONE;
1091 }
1092
1093 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1094 {
1095         appcore_multiwindow_base_instance_h instance_h;
1096
1097         if (!__is_widget_feature_enabled()) {
1098                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1099                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1100         }
1101
1102         instance_h = (appcore_multiwindow_base_instance_h)context;
1103         *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1104
1105         return WIDGET_BASE_ERROR_NONE;
1106 }
1107
1108 EXPORT_API const char *widget_base_get_viewer_endpoint()
1109 {
1110         return __viewer_endpoint;
1111 }
1112
1113 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1114                 void *data)
1115 {
1116         bundle *kb;
1117         char *viewer_endpoint = NULL;
1118         appcore_multiwindow_base_ops raw_ops
1119                         = appcore_multiwindow_base_get_default_ops();
1120
1121         __context.ops = ops;
1122         __context.argc = argc;
1123         __context.argv = argv;
1124         __context.data = data;
1125
1126         /* override methods */
1127         raw_ops.base.create = __multiwindow_create;
1128         raw_ops.base.control = __multiwindow_control;
1129         raw_ops.base.terminate = __multiwindow_terminate;
1130         raw_ops.base.receive = __multiwindow_receive;
1131         raw_ops.base.init = __multiwindow_init;
1132         raw_ops.base.finish = __multiwindow_finish;
1133         raw_ops.base.run = __multiwindow_run;
1134         raw_ops.base.exit = __multiwindow_exit;
1135         raw_ops.base.trim_memory = __multiwindow_trim_memory;
1136
1137         if (!__is_widget_feature_enabled()) {
1138                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1139                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1140         }
1141
1142         kb = bundle_import_from_argv(argc, argv);
1143         if (kb) {
1144                 bundle_get_str(kb, AUL_K_WIDGET_VIEWER, &viewer_endpoint);
1145                 if (viewer_endpoint) {
1146                         LOGD("viewer endpoint :%s", viewer_endpoint);
1147                         __viewer_endpoint = strdup(viewer_endpoint);
1148                 } else {
1149                         LOGE("endpoint is missing");
1150                 }
1151
1152                 bundle_free(kb);
1153         } else {
1154                 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1155                 return WIDGET_ERROR_FAULT;
1156         }
1157
1158         if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1159                return WIDGET_ERROR_FAULT;
1160
1161         return WIDGET_ERROR_NONE;
1162 }
1163
1164 static int __on_create(void *data)
1165 {
1166         return widget_base_on_create();
1167 }
1168
1169 static int __on_terminate(void *data)
1170 {
1171         return widget_base_on_terminate();
1172 }
1173
1174 static void __on_init(int argc, char **argv, void *data)
1175 {
1176         widget_base_on_init(argc, argv);
1177 }
1178
1179 static void __on_finish(void)
1180 {
1181         widget_base_on_finish();
1182 }
1183
1184 static void __on_run(void *data)
1185 {
1186         widget_base_on_run();
1187 }
1188
1189 static void __on_exit(void *data)
1190 {
1191         widget_base_on_exit();
1192 }
1193
1194 static void __on_trim_memory(void *data)
1195 {
1196         widget_base_on_trim_memory();
1197 }
1198
1199 EXPORT_API int widget_base_on_create(void)
1200 {
1201         appcore_multiwindow_base_on_create();
1202
1203         return 0;
1204 }
1205
1206 EXPORT_API int widget_base_on_terminate(void)
1207 {
1208         appcore_multiwindow_base_on_terminate();
1209
1210         return 0;
1211 }
1212
1213 EXPORT_API int widget_base_on_init(int argc, char **argv)
1214 {
1215         return 0;
1216 }
1217
1218 EXPORT_API void widget_base_on_finish(void)
1219 {
1220 }
1221
1222 EXPORT_API void widget_base_on_run(void)
1223 {
1224 }
1225
1226 EXPORT_API void widget_base_on_exit(void)
1227 {
1228 }
1229
1230 EXPORT_API int widget_base_on_trim_memory(void)
1231 {
1232         appcore_multiwindow_base_on_trim_memory();
1233
1234         return 0;
1235 }
1236
1237 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1238 {
1239         widget_base_ops ops;
1240
1241         /* override methods */
1242         ops.create = __on_create;
1243         ops.terminate = __on_terminate;
1244         ops.init = __on_init;
1245         ops.finish = __on_finish;
1246         ops.run = __on_run;
1247         ops.exit = __on_exit;
1248         ops.trim_memory = __on_trim_memory;
1249
1250         return ops;
1251 }
1252
1253 static void __free_class(gpointer data)
1254 {
1255         widget_base_class *cls = data;
1256
1257         free(cls->id);
1258         free(cls);
1259 }
1260
1261 EXPORT_API void widget_base_fini(void)
1262 {
1263         appcore_multiwindow_base_fini();
1264         g_list_free_full(__context.classes, __free_class);
1265         __context.classes = NULL;
1266 }
1267
1268 EXPORT_API int widget_base_context_window_bind(
1269                 widget_base_instance_h instance_h, const char *id,
1270                 Ecore_Wl2_Window *wl_win)
1271 {
1272         struct wl_surface *surface;
1273
1274         surface = ecore_wl2_window_surface_get(wl_win);
1275         if (surface == NULL) {
1276                 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1277                 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1278         }
1279
1280         screen_connector_provider_remote_enable(id, surface);
1281         appcore_multiwindow_base_window_bind(instance_h, wl_win);
1282
1283         return WIDGET_BASE_ERROR_NONE;
1284 }
1285
1286 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1287                 int w, int h, void *class_data)
1288 {
1289         return widget_base_class_on_create(instance_h, content, w, h);
1290 }
1291
1292 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1293 {
1294         return widget_base_class_on_resume(instance_h);
1295 }
1296
1297 static int __class_on_pause(widget_base_instance_h instance_h,
1298                 void *class_data)
1299 {
1300         return widget_base_class_on_pause(instance_h);
1301 }
1302
1303 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1304                 void *class_data)
1305 {
1306         return widget_base_class_on_resize(instance_h, w, h);
1307 }
1308
1309 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1310                 int force, void *class_data)
1311 {
1312         return widget_base_class_on_update(instance_h, content, force);
1313 }
1314
1315 static int __class_on_destroy(widget_base_instance_h instance_h,
1316                 widget_base_destroy_type_e reason, bundle *content,
1317                 void *class_data)
1318 {
1319         return widget_base_class_on_destroy(instance_h, reason, content);
1320 }
1321
1322 static void __multiwindow_instance_create(
1323                 appcore_multiwindow_base_instance_h instance_h,
1324                 void *class_data)
1325 {
1326         widget_base_instance_data *instance_data;
1327         bundle *b;
1328         bundle *content_info = NULL;
1329         char *id = NULL;
1330         char *class_id = NULL;
1331         char *operation = NULL;
1332         char *content = NULL;
1333         char *w_str = NULL;
1334         char *h_str = NULL;
1335         char *remain = NULL;
1336         int w = 0;
1337         int h = 0;
1338         int ret = -1;
1339         widget_base_class *cls;
1340         double *period = NULL;
1341         size_t size;
1342
1343         appcore_multiwindow_base_class_on_create(instance_h);
1344         instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1345         b = instance_data->args;
1346
1347         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1348         /* for previous version compatibility, use appid for default class id */
1349         if (class_id == NULL)
1350                 class_id = __appid;
1351
1352         cls = __get_class(class_id);
1353         if (cls == NULL) {
1354                 LOGE("class not found: %s", class_id);
1355                 return;
1356         }
1357
1358         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1359         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1360
1361         if (!operation) {
1362                 LOGE("no operation provided");
1363                 return;
1364         }
1365
1366         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1367         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1368         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1369
1370         if (w_str)
1371                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1372
1373         if (h_str)
1374                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1375
1376         if (content)
1377                 content_info = bundle_decode((const bundle_raw *)content,
1378                                 strlen(content));
1379
1380         if (cls->ops.create)
1381                 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1382
1383         if (ret < 0) {
1384                 LOGW("Create callback returns error(%d)", ret);
1385                 ret = __send_update_status(class_id, id,
1386                                 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
1387                 if (ret < 0)
1388                         LOGE("Fail to send abort status (%d) ", ret);
1389                 __instance_drop(instance_h);
1390         } else {
1391                 LOGD("%s is created", id);
1392                 ret = __send_update_status(class_id, id,
1393                         WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
1394                 if (ret < 0)
1395                         LOGE("Fail to send create status (%d) ", ret);
1396
1397                 aul_widget_instance_add(class_id, id);
1398
1399                 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period,
1400                                 &size);
1401                 if (ret == BUNDLE_ERROR_NONE && *period > 0) {
1402                         LOGI("set periodic update timer (%lf)", *period);
1403                         instance_data->period = *period;
1404                         instance_data->periodic_timer = g_timeout_add_seconds(
1405                                         instance_data->period,
1406                                         __timeout_cb, instance_data);
1407                 }
1408         }
1409
1410         if (content_info)
1411                 bundle_free(content_info);
1412 }
1413
1414 static void __multiwindow_instance_resume(
1415                 appcore_multiwindow_base_instance_h instance_h,
1416                 void *class_data)
1417 {
1418         const char *id;
1419         const char *class_id;
1420         widget_base_class *cls;
1421         widget_base_instance_data *data;
1422
1423         appcore_multiwindow_base_class_on_resume(instance_h);
1424         id = appcore_multiwindow_base_instance_get_id(instance_h);
1425         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1426         cls = __get_class(class_id);
1427         if (cls == NULL) {
1428                 LOGE("class not found: %s", class_id);
1429                 return;
1430         }
1431
1432         data = (widget_base_instance_data *)
1433                         appcore_multiwindow_base_instance_get_extra(instance_h);
1434
1435         if (data->pending_update) {
1436                 LOGD("pending update!");
1437                 data->pending_update = false;
1438                 __call_update_cb(class_id, data->id, 0, data->pending_content);
1439                 if (data->period > 0) {
1440                         LOGD("Restart timer!");
1441                         data->periodic_timer = g_timeout_add_seconds(
1442                                         data->period,
1443                                         __timeout_cb, data);
1444                 }
1445         }
1446
1447         if (cls->ops.resume)
1448                 cls->ops.resume(instance_h, class_data);
1449
1450         LOGD("%s is resumed", id);
1451         __send_update_status(class_id, id,
1452                 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
1453
1454         if (!__fg_signal) {
1455                 LOGD("Send fg signal to resourceD");
1456                 aul_widget_instance_change_status(class_id, STATUS_FOREGROUND);
1457                 __fg_signal = true;
1458         }
1459 }
1460
1461 static void __multiwindow_instance_pause(
1462                 appcore_multiwindow_base_instance_h instance_h,
1463                 void *class_data)
1464 {
1465         const char *id;
1466         const char *class_id;
1467         widget_base_class *cls;
1468
1469         appcore_multiwindow_base_class_on_pause(instance_h);
1470         id = appcore_multiwindow_base_instance_get_id(instance_h);
1471         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1472         cls = __get_class(class_id);
1473         if (cls == NULL) {
1474                 LOGE("class not found: %s", class_id);
1475                 return;
1476         }
1477
1478         if (cls->ops.pause)
1479                 cls->ops.pause(instance_h, class_data);
1480
1481         LOGD("%s is paused", id);
1482         __send_update_status(class_id, id,
1483                 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
1484
1485         if (__fg_signal) {
1486                 LOGD("Send bg signal to resourceD");
1487                 aul_widget_instance_change_status(class_id, STATUS_BACKGROUND);
1488                 __fg_signal = false;
1489         }
1490 }
1491
1492 static void __multiwindow_instance_terminate(
1493                 appcore_multiwindow_base_instance_h instance_h,
1494                 void *class_data)
1495 {
1496         widget_base_instance_data *data;
1497         bundle *b;
1498         char *operation = NULL;
1499         bundle *content_info;
1500         widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1501         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1502         const char *id;
1503         const char *class_id;
1504         widget_base_class *cls;
1505
1506         id = appcore_multiwindow_base_instance_get_id(instance_h);
1507         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1508         data  = appcore_multiwindow_base_instance_get_extra(
1509                         (appcore_multiwindow_base_instance_h)instance_h);
1510         b = data->args;
1511         cls = __get_class(class_id);
1512         if (cls == NULL) {
1513                 LOGE("class not found: %s", class_id);
1514                 return;
1515         }
1516
1517         if (b) {
1518                 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1519                 if (operation && strcmp(operation, "destroy") == 0)
1520                         reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1521         }
1522
1523         if (data->content)
1524                 content_info = bundle_decode((const bundle_raw *)data->content,
1525                                 strlen(data->content));
1526         else
1527                 content_info = bundle_create();
1528
1529         if (cls->ops.destroy)
1530                 cls->ops.destroy(instance_h, reason, content_info, class_data);
1531
1532         LOGW("%s is destroyed %d", id, reason);
1533         if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1534                 __is_permanent = true;
1535                 event = WIDGET_INSTANCE_EVENT_DESTROY;
1536                 aul_widget_instance_del(class_id, id);
1537         } else {
1538                 __is_permanent = false;
1539                 __send_update_status(class_id, id,
1540                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0,
1541                                 content_info);
1542         }
1543
1544         if (content_info)
1545                 bundle_free(content_info);
1546
1547         if (data->periodic_timer)
1548                 g_source_remove(data->periodic_timer);
1549
1550         __send_update_status(class_id, id, event, 0, NULL);
1551         appcore_multiwindow_base_class_on_terminate(instance_h);
1552 }
1553
1554 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1555                 bundle *content, int w, int h)
1556 {
1557         appcore_multiwindow_base_class_on_create(instance_h);
1558
1559         return 0;
1560 }
1561
1562 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1563 {
1564         appcore_multiwindow_base_class_on_pause(instance_h);
1565
1566         return 0;
1567 }
1568
1569 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1570 {
1571         appcore_multiwindow_base_class_on_resume(instance_h);
1572
1573         return 0;
1574 }
1575
1576 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1577                 int w, int h)
1578 {
1579         return 0;
1580 }
1581
1582 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1583                 bundle *content, int force)
1584 {
1585         return 0;
1586 }
1587
1588 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1589                 widget_base_destroy_type_e reason, bundle *content)
1590 {
1591         appcore_multiwindow_base_class_on_terminate(instance_h);
1592
1593         return 0;
1594 }
1595
1596 EXPORT_API widget_base_class widget_base_class_get_default(void)
1597 {
1598         widget_base_class cls;
1599
1600         cls.ops.create = __class_on_create;
1601         cls.ops.resize = __class_on_resize;
1602         cls.ops.update = __class_on_update;
1603         cls.ops.destroy = __class_on_destroy;
1604         cls.ops.pause = __class_on_pause;
1605         cls.ops.resume = __class_on_resume;
1606         cls.id = NULL;
1607
1608         return cls;
1609 }
1610
1611 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1612                 const char *class_id, void *class_data)
1613 {
1614         widget_base_class *c;
1615         appcore_multiwindow_base_class raw_cls;
1616
1617         if (!__is_widget_feature_enabled()) {
1618                 LOGE("not supported");
1619                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1620                 return NULL;
1621         }
1622
1623         if (!class_id) {
1624                 LOGE("class is is NULL");
1625                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1626                 return NULL;
1627         }
1628
1629         raw_cls.id = strdup(class_id);
1630         raw_cls.data = class_data;
1631         raw_cls.create = __multiwindow_instance_create;
1632         raw_cls.terminate = __multiwindow_instance_terminate;
1633         raw_cls.pause = __multiwindow_instance_pause;
1634         raw_cls.resume = __multiwindow_instance_resume;
1635         appcore_multiwindow_base_class_add(raw_cls);
1636
1637         c = malloc(sizeof(widget_base_class));
1638         if (!c)
1639                 return NULL;
1640
1641         *c = cls;
1642         c->id = strdup(class_id);
1643         __context.classes = g_list_append(__context.classes, c);
1644
1645         return c;
1646 }