Sync add_filter & match for dbus watch 55/15155/1
authorKrzysztof Sasiak <k.sasiak@samsung.com>
Fri, 9 Aug 2013 10:45:46 +0000 (12:45 +0200)
committerKrzysztof Sasiak <k.sasiak@samsung.com>
Thu, 16 Jan 2014 13:37:25 +0000 (14:37 +0100)
deviced added message filter unconditionally when it's starting.
Without adding dbus match, It's no checking logic.
Now Add dbus filter only when there is match request.

Change-Id: Icf1356d985f5cb9638afd7c82ac9ef67813c1ce1
Signed-off-by: Krzysztof Sasiak <k.sasiak@samsung.com>
src/core/edbus-handler.c

index 7546175..87c70d2 100644 (file)
@@ -242,46 +242,6 @@ int broadcast_edbus_signal(const char *path, const char *interface,
        return 0;
 }
 
-int register_edbus_watch(DBusMessage *msg)
-{
-       char match[256];
-       const char *sender, *watch;
-       Eina_List *l;
-
-       if (!msg) {
-               _E("invalid argument!");
-               return -EINVAL;
-       }
-
-       sender = dbus_message_get_sender(msg);
-       if (!sender) {
-               _E("invalid sender!");
-               return -EINVAL;
-       }
-
-       /* check the sender is already registered */
-       EINA_LIST_FOREACH(edbus_watch_list, l, watch) {
-               if (strcmp(sender, watch)) continue;
-
-               _I("%s is already watched!", watch);
-               return 0;
-       }
-
-       watch = strndup(sender, strlen(sender));
-       if (!watch) {
-               _E("Malloc failed");
-               return -ENOMEM;
-       }
-
-       /* Add sender to watch list */
-       EINA_LIST_APPEND(edbus_watch_list, watch);
-
-       snprintf(match, sizeof(match), NAME_OWNER_MATCH, watch);
-       dbus_bus_add_match(conn, match, NULL);
-
-       _I("%s is watched by dbus!", watch);
-}
-
 static DBusHandlerResult message_filter(DBusConnection *connection,
                DBusMessage *message, void *data)
 {
@@ -325,9 +285,66 @@ static DBusHandlerResult message_filter(DBusConnection *connection,
                break;
        }
 
+       if (eina_list_count(edbus_watch_list) == 0) {
+               dbus_connection_remove_filter(conn, message_filter, NULL);
+               _I("remove message filter, no watcher!");
+       }
+
        return DBUS_HANDLER_RESULT_HANDLED;
 }
 
+int register_edbus_watch(DBusMessage *msg)
+{
+       char match[256];
+       const char *sender, *watch;
+       Eina_List *l;
+       int ret;
+
+       if (!msg) {
+               _E("invalid argument!");
+               return -EINVAL;
+       }
+
+       sender = dbus_message_get_sender(msg);
+       if (!sender) {
+               _E("invalid sender!");
+               return -EINVAL;
+       }
+
+       /* check the sender is already registered */
+       EINA_LIST_FOREACH(edbus_watch_list, l, watch) {
+               if (strcmp(sender, watch)) continue;
+
+               _I("%s is already watched!", watch);
+               return 0;
+       }
+
+       watch = strndup(sender, strlen(sender));
+       if (!watch) {
+               _E("Malloc failed");
+               return -ENOMEM;
+       }
+
+       /* Add message filter */
+       if (eina_list_count(edbus_watch_list) == 0) {
+               ret = dbus_connection_add_filter(conn, message_filter, NULL, NULL);
+               if (!ret) {
+                       _E("fail to add message filter!");
+                       free(watch);
+                       return -ENOMEM;
+               }
+               _I("success to add message filter!");
+       }
+
+       /* Add sender to watch list */
+       EINA_LIST_APPEND(edbus_watch_list, watch);
+
+       snprintf(match, sizeof(match), NAME_OWNER_MATCH, watch);
+       dbus_bus_add_match(conn, match, NULL);
+
+       _I("%s is watched by dbus!", watch);
+}
+
 static unregister_edbus_watch_all(void)
 {
        char match[256];
@@ -387,8 +404,6 @@ static void edbus_init(void *data)
 
                _I("add new obj for %s", edbus_objects[i].interface);
        }
-
-       _D("start edbus service");
        return;
 
 err_dbus_close: