9d717ae7d04b878d406a7610d3ac74271ebb5b10
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
1 /*
2  * Copyright (c) 2015 - 2016 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
18 #include <stdlib.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 <app_control.h>
28 #include <app_control_internal.h>
29 #include <Elementary.h>
30 #include <widget_errno.h>
31 #include <widget_instance.h>
32 #include <widget_service.h>
33 #include <widget_service_internal.h>
34 #include <aul_app_com.h>
35 #include <Ecore_Wayland.h>
36 #include <system_info.h>
37 #include <vconf.h>
38 #include <vconf-internal-keys.h>
39
40 #include "widget_app.h"
41 #include "widget-log.h"
42 #include "widget-private.h"
43 #include "widget_app_internal.h"
44
45 #ifdef LOG_TAG
46 #undef LOG_TAG
47 #endif
48
49 #define STR_MAX_BUF 128
50 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
51 #define K_REASON    "__WC_K_REASON__"
52
53 typedef enum _widget_obj_state_e {
54         WC_READY = 0,
55         WC_RUNNING = 1,
56         WC_PAUSED = 2,
57         WC_TERMINATED = 3
58 } widget_obj_state_e;
59
60 enum {
61         UPDATE_LOCAL = 0,
62         UPDATE_ALL = 1,
63 };
64
65 struct _widget_class {
66         void *user_data;
67         widget_instance_lifecycle_callback_s ops;
68         char *classid;
69         struct _widget_class *next;
70         struct _widget_class *prev;
71 };
72
73 struct app_event_handler {
74         app_event_type_e type;
75         app_event_cb cb;
76         void *data;
77 };
78
79 struct app_event_info {
80         app_event_type_e type;
81         void *value;
82 };
83
84 struct _widget_context {
85         char *id;
86         struct _widget_class *provider;
87         int state;
88         void *tag;
89         Evas_Object *win;
90         int win_id;
91         char *content;
92         widget_instance_lifecycle_callback_s ops;
93 };
94
95 typedef struct _widget_class widget_class_s;
96 typedef struct _widget_context widget_context_s;
97
98 #define WIDGET_APP_EVENT_MAX 5
99 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
100
101 static int caller_pid;
102 static widget_app_lifecycle_callback_s *app_ops;
103 static void *app_user_data;
104 static char *appid;
105 static widget_class_h class_provider;
106 static GList *contexts;
107 static char *viewer_endpoint;
108 static int exit_called;
109
110 static void _widget_core_set_appcore_event_cb(void);
111 static void _widget_core_unset_appcore_event_cb(void);
112
113 static void __free_handler_cb(gpointer data)
114 {
115         if (data)
116                 free(data);
117 }
118
119 static void __free_handler_list(void)
120 {
121         int i;
122
123         for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
124                 g_list_free_full(handler_list[i], __free_handler_cb);
125                 handler_list[i] = NULL;
126         }
127 }
128
129 static inline bool _is_widget_feature_enabled(void)
130 {
131         static bool feature = false;
132         static bool retrieved = false;
133         int ret;
134
135         if (retrieved == true)
136                 return feature;
137
138         ret = system_info_get_platform_bool(
139                         "http://tizen.org/feature/shell.appwidget", &feature);
140         if (ret != SYSTEM_INFO_ERROR_NONE) {
141                 _E("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 static gint __comp_by_id(gconstpointer a, gconstpointer b)
151 {
152         widget_context_s *wc = (widget_context_s *)a;
153
154         return strcmp(wc->id, (const char *)b);
155 }
156
157 static widget_context_s *__find_context_by_id(const char *id)
158 {
159         GList *ret;
160
161         if (id == NULL)
162                 return NULL;
163
164         ret = g_list_find_custom(contexts, id, __comp_by_id);
165         if (ret == NULL)
166                 return NULL;
167
168         return ret->data;
169 }
170
171 static gint __comp_by_win(gconstpointer a, gconstpointer b)
172 {
173         int win = GPOINTER_TO_INT(b);
174         widget_context_s *wc = (widget_context_s *)a;
175
176         return (wc && wc->win_id == win) ? 0 : -1;
177 }
178
179 static widget_context_s *__find_context_by_win(int win)
180 {
181         GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
182
183         if (ret == NULL)
184                 return NULL;
185
186         return ret->data;
187 }
188
189 static int __send_lifecycle_event(const char *class_id, const char *instance_id,
190         int status)
191 {
192         bundle *b = bundle_create();
193         int ret;
194
195         if (b == NULL) {
196                 _E("out of memory"); /* LCOV_EXCL_LINE */
197                 return -1; /* LCOV_EXCL_LINE */
198         }
199
200         bundle_add_str(b, WIDGET_K_ID, class_id);
201         bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
202         bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
203
204         _D("send lifecycle %s(%d)", instance_id, status);
205         ret = aul_app_com_send("widget.status", b);
206         if (ret < 0)
207                 _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
208
209         bundle_free(b);
210
211         return ret;
212 }
213
214 static int __send_update_status(const char *class_id, const char *instance_id,
215         int status, bundle *extra)
216 {
217         bundle *b;
218         int lifecycle = -1;
219         bundle_raw *raw = NULL;
220         int len;
221
222         b = bundle_create();
223         if (!b) {
224                 _E("out of memory"); /* LCOV_EXCL_LINE */
225                 return -1; /* LCOV_EXCL_LINE */
226         }
227
228         bundle_add_str(b, WIDGET_K_ID, class_id);
229         bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
230         bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
231
232         if (extra) {
233                 bundle_encode(extra, &raw, &len);
234                 bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
235                 aul_widget_instance_add(class_id, instance_id);
236         }
237
238         _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
239         aul_app_com_send(viewer_endpoint, b);
240
241         switch (status) {
242         case WIDGET_INSTANCE_EVENT_CREATE:
243                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
244                 break;
245         case WIDGET_INSTANCE_EVENT_DESTROY:
246                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
247                 break;
248         case WIDGET_INSTANCE_EVENT_PAUSE:
249                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
250                 break;
251         case WIDGET_INSTANCE_EVENT_RESUME:
252                 lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
253                 break;
254         }
255
256         if (lifecycle > -1)
257                 __send_lifecycle_event(class_id, instance_id, lifecycle);
258
259         bundle_free(b);
260         if (raw)
261                 free(raw);
262
263         return 0;
264 }
265
266 static int __instance_resume(widget_class_h handle, const char *id, int send_update)
267 {
268         widget_context_s *wc = __find_context_by_id(id);
269         int ret;
270
271         if (!wc) {
272                 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
273                 return -1; /* LCOV_EXCL_LINE */
274         }
275
276         if (wc->state == WC_RUNNING) {
277                 _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
278                 return 0; /* LCOV_EXCL_LINE */
279         }
280
281         if (wc->state == WC_TERMINATED) {
282                 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
283                 return 0; /* LCOV_EXCL_LINE */
284         }
285
286         if (handle->ops.resume)
287                 handle->ops.resume(wc, handle->user_data);
288
289         wc->state = WC_RUNNING;
290         _D("%s is resumed", id);
291         if (send_update) {
292                 ret = __send_update_status(handle->classid, wc->id,
293                         WIDGET_INSTANCE_EVENT_RESUME, NULL);
294         } else {
295                 ret = 0;
296         }
297
298         return ret;
299 }
300
301 static int __instance_pause(widget_class_h handle, const char *id, int send_update)
302 {
303         widget_context_s *wc = __find_context_by_id(id);
304         int ret;
305
306         if (!wc) {
307                 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
308                 return -1; /* LCOV_EXCL_LINE */
309         }
310
311         if (wc->state == WC_PAUSED) {
312                 _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
313                 return 0; /* LCOV_EXCL_LINE */
314         }
315
316         if (wc->state == WC_TERMINATED) {
317                 _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
318                 return 0; /* LCOV_EXCL_LINE */
319         }
320
321         if (handle->ops.pause)
322                 handle->ops.pause(wc, handle->user_data);
323
324         wc->state = WC_PAUSED;
325         _D("%s is paused", id);
326         if (send_update) {
327                 ret = __send_update_status(handle->classid, wc->id,
328                         WIDGET_INSTANCE_EVENT_PAUSE, NULL);
329         } else {
330                 ret = 0;
331         }
332
333         return ret;
334 }
335
336 static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
337 {
338         widget_context_s *wc = __find_context_by_id(id);
339         int ret;
340
341         if (!wc) {
342                 _E("context not found: %s", id); /* LCOV_EXCL_LINE */
343                 return -1; /* LCOV_EXCL_LINE */
344         }
345
346         if (handle->ops.resize)
347                 handle->ops.resize(wc, w, h, handle->user_data);
348
349         _D("%s is resized to %dx%d", id, w, h);
350         ret = __send_update_status(handle->classid, wc->id,
351                 WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
352
353         return ret;
354 }
355
356 /* LCOV_EXCL_START */
357 static int __instance_update_all(widget_class_h handle, int force, const char *content)
358 {
359         widget_context_s *wc;
360         int ret = 0;
361         bundle *b = NULL;
362         GList *context = contexts;
363
364         if (content)
365                 b = bundle_decode((const bundle_raw *)content, strlen(content));
366
367         if (handle->ops.update) {
368                 while (context) {
369                         wc = (widget_context_s *)context->data;
370                         handle->ops.update(wc, b, force, handle->user_data);
371                         ret = __send_update_status(handle->classid, wc->id,
372                                 WIDGET_INSTANCE_EVENT_UPDATE, NULL);
373                         _D("updated:%s", wc->id);
374                         context = context->next;
375                 }
376         }
377
378         if (b)
379                 bundle_free(b);
380
381         return ret;
382 }
383 /* LCOV_EXCL_STOP */
384
385 /* LCOV_EXCL_START */
386 static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
387 {
388         widget_context_s *wc = __find_context_by_id(id);
389         int ret = 0;
390         bundle *b = NULL;
391         if (!wc) {
392                 _E("context not found: %s", id);
393                 return -1;
394         }
395
396         if (content)
397                 b = bundle_decode((const bundle_raw *)content, strlen(content));
398
399         if (handle->ops.update) {
400                 handle->ops.update(wc, b, force, handle->user_data);
401                 ret = __send_update_status(handle->classid, wc->id,
402                         WIDGET_INSTANCE_EVENT_UPDATE, NULL);
403                 _D("updated:%s", id);
404         }
405
406         if (b)
407                 bundle_free(b);
408
409         return ret;
410 }
411 /* LCOV_EXCL_STOP */
412
413 static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
414 {
415         widget_context_s *wc = NULL;
416         int ret = 0;
417         bundle *content_info = NULL;
418
419         wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
420         if (!wc)
421                 return WIDGET_ERROR_OUT_OF_MEMORY;
422
423         wc->state = WC_READY;
424         wc->id = g_strdup(id);
425         wc->provider = handle;
426         wc->win = NULL;
427         wc->win_id = -1;
428
429         if (content) {
430                 wc->content = strdup(content);
431                 content_info = bundle_decode((const bundle_raw *)content, strlen(content));
432         }
433
434         contexts = g_list_append(contexts, wc);
435
436         ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
437         if (ret < 0) {
438                 /* TODO send abort */
439         } else {
440                 ret = __send_update_status(handle->classid, wc->id,
441                         WIDGET_INSTANCE_EVENT_CREATE, NULL);
442
443                 if (content == NULL)
444                         content = "NULL";
445
446                 aul_widget_instance_add(handle->classid, id);
447         }
448
449         if (content_info)
450                 bundle_free(content_info);
451
452         return ret;
453 }
454
455 static int __instance_destroy(widget_class_h handle, const char *id,
456                 widget_app_destroy_type_e reason, int send_update)
457 {
458         widget_context_s *wc = __find_context_by_id(id);
459         int ret = 0;
460         int event = WIDGET_INSTANCE_EVENT_TERMINATE;
461         bundle *content_info;
462
463         if (!wc) {
464                 _E("could not find widget obj: %s", id); /* LCOV_EXCL_LINE */
465                 return WIDGET_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */
466         }
467
468         wc->state = WC_TERMINATED;
469         if (wc->content)
470                 content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
471         else
472                 content_info = bundle_create();
473
474         handle->ops.destroy(wc, reason, content_info,
475                         handle->user_data);
476
477         if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
478                 event = WIDGET_INSTANCE_EVENT_DESTROY;
479         } else {
480                 ret = __send_update_status(handle->classid, id,
481                                 WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
482         }
483
484         aul_widget_instance_del(handle->classid, id);
485
486         if (content_info)
487                 bundle_free(content_info);
488
489         ret = __send_update_status(handle->classid, id, event, NULL);
490
491         contexts = g_list_remove(contexts, wc);
492
493         if (wc->id)
494                 free(wc->id);
495
496         if (wc->content)
497                 free(wc->content);
498
499         free(wc);
500
501         if (contexts == NULL && !exit_called) /* all instance destroyed */
502                 widget_app_exit();
503
504         return ret;
505 }
506
507 static widget_class_h __find_class_handler(const char *class_id,
508                 widget_class_h handle)
509 {
510         if (!class_id || !handle)
511                 return NULL;
512
513         widget_class_h head = handle;
514
515         while (head) {
516                 if (head->classid && strcmp(head->classid, class_id) == 0)
517                         return head;
518
519                 head = head->next;
520         }
521
522         return NULL;
523 }
524
525 /* LCOV_EXCL_START */
526 static void __resize_window(char *id, int w, int h)
527 {
528         widget_context_s *wc = __find_context_by_id(id);
529
530         if (!wc) {
531                 _E("can not find instance: %s", id);
532                 return;
533         }
534
535         if (wc->win)
536                 evas_object_resize(wc->win, w, h);
537         else
538                 _E("unable to find window of %d", wc->id);
539 }
540 /* LCOV_EXCL_STOP */
541
542 static void __control(bundle *b)
543 {
544         char *class_id = NULL;
545         char *id = NULL;
546         char *operation = NULL;
547         char *content = NULL;
548         char *w_str = NULL;
549         char *h_str = NULL;
550         int w = 0;
551         int h = 0;
552         char *remain = NULL;
553         int force;
554         char *force_str = NULL;
555         widget_class_h handle = NULL;
556
557         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
558         /* for previous version compatibility, use appid for default class id */
559         if (class_id == NULL)
560                 class_id = appid;
561
562         bundle_get_str(b, WIDGET_K_INSTANCE, &id);
563         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
564
565         handle = __find_class_handler(class_id, class_provider);
566         if (!handle) {
567                 _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
568                 goto error;
569         }
570
571         if (!operation) {
572                 _E("no operation provided");
573                 goto error;
574         }
575
576         bundle_get_str(b, WIDGET_K_FORCE, &force_str);
577
578         if (force_str && strcmp(force_str, "true") == 0)
579                 force = 1;
580         else
581                 force = 0;
582
583         bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
584         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
585         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
586         if (w_str)
587                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
588
589         if (h_str)
590                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
591
592         if (strcmp(operation, "create") == 0) {
593                 __instance_create(handle, id, content, w, h);
594         } else if (strcmp(operation, "resize") == 0) {
595                 __resize_window(id, w, h);
596         } else if (strcmp(operation, "update") == 0) {
597                 if (id)
598                         __instance_update(handle, id, force, content);
599                 else
600                         __instance_update_all(handle, force, content);
601
602         } else if (strcmp(operation, "destroy") == 0) {
603                 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
604         } else if (strcmp(operation, "resume") == 0) {
605                 __instance_resume(handle, id, UPDATE_ALL);
606         } else if (strcmp(operation, "pause") == 0) {
607                 __instance_pause(handle, id, UPDATE_ALL);
608         } else if (strcmp(operation, "terminate") == 0) {
609                 __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
610         }
611
612         return;
613 error:
614         LOGD("error on control");
615         return;
616 }
617
618 static void __pause_all(int send_update)
619 {
620         GList *iter = g_list_first(contexts);
621
622         while (iter != NULL) {
623                 widget_context_s *cxt = (widget_context_s *)iter->data;
624
625                 switch (cxt->state) {
626                 case WC_READY:
627                         __instance_resume(cxt->provider, cxt->id, send_update);
628                         __instance_pause(cxt->provider, cxt->id, send_update);
629                         break;
630                 case WC_RUNNING:
631                         __instance_pause(cxt->provider, cxt->id, send_update);
632                         break;
633                 }
634                 iter = g_list_next(iter);
635         }
636 }
637
638 /* LCOV_EXCL_START */
639 static void __resume_all(int send_update)
640 {
641         GList *iter = g_list_first(contexts);
642
643         while (iter != NULL) {
644                 widget_context_s *cxt = (widget_context_s *)iter->data;
645
646                 switch (cxt->state) {
647                 case WC_READY:
648                         __instance_resume(cxt->provider, cxt->id, send_update);
649                         break;
650                 case WC_PAUSED:
651                         __instance_resume(cxt->provider, cxt->id, send_update);
652                         break;
653                 }
654                 iter = g_list_next(iter);
655         }
656 }
657 /* LCOV_EXCL_STOP */
658
659 static void __destroy_all(int reason, int send_update)
660 {
661         GList *iter = g_list_first(contexts);
662
663         __pause_all(send_update);
664
665         while (iter != NULL) {
666                 widget_context_s *cxt = (widget_context_s *)iter->data;
667
668                 switch (cxt->state) {
669                 case WC_PAUSED:
670                         __instance_destroy(cxt->provider, cxt->id, reason, send_update);
671                         break;
672                 }
673                 iter = g_list_next(iter);
674         }
675 }
676
677
678 static Eina_Bool __show_cb(void *data, int type, void *event)
679 {
680         Ecore_Wl_Event_Window_Show *ev = event;
681         widget_context_s *cxt = __find_context_by_win(ev->win);
682
683         LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
684
685         if (cxt)
686                 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
687         else
688                 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
689
690         return ECORE_CALLBACK_RENEW;
691 }
692
693 static Eina_Bool __hide_cb(void *data, int type, void *event)
694 {
695         Ecore_Wl_Event_Window_Hide *ev = event;
696         widget_context_s *cxt = __find_context_by_win(ev->win);
697
698
699         LOGD("hide %d", (unsigned int)ev->win);
700
701         if (cxt)
702                 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
703         else
704                 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
705
706         return ECORE_CALLBACK_RENEW;
707 }
708
709 static Eina_Bool __visibility_cb(void *data, int type, void *event)
710 {
711         Ecore_Wl_Event_Window_Visibility_Change *ev = event;
712         widget_context_s *cxt = __find_context_by_win(ev->win);
713
714         LOGD("visiblity change: %d %d", (unsigned int)ev->win,  (unsigned int)ev->fully_obscured);
715
716         if (!cxt) {
717                 LOGE("unknown window error: %d", ev->win);
718                 return ECORE_CALLBACK_RENEW;
719         }
720
721         if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
722                 __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
723         } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
724                 __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
725         } else {
726                 LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
727         }
728
729         return ECORE_CALLBACK_RENEW;
730 }
731
732 /* LCOV_EXCL_START */
733 static Eina_Bool __lower_cb(void *data, int type, void *event)
734 {
735         LOGD("lower");
736         return ECORE_CALLBACK_RENEW;
737 }
738 /* LCOV_EXCL_STOP */
739
740 static Eina_Bool __configure_cb(void *data, int type, void *event)
741 {
742         Ecore_Wl_Event_Window_Configure *ev = event;
743         widget_context_s *cxt = __find_context_by_win(ev->win);
744
745         LOGD("configure: %d %d", ev->w, ev->h);
746
747         if (!cxt) {
748                 LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
749                 return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
750         }
751
752         if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
753                 __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
754         LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
755
756         return ECORE_CALLBACK_RENEW;
757 }
758
759 static void __add_climsg()
760 {
761         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
762         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
763         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
764         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
765         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
766 }
767
768 static int __aul_handler(aul_type type, bundle *b, void *data)
769 {
770         char *caller = NULL;
771         char *remain = NULL;
772
773         switch (type) {
774         case AUL_START:
775                 if (b) {
776                         bundle_get_str(b, WIDGET_K_CALLER, &caller);
777                         if (caller) {
778                                 caller_pid = g_ascii_strtoll(caller, &remain,
779                                                 10);
780                         } else {
781                                 /* using caller appid and query pid using caller appid? */
782                                 _E("no caller pid");
783                         }
784                 }
785
786                 __control(b);
787                 break;
788         case AUL_RESUME:
789                 __resume_all(UPDATE_ALL);
790                 break;
791         case AUL_TERMINATE:
792                 widget_app_exit();
793                 break;
794         default:
795                 break;
796         }
797
798         return 0;
799 }
800
801 static char *__get_domain_name(char *appid)
802 {
803         char *name_token;
804
805         if (appid == NULL) {
806                 _E("appid is NULL"); /* LCOV_EXCL_LINE */
807                 return NULL; /* LCOV_EXCL_LINE */
808         }
809
810         name_token = strrchr(appid, '.');
811
812         if (name_token == NULL) {
813                 _E("appid is invalid"); /* LCOV_EXCL_LINE */
814                 return appid; /* LCOV_EXCL_LINE */
815         }
816
817         name_token++;
818
819         return name_token;
820 }
821
822 /* LCOV_EXCL_START */
823 static void __on_poweroff(keynode_t *key, void *data)
824 {
825         int val;
826
827         val = vconf_keynode_get_int(key);
828         switch (val) {
829         case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
830         case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
831                 _I("power off changed: %d", val);
832                 widget_app_exit();
833                 break;
834         case VCONFKEY_SYSMAN_POWER_OFF_NONE:
835         case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
836         default:
837                 /* DO NOTHING */
838                 break;
839         }
840 }
841 /* LCOV_EXCL_STOP */
842
843 extern int _set_i18n(const char *name);
844
845 static int __before_loop(int argc, char **argv)
846 {
847         int r;
848         bundle *kb = NULL;
849         char *wayland_display = NULL;
850         char *xdg_runtime_dir = NULL;
851         char *name;
852
853 #if !(GLIB_CHECK_VERSION(2, 36, 0))
854         g_type_init();
855 #endif
856
857         kb = bundle_import_from_argv(argc, argv);
858         if (kb) {
859                 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
860                 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
861                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
862                 if (viewer_endpoint) {
863                         _E("viewer endpoint :%s", viewer_endpoint);
864                         viewer_endpoint = strdup(viewer_endpoint);
865                 } else {
866                         _E("endpoint is missing");
867                 }
868
869                 if (xdg_runtime_dir)
870                         setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
871
872                 _D("xdg_runtime_dir:%s", xdg_runtime_dir);
873
874                 if (wayland_display)
875                         setenv("WAYLAND_DISPLAY", wayland_display, 1);
876
877                 _D("wayland_display:%s", wayland_display);
878
879                 bundle_free(kb);
880                 kb = NULL;
881         } else {
882                 _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
883         }
884
885         elm_init(argc, argv);
886
887         r = aul_launch_init(__aul_handler, NULL);
888         if (r < 0) {
889                 /* LCOV_EXCL_START */
890                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
891                                 __FUNCTION__,
892                                 "Fail to call the aul_launch_init");
893                 /* LCOV_EXCL_STOP */
894         }
895
896         r = aul_launch_argv_handler(argc, argv);
897         if (r < 0) {
898                 /* LCOV_EXCL_START */
899                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
900                                 __FUNCTION__,
901                                 "Fail to call the aul_launch_argv_handler");
902                 /* LCOV_EXCL_STOP */
903         }
904
905         r = app_get_id(&appid);
906         if (r != APP_ERROR_NONE)
907                 return r;
908
909         name = __get_domain_name(appid);
910
911         if (name == NULL) {
912                 /* LCOV_EXCL_START */
913                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
914                                 __FUNCTION__,
915                                 "Fail to call __get_domain_name");
916                 /* LCOV_EXCL_STOP */
917         }
918
919         r = _set_i18n(name);
920
921         if (r < 0) {
922                 /* LCOV_EXCL_START */
923                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
924                                 __FUNCTION__,
925                                 "Fail to call _set_i18n");
926                 /* LCOV_EXCL_STOP */
927         }
928
929         __add_climsg();
930
931         _widget_core_set_appcore_event_cb();
932
933         class_provider = app_ops->create(app_user_data);
934         if (class_provider == NULL) {
935                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
936                                 __FUNCTION__, "widget_class is NULL");
937         }
938
939         vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
940
941         return WIDGET_ERROR_NONE;
942 }
943
944 static void __after_loop()
945 {
946         exit_called = 1;
947         vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
948
949         __pause_all(UPDATE_LOCAL);
950         __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
951
952         if (app_ops->terminate)
953                 app_ops->terminate(app_user_data);
954
955         if (viewer_endpoint)
956                 free(viewer_endpoint);
957
958         _widget_core_unset_appcore_event_cb();
959         __free_handler_list();
960         elm_shutdown();
961 }
962
963 static void __on_low_memory(keynode_t *key, void *data)
964 {
965         int val;
966
967         val = vconf_keynode_get_int(key);
968         if (val == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
969                 app_event_handler_h handler;
970                 struct app_event_info event;
971
972                 _I("widget_app_low_memory");
973
974                 event.type = APP_EVENT_LOW_MEMORY;
975                 event.value = (void *)&val;
976
977                 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
978
979                 while (iter) {
980                         handler = (app_event_handler_h) iter->data;
981                         handler->cb(&event, handler->data);
982                         iter = g_list_next(iter);
983                 }
984         }
985 }
986
987 static void __on_low_battery(keynode_t *key, void *data)
988 {
989         int val;
990
991         val = vconf_keynode_get_int(key);
992         if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
993                 app_event_handler_h handler;
994                 struct app_event_info event;
995
996                 _I("widget_app_low_battery");
997
998                 event.type = APP_EVENT_LOW_BATTERY;
999                 event.value = (void *)&val;
1000
1001                 GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
1002
1003                 while (iter) {
1004                         handler = (app_event_handler_h) iter->data;
1005                         handler->cb(&event, handler->data);
1006                         iter = g_list_next(iter);
1007                 }
1008         }
1009 }
1010
1011 static void __on_lang_changed(keynode_t *key, void *data)
1012 {
1013         char *val;
1014
1015         _update_lang();
1016         val = vconf_keynode_get_str(key);
1017
1018         app_event_handler_h handler;
1019         struct app_event_info event;
1020
1021         _I("widget_app_lang_changed");
1022
1023         event.type = APP_EVENT_LANGUAGE_CHANGED;
1024         event.value = (void *)val;
1025
1026         GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
1027
1028         while (iter) {
1029                 handler = (app_event_handler_h) iter->data;
1030                 handler->cb(&event, handler->data);
1031                 iter = g_list_next(iter);
1032         }
1033 }
1034
1035 static void __on_region_changed(keynode_t *key, void *data)
1036 {
1037         char *val;
1038
1039         _update_region();
1040         val = vconf_keynode_get_str(key);
1041
1042         app_event_handler_h handler;
1043         struct app_event_info event;
1044
1045         _I("widget_app_region_changed");
1046
1047         event.type = APP_EVENT_REGION_FORMAT_CHANGED;
1048         event.value = (void *)val;
1049
1050         GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
1051
1052         while (iter) {
1053                 handler = (app_event_handler_h) iter->data;
1054                 handler->cb(&event, handler->data);
1055                 iter = g_list_next(iter);
1056         }
1057 }
1058
1059 static void __register_event(int event_type)
1060 {
1061         switch (event_type) {
1062         case APP_EVENT_LOW_MEMORY:
1063                 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1064                 break;
1065
1066         case APP_EVENT_LOW_BATTERY:
1067                 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1068                 break;
1069
1070         case APP_EVENT_LANGUAGE_CHANGED:
1071                 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_lang_changed, NULL);
1072                 break;
1073
1074         case APP_EVENT_REGION_FORMAT_CHANGED:
1075                 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed, NULL);
1076                 break;
1077         }
1078 }
1079
1080 static void __unregister_event(int event_type)
1081 {
1082         switch (event_type) {
1083         case APP_EVENT_LOW_MEMORY:
1084                 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1085                 break;
1086
1087         case APP_EVENT_LOW_BATTERY:
1088                 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1089                 break;
1090
1091         case APP_EVENT_LANGUAGE_CHANGED:
1092                 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_lang_changed);
1093                 break;
1094
1095         case APP_EVENT_REGION_FORMAT_CHANGED:
1096                 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_changed);
1097                 break;
1098         }
1099 }
1100
1101 static void _widget_core_set_appcore_event_cb(void)
1102 {
1103         __register_event(APP_EVENT_LANGUAGE_CHANGED);
1104         __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
1105 }
1106
1107 static void _widget_core_unset_appcore_event_cb(void)
1108 {
1109         __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
1110         __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
1111 }
1112
1113 EXPORT_API int widget_app_main(int argc, char **argv,
1114                 widget_app_lifecycle_callback_s *callback, void *user_data)
1115 {
1116         int r;
1117
1118         if (!_is_widget_feature_enabled()) {
1119                 _E("not supported"); /* LCOV_EXCL_LINE */
1120                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1121         }
1122
1123         if (argc <= 0 || argv == NULL || callback == NULL)
1124                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1125                                 __FUNCTION__, NULL);
1126
1127         if (callback->create == NULL)
1128                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1129                                 __FUNCTION__,
1130                                 "widget_app_create_cb() callback must be "
1131                                 "registered");
1132
1133         app_ops = callback;
1134         app_user_data = user_data;
1135         r = __before_loop(argc, argv);
1136         if (r < 0)
1137                 return r;
1138
1139         ecore_main_loop_begin();
1140         aul_status_update(STATUS_DYING);
1141         __after_loop();
1142
1143         return WIDGET_ERROR_NONE;
1144 }
1145
1146 EXPORT_API int widget_app_exit(void)
1147 {
1148         if (!_is_widget_feature_enabled()) {
1149                 _E("not supported"); /* LCOV_EXCL_LINE */
1150                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1151         }
1152
1153         if (exit_called)
1154                 return WIDGET_ERROR_NONE;
1155
1156         exit_called = 1;
1157
1158         ecore_main_loop_quit();
1159
1160         return WIDGET_ERROR_NONE;
1161 }
1162
1163 /* LCOV_EXCL_START */
1164 static gboolean __finish_event_cb(gpointer user_data)
1165 {
1166         if (user_data == NULL)
1167                 return FALSE;
1168
1169         widget_context_s *wc = (widget_context_s *)user_data;
1170
1171         switch (wc->state) {
1172         case WC_READY:
1173                 __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
1174         case WC_RUNNING:
1175                 __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
1176         case WC_PAUSED:
1177                 __instance_destroy(wc->provider, wc->id,
1178                                 WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
1179                 break;
1180         default:
1181                 break;
1182         }
1183
1184         return FALSE;
1185 }
1186 /* LCOV_EXCL_STOP */
1187
1188 EXPORT_API int widget_app_terminate_context(widget_context_h context)
1189 {
1190         if (!_is_widget_feature_enabled()) {
1191                 _E("not supported"); /* LCOV_EXCL_LINE */
1192                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1193         }
1194
1195         if (context == NULL)
1196                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1197                                 __FUNCTION__, NULL);
1198
1199         g_idle_add(__finish_event_cb, context);
1200         return WIDGET_ERROR_NONE;
1201 }
1202
1203 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
1204 {
1205         GList *list;
1206         widget_context_s *wc;
1207
1208         if (!_is_widget_feature_enabled()) {
1209                 _E("not supported"); /* LCOV_EXCL_LINE */
1210                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1211         }
1212
1213         if (!cb)
1214                 return WIDGET_ERROR_INVALID_PARAMETER;
1215
1216         list = g_list_first(contexts);
1217
1218         while (list) {
1219                 wc = (widget_context_s *)list->data;
1220                 if (wc) {
1221                         if (!cb(wc, data))
1222                                 break;
1223                 }
1224                 list = list->next;
1225         }
1226
1227         return WIDGET_ERROR_NONE;
1228 }
1229
1230 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
1231                                         app_event_type_e event_type, app_event_cb callback,
1232                                         void *user_data)
1233 {
1234         int r;
1235         bool feature;
1236
1237         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1238         if (r < 0)
1239                 return WIDGET_ERROR_FAULT;
1240
1241         if (!feature)
1242                 return WIDGET_ERROR_NOT_SUPPORTED;
1243
1244         app_event_handler_h handler;
1245
1246         if (event_handler == NULL || callback == NULL)
1247                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1248
1249         if (event_type < APP_EVENT_LOW_MEMORY
1250             || event_type > APP_EVENT_REGION_FORMAT_CHANGED)
1251                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1252
1253         if (event_type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
1254                 return widget_app_error(WIDGET_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
1255
1256         GList *iter = g_list_first(handler_list[event_type]);
1257
1258         while (iter) {
1259                 handler = (app_event_handler_h) iter->data;
1260
1261                 if (handler->cb == callback)
1262                         return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1263
1264                 iter = g_list_next(iter);
1265         }
1266
1267         handler = calloc(1, sizeof(struct app_event_handler));
1268         if (!handler)
1269                 return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
1270
1271         if (g_list_length(handler_list[event_type]) == 0)
1272                 __register_event(event_type);
1273
1274         handler->type = event_type;
1275         handler->cb = callback;
1276         handler->data = user_data;
1277         handler_list[event_type] = g_list_append(handler_list[event_type], handler);
1278
1279         *event_handler = handler;
1280
1281         return WIDGET_ERROR_NONE;
1282 }
1283
1284 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
1285                                                 event_handler)
1286 {
1287         int r;
1288         bool feature;
1289
1290         r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
1291         if (r < 0)
1292                 return WIDGET_ERROR_FAULT;
1293
1294         if (!feature)
1295                 return WIDGET_ERROR_NOT_SUPPORTED;
1296
1297         app_event_type_e type;
1298
1299         if (event_handler == NULL)
1300                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1301
1302         type = event_handler->type;
1303         if (type < APP_EVENT_LOW_MEMORY || type > APP_EVENT_REGION_FORMAT_CHANGED)
1304                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
1305
1306         handler_list[type] = g_list_remove(handler_list[type], event_handler);
1307         free(event_handler);
1308
1309         if (g_list_length(handler_list[type]) == 0)
1310                 __unregister_event(type);
1311
1312         return WIDGET_ERROR_NONE;
1313 }
1314
1315 EXPORT_API const char *widget_app_get_id(widget_context_h context)
1316 {
1317         if (!_is_widget_feature_enabled()) {
1318                 _E("not supported"); /* LCOV_EXCL_LINE */
1319                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1320                 return NULL; /* LCOV_EXCL_LINE */
1321         }
1322
1323         if (!context) {
1324                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1325                 return NULL;
1326         }
1327
1328         set_last_result(WIDGET_ERROR_NONE);
1329         return context->id;
1330 }
1331
1332 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
1333                                         Evas_Object **win)
1334 {
1335         widget_context_s *cxt = (widget_context_s *)context;
1336         Evas_Object *ret_win;
1337         Ecore_Wl_Window *wl_win;
1338
1339         if (!_is_widget_feature_enabled()) {
1340                 _E("not supported"); /* LCOV_EXCL_LINE */
1341                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1342         }
1343
1344         if (context == NULL || win == NULL)
1345                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1346                                 __FUNCTION__, NULL);
1347
1348         ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
1349         if (ret_win == NULL) {
1350                 _E("failed to create window"); /* LCOV_EXCL_LINE */
1351                 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1352         }
1353
1354         wl_win = elm_win_wl_window_get(ret_win);
1355         if (wl_win == NULL) {
1356                 _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
1357                 evas_object_del(ret_win); /* LCOV_EXCL_LINE */
1358                 return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
1359         }
1360
1361         ecore_wl_window_class_name_set(wl_win, cxt->id);
1362
1363         *win = ret_win;
1364         cxt->win = ret_win;
1365         cxt->win_id = ecore_wl_window_id_get(wl_win);
1366
1367         _D("window created: %d", cxt->win_id);
1368
1369         return WIDGET_ERROR_NONE;
1370 }
1371
1372 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
1373                 widget_instance_lifecycle_callback_s callback, void *user_data)
1374 {
1375         widget_class_s *wc;
1376
1377         if (!_is_widget_feature_enabled()) {
1378                 _E("not supported"); /* LCOV_EXCL_LINE */
1379                 set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
1380                 return NULL;
1381         }
1382
1383         if (class_id == NULL) {
1384                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1385                 return NULL;
1386         }
1387
1388         wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
1389         if (wc == NULL) {
1390                 _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
1391                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
1392                 return NULL; /* LCOV_EXCL_LINE */
1393         }
1394
1395         wc->classid = strdup(class_id);
1396         wc->user_data = user_data;
1397         wc->ops = callback;
1398         wc->next = prev;
1399         wc->prev = NULL;
1400
1401         set_last_result(WIDGET_ERROR_NONE);
1402
1403         if (prev)
1404                 prev->prev = wc;
1405
1406         return wc;
1407 }
1408
1409 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
1410                 const char *class_id,
1411                 widget_instance_lifecycle_callback_s callback, void *user_data)
1412 {
1413         return _widget_class_create(widget_class, class_id, callback,
1414                         user_data);
1415 }
1416
1417 EXPORT_API widget_class_h widget_app_class_create(
1418                 widget_instance_lifecycle_callback_s callback, void *user_data)
1419 {
1420         return _widget_class_create(class_provider, appid, callback, user_data);
1421 }
1422
1423 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
1424 {
1425         if (!_is_widget_feature_enabled()) {
1426                 _E("not supported"); /* LCOV_EXCL_LINE */
1427                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1428         }
1429
1430         if (context == NULL)
1431                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1432                                 __FUNCTION__, NULL);
1433
1434         context->tag = tag;
1435
1436         return WIDGET_ERROR_NONE;
1437 }
1438
1439 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
1440 {
1441         if (!_is_widget_feature_enabled()) {
1442                 _E("not supported"); /* LCOV_EXCL_LINE */
1443                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1444         }
1445
1446         if (context == NULL || tag == NULL)
1447                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1448                                 __FUNCTION__, NULL);
1449
1450         *tag = context->tag;
1451
1452         return WIDGET_ERROR_NONE;
1453 }
1454
1455 EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
1456                 bundle *content_info)
1457 {
1458         const char *class_id = NULL;
1459         int ret = 0;
1460         bundle_raw *raw = NULL;
1461         int len;
1462
1463         if (!_is_widget_feature_enabled()) {
1464                 _E("not supported"); /* LCOV_EXCL_LINE */
1465                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1466         }
1467
1468         if (context == NULL || content_info == NULL)
1469                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1470                                 __FUNCTION__, NULL);
1471
1472         if (context->provider == NULL)
1473                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
1474                                 __FUNCTION__, NULL);
1475
1476         class_id = context->provider->classid;
1477         if (class_id == NULL)
1478                 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
1479
1480         ret = __send_update_status(class_id, context->id,
1481                         WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
1482
1483         if (context->content)
1484                 free(context->content);
1485
1486         bundle_encode(content_info, &raw, &len);
1487         if (raw)
1488                 context->content = strdup((const char *)raw);
1489         else
1490                 context->content = NULL;
1491
1492         if (ret < 0) {
1493                 /* LCOV_EXCL_START */
1494                 _E("failed to send content info: %s of %s (%d)", context->id,
1495                                 class_id, ret);
1496                 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
1497                                 NULL);
1498                 /* LCOV_EXCL_STOP */
1499         }
1500
1501         return WIDGET_ERROR_NONE;
1502 }
1503
1504 EXPORT_API int widget_app_context_set_title(widget_context_h context,
1505                 const char *title)
1506 {
1507         if (!_is_widget_feature_enabled()) {
1508                 _E("not supported"); /* LCOV_EXCL_LINE */
1509                 return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
1510         }
1511
1512         if (!context || !title)
1513                 return WIDGET_ERROR_INVALID_PARAMETER;
1514
1515         if (context->win)
1516                 elm_win_title_set(context->win, title);
1517
1518         return WIDGET_ERROR_NONE;
1519 }
1520