1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_BASE_GTK_GTK_SIGNAL_REGISTRAR_H_
6 #define UI_BASE_GTK_GTK_SIGNAL_REGISTRAR_H_
12 #include "base/basictypes.h"
13 #include "ui/base/ui_export.h"
15 typedef void (*GCallback) (void);
16 typedef struct _GObject GObject;
17 typedef struct _GtkWidget GtkWidget;
21 // A class that ensures that callbacks don't run on stale owner objects. Similar
22 // in spirit to NotificationRegistrar. Use as follows:
24 // class ChromeObject {
29 // signals_.Connect(widget, "event", CallbackThunk, this);
35 // GtkSignalRegistrar signals_;
38 // When |signals_| goes down, it will disconnect the handlers connected via
40 class UI_EXPORT GtkSignalRegistrar {
43 ~GtkSignalRegistrar();
45 // Connect before the default handler. Returns the handler id.
46 glong Connect(gpointer instance, const gchar* detailed_signal,
47 GCallback signal_handler, gpointer data);
48 // Connect after the default handler. Returns the handler id.
49 glong ConnectAfter(gpointer instance, const gchar* detailed_signal,
50 GCallback signal_handler, gpointer data);
52 // Disconnects all signal handlers connected to |instance|.
53 void DisconnectAll(gpointer instance);
56 typedef std::vector<glong> HandlerList;
57 typedef std::map<GObject*, HandlerList> HandlerMap;
59 static void WeakNotifyThunk(gpointer data, GObject* where_the_object_was) {
60 reinterpret_cast<GtkSignalRegistrar*>(data)->WeakNotify(
61 where_the_object_was);
63 void WeakNotify(GObject* where_the_object_was);
65 glong ConnectInternal(gpointer instance, const gchar* detailed_signal,
66 GCallback signal_handler, gpointer data, bool after);
68 HandlerMap handler_lists_;
70 DISALLOW_COPY_AND_ASSIGN(GtkSignalRegistrar);
75 #endif // UI_BASE_GTK_GTK_SIGNAL_REGISTRAR_H_