Use aul key for end point data
[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 static void __multiwindow_trim_memory(void *data)
783 {
784         if (__context.ops.trim_memory)
785                 __context.ops.trim_memory(data);
786 }
787
788 EXPORT_API int widget_base_exit(void)
789 {
790         int ret;
791
792         appcore_multiwindow_base_exit();
793         if (appcore_multiwindow_base_instance_get_cnt() == 0 && __is_permanent) {
794                 ret = aul_notify_exit();
795                 aul_widget_write_log(LOG_TAG, "[%s:%d] permanent exit : %d",
796                         __FUNCTION__, __LINE__, ret);
797         }
798
799         return 0;
800 }
801
802 static gboolean __finish_event_cb(gpointer user_data)
803 {
804         appcore_multiwindow_base_instance_h cxt = user_data;
805         bundle *b;
806         const char *id;
807         const char *class_id;
808
809         if (!cxt) {
810                 LOGE("user_data is NULL");
811                 return FALSE;
812         }
813
814         id = appcore_multiwindow_base_instance_get_id(cxt);
815         class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
816         b = bundle_create();
817
818         if (!b) {
819                 LOGE("Out-of-memory");
820                 return FALSE;
821         }
822
823         bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
824         __control_destroy(class_id, id, b);
825         bundle_free(b);
826
827         return FALSE;
828 }
829
830 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
831 {
832         if (!__is_widget_feature_enabled()) {
833                 LOGE("not supported"); /* LCOV_EXCL_LINE */
834                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
835         }
836
837         if (!context) {
838                 LOGE("context is null");
839                 return WIDGET_ERROR_INVALID_PARAMETER;
840         }
841
842         g_idle_add(__finish_event_cb, context);
843
844         return WIDGET_ERROR_NONE;
845 }
846
847 static void __inst_full_cb(const char *class_id, const char *id,
848                 appcore_multiwindow_base_instance_h cxt, void *data)
849 {
850         struct widget_foreach_context *foreach_context = data;
851
852         if (!data)
853                 return;
854
855         if (foreach_context->callback)
856                 foreach_context->callback(cxt, foreach_context->data);
857 }
858
859 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
860 {
861         struct widget_foreach_context foreach_context;
862
863         if (!__is_widget_feature_enabled()) {
864                 LOGE("not supported"); /* LCOV_EXCL_LINE */
865                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
866         }
867
868         if (!cb) {
869                 LOGE("callback is NULL");
870                 return WIDGET_ERROR_INVALID_PARAMETER;
871         }
872
873         foreach_context.callback = cb;
874         foreach_context.data = data;
875         appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
876
877         return WIDGET_ERROR_NONE;
878 }
879
880 static int __event_cb(void *event, void *data)
881 {
882         app_event_handler_h handler = data;
883
884         struct app_event_info app_event;
885
886         app_event.type = handler->type;
887         app_event.value = event;
888
889         if (handler->cb)
890                 handler->cb(&app_event, handler->data);
891
892         return 0;
893 }
894
895 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
896                                         app_event_type_e event_type,
897                                         app_event_cb callback,
898                                         void *user_data)
899 {
900         int r;
901         bool feature;
902         app_event_handler_h handler;
903
904         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
905         if (r < 0)
906                 return WIDGET_BASE_ERROR_FAULT;
907
908         if (!feature)
909                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
910
911         if (event_handler == NULL || callback == NULL)
912                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
913
914         if (event_type < APP_EVENT_LOW_MEMORY
915             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
916                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
917
918         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
919                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
920
921
922         handler = calloc(1, sizeof(struct app_event_handler));
923         if (!handler)
924                 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
925
926         handler->type = event_type;
927         handler->cb = callback;
928         handler->data = user_data;
929         handler->raw = appcore_base_add_event(
930                         __app_event_converter[event_type], __event_cb, handler);
931         *event_handler = handler;
932
933         return WIDGET_BASE_ERROR_NONE;
934 }
935
936 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
937                                                 event_handler)
938 {
939         int r;
940         bool feature;
941         app_event_type_e type;
942
943         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
944         if (r < 0)
945                 return WIDGET_BASE_ERROR_FAULT;
946
947         if (!feature)
948                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
949
950         if (event_handler == NULL)
951                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
952
953         type = event_handler->type;
954         if (type < APP_EVENT_LOW_MEMORY ||
955                         type > APP_EVENT_REGION_FORMAT_CHANGED)
956                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
957
958         r = appcore_base_remove_event(event_handler->raw);
959         if (r < 0)
960                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
961
962         free(event_handler);
963
964         return WIDGET_BASE_ERROR_NONE;
965 }
966
967 EXPORT_API int widget_base_context_set_content_info(
968                 widget_base_instance_h context,
969                 bundle *content_info)
970 {
971         int ret = 0;
972         bundle_raw *raw = NULL;
973         int len;
974         const char *id;
975         const char *class_id;
976         widget_base_instance_data *data;
977         appcore_multiwindow_base_instance_h instance_h;
978
979         if (!__is_widget_feature_enabled()) {
980                 LOGE("not supported"); /* LCOV_EXCL_LINE */
981                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
982         }
983
984         if (!context || !content_info)
985                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
986
987         instance_h = (appcore_multiwindow_base_instance_h)context;
988         id = appcore_multiwindow_base_instance_get_id(instance_h);
989         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
990         data = appcore_multiwindow_base_instance_get_extra(instance_h);
991
992         if (!class_id || !id || !data)
993                 return WIDGET_BASE_ERROR_FAULT;
994
995         ret = __send_update_status(class_id, id,
996                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0, content_info);
997
998         if (data->content)
999                 free(data->content);
1000
1001         bundle_encode(content_info, &raw, &len);
1002         if (raw)
1003                 data->content = strdup((const char *)raw);
1004         else
1005                 data->content = NULL;
1006
1007         free(raw);
1008         if (ret < 0) {
1009                 /* LCOV_EXCL_START */
1010                 LOGE("failed to send content info: %s of %s (%d)", id,
1011                                 class_id, ret);
1012                 return WIDGET_BASE_ERROR_IO_ERROR;
1013                 /* LCOV_EXCL_STOP */
1014         }
1015
1016         return WIDGET_BASE_ERROR_NONE;
1017 }
1018
1019 EXPORT_API int widget_base_context_get_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 || !tag) {
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
1038         if (!data) {
1039                 LOGE("Invalid parameter");
1040                 return WIDGET_ERROR_INVALID_PARAMETER;
1041         }
1042
1043         *tag = data->tag;
1044
1045         return WIDGET_BASE_ERROR_NONE;
1046 }
1047
1048 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
1049 {
1050         appcore_multiwindow_base_instance_h instance_h;
1051         widget_base_instance_data *data;
1052
1053         if (!__is_widget_feature_enabled()) {
1054                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1055                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1056         }
1057
1058         if (!context) {
1059                 LOGE("Invalid parameter");
1060                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1061         }
1062
1063         instance_h = (appcore_multiwindow_base_instance_h)context;
1064         data = (widget_base_instance_data *)
1065                         appcore_multiwindow_base_instance_get_extra(instance_h);
1066         data->tag = tag;
1067
1068         return WIDGET_BASE_ERROR_NONE;
1069 }
1070
1071 EXPORT_API void *widget_base_context_get_user_data(
1072                 widget_base_instance_h context)
1073 {
1074         appcore_multiwindow_base_instance_h instance_h;
1075         widget_base_instance_data *data;
1076
1077         if (!__is_widget_feature_enabled()) {
1078                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1079                 return NULL; /* LCOV_EXCL_LINE */
1080         }
1081
1082         if (!context) {
1083                 LOGE("Invalid parameter");
1084                 return NULL;
1085         }
1086
1087         instance_h = (appcore_multiwindow_base_instance_h)context;
1088         data = (widget_base_instance_data *)
1089                         appcore_multiwindow_base_instance_get_extra(instance_h);
1090
1091         return data->user_data;
1092 }
1093
1094
1095 EXPORT_API int widget_base_context_set_user_data(
1096                 widget_base_instance_h context, void *user_data)
1097 {
1098         appcore_multiwindow_base_instance_h instance_h;
1099         widget_base_instance_data *data;
1100
1101         if (!__is_widget_feature_enabled()) {
1102                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1103                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1104         }
1105
1106         if (!context) {
1107                 LOGE("Invalid parameter");
1108                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1109         }
1110
1111         instance_h = (appcore_multiwindow_base_instance_h)context;
1112         data = (widget_base_instance_data *)
1113                         appcore_multiwindow_base_instance_get_extra(instance_h);
1114         data->user_data = user_data;
1115
1116         return WIDGET_BASE_ERROR_NONE;
1117 }
1118
1119 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1120 {
1121         appcore_multiwindow_base_instance_h instance_h;
1122
1123         if (!__is_widget_feature_enabled()) {
1124                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1125                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1126         }
1127
1128         instance_h = (appcore_multiwindow_base_instance_h)context;
1129         *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1130
1131         return WIDGET_BASE_ERROR_NONE;
1132 }
1133
1134 EXPORT_API const char *widget_base_get_viewer_endpoint()
1135 {
1136         return __viewer_endpoint;
1137 }
1138
1139 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1140                 void *data)
1141 {
1142         bundle *kb;
1143         char *viewer_endpoint = NULL;
1144         appcore_multiwindow_base_ops raw_ops
1145                         = appcore_multiwindow_base_get_default_ops();
1146
1147         __context.ops = ops;
1148         __context.argc = argc;
1149         __context.argv = argv;
1150         __context.data = data;
1151
1152         /* override methods */
1153         raw_ops.base.create = __multiwindow_create;
1154         raw_ops.base.control = __multiwindow_control;
1155         raw_ops.base.terminate = __multiwindow_terminate;
1156         raw_ops.base.receive = __multiwindow_receive;
1157         raw_ops.base.init = __multiwindow_init;
1158         raw_ops.base.finish = __multiwindow_finish;
1159         raw_ops.base.run = __multiwindow_run;
1160         raw_ops.base.exit = __multiwindow_exit;
1161         raw_ops.base.trim_memory = __multiwindow_trim_memory;
1162
1163         if (!__is_widget_feature_enabled()) {
1164                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1165                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1166         }
1167
1168         kb = bundle_import_from_argv(argc, argv);
1169         if (kb) {
1170                 bundle_get_str(kb, AUL_K_WIDGET_VIEWER, &viewer_endpoint);
1171                 if (viewer_endpoint) {
1172                         LOGD("viewer endpoint :%s", viewer_endpoint);
1173                         __viewer_endpoint = strdup(viewer_endpoint);
1174                 } else {
1175                         LOGE("endpoint is missing");
1176                 }
1177
1178                 bundle_free(kb);
1179         } else {
1180                 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1181                 return WIDGET_ERROR_FAULT;
1182         }
1183
1184         if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1185                return WIDGET_ERROR_FAULT;
1186
1187         return WIDGET_ERROR_NONE;
1188 }
1189
1190 static int __on_create(void *data)
1191 {
1192         return widget_base_on_create();
1193 }
1194
1195 static int __on_terminate(void *data)
1196 {
1197         return widget_base_on_terminate();
1198 }
1199
1200 static void __on_init(int argc, char **argv, void *data)
1201 {
1202         widget_base_on_init(argc, argv);
1203 }
1204
1205 static void __on_finish(void)
1206 {
1207         widget_base_on_finish();
1208 }
1209
1210 static void __on_run(void *data)
1211 {
1212         widget_base_on_run();
1213 }
1214
1215 static void __on_exit(void *data)
1216 {
1217         widget_base_on_exit();
1218 }
1219
1220 static void __on_trim_memory(void *data)
1221 {
1222         widget_base_on_trim_memory();
1223 }
1224
1225 EXPORT_API int widget_base_on_create(void)
1226 {
1227         appcore_multiwindow_base_on_create();
1228
1229         return 0;
1230 }
1231
1232 EXPORT_API int widget_base_on_terminate(void)
1233 {
1234         appcore_multiwindow_base_on_terminate();
1235
1236         return 0;
1237 }
1238
1239 EXPORT_API int widget_base_on_init(int argc, char **argv)
1240 {
1241         return 0;
1242 }
1243
1244 EXPORT_API void widget_base_on_finish(void)
1245 {
1246 }
1247
1248 EXPORT_API void widget_base_on_run(void)
1249 {
1250 }
1251
1252 EXPORT_API void widget_base_on_exit(void)
1253 {
1254 }
1255
1256 EXPORT_API int widget_base_on_trim_memory(void)
1257 {
1258         appcore_multiwindow_base_on_trim_memory();
1259
1260         return 0;
1261 }
1262
1263 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1264 {
1265         widget_base_ops ops;
1266
1267         /* override methods */
1268         ops.create = __on_create;
1269         ops.terminate = __on_terminate;
1270         ops.init = __on_init;
1271         ops.finish = __on_finish;
1272         ops.run = __on_run;
1273         ops.exit = __on_exit;
1274         ops.trim_memory = __on_trim_memory;
1275
1276         return ops;
1277 }
1278
1279 static void __free_class(gpointer data)
1280 {
1281         widget_base_class *cls = data;
1282
1283         free(cls->id);
1284         free(cls);
1285 }
1286
1287 EXPORT_API void widget_base_fini(void)
1288 {
1289         appcore_multiwindow_base_fini();
1290         g_list_free_full(__context.classes, __free_class);
1291         __context.classes = NULL;
1292 }
1293
1294 EXPORT_API int widget_base_context_window_bind(
1295                 widget_base_instance_h instance_h, const char *id,
1296                 Ecore_Wl2_Window *wl_win)
1297 {
1298         struct wl_surface *surface;
1299
1300         surface = ecore_wl2_window_surface_get(wl_win);
1301         if (surface == NULL) {
1302                 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1303                 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1304         }
1305
1306         screen_connector_provider_remote_enable(id, surface);
1307         appcore_multiwindow_base_window_bind(instance_h, wl_win);
1308
1309         return WIDGET_BASE_ERROR_NONE;
1310 }
1311
1312 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1313                 int w, int h, void *class_data)
1314 {
1315         return widget_base_class_on_create(instance_h, content, w, h);
1316 }
1317
1318 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1319 {
1320         return widget_base_class_on_resume(instance_h);
1321 }
1322
1323 static int __class_on_pause(widget_base_instance_h instance_h,
1324                 void *class_data)
1325 {
1326         return widget_base_class_on_pause(instance_h);
1327 }
1328
1329 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1330                 void *class_data)
1331 {
1332         return widget_base_class_on_resize(instance_h, w, h);
1333 }
1334
1335 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1336                 int force, void *class_data)
1337 {
1338         return widget_base_class_on_update(instance_h, content, force);
1339 }
1340
1341 static int __class_on_destroy(widget_base_instance_h instance_h,
1342                 widget_base_destroy_type_e reason, bundle *content,
1343                 void *class_data)
1344 {
1345         return widget_base_class_on_destroy(instance_h, reason, content);
1346 }
1347
1348 static void __multiwindow_instance_create(
1349                 appcore_multiwindow_base_instance_h instance_h,
1350                 void *class_data)
1351 {
1352         widget_base_instance_data *instance_data;
1353         bundle *b;
1354         bundle *content_info = NULL;
1355         char *id = NULL;
1356         char *class_id = NULL;
1357         char *operation = NULL;
1358         char *content = NULL;
1359         char *w_str = NULL;
1360         char *h_str = NULL;
1361         char *remain = NULL;
1362         int w = 0;
1363         int h = 0;
1364         int ret = -1;
1365         widget_base_class *cls;
1366         double *period = NULL;
1367         size_t size;
1368
1369         appcore_multiwindow_base_class_on_create(instance_h);
1370         instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1371         b = instance_data->args;
1372
1373         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1374         /* for previous version compatibility, use appid for default class id */
1375         if (class_id == NULL)
1376                 class_id = __appid;
1377
1378         cls = __get_class(class_id);
1379         if (cls == NULL) {
1380                 LOGE("class not found: %s", class_id);
1381                 return;
1382         }
1383
1384         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1385         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1386
1387         if (!operation) {
1388                 LOGE("no operation provided");
1389                 return;
1390         }
1391
1392         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1393         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1394         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1395
1396         if (w_str)
1397                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1398
1399         if (h_str)
1400                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1401
1402         if (content)
1403                 content_info = bundle_decode((const bundle_raw *)content,
1404                                 strlen(content));
1405
1406         if (cls->ops.create)
1407                 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1408
1409         if (ret < 0) {
1410                 LOGW("Create callback returns error(%d)", ret);
1411                 ret = __send_update_status(class_id, id,
1412                                 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, ret, NULL);
1413                 if (ret < 0)
1414                         LOGE("Fail to send abort status (%d) ", ret);
1415                 __instance_drop(instance_h);
1416         } else {
1417                 LOGD("%s is created", id);
1418                 ret = __send_update_status(class_id, id,
1419                         WIDGET_INSTANCE_EVENT_CREATE, 0, NULL);
1420                 if (ret < 0)
1421                         LOGE("Fail to send create status (%d) ", ret);
1422
1423                 aul_widget_instance_add(class_id, id);
1424
1425                 ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period,
1426                                 &size);
1427                 if (ret == BUNDLE_ERROR_NONE && *period > 0) {
1428                         LOGI("set periodic update timer (%lf)", *period);
1429                         instance_data->period = *period;
1430                         instance_data->periodic_timer = g_timeout_add_seconds(
1431                                         instance_data->period,
1432                                         __timeout_cb, instance_data);
1433                 }
1434         }
1435
1436         if (content_info)
1437                 bundle_free(content_info);
1438 }
1439
1440 static void __multiwindow_instance_resume(
1441                 appcore_multiwindow_base_instance_h instance_h,
1442                 void *class_data)
1443 {
1444         const char *id;
1445         const char *class_id;
1446         widget_base_class *cls;
1447         widget_base_instance_data *data;
1448
1449         appcore_multiwindow_base_class_on_resume(instance_h);
1450         id = appcore_multiwindow_base_instance_get_id(instance_h);
1451         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1452         cls = __get_class(class_id);
1453         if (cls == NULL) {
1454                 LOGE("class not found: %s", class_id);
1455                 return;
1456         }
1457
1458         data = (widget_base_instance_data *)
1459                         appcore_multiwindow_base_instance_get_extra(instance_h);
1460
1461         if (data->pending_update) {
1462                 LOGD("pending update!");
1463                 data->pending_update = false;
1464                 __call_update_cb(class_id, data->id, 0, data->pending_content);
1465                 if (data->period > 0) {
1466                         LOGD("Restart timer!");
1467                         data->periodic_timer = g_timeout_add_seconds(
1468                                         data->period,
1469                                         __timeout_cb, data);
1470                 }
1471         }
1472
1473         if (cls->ops.resume)
1474                 cls->ops.resume(instance_h, class_data);
1475
1476         LOGD("%s is resumed", id);
1477         __send_update_status(class_id, id,
1478                 WIDGET_INSTANCE_EVENT_RESUME, 0, NULL);
1479
1480         if (!__fg_signal) {
1481                 LOGD("Send fg signal to resourceD");
1482                 aul_widget_instance_change_status(class_id, STATUS_FOREGROUND);
1483                 __fg_signal = true;
1484         }
1485 }
1486
1487 static void __multiwindow_instance_pause(
1488                 appcore_multiwindow_base_instance_h instance_h,
1489                 void *class_data)
1490 {
1491         const char *id;
1492         const char *class_id;
1493         widget_base_class *cls;
1494
1495         appcore_multiwindow_base_class_on_pause(instance_h);
1496         id = appcore_multiwindow_base_instance_get_id(instance_h);
1497         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1498         cls = __get_class(class_id);
1499         if (cls == NULL) {
1500                 LOGE("class not found: %s", class_id);
1501                 return;
1502         }
1503
1504         if (cls->ops.pause)
1505                 cls->ops.pause(instance_h, class_data);
1506
1507         LOGD("%s is paused", id);
1508         __send_update_status(class_id, id,
1509                 WIDGET_INSTANCE_EVENT_PAUSE, 0, NULL);
1510
1511         if (__fg_signal) {
1512                 LOGD("Send bg signal to resourceD");
1513                 aul_widget_instance_change_status(class_id, STATUS_BACKGROUND);
1514                 __fg_signal = false;
1515         }
1516 }
1517
1518 static void __multiwindow_instance_terminate(
1519                 appcore_multiwindow_base_instance_h instance_h,
1520                 void *class_data)
1521 {
1522         widget_base_instance_data *data;
1523         bundle *b;
1524         char *operation = NULL;
1525         bundle *content_info;
1526         widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1527         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1528         const char *id;
1529         const char *class_id;
1530         widget_base_class *cls;
1531
1532         id = appcore_multiwindow_base_instance_get_id(instance_h);
1533         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1534         data  = appcore_multiwindow_base_instance_get_extra(
1535                         (appcore_multiwindow_base_instance_h)instance_h);
1536         b = data->args;
1537         cls = __get_class(class_id);
1538         if (cls == NULL) {
1539                 LOGE("class not found: %s", class_id);
1540                 return;
1541         }
1542
1543         if (b) {
1544                 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1545                 if (operation && strcmp(operation, "destroy") == 0)
1546                         reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1547         }
1548
1549         if (data->content)
1550                 content_info = bundle_decode((const bundle_raw *)data->content,
1551                                 strlen(data->content));
1552         else
1553                 content_info = bundle_create();
1554
1555         if (cls->ops.destroy)
1556                 cls->ops.destroy(instance_h, reason, content_info, class_data);
1557
1558         LOGW("%s is destroyed %d", id, reason);
1559         if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1560                 __is_permanent = true;
1561                 event = WIDGET_INSTANCE_EVENT_DESTROY;
1562                 aul_widget_instance_del(class_id, id);
1563         } else {
1564                 __is_permanent = false;
1565                 __send_update_status(class_id, id,
1566                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, 0,
1567                                 content_info);
1568         }
1569
1570         if (content_info)
1571                 bundle_free(content_info);
1572
1573         if (data->periodic_timer)
1574                 g_source_remove(data->periodic_timer);
1575
1576         __send_update_status(class_id, id, event, 0, NULL);
1577         appcore_multiwindow_base_class_on_terminate(instance_h);
1578 }
1579
1580 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1581                 bundle *content, int w, int h)
1582 {
1583         appcore_multiwindow_base_class_on_create(instance_h);
1584
1585         return 0;
1586 }
1587
1588 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1589 {
1590         appcore_multiwindow_base_class_on_pause(instance_h);
1591
1592         return 0;
1593 }
1594
1595 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1596 {
1597         appcore_multiwindow_base_class_on_resume(instance_h);
1598
1599         return 0;
1600 }
1601
1602 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1603                 int w, int h)
1604 {
1605         return 0;
1606 }
1607
1608 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1609                 bundle *content, int force)
1610 {
1611         return 0;
1612 }
1613
1614 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1615                 widget_base_destroy_type_e reason, bundle *content)
1616 {
1617         appcore_multiwindow_base_class_on_terminate(instance_h);
1618
1619         return 0;
1620 }
1621
1622 EXPORT_API widget_base_class widget_base_class_get_default(void)
1623 {
1624         widget_base_class cls;
1625
1626         cls.ops.create = __class_on_create;
1627         cls.ops.resize = __class_on_resize;
1628         cls.ops.update = __class_on_update;
1629         cls.ops.destroy = __class_on_destroy;
1630         cls.ops.pause = __class_on_pause;
1631         cls.ops.resume = __class_on_resume;
1632         cls.id = NULL;
1633
1634         return cls;
1635 }
1636
1637 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1638                 const char *class_id, void *class_data)
1639 {
1640         widget_base_class *c;
1641         appcore_multiwindow_base_class raw_cls;
1642
1643         if (!__is_widget_feature_enabled()) {
1644                 LOGE("not supported");
1645                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1646                 return NULL;
1647         }
1648
1649         if (!class_id) {
1650                 LOGE("class is is NULL");
1651                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1652                 return NULL;
1653         }
1654
1655         raw_cls.id = strdup(class_id);
1656         raw_cls.data = class_data;
1657         raw_cls.create = __multiwindow_instance_create;
1658         raw_cls.terminate = __multiwindow_instance_terminate;
1659         raw_cls.pause = __multiwindow_instance_pause;
1660         raw_cls.resume = __multiwindow_instance_resume;
1661         appcore_multiwindow_base_class_add(raw_cls);
1662
1663         c = malloc(sizeof(widget_base_class));
1664         if (!c)
1665                 return NULL;
1666
1667         *c = cls;
1668         c->id = strdup(class_id);
1669         __context.classes = g_list_append(__context.classes, c);
1670
1671         return c;
1672 }