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