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