Find the internal API using dlsym.
[platform/framework/web/livebox.git] / src / livebox.c
index f6a6732..60bc528 100644 (file)
@@ -20,6 +20,8 @@
 #include <string.h> /* strdup */
 #include <libgen.h>
 #include <unistd.h> /* access */
+#define __USE_GNU
+#include <dlfcn.h>
 
 #include <dlog.h>
 #include <livebox-service.h>
 /*!
  * \brief This function is defined by the data-provider-slave
  */
-extern const char *livebox_find_pkgname(const char *filename);
-extern int livebox_request_update_by_id(const char *uri);
-extern int livebox_trigger_update_monitor(const char *id, int is_pd);
+static struct info {
+       const char *(*find_pkgname)(const char *filename);
+       int (*request_update_by_id)(const char *uri);
+       int (*trigger_update_monitor)(const char *id, int is_pd);
+} s_info = {
+       .find_pkgname = NULL,
+       .request_update_by_id = NULL,
+       .trigger_update_monitor = NULL,
+};
 
 struct block {
        unsigned int idx;
@@ -141,30 +149,37 @@ PUBLIC int livebox_desc_close(struct livebox_desc *handle)
        struct dlist *n;
        struct block *block;
 
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        dlist_foreach_safe(handle->block_list, l, n, block) {
                handle->block_list = dlist_remove(handle->block_list, l);
 
                fprintf(handle->fp, "{\n");
-               if (block->type)
+               if (block->type) {
                        fprintf(handle->fp, "type=%s\n", block->type);
+               }
 
-               if (block->part)
+               if (block->part) {
                        fprintf(handle->fp, "part=%s\n", block->part);
+               }
 
-               if (block->data)
+               if (block->data) {
                        fprintf(handle->fp, "data=%s\n", block->data);
+               }
 
-               if (block->option)
+               if (block->option) {
                        fprintf(handle->fp, "option=%s\n", block->option);
+               }
 
-               if (block->id)
+               if (block->id) {
                        fprintf(handle->fp, "id=%s\n", block->id);
+               }
 
-               if (block->target_id)
+               if (block->target_id) {
                        fprintf(handle->fp, "target=%s\n", block->target_id);
+               }
                fprintf(handle->fp, "}\n");
 
                free(block->type);
@@ -176,7 +191,9 @@ PUBLIC int livebox_desc_close(struct livebox_desc *handle)
                free(block);
        }
 
-       fclose(handle->fp);
+       if (fclose(handle->fp) != 0) {
+               ErrPrint("fclose: %s\n", strerror(errno));
+       }
        free(handle);
        return LB_STATUS_SUCCESS;
 }
@@ -185,12 +202,14 @@ PUBLIC int livebox_desc_set_category(struct livebox_desc *handle, const char *id
 {
        struct block *block;
 
-       if (!handle || !category)
+       if (!handle || !category) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        block = calloc(1, sizeof(*block));
-       if (!block)
+       if (!block) {
                return LB_STATUS_ERROR_MEMORY;
+       }
 
        block->type = strdup(LB_DESC_TYPE_INFO);
        if (!block->type) {
@@ -234,12 +253,14 @@ PUBLIC int livebox_desc_set_size(struct livebox_desc *handle, const char *id, in
        struct block *block;
        char buffer[BUFSIZ];
 
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        block = calloc(1, sizeof(*block));
-       if (!block)
+       if (!block) {
                return LB_STATUS_ERROR_MEMORY;
+       }
 
        block->type = strdup(LB_DESC_TYPE_INFO);
        if (!block->type) {
@@ -285,16 +306,19 @@ PUBLIC char *livebox_util_nl2br(const char *str)
        char *ret;
        char *ptr;
 
-       if (!str)
+       if (!str) {
                return NULL;
+       }
 
        len = strlen(str);
-       if (!len)
+       if (!len) {
                return NULL;
+       }
 
        ret = malloc(len + 1);
-       if (!ret)
+       if (!ret) {
                return NULL;
+       }
 
        ptr = ret;
        i = 0;
@@ -347,8 +371,9 @@ PUBLIC int livebox_desc_set_id(struct livebox_desc *handle, int idx, const char
                        free(block->target_id);
                        block->target_id = NULL;
 
-                       if (!id || !strlen(id))
+                       if (!id || !strlen(id)) {
                                return LB_STATUS_SUCCESS;
+                       }
 
                        block->target_id = strdup(id);
                        if (!block->target_id) {
@@ -370,14 +395,17 @@ PUBLIC int livebox_desc_add_block(struct livebox_desc *handle, const char *id, c
 {
        struct block *block;
 
-       if (!handle || !type)
+       if (!handle || !type) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
-       if (!part)
+       if (!part) {
                part = "";
+       }
 
-       if (!data)
+       if (!data) {
                data = "";
+       }
 
        block = calloc(1, sizeof(*block));
        if (!block) {
@@ -473,7 +501,16 @@ static inline int event_handler_wrapper(struct livebox_buffer *buffer, enum buff
        int ret;
 
        pkgname = provider_buffer_pkgname(buffer);
-       id = provider_buffer_pkgname(buffer);
+       if (!pkgname) {
+               ErrPrint("pkgname is not valid\n");
+               return LB_STATUS_ERROR_INVALID;
+       }
+
+       id = provider_buffer_id(buffer);
+       if (!id) {
+               ErrPrint("id is not valid[%s]\n", pkgname);
+               return LB_STATUS_ERROR_INVALID;
+       }
 
        ret = cbdata->handler(buffer, event, timestamp, x, y, cbdata->cbdata);
 
@@ -488,10 +525,12 @@ static inline int event_handler_wrapper(struct livebox_buffer *buffer, enum buff
        case BUFFER_EVENT_SCROLL_MOVE:
        case BUFFER_EVENT_SCROLL_DOWN:
        case BUFFER_EVENT_UNHIGHLIGHT:
-               if (ret < 0)
+               DbgPrint("Accessibility event: %d\n", event);
+               if (ret < 0) {
                        (void)provider_send_access_status(pkgname, id, LB_ACCESS_STATUS_ERROR);
-               else
+               } else {
                        (void)provider_send_access_status(pkgname, id, ret);
+               }
                break;
        default:
                break;
@@ -513,7 +552,6 @@ PUBLIC struct livebox_buffer *livebox_acquire_buffer(const char *filename, int i
        struct livebox_buffer *handle;
        char *uri;
        int uri_len;
-       struct event_cbdata *cbdata;
 
        if (!filename || !width || !height) {
                ErrPrint("Invalid argument: %p(%dx%d)\n", filename, width, height);
@@ -539,7 +577,17 @@ PUBLIC struct livebox_buffer *livebox_acquire_buffer(const char *filename, int i
        }
 
        snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
-       pkgname = livebox_find_pkgname(uri);
+       if (!s_info.find_pkgname) {
+               s_info.find_pkgname = dlsym(RTLD_DEFAULT, "livebox_find_pkgname");
+               if (!s_info.find_pkgname) {
+                       ErrPrint("Failed to find a \"livebox_find_pkgname\"\n");
+                       free(user_data);
+                       free(uri);
+                       return NULL;
+               }
+       }
+
+       pkgname = s_info.find_pkgname(uri);
        if (!pkgname) {
                ErrPrint("Invalid Request\n");
                free(user_data);
@@ -578,7 +626,15 @@ PUBLIC int livebox_request_update(const char *filename)
        }
 
        snprintf(uri, uri_len, FILE_SCHEMA "%s", filename);
-       ret = livebox_request_update_by_id(uri);
+       if (!s_info.request_update_by_id) {
+               s_info.request_update_by_id = dlsym(RTLD_DEFAULT, "livebox_request_update_by_id");
+               if (!s_info.request_update_by_id) {
+                       ErrPrint("\"livebox_request_update_by_id\" is not exists\n");
+                       free(uri);
+                       return LB_STATUS_ERROR_FAULT;
+               }
+       }
+       ret = s_info.request_update_by_id(uri);
        free(uri);
        return ret;
 }
@@ -592,8 +648,9 @@ PUBLIC int livebox_release_buffer(struct livebox_buffer *handle)
 {
        struct livebox_buffer_data *user_data;
 
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        user_data = provider_buffer_user_data(handle);
        if (user_data) {
@@ -612,12 +669,14 @@ PUBLIC void *livebox_ref_buffer(struct livebox_buffer *handle)
        int w, h, size;
        int ret;
 
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        user_data = provider_buffer_user_data(handle);
-       if (!user_data)
+       if (!user_data) {
                return NULL;
+       }
 
        if (user_data->accelerated) {
                DbgPrint("H/W accelerated buffer is allocated\n");
@@ -638,8 +697,9 @@ PUBLIC void *livebox_ref_buffer(struct livebox_buffer *handle)
 
 PUBLIC int livebox_unref_buffer(void *buffer)
 {
-       if (!buffer)
+       if (!buffer) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        DbgPrint("Unref buffer\n");
        return provider_buffer_unref(buffer);
@@ -651,8 +711,9 @@ PUBLIC int livebox_sync_buffer(struct livebox_buffer *handle)
        const char *pkgname;
        const char *id;
 
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        user_data = provider_buffer_user_data(handle);
        if (!user_data) {
@@ -680,18 +741,21 @@ PUBLIC int livebox_sync_buffer(struct livebox_buffer *handle)
        provider_buffer_sync(handle);
 
        if (user_data->is_pd) {
-               if (provider_send_desc_updated(pkgname, id, NULL) < 0)
+               if (provider_send_desc_updated(pkgname, id, NULL) < 0) {
                        ErrPrint("Failed to send PD updated (%s)\n", id);
+               }
        } else {
                int w;
                int h;
                int pixel_size;
 
-               if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
+               if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0) {
                        ErrPrint("Failed to get size (%s)\n", id);
+               }
 
-               if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
+               if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0) {
                        ErrPrint("Failed to send updated (%s)\n", id);
+               }
        }
 
        return LB_STATUS_SUCCESS;
@@ -699,8 +763,9 @@ PUBLIC int livebox_sync_buffer(struct livebox_buffer *handle)
 
 PUBLIC int livebox_support_hw_buffer(struct livebox_buffer *handle)
 {
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        return provider_buffer_pixmap_is_support_hw(handle);
 }
@@ -710,15 +775,18 @@ PUBLIC int livebox_create_hw_buffer(struct livebox_buffer *handle)
        struct livebox_buffer_data *user_data;
        int ret;
 
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        user_data = provider_buffer_user_data(handle);
-       if (!user_data)
+       if (!user_data) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
-       if (user_data->accelerated)
-               return -EALREADY;
+       if (user_data->accelerated) {
+               return LB_STATUS_ERROR_ALREADY;
+       }
 
        ret = provider_buffer_pixmap_create_hw(handle);
        user_data->accelerated = (ret == 0);
@@ -728,12 +796,14 @@ PUBLIC int livebox_create_hw_buffer(struct livebox_buffer *handle)
 PUBLIC int livebox_destroy_hw_buffer(struct livebox_buffer *handle)
 {
        struct livebox_buffer_data *user_data;
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        user_data = provider_buffer_user_data(handle);
-       if (!user_data || !user_data->accelerated)
+       if (!user_data || !user_data->accelerated) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        user_data->accelerated = 0;
 
@@ -744,12 +814,14 @@ PUBLIC void *livebox_buffer_hw_buffer(struct livebox_buffer *handle)
 {
        struct livebox_buffer_data *user_data;
 
-       if (!handle)
+       if (!handle) {
                return NULL;
+       }
 
        user_data = provider_buffer_user_data(handle);
-       if (!user_data || !user_data->accelerated)
+       if (!user_data || !user_data->accelerated) {
                return NULL;
+       }
 
        return provider_buffer_pixmap_hw_addr(handle);
 }
@@ -758,15 +830,18 @@ PUBLIC int livebox_buffer_pre_render(struct livebox_buffer *handle)
 {
        struct livebox_buffer_data *user_data;
 
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        user_data = provider_buffer_user_data(handle);
-       if (!user_data)
+       if (!user_data) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
-       if (!user_data->accelerated)
+       if (!user_data->accelerated) {
                return LB_STATUS_SUCCESS;
+       }
 
        /*!
         * \note
@@ -782,15 +857,18 @@ PUBLIC int livebox_buffer_post_render(struct livebox_buffer *handle)
        const char *id;
        struct livebox_buffer_data *user_data;
 
-       if (!handle)
+       if (!handle) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
        user_data = provider_buffer_user_data(handle);
-       if (!user_data)
+       if (!user_data) {
                return LB_STATUS_ERROR_INVALID;
+       }
 
-       if (!user_data->accelerated)
+       if (!user_data->accelerated) {
                return LB_STATUS_SUCCESS;
+       }
 
        pkgname = provider_buffer_pkgname(handle);
        if (!pkgname) {
@@ -811,18 +889,21 @@ PUBLIC int livebox_buffer_post_render(struct livebox_buffer *handle)
        }
 
        if (user_data->is_pd == 1) {
-               if (provider_send_desc_updated(pkgname, id, NULL) < 0)
+               if (provider_send_desc_updated(pkgname, id, NULL) < 0) {
                        ErrPrint("Failed to send PD updated (%s)\n", id);
+               }
        } else {
                int w;
                int h;
                int pixel_size;
 
-               if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0)
+               if (provider_buffer_get_size(handle, &w, &h, &pixel_size) < 0) {
                        ErrPrint("Failed to get size (%s)\n", id);
+               }
 
-               if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0)
+               if (provider_send_updated(pkgname, id, w, h, -1.0f, NULL, NULL) < 0) {
                        ErrPrint("Failed to send updated (%s)\n", id);
+               }
        }
 
        return LB_STATUS_SUCCESS;
@@ -830,7 +911,15 @@ PUBLIC int livebox_buffer_post_render(struct livebox_buffer *handle)
 
 PUBLIC int livebox_content_is_updated(const char *filename, int is_pd)
 {
-       return livebox_trigger_update_monitor(filename, is_pd);
+       if (!s_info.trigger_update_monitor) {
+               s_info.trigger_update_monitor = dlsym(RTLD_DEFAULT, "livebox_trigger_update_monitor");
+               if (!s_info.trigger_update_monitor) {
+                       ErrPrint("Trigger update monitor function is not exists\n");
+                       return LB_STATUS_ERROR_FAULT;
+               }
+       }
+
+       return s_info.trigger_update_monitor(filename, is_pd);
 }
 
 /* End of a file */