1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2019 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "gmemorymonitor.h"
22 #include "gmemorymonitordbus.h"
24 #include "ginitable.h"
25 #include "giomodule-priv.h"
27 #include "glib/gstdio.h"
28 #include "gdbusproxy.h"
29 #include "gdbusnamewatching.h"
31 #define G_MEMORY_MONITOR_DBUS_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))
33 static void g_memory_monitor_dbus_iface_init (GMemoryMonitorInterface *iface);
34 static void g_memory_monitor_dbus_initable_iface_init (GInitableIface *iface);
36 struct _GMemoryMonitorDBus
38 GObject parent_instance;
45 G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorDBus, g_memory_monitor_dbus, G_TYPE_OBJECT,
46 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
47 g_memory_monitor_dbus_initable_iface_init)
48 G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
49 g_memory_monitor_dbus_iface_init)
50 _g_io_modules_ensure_extension_points_registered ();
51 g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
57 g_memory_monitor_dbus_init (GMemoryMonitorDBus *dbus)
62 proxy_signal_cb (GDBusProxy *proxy,
63 const gchar *sender_name,
64 const gchar *signal_name,
66 GMemoryMonitorDBus *dbus)
70 if (g_strcmp0 (signal_name, "LowMemoryWarning") != 0)
72 if (parameters == NULL)
75 g_variant_get (parameters, "(y)", &level);
76 g_signal_emit_by_name (dbus, "low-memory-warning", level);
80 lmm_appeared_cb (GDBusConnection *connection,
82 const gchar *name_owner,
85 GMemoryMonitorDBus *dbus = user_data;
89 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
90 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
92 "org.freedesktop.LowMemoryMonitor",
93 "/org/freedesktop/LowMemoryMonitor",
94 "org.freedesktop.LowMemoryMonitor",
100 g_debug ("Failed to create LowMemoryMonitor D-Bus proxy: %s",
102 g_error_free (error);
106 dbus->signal_id = g_signal_connect (G_OBJECT (proxy), "g-signal",
107 G_CALLBACK (proxy_signal_cb), dbus);
112 lmm_vanished_cb (GDBusConnection *connection,
116 GMemoryMonitorDBus *dbus = user_data;
118 if (dbus->proxy != NULL)
119 g_clear_signal_handler (&dbus->signal_id, dbus->proxy);
120 g_clear_object (&dbus->proxy);
124 g_memory_monitor_dbus_initable_init (GInitable *initable,
125 GCancellable *cancellable,
128 GMemoryMonitorDBus *dbus = G_MEMORY_MONITOR_DBUS (initable);
130 dbus->watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
131 "org.freedesktop.LowMemoryMonitor",
132 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
142 g_memory_monitor_dbus_finalize (GObject *object)
144 GMemoryMonitorDBus *dbus = G_MEMORY_MONITOR_DBUS (object);
146 if (dbus->proxy != NULL)
147 g_clear_signal_handler (&dbus->signal_id, dbus->proxy);
148 g_clear_object (&dbus->proxy);
149 g_clear_handle_id (&dbus->watch_id, g_bus_unwatch_name);
151 G_OBJECT_CLASS (g_memory_monitor_dbus_parent_class)->finalize (object);
155 g_memory_monitor_dbus_class_init (GMemoryMonitorDBusClass *nl_class)
157 GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
159 gobject_class->finalize = g_memory_monitor_dbus_finalize;
163 g_memory_monitor_dbus_iface_init (GMemoryMonitorInterface *monitor_iface)
168 g_memory_monitor_dbus_initable_iface_init (GInitableIface *iface)
170 iface->init = g_memory_monitor_dbus_initable_init;