d479c5255631eb6c0a742f7562b2b1f3d25596d6
[platform/core/appfw/appcore-widget.git] / src / base / widget_base.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <stdbool.h>
19
20 #include <bundle.h>
21 #include <bundle_internal.h>
22 #include <aul.h>
23 #include <dlog.h>
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <widget_errno.h>
29 #include <widget_instance.h>
30 #include <aul_app_com.h>
31 #include <Ecore_Wayland.h>
32 #include <system_info.h>
33 #include <vconf.h>
34 #include <vconf-internal-keys.h>
35 #include <screen_connector_provider.h>
36 #include <appcore_multiwindow_base.h>
37
38 #include "widget_base.h"
39
40 #ifdef LOG_TAG
41 #undef LOG_TAG
42 #endif
43
44 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
45 #define APP_TYPE_WIDGET "widgetapp"
46 #define STATUS_FOREGROUND "fg"
47 #define STATUS_BACKGROUND "bg"
48
49 static int __app_event_converter[APPCORE_BASE_EVENT_MAX] = {
50         [APP_EVENT_LOW_MEMORY] = APPCORE_BASE_EVENT_LOW_MEMORY,
51         [APP_EVENT_LOW_BATTERY] = APPCORE_BASE_EVENT_LOW_BATTERY,
52         [APP_EVENT_LANGUAGE_CHANGED] = APPCORE_BASE_EVENT_LANG_CHANGE,
53         [APP_EVENT_DEVICE_ORIENTATION_CHANGED]
54                         = APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED,
55         [APP_EVENT_REGION_FORMAT_CHANGED] = APPCORE_BASE_EVENT_REGION_CHANGE,
56         [APP_EVENT_SUSPENDED_STATE_CHANGED]
57                         = APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE,
58 };
59
60 struct app_event_info {
61         app_event_type_e type;
62         void *value;
63 };
64
65 struct app_event_handler {
66         app_event_type_e type;
67         app_event_cb cb;
68         void *data;
69         void *raw;
70 };
71
72 struct widget_foreach_context {
73         widget_base_instance_cb callback;
74         void *data;
75 };
76
77 typedef struct _widget_base_context {
78         widget_base_ops ops;
79         void *data;
80         int argc;
81         char **argv;
82         GList *classes;
83 } widget_base_context;
84
85 typedef struct _widget_base_instance_data {
86         bundle *args;
87         char *id;
88         char *content;
89         void *tag;
90         double period;
91         guint periodic_timer;
92         bool pending_update;
93         void *user_data;
94 } widget_base_instance_data;
95
96 static widget_base_context __context;
97 static char *__appid;
98 static char *__package_id;
99 static bool __fg_signal;
100 static char *__viewer_endpoint;
101 static void __call_update_cb(const char *class_id, const char *id, int force,
102                 const char *content_raw);
103
104 static gboolean __timeout_cb(gpointer user_data)
105 {
106         widget_base_instance_data *data =
107                         (widget_base_instance_data *)user_data;
108         appcore_multiwindow_base_instance_h cxt;
109         const char *class_id;
110
111         cxt = appcore_multiwindow_base_instance_find(data->id);
112         if (appcore_multiwindow_base_instance_is_resumed(cxt)) {
113                 LOGD("Periodic update!");
114                 class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
115                 __call_update_cb(class_id, data->id, 0, NULL);
116         } else {
117                 data->pending_update = true;
118                 if (data->periodic_timer) {
119                         LOGD("Remove timer!");
120                         g_source_remove(data->periodic_timer);
121                         data->periodic_timer = 0;
122                 }
123         }
124
125         return G_SOURCE_CONTINUE;
126 }
127
128 static bool __is_widget_feature_enabled(void)
129 {
130         static bool feature = false;
131         static bool retrieved = false;
132         int ret;
133
134         if (retrieved == true)
135                 return feature;
136
137         ret = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
138         if (ret != SYSTEM_INFO_ERROR_NONE) {
139                 LOGE("failed to get system info"); /* LCOV_EXCL_LINE */
140                 return false; /* LCOV_EXCL_LINE */
141         }
142
143         retrieved = true;
144
145         return feature;
146 }
147
148 /* LCOV_EXCL_START */
149 static void __on_poweroff(keynode_t *key, void *data)
150 {
151         int val;
152
153         val = vconf_keynode_get_int(key);
154         switch (val) {
155         case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
156         case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
157                 LOGI("power off changed: %d", val);
158                 widget_base_exit();
159                 break;
160         case VCONFKEY_SYSMAN_POWER_OFF_NONE:
161         case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
162         default:
163                 /* DO NOTHING */
164                 break;
165         }
166 }
167 /* LCOV_EXCL_STOP */
168
169 static void __check_empty_instance(void)
170 {
171         int cnt = appcore_multiwindow_base_instance_get_cnt();
172
173         if (cnt == 0)
174                 widget_base_exit();
175 }
176
177 static void __instance_drop(appcore_multiwindow_base_instance_h instance_h)
178 {
179         widget_base_instance_data *data;
180
181         data = appcore_multiwindow_base_instance_get_extra(instance_h);
182         appcore_multiwindow_base_instance_drop(instance_h);
183         free(data->content);
184         free(data->id);
185         free(data);
186         __check_empty_instance();
187 }
188
189 static gint __comp_class(gconstpointer a, gconstpointer b)
190 {
191         const widget_base_class *cls = a;
192
193         return strcmp(cls->id, b);
194 }
195
196 static widget_base_class *__get_class(const char *class_id)
197 {
198         widget_base_class *cls;
199         GList *class_node;
200
201         class_node = g_list_find_custom(__context.classes, class_id,
202                         __comp_class);
203         if (class_node == NULL) {
204                 LOGE("empty classes");
205                 return NULL;
206         }
207         cls = (widget_base_class *)class_node->data;
208
209         return cls;
210 }
211
212 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
213         int status)
214 {
215         bundle *b = bundle_create();
216         int ret;
217
218         if (b == NULL) {
219                 LOGE("out of memory"); /* LCOV_EXCL_LINE */
220                 return -1; /* LCOV_EXCL_LINE */
221         }
222
223         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
224         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
225         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
226         bundle_add_str(b, AUL_K_PKGID, __package_id);
227
228         LOGD("send lifecycle %s(%d)", instance_id, status);
229         ret = aul_app_com_send("widget.status", b);
230         if (ret < 0)
231                 LOGE("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
232
233         bundle_free(b);
234
235         return ret;
236 }
237
238 static int __send_update_status(const char *class_id, const char *instance_id,
239         int status, bundle *extra)
240 {
241         bundle *b;
242         int lifecycle = -1;
243         bundle_raw *raw = NULL;
244         int len;
245
246         b = bundle_create();
247         if (!b) {
248                 LOGE("out of memory"); /* LCOV_EXCL_LINE */
249                 return -1; /* LCOV_EXCL_LINE */
250         }
251
252         bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
253         bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
254         bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
255
256         if (extra) {
257                 bundle_encode(extra, &raw, &len);
258                 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
259                 aul_widget_instance_add(class_id, instance_id);
260         }
261
262         LOGD("send update %s(%d) to %s", instance_id, status, __viewer_endpoint);
263         aul_app_com_send(__viewer_endpoint, b);
264
265         switch (status) {
266         case WIDGET_INSTANCE_EVENT_CREATE:
267                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
268                 break;
269         case WIDGET_INSTANCE_EVENT_DESTROY:
270                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
271                 break;
272         case WIDGET_INSTANCE_EVENT_PAUSE:
273                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
274                 break;
275         case WIDGET_INSTANCE_EVENT_RESUME:
276                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
277                 break;
278         }
279
280         if (lifecycle > -1)
281                 __send_lifecycle_event(class_id, instance_id, lifecycle);
282
283         bundle_free(b);
284         if (raw)
285                 free(raw);
286
287         return 0;
288 }
289
290 static void __control_create(const char *class_id, const char *id, bundle *b)
291 {
292         widget_base_instance_data *data;
293         char *content = NULL;
294         double *period = NULL;
295         size_t size;
296         int ret;
297
298         data = (widget_base_instance_data *)
299                         calloc(1, sizeof(widget_base_instance_data));
300         if (!data) {
301                 LOGE("Out of memory");
302                 return;
303         }
304
305         data->id = strdup(id);
306         data->args = b;
307         ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
308         if (ret == BUNDLE_ERROR_NONE) {
309                 data->period = *period;
310                 data->periodic_timer = g_timeout_add_seconds(data->period,
311                                 __timeout_cb, data);
312         }
313
314         /* call stub create */
315         appcore_multiwindow_base_instance_run(class_id, id, data);
316         data->args = NULL;
317         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
318         if (content)
319                 data->content = strdup(content);
320
321 }
322
323 static void __control_resume(const char *class_id, const char *id, bundle *b)
324 {
325         appcore_multiwindow_base_instance_h cxt;
326
327         cxt = appcore_multiwindow_base_instance_find(id);
328         if (!cxt) {
329                 LOGE("context not found: %s", id);
330                 return;
331         }
332
333         /* call stub resume */
334         appcore_multiwindow_base_instance_resume(cxt);
335 }
336
337 static void __control_pause(const char *class_id, const char *id, bundle *b)
338 {
339         appcore_multiwindow_base_instance_h instance_h;
340
341         instance_h = appcore_multiwindow_base_instance_find(id);
342
343         if (!instance_h) {
344                 LOGE("instance not found: %s", id);
345                 return;
346         }
347
348         /* call stub pause */
349         appcore_multiwindow_base_instance_pause(instance_h);
350 }
351
352 static void __control_resize(const char *class_id, const char *id, bundle *b)
353 {
354         appcore_multiwindow_base_instance_h instance_h;
355         char *remain = NULL;
356         char *w_str = NULL;
357         char *h_str = NULL;
358         int w = 0;
359         int h = 0;
360         void *class_data;
361         widget_base_class *cls;
362         const appcore_multiwindow_base_class *raw_cls;
363
364         instance_h = appcore_multiwindow_base_instance_find(id);
365         if (!instance_h) {
366                 LOGE("context not found: %s", id);
367                 return;
368         }
369
370         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
371         if (!raw_cls)
372                 return;
373
374         cls = __get_class(class_id);
375         if (cls == NULL) {
376                 LOGE("class not found: %s", class_id);
377                 return;
378         }
379         class_data = raw_cls->data;
380         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
381         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
382
383         if (w_str)
384                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
385
386         if (h_str)
387                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
388
389         if (cls->ops.resize)
390                 cls->ops.resize(instance_h, w, h, class_data);
391
392         LOGD("%s is resized to %dx%d", id, w, h);
393         __send_update_status(class_id, id,
394                 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
395 }
396
397 static void __call_update_cb(const char *class_id, const char *id, int force,
398                 const char *content_raw)
399 {
400         void *class_data;
401         widget_base_class *cls;
402         const appcore_multiwindow_base_class *raw_cls;
403         appcore_multiwindow_base_instance_h instance_h;
404         bundle *content = NULL;
405
406         instance_h = appcore_multiwindow_base_instance_find(id);
407         if (!instance_h) {
408                 LOGE("context not found: %s", id);
409                 return;
410         }
411
412         raw_cls = appcore_multiwindow_base_instance_get_class(instance_h);
413         if (!raw_cls) {
414                 LOGE("class is NULL");
415                 return;
416         }
417
418         class_data = raw_cls->data;
419         cls = __get_class(class_id);
420         if (cls == NULL) {
421                 LOGE("class not found: %s", class_id);
422                 return;
423         }
424
425         if (!cls->ops.update) {
426                 LOGE("update callback is NULL");
427                 return;
428         }
429
430         if (content_raw) {
431                 content = bundle_decode((const bundle_raw *)content_raw,
432                                 strlen(content_raw));
433         }
434
435         if (cls->ops.update)
436                 cls->ops.update(instance_h, content, force, class_data);
437
438         __send_update_status(class_id, id,
439                 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
440         LOGD("updated:%s", id);
441
442         if (content)
443                 bundle_free(content);
444 }
445
446 static void __update_process(const char *class_id, const char *id,
447                 appcore_multiwindow_base_instance_h instance_h, void *data)
448 {
449         char *content_raw = NULL;
450         char *force_str = NULL;
451         int force;
452         bundle *b = data;
453
454         if (!b) {
455                 LOGE("bundle is NULL");
456                 return;
457         }
458
459         bundle_get_str(b, WIDGET_K_FORCE, &force_str);
460
461         if (force_str && strcmp(force_str, "true") == 0)
462                 force = 1;
463         else
464                 force = 0;
465
466         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content_raw);
467         __call_update_cb(class_id, id, force, content_raw);
468 }
469
470 static void __control_update(const char *class_id, const char *id, bundle *b)
471 {
472         appcore_multiwindow_base_instance_h instance_h;
473
474         if (!id) {
475                 appcore_multiwindow_base_instance_foreach(class_id,
476                                 __update_process, b);
477                 return;
478         }
479
480         instance_h = appcore_multiwindow_base_instance_find(id);
481         if (!instance_h) {
482                 LOGE("context not found: %s", id);
483                 return;
484         }
485
486         __update_process(class_id, id, instance_h, b);
487 }
488
489 static void __control_destroy(const char *class_id, const char *id, bundle *b)
490 {
491         appcore_multiwindow_base_instance_h instance_h;
492         widget_base_instance_data *data;
493
494         instance_h = appcore_multiwindow_base_instance_find(id);
495         if (!instance_h) {
496                 LOGE("could not find widget obj: %s, clear amd info", id);
497                 aul_widget_instance_del(class_id, id);
498                 return;
499         }
500
501         data = (widget_base_instance_data *)
502                         appcore_multiwindow_base_instance_get_extra(instance_h);
503         data->args = b;
504
505         /* call stub terminate */
506         appcore_multiwindow_base_instance_exit(instance_h);
507         free(data->content);
508         free(data->id);
509         free(data);
510         __check_empty_instance();
511 }
512
513 static int __multiwindow_create(void *data)
514 {
515         char pkgid[256] = {0, };
516         int ret = 0;
517
518         appcore_multiwindow_base_on_create();
519         app_get_id(&__appid);
520         if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
521                 __package_id = strdup(pkgid);
522
523         if (!__package_id || !__appid) {
524                 LOGE("__package_id is NULL");
525                 return -1;
526         }
527
528         screen_connector_provider_init();
529         vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
530                         __on_poweroff, NULL);
531
532
533         if (__context.ops.create)
534                 ret = __context.ops.create(data);
535
536         LOGD("widget base is created");
537         return ret;
538 }
539
540 static int __multiwindow_terminate(void *data)
541 {
542         if (__context.ops.terminate)
543                 __context.ops.terminate(data);
544
545         vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
546                         __on_poweroff);
547         screen_connector_provider_fini();
548
549         if (__viewer_endpoint) {
550                 free(__viewer_endpoint);
551                 __viewer_endpoint = NULL;
552         }
553
554         if (__package_id) {
555                 free(__package_id);
556                 __package_id = NULL;
557         }
558
559         if (__appid) {
560                 free(__appid);
561                 __appid = NULL;
562         }
563
564         appcore_multiwindow_base_on_terminate();
565
566         LOGD("widget base is terminated");
567         return 0;
568 }
569
570 static int __multiwindow_control(bundle *b, void *data)
571 {
572         char *class_id = NULL;
573         char *id = NULL;
574         char *operation = NULL;
575
576         appcore_multiwindow_base_on_control(b);
577         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
578         /* for previous version compatibility, use appid for default class id */
579         if (class_id == NULL)
580                 class_id = __appid;
581
582         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
583         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
584
585         if (!operation) {
586                 LOGE("operation is NULL");
587                 return 0;
588         }
589
590         if (strcmp(operation, "create") == 0)
591                 __control_create(class_id, id, b);
592         else if (strcmp(operation, "resize") == 0)
593                 __control_resize(class_id, id, b);
594         else if (strcmp(operation, "update") == 0)
595                 __control_update(class_id, id, b);
596         else if (strcmp(operation, "destroy") == 0)
597                 __control_destroy(class_id, id, b);
598         else if (strcmp(operation, "resume") == 0)
599                 __control_resume(class_id, id, b);
600         else if (strcmp(operation, "pause") == 0)
601                 __control_pause(class_id, id, b);
602         else if (strcmp(operation, "terminate") == 0)
603                 __control_destroy(class_id, id, b);
604
605         return 0;
606 }
607
608 static void __inst_resume_cb(const char *class_id, const char *id,
609                 appcore_multiwindow_base_instance_h cxt, void *data)
610 {
611         __control_resume(class_id, id, data);
612 }
613
614 static void __get_content(bundle *b)
615 {
616         char *instance_id = NULL;
617         appcore_multiwindow_base_instance_h cxt;
618         widget_base_instance_data * we;
619
620         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
621         if (!instance_id) {
622                 LOGE("instance id is NULL");
623                 return;
624         }
625
626         cxt = appcore_multiwindow_base_instance_find(instance_id);
627         if (!cxt) {
628                 LOGE("could not find widget obj: %s", instance_id);
629                 return;
630         }
631
632         we = appcore_multiwindow_base_instance_get_extra(cxt);
633         if (!we) {
634                 LOGE("widget extra is NULL");
635                 return;
636         }
637
638         if (we->content) {
639                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
640                 LOGD("content info of %s found", instance_id);
641         } else {
642                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
643                 LOGD("empty content info added");
644         }
645 }
646
647 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
648 {
649         appcore_multiwindow_base_on_receive(type, b);
650
651         switch (type) {
652         case AUL_RESUME:
653                 appcore_multiwindow_base_instance_foreach_full(
654                                 __inst_resume_cb, b);
655                 break;
656         case AUL_TERMINATE:
657                 widget_base_exit();
658                 break;
659         case AUL_WIDGET_CONTENT:
660                 __get_content(b);
661                 break;
662         default:
663                 break;
664         }
665
666         return 0;
667 }
668
669 static void __multiwindow_init(int argc, char **argv, void *data)
670 {
671         if (__context.ops.init)
672                 __context.ops.init(argc, argv, data);
673 }
674
675 static void __multiwindow_finish(void)
676 {
677         if (__context.ops.finish) {
678                 __context.ops.finish();
679                 /* Check Loader case */
680                 if (getenv("AUL_LOADER_INIT")) {
681                         unsetenv("AUL_LOADER_INIT");
682                         __context.ops.finish();
683                 }
684         }
685 }
686
687 static void __multiwindow_run(void *data)
688 {
689         if (__context.ops.run)
690                 __context.ops.run(data);
691 }
692
693 static void __multiwindow_exit(void *data)
694 {
695         if (__context.ops.exit)
696                 __context.ops.exit(data);
697 }
698
699 EXPORT_API int widget_base_exit(void)
700 {
701         appcore_multiwindow_base_exit();
702         aul_notify_exit();
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, 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
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, WIDGET_K_ENDPOINT, &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 EXPORT_API int widget_base_on_create(void)
1125 {
1126         appcore_multiwindow_base_on_create();
1127
1128         return 0;
1129 }
1130
1131 EXPORT_API int widget_base_on_terminate(void)
1132 {
1133         appcore_multiwindow_base_on_terminate();
1134
1135         return 0;
1136 }
1137
1138 EXPORT_API int widget_base_on_init(int argc, char **argv)
1139 {
1140         return 0;
1141 }
1142
1143 EXPORT_API void widget_base_on_finish(void)
1144 {
1145 }
1146
1147 EXPORT_API void widget_base_on_run(void)
1148 {
1149 }
1150
1151 EXPORT_API void widget_base_on_exit(void)
1152 {
1153 }
1154
1155 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1156 {
1157         widget_base_ops ops;
1158
1159         /* override methods */
1160         ops.create = __on_create;
1161         ops.terminate = __on_terminate;
1162         ops.init = __on_init;
1163         ops.finish = __on_finish;
1164         ops.run = __on_run;
1165         ops.exit = __on_exit;
1166
1167         return ops;
1168 }
1169
1170 static void __free_class(gpointer data)
1171 {
1172         widget_base_class *cls = data;
1173
1174         free(cls->id);
1175         free(cls);
1176 }
1177
1178 EXPORT_API void widget_base_fini(void)
1179 {
1180         g_list_free_full(__context.classes, __free_class);
1181         __context.classes = NULL;
1182
1183         appcore_multiwindow_base_fini();
1184 }
1185
1186 EXPORT_API int widget_base_context_window_bind(
1187                 widget_base_instance_h instance_h, const char *id,
1188                 Ecore_Wl_Window *wl_win)
1189 {
1190         struct wl_surface *surface;
1191
1192         surface = ecore_wl_window_surface_get(wl_win);
1193         if (surface == NULL) {
1194                 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1195                 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1196         }
1197
1198         screen_connector_provider_remote_enable(id, surface);
1199         appcore_multiwindow_base_window_bind(instance_h, wl_win);
1200
1201         return WIDGET_BASE_ERROR_NONE;
1202 }
1203
1204 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1205                 int w, int h, void *class_data)
1206 {
1207         return widget_base_class_on_create(instance_h, content, w, h);
1208 }
1209
1210 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1211 {
1212         return widget_base_class_on_resume(instance_h);
1213 }
1214
1215 static int __class_on_pause(widget_base_instance_h instance_h,
1216                 void *class_data)
1217 {
1218         return widget_base_class_on_pause(instance_h);
1219 }
1220
1221 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1222                 void *class_data)
1223 {
1224         return widget_base_class_on_resize(instance_h, w, h);
1225 }
1226
1227 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1228                 int force, void *class_data)
1229 {
1230         return widget_base_class_on_update(instance_h, content, force);
1231 }
1232
1233 static int __class_on_destroy(widget_base_instance_h instance_h,
1234                 widget_base_destroy_type_e reason, bundle *content,
1235                 void *class_data)
1236 {
1237         return widget_base_class_on_destroy(instance_h, reason, content);
1238 }
1239
1240 static void __multiwindow_instance_create(
1241                 appcore_multiwindow_base_instance_h instance_h,
1242                 void *class_data)
1243 {
1244         widget_base_instance_data *instance_data;
1245         bundle *b;
1246         bundle *content_info = NULL;
1247         char *id = NULL;
1248         char *class_id = NULL;
1249         char *operation = NULL;
1250         char *content = NULL;
1251         char *w_str = NULL;
1252         char *h_str = NULL;
1253         char *remain = NULL;
1254         int w = 0;
1255         int h = 0;
1256         int ret = -1;
1257         widget_base_class *cls;
1258
1259         appcore_multiwindow_base_class_on_create(instance_h);
1260         instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1261         b = instance_data->args;
1262
1263         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1264         /* for previous version compatibility, use appid for default class id */
1265         if (class_id == NULL)
1266                 class_id = __appid;
1267
1268         cls = __get_class(class_id);
1269         if (cls == NULL) {
1270                 LOGE("class not found: %s", class_id);
1271                 return;
1272         }
1273
1274         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1275         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1276
1277         if (!operation) {
1278                 LOGE("no operation provided");
1279                 return;
1280         }
1281
1282         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1283         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1284         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1285
1286         if (w_str)
1287                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1288
1289         if (h_str)
1290                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1291
1292         if (content)
1293                 content_info = bundle_decode((const bundle_raw *)content,
1294                                 strlen(content));
1295
1296         if (cls->ops.create)
1297                 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1298
1299         if (ret < 0) {
1300                 LOGW("Create callback returns error(%d)", ret);
1301                 ret = __send_update_status(class_id, id,
1302                                 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, NULL);
1303                 __instance_drop(instance_h);
1304         } else {
1305                 LOGD("%s is created", id);
1306                 ret = __send_update_status(class_id, id,
1307                         WIDGET_INSTANCE_EVENT_CREATE, NULL);
1308
1309                 aul_widget_instance_add(class_id, id);
1310         }
1311
1312         if (content_info)
1313                 bundle_free(content_info);
1314 }
1315
1316 static void __multiwindow_instance_resume(
1317                 appcore_multiwindow_base_instance_h instance_h,
1318                 void *class_data)
1319 {
1320         const char *id;
1321         const char *class_id;
1322         widget_base_class *cls;
1323         widget_base_instance_data *data;
1324
1325         appcore_multiwindow_base_class_on_resume(instance_h);
1326         id = appcore_multiwindow_base_instance_get_id(instance_h);
1327         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1328         cls = __get_class(class_id);
1329         if (cls == NULL) {
1330                 LOGE("class not found: %s", class_id);
1331                 return;
1332         }
1333
1334         data = (widget_base_instance_data *)
1335                         appcore_multiwindow_base_instance_get_extra(instance_h);
1336
1337         if (data->pending_update) {
1338                 LOGD("pending update!");
1339                 data->pending_update = false;
1340                 __control_update(class_id, data->id, data->args);
1341                 if (data->period > 0) {
1342                         LOGD("Restart timer!");
1343                         data->periodic_timer = g_timeout_add_seconds(
1344                                         data->period,
1345                                         __timeout_cb, data);
1346                 }
1347         }
1348
1349         if (cls->ops.resume)
1350                 cls->ops.resume(instance_h, class_data);
1351
1352         LOGD("%s is resumed", id);
1353         __send_update_status(class_id, id,
1354                 WIDGET_INSTANCE_EVENT_RESUME, NULL);
1355
1356         if (!__fg_signal) {
1357                 LOGD("Send fg signal to resourceD");
1358                 aul_send_app_status_change_signal(getpid(),
1359                                 __appid,
1360                                 __package_id,
1361                                 STATUS_FOREGROUND,
1362                                 APP_TYPE_WIDGET);
1363                 __fg_signal = true;
1364         }
1365 }
1366
1367 static void __multiwindow_instance_pause(
1368                 appcore_multiwindow_base_instance_h instance_h,
1369                 void *class_data)
1370 {
1371         const char *id;
1372         const char *class_id;
1373         widget_base_class *cls;
1374
1375         appcore_multiwindow_base_class_on_pause(instance_h);
1376         id = appcore_multiwindow_base_instance_get_id(instance_h);
1377         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1378         cls = __get_class(class_id);
1379         if (cls == NULL) {
1380                 LOGE("class not found: %s", class_id);
1381                 return;
1382         }
1383
1384         if (cls->ops.pause)
1385                 cls->ops.pause(instance_h, class_data);
1386
1387         LOGD("%s is paused", id);
1388         __send_update_status(class_id, id,
1389                 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
1390
1391         if (__fg_signal) {
1392                 LOGD("Send bg signal to resourceD");
1393                 aul_send_app_status_change_signal(getpid(),
1394                                 __appid,
1395                                 __package_id,
1396                                 STATUS_BACKGROUND,
1397                                 APP_TYPE_WIDGET);
1398                 __fg_signal = false;
1399         }
1400 }
1401
1402 static void __multiwindow_instance_terminate(
1403                 appcore_multiwindow_base_instance_h instance_h,
1404                 void *class_data)
1405 {
1406         widget_base_instance_data *data;
1407         bundle *b;
1408         char *operation = NULL;
1409         bundle *content_info;
1410         widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1411         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1412         const char *id;
1413         const char *class_id;
1414         widget_base_class *cls;
1415
1416         id = appcore_multiwindow_base_instance_get_id(instance_h);
1417         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1418         data  = appcore_multiwindow_base_instance_get_extra(
1419                         (appcore_multiwindow_base_instance_h)instance_h);
1420         b = data->args;
1421         cls = __get_class(class_id);
1422         if (cls == NULL) {
1423                 LOGE("class not found: %s", class_id);
1424                 return;
1425         }
1426
1427         if (b) {
1428                 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1429                 if (operation && strcmp(operation, "destroy") == 0)
1430                         reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1431         }
1432
1433         if (data->content)
1434                 content_info = bundle_decode((const bundle_raw *)data->content,
1435                                 strlen(data->content));
1436         else
1437                 content_info = bundle_create();
1438
1439         if (cls->ops.destroy)
1440                 cls->ops.destroy(instance_h, reason, content_info, class_data);
1441
1442         LOGD("%s is destroyed %d", id, reason);
1443         if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1444                 event = WIDGET_INSTANCE_EVENT_DESTROY;
1445                 aul_widget_instance_del(class_id, id);
1446         } else {
1447                 __send_update_status(class_id, id,
1448                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED,
1449                                 content_info);
1450         }
1451
1452         if (content_info)
1453                 bundle_free(content_info);
1454
1455         if (data->periodic_timer)
1456                 g_source_remove(data->periodic_timer);
1457
1458         __send_update_status(class_id, id, event, NULL);
1459         appcore_multiwindow_base_class_on_terminate(instance_h);
1460 }
1461
1462 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1463                 bundle *content, int w, int h)
1464 {
1465         appcore_multiwindow_base_class_on_create(instance_h);
1466
1467         return 0;
1468 }
1469
1470 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1471 {
1472         appcore_multiwindow_base_class_on_pause(instance_h);
1473
1474         return 0;
1475 }
1476
1477 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1478 {
1479         appcore_multiwindow_base_class_on_resume(instance_h);
1480
1481         return 0;
1482 }
1483
1484 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1485                 int w, int h)
1486 {
1487         return 0;
1488 }
1489
1490 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1491                 bundle *content, int force)
1492 {
1493         return 0;
1494 }
1495
1496 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1497                 widget_base_destroy_type_e reason, bundle *content)
1498 {
1499         appcore_multiwindow_base_class_on_terminate(instance_h);
1500
1501         return 0;
1502 }
1503
1504 EXPORT_API widget_base_class widget_base_class_get_default(void)
1505 {
1506         widget_base_class cls;
1507
1508         cls.ops.create = __class_on_create;
1509         cls.ops.resize = __class_on_resize;
1510         cls.ops.update = __class_on_update;
1511         cls.ops.destroy = __class_on_destroy;
1512         cls.ops.pause = __class_on_pause;
1513         cls.ops.resume = __class_on_resume;
1514
1515         return cls;
1516 }
1517
1518 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1519                 const char *class_id, void *class_data)
1520 {
1521         widget_base_class *c;
1522         appcore_multiwindow_base_class raw_cls;
1523
1524         if (!__is_widget_feature_enabled()) {
1525                 LOGE("not supported");
1526                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1527                 return NULL;
1528         }
1529
1530         if (!class_id) {
1531                 LOGE("class is is NULL");
1532                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1533                 return NULL;
1534         }
1535
1536         raw_cls.id = strdup(class_id);
1537         raw_cls.data = class_data;
1538         raw_cls.create = __multiwindow_instance_create;
1539         raw_cls.terminate = __multiwindow_instance_terminate;
1540         raw_cls.pause = __multiwindow_instance_pause;
1541         raw_cls.resume = __multiwindow_instance_resume;
1542         appcore_multiwindow_base_class_add(raw_cls);
1543
1544         c = malloc(sizeof(widget_base_class));
1545         if (!c)
1546                 return NULL;
1547
1548         *c = cls;
1549         c->id = strdup(class_id);
1550         __context.classes = g_list_append(__context.classes, c);
1551
1552         return c;
1553 }