Send create aborted event to the widget viewer
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
index 4556c3d..e25fdd1 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
 
 
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include <bundle.h>
+#include <bundle_internal.h>
 #include <aul.h>
 #include <dlog.h>
 #include <glib.h>
 #include <glib-object.h>
+#include <stdlib.h>
 #include <app_control.h>
 #include <app_control_internal.h>
-#include <widget.h>
-#include <widget_service.h>
-#include <widget_service_internal.h>
-#include <widget_provider_app.h>
-#include <widget_provider_app_internal.h>
 #include <Elementary.h>
-#include <vconf.h>
 #include <widget_errno.h>
+#include <widget_instance.h>
+#include <widget_service.h>
+#include <widget_service_internal.h>
+#include <aul_app_com.h>
+#include <Ecore_Wayland.h>
 #include <system_info.h>
+#include <vconf.h>
+#include <vconf-internal-keys.h>
+#include <screen_connector_provider.h>
 
 #include "widget_app.h"
 #include "widget-log.h"
 #define STR_MAX_BUF 128
 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
 #define K_REASON    "__WC_K_REASON__"
-
-#define ELM_WIN_TIZEN_WIDGET -1
-/* TODO
- * This definition is intended to prevent the build break.
- * This should be removed after adding ELM_WIN_TIZEN_WIDGET in Elm_Win_Type.
- */
-
-#define WIDGET_APP_EVENT_MAX 5
-static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
+#define APP_TYPE_WIDGET "widgetapp"
+#define STATUS_FOREGROUND "fg"
+#define STATUS_BACKGROUND "bg"
 
 typedef enum _widget_obj_state_e {
        WC_READY = 0,
@@ -63,6 +62,11 @@ typedef enum _widget_obj_state_e {
        WC_TERMINATED = 3
 } widget_obj_state_e;
 
+enum {
+       UPDATE_LOCAL = 0,
+       UPDATE_ALL = 1,
+};
+
 struct app_event_handler {
        app_event_type_e type;
        app_event_cb cb;
@@ -74,335 +78,800 @@ struct app_event_info {
        void *value;
 };
 
-struct _widget_class {
-       void *user_data;
-       widget_instance_lifecycle_callback_s ops;
-       widget_obj_private_ops_s ops_private;
-};
+typedef struct _widget_class widget_class_s;
 
-struct _widget_context {
-       char *id;
-       int state;
-       void *tag;
-       widget_instance_lifecycle_callback_s ops;
-       widget_obj_private_ops_s ops_private;
-};
+#define WIDGET_APP_EVENT_MAX 5
+static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
 
-typedef struct _widget_class widget_class_s;
-typedef struct _widget_context widget_context_s;
-
-static widget_app_lifecycle_callback_s *app_ops = NULL;
-static void *app_user_data = NULL;
-static widget_class_factory_full_s factory;
-static widget_class_s *widget_class = NULL;
-static widget_class_s widget_class_tmp;
-static GList *contexts = NULL;
-static int is_init_provider = 0;
-static char *appid = NULL;
-static int is_background = -1;
+static int caller_pid;
+static widget_app_lifecycle_callback_s *app_ops;
+static void *app_user_data;
+static char *appid;
+static widget_class_h class_provider;
+static int exit_called;
+static char *package_id;
+static bool fg_signal;
+
+static void _widget_core_set_appcore_event_cb(void);
+static void _widget_core_unset_appcore_event_cb(void);
+
+static void __free_handler_cb(gpointer data)
+{
+       if (data)
+               free(data);
+}
+
+static void __free_handler_list(void)
+{
+       int i;
+
+       for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
+               g_list_free_full(handler_list[i], __free_handler_cb);
+               handler_list[i] = NULL;
+       }
+}
+
+static inline bool _is_widget_feature_enabled(void)
+{
+       static bool feature = false;
+       static bool retrieved = false;
+       int ret;
+
+       if (retrieved == true)
+               return feature;
+
+       ret = system_info_get_platform_bool(
+                       "http://tizen.org/feature/shell.appwidget", &feature);
+       if (ret != SYSTEM_INFO_ERROR_NONE) {
+               _E("failed to get system info"); /* LCOV_EXCL_LINE */
+               return false; /* LCOV_EXCL_LINE */
+       }
+
+       retrieved = true;
+
+       return feature;
+}
 
 static gint __comp_by_id(gconstpointer a, gconstpointer b)
 {
-       widget_context_s *wc = (widget_context_s*)a;
+       widget_context_s *wc = (widget_context_s *)a;
 
-       return strcmp(wc->id, (const char*)b);
+       return strcmp(wc->id, (const char *)b);
 }
 
-static void __check_status_for_cgroup(void)
+static widget_context_s *__find_context_by_id(const char *id)
 {
-       GList *iter = g_list_first(contexts);
+       GList *ret;
+       GList *contexts = _widget_app_get_contexts();
 
-       while (iter != NULL) {
-               widget_context_s *cxt = (widget_context_s*) iter->data;
+       if (id == NULL)
+               return NULL;
 
-               if (cxt->state != WC_PAUSED) {
-                       if (is_background != 0) {
-                               is_background = 0;
-                               _I("enter foreground group");
-                               //TODO: Do something to enter foreground group
-                       }
-                       return;
-               }
-               iter = g_list_next(iter);
-       }
+       ret = g_list_find_custom(contexts, id, __comp_by_id);
+       if (ret == NULL)
+               return NULL;
 
-       if (g_list_length(contexts) > 0 && is_background == 0) {
-               is_background = 1;
-               _I("enter background group");
-               //TODO: DO something to enter background group
-       }
+       return ret->data;
 }
 
-static widget_context_s* __find_context_by_id(const char *id)
+static gint __comp_by_state(gconstpointer a, gconstpointer b)
 {
-       GList* ret = g_list_find_custom(contexts, id, __comp_by_id);
+       widget_context_s *wc = (widget_context_s *)a;
+
+       if (wc->state == (widget_obj_state_e)GPOINTER_TO_INT(b))
+               return 0;
+
+       return -1;
+}
 
+static widget_context_s *__find_context_by_state(widget_obj_state_e state)
+{
+       GList *ret;
+       GList *contexts = _widget_app_get_contexts();
+
+       ret = g_list_find_custom(contexts, GINT_TO_POINTER((int)state), __comp_by_state);
        if (ret == NULL)
                return NULL;
 
        return ret->data;
 }
 
-static int __provider_create_cb(const char *id, const char *content, int w,
-                               int h,
-                               void *data)
+static gint __comp_by_win(gconstpointer a, gconstpointer b)
 {
-       int ret = WIDGET_ERROR_FAULT;
-       widget_context_s *wc = (widget_context_s*)malloc(sizeof(widget_context_s));
-       if (wc == NULL)
-               return WIDGET_ERROR_OUT_OF_MEMORY;
+       int win = GPOINTER_TO_INT(b);
+       widget_context_s *wc = (widget_context_s *)a;
 
-       wc->id = strdup(id);
-       wc->state = WC_READY;
-       wc->tag = NULL;
-       wc->ops = widget_class->ops;
-       wc->ops_private = widget_class->ops_private;
-       contexts = g_list_append(contexts, wc);
+       return (wc && wc->win_id == win) ? 0 : -1;
+}
+
+static widget_context_s *__find_context_by_win(int win)
+{
+       GList *contexts = _widget_app_get_contexts();
+       GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
+
+       if (ret == NULL)
+               return NULL;
+
+       return ret->data;
+}
+
+static int __send_lifecycle_event(const char *class_id, const char *instance_id,
+       int status)
+{
+       bundle *b = bundle_create();
+       char pkgid[256] = {0, };
+       int ret;
+
+       if (b == NULL) {
+               _E("out of memory"); /* LCOV_EXCL_LINE */
+               return -1; /* LCOV_EXCL_LINE */
+       }
 
-       if (wc->ops.create) {
-               bundle *b = NULL;
-               if (content)
-                       b = bundle_decode((const bundle_raw*)content, strlen(content));
-               ret = wc->ops.create(wc, b,  w, h, widget_class->user_data);
-               if (b)
-                       bundle_free(b);
+       if (package_id == NULL) {
+               ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
+               if (ret == 0)
+                       package_id = strdup(pkgid);
        }
-       _I("widget obj was created");
+
+       bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
+       bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
+       bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
+       if (package_id)
+               bundle_add_str(b, AUL_K_PKGID, package_id);
+
+       _D("send lifecycle %s(%d)", instance_id, status);
+       ret = aul_app_com_send("widget.status", b);
+       if (ret < 0)
+               _E("send lifecycle error:%d", ret); /* LCOV_EXCL_LINE */
+
+       bundle_free(b);
 
        return ret;
 }
 
-static int __provider_resize_cb(const char *id, int w, int h, void *data)
+static int __send_update_status(const char *class_id, const char *instance_id,
+       int status, bundle *extra)
 {
-       int ret = WIDGET_ERROR_FAULT;
-       widget_context_s *cxt = __find_context_by_id(id);
+       bundle *b;
+       int lifecycle = -1;
+       bundle_raw *raw = NULL;
+       int len;
+       char *viewer_endpoint = _widget_app_get_viewer_endpoint();
 
-       if (cxt) {
-               if (cxt->ops.resize)
-                       ret = cxt->ops.resize(cxt, w, h, widget_class->user_data);
-               _I("received resizing signal");
-       } else {
-               _E("could not find widget obj : %s", __FUNCTION__);
+       b = bundle_create();
+       if (!b) {
+               _E("out of memory"); /* LCOV_EXCL_LINE */
+               return -1; /* LCOV_EXCL_LINE */
        }
 
-       return ret;
+       bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
+       bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
+       bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
+
+       if (extra) {
+               bundle_encode(extra, &raw, &len);
+               bundle_add_str(b, WIDGET_K_CONTENT_INFO, (const char *)raw);
+               aul_widget_instance_add(class_id, instance_id);
+       }
+
+       _D("send update %s(%d) to %s", instance_id, status, viewer_endpoint);
+       aul_app_com_send(viewer_endpoint, b);
+
+       switch (status) {
+       case WIDGET_INSTANCE_EVENT_CREATE:
+               lifecycle = WIDGET_LIFE_CYCLE_EVENT_CREATE;
+               break;
+       case WIDGET_INSTANCE_EVENT_DESTROY:
+               lifecycle = WIDGET_LIFE_CYCLE_EVENT_DESTROY;
+               break;
+       case WIDGET_INSTANCE_EVENT_PAUSE:
+               lifecycle = WIDGET_LIFE_CYCLE_EVENT_PAUSE;
+               break;
+       case WIDGET_INSTANCE_EVENT_RESUME:
+               lifecycle = WIDGET_LIFE_CYCLE_EVENT_RESUME;
+               break;
+       }
+
+       if (lifecycle > -1)
+               __send_lifecycle_event(class_id, instance_id, lifecycle);
+
+       bundle_free(b);
+       if (raw)
+               free(raw);
+
+       return 0;
 }
 
-static int __provider_destroy_cb(const char *id, widget_destroy_type_e reason,
-                               void *data)
+static int __instance_resume(widget_class_h handle, const char *id, int send_update)
 {
-       int ret = WIDGET_ERROR_FAULT;
-       widget_context_s *cxt = __find_context_by_id(id);
-
-       if (cxt) {
-               cxt->state = WC_TERMINATED;
-               if (cxt->ops.destroy) {
-                       bundle *b  = bundle_create();
-                       ret = cxt->ops.destroy(cxt, (widget_app_destroy_type_e)reason, b, widget_class->user_data);
-
-                       bundle_raw *raw = NULL;
-                       int len;
-
-                       bundle_encode(b, &raw, &len);
-                       if (raw) {
-                               widget_provider_app_send_extra_info(id, (const char*)raw, NULL);
-                               free(raw);
-                       }
+       widget_context_s *wc = __find_context_by_id(id);
+       int ret;
 
-                       bundle_free(b);
-                       contexts = g_list_remove(contexts, cxt);
+       if (!wc) {
+               _E("context not found: %s", id); /* LCOV_EXCL_LINE */
+               return -1; /* LCOV_EXCL_LINE */
+       }
 
-                       free(cxt->id);
-                       free(cxt);
+       if (wc->state == WC_RUNNING) {
+               _D("%s is already in running state", id); /* LCOV_EXCL_LINE */
+               return 0; /* LCOV_EXCL_LINE */
+       }
+
+       if (wc->state == WC_TERMINATED) {
+               _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
+               return 0; /* LCOV_EXCL_LINE */
+       }
+
+       if (handle->ops.resume)
+               handle->ops.resume(wc, handle->user_data);
+
+       wc->state = WC_RUNNING;
+       _D("%s is resumed", id);
+       if (send_update) {
+               ret = __send_update_status(handle->classid, wc->id,
+                       WIDGET_INSTANCE_EVENT_RESUME, NULL);
+               if (!fg_signal) {
+                       _D("Send fg signal to resourceD");
+                       aul_send_app_status_change_signal(getpid(),
+                                       appid,
+                                       package_id,
+                                       STATUS_FOREGROUND,
+                                       APP_TYPE_WIDGET);
+                       fg_signal = true;
                }
-               _I("widget obj was deleted");
        } else {
-               _E("could not find widget obj : %s", __FUNCTION__);
+               ret = 0;
        }
 
        return ret;
 }
 
-static int __provider_update_cb(const char *id, const char *content, int force,
-                               void *data)
+static int __instance_pause(widget_class_h handle, const char *id, int send_update)
 {
-       int ret = WIDGET_ERROR_FAULT;
-       widget_context_s *cxt = __find_context_by_id(id);
-
-       if (cxt) {
-               if (cxt->ops.update) {
-                       bundle *b = NULL;
-                       if (content)
-                               b = bundle_decode((const bundle_raw*)content, strlen(content));
-                       ret = cxt->ops.update(cxt, b, force, widget_class->user_data);
-
-                       if (b)
-                               bundle_free(b);
+       widget_context_s *wc = __find_context_by_id(id);
+       int ret;
+
+       if (!wc) {
+               _E("context not found: %s", id); /* LCOV_EXCL_LINE */
+               return -1; /* LCOV_EXCL_LINE */
+       }
+
+       if (wc->state == WC_PAUSED) {
+               _D("%s is already in paused state", id); /* LCOV_EXCL_LINE */
+               return 0; /* LCOV_EXCL_LINE */
+       }
+
+       if (wc->state == WC_TERMINATED) {
+               _D("%s is in terminated state", id); /* LCOV_EXCL_LINE */
+               return 0; /* LCOV_EXCL_LINE */
+       }
+
+       if (handle->ops.pause)
+               handle->ops.pause(wc, handle->user_data);
+
+       wc->state = WC_PAUSED;
+       _D("%s is paused", id);
+       if (send_update) {
+               ret = __send_update_status(handle->classid, wc->id,
+                       WIDGET_INSTANCE_EVENT_PAUSE, NULL);
+               wc = __find_context_by_state(WC_RUNNING);
+               if (!wc && fg_signal) {
+                       _D("Send bg signal to resourceD");
+                       aul_send_app_status_change_signal(getpid(),
+                                       appid,
+                                       package_id,
+                                       STATUS_BACKGROUND,
+                                       APP_TYPE_WIDGET);
+                       fg_signal = false;
                }
-               _I("received updating signal");
        } else {
-               _E("could not find widget obj : %s", __FUNCTION__);
+               ret = 0;
        }
 
        return ret;
 }
 
-static int __provider_pause_cb(const char *id, void *data)
+static int __instance_resize(widget_class_h handle, const char *id, int w, int h)
 {
-       int ret = WIDGET_ERROR_FAULT;
-       widget_context_s *cxt = __find_context_by_id(id);
-
-       if (cxt) {
-               if (cxt->ops.pause)
-                       ret = cxt->ops.pause(cxt, widget_class->user_data);
-               cxt->state = WC_PAUSED;
-               _I("widget obj was paused");
-       } else {
-               _E("could not find widget obj : %s", __FUNCTION__);
+       widget_context_s *wc = __find_context_by_id(id);
+       int ret;
+
+       if (!wc) {
+               _E("context not found: %s", id); /* LCOV_EXCL_LINE */
+               return -1; /* LCOV_EXCL_LINE */
        }
 
-       __check_status_for_cgroup();
+       if (handle->ops.resize)
+               handle->ops.resize(wc, w, h, handle->user_data);
+
+       _D("%s is resized to %dx%d", id, w, h);
+       ret = __send_update_status(handle->classid, wc->id,
+               WIDGET_INSTANCE_EVENT_SIZE_CHANGED, NULL);
+
        return ret;
 }
 
-static int __provider_resume_cb(const char *id, void *data)
+/* LCOV_EXCL_START */
+static int __instance_update_all(widget_class_h handle, int force, const char *content)
 {
-       int ret = WIDGET_ERROR_FAULT;
-       widget_context_s *cxt = __find_context_by_id(id);
-
-       if (cxt) {
-               if (cxt->ops.resume)
-                       ret = cxt->ops.resume(cxt, widget_class->user_data);
-               cxt->state = WC_RUNNING;
-               _I("widget obj was resumed");
-       } else {
-               _E("could not find widget obj : %s", __FUNCTION__);
+       widget_context_s *wc;
+       int ret = 0;
+       bundle *b = NULL;
+       GList *context = _widget_app_get_contexts();
+
+       if (content)
+               b = bundle_decode((const bundle_raw *)content, strlen(content));
+
+       if (handle->ops.update) {
+               while (context) {
+                       wc = (widget_context_s *)context->data;
+                       handle->ops.update(wc, b, force, handle->user_data);
+                       ret = __send_update_status(handle->classid, wc->id,
+                               WIDGET_INSTANCE_EVENT_UPDATE, NULL);
+                       _D("updated:%s", wc->id);
+                       context = context->next;
+               }
        }
 
-       __check_status_for_cgroup();
+       if (b)
+               bundle_free(b);
+
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
-static int __provider_text_signal_cb(const char *id, const char *signal_name,
-                               const char *source, struct widget_event_info *info, void *data)
+/* LCOV_EXCL_START */
+static int __instance_update(widget_class_h handle, const char *id, int force, const char *content)
 {
-       int ret = WIDGET_ERROR_FAULT;
-       widget_context_s *cxt = __find_context_by_id(id);
+       widget_context_s *wc = __find_context_by_id(id);
+       int ret = 0;
+       bundle *b = NULL;
+       if (!wc) {
+               _E("context not found: %s", id);
+               return -1;
+       }
 
-       if (cxt) {
-               if (cxt->ops_private.text_signal) {
-                       ret = cxt->ops_private.text_signal(cxt, signal_name, source,
-                                                       (widget_obj_event_info_s*)info, widget_class->user_data);
-               }
-               _I("received text signal");
-       } else {
-               _E("could not find widget obj : %s", __FUNCTION__);
+       if (content)
+               b = bundle_decode((const bundle_raw *)content, strlen(content));
+
+       if (handle->ops.update) {
+               handle->ops.update(wc, b, force, handle->user_data);
+               ret = __send_update_status(handle->classid, wc->id,
+                       WIDGET_INSTANCE_EVENT_UPDATE, NULL);
+               _D("updated:%s", id);
        }
 
+       if (b)
+               bundle_free(b);
+
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
-static const widget_class_factory_full_s*
-__widget_class_factory_override_text_signal(widget_instance_text_signal_cb op)
+static int __instance_create(widget_class_h handle, const char *id, const char *content, int w, int h)
 {
-       widget_class_tmp.ops_private.text_signal = op;
-       return &factory;
-}
+       widget_context_s *wc = NULL;
+       int ret = 0;
+       bundle *content_info = NULL;
 
-static void __free_handler_cb(gpointer data)
-{
-       if (data)
-               free(data);
+       wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
+       if (!wc) {
+               _E("Out of memory");
+               return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
+
+       wc->state = WC_READY;
+       wc->id = strdup(id);
+       wc->provider = handle;
+       wc->win = NULL;
+       wc->win_id = -1;
+
+       if (content) {
+               wc->content = strdup(content);
+               content_info = bundle_decode((const bundle_raw *)content, strlen(content));
+       }
+
+       _widget_app_add_context(wc);
+
+       ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
+       if (ret < 0) {
+               _W("Create callback resturns error(%d)", ret);
+               ret = __send_update_status(handle->classid, wc->id,
+                               WIDGET_INSTANCE_EVENT_CREATE_ABORTED, NULL);
+               _widget_app_remove_context(wc);
+               if (wc->id)
+                       free(wc->id);
+               if (wc->content)
+                       free(wc->content);
+               free(wc);
+
+               if (_widget_app_get_contexts() == NULL && !exit_called)
+                       widget_app_exit();
+       } else {
+               ret = __send_update_status(handle->classid, wc->id,
+                       WIDGET_INSTANCE_EVENT_CREATE, NULL);
+
+               if (content == NULL)
+                       content = "NULL";
+
+               aul_widget_instance_add(handle->classid, id);
+       }
+
+       if (content_info)
+               bundle_free(content_info);
+
+       return ret;
 }
 
-static void __free_handler_list(void)
+static int __instance_destroy(widget_class_h handle, const char *id,
+               widget_app_destroy_type_e reason, int send_update)
 {
-       int i;
+       widget_context_s *wc = __find_context_by_id(id);
+       int ret = 0;
+       int event = WIDGET_INSTANCE_EVENT_TERMINATE;
+       bundle *content_info;
+
+       if (!wc) {
+               _E("could not find widget obj: %s, clear amd info", id);        /* LCOV_EXCL_LINE */
+               aul_widget_instance_del(handle->classid, id);                   /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NONE;                                       /* LCOV_EXCL_LINE */
+       }
 
-       for (i = 0; i < WIDGET_APP_EVENT_MAX; i++) {
-               g_list_free_full(handler_list[i], __free_handler_cb);
-               handler_list[i] = NULL;
+       wc->state = WC_TERMINATED;
+       if (wc->content)
+               content_info = bundle_decode((const bundle_raw *)wc->content, strlen(wc->content));
+       else
+               content_info = bundle_create();
+
+       handle->ops.destroy(wc, reason, content_info,
+                       handle->user_data);
+
+       if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
+               event = WIDGET_INSTANCE_EVENT_DESTROY;
+               aul_widget_instance_del(handle->classid, id);
+       } else {
+               ret = __send_update_status(handle->classid, id,
+                               WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
        }
+
+       if (content_info)
+               bundle_free(content_info);
+
+       ret = __send_update_status(handle->classid, id, event, NULL);
+
+       _widget_app_remove_context(wc);
+
+       if (wc->id)
+               free(wc->id);
+
+       if (wc->content)
+               free(wc->content);
+
+       free(wc);
+
+       if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
+               widget_app_exit();
+
+       return ret;
 }
 
-static void __control(bundle *b)
+static widget_class_h __find_class_handler(const char *class_id,
+               widget_class_h handle)
 {
-       app_control_h app_control;
+       if (!class_id || !handle)
+               return NULL;
 
-       if (is_init_provider) {
-               _E("already initialized");
-               return;
+       widget_class_h head = handle;
+
+       while (head) {
+               if (head->classid && strcmp(head->classid, class_id) == 0)
+                       return head;
+
+               head = head->next;
        }
 
-       if (app_control_create_event(b, &app_control) != 0) {
-               _E("failed to get the app_control handle");
+       return NULL;
+}
+
+/* LCOV_EXCL_START */
+static void __resize_window(char *id, int w, int h)
+{
+       widget_context_s *wc = __find_context_by_id(id);
+
+       if (!wc) {
+               _E("can not find instance: %s", id);
                return;
        }
 
-       char *op = NULL;
-       app_control_get_operation(app_control, &op);
+       if (wc->win)
+               evas_object_resize(wc->win, w, h);
+       else
+               _E("unable to find window of %d", wc->id);
+}
+/* LCOV_EXCL_STOP */
 
-       if (op && strcmp(op, "http://tizen.org/appcontrol/operation/main") == 0) {
-               static struct widget_provider_event_callback cb = {
-                       .create = __provider_create_cb,
-                       .resize = __provider_resize_cb,
-                       .destroy = __provider_destroy_cb,
+static void __control(bundle *b)
+{
+       char *class_id = NULL;
+       char *id = NULL;
+       char *operation = NULL;
+       char *content = NULL;
+       char *w_str = NULL;
+       char *h_str = NULL;
+       int w = 0;
+       int h = 0;
+       char *remain = NULL;
+       int force;
+       char *force_str = NULL;
+       widget_class_h handle = NULL;
+
+       bundle_get_str(b, WIDGET_K_CLASS, &class_id);
+       /* for previous version compatibility, use appid for default class id */
+       if (class_id == NULL)
+               class_id = appid;
+
+       bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
+       bundle_get_str(b, WIDGET_K_OPERATION, &operation);
+
+       handle = __find_class_handler(class_id, class_provider);
+       if (!handle) {
+               _E("no handle provided: %s", class_id); /* LCOV_EXCL_LINE */
+               goto error;
+       }
 
-                       .update = __provider_update_cb,
-                       .text_signal = __provider_text_signal_cb,
+       if (!operation) {
+               _E("no operation provided");
+               goto error;
+       }
 
-                       .pause = __provider_pause_cb,
-                       .resume = __provider_resume_cb,
+       bundle_get_str(b, WIDGET_K_FORCE, &force_str);
+
+       if (force_str && strcmp(force_str, "true") == 0)
+               force = 1;
+       else
+               force = 0;
+
+       bundle_get_str(b, WIDGET_K_CONTENT_INFO, &content);
+       bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
+       bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
+       if (w_str)
+               w = (int)g_ascii_strtoll(w_str, &remain, 10);
+
+       if (h_str)
+               h = (int)g_ascii_strtoll(h_str, &remain, 10);
+
+       if (strcmp(operation, "create") == 0) {
+               __instance_create(handle, id, content, w, h);
+       } else if (strcmp(operation, "resize") == 0) {
+               __resize_window(id, w, h);
+       } else if (strcmp(operation, "update") == 0) {
+               if (id)
+                       __instance_update(handle, id, force, content);
+               else
+                       __instance_update_all(handle, force, content);
+
+       } else if (strcmp(operation, "destroy") == 0) {
+               __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_PERMANENT, UPDATE_ALL);
+       } else if (strcmp(operation, "resume") == 0) {
+               __instance_resume(handle, id, UPDATE_ALL);
+       } else if (strcmp(operation, "pause") == 0) {
+               __instance_pause(handle, id, UPDATE_ALL);
+       } else if (strcmp(operation, "terminate") == 0) {
+               __instance_destroy(handle, id, WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
+       }
 
-                       .connected = NULL,
-                       .disconnected = NULL,
+       return;
+error:
+       LOGD("error on control");
+       return;
+}
 
-                       .data = NULL,
-               };
+static void __pause_all(int send_update)
+{
+       GList *contexts = _widget_app_get_contexts();
+       GList *iter = g_list_first(contexts);
 
-               if (widget_provider_app_init(app_control, &cb) == 0)
-                       is_init_provider = 1;
+       while (iter != NULL) {
+               widget_context_s *cxt = (widget_context_s *)iter->data;
 
+               switch (cxt->state) {
+               case WC_READY:
+                       __instance_resume(cxt->provider, cxt->id, send_update);
+                       __instance_pause(cxt->provider, cxt->id, send_update);
+                       break;
+               case WC_RUNNING:
+                       __instance_pause(cxt->provider, cxt->id, send_update);
+                       break;
+               }
+               LOGD("pause %s", cxt->id);
+               iter = g_list_next(iter);
        }
-
-       app_control_destroy(app_control);
-       if (op)
-               free(op);
 }
 
-static void __pause_all()
+/* LCOV_EXCL_START */
+static void __resume_all(int send_update)
 {
+       GList *contexts = _widget_app_get_contexts();
        GList *iter = g_list_first(contexts);
 
        while (iter != NULL) {
-               widget_context_s *cxt = (widget_context_s*) iter->data;
-               const char *id = cxt->id;
+               widget_context_s *cxt = (widget_context_s *)iter->data;
 
                switch (cxt->state) {
                case WC_READY:
-                       __provider_resume_cb(id, NULL);
-                       __provider_pause_cb(id, NULL);
+                       __instance_resume(cxt->provider, cxt->id, send_update);
                        break;
-
-               case WC_RUNNING:
-                       __provider_pause_cb(id, NULL);
+               case WC_PAUSED:
+                       __instance_resume(cxt->provider, cxt->id, send_update);
                        break;
                }
                iter = g_list_next(iter);
        }
 }
+/* LCOV_EXCL_STOP */
+
+static void __destroy_all(int reason, int send_update)
+{
+       GList *contexts = _widget_app_get_contexts();
+       GList *iter = g_list_first(contexts);
+
+       __pause_all(send_update);
+       while (iter != NULL) {
+               widget_context_s *cxt = (widget_context_s *)iter->data;
+               iter = g_list_next(iter);
+               switch (cxt->state) {
+               case WC_PAUSED:
+                       LOGD("destroy %d : %s", cxt->state, cxt->id);
+                       __instance_destroy(cxt->provider, cxt->id, reason, send_update);
+                       break;
+               }
+       }
+}
+
+
+static Eina_Bool __show_cb(void *data, int type, void *event)
+{
+       Ecore_Wl_Event_Window_Show *ev = event;
+       widget_context_s *cxt = __find_context_by_win(ev->win);
+
+       LOGD("show %d %d", (unsigned int)ev->win, (unsigned int)ev->data[0]);
+
+       if (cxt)
+               __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
+       else
+               LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+static Eina_Bool __hide_cb(void *data, int type, void *event)
+{
+       Ecore_Wl_Event_Window_Hide *ev = event;
+       widget_context_s *cxt = __find_context_by_win(ev->win);
+
+
+       LOGD("hide %d", (unsigned int)ev->win);
+
+       if (cxt)
+               __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
+       else
+               LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+static Eina_Bool __visibility_cb(void *data, int type, void *event)
+{
+       Ecore_Wl_Event_Window_Visibility_Change *ev = event;
+       widget_context_s *cxt = __find_context_by_win(ev->win);
+
+       LOGD("visiblity change: %d %d", (unsigned int)ev->win,  (unsigned int)ev->fully_obscured);
+
+       if (!cxt) {
+               LOGE("unknown window error: %d", ev->win);
+               return ECORE_CALLBACK_RENEW;
+       }
+
+       if (cxt->state == WC_PAUSED && ev->fully_obscured == 0) {
+               __instance_resume(cxt->provider, cxt->id, UPDATE_ALL);
+       } else if (cxt->state == WC_RUNNING && ev->fully_obscured == 1) {
+               __instance_pause(cxt->provider, cxt->id, UPDATE_ALL);
+       } else {
+               LOGD("cxt:%s state:%d obscured:%d", cxt->id, cxt->state, ev->fully_obscured);
+       }
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+/* LCOV_EXCL_START */
+static Eina_Bool __lower_cb(void *data, int type, void *event)
+{
+       LOGD("lower");
+       return ECORE_CALLBACK_RENEW;
+}
+/* LCOV_EXCL_STOP */
+
+static Eina_Bool __configure_cb(void *data, int type, void *event)
+{
+       Ecore_Wl_Event_Window_Configure *ev = event;
+       widget_context_s *cxt = __find_context_by_win(ev->win);
+
+       LOGD("configure: %d %d", ev->w, ev->h);
+
+       if (!cxt) {
+               LOGE("unknown window error: %d", ev->win); /* LCOV_EXCL_LINE */
+               return ECORE_CALLBACK_RENEW; /* LCOV_EXCL_LINE */
+       }
+
+       if (cxt->state == WC_PAUSED || cxt->state == WC_RUNNING)
+               __instance_resize(cxt->provider, cxt->id, ev->w, ev->h);
+       LOGD("cxt:%s resized to %dx%d", cxt->id, ev->w, ev->h);
+
+       return ECORE_CALLBACK_RENEW;
+}
+
+static void __add_climsg()
+{
+       ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, NULL);
+       ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, NULL);
+       ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE, __visibility_cb, NULL);
+       ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __lower_cb, NULL);
+       ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
+}
+
+static void __get_content(bundle *b)
+{
+       char *instance_id = NULL;
+       widget_context_s *cxt = NULL;
+
+       bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
+       if (!instance_id)
+               return;
+
+       cxt = __find_context_by_id(instance_id);
+       if (!cxt) {
+               _E("can not find instance id:%s", instance_id);
+               return;
+       }
+
+       if (cxt->content) {
+               bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, cxt->content);
+               _D("content info of %s found", cxt->id);
+       } else {
+               bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
+               _D("empty content info added");
+       }
+}
 
 static int __aul_handler(aul_type type, bundle *b, void *data)
 {
+       char *caller = NULL;
+       char *remain = NULL;
+
        switch (type) {
        case AUL_START:
+               if (b) {
+                       bundle_get_str(b, WIDGET_K_CALLER, &caller);
+                       if (caller) {
+                               caller_pid = g_ascii_strtoll(caller, &remain,
+                                               10);
+                       } else {
+                               /* using caller appid and query pid using caller appid? */
+                               _E("no caller pid");
+                       }
+               }
+
                __control(b);
                break;
        case AUL_RESUME:
+               __resume_all(UPDATE_ALL);
                break;
        case AUL_TERMINATE:
                widget_app_exit();
                break;
+       case AUL_WIDGET_CONTENT:
+               __get_content(b);
+               break;
        default:
                break;
        }
@@ -410,131 +879,165 @@ static int __aul_handler(aul_type type, bundle *b, void *data)
        return 0;
 }
 
-static char* __get_domain_name(const char *aid)
+static char *__get_domain_name(char *appid)
 {
-       char *name_token = NULL;
+       char *name_token;
 
-       if (aid == NULL) {
-               _E("appid is NULL");
-               return NULL;
+       if (appid == NULL) {
+               _E("appid is NULL"); /* LCOV_EXCL_LINE */
+               return NULL; /* LCOV_EXCL_LINE */
        }
 
-       /* com.vendor.name -> name */
-       name_token = strrchr(aid, '.');
+       name_token = strrchr(appid, '.');
 
        if (name_token == NULL) {
-               _E("appid is invalid");
-               return strdup(aid);
+               _E("appid is invalid"); /* LCOV_EXCL_LINE */
+               return appid; /* LCOV_EXCL_LINE */
        }
 
        name_token++;
 
-       return strdup(name_token);
+       return name_token;
 }
 
+/* LCOV_EXCL_START */
+static void __on_poweroff(keynode_t *key, void *data)
+{
+       int val;
+
+       val = vconf_keynode_get_int(key);
+       switch (val) {
+       case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
+       case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
+               _I("power off changed: %d", val);
+               widget_app_exit();
+               break;
+       case VCONFKEY_SYSMAN_POWER_OFF_NONE:
+       case VCONFKEY_SYSMAN_POWER_OFF_POPUP:
+       default:
+               /* DO NOTHING */
+               break;
+       }
+}
+/* LCOV_EXCL_STOP */
+
+extern int _set_i18n(const char *name);
+
 static int __before_loop(int argc, char **argv)
 {
        int r;
+       bundle *kb = NULL;
+       char *name;
+       char *viewer_endpoint = NULL;
 
 #if !(GLIB_CHECK_VERSION(2, 36, 0))
        g_type_init();
 #endif
 
-       elm_init(argc, argv);
+       kb = bundle_import_from_argv(argc, argv);
+       if (kb) {
+               bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
+               if (viewer_endpoint) {
+                       _E("viewer endpoint :%s", viewer_endpoint);
+                       _widget_app_set_viewer_endpoint(viewer_endpoint);
+               } else {
+                       _E("endpoint is missing");
+               }
+
+               bundle_free(kb);
+               kb = NULL;
+       } else {
+               _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
+       }
 
-       factory.override_text_signal = __widget_class_factory_override_text_signal;
+       screen_connector_provider_init();
+       elm_init(argc, argv);
 
        r = aul_launch_init(__aul_handler, NULL);
        if (r < 0) {
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
-                                       "Fail to call the aul_launch_init");
+               /* LCOV_EXCL_START */
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__,
+                               "Fail to call the aul_launch_init");
+               /* LCOV_EXCL_STOP */
        }
 
        r = aul_launch_argv_handler(argc, argv);
        if (r < 0) {
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
-                                       "Fail to call the aul_launch_argv_handler");
+               /* LCOV_EXCL_START */
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__,
+                               "Fail to call the aul_launch_argv_handler");
+               /* LCOV_EXCL_STOP */
        }
 
        r = app_get_id(&appid);
        if (r != APP_ERROR_NONE)
                return r;
 
-       char *name = __get_domain_name(appid);
+       name = __get_domain_name(appid);
 
        if (name == NULL) {
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
-                                       "Fail to call __get_domain_name");
+               /* LCOV_EXCL_START */
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__,
+                               "Fail to call __get_domain_name");
+               /* LCOV_EXCL_STOP */
        }
 
        r = _set_i18n(name);
 
-       free(name);
        if (r < 0) {
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
-                                       "Fail to call _set_i18n");
+               /* LCOV_EXCL_START */
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__,
+                               "Fail to call _set_i18n");
+               /* LCOV_EXCL_STOP */
        }
 
-       widget_provider_app_create_app();
+       __add_climsg();
+
+       _widget_core_set_appcore_event_cb();
 
-       memset(&widget_class_tmp, 0, sizeof(widget_class_tmp));
-       widget_class = app_ops->create(app_user_data);
-       if (widget_class == NULL) {
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
-                                       "widget_class is NULL");
+       class_provider = app_ops->create(app_user_data);
+       if (class_provider == NULL) {
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, "widget_class is NULL");
        }
 
+       vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff, NULL);
+
        return WIDGET_ERROR_NONE;
 }
 
 static void __after_loop()
 {
-       __pause_all();
-       widget_provider_app_terminate_app(WIDGET_DESTROY_TYPE_TEMPORARY, 1);
+       exit_called = 1;
+       vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, __on_poweroff);
+
+       __pause_all(UPDATE_LOCAL);
+       __destroy_all(WIDGET_APP_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
 
        if (app_ops->terminate)
                app_ops->terminate(app_user_data);
 
-       if (is_init_provider) {
-               widget_provider_app_fini();
-               is_init_provider = 0;
-       }
-       __free_handler_list();
-       elm_shutdown();
-       if (appid)
-               free(appid);
-       if (contexts)
-               g_list_free(contexts);
-       contexts = NULL;
-}
-
-static gboolean __finish_event_cb(gpointer user_data)
-{
-       if (user_data == NULL)
-               return FALSE;
-
-       widget_context_s *wc = (widget_context_s*) user_data;
-       const char* id = wc->id;
+       screen_connector_provider_fini();
 
-       switch (wc->state) {
-       case WC_READY:
-               __provider_resume_cb(id, NULL);
-               __provider_pause_cb(id, NULL);
-               __provider_destroy_cb(id, WIDGET_DESTROY_TYPE_DEFAULT, NULL);
-               break;
+       _widget_app_free_viewer_endpoint();
+       _widget_core_unset_appcore_event_cb();
+       __free_handler_list();
 
-       case WC_RUNNING:
-               __provider_pause_cb(id, NULL);
-               __provider_destroy_cb(id, WIDGET_DESTROY_TYPE_DEFAULT, NULL);
-               break;
+       if (package_id) {
+               free(package_id);
+               package_id = NULL;
+       }
 
-       case WC_PAUSED:
-               __provider_destroy_cb(id, WIDGET_DESTROY_TYPE_DEFAULT, NULL);
-               break;
+       if (appid) {
+               free(appid);
+               appid = NULL;
        }
 
-       widget_provider_app_send_deleted(id);
-       return FALSE;
+       elm_shutdown();
 }
 
 static void __on_low_memory(keynode_t *key, void *data)
@@ -549,7 +1052,7 @@ static void __on_low_memory(keynode_t *key, void *data)
                _I("widget_app_low_memory");
 
                event.type = APP_EVENT_LOW_MEMORY;
-               event.value = (void*)&val;
+               event.value = (void *)&val;
 
                GList *iter = g_list_first(handler_list[APP_EVENT_LOW_MEMORY]);
 
@@ -573,7 +1076,7 @@ static void __on_low_battery(keynode_t *key, void *data)
                _I("widget_app_low_battery");
 
                event.type = APP_EVENT_LOW_BATTERY;
-               event.value = (void*)&val;
+               event.value = (void *)&val;
 
                GList *iter = g_list_first(handler_list[APP_EVENT_LOW_BATTERY]);
 
@@ -598,7 +1101,7 @@ static void __on_lang_changed(keynode_t *key, void *data)
        _I("widget_app_lang_changed");
 
        event.type = APP_EVENT_LANGUAGE_CHANGED;
-       event.value = (void*)val;
+       event.value = (void *)val;
 
        GList *iter = g_list_first(handler_list[APP_EVENT_LANGUAGE_CHANGED]);
 
@@ -622,7 +1125,7 @@ static void __on_region_changed(keynode_t *key, void *data)
        _I("widget_app_region_changed");
 
        event.type = APP_EVENT_REGION_FORMAT_CHANGED;
-       event.value = (void*)val;
+       event.value = (void *)val;
 
        GList *iter = g_list_first(handler_list[APP_EVENT_REGION_FORMAT_CHANGED]);
 
@@ -675,33 +1178,50 @@ static void __unregister_event(int event_type)
        }
 }
 
+static void _widget_core_set_appcore_event_cb(void)
+{
+       __register_event(APP_EVENT_LANGUAGE_CHANGED);
+       __register_event(APP_EVENT_REGION_FORMAT_CHANGED);
+}
+
+static void _widget_core_unset_appcore_event_cb(void)
+{
+       __unregister_event(APP_EVENT_LANGUAGE_CHANGED);
+       __unregister_event(APP_EVENT_REGION_FORMAT_CHANGED);
+}
+
 EXPORT_API int widget_app_main(int argc, char **argv,
-                               widget_app_lifecycle_callback_s *callback, void *user_data)
+               widget_app_lifecycle_callback_s *callback, void *user_data)
 {
        int r;
-       bool feature;
 
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
-
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
        if (argc <= 0 || argv == NULL || callback == NULL)
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, NULL);
 
        if (callback->create == NULL)
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
-
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__,
+                               "widget_app_create_cb() callback must be "
+                               "registered");
        app_ops = callback;
        app_user_data = user_data;
        r = __before_loop(argc, argv);
-       if (r < 0)
+       if (r < 0) {
+               if (appid) {
+                       free(appid);
+                       appid = NULL;
+               }
                return r;
+       }
 
        ecore_main_loop_begin();
-       //aul_status_update(STATUS_DYING);
+       aul_status_update(STATUS_DYING);
        __after_loop();
 
        return WIDGET_ERROR_NONE;
@@ -709,66 +1229,84 @@ EXPORT_API int widget_app_main(int argc, char **argv,
 
 EXPORT_API int widget_app_exit(void)
 {
-       int r;
-       bool feature;
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
+       if (exit_called)
+               return WIDGET_ERROR_NONE;
 
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       exit_called = 1;
 
        ecore_main_loop_quit();
 
        return WIDGET_ERROR_NONE;
 }
 
-EXPORT_API int widget_app_terminate_context(widget_context_h context)
+/* LCOV_EXCL_START */
+static gboolean __finish_event_cb(gpointer user_data)
 {
-       int r;
-       bool feature;
+       if (user_data == NULL)
+               return FALSE;
 
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
+       widget_context_s *wc = (widget_context_s *)user_data;
 
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       switch (wc->state) {
+       case WC_READY:
+               __instance_resume(wc->provider, wc->id, UPDATE_LOCAL);
+       case WC_RUNNING:
+               __instance_pause(wc->provider, wc->id, UPDATE_LOCAL);
+       case WC_PAUSED:
+               __instance_destroy(wc->provider, wc->id,
+                               WIDGET_DESTROY_TYPE_TEMPORARY, UPDATE_ALL);
+               break;
+       default:
+               break;
+       }
 
-       if (context == NULL) {
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
-                                       "obj is NULL");
+       return FALSE;
+}
+/* LCOV_EXCL_STOP */
+
+EXPORT_API int widget_app_terminate_context(widget_context_h context)
+{
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
        }
 
+       if (context == NULL)
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, NULL);
+
        g_idle_add(__finish_event_cb, context);
        return WIDGET_ERROR_NONE;
 }
 
 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
 {
-       int r;
-       bool feature;
-
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
+       GList *contexts = _widget_app_get_contexts();
+       GList *list;
+       widget_context_s *wc;
 
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
-       if (cb == NULL)
+       if (!cb)
                return WIDGET_ERROR_INVALID_PARAMETER;
 
-       GList *iter = g_list_first(contexts);
-
-       while (iter != NULL) {
-               widget_context_s *cxt = (widget_context_s*) iter->data;
+       list = g_list_first(contexts);
 
-               if (!cb(cxt, data))
-                       return WIDGET_ERROR_CANCELED;
-
-               iter = g_list_next(iter);
+       while (list) {
+               wc = (widget_context_s *)list->data;
+               if (wc) {
+                       if (!cb(wc, data))
+                               break;
+               }
+               list = list->next;
        }
 
        return WIDGET_ERROR_NONE;
@@ -813,7 +1351,7 @@ EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
 
        handler = calloc(1, sizeof(struct app_event_handler));
        if (!handler)
-               return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
+               return widget_app_error(WIDGET_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL); /* LCOV_EXCL_LINE */
 
        if (g_list_length(handler_list[event_type]) == 0)
                __register_event(event_type);
@@ -859,124 +1397,157 @@ EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
        return WIDGET_ERROR_NONE;
 }
 
-EXPORT_API const charwidget_app_get_id(widget_context_h context)
+EXPORT_API const char *widget_app_get_id(widget_context_h context)
 {
-       int r;
-       bool feature;
-
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0) {
-               set_last_result(WIDGET_ERROR_FAULT);
-               return NULL;
-       }
-
-       if (!feature) {
-               set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
-               return NULL;
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
+               return NULL; /* LCOV_EXCL_LINE */
        }
 
-       if (context == NULL) {
+       if (!context) {
                set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
                return NULL;
        }
 
-       widget_context_s *cxt = (widget_context_s*)context;
-
        set_last_result(WIDGET_ERROR_NONE);
-       return cxt->id;
+       return context->id;
+}
+
+static void _win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
+{
+       /* Remove data used in accessibility */
+       char *plug_id;
+       plug_id = evas_object_data_del(obj, "___PLUGID");
+       free(plug_id);
 }
 
 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
                                        Evas_Object **win)
 {
-       int r;
-       bool feature;
+       widget_context_s *cxt = (widget_context_s *)context;
+       Evas_Object *ret_win;
+       Ecore_Wl_Window *wl_win;
+       struct wl_surface *surface;
+       char buffer[256];
+       int rots[3] = {0};
+
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
+       if (context == NULL || win == NULL)
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, NULL);
 
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
+       if (ret_win == NULL) {
+               _E("failed to create window"); /* LCOV_EXCL_LINE */
+               goto fault; /* LCOV_EXCL_LINE */
+       }
 
-       if (context == NULL || win == NULL)
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+       elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
+       elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
 
-       widget_context_s *cxt = (widget_context_s*)context;
-       Evas *evas;
-       Evas_Object *ret_win = NULL;
-
-       evas = widget_get_evas(cxt->id);
-       if (evas) {
-               Evas_Object *widget_parent;
-               widget_parent = evas_object_rectangle_add(evas);
-               if (widget_parent) {
-                       ret_win = elm_win_add(widget_parent, cxt->id, ELM_WIN_TIZEN_WIDGET);
-                       /* ret_win = elm_win_tizen_widget_add(cxt->id, widget_parent); */
-
-                       evas_object_del(widget_parent);
-                       if (ret_win == NULL) {
-                               _E("win is NULL");
-                               return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
-                       }
-               } else {
-                       _E("Failed to get parent widget");
-                       return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
-               }
-       } else {
-               _E("parent evas object is NULL");
-               return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
+       wl_win = elm_win_wl_window_get(ret_win);
+       if (wl_win == NULL) {
+               _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
+               goto fault;
        }
 
+       surface = ecore_wl_window_surface_get(wl_win);
+       if (surface == NULL) {
+               _E("failed to get surface"); /* LCOV_EXCL_LINE */
+               goto fault; /* LCOV_EXCL_LINE */
+       }
+       screen_connector_provider_remote_enable(cxt->id, surface);
+
+       ecore_wl_window_class_name_set(wl_win, cxt->id);
+       elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
+
        *win = ret_win;
+       cxt->win = ret_win;
+       cxt->win_id = ecore_wl_window_id_get(wl_win);
+
+       /* Set data to use in accessibility */
+       snprintf(buffer, sizeof(buffer), "%s:%d", cxt->id, getpid());
+       evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
+       evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, _win_del_cb, NULL);
+
+       _D("window created: %d", cxt->win_id);
+
        return WIDGET_ERROR_NONE;
+
+fault:
+       if (ret_win)    /* LCOV_EXCL_LINE */
+               evas_object_del(ret_win); /* LCOV_EXCL_LINE */
+
+       return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
+
 }
 
-EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
+widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
+               widget_instance_lifecycle_callback_s callback, void *user_data)
 {
-       int r;
-       bool feature;
+       widget_class_s *wc;
 
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0) {
-               set_last_result(WIDGET_ERROR_FAULT);
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               set_last_result(WIDGET_ERROR_NOT_SUPPORTED); /* LCOV_EXCL_LINE */
                return NULL;
        }
 
-       if (!feature) {
-               set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
+       if (class_id == NULL) {
+               set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
                return NULL;
        }
 
-       widget_class_s *wc = (widget_class_s*)malloc(sizeof(widget_class_s));
-
+       wc = (widget_class_s *)calloc(1, sizeof(widget_class_s));
        if (wc == NULL) {
-               _E("failed to malloc : %s", __FUNCTION__);
-               set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
-               return NULL;
+               _E("failed to calloc : %s", __FUNCTION__); /* LCOV_EXCL_LINE */
+               set_last_result(WIDGET_ERROR_OUT_OF_MEMORY); /* LCOV_EXCL_LINE */
+               return NULL; /* LCOV_EXCL_LINE */
        }
 
+       wc->classid = strdup(class_id);
        wc->user_data = user_data;
        wc->ops = callback;
-       wc->ops_private = widget_class_tmp.ops_private;
+       wc->next = prev;
+       wc->prev = NULL;
+
        set_last_result(WIDGET_ERROR_NONE);
+
+       if (prev)
+               prev->prev = wc;
+
        return wc;
 }
 
-EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
+EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class,
+               const char *class_id,
+               widget_instance_lifecycle_callback_s callback, void *user_data)
 {
-       int r;
-       bool feature;
+       return _widget_class_create(widget_class, class_id, callback,
+                       user_data);
+}
 
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
+EXPORT_API widget_class_h widget_app_class_create(
+               widget_instance_lifecycle_callback_s callback, void *user_data)
+{
+       return _widget_class_create(class_provider, appid, callback, user_data);
+}
 
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
+{
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
        if (context == NULL)
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, NULL);
 
        context->tag = tag;
 
@@ -985,72 +1556,83 @@ EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
 
 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
 {
-       int r;
-       bool feature;
-
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
-
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
        if (context == NULL || tag == NULL)
-               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, NULL);
 
        *tag = context->tag;
 
        return WIDGET_ERROR_NONE;
 }
 
-EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
+EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
+               bundle *content_info)
 {
-       int r;
-       bool feature;
+       const char *class_id = NULL;
+       int ret = 0;
+       bundle_raw *raw = NULL;
+       int len;
 
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       if (context == NULL || content_info == NULL)
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, NULL);
 
-       if (content_info == NULL)
-               return WIDGET_ERROR_INVALID_PARAMETER;
+       if (context->provider == NULL)
+               return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER,
+                               __FUNCTION__, NULL);
 
-       bundle_raw *raw = NULL;
-       int len;
-       int ret = WIDGET_ERROR_FAULT;
+       class_id = context->provider->classid;
+       if (class_id == NULL)
+               return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
+
+       ret = __send_update_status(class_id, context->id,
+                       WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
+
+       if (context->content)
+               free(context->content);
 
        bundle_encode(content_info, &raw, &len);
-       if (raw) {
-               ret = widget_provider_app_send_extra_info(context->id, (const char*)raw, NULL);
-               free(raw);
+       if (raw)
+               context->content = strdup((const char *)raw);
+       else
+               context->content = NULL;
+
+       free(raw);
+       if (ret < 0) {
+               /* LCOV_EXCL_START */
+               _E("failed to send content info: %s of %s (%d)", context->id,
+                               class_id, ret);
+               return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__,
+                               NULL);
+               /* LCOV_EXCL_STOP */
        }
 
-       return ret;
+       return WIDGET_ERROR_NONE;
 }
 
-EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
+EXPORT_API int widget_app_context_set_title(widget_context_h context,
+               const char *title)
 {
-       int r;
-       bool feature;
-
-       r = system_info_get_platform_bool(FEATURE_SHELL_APPWIDGET, &feature);
-       if (r < 0)
-               return WIDGET_ERROR_FAULT;
-
-       if (!feature)
-               return WIDGET_ERROR_NOT_SUPPORTED;
+       if (!_is_widget_feature_enabled()) {
+               _E("not supported"); /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NOT_SUPPORTED; /* LCOV_EXCL_LINE */
+       }
 
-       if (context == NULL)
+       if (!context || !title)
                return WIDGET_ERROR_INVALID_PARAMETER;
 
-       return widget_provider_app_send_extra_info(context->id, NULL, title);
-}
+       if (context->win)
+               elm_win_title_set(context->win, title);
 
-/* private API */
-EXPORT_API const widget_class_factory_full_s* widget_app_get_class_factory(void)
-{
-       return &factory;
+       return WIDGET_ERROR_NONE;
 }