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