Resolve prevent issues
[platform/framework/web/livebox-viewer.git] / src / client.c
index b9da06a..e4322bc 100644 (file)
@@ -5,7 +5,7 @@
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- * http://www.tizenopensource.org/license
+ * http://floralicense.org/license/
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,6 +23,9 @@
 #include <dlog.h>
 #include <glib.h>
 
+#include <vconf.h>
+#include <vconf-keys.h>
+
 #include <packet.h>
 #include <com-core.h>
 #include <com-core_packet.h>
 #include "conf.h"
 #include "critical_log.h"
 
-static inline void make_connection(void);
-
 static struct info {
        int fd;
-       guint reconnector;
 } s_info = {
        .fd = -1,
-       .reconnector = 0,
 };
 
 static struct packet *master_fault_package(pid_t pid, int handle, const struct packet *packet)
@@ -101,9 +100,10 @@ static struct packet *master_pinup(pid_t pid, int handle, const struct packet *p
        struct livebox *handler;
        char *new_content;
        int ret;
+       int status;
        int pinup;
 
-       ret = packet_get(packet, "iisss", &ret, &pinup, &pkgname, &id, &content);
+       ret = packet_get(packet, "iisss", &status, &pinup, &pkgname, &id, &content);
        if (ret != 5) {
                ErrPrint("Invalid argument\n");
                goto out;
@@ -115,7 +115,7 @@ static struct packet *master_pinup(pid_t pid, int handle, const struct packet *p
                goto out;
        }
 
-       if (ret == 0) {
+       if (status == 0) {
                new_content = strdup(content);
                if (new_content) {
                        free(handler->content);
@@ -123,16 +123,23 @@ static struct packet *master_pinup(pid_t pid, int handle, const struct packet *p
                        handler->is_pinned_up = pinup;
                } else {
                        ErrPrint("Heap: %s\n", strerror(errno));
-                       ret = LB_STATUS_ERROR_MEMORY;
+                       status = LB_STATUS_ERROR_MEMORY;
                }
        }
 
        if (handler->pinup_cb) {
-               handler->pinup_cb(handler, ret, handler->pinup_cbdata);
+               ret_cb_t cb;
+               void *cbdata;
+
+               /* Make sure that user can call pinup API in its result callback */
+               cb = handler->pinup_cb;
+               cbdata = handler->pinup_cbdata;
 
-               handler->pinup_cb = NULL; /*!< Reset pinup cb */
+               handler->pinup_cb = NULL;
                handler->pinup_cbdata = NULL;
-       } else if (ret == 0) {
+
+               cb(handler, status, cbdata);
+       } else if (status == 0) {
                lb_invoke_event_handler(handler, LB_EVENT_PINUP_CHANGED);
        }
 
@@ -175,6 +182,8 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
        }
 
        if (handler->created_cb) {
+               ret_cb_t cb;
+               void *cbdata;
                /*!
                 * \note
                 *
@@ -196,16 +205,26 @@ static struct packet *master_deleted(pid_t pid, int handle, const struct packet
                }
 
                DbgPrint("Call the created cb with LB_STATUS_ERROR_CANCEL\n");
-               handler->created_cb(handler, LB_STATUS_ERROR_CANCEL, handler->created_cbdata);
+               cb = handler->created_cb;
+               cbdata = handler->created_cbdata;
+
                handler->created_cb = NULL;
                handler->created_cbdata = NULL;
+
+               cb(handler, LB_STATUS_ERROR_CANCEL, cbdata);
        } else if (handler->id) {
                if (handler->deleted_cb) {
-                       DbgPrint("Call the deleted cb\n");
-                       handler->deleted_cb(handler, LB_STATUS_SUCCESS, handler->deleted_cbdata);
+                       ret_cb_t cb;
+                       void *cbdata;
+
+                       cb = handler->deleted_cb;
+                       cbdata = handler->deleted_cbdata;
 
                        handler->deleted_cb = NULL;
                        handler->deleted_cbdata = NULL;
+
+                       DbgPrint("Call the deleted cb\n");
+                       cb(handler, LB_STATUS_SUCCESS, cbdata);
                } else {
                        DbgPrint("Call the lb,deleted\n");
                        lb_invoke_event_handler(handler, LB_EVENT_DELETED);
@@ -379,9 +398,16 @@ static struct packet *master_access_status(pid_t pid, int handle, const struct p
        DbgPrint("Access status: %d\n", status);
 
        if (handler->access_event_cb) {
-               handler->access_event_cb(handler, status, handler->access_event_cbdata);
+               ret_cb_t cb;
+               void *cbdata;
+
+               cb = handler->access_event_cb;
+               cbdata = handler->access_event_cbdata;
+
                handler->access_event_cb = NULL;
                handler->access_event_cbdata = NULL;
+
+               cb(handler, status, cbdata);
        } else {
                ErrPrint("Invalid event[%s]\n", id);
        }
@@ -532,11 +558,17 @@ static struct packet *master_pd_created(pid_t pid, int handle, const struct pack
        handler->is_pd_created = (status == 0);
 
        if (handler->pd_created_cb) {
-               DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
-               handler->pd_created_cb(handler, status, handler->pd_created_cbdata);
+               ret_cb_t cb;
+               void *cbdata;
+
+               cb = handler->pd_created_cb;
+               cbdata = handler->pd_created_cbdata;
 
                handler->pd_created_cb = NULL;
                handler->pd_created_cbdata = NULL;
+
+               DbgPrint("pd_created_cb (%s) - %d\n", buf_id, status);
+               cb(handler, status, cbdata);
        } else if (status == 0) {
                DbgPrint("LB_EVENT_PD_CREATED (%s) - %d\n", buf_id, status);
                lb_invoke_event_handler(handler, LB_EVENT_PD_CREATED);
@@ -574,11 +606,17 @@ static struct packet *master_pd_destroyed(pid_t pid, int handle, const struct pa
        handler->is_pd_created = 0;
 
        if (handler->pd_destroyed_cb) {
-               DbgPrint("Invoke the PD Destroyed CB\n");
-               handler->pd_destroyed_cb(handler, status, handler->pd_destroyed_cbdata);
+               ret_cb_t cb;
+               void *cbdata;
+
+               cb = handler->pd_destroyed_cb;
+               cbdata = handler->pd_destroyed_cbdata;
 
                handler->pd_destroyed_cb = NULL;
                handler->pd_destroyed_cbdata = NULL;
+
+               DbgPrint("Invoke the PD Destroyed CB\n");
+               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);
@@ -680,10 +718,16 @@ static struct packet *master_update_mode(pid_t pid, int handle, const struct pac
                lb_set_update_mode(handler, active_mode);
 
        if (handler->update_mode_cb) {
-               handler->update_mode_cb(handler, status, handler->update_mode_cbdata);
+               ret_cb_t cb;
+               void *cbdata;
+
+               cb = handler->update_mode_cb;
+               cbdata = handler->update_mode_cbdata;
 
                handler->update_mode_cb = NULL;
                handler->update_mode_cbdata = NULL;
+
+               cb(handler, status, cbdata);
        } else {
                lb_invoke_event_handler(handler, LB_EVENT_UPDATE_MODE_CHANGED);
        }
@@ -769,20 +813,32 @@ static struct packet *master_size_changed(pid_t pid, int handle, const struct pa
                         * So I added some log before & after call the user callback.
                         */
                        if (handler->size_changed_cb) {
-                               handler->size_changed_cb(handler, status, handler->size_cbdata);
+                               ret_cb_t cb;
+                               void *cbdata;
+
+                               cb = handler->size_changed_cb;
+                               cbdata = handler->size_cbdata;
 
                                handler->size_changed_cb = NULL;
                                handler->size_cbdata = NULL;
+
+                               cb(handler, status, cbdata);
                        } else {
                                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) {
-                               handler->size_changed_cb(handler, status, handler->size_cbdata);
+                               ret_cb_t cb;
+                               void *cbdata;
+
+                               cb = handler->size_changed_cb;
+                               cbdata = handler->size_cbdata;
 
                                handler->size_changed_cb = NULL;
                                handler->size_cbdata = NULL;
+
+                               cb(handler, status, cbdata);
                        }
                }
        }
@@ -822,10 +878,16 @@ static struct packet *master_period_changed(pid_t pid, int handle, const struct
                lb_set_period(handler, period);
 
        if (handler->period_changed_cb) {
-               handler->period_changed_cb(handler, status, handler->group_cbdata);
+               ret_cb_t cb;
+               void *cbdata;
+
+               cb = handler->period_changed_cb;
+               cbdata = handler->period_cbdata;
 
                handler->period_changed_cb = NULL;
                handler->period_cbdata = NULL;
+
+               cb(handler, status, cbdata);
        } else if (status == 0) {
                lb_invoke_event_handler(handler, LB_EVENT_PERIOD_CHANGED);
        }
@@ -871,10 +933,16 @@ static struct packet *master_group_changed(pid_t pid, int handle, const struct p
                (void)lb_set_group(handler, cluster, category);
 
        if (handler->group_changed_cb) {
-               handler->group_changed_cb(handler, status, handler->group_cbdata);
+               ret_cb_t cb;
+               void *cbdata;
+
+               cb = handler->group_changed_cb;
+               cbdata = handler->group_cbdata;
 
                handler->group_changed_cb = NULL;
                handler->group_cbdata = NULL;
+
+               cb(handler, status, cbdata);
        } else if (status == 0) {
                lb_invoke_event_handler(handler, LB_EVENT_GROUP_CHANGED);
        }
@@ -1055,11 +1123,17 @@ static struct packet *master_created(pid_t pid, int handle, const struct packet
                 */
 
                if (handler->created_cb) {
-                       DbgPrint("Invoke the created_cb\n");
-                       handler->created_cb(handler, ret, handler->created_cbdata);
+                       ret_cb_t cb;
+                       void *cbdata;
+
+                       cb = handler->created_cb;
+                       cbdata = handler->created_cbdata;
 
                        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);
@@ -1186,20 +1260,7 @@ static void acquire_cb(struct livebox *handler, const struct packet *result, voi
        return;
 }
 
-static gboolean connector_cb(gpointer user_data)
-{
-       s_info.reconnector = 0;
-
-       if (s_info.fd > 0) {
-               DbgPrint("Connection is already made\n");
-               return FALSE;
-       }
-
-       make_connection();
-       return FALSE;
-}
-
-static inline void make_connection(void)
+static inline int make_connection(void)
 {
        struct packet *packet;
        int ret;
@@ -1208,20 +1269,15 @@ static inline void make_connection(void)
 
        s_info.fd = com_core_packet_client_init(CLIENT_SOCKET, 0, s_table);
        if (s_info.fd < 0) {
-               /*!< After 10 secs later, try to connect again */
-               s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
-               if (s_info.reconnector == 0)
-                       ErrPrint("Failed to fire the reconnector\n");
-
-               ErrPrint("Try this again A sec later\n");
-               return;
+               ErrPrint("Try this again later\n");
+               return LB_STATUS_ERROR_IO;
        }
 
        packet = packet_create("acquire", "d", util_timestamp());
        if (!packet) {
                com_core_packet_client_fini(s_info.fd);
                s_info.fd = -1;
-               return;
+               return LB_STATUS_ERROR_FAULT;
        }
 
        ret = master_rpc_async_request(NULL, packet, 1, acquire_cb, NULL);
@@ -1229,8 +1285,10 @@ static inline void make_connection(void)
                ErrPrint("Master RPC returns %d\n", ret);
                com_core_packet_client_fini(s_info.fd);
                s_info.fd = -1;
+               return LB_STATUS_ERROR_IO;
        }
 
+       return LB_STATUS_SUCCESS;
 }
 
 static int connected_cb(int handle, void *data)
@@ -1239,6 +1297,21 @@ static int connected_cb(int handle, void *data)
        return 0;
 }
 
+static void master_started_cb(keynode_t *node, void *data)
+{
+       int 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) {
+               int ret;
+               ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
+               DbgPrint("master_started vconf key de-registered [%d]\n", ret);
+       }
+}
+
 static int disconnected_cb(int handle, void *data)
 {
        if (s_info.fd != handle) {
@@ -1248,22 +1321,17 @@ static int disconnected_cb(int handle, void *data)
 
        s_info.fd = -1; /*!< Disconnected */
 
-       if (s_info.reconnector > 0) {
-               DbgPrint("Reconnector already fired\n");
-               return 0;
-       }
-
-       /*!< After 10 secs later, try to connect again */
-       s_info.reconnector = g_timeout_add(RECONNECT_PERIOD, connector_cb, NULL);
-       if (s_info.reconnector == 0) {
-               ErrPrint("Failed to fire the reconnector\n");
-               make_connection();
-       }
-
        master_rpc_clear_all_request();
        lb_invoke_fault_handler(LB_FAULT_PROVIDER_DISCONNECTED, MASTER_PKGNAME, "default", "disconnected");
 
        lb_delete_all();
+
+       if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
+               ErrPrint("Failed to add vconf for monitoring service state\n");
+       else
+               DbgPrint("vconf event callback is registered\n");
+
+       master_started_cb(NULL, NULL);
        return 0;
 }
 
@@ -1271,7 +1339,12 @@ int client_init(void)
 {
        com_core_add_event_callback(CONNECTOR_DISCONNECTED, disconnected_cb, NULL);
        com_core_add_event_callback(CONNECTOR_CONNECTED, connected_cb, NULL);
-       make_connection();
+       if (vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb, NULL) < 0)
+               ErrPrint("Failed to add vconf for service state\n");
+       else
+               DbgPrint("vconf event callback is registered\n");
+
+       master_started_cb(NULL, NULL);
        return 0;
 }
 
@@ -1287,11 +1360,15 @@ const char *client_addr(void)
 
 int client_fini(void)
 {
+       int ret;
+       ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, master_started_cb);
+       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);
        s_info.fd = -1;
-       return 0;
+       return LB_STATUS_SUCCESS;
 }
 
 /* End of a file */