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