If the box is deleted while creating, and the delete reason is SUCCESS, change it...
[platform/framework/web/livebox-viewer.git] / src / client.c
index 18cbbec..6d7d011 100644 (file)
@@ -19,6 +19,7 @@
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
+#include <pthread.h>
 
 #include <dlog.h>
 #include <glib.h>
@@ -30,6 +31,7 @@
 #include <com-core.h>
 #include <com-core_packet.h>
 #include <livebox-errno.h>
+#include <secure_socket.h>
 
 #include "debug.h"
 #include "client.h"
 #include "master_rpc.h"
 #include "conf.h"
 #include "critical_log.h"
+#include "file_service.h"
+
+int errno;
 
 static struct info {
        int fd;
        guint timer_id;
+       char *client_addr;
 } s_info = {
        .fd = -1,
        .timer_id = 0,
+       .client_addr = NULL,
 };
 
 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
@@ -63,7 +70,6 @@ static struct packet *master_fault_package(pid_t pid, int handle, const struct p
 
        master_rpc_clear_fault_package(pkgname);
        lb_invoke_fault_handler(LB_FAULT_DEACTIVATED, pkgname, id, function);
-       DbgPrint("%s(%s) is deactivated\n", pkgname, id);
        return NULL;
 }
 
@@ -87,7 +93,7 @@ static struct packet *master_hold_scroll(pid_t pid, int handle, const struct pac
                goto out;
        }
 
-       DbgPrint("[HOLD] %s %d\n", id, seize);
+       DbgPrint("HOLD: %s %d\n", id, seize);
        lb_invoke_event_handler(handler, seize ? LB_EVENT_HOLD_SCROLL : LB_EVENT_RELEASE_SCROLL);
 
 out:
@@ -155,8 +161,9 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
        const char *id;
        double timestamp;
        struct livebox *handler;
+       int reason;
 
-       if (packet_get(packet, "ssd", &pkgname, &id, &timestamp) != 3) {
+       if (packet_get(packet, "ssdi", &pkgname, &id, &timestamp, &reason) != 4) {
                ErrPrint("Invalid arguemnt\n");
                goto out;
        }
@@ -189,7 +196,7 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                /*!
                 * \note
                 *
-                * "if (handler->id == NULL)"
+                * "if (handler->id == NULL) {"
                 *
                 * The instance is not created yet.
                 * But the master forcely destroy it and send destroyed event to this
@@ -199,21 +206,24 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                 * before creating an instance successfully.
                 */
                if (handler->created_cb == handler->deleted_cb) {
-                       if (handler->created_cbdata != handler->deleted_cbdata)
+                       if (handler->created_cbdata != handler->deleted_cbdata) {
                                DbgPrint("cb is same but cbdata is different (%s - %s)\n", pkgname, id);
+                       }
 
                        handler->deleted_cb = NULL;
                        handler->deleted_cbdata = NULL;
                }
 
-               DbgPrint("Call the created cb with LB_STATUS_ERROR_CANCEL\n");
                cb = handler->created_cb;
                cbdata = handler->created_cbdata;
 
                handler->created_cb = NULL;
                handler->created_cbdata = NULL;
 
-               cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
+               if (reason == LB_STATUS_SUCCESS) {
+                       reason = LB_STATUS_ERROR_CANCEL;
+               }
+               cb(handler, reason, cbdata);
        } else if (handler->id) {
                if (handler->deleted_cb) {
                        ret_cb_t cb;
@@ -225,16 +235,12 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                        handler->deleted_cb = NULL;
                        handler->deleted_cbdata = NULL;
 
-                       DbgPrint("Call the deleted cb\n");
-                       cb(handler, LB_STATUS_SUCCESS, cbdata);
+                       cb(handler, reason, cbdata);
                } else {
-                       DbgPrint("Call the lb,deleted\n");
                        lb_invoke_event_handler(handler, LB_EVENT_DELETED);
                }
        }
 
-       DbgPrint("[%p] %s(%s) is deleted\n", handler, pkgname, id);
-
        /* Just try to delete it, if a user didn't remove it from the live box list */
        lb_unref(handler);
 
@@ -266,7 +272,7 @@ static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct
        }
 
        if (handler->state != CREATE) {
-               DbgPrint("(%s) is not created\n", id);
+               ErrPrint("(%s) is not created\n", id);
                goto out;
        }
 
@@ -282,13 +288,14 @@ static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct
         * And then begin updating again after change its size.
         */
        if (lb_get_lb_fb(handler)) {
-               lb_set_lb_fb(handler, fbfile);
+               (void)lb_set_lb_fb(handler, fbfile);
 
                ret = fb_sync(lb_get_lb_fb(handler));
-               if (ret != LB_STATUS_SUCCESS)
+               if (ret != LB_STATUS_SUCCESS) {
                        ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
-               else
+               } else {
                        lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATE_BEGIN);
+               }
        } else {
                ErrPrint("Invalid request[%s], %s\n", id, fbfile);
        }
@@ -318,18 +325,19 @@ static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct
        }
 
        if (handler->state != CREATE) {
-               DbgPrint("[%s] is not created\n", id);
+               ErrPrint("[%s] is not created\n", id);
                goto out;
        }
 
        if (lb_get_pd_fb(handler)) {
-               lb_set_lb_fb(handler, fbfile);
+               (void)lb_set_lb_fb(handler, fbfile);
 
                ret = fb_sync(lb_get_lb_fb(handler));
-               if (ret != LB_STATUS_SUCCESS)
+               if (ret != LB_STATUS_SUCCESS) {
                        ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
-               else
+               } else {
                        lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATE_BEGIN);
+               }
        } else {
                ErrPrint("Invalid request[%s], %s\n", id, fbfile);
        }
@@ -358,7 +366,7 @@ static struct packet *master_lb_update_end(pid_t pid, int handle, const struct p
        }
 
        if (handler->state != CREATE) {
-               DbgPrint("[%s] is not created\n", id);
+               ErrPrint("[%s] is not created\n", id);
                goto out;
        }
 
@@ -393,12 +401,10 @@ static struct packet *master_access_status(pid_t pid, int handle, const struct p
        }
 
        if (handler->state != CREATE) {
-               DbgPrint("[%s] is not created\n", id);
+               ErrPrint("[%s] is not created\n", id);
                goto out;
        }
 
-       DbgPrint("Access status: %d\n", status);
-
        if (handler->access_event_cb) {
                ret_cb_t cb;
                void *cbdata;
@@ -437,7 +443,7 @@ static struct packet *master_pd_update_end(pid_t pid, int handle, const struct p
        }
 
        if (handler->state != CREATE) {
-               DbgPrint("[%s] is not created\n", id);
+               ErrPrint("[%s] is not created\n", id);
                goto out;
        }
 
@@ -458,17 +464,19 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
        const char *fbfile;
        const char *content;
        const char *title;
+       const char *safe_file;
        struct livebox *handler;
        int lb_w;
        int lb_h;
        double priority;
        int ret;
 
-       ret = packet_get(packet, "sssiidss",
+       ret = packet_get(packet, "sssiidsss",
                                &pkgname, &id,
                                &fbfile, &lb_w, &lb_h,
-                               &priority, &content, &title);
-       if (ret != 8) {
+                               &priority, &content, &title,
+                               &safe_file);
+       if (ret != 9) {
                ErrPrint("Invalid argument\n");
                goto out;
        }
@@ -486,7 +494,7 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
                 * Don't try to notice anything with this, Just ignore all events
                 * Beacuse the user doesn't wants know about this anymore
                 */
-               DbgPrint("(%s) is not exists, but updated\n", id);
+               ErrPrint("(%s) is not exists, but updated\n", id);
                goto out;
        }
 
@@ -494,6 +502,7 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
        lb_set_content(handler, content);
        lb_set_title(handler, title);
        lb_set_size(handler, lb_w, lb_h);
+       lb_set_filename(handler, safe_file);
 
        if (lb_text_lb(handler)) {
                (void)parse_desc(handler, livebox_filename(handler), 0);
@@ -504,16 +513,29 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
                 */
                goto out;
        } else if (lb_get_lb_fb(handler)) {
-               lb_set_lb_fb(handler, fbfile);
-               ret = fb_sync(lb_get_lb_fb(handler));
-               if (ret != LB_STATUS_SUCCESS)
-                       ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
+               if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
+                       /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
+                       DbgPrint("Discards obsoloted update event\n");
+                       ret = LB_STATUS_ERROR_BUSY;
+               } else {
+                       (void)lb_set_lb_fb(handler, fbfile);
+
+                       if (!conf_manual_sync()) {
+                               ret = fb_sync(lb_get_lb_fb(handler));
+                               if (ret != LB_STATUS_SUCCESS) {
+                                       ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, util_basename(util_uri_to_path(id)), ret);
+                               }
+                       } else {
+                               ret = LB_STATUS_SUCCESS;
+                       }
+               }
        } else {
                ret = LB_STATUS_SUCCESS;
        }
 
-       if (ret == LB_STATUS_SUCCESS)
+       if (ret == LB_STATUS_SUCCESS) {
                lb_invoke_event_handler(handler, LB_EVENT_LB_UPDATED);
+       }
 
 out:
        return NULL;
@@ -553,8 +575,9 @@ static struct packet *master_pd_created(pid_t pid, int handle, const struct pack
        } else {
                (void)lb_set_pd_fb(handler, buf_id);
                ret = fb_sync(lb_get_pd_fb(handler));
-               if (ret < 0)
+               if (ret < 0) {
                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
+               }
        }
 
        handler->is_pd_created = (status == 0);
@@ -569,10 +592,14 @@ static struct packet *master_pd_created(pid_t pid, int handle, const struct pack
                handler->pd_created_cb = NULL;
                handler->pd_created_cbdata = NULL;
 
-               DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
+               /*!
+                * Before call the Callback function,
+                * pd_create_cb must be reset.
+                * Because, in the create callback, user can call create_pd function again.
+                */
+               DbgPrint("PERF_DBOX\n");
                cb(handler, status, cbdata);
-       } else if (status == 0) {
-               DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status);
+       } else if (handler->is_pd_created) {
                lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
        }
 
@@ -617,10 +644,13 @@ static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct pa
                handler->pd_destroyed_cb = NULL;
                handler->pd_destroyed_cbdata = NULL;
 
-               DbgPrint("Invoke the PD Destroyed CB\n");
+               /*!
+                * Before call the Callback function,
+                * pd_destroyed_cb must be reset.
+                * Because, in the create callback, user can call destroy_pd function again.
+                */
                cb(handler, status, cbdata);
        } else if (status == 0) {
-               DbgPrint("Invoke the LB_EVENT_PD_DESTROYED event\n");
                lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
        }
 
@@ -670,13 +700,23 @@ static struct packet *master_pd_updated(pid_t pid, int handle, const struct pack
        if (lb_text_pd(handler)) {
                (void)parse_desc(handler, descfile, 1);
        } else {
-               (void)lb_set_pd_fb(handler, fbfile);
-
-               ret = fb_sync(lb_get_pd_fb(handler));
-               if (ret < 0)
-                       ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
-               else
-                       lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
+               if (conf_frame_drop_for_resizing() && handler->size_changed_cb) {
+                       /* Just for skipping the update event callback call, After request to resize buffer, update event will be discarded */
+                       DbgPrint("Discards obsoloted update event\n");
+               } else {
+                       (void)lb_set_pd_fb(handler, fbfile);
+
+                        if (!conf_manual_sync()) {
+                               ret = fb_sync(lb_get_pd_fb(handler));
+                               if (ret < 0) {
+                                       ErrPrint("Failed to do sync FB (%s - %s), %d\n", pkgname, util_basename(util_uri_to_path(id)), ret);
+                               } else {
+                                       lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
+                               }
+                       } else {
+                               lb_invoke_event_handler(handler, LB_EVENT_PD_UPDATED);
+                       }
+               }
        }
 
 out:
@@ -703,8 +743,6 @@ static struct packet *master_update_mode(pid_t pid, int handle, const struct pac
                goto out;
        }
 
-       DbgPrint("Update mode is changed: %d, %d, %s\n", status, active_mode, id);
-
        handler = lb_find_livebox(pkgname, id);
        if (!handler) {
                ErrPrint("Livebox(%s) is not found\n", id);
@@ -716,8 +754,9 @@ static struct packet *master_update_mode(pid_t pid, int handle, const struct pac
                goto out;
        }
 
-       if (status == LB_STATUS_SUCCESS)
+       if (status == LB_STATUS_SUCCESS) {
                lb_set_update_mode(handler, active_mode);
+       }
 
        if (handler->update_mode_cb) {
                ret_cb_t cb;
@@ -761,8 +800,6 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa
                goto out;
        }
 
-       DbgPrint("Size is changed: %dx%d (%s), fb: [%s]\n", w, h, id, fbfile);
-
        handler = lb_find_livebox(pkgname, id);
        if (!handler) {
                ErrPrint("Livebox(%s) is not found\n", id);
@@ -775,7 +812,6 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa
        }
 
        if (is_pd) {
-               DbgPrint("PD is resized\n");
                /*!
                 * \NOTE
                 * PD is not able to resized by the client.
@@ -791,7 +827,6 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa
                }
        } else {
                if (status == 0) {
-                       DbgPrint("LB is successfully resized (%dx%d)\n", w, h);
                        lb_set_size(handler, w, h);
 
                        /*!
@@ -800,11 +835,12 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa
                         * Update it too.
                         */
                        if (lb_get_lb_fb(handler)) {
-                               lb_set_lb_fb(handler, fbfile);
+                               (void)lb_set_lb_fb(handler, fbfile);
 
                                ret = fb_sync(lb_get_lb_fb(handler));
-                               if (ret < 0)
+                               if (ret < 0) {
                                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
+                               }
 
                                /* Just update the size info only. */
                        }
@@ -829,7 +865,6 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa
                                lb_invoke_event_handler(handler, LB_EVENT_LB_SIZE_CHANGED);
                        }
                } else {
-                       DbgPrint("LB is not resized: %dx%d (%d)\n", w, h, status);
                        if (handler->size_changed_cb) {
                                ret_cb_t cb;
                                void *cbdata;
@@ -875,9 +910,9 @@ static struct packet *master_period_changed(pid_t pid, int handle, const struct
                goto out;
        }
 
-       DbgPrint("Update period is changed? %lf (%d)\n", period, status);
-       if (status == 0)
+       if (status == 0) {
                lb_set_period(handler, period);
+       }
 
        if (handler->period_changed_cb) {
                ret_cb_t cb;
@@ -930,9 +965,9 @@ static struct packet *master_group_changed(pid_t pid, int handle, const struct p
                goto out;
        }
 
-       DbgPrint("Group is changed? [%s] / [%s] (%d)\n", cluster, category, status);
-       if (status == 0)
+       if (status == 0) {
                (void)lb_set_group(handler, cluster, category);
+       }
 
        if (handler->group_changed_cb) {
                ret_cb_t cb;
@@ -1065,12 +1100,15 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                break;
        case _LB_TYPE_SCRIPT:
        case _LB_TYPE_BUFFER:
-               if (!strlen(lb_fname))
+               if (!strlen(lb_fname)) {
                        break;
-               lb_set_lb_fb(handler, lb_fname);
+               }
+               (void)lb_set_lb_fb(handler, lb_fname);
+
                ret = fb_sync(lb_get_lb_fb(handler));
-               if (ret < 0)
+               if (ret < 0) {
                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
+               }
                break;
        case _LB_TYPE_TEXT:
                lb_set_text_lb(handler);
@@ -1085,13 +1123,15 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
        switch (pd_type) {
        case _PD_TYPE_SCRIPT:
        case _PD_TYPE_BUFFER:
-               if (!strlen(pd_fname))
+               if (!strlen(pd_fname)) {
                        break;
+               }
 
                lb_set_pd_fb(handler, pd_fname);
                ret = fb_sync(lb_get_pd_fb(handler));
-               if (ret < 0)
+               if (ret < 0) {
                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
+               }
                break;
        case _PD_TYPE_TEXT:
                lb_set_text_pd(handler);
@@ -1134,17 +1174,14 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                        handler->created_cb = NULL;
                        handler->created_cbdata = NULL;
 
-                       DbgPrint("Invoke the created_cb\n");
                        cb(handler, ret, cbdata);
                } else {
-                       DbgPrint("Invoke the lb,created\n");
                        lb_invoke_event_handler(handler, LB_EVENT_CREATED);
                }
        }
 
 out:
        if (ret == 0 && old_state == DELETE) {
-               DbgPrint("Send the delete request\n");
                lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
 
                /*!
@@ -1253,10 +1290,11 @@ static void acquire_cb(struct livebox *handler, const struct packet *result, voi
        } else {
                int ret;
 
-               if (packet_get(result, "i", &ret) != 1)
+               if (packet_get(result, "i", &ret) != 1) {
                        ErrPrint("Invalid argument\n");
-               else
+               } else {
                        DbgPrint("Acquire returns: %d\n", ret);
+               }
        }
 
        return;
@@ -1269,7 +1307,7 @@ static inline int make_connection(void)
 
        DbgPrint("Let's making connection!\n");
 
-       s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
+       s_info.fd = com_core_packet_client_init(client_addr(), 0, s_table);
        if (s_info.fd < 0) {
                ErrPrint("Try this again later\n");
                return LB_STATUS_ERROR_IO;
@@ -1303,8 +1341,9 @@ static void master_started_cb(keynode_t *node, void *data)
 {
        int state = 0;
 
-       if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0)
+       if (vconf_get_bool(VCONFKEY_MASTER_STARTED, &state) < 0) {
                ErrPrint("Unable to get [%s]\n", VCONFKEY_MASTER_STARTED);
+       }
 
        DbgPrint("Master state: %d\n", state);
        if (state == 1 && make_connection() == LB_STATUS_SUCCESS) {
@@ -1316,10 +1355,11 @@ static void master_started_cb(keynode_t *node, void *data)
 
 static gboolean timeout_cb(gpointer data)
 {
-       if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
+       if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
                ErrPrint("Failed to add vconf for monitoring service state\n");
-       else
+       } else {
                DbgPrint("vconf event callback is registered\n");
+       }
 
        master_started_cb(NULL, NULL);
 
@@ -1358,12 +1398,26 @@ static int disconnected_cb(int handle, void *data)
 
 int client_init(void)
 {
+       s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
+       if (!s_info.client_addr) {
+               s_info.client_addr = strdup(CLIENT_SOCKET);
+               if (!s_info.client_addr) {
+                       ErrPrint("Heap: %s\n", strerror(errno));
+                       return -ENOMEM;
+               }
+       }
+
+       (void)file_service_init();
+
+       DbgPrint("Server Address: %s\n", s_info.client_addr);
+
        com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
        com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
-       if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
+       if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0) {
                ErrPrint("Failed to add vconf for service state\n");
-       else
+       } else {
                DbgPrint("vconf event callback is registered\n");
+       }
 
        master_started_cb(NULL, NULL);
        return 0;
@@ -1376,19 +1430,26 @@ int client_fd(void)
 
 const char *client_addr(void)
 {
-       return CLIENT_SOCKET;
+       return s_info.client_addr;
 }
 
 int client_fini(void)
 {
        int ret;
+
+       (void)file_service_fini();
+
        ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
-       if (ret < 0)
+       if (ret < 0) {
                DbgPrint("Ignore vconf key: %d\n", ret);
-       com_core_packet_client_fini(s_info.fd);
+       }
+
        com_core_del_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
        com_core_del_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
+       com_core_packet_client_fini(s_info.fd);
        s_info.fd = -1;
+       free(s_info.client_addr);
+       s_info.client_addr = NULL;
        return LB_STATUS_SUCCESS;
 }