summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-16 21:10:32 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-16 21:10:32 +0000
commit05bcb65e5b8d1ddc63f19ecc6802c312b7c54243 (patch)
treead5e5d269de770fd3d2e2f083b6f080f66eae778 /app
parentb69e649feb5dc5f7daa6daa84c98b3a2d9a28124 (diff)
downloadchromium_src-05bcb65e5b8d1ddc63f19ecc6802c312b7c54243.zip
chromium_src-05bcb65e5b8d1ddc63f19ecc6802c312b7c54243.tar.gz
chromium_src-05bcb65e5b8d1ddc63f19ecc6802c312b7c54243.tar.bz2
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
Diffstat (limited to 'app')
-rw-r--r--app/app_base.gypi2
-rw-r--r--app/gtk_signal.cc72
-rw-r--r--app/gtk_signal.h55
3 files changed, 127 insertions, 2 deletions
diff --git a/app/app_base.gypi b/app/app_base.gypi
index 2eaa8f6..802a0d2 100644
--- a/app/app_base.gypi
+++ b/app/app_base.gypi
@@ -47,6 +47,7 @@
'sources!': [
'gtk_dnd_util.cc',
'gtk_dnd_util.h',
+ 'gtk_signal.cc',
'gtk_signal.h',
'gtk_util.cc',
'gtk_util.h',
@@ -118,6 +119,7 @@
'gfx/font_util.cc',
'gtk_dnd_util.cc',
'gtk_dnd_util.h',
+ 'gtk_signal.cc',
'gtk_signal.h',
'gtk_util.cc',
'gtk_util.h',
diff --git a/app/gtk_signal.cc b/app/gtk_signal.cc
new file mode 100644
index 0000000..4db611d
--- /dev/null
+++ b/app/gtk_signal.cc
@@ -0,0 +1,72 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "app/gtk_signal.h"
+
+#include "base/logging.h"
+
+GtkSignalRegistrar::GtkSignalRegistrar() {
+}
+
+GtkSignalRegistrar::~GtkSignalRegistrar() {
+ for (HandlerMap::iterator list_iter = handler_lists_.begin();
+ list_iter != handler_lists_.end(); ++list_iter) {
+ GObject* object = list_iter->first;
+ g_object_weak_unref(object, WeakNotifyThunk, this);
+
+ HandlerList& handlers = list_iter->second;
+ for (HandlerList::iterator ids_iter = handlers.begin();
+ ids_iter != handlers.end(); ids_iter++) {
+ g_signal_handler_disconnect(list_iter->first, *ids_iter);
+ }
+ }
+}
+
+glong GtkSignalRegistrar::Connect(gpointer instance,
+ const gchar* detailed_signal,
+ GCallback signal_handler,
+ gpointer data) {
+ return ConnectInternal(instance, detailed_signal, signal_handler, data,
+ false);
+}
+
+glong GtkSignalRegistrar::ConnectAfter(gpointer instance,
+ const gchar* detailed_signal,
+ GCallback signal_handler,
+ gpointer data) {
+ return ConnectInternal(instance, detailed_signal, signal_handler, data, true);
+}
+
+glong GtkSignalRegistrar::ConnectInternal(gpointer instance,
+ const gchar* detailed_signal,
+ GCallback signal_handler,
+ gpointer data,
+ bool after) {
+ GObject* object = G_OBJECT(instance);
+
+ HandlerMap::iterator iter = handler_lists_.find(object);
+ if (iter == handler_lists_.end()) {
+ g_object_weak_ref(object, WeakNotifyThunk, this);
+ handler_lists_[object] = HandlerList();
+ iter = handler_lists_.find(object);
+ }
+
+ glong handler_id = after ?
+ g_signal_connect_after(instance, detailed_signal, signal_handler, data) :
+ g_signal_connect(instance, detailed_signal, signal_handler, data);
+ iter->second.push_back(handler_id);
+
+ return handler_id;
+}
+
+void GtkSignalRegistrar::WeakNotify(GObject* where_the_object_was) {
+ HandlerMap::iterator iter = handler_lists_.find(where_the_object_was);
+ if (iter == handler_lists_.end()) {
+ NOTREACHED();
+ return;
+ }
+ // The signal handlers will be disconnected automatically. Just erase the
+ // handler id list.
+ handler_lists_.erase(iter);
+}
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 <gtk/gtk.h>
+#include <map>
+#include <vector>
+
+#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<GtkSignalRegistrar*>(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<glong> HandlerList;
+ typedef std::map<GObject*, HandlerList> HandlerMap;
+ HandlerMap handler_lists_;
+
+ DISALLOW_COPY_AND_ASSIGN(GtkSignalRegistrar);
+};
+
#endif // APP_GTK_SIGNAL_H_