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