summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/app_base.gypi2
-rw-r--r--app/gtk_signal.cc72
-rw-r--r--app/gtk_signal.h55
-rw-r--r--chrome/browser/gtk/info_bubble_gtk.cc57
-rw-r--r--chrome/browser/gtk/info_bubble_gtk.h2
5 files changed, 141 insertions, 47 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_
diff --git a/chrome/browser/gtk/info_bubble_gtk.cc b/chrome/browser/gtk/info_bubble_gtk.cc
index 16a1ff3..28dc047 100644
--- a/chrome/browser/gtk/info_bubble_gtk.cc
+++ b/chrome/browser/gtk/info_bubble_gtk.cc
@@ -80,38 +80,8 @@ InfoBubbleGtk::~InfoBubbleGtk() {
delegate_->InfoBubbleClosing(this, closed_by_escape_);
g_object_unref(accel_group_);
- if (mask_region_) {
+ if (mask_region_)
gdk_region_destroy(mask_region_);
- mask_region_ = NULL;
- }
-
- if (anchor_widget_) {
- g_signal_handlers_disconnect_by_func(
- anchor_widget_,
- reinterpret_cast<gpointer>(OnAnchorAllocateThunk),
- this);
- g_signal_handlers_disconnect_by_func(
- anchor_widget_,
- reinterpret_cast<gpointer>(gtk_widget_destroyed),
- &anchor_widget_);
- }
- anchor_widget_ = NULL;
-
- if (toplevel_window_) {
- g_signal_handlers_disconnect_by_func(
- toplevel_window_,
- reinterpret_cast<gpointer>(OnToplevelConfigureThunk),
- this);
- g_signal_handlers_disconnect_by_func(
- toplevel_window_,
- reinterpret_cast<gpointer>(OnToplevelUnmapThunk),
- this);
- g_signal_handlers_disconnect_by_func(
- toplevel_window_,
- reinterpret_cast<gpointer>(gtk_widget_destroyed),
- &toplevel_window_);
- }
- toplevel_window_ = NULL;
}
void InfoBubbleGtk::Init(GtkWidget* anchor_widget,
@@ -164,32 +134,29 @@ void InfoBubbleGtk::Init(GtkWidget* anchor_widget,
gtk_widget_add_events(window_, GDK_BUTTON_PRESS_MASK);
- g_signal_connect(window_, "expose-event",
- G_CALLBACK(OnExposeThunk), this);
- g_signal_connect(window_, "size-allocate",
- G_CALLBACK(OnSizeAllocateThunk), this);
- g_signal_connect(window_, "button-press-event",
+ signals_.Connect(window_, "expose-event", G_CALLBACK(OnExposeThunk), this);
+ signals_.Connect(window_, "size-allocate", G_CALLBACK(OnSizeAllocateThunk),
+ this);
+ signals_.Connect(window_, "button-press-event",
G_CALLBACK(OnButtonPressThunk), this);
- g_signal_connect(window_, "destroy",
- G_CALLBACK(OnDestroyThunk), this);
- g_signal_connect(window_, "hide",
- G_CALLBACK(OnHideThunk), this);
+ signals_.Connect(window_, "destroy", G_CALLBACK(OnDestroyThunk), this);
+ signals_.Connect(window_, "hide", G_CALLBACK(OnHideThunk), this);
// If the toplevel window is being used as the anchor, then the signals below
// are enough to keep us positioned correctly.
if (anchor_widget_ != GTK_WIDGET(toplevel_window_)) {
- g_signal_connect(anchor_widget_, "size-allocate",
+ signals_.Connect(anchor_widget_, "size-allocate",
G_CALLBACK(OnAnchorAllocateThunk), this);
- g_signal_connect(anchor_widget_, "destroy",
+ signals_.Connect(anchor_widget_, "destroy",
G_CALLBACK(gtk_widget_destroyed), &anchor_widget_);
}
- g_signal_connect(toplevel_window_, "configure-event",
+ signals_.Connect(toplevel_window_, "configure-event",
G_CALLBACK(OnToplevelConfigureThunk), this);
- g_signal_connect(toplevel_window_, "unmap-event",
+ signals_.Connect(toplevel_window_, "unmap-event",
G_CALLBACK(OnToplevelUnmapThunk), this);
// Set |toplevel_window_| to NULL if it gets destroyed.
- g_signal_connect(toplevel_window_, "destroy",
+ signals_.Connect(toplevel_window_, "destroy",
G_CALLBACK(gtk_widget_destroyed), &toplevel_window_);
gtk_widget_show_all(window_);
diff --git a/chrome/browser/gtk/info_bubble_gtk.h b/chrome/browser/gtk/info_bubble_gtk.h
index 65515d6..104dc01 100644
--- a/chrome/browser/gtk/info_bubble_gtk.h
+++ b/chrome/browser/gtk/info_bubble_gtk.h
@@ -209,6 +209,8 @@ class InfoBubbleGtk : public NotificationObserver {
NotificationRegistrar registrar_;
+ GtkSignalRegistrar signals_;
+
DISALLOW_COPY_AND_ASSIGN(InfoBubbleGtk);
};