Sync with the latest one
[platform/framework/web/livebox-viewer.git] / src / client.c
index 6d7d011..3a3266d 100644 (file)
@@ -31,6 +31,7 @@
 #include <com-core.h>
 #include <com-core_packet.h>
 #include <livebox-errno.h>
+#include <livebox-service.h>
 #include <secure_socket.h>
 
 #include "debug.h"
@@ -42,7 +43,6 @@
 #include "util.h"
 #include "master_rpc.h"
 #include "conf.h"
-#include "critical_log.h"
 #include "file_service.h"
 
 int errno;
@@ -185,7 +185,7 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                         * \note
                         * This is not possible
                         */
-                       CRITICAL_LOG("Already deleted handler (%s - %s)\n", pkgname, id);
+                       ErrPrint("Already deleted handler (%s - %s)\n", pkgname, id);
                        return NULL;
                }
        }
@@ -241,6 +241,12 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                }
        }
 
+       /*!
+        * \note
+        * Lock file should be deleted after all callbacks are processed.
+        */
+       lb_destroy_lock_file(handler, 0);
+
        /* Just try to delete it, if a user didn't remove it from the live box list */
        lb_unref(handler);
 
@@ -290,7 +296,7 @@ static struct packet *master_lb_update_begin(pid_t pid, int handle, const struct
        if (lb_get_lb_fb(handler)) {
                (void)lb_set_lb_fb(handler, fbfile);
 
-               ret = fb_sync(lb_get_lb_fb(handler));
+               ret = livebox_sync_lb_fb(handler);
                if (ret != LB_STATUS_SUCCESS) {
                        ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
                } else {
@@ -332,7 +338,7 @@ static struct packet *master_pd_update_begin(pid_t pid, int handle, const struct
        if (lb_get_pd_fb(handler)) {
                (void)lb_set_lb_fb(handler, fbfile);
 
-               ret = fb_sync(lb_get_lb_fb(handler));
+               ret = livebox_sync_lb_fb(handler);
                if (ret != LB_STATUS_SUCCESS) {
                        ErrPrint("Failed to do sync FB (%s - %s) (%d)\n", pkgname, fbfile, ret);
                } else {
@@ -380,6 +386,80 @@ out:
        return NULL;
 }
 
+static struct packet *master_key_status(pid_t pid, int handle, const struct packet *packet)
+{
+       struct livebox *handler;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       int status;
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &status);
+       if (ret != 3) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       handler = lb_find_livebox(pkgname, id);
+       if (!handler) {
+               ErrPrint("Instance[%s] is not exists\n", id);
+               goto out;
+       }
+
+       if (handler->state != CREATE) {
+               ErrPrint("[%s] is not created\n", id);
+               goto out;
+       }
+
+       if (handler->key_event_cb) {
+               ret_cb_t cb;
+               void *cbdata;
+
+               cb = handler->key_event_cb;
+               cbdata = handler->key_event_cbdata;
+
+               handler->key_event_cb = NULL;
+               handler->key_event_cbdata = NULL;
+
+               cb(handler, status, cbdata);
+       } else {
+               ErrPrint("Invalid event[%s]\n", id);
+       }
+out:
+       return NULL;
+}
+
+static struct packet *master_request_close_pd(pid_t pid, int handle, const struct packet *packet)
+{
+       struct livebox *handler;
+       const char *pkgname;
+       const char *id;
+       int ret;
+       int reason;
+
+       ret = packet_get(packet, "ssi", &pkgname, &id, &reason);
+       if (ret != 3) {
+               ErrPrint("Invalid argument\n");
+               goto out;
+       }
+
+       handler = lb_find_livebox(pkgname, id);
+       if (!handler) {
+               ErrPrint("Instance[%s] is not exists\n", id);
+               goto out;
+       }
+
+       if (handler->state != CREATE) {
+               ErrPrint("[%s] is not created\n", id);
+               goto out;
+       }
+
+       DbgPrint("Reason: %d\n", reason);
+       lb_invoke_event_handler(handler, LB_EVENT_REQUEST_CLOSE_PD);
+out:
+       return NULL;
+}
+
 static struct packet *master_access_status(pid_t pid, int handle, const struct packet *packet)
 {
        struct livebox *handler;
@@ -465,18 +545,20 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
        const char *content;
        const char *title;
        const char *safe_file;
+       const char *icon;
+       const char *name;
        struct livebox *handler;
        int lb_w;
        int lb_h;
        double priority;
        int ret;
 
-       ret = packet_get(packet, "sssiidsss",
+       ret = packet_get(packet, "sssiidsssss",
                                &pkgname, &id,
                                &fbfile, &lb_w, &lb_h,
                                &priority, &content, &title,
-                               &safe_file);
-       if (ret != 9) {
+                               &safe_file, &icon, &name);
+       if (ret != 11) {
                ErrPrint("Invalid argument\n");
                goto out;
        }
@@ -521,7 +603,7 @@ static struct packet *master_lb_updated(pid_t pid, int handle, const struct pack
                        (void)lb_set_lb_fb(handler, fbfile);
 
                        if (!conf_manual_sync()) {
-                               ret = fb_sync(lb_get_lb_fb(handler));
+                               ret = livebox_sync_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);
                                }
@@ -574,7 +656,7 @@ static struct packet *master_pd_created(pid_t pid, int handle, const struct pack
                DbgPrint("Text TYPE does not need to handle this\n");
        } else {
                (void)lb_set_pd_fb(handler, buf_id);
-               ret = fb_sync(lb_get_pd_fb(handler));
+               ret = livebox_sync_pd_fb(handler);
                if (ret < 0) {
                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
                }
@@ -582,6 +664,25 @@ static struct packet *master_pd_created(pid_t pid, int handle, const struct pack
 
        handler->is_pd_created = (status == 0);
 
+       switch (handler->pd.type) {
+       case _PD_TYPE_SCRIPT:
+       case _PD_TYPE_BUFFER:
+               switch (fb_type(lb_get_pd_fb(handler))) {
+               case BUFFER_TYPE_FILE:
+               case BUFFER_TYPE_SHM:
+                       lb_create_lock_file(handler, 1);
+                       break;
+               case BUFFER_TYPE_PIXMAP:
+               case BUFFER_TYPE_ERROR:
+               default:
+                       break;
+               }
+               break;
+       case _PD_TYPE_TEXT:
+       default:
+               break;
+       }
+
        if (handler->pd_created_cb) {
                ret_cb_t cb;
                void *cbdata;
@@ -654,6 +755,29 @@ static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct pa
                lb_invoke_event_handler(handler, LB_EVENT_PD_DESTROYED);
        }
 
+       /*!
+        * \note
+        * Lock file should be deleted after all callbacks are processed.
+        */
+       switch (handler->pd.type) {
+       case _PD_TYPE_SCRIPT:
+       case _PD_TYPE_BUFFER:
+               switch (fb_type(lb_get_pd_fb(handler))) {
+               case BUFFER_TYPE_FILE:
+               case BUFFER_TYPE_SHM:
+                       lb_destroy_lock_file(handler, 1);
+                       break;
+               case BUFFER_TYPE_PIXMAP:
+               case BUFFER_TYPE_ERROR:
+               default:
+                       break;
+               }
+               break;
+       case _PD_TYPE_TEXT:
+       default:
+               break;
+       }
+
 out:
        return NULL;
 }
@@ -707,7 +831,7 @@ static struct packet *master_pd_updated(pid_t pid, int handle, const struct pack
                        (void)lb_set_pd_fb(handler, fbfile);
 
                         if (!conf_manual_sync()) {
-                               ret = fb_sync(lb_get_pd_fb(handler));
+                               ret = livebox_sync_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 {
@@ -837,7 +961,7 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa
                        if (lb_get_lb_fb(handler)) {
                                (void)lb_set_lb_fb(handler, fbfile);
 
-                               ret = fb_sync(lb_get_lb_fb(handler));
+                               ret = livebox_sync_lb_fb(handler);
                                if (ret < 0) {
                                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
                                }
@@ -1105,7 +1229,24 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                }
                (void)lb_set_lb_fb(handler, lb_fname);
 
-               ret = fb_sync(lb_get_lb_fb(handler));
+               /*!
+                * \note
+                * Livebox should create the lock file from here.
+                * Even if the old_state == DELETE,
+                * the lock file will be deleted from deleted event callback.
+                */
+               switch (fb_type(lb_get_lb_fb(handler))) {
+               case BUFFER_TYPE_FILE:
+               case BUFFER_TYPE_SHM:
+                       lb_create_lock_file(handler, 0);
+                       break;
+               case BUFFER_TYPE_PIXMAP:
+               case BUFFER_TYPE_ERROR:
+               default:
+                       break;
+               }
+
+               ret = livebox_sync_lb_fb(handler);
                if (ret < 0) {
                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
                }
@@ -1128,10 +1269,17 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                }
 
                lb_set_pd_fb(handler, pd_fname);
-               ret = fb_sync(lb_get_pd_fb(handler));
+               ret = livebox_sync_pd_fb(handler);
                if (ret < 0) {
                        ErrPrint("Failed to do sync FB (%s - %s)\n", pkgname, util_basename(util_uri_to_path(id)));
                }
+
+               /*!
+                * \brief
+                * PD doesn't need to create the lock file from here.
+                * Just create it from PD_CREATED event.
+                */
+
                break;
        case _PD_TYPE_TEXT:
                lb_set_text_pd(handler);
@@ -1182,7 +1330,7 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
 
 out:
        if (ret == 0 && old_state == DELETE) {
-               lb_send_delete(handler, handler->created_cb, handler->created_cbdata);
+               lb_send_delete(handler, handler->delete_type, handler->created_cb, handler->created_cbdata);
 
                /*!
                 * \note
@@ -1276,6 +1424,14 @@ static struct method s_table[] = {
                .cmd = "access_status",
                .handler = master_access_status,
        },
+       {
+               .cmd = "key_status",
+               .handler = master_key_status,
+       },
+       {
+               .cmd = "close_pd",
+               .handler = master_request_close_pd,
+       },
 
        {
                .cmd = NULL,
@@ -1396,8 +1552,10 @@ static int disconnected_cb(int handle, void *data)
        return 0;
 }
 
-int client_init(void)
+int client_init(int use_thread)
 {
+       com_core_packet_use_thread(use_thread);
+
        s_info.client_addr = vconf_get_str(VCONFKEY_MASTER_CLIENT_ADDR);
        if (!s_info.client_addr) {
                s_info.client_addr = strdup(CLIENT_SOCKET);