Implement widget operation for 'period'
[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 void __control_change_period(const char *class_id, const char *id,
514                 bundle *b)
515 {
516         appcore_multiwindow_base_instance_h instance_h;
517         widget_base_instance_data *data;
518         double *period = NULL;
519         size_t size;
520         int ret;
521
522         instance_h = appcore_multiwindow_base_instance_find(id);
523         if (!instance_h) {
524                 LOGE("context not found: %s", id);
525                 return;
526         }
527
528         data = (widget_base_instance_data *)
529                         appcore_multiwindow_base_instance_get_extra(instance_h);
530
531         if (!data) {
532                 LOGE("could not find instance data: %s", id);
533                 return;
534         }
535
536         if (data->periodic_timer) {
537                 LOGD("Remove timer!");
538                 g_source_remove(data->periodic_timer);
539                 data->periodic_timer = 0;
540         }
541
542         ret = bundle_get_byte(b, WIDGET_K_PERIOD, (void **)&period, &size);
543         if (ret == BUNDLE_ERROR_NONE)
544                 data->period = *period;
545
546         if (data->period > 0) {
547                 LOGD("Restart timer!");
548                 data->periodic_timer = g_timeout_add_seconds(data->period,
549                                 __timeout_cb, data);
550         }
551
552         return;
553 }
554
555 static int __multiwindow_create(void *data)
556 {
557         char pkgid[256] = {0, };
558         int ret = 0;
559
560         appcore_multiwindow_base_on_create();
561         app_get_id(&__appid);
562         if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) == 0)
563                 __package_id = strdup(pkgid);
564
565         if (!__package_id || !__appid) {
566                 LOGE("__package_id is NULL");
567                 return -1;
568         }
569
570         screen_connector_provider_init();
571         vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
572                         __on_poweroff, NULL);
573
574
575         if (__context.ops.create)
576                 ret = __context.ops.create(data);
577
578         LOGD("widget base is created");
579         return ret;
580 }
581
582 static int __multiwindow_terminate(void *data)
583 {
584         if (__context.ops.terminate)
585                 __context.ops.terminate(data);
586
587         vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
588                         __on_poweroff);
589         screen_connector_provider_fini();
590
591         if (__viewer_endpoint) {
592                 free(__viewer_endpoint);
593                 __viewer_endpoint = NULL;
594         }
595
596         if (__package_id) {
597                 free(__package_id);
598                 __package_id = NULL;
599         }
600
601         if (__appid) {
602                 free(__appid);
603                 __appid = NULL;
604         }
605
606         appcore_multiwindow_base_on_terminate();
607
608         LOGD("widget base is terminated");
609         return 0;
610 }
611
612 static int __multiwindow_control(bundle *b, void *data)
613 {
614         char *class_id = NULL;
615         char *id = NULL;
616         char *operation = NULL;
617
618         appcore_multiwindow_base_on_control(b);
619         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
620         /* for previous version compatibility, use appid for default class id */
621         if (class_id == NULL)
622                 class_id = __appid;
623
624         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
625         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
626
627         if (!operation) {
628                 LOGE("operation is NULL");
629                 return 0;
630         }
631
632         if (strcmp(operation, "create") == 0)
633                 __control_create(class_id, id, b);
634         else if (strcmp(operation, "resize") == 0)
635                 __control_resize(class_id, id, b);
636         else if (strcmp(operation, "update") == 0)
637                 __control_update(class_id, id, b);
638         else if (strcmp(operation, "destroy") == 0)
639                 __control_destroy(class_id, id, b);
640         else if (strcmp(operation, "resume") == 0)
641                 __control_resume(class_id, id, b);
642         else if (strcmp(operation, "pause") == 0)
643                 __control_pause(class_id, id, b);
644         else if (strcmp(operation, "terminate") == 0)
645                 __control_destroy(class_id, id, b);
646         else if (strcmp(operation, "period") == 0)
647                 __control_change_period(class_id, id, b);
648
649         return 0;
650 }
651
652 static void __inst_resume_cb(const char *class_id, const char *id,
653                 appcore_multiwindow_base_instance_h cxt, void *data)
654 {
655         __control_resume(class_id, id, data);
656 }
657
658 static void __get_content(bundle *b)
659 {
660         char *instance_id = NULL;
661         appcore_multiwindow_base_instance_h cxt;
662         widget_base_instance_data * we;
663
664         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
665         if (!instance_id) {
666                 LOGE("instance id is NULL");
667                 return;
668         }
669
670         cxt = appcore_multiwindow_base_instance_find(instance_id);
671         if (!cxt) {
672                 LOGE("could not find widget obj: %s", instance_id);
673                 return;
674         }
675
676         we = appcore_multiwindow_base_instance_get_extra(cxt);
677         if (!we) {
678                 LOGE("widget extra is NULL");
679                 return;
680         }
681
682         if (we->content) {
683                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, we->content);
684                 LOGD("content info of %s found", instance_id);
685         } else {
686                 bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
687                 LOGD("empty content info added");
688         }
689 }
690
691 static int __multiwindow_receive(aul_type type, bundle *b, void *data)
692 {
693         appcore_multiwindow_base_on_receive(type, b);
694
695         switch (type) {
696         case AUL_RESUME:
697                 appcore_multiwindow_base_instance_foreach_full(
698                                 __inst_resume_cb, b);
699                 break;
700         case AUL_TERMINATE:
701                 widget_base_exit();
702                 break;
703         case AUL_WIDGET_CONTENT:
704                 __get_content(b);
705                 break;
706         default:
707                 break;
708         }
709
710         return 0;
711 }
712
713 static void __multiwindow_init(int argc, char **argv, void *data)
714 {
715         if (__context.ops.init)
716                 __context.ops.init(argc, argv, data);
717 }
718
719 static void __multiwindow_finish(void)
720 {
721         if (__context.ops.finish) {
722                 __context.ops.finish();
723                 /* Check Loader case */
724                 if (getenv("AUL_LOADER_INIT")) {
725                         unsetenv("AUL_LOADER_INIT");
726                         __context.ops.finish();
727                 }
728         }
729 }
730
731 static void __multiwindow_run(void *data)
732 {
733         if (__context.ops.run)
734                 __context.ops.run(data);
735 }
736
737 static void __multiwindow_exit(void *data)
738 {
739         if (__context.ops.exit)
740                 __context.ops.exit(data);
741 }
742
743 EXPORT_API int widget_base_exit(void)
744 {
745         appcore_multiwindow_base_exit();
746         aul_notify_exit();
747
748         return 0;
749 }
750
751 static gboolean __finish_event_cb(gpointer user_data)
752 {
753         appcore_multiwindow_base_instance_h cxt = user_data;
754         bundle *b;
755         const char *id;
756         const char *class_id;
757
758         if (!cxt) {
759                 LOGE("user_data is NULL");
760                 return FALSE;
761         }
762
763         id = appcore_multiwindow_base_instance_get_id(cxt);
764         class_id = appcore_multiwindow_base_instance_get_class_id(cxt);
765         b = bundle_create();
766
767         if (!b) {
768                 LOGE("Out-of-memory");
769                 return FALSE;
770         }
771
772         bundle_add_str(b, WIDGET_K_OPERATION, "terminate");
773         __control_destroy(class_id, id, b);
774         bundle_free(b);
775
776         return FALSE;
777 }
778
779 EXPORT_API int widget_base_terminate_context(widget_base_instance_h context)
780 {
781         if (!__is_widget_feature_enabled()) {
782                 LOGE("not supported"); /* LCOV_EXCL_LINE */
783                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
784         }
785
786         if (!context) {
787                 LOGE("context is null");
788                 return WIDGET_ERROR_INVALID_PARAMETER;
789         }
790
791         g_idle_add(__finish_event_cb, context);
792
793         return WIDGET_ERROR_NONE;
794 }
795
796 static void __inst_full_cb(const char *class_id, const char *id,
797                 appcore_multiwindow_base_instance_h cxt, void *data)
798 {
799         struct widget_foreach_context *foreach_context = data;
800
801         if (!data)
802                 return;
803
804         if (foreach_context->callback)
805                 foreach_context->callback(cxt, foreach_context->data);
806 }
807
808 EXPORT_API int widget_base_foreach_context(widget_base_instance_cb cb, void *data)
809 {
810         struct widget_foreach_context foreach_context;
811
812         if (!__is_widget_feature_enabled()) {
813                 LOGE("not supported"); /* LCOV_EXCL_LINE */
814                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
815         }
816
817         if (!cb) {
818                 LOGE("callback is NULL");
819                 return WIDGET_ERROR_INVALID_PARAMETER;
820         }
821
822         foreach_context.callback = cb;
823         foreach_context.data = data;
824         appcore_multiwindow_base_instance_foreach_full(__inst_full_cb, &foreach_context);
825
826         return WIDGET_ERROR_NONE;
827 }
828
829 static int __event_cb(void *event, void *data)
830 {
831         app_event_handler_h handler = data;
832
833         struct app_event_info app_event;
834
835         app_event.type = handler->type;
836         app_event.value = event;
837
838         if (handler->cb)
839                 handler->cb(&app_event, handler->data);
840
841         return 0;
842 }
843
844 EXPORT_API int widget_base_add_event_handler(app_event_handler_h *event_handler,
845                                         app_event_type_e event_type,
846                                         app_event_cb callback,
847                                         void *user_data)
848 {
849         int r;
850         bool feature;
851         app_event_handler_h handler;
852
853         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
854         if (r < 0)
855                 return WIDGET_BASE_ERROR_FAULT;
856
857         if (!feature)
858                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
859
860         if (event_handler == NULL || callback == NULL)
861                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
862
863         if (event_type < APP_EVENT_LOW_MEMORY
864             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
865                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
866
867         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
868                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
869
870
871         handler = calloc(1, sizeof(struct app_event_handler));
872         if (!handler)
873                 return WIDGET_BASE_ERROR_OUT_OF_MEMORY;
874
875         handler->type = event_type;
876         handler->cb = callback;
877         handler->data = user_data;
878         handler->raw = appcore_base_add_event(
879                         __app_event_converter[event_type], __event_cb, handler);
880         *event_handler = handler;
881
882         return WIDGET_BASE_ERROR_NONE;
883 }
884
885 EXPORT_API int widget_base_remove_event_handler(app_event_handler_h
886                                                 event_handler)
887 {
888         int r;
889         bool feature;
890         app_event_type_e type;
891
892         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
893         if (r < 0)
894                 return WIDGET_BASE_ERROR_FAULT;
895
896         if (!feature)
897                 return WIDGET_BASE_ERROR_NOT_SUPPORTED;
898
899         if (event_handler == NULL)
900                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
901
902         type = event_handler->type;
903         if (type < APP_EVENT_LOW_MEMORY ||
904                         type > APP_EVENT_REGION_FORMAT_CHANGED)
905                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
906
907         r = appcore_base_remove_event(event_handler->raw);
908         if (r < 0)
909                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
910
911         free(event_handler);
912
913         return WIDGET_BASE_ERROR_NONE;
914 }
915
916 EXPORT_API int widget_base_context_set_content_info(
917                 widget_base_instance_h context,
918                 bundle *content_info)
919 {
920         int ret = 0;
921         bundle_raw *raw = NULL;
922         int len;
923         const char *id;
924         const char *class_id;
925         widget_base_instance_data *data;
926         appcore_multiwindow_base_instance_h instance_h;
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 || !content_info)
934                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
935
936         instance_h = (appcore_multiwindow_base_instance_h)context;
937         id = appcore_multiwindow_base_instance_get_id(instance_h);
938         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
939         data = appcore_multiwindow_base_instance_get_extra(instance_h);
940
941         if (!class_id || !id || !data)
942                 return WIDGET_BASE_ERROR_FAULT;
943
944         ret = __send_update_status(class_id, id,
945                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
946
947         if (data->content)
948                 free(data->content);
949
950         bundle_encode(content_info, &raw, &len);
951         if (raw)
952                 data->content = strdup((const char *)raw);
953         else
954                 data->content = NULL;
955
956         free(raw);
957         if (ret < 0) {
958                 /* LCOV_EXCL_START */
959                 LOGE("failed to send content info: %s of %s (%d)", id,
960                                 class_id, ret);
961                 return WIDGET_BASE_ERROR_IO_ERROR;
962                 /* LCOV_EXCL_STOP */
963         }
964
965         return WIDGET_BASE_ERROR_NONE;
966 }
967
968 EXPORT_API int widget_base_context_get_tag(widget_base_instance_h context, void **tag)
969 {
970         appcore_multiwindow_base_instance_h instance_h;
971         widget_base_instance_data *data;
972
973         if (!__is_widget_feature_enabled()) {
974                 LOGE("not supported"); /* LCOV_EXCL_LINE */
975                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
976         }
977
978         if (!context || !tag) {
979                 LOGE("Invalid parameter");
980                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
981         }
982
983         instance_h = (appcore_multiwindow_base_instance_h)context;
984         data = (widget_base_instance_data *)
985                         appcore_multiwindow_base_instance_get_extra(instance_h);
986
987         if (!data) {
988                 LOGE("Invalid parameter");
989                 return WIDGET_ERROR_INVALID_PARAMETER;
990         }
991
992         *tag = data->tag;
993
994         return WIDGET_BASE_ERROR_NONE;
995 }
996
997 EXPORT_API int widget_base_context_set_tag(widget_base_instance_h context, void *tag)
998 {
999         appcore_multiwindow_base_instance_h instance_h;
1000         widget_base_instance_data *data;
1001
1002         if (!__is_widget_feature_enabled()) {
1003                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1004                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1005         }
1006
1007         if (!context) {
1008                 LOGE("Invalid parameter");
1009                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1010         }
1011
1012         instance_h = (appcore_multiwindow_base_instance_h)context;
1013         data = (widget_base_instance_data *)
1014                         appcore_multiwindow_base_instance_get_extra(instance_h);
1015         data->tag = tag;
1016
1017         return WIDGET_BASE_ERROR_NONE;
1018 }
1019
1020 EXPORT_API void *widget_base_context_get_user_data(
1021                 widget_base_instance_h context)
1022 {
1023         appcore_multiwindow_base_instance_h instance_h;
1024         widget_base_instance_data *data;
1025
1026         if (!__is_widget_feature_enabled()) {
1027                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1028                 return NULL; /* LCOV_EXCL_LINE */
1029         }
1030
1031         if (!context) {
1032                 LOGE("Invalid parameter");
1033                 return NULL;
1034         }
1035
1036         instance_h = (appcore_multiwindow_base_instance_h)context;
1037         data = (widget_base_instance_data *)
1038                         appcore_multiwindow_base_instance_get_extra(instance_h);
1039
1040         return data->user_data;
1041 }
1042
1043
1044 EXPORT_API int widget_base_context_set_user_data(
1045                 widget_base_instance_h context, void *user_data)
1046 {
1047         appcore_multiwindow_base_instance_h instance_h;
1048         widget_base_instance_data *data;
1049
1050         if (!__is_widget_feature_enabled()) {
1051                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1052                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1053         }
1054
1055         if (!context) {
1056                 LOGE("Invalid parameter");
1057                 return WIDGET_BASE_ERROR_INVALID_PARAMETER;
1058         }
1059
1060         instance_h = (appcore_multiwindow_base_instance_h)context;
1061         data = (widget_base_instance_data *)
1062                         appcore_multiwindow_base_instance_get_extra(instance_h);
1063         data->user_data = user_data;
1064
1065         return WIDGET_BASE_ERROR_NONE;
1066 }
1067
1068 EXPORT_API int widget_base_context_get_id(widget_base_instance_h context, char **id)
1069 {
1070         appcore_multiwindow_base_instance_h instance_h;
1071
1072         if (!__is_widget_feature_enabled()) {
1073                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1074                 return WIDGET_BASE_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1075         }
1076
1077         instance_h = (appcore_multiwindow_base_instance_h)context;
1078         *id = (char *)appcore_multiwindow_base_instance_get_id(instance_h);
1079
1080         return WIDGET_BASE_ERROR_NONE;
1081 }
1082
1083 EXPORT_API const char *widget_base_get_viewer_endpoint()
1084 {
1085         return __viewer_endpoint;
1086 }
1087
1088 EXPORT_API int widget_base_init(widget_base_ops ops, int argc, char **argv,
1089                 void *data)
1090 {
1091         bundle *kb;
1092         char *viewer_endpoint = NULL;
1093         appcore_multiwindow_base_ops raw_ops
1094                         = appcore_multiwindow_base_get_default_ops();
1095
1096         __context.ops = ops;
1097         __context.argc = argc;
1098         __context.argv = argv;
1099         __context.data = data;
1100
1101         /* override methods */
1102         raw_ops.base.create = __multiwindow_create;
1103         raw_ops.base.control = __multiwindow_control;
1104         raw_ops.base.terminate = __multiwindow_terminate;
1105         raw_ops.base.receive = __multiwindow_receive;
1106         raw_ops.base.init = __multiwindow_init;
1107         raw_ops.base.finish = __multiwindow_finish;
1108         raw_ops.base.run = __multiwindow_run;
1109         raw_ops.base.exit = __multiwindow_exit;
1110
1111         if (!__is_widget_feature_enabled()) {
1112                 LOGE("not supported"); /* LCOV_EXCL_LINE */
1113                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1114         }
1115
1116         kb = bundle_import_from_argv(argc, argv);
1117         if (kb) {
1118                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
1119                 if (viewer_endpoint) {
1120                         LOGD("viewer endpoint :%s", viewer_endpoint);
1121                         __viewer_endpoint = strdup(viewer_endpoint);
1122                 } else {
1123                         LOGE("endpoint is missing");
1124                 }
1125
1126                 bundle_free(kb);
1127         } else {
1128                 LOGE("failed to get launch argv"); /* LCOV_EXCL_LINE */
1129                 return WIDGET_ERROR_FAULT;
1130         }
1131
1132         if (appcore_multiwindow_base_init(raw_ops, argc, argv, data) < 0)
1133                return WIDGET_ERROR_FAULT;
1134
1135         return WIDGET_ERROR_NONE;
1136 }
1137
1138 static int __on_create(void *data)
1139 {
1140         return widget_base_on_create();
1141 }
1142
1143 static int __on_terminate(void *data)
1144 {
1145         return widget_base_on_terminate();
1146 }
1147
1148 static void __on_init(int argc, char **argv, void *data)
1149 {
1150         widget_base_on_init(argc, argv);
1151 }
1152
1153 static void __on_finish(void)
1154 {
1155         widget_base_on_finish();
1156 }
1157
1158 static void __on_run(void *data)
1159 {
1160         widget_base_on_run();
1161 }
1162
1163 static void __on_exit(void *data)
1164 {
1165         widget_base_on_exit();
1166 }
1167
1168 EXPORT_API int widget_base_on_create(void)
1169 {
1170         appcore_multiwindow_base_on_create();
1171
1172         return 0;
1173 }
1174
1175 EXPORT_API int widget_base_on_terminate(void)
1176 {
1177         appcore_multiwindow_base_on_terminate();
1178
1179         return 0;
1180 }
1181
1182 EXPORT_API int widget_base_on_init(int argc, char **argv)
1183 {
1184         return 0;
1185 }
1186
1187 EXPORT_API void widget_base_on_finish(void)
1188 {
1189 }
1190
1191 EXPORT_API void widget_base_on_run(void)
1192 {
1193 }
1194
1195 EXPORT_API void widget_base_on_exit(void)
1196 {
1197 }
1198
1199 EXPORT_API widget_base_ops widget_base_get_default_ops(void)
1200 {
1201         widget_base_ops ops;
1202
1203         /* override methods */
1204         ops.create = __on_create;
1205         ops.terminate = __on_terminate;
1206         ops.init = __on_init;
1207         ops.finish = __on_finish;
1208         ops.run = __on_run;
1209         ops.exit = __on_exit;
1210
1211         return ops;
1212 }
1213
1214 static void __free_class(gpointer data)
1215 {
1216         widget_base_class *cls = data;
1217
1218         free(cls->id);
1219         free(cls);
1220 }
1221
1222 EXPORT_API void widget_base_fini(void)
1223 {
1224         g_list_free_full(__context.classes, __free_class);
1225         __context.classes = NULL;
1226
1227         appcore_multiwindow_base_fini();
1228 }
1229
1230 EXPORT_API int widget_base_context_window_bind(
1231                 widget_base_instance_h instance_h, const char *id,
1232                 Ecore_Wl_Window *wl_win)
1233 {
1234         struct wl_surface *surface;
1235
1236         surface = ecore_wl_window_surface_get(wl_win);
1237         if (surface == NULL) {
1238                 LOGE("failed to get surface"); /* LCOV_EXCL_LINE */
1239                 return WIDGET_BASE_ERROR_FAULT; /* LCOV_EXCL_LINE */
1240         }
1241
1242         screen_connector_provider_remote_enable(id, surface);
1243         appcore_multiwindow_base_window_bind(instance_h, wl_win);
1244
1245         return WIDGET_BASE_ERROR_NONE;
1246 }
1247
1248 static int __class_on_create(widget_base_instance_h instance_h, bundle *content,
1249                 int w, int h, void *class_data)
1250 {
1251         return widget_base_class_on_create(instance_h, content, w, h);
1252 }
1253
1254 static int __class_on_resume(widget_base_instance_h instance_h, void *class_data)
1255 {
1256         return widget_base_class_on_resume(instance_h);
1257 }
1258
1259 static int __class_on_pause(widget_base_instance_h instance_h,
1260                 void *class_data)
1261 {
1262         return widget_base_class_on_pause(instance_h);
1263 }
1264
1265 static int __class_on_resize(widget_base_instance_h instance_h, int w, int h,
1266                 void *class_data)
1267 {
1268         return widget_base_class_on_resize(instance_h, w, h);
1269 }
1270
1271 static int __class_on_update(widget_base_instance_h instance_h, bundle *content,
1272                 int force, void *class_data)
1273 {
1274         return widget_base_class_on_update(instance_h, content, force);
1275 }
1276
1277 static int __class_on_destroy(widget_base_instance_h instance_h,
1278                 widget_base_destroy_type_e reason, bundle *content,
1279                 void *class_data)
1280 {
1281         return widget_base_class_on_destroy(instance_h, reason, content);
1282 }
1283
1284 static void __multiwindow_instance_create(
1285                 appcore_multiwindow_base_instance_h instance_h,
1286                 void *class_data)
1287 {
1288         widget_base_instance_data *instance_data;
1289         bundle *b;
1290         bundle *content_info = NULL;
1291         char *id = NULL;
1292         char *class_id = NULL;
1293         char *operation = NULL;
1294         char *content = NULL;
1295         char *w_str = NULL;
1296         char *h_str = NULL;
1297         char *remain = NULL;
1298         int w = 0;
1299         int h = 0;
1300         int ret = -1;
1301         widget_base_class *cls;
1302
1303         appcore_multiwindow_base_class_on_create(instance_h);
1304         instance_data = appcore_multiwindow_base_instance_get_extra(instance_h);
1305         b = instance_data->args;
1306
1307         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
1308         /* for previous version compatibility, use appid for default class id */
1309         if (class_id == NULL)
1310                 class_id = __appid;
1311
1312         cls = __get_class(class_id);
1313         if (cls == NULL) {
1314                 LOGE("class not found: %s", class_id);
1315                 return;
1316         }
1317
1318         bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
1319         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1320
1321         if (!operation) {
1322                 LOGE("no operation provided");
1323                 return;
1324         }
1325
1326         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
1327         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
1328         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
1329
1330         if (w_str)
1331                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
1332
1333         if (h_str)
1334                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
1335
1336         if (content)
1337                 content_info = bundle_decode((const bundle_raw *)content,
1338                                 strlen(content));
1339
1340         if (cls->ops.create)
1341                 ret = cls->ops.create(instance_h, content_info, w, h, class_data);
1342
1343         if (ret < 0) {
1344                 LOGW("Create callback returns error(%d)", ret);
1345                 ret = __send_update_status(class_id, id,
1346                                 WIDGET_INSTANCE_EVENT_CREATE_ABORTED, NULL);
1347                 __instance_drop(instance_h);
1348         } else {
1349                 LOGD("%s is created", id);
1350                 ret = __send_update_status(class_id, id,
1351                         WIDGET_INSTANCE_EVENT_CREATE, NULL);
1352
1353                 aul_widget_instance_add(class_id, id);
1354         }
1355
1356         if (content_info)
1357                 bundle_free(content_info);
1358 }
1359
1360 static void __multiwindow_instance_resume(
1361                 appcore_multiwindow_base_instance_h instance_h,
1362                 void *class_data)
1363 {
1364         const char *id;
1365         const char *class_id;
1366         widget_base_class *cls;
1367         widget_base_instance_data *data;
1368
1369         appcore_multiwindow_base_class_on_resume(instance_h);
1370         id = appcore_multiwindow_base_instance_get_id(instance_h);
1371         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1372         cls = __get_class(class_id);
1373         if (cls == NULL) {
1374                 LOGE("class not found: %s", class_id);
1375                 return;
1376         }
1377
1378         data = (widget_base_instance_data *)
1379                         appcore_multiwindow_base_instance_get_extra(instance_h);
1380
1381         if (data->pending_update) {
1382                 LOGD("pending update!");
1383                 data->pending_update = false;
1384                 __control_update(class_id, data->id, data->args);
1385                 if (data->period > 0) {
1386                         LOGD("Restart timer!");
1387                         data->periodic_timer = g_timeout_add_seconds(
1388                                         data->period,
1389                                         __timeout_cb, data);
1390                 }
1391         }
1392
1393         if (cls->ops.resume)
1394                 cls->ops.resume(instance_h, class_data);
1395
1396         LOGD("%s is resumed", id);
1397         __send_update_status(class_id, id,
1398                 WIDGET_INSTANCE_EVENT_RESUME, NULL);
1399
1400         if (!__fg_signal) {
1401                 LOGD("Send fg signal to resourceD");
1402                 aul_send_app_status_change_signal(getpid(),
1403                                 __appid,
1404                                 __package_id,
1405                                 STATUS_FOREGROUND,
1406                                 APP_TYPE_WIDGET);
1407                 __fg_signal = true;
1408         }
1409 }
1410
1411 static void __multiwindow_instance_pause(
1412                 appcore_multiwindow_base_instance_h instance_h,
1413                 void *class_data)
1414 {
1415         const char *id;
1416         const char *class_id;
1417         widget_base_class *cls;
1418
1419         appcore_multiwindow_base_class_on_pause(instance_h);
1420         id = appcore_multiwindow_base_instance_get_id(instance_h);
1421         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1422         cls = __get_class(class_id);
1423         if (cls == NULL) {
1424                 LOGE("class not found: %s", class_id);
1425                 return;
1426         }
1427
1428         if (cls->ops.pause)
1429                 cls->ops.pause(instance_h, class_data);
1430
1431         LOGD("%s is paused", id);
1432         __send_update_status(class_id, id,
1433                 WIDGET_INSTANCE_EVENT_PAUSE, NULL);
1434
1435         if (__fg_signal) {
1436                 LOGD("Send bg signal to resourceD");
1437                 aul_send_app_status_change_signal(getpid(),
1438                                 __appid,
1439                                 __package_id,
1440                                 STATUS_BACKGROUND,
1441                                 APP_TYPE_WIDGET);
1442                 __fg_signal = false;
1443         }
1444 }
1445
1446 static void __multiwindow_instance_terminate(
1447                 appcore_multiwindow_base_instance_h instance_h,
1448                 void *class_data)
1449 {
1450         widget_base_instance_data *data;
1451         bundle *b;
1452         char *operation = NULL;
1453         bundle *content_info;
1454         widget_base_destroy_type_e reason = WIDGET_BASE_DESTROY_TYPE_TEMPORARY;
1455         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
1456         const char *id;
1457         const char *class_id;
1458         widget_base_class *cls;
1459
1460         id = appcore_multiwindow_base_instance_get_id(instance_h);
1461         class_id = appcore_multiwindow_base_instance_get_class_id(instance_h);
1462         data  = appcore_multiwindow_base_instance_get_extra(
1463                         (appcore_multiwindow_base_instance_h)instance_h);
1464         b = data->args;
1465         cls = __get_class(class_id);
1466         if (cls == NULL) {
1467                 LOGE("class not found: %s", class_id);
1468                 return;
1469         }
1470
1471         if (b) {
1472                 bundle_get_str(b, WIDGET_K_OPERATION, &operation);
1473                 if (operation && strcmp(operation, "destroy") == 0)
1474                         reason = WIDGET_BASE_DESTROY_TYPE_PERMANENT;
1475         }
1476
1477         if (data->content)
1478                 content_info = bundle_decode((const bundle_raw *)data->content,
1479                                 strlen(data->content));
1480         else
1481                 content_info = bundle_create();
1482
1483         if (cls->ops.destroy)
1484                 cls->ops.destroy(instance_h, reason, content_info, class_data);
1485
1486         LOGD("%s is destroyed %d", id, reason);
1487         if (reason == WIDGET_BASE_DESTROY_TYPE_PERMANENT) {
1488                 event = WIDGET_INSTANCE_EVENT_DESTROY;
1489                 aul_widget_instance_del(class_id, id);
1490         } else {
1491                 __send_update_status(class_id, id,
1492                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED,
1493                                 content_info);
1494         }
1495
1496         if (content_info)
1497                 bundle_free(content_info);
1498
1499         if (data->periodic_timer)
1500                 g_source_remove(data->periodic_timer);
1501
1502         __send_update_status(class_id, id, event, NULL);
1503         appcore_multiwindow_base_class_on_terminate(instance_h);
1504 }
1505
1506 EXPORT_API int widget_base_class_on_create(widget_base_instance_h instance_h,
1507                 bundle *content, int w, int h)
1508 {
1509         appcore_multiwindow_base_class_on_create(instance_h);
1510
1511         return 0;
1512 }
1513
1514 EXPORT_API int widget_base_class_on_pause(widget_base_instance_h instance_h)
1515 {
1516         appcore_multiwindow_base_class_on_pause(instance_h);
1517
1518         return 0;
1519 }
1520
1521 EXPORT_API int widget_base_class_on_resume(widget_base_instance_h instance_h)
1522 {
1523         appcore_multiwindow_base_class_on_resume(instance_h);
1524
1525         return 0;
1526 }
1527
1528 EXPORT_API int widget_base_class_on_resize(widget_base_instance_h instance_h,
1529                 int w, int h)
1530 {
1531         return 0;
1532 }
1533
1534 EXPORT_API int widget_base_class_on_update(widget_base_instance_h instance_h,
1535                 bundle *content, int force)
1536 {
1537         return 0;
1538 }
1539
1540 EXPORT_API int widget_base_class_on_destroy(widget_base_instance_h instance_h,
1541                 widget_base_destroy_type_e reason, bundle *content)
1542 {
1543         appcore_multiwindow_base_class_on_terminate(instance_h);
1544
1545         return 0;
1546 }
1547
1548 EXPORT_API widget_base_class widget_base_class_get_default(void)
1549 {
1550         widget_base_class cls;
1551
1552         cls.ops.create = __class_on_create;
1553         cls.ops.resize = __class_on_resize;
1554         cls.ops.update = __class_on_update;
1555         cls.ops.destroy = __class_on_destroy;
1556         cls.ops.pause = __class_on_pause;
1557         cls.ops.resume = __class_on_resume;
1558
1559         return cls;
1560 }
1561
1562 EXPORT_API widget_base_class *widget_base_class_add(widget_base_class cls,
1563                 const char *class_id, void *class_data)
1564 {
1565         widget_base_class *c;
1566         appcore_multiwindow_base_class raw_cls;
1567
1568         if (!__is_widget_feature_enabled()) {
1569                 LOGE("not supported");
1570                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1571                 return NULL;
1572         }
1573
1574         if (!class_id) {
1575                 LOGE("class is is NULL");
1576                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1577                 return NULL;
1578         }
1579
1580         raw_cls.id = strdup(class_id);
1581         raw_cls.data = class_data;
1582         raw_cls.create = __multiwindow_instance_create;
1583         raw_cls.terminate = __multiwindow_instance_terminate;
1584         raw_cls.pause = __multiwindow_instance_pause;
1585         raw_cls.resume = __multiwindow_instance_resume;
1586         appcore_multiwindow_base_class_add(raw_cls);
1587
1588         c = malloc(sizeof(widget_base_class));
1589         if (!c)
1590                 return NULL;
1591
1592         *c = cls;
1593         c->id = strdup(class_id);
1594         __context.classes = g_list_append(__context.classes, c);
1595
1596         return c;
1597 }