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