From 05bcb65e5b8d1ddc63f19ecc6802c312b7c54243 Mon Sep 17 00:00:00 2001 From: "estade@chromium.org" Date: Fri, 16 Apr 2010 21:10:32 +0000 Subject: GTK: Add a scoping class for g signals. BUG=40735 TEST=compile; trybots; manually opening and closing a lot of infobubbles Review URL: http://codereview.chromium.org/1652005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44821 0039d316-1c4b-4281-b951-d872f2087c98 --- app/gtk_signal.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'app/gtk_signal.h') diff --git a/app/gtk_signal.h b/app/gtk_signal.h index a3d5f7a..ffa487b 100644 --- a/app/gtk_signal.h +++ b/app/gtk_signal.h @@ -5,8 +5,11 @@ #ifndef APP_GTK_SIGNAL_H_ #define APP_GTK_SIGNAL_H_ -typedef void* gpointer; -typedef struct _GtkWidget GtkWidget; +#include +#include +#include + +#include "base/basictypes.h" // At the time of writing this, there were two common ways of binding our C++ // code to the gobject C system. We either defined a whole bunch of "static @@ -113,4 +116,52 @@ typedef struct _GtkWidget GtkWidget; CHROMEG_CALLBACK_6(CLASS, RETURN, METHOD, GtkWidget*, ARG1, ARG2, ARG3, \ ARG4, ARG5, ARG6); +// A class that ensures that callbacks don't run on stale owner objects. Similar +// in spirit to NotificationRegistrar. Use as follows: +// +// class ChromeObject { +// public: +// ChromeObject() { +// ... +// +// signals_.Connect(widget, "event", CallbackThunk, this); +// } +// +// ... +// +// private: +// GtkSignalRegistrar signals_; +// }; +// +// When |signals_| goes down, it will disconnect the handlers connected via +// Connect. +class GtkSignalRegistrar { + public: + GtkSignalRegistrar(); + ~GtkSignalRegistrar(); + + // Connect before the default handler. Returns the handler id. + glong Connect(gpointer instance, const gchar* detailed_signal, + GCallback signal_handler, gpointer data); + // Connect after the default handler. Returns the handler id. + glong ConnectAfter(gpointer instance, const gchar* detailed_signal, + GCallback signal_handler, gpointer data); + + private: + static void WeakNotifyThunk(gpointer data, GObject* where_the_object_was) { + reinterpret_cast(data)->WeakNotify( + where_the_object_was); + } + void WeakNotify(GObject* where_the_object_was); + + glong ConnectInternal(gpointer instance, const gchar* detailed_signal, + GCallback signal_handler, gpointer data, bool after); + + typedef std::vector HandlerList; + typedef std::map HandlerMap; + HandlerMap handler_lists_; + + DISALLOW_COPY_AND_ASSIGN(GtkSignalRegistrar); +}; + #endif // APP_GTK_SIGNAL_H_ -- cgit v1.1