OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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. | |
4 | |
5 #ifndef APP_GTK_SIGNAL_REGISTRAR_H_ | |
6 #define APP_GTK_SIGNAL_REGISTRAR_H_ | |
7 #pragma once | |
8 | |
9 #include <glib.h> | |
10 #include <map> | |
11 #include <vector> | |
12 | |
13 #include "base/basictypes.h" | |
14 | |
15 typedef void (*GCallback) (void); | |
16 typedef struct _GObject GObject; | |
17 typedef struct _GtkWidget GtkWidget; | |
18 | |
19 // A class that ensures that callbacks don't run on stale owner objects. Similar | |
20 // in spirit to NotificationRegistrar. Use as follows: | |
21 // | |
22 // class ChromeObject { | |
23 // public: | |
24 // ChromeObject() { | |
25 // ... | |
26 // | |
27 // signals_.Connect(widget, "event", CallbackThunk, this); | |
28 // } | |
29 // | |
30 // ... | |
31 // | |
32 // private: | |
33 // GtkSignalRegistrar signals_; | |
34 // }; | |
35 // | |
36 // When |signals_| goes down, it will disconnect the handlers connected via | |
37 // Connect. | |
38 class GtkSignalRegistrar { | |
39 public: | |
40 GtkSignalRegistrar(); | |
41 ~GtkSignalRegistrar(); | |
42 | |
43 // Connect before the default handler. Returns the handler id. | |
44 glong Connect(gpointer instance, const gchar* detailed_signal, | |
45 GCallback signal_handler, gpointer data); | |
46 // Connect after the default handler. Returns the handler id. | |
47 glong ConnectAfter(gpointer instance, const gchar* detailed_signal, | |
48 GCallback signal_handler, gpointer data); | |
49 | |
50 private: | |
51 typedef std::vector<glong> HandlerList; | |
52 typedef std::map<GObject*, HandlerList> HandlerMap; | |
53 | |
54 static void WeakNotifyThunk(gpointer data, GObject* where_the_object_was) { | |
55 reinterpret_cast<GtkSignalRegistrar*>(data)->WeakNotify( | |
56 where_the_object_was); | |
57 } | |
58 void WeakNotify(GObject* where_the_object_was); | |
59 | |
60 glong ConnectInternal(gpointer instance, const gchar* detailed_signal, | |
61 GCallback signal_handler, gpointer data, bool after); | |
62 | |
63 HandlerMap handler_lists_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(GtkSignalRegistrar); | |
66 }; | |
67 | |
68 #endif // APP_GTK_SIGNAL_REGISTRAR_H_ | |
OLD | NEW |