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