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