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