Commit 5d77616b authored by Zeeshan Ali's avatar Zeeshan Ali

Improved service instance initialization

Do D-Bus registration/export at construction time by implementing
GInitable interface and keeping D-Bus connection as a construct-only
property.

This rids of the seperate registration/export methods.
parent 5461fd60
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#define BUS_NAME "org.freedesktop.GeoClue2" #define BUS_NAME "org.freedesktop.GeoClue2"
GClueServiceManager *manager; GClueServiceManager *manager = NULL;
static void static void
on_bus_acquired (GDBusConnection *connection, on_bus_acquired (GDBusConnection *connection,
...@@ -37,7 +37,8 @@ on_bus_acquired (GDBusConnection *connection, ...@@ -37,7 +37,8 @@ on_bus_acquired (GDBusConnection *connection,
{ {
GError *error = NULL; GError *error = NULL;
if (!gclue_service_manager_export (manager, connection, &error)) { manager = gclue_service_manager_new (connection, &error);
if (error != NULL) {
g_critical ("Failed to register server: %s", error->message); g_critical ("Failed to register server: %s", error->message);
g_error_free (&error); g_error_free (&error);
...@@ -65,8 +66,6 @@ main (int argc, char **argv) ...@@ -65,8 +66,6 @@ main (int argc, char **argv)
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
g_set_application_name (_("GeoClue")); g_set_application_name (_("GeoClue"));
manager = gclue_service_manager_new ();
owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
BUS_NAME, BUS_NAME,
G_BUS_NAME_OWNER_FLAGS_NONE, G_BUS_NAME_OWNER_FLAGS_NONE,
...@@ -79,7 +78,8 @@ main (int argc, char **argv) ...@@ -79,7 +78,8 @@ main (int argc, char **argv)
main_loop = g_main_loop_new (NULL, FALSE); main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop); g_main_loop_run (main_loop);
g_object_unref (manager); if (manager != NULL);
g_object_unref (manager);
g_bus_unown_name (owner_id); g_bus_unown_name (owner_id);
g_main_loop_unref (main_loop); g_main_loop_unref (main_loop);
......
...@@ -24,23 +24,29 @@ ...@@ -24,23 +24,29 @@
#include "gclue-service-client.h" #include "gclue-service-client.h"
static void static void
gclue_client_init (GClueClientIface *iface); gclue_service_client_client_iface_init (GClueClientIface *iface);
static void
gclue_service_client_initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GClueServiceClient, G_DEFINE_TYPE_WITH_CODE (GClueServiceClient,
gclue_service_client, gclue_service_client,
GCLUE_TYPE_CLIENT_SKELETON, GCLUE_TYPE_CLIENT_SKELETON,
G_IMPLEMENT_INTERFACE (GCLUE_TYPE_CLIENT, G_IMPLEMENT_INTERFACE (GCLUE_TYPE_CLIENT,
gclue_client_init)) gclue_service_client_client_iface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
gclue_service_client_initable_iface_init));
struct _GClueServiceClientPrivate struct _GClueServiceClientPrivate
{ {
const char *path; const char *path;
GDBusConnection *connection;
}; };
enum enum
{ {
PROP_0, PROP_0,
PROP_PATH, PROP_PATH,
PROP_CONNECTION,
LAST_PROP LAST_PROP
}; };
...@@ -75,6 +81,7 @@ gclue_service_client_finalize (GObject *object) ...@@ -75,6 +81,7 @@ gclue_service_client_finalize (GObject *object)
g_free (priv->path); g_free (priv->path);
priv->path = NULL; priv->path = NULL;
} }
g_clear_object (&priv->connection);
/* Chain up to the parent class */ /* Chain up to the parent class */
G_OBJECT_CLASS (gclue_service_client_parent_class)->finalize (object); G_OBJECT_CLASS (gclue_service_client_parent_class)->finalize (object);
...@@ -93,6 +100,10 @@ gclue_service_client_get_property (GObject *object, ...@@ -93,6 +100,10 @@ gclue_service_client_get_property (GObject *object,
g_value_set_string (value, client->priv->path); g_value_set_string (value, client->priv->path);
break; break;
case PROP_CONNECTION:
g_value_set_object (value, client->priv->connection);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
} }
...@@ -111,6 +122,10 @@ gclue_service_client_set_property (GObject *object, ...@@ -111,6 +122,10 @@ gclue_service_client_set_property (GObject *object,
client->priv->path = g_value_dup_string (value); client->priv->path = g_value_dup_string (value);
break; break;
case PROP_CONNECTION:
client->priv->connection = g_value_dup_object (value);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
} }
...@@ -137,15 +152,48 @@ gclue_service_client_class_init (GClueServiceClientClass *klass) ...@@ -137,15 +152,48 @@ gclue_service_client_class_init (GClueServiceClientClass *klass)
g_object_class_install_property (object_class, g_object_class_install_property (object_class,
PROP_PATH, PROP_PATH,
gParamSpecs[PROP_PATH]); gParamSpecs[PROP_PATH]);
gParamSpecs[PROP_CONNECTION] = g_param_spec_object ("connection",
"Connection",
"DBus Connection",
G_TYPE_DBUS_CONNECTION,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_property (object_class,
PROP_CONNECTION,
gParamSpecs[PROP_CONNECTION]);
} }
static void static void
gclue_client_init (GClueClientIface *iface) gclue_service_client_client_iface_init (GClueClientIface *iface)
{ {
iface->handle_start = gclue_service_client_handle_start; iface->handle_start = gclue_service_client_handle_start;
iface->handle_stop = gclue_service_client_handle_stop; iface->handle_stop = gclue_service_client_handle_stop;
} }
static gboolean
gclue_service_client_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
GDBusInterfaceSkeleton *skeleton = G_DBUS_INTERFACE_SKELETON (initable);
return (g_dbus_connection_register_object
(GCLUE_SERVICE_CLIENT (initable)->priv->connection,
GCLUE_SERVICE_CLIENT (initable)->priv->path,
g_dbus_interface_skeleton_get_info (skeleton),
g_dbus_interface_skeleton_get_vtable (skeleton),
initable,
NULL,
error) != 0);
}
static void
gclue_service_client_initable_iface_init (GInitableIface *iface)
{
iface->init = gclue_service_client_initable_init;
}
static void static void
gclue_service_client_init (GClueServiceClient *client) gclue_service_client_init (GClueServiceClient *client)
{ {
...@@ -155,26 +203,16 @@ gclue_service_client_init (GClueServiceClient *client) ...@@ -155,26 +203,16 @@ gclue_service_client_init (GClueServiceClient *client)
} }
GClueServiceClient * GClueServiceClient *
gclue_service_client_new (const char *path) gclue_service_client_new (const char *path,
GDBusConnection *connection,
GError **error)
{ {
return g_object_new (GClUE_TYPE_SERVICE_CLIENT, return g_initable_new (GClUE_TYPE_SERVICE_CLIENT,
"path", path, NULL,
NULL); error,
} "path", path,
"connection", connection,
gboolean NULL);
gclue_service_client_register (GClueServiceClient *client,
GDBusConnection *connection,
GError **error)
{
return (g_dbus_connection_register_object
(connection,
gclue_service_client_get_path (client),
g_dbus_interface_skeleton_get_info (client),
g_dbus_interface_skeleton_get_vtable (client),
client,
NULL,
error) != 0);
} }
const gchar * const gchar *
......
...@@ -54,10 +54,9 @@ struct _GClueServiceClientClass ...@@ -54,10 +54,9 @@ struct _GClueServiceClientClass
GType gclue_service_client_get_type (void) G_GNUC_CONST; GType gclue_service_client_get_type (void) G_GNUC_CONST;
GClueServiceClient * gclue_service_client_new (const char *path); GClueServiceClient * gclue_service_client_new (const char *path,
gboolean gclue_service_client_register (GClueServiceClient *client, GDBusConnection *connection,
GDBusConnection *connection, GError **error);
GError **error);
const char * gclue_service_client_get_path (GClueServiceClient *client); const char * gclue_service_client_get_path (GClueServiceClient *client);
G_END_DECLS G_END_DECLS
......
...@@ -25,22 +25,28 @@ ...@@ -25,22 +25,28 @@
#include "gclue-service-client.h" #include "gclue-service-client.h"
static void static void
gclue_manager_init (GClueManagerIface *iface); gclue_service_manager_manager_iface_init (GClueManagerIface *iface);
static void
gclue_service_manager_initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (GClueServiceManager, G_DEFINE_TYPE_WITH_CODE (GClueServiceManager,
gclue_service_manager, gclue_service_manager,
GCLUE_TYPE_MANAGER_SKELETON, GCLUE_TYPE_MANAGER_SKELETON,
G_IMPLEMENT_INTERFACE (GCLUE_TYPE_MANAGER, G_IMPLEMENT_INTERFACE (GCLUE_TYPE_MANAGER,
gclue_manager_init)) gclue_service_manager_manager_iface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
gclue_service_manager_initable_iface_init))
struct _GClueServiceManagerPrivate struct _GClueServiceManagerPrivate
{ {
GDBusConnection *connection;
GHashTable *clients; GHashTable *clients;
}; };
enum enum
{ {
PROP_0, PROP_0,
PROP_CONNECTION,
LAST_PROP LAST_PROP
}; };
...@@ -89,11 +95,8 @@ gclue_service_manager_handle_get_client (GClueServiceManager *manager, ...@@ -89,11 +95,8 @@ gclue_service_manager_handle_get_client (GClueServiceManager *manager,
return TRUE; return TRUE;
} }
client = gclue_service_client_new (path); client = gclue_service_client_new (path, priv->connection, &error);
if (!gclue_service_client_register if (error != NULL) {
(client,
g_dbus_method_invocation_get_connection (invocation),
&error)) {
g_dbus_method_invocation_return_dbus_error (invocation, g_dbus_method_invocation_return_dbus_error (invocation,
"Object registration failure", "Object registration failure",
error->message); error->message);
...@@ -108,14 +111,63 @@ gclue_service_manager_handle_get_client (GClueServiceManager *manager, ...@@ -108,14 +111,63 @@ gclue_service_manager_handle_get_client (GClueServiceManager *manager,
return TRUE; return TRUE;
} }
static void
gclue_service_manager_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GClueServiceManager *manager = GCLUE_SERVICE_MANAGER (object);
switch (prop_id) {
case PROP_CONNECTION:
g_value_set_object (value, manager->priv->connection);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gclue_service_manager_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GClueServiceManager *manager = GCLUE_SERVICE_MANAGER (object);
switch (prop_id) {
case PROP_CONNECTION:
manager->priv->connection = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void static void
gclue_service_manager_class_init (GClueServiceManagerClass *klass) gclue_service_manager_class_init (GClueServiceManagerClass *klass)
{ {
GObjectClass *object_class; GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass); object_class = G_OBJECT_CLASS (klass);
object_class->get_property = gclue_service_manager_get_property;
object_class->set_property = gclue_service_manager_set_property;
g_type_class_add_private (object_class, sizeof (GClueServiceManagerPrivate)); g_type_class_add_private (object_class, sizeof (GClueServiceManagerPrivate));
gParamSpecs[PROP_CONNECTION] = g_param_spec_object ("connection",
"Connection",
"DBus Connection",
G_TYPE_DBUS_CONNECTION,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_property (object_class,
PROP_CONNECTION,
gParamSpecs[PROP_CONNECTION]);
} }
static void static void
...@@ -131,25 +183,37 @@ gclue_service_manager_init (GClueServiceManager *manager) ...@@ -131,25 +183,37 @@ gclue_service_manager_init (GClueServiceManager *manager)
g_object_unref); g_object_unref);
} }
static gboolean
gclue_service_manager_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
return g_dbus_interface_skeleton_export
(G_DBUS_INTERFACE_SKELETON (initable),
GCLUE_SERVICE_MANAGER (initable)->priv->connection,
"/org/freedesktop/GeoClue2/Manager",
error);
}
static void static void
gclue_manager_init (GClueManagerIface *iface) gclue_service_manager_manager_iface_init (GClueManagerIface *iface)
{ {
iface->handle_get_client = gclue_service_manager_handle_get_client; iface->handle_get_client = gclue_service_manager_handle_get_client;
} }
GClueServiceManager * static void
gclue_service_manager_new (void) gclue_service_manager_initable_iface_init (GInitableIface *iface)
{ {
return g_object_new (GClUE_TYPE_SERVICE_MANAGER, NULL); iface->init = gclue_service_manager_initable_init;
} }
gboolean GClueServiceManager *
gclue_service_manager_export (GClueServiceManager *manager, gclue_service_manager_new (GDBusConnection *connection,
GDBusConnection *connection, GError **error)
GError **error)
{ {
return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (manager), return g_initable_new (GClUE_TYPE_SERVICE_MANAGER,
connection, NULL,
"/org/freedesktop/GeoClue2/Manager", error,
error); "connection", connection,
NULL);
} }
...@@ -54,10 +54,8 @@ struct _GClueServiceManagerClass ...@@ -54,10 +54,8 @@ struct _GClueServiceManagerClass
GType gclue_service_manager_get_type (void) G_GNUC_CONST; GType gclue_service_manager_get_type (void) G_GNUC_CONST;
GClueServiceManager * gclue_service_manager_new (void); GClueServiceManager * gclue_service_manager_new (GDBusConnection *connection,
gboolean gclue_service_manager_export (GClueServiceManager *manager, GError **error);
GDBusConnection *connection,
GError **error);
G_END_DECLS G_END_DECLS
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment