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