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