Do not automatically remove watches for service names
[framework/connectivity/connman.git] / gdbus / object.c
index 0786aeb..ff69641 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  D-Bus helper library
  *
- *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
+ *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
  *
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -45,9 +45,9 @@ struct generic_data {
 
 struct interface_data {
        char *name;
-       GDBusMethodTable *methods;
-       GDBusSignalTable *signals;
-       GDBusPropertyTable *properties;
+       const GDBusMethodTable *methods;
+       const GDBusSignalTable *signals;
+       const GDBusPropertyTable *properties;
        void *user_data;
        GDBusDestroyFunction destroy;
 };
@@ -114,8 +114,8 @@ static void print_arguments(GString *gstr, const char *sig,
 
 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
 {
-       GDBusMethodTable *method;
-       GDBusSignalTable *signal;
+       const GDBusMethodTable *method;
+       const GDBusSignalTable *signal;
 
        for (method = iface->methods; method && method->name; method++) {
                if (!strlen(method->signature) && !strlen(method->reply))
@@ -183,14 +183,15 @@ done:
        data->introspect = g_string_free(gstr, FALSE);
 }
 
-static DBusHandlerResult introspect(DBusConnection *connection,
-                               DBusMessage *message, struct generic_data *data)
+static DBusMessage *introspect(DBusConnection *connection,
+                               DBusMessage *message, void *user_data)
 {
+       struct generic_data *data = user_data;
        DBusMessage *reply;
 
        if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
                error("Unexpected signature to introspect call");
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+               return NULL;
        }
 
        if (!data->introspect)
@@ -199,16 +200,12 @@ static DBusHandlerResult introspect(DBusConnection *connection,
 
        reply = dbus_message_new_method_return(message);
        if (!reply)
-               return DBUS_HANDLER_RESULT_NEED_MEMORY;
+               return NULL;
 
        dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
                                        DBUS_TYPE_INVALID);
 
-       dbus_connection_send(connection, reply, NULL);
-
-       dbus_message_unref(reply);
-
-       return DBUS_HANDLER_RESULT_HANDLED;
+       return reply;
 }
 
 static void generic_unregister(DBusConnection *connection, void *user_data)
@@ -224,6 +221,9 @@ static struct interface_data *find_interface(GSList *interfaces,
 {
        GSList *list;
 
+       if (!name)
+               return NULL;
+
        for (list = interfaces; list; list = list->next) {
                struct interface_data *iface = list->data;
                if (!strcmp(name, iface->name))
@@ -238,14 +238,9 @@ static DBusHandlerResult generic_message(DBusConnection *connection,
 {
        struct generic_data *data = user_data;
        struct interface_data *iface;
-       GDBusMethodTable *method;
+       const GDBusMethodTable *method;
        const char *interface;
 
-       if (dbus_message_is_method_call(message,
-                                       DBUS_INTERFACE_INTROSPECTABLE,
-                                                               "Introspect"))
-               return introspect(connection, message, data);
-
        interface = dbus_message_get_interface(message);
 
        iface = find_interface(data->interfaces, interface);
@@ -304,13 +299,19 @@ static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
        if (!slash)
                goto done;
 
-       *slash = '\0';
+       if (slash == parent_path && parent_path[1] != '\0')
+               parent_path[1] = '\0';
+       else
+               *slash = '\0';
+
        if (!strlen(parent_path))
                goto done;
 
        if (!dbus_connection_get_object_path_data(conn, parent_path,
-                                                       (void *) &data))
+                                                       (void *) &data)) {
+               invalidate_parent_data(conn, parent_path);
                goto done;
+       }
 
        if (!data)
                goto done;
@@ -322,6 +323,31 @@ done:
        g_free(parent_path);
 }
 
+static GDBusMethodTable introspect_methods[] = {
+       { "Introspect", "",     "s", introspect },
+       { }
+};
+
+static void add_interface(struct generic_data *data, const char *name,
+                               const GDBusMethodTable *methods,
+                               const GDBusSignalTable *signals,
+                               const GDBusPropertyTable *properties,
+                               void *user_data,
+                               GDBusDestroyFunction destroy)
+{
+       struct interface_data *iface;
+
+       iface = g_new0(struct interface_data, 1);
+       iface->name = g_strdup(name);
+       iface->methods = methods;
+       iface->signals = signals;
+       iface->properties = properties;
+       iface->user_data = user_data;
+       iface->destroy = destroy;
+
+       data->interfaces = g_slist_append(data->interfaces, iface);
+}
+
 static struct generic_data *object_path_ref(DBusConnection *connection,
                                                        const char *path)
 {
@@ -350,9 +376,31 @@ static struct generic_data *object_path_ref(DBusConnection *connection,
 
        invalidate_parent_data(connection, path);
 
+       add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
+                       introspect_methods, NULL, NULL, data, NULL);
+
        return data;
 }
 
+static gboolean remove_interface(struct generic_data *data, const char *name)
+{
+       struct interface_data *iface;
+
+       iface = find_interface(data->interfaces, name);
+       if (!iface)
+               return FALSE;
+
+       data->interfaces = g_slist_remove(data->interfaces, iface);
+
+       if (iface->destroy)
+               iface->destroy(iface->user_data);
+
+       g_free(iface->name);
+       g_free(iface);
+
+       return TRUE;
+}
+
 static void object_path_unref(DBusConnection *connection, const char *path)
 {
        struct generic_data *data = NULL;
@@ -369,6 +417,8 @@ static void object_path_unref(DBusConnection *connection, const char *path)
        if (data->refcount > 0)
                return;
 
+       remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
+
        invalidate_parent_data(connection, path);
 
        dbus_connection_unregister_object_path(connection, path);
@@ -380,7 +430,7 @@ static gboolean check_signal(DBusConnection *conn, const char *path,
 {
        struct generic_data *data = NULL;
        struct interface_data *iface;
-       GDBusSignalTable *signal;
+       const GDBusSignalTable *signal;
 
        *args = NULL;
        if (!dbus_connection_get_object_path_data(conn, path,
@@ -391,7 +441,6 @@ static gboolean check_signal(DBusConnection *conn, const char *path,
        }
 
        iface = find_interface(data->interfaces, interface);
-
        if (!iface) {
                error("dbus_connection_emit_signal: %s does not implement %s",
                                path, interface);
@@ -455,32 +504,25 @@ fail:
 
 gboolean g_dbus_register_interface(DBusConnection *connection,
                                        const char *path, const char *name,
-                                       GDBusMethodTable *methods,
-                                       GDBusSignalTable *signals,
-                                       GDBusPropertyTable *properties,
+                                       const GDBusMethodTable *methods,
+                                       const GDBusSignalTable *signals,
+                                       const GDBusPropertyTable *properties,
                                        void *user_data,
                                        GDBusDestroyFunction destroy)
 {
        struct generic_data *data;
-       struct interface_data *iface;
 
        data = object_path_ref(connection, path);
        if (data == NULL)
                return FALSE;
 
-       if (find_interface(data->interfaces, name))
+       if (find_interface(data->interfaces, name)) {
+               object_path_unref(connection, path);
                return FALSE;
+       }
 
-       iface = g_new0(struct interface_data, 1);
-
-       iface->name = g_strdup(name);
-       iface->methods = methods;
-       iface->signals = signals;
-       iface->properties = properties;
-       iface->user_data = user_data;
-       iface->destroy = destroy;
-
-       data->interfaces = g_slist_append(data->interfaces, iface);
+       add_interface(data, name, methods, signals,
+                       properties, user_data, destroy);
 
        g_free(data->introspect);
        data->introspect = NULL;
@@ -492,7 +534,6 @@ gboolean g_dbus_unregister_interface(DBusConnection *connection,
                                        const char *path, const char *name)
 {
        struct generic_data *data = NULL;
-       struct interface_data *iface;
 
        if (!path)
                return FALSE;
@@ -504,18 +545,9 @@ gboolean g_dbus_unregister_interface(DBusConnection *connection,
        if (data == NULL)
                return FALSE;
 
-       iface = find_interface(data->interfaces, name);
-       if (!iface)
+       if (remove_interface(data, name) == FALSE)
                return FALSE;
 
-       data->interfaces = g_slist_remove(data->interfaces, iface);
-
-       if (iface->destroy)
-               iface->destroy(iface->user_data);
-
-       g_free(iface->name);
-       g_free(iface);
-
        g_free(data->introspect);
        data->introspect = NULL;