[Spice-devel] [PATCH spice-gtk] Remove g_object_{set, get}_data() usage

Marc-André Lureau marcandre.lureau at gmail.com
Mon Nov 7 06:01:09 PST 2011


This is not really clean, as it may conflict with client usage
---
 gtk/spice-audio.c        |    7 ++---
 gtk/spice-gtk-session.c  |   20 +++------------
 gtk/spice-session-priv.h |   58 ++++++++++++++++++++++++++++++++++++++++++++++
 gtk/spice-session.c      |   56 +++-----------------------------------------
 gtk/usb-device-manager.c |   19 ++------------
 5 files changed, 72 insertions(+), 88 deletions(-)

diff --git a/gtk/spice-audio.c b/gtk/spice-audio.c
index 3f4bb80..8a5cd19 100644
--- a/gtk/spice-audio.c
+++ b/gtk/spice-audio.c
@@ -40,6 +40,7 @@
 #include "spice-common.h"
 
 #include "spice-audio.h"
+#include "spice-session-priv.h"
 
 #ifdef WITH_PULSE
 #include "spice-pulse.h"
@@ -112,12 +113,10 @@ SpiceAudio *spice_audio_get(SpiceSession *session, GMainContext *context)
     SpiceAudio *self;
 
     g_static_mutex_lock(&mutex);
-    self = g_object_get_data(G_OBJECT(session), "spice-audio");
+    self = session->priv->audio_manager;
     if (self == NULL) {
         self = spice_audio_new(session, context, NULL);
-        g_object_set_data(G_OBJECT(session), "spice-audio", self);
-        if (self)
-            g_object_weak_ref(G_OBJECT(session), (GWeakNotify)g_object_unref, self);
+        session->priv->audio_manager = self;
     }
     g_static_mutex_unlock(&mutex);
 
diff --git a/gtk/spice-gtk-session.c b/gtk/spice-gtk-session.c
index 92461e0..116eead 100644
--- a/gtk/spice-gtk-session.c
+++ b/gtk/spice-gtk-session.c
@@ -21,6 +21,7 @@
 #include "spice-common.h"
 #include "spice-gtk-session.h"
 #include "spice-gtk-session-priv.h"
+#include "spice-session-priv.h"
 
 #define CLIPBOARD_LAST (VD_AGENT_CLIPBOARD_SELECTION_SECONDARY + 1)
 
@@ -311,15 +312,6 @@ static void spice_gtk_session_class_init(SpiceGtkSessionClass *klass)
     g_type_class_add_private(klass, sizeof(SpiceGtkSessionPrivate));
 }
 
-static void
-spice_gtk_session_spice_session_destroyed_cb(gpointer user_data,
-                                             GObject *object)
-{
-    SpiceGtkSession *self = user_data;
-
-    g_object_unref(self);
-}
-
 /* ---------------------------------------------------------------- */
 /* private functions (clipboard related)                            */
 
@@ -824,18 +816,14 @@ SpiceGtkSession *spice_gtk_session_get(SpiceSession *session)
 {
     g_return_val_if_fail(SPICE_IS_SESSION(session), NULL);
 
-    GObject *self;
+    SpiceGtkSession *self;
     static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
 
     g_static_mutex_lock(&mutex);
-    self = g_object_get_data(G_OBJECT(session), "spice-gtk-session");
+    self = session->priv->gtk_session;
     if (self == NULL) {
         self = g_object_new(SPICE_TYPE_GTK_SESSION, "session", session, NULL);
-        g_object_set_data(G_OBJECT(session), "spice-gtk-session", self);
-        /* Ensure we are destroyed together with the SpiceSession */
-        g_object_weak_ref(G_OBJECT(session),
-                          spice_gtk_session_spice_session_destroyed_cb,
-                          self);
+        session->priv->gtk_session = self;
     }
     g_static_mutex_unlock(&mutex);
 
diff --git a/gtk/spice-session-priv.h b/gtk/spice-session-priv.h
index 2815ed2..8f10407 100644
--- a/gtk/spice-session-priv.h
+++ b/gtk/spice-session-priv.h
@@ -21,11 +21,69 @@
 #include <glib.h>
 #include <gio/gio.h>
 #include "spice-session.h"
+#include "spice-gtk-session.h"
 #include "spice-channel-cache.h"
 #include "decode.h"
 
 G_BEGIN_DECLS
 
+struct _SpiceSessionPrivate {
+    char              *host;
+    char              *port;
+    char              *tls_port;
+    char              *password;
+    char              *ca_file;
+    char              *ciphers;
+    GByteArray        *pubkey;
+    char              *cert_subject;
+    guint             verify;
+
+    /* whether to enable audio */
+    gboolean          audio;
+
+    /* whether to enable smartcard event forwarding to the server */
+    gboolean          smartcard;
+
+    /* list of certificates to use for the software smartcard reader if
+     * enabled. For now, it has to contain exactly 3 certificates for
+     * the software reader to be functional
+     */
+    GStrv             smartcard_certificates;
+
+    /* path to the local certificate database to use to lookup the
+     * certificates stored in 'certificates'. If NULL, libcacard will
+     * fallback to using a default database.
+     */
+    char *            smartcard_db;
+
+    /* whether to enable USB redirection */
+    gboolean          usbredir;
+
+    GStrv             disable_effects;
+    gint              color_depth;
+
+    int               connection_id;
+    int               protocol;
+    SpiceChannel      *cmain; /* weak reference */
+    Ring              channels;
+    guint32           mm_time;
+    gboolean          client_provided_sockets;
+    guint64           mm_time_at_clock;
+    SpiceSession      *migration;
+    GList             *migration_left;
+    SpiceSessionMigration migration_state;
+    gboolean          disconnecting;
+
+    display_cache     images;
+    display_cache     palettes;
+    SpiceGlzDecoderWindow *glz_window;
+
+    /* associated objects */
+    SpiceAudio        *audio_manager;
+    SpiceGtkSession   *gtk_session;
+    SpiceUsbDeviceManager *usb_manager;
+};
+
 SpiceSession *spice_session_new_from_session(SpiceSession *session);
 
 void spice_session_set_connection_id(SpiceSession *session, int id);
diff --git a/gtk/spice-session.c b/gtk/spice-session.c
index 6884cb2..9e30c74 100644
--- a/gtk/spice-session.c
+++ b/gtk/spice-session.c
@@ -34,58 +34,6 @@ struct channel {
     RingItem          link;
 };
 
-struct _SpiceSessionPrivate {
-    char              *host;
-    char              *port;
-    char              *tls_port;
-    char              *password;
-    char              *ca_file;
-    char              *ciphers;
-    GByteArray        *pubkey;
-    char              *cert_subject;
-    guint             verify;
-
-    /* whether to enable audio */
-    gboolean          audio;
-
-    /* whether to enable smartcard event forwarding to the server */
-    gboolean          smartcard;
-
-    /* list of certificates to use for the software smartcard reader if
-     * enabled. For now, it has to contain exactly 3 certificates for
-     * the software reader to be functional
-     */
-    GStrv             smartcard_certificates;
-
-    /* path to the local certificate database to use to lookup the
-     * certificates stored in 'certificates'. If NULL, libcacard will
-     * fallback to using a default database.
-     */
-    char *            smartcard_db;
-
-    /* whether to enable USB redirection */
-    gboolean          usbredir;
-
-    GStrv             disable_effects;
-    gint              color_depth;
-
-    int               connection_id;
-    int               protocol;
-    SpiceChannel      *cmain; /* weak reference */
-    Ring              channels;
-    guint32           mm_time;
-    gboolean          client_provided_sockets;
-    guint64           mm_time_at_clock;
-    SpiceSession      *migration;
-    GList             *migration_left;
-    SpiceSessionMigration migration_state;
-    gboolean          disconnecting;
-
-    display_cache     images;
-    display_cache     palettes;
-    SpiceGlzDecoderWindow *glz_window;
-};
-
 /**
  * SECTION:spice-session
  * @short_description: handles connection details, and active channels
@@ -196,6 +144,10 @@ spice_session_dispose(GObject *gobject)
         s->migration_left = NULL;
     }
 
+    g_clear_object(&s->audio_manager);
+    g_clear_object(&s->gtk_session);
+    g_clear_object(&s->usb_manager);
+
     /* Chain up to the parent class */
     if (G_OBJECT_CLASS(spice_session_parent_class)->dispose)
         G_OBJECT_CLASS(spice_session_parent_class)->dispose(gobject);
diff --git a/gtk/usb-device-manager.c b/gtk/usb-device-manager.c
index 929e636..2fb3dbb 100644
--- a/gtk/usb-device-manager.c
+++ b/gtk/usb-device-manager.c
@@ -32,6 +32,7 @@
 #include "channel-usbredir-priv.h"
 #endif
 
+#include "spice-session-priv.h"
 #include "spice-client.h"
 #include "spice-marshal.h"
 
@@ -469,15 +470,6 @@ static void spice_usb_device_manager_dev_removed(GUsbDeviceList *devlist,
 }
 #endif
 
-static void
-spice_usb_device_manager_spice_session_destroyed_cb(gpointer user_data,
-                                                    GObject *object)
-{
-    SpiceUsbDeviceManager *self = user_data;
-
-    g_object_unref(self);
-}
-
 /* ------------------------------------------------------------------ */
 /* private api                                                        */
 static SpiceUsbredirChannel *spice_usb_device_manager_get_channel_for_dev(
@@ -524,17 +516,12 @@ SpiceUsbDeviceManager *spice_usb_device_manager_get(SpiceSession *session,
     g_return_val_if_fail(err == NULL || *err == NULL, NULL);
 
     g_static_mutex_lock(&mutex);
-    self = g_object_get_data(G_OBJECT(session), "spice-usb-device-manager");
+    self = session->priv->usb_manager;
     if (self == NULL) {
         self = g_initable_new(SPICE_TYPE_USB_DEVICE_MANAGER, NULL, err,
                               "session", session,
                               "main-context", main_context, NULL);
-        g_object_set_data(G_OBJECT(session), "spice-usb-device-manager", self);
-        if (self)
-            /* Ensure we are destroyed together with the SpiceSession */
-            g_object_weak_ref(G_OBJECT(session),
-                          spice_usb_device_manager_spice_session_destroyed_cb,
-                          self);
+        session->priv->usb_manager = self;
     }
     g_static_mutex_unlock(&mutex);
 
-- 
1.7.7



More information about the Spice-devel mailing list