add reference count for noti set/unset
authorYong Yeon Kim <yy9875.kim@samsung.com>
Wed, 27 Mar 2013 01:07:39 +0000 (10:07 +0900)
committerYong Yeon Kim <yy9875.kim@samsung.com>
Fri, 29 Mar 2013 05:27:58 +0000 (14:27 +0900)
Change-Id: I3c0ad737f9f117e34ef2ae77f4a88fec467f3e64

lib/media-util-noti.c
media-server.manifest
packaging/media-server.spec

index b9c1976..e1eafe4 100755 (executable)
@@ -44,6 +44,7 @@
 DBusConnection *g_bus;
 void *g_data_store;
 GMutex *noti_mutex = NULL;
+int ref_count;
 
 typedef struct noti_callback_data{
        db_update_cb user_callback;
@@ -141,6 +142,7 @@ __message_filter (DBusConnection *connection, DBusMessage *message, void *user_d
 
 int media_db_update_subscribe(db_update_cb user_cb, void *user_data)
 {
+       int ret = MS_MEDIA_ERR_NONE;
        DBusError error;
        noti_callback_data *callback_data = NULL;
 
@@ -151,6 +153,8 @@ int media_db_update_subscribe(db_update_cb user_cb, void *user_data)
                }
        }
 
+       g_mutex_lock(noti_mutex);
+
        if (g_bus == NULL) {
                dbus_g_thread_init();
 
@@ -160,9 +164,8 @@ int media_db_update_subscribe(db_update_cb user_cb, void *user_data)
                if (!g_bus) {
                        MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
                        dbus_error_free (&error);
-                       g_mutex_free(noti_mutex);
-                       noti_mutex = NULL;
-                       return MS_MEDIA_ERR_DBUS_GET;
+                       ret = MS_MEDIA_ERR_DBUS_GET;
+                       goto ERROR;
                }
 
                dbus_connection_setup_with_g_main (g_bus, NULL);
@@ -171,9 +174,8 @@ int media_db_update_subscribe(db_update_cb user_cb, void *user_data)
                MS_MALLOC(callback_data, sizeof(noti_callback_data));
                if (callback_data == NULL) {
                        MSAPI_DBG_ERR("MS_MALLOC failed");
-                       g_mutex_free(noti_mutex);
-                       noti_mutex = NULL;
-                       return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+                       ret = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+                       goto ERROR;
                }
                callback_data->user_callback = user_cb;
                callback_data->user_data = user_data;
@@ -181,15 +183,32 @@ int media_db_update_subscribe(db_update_cb user_cb, void *user_data)
                /* listening to messages from all objects as no path is specified */
                dbus_bus_add_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, &error);
                if( !dbus_connection_add_filter (g_bus, __message_filter, callback_data, __free_data_fuction)) {
-                       MS_SAFE_FREE(callback_data);
-                       g_mutex_free(noti_mutex);
-                       noti_mutex = NULL;
-                       return MS_MEDIA_ERR_DBUS_ADD_FILTER;
+                       dbus_bus_remove_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, NULL);
+                       ret =  MS_MEDIA_ERR_DBUS_ADD_FILTER;
+                       goto ERROR;
                }
                g_data_store = (void *)callback_data;
        }
 
+       ref_count ++;
+
+       g_mutex_unlock(noti_mutex);
+
        return MS_MEDIA_ERR_NONE;
+
+ERROR:
+
+       if (g_bus != NULL) {
+               dbus_connection_unref(g_bus);
+               g_bus = NULL;
+       }
+       MS_SAFE_FREE(callback_data);
+
+       g_mutex_unlock(noti_mutex);
+       g_mutex_free(noti_mutex);
+       noti_mutex = NULL;
+
+       return ret;
 }
 
 int media_db_update_unsubscribe(void)
@@ -200,15 +219,22 @@ int media_db_update_unsubscribe(void)
 
        g_mutex_lock(noti_mutex);
 
-       dbus_connection_remove_filter(g_bus, __message_filter, g_data_store);
-       dbus_connection_unref(g_bus);
+       if (ref_count == 1) {
+               dbus_connection_remove_filter(g_bus, __message_filter, g_data_store);
+               dbus_bus_remove_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, NULL);
+               dbus_connection_unref(g_bus);
 
-       g_bus = NULL;
+               g_bus = NULL;
+       }
+
+       ref_count --;
 
        g_mutex_unlock(noti_mutex);
 
-       g_mutex_free(noti_mutex);
-       noti_mutex = NULL;
+       if (ref_count == 0) {
+               g_mutex_free(noti_mutex);
+               noti_mutex = NULL;
+       }
 
        return MS_MEDIA_ERR_NONE;
 }
index 2dee8c7..9c68207 100644 (file)
@@ -1,6 +1,9 @@
 <manifest>
        <define>
                <domain name="media-server" policy="restricted" plist="media-thumbnail-server, media-data"/>
+               <request>
+                       <smack request="system::media" type="rwxat"/>
+               </request>
        </define>
        <request>
                <domain name="_"/>
index 7daf194..ecc4684 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       media-server
 Summary:    File manager service server.
-Version: 0.2.39
+Version: 0.2.41
 Release:    1
 Group:      utils
 License:    Apache License, Version 2.0