- add sources.
[platform/framework/web/crosswalk.git] / src / ui / base / gtk / gtk_signal_registrar.h
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.
4
5 #ifndef UI_BASE_GTK_GTK_SIGNAL_REGISTRAR_H_
6 #define UI_BASE_GTK_GTK_SIGNAL_REGISTRAR_H_
7
8 #include <glib.h>
9 #include <map>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "ui/base/ui_export.h"
14
15 typedef void (*GCallback) (void);
16 typedef struct _GObject GObject;
17 typedef struct _GtkWidget GtkWidget;
18
19 namespace ui {
20
21 // A class that ensures that callbacks don't run on stale owner objects. Similar
22 // in spirit to NotificationRegistrar. Use as follows:
23 //
24 //   class ChromeObject {
25 //    public:
26 //     ChromeObject() {
27 //       ...
28 //
29 //       signals_.Connect(widget, "event", CallbackThunk, this);
30 //     }
31 //
32 //     ...
33 //
34 //    private:
35 //     GtkSignalRegistrar signals_;
36 //   };
37 //
38 // When |signals_| goes down, it will disconnect the handlers connected via
39 // Connect.
40 class UI_EXPORT GtkSignalRegistrar {
41  public:
42   GtkSignalRegistrar();
43   ~GtkSignalRegistrar();
44
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);
51
52   // Disconnects all signal handlers connected to |instance|.
53   void DisconnectAll(gpointer instance);
54
55  private:
56   typedef std::vector<glong> HandlerList;
57   typedef std::map<GObject*, HandlerList> HandlerMap;
58
59   static void WeakNotifyThunk(gpointer data, GObject* where_the_object_was) {
60     reinterpret_cast<GtkSignalRegistrar*>(data)->WeakNotify(
61         where_the_object_was);
62   }
63   void WeakNotify(GObject* where_the_object_was);
64
65   glong ConnectInternal(gpointer instance, const gchar* detailed_signal,
66                         GCallback signal_handler, gpointer data, bool after);
67
68   HandlerMap handler_lists_;
69
70   DISALLOW_COPY_AND_ASSIGN(GtkSignalRegistrar);
71 };
72
73 }  // namespace ui
74
75 #endif  // UI_BASE_GTK_GTK_SIGNAL_REGISTRAR_H_