1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2019 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "gmemorymonitor.h"
24 #include "gmemorymonitordbus.h"
26 #include "ginitable.h"
27 #include "giomodule-priv.h"
29 #include "glib/gstdio.h"
30 #include "gcancellable.h"
31 #include "gdbusproxy.h"
32 #include "gdbusnamewatching.h"
34 #define G_MEMORY_MONITOR_DBUS_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))
36 static void g_memory_monitor_dbus_iface_init (GMemoryMonitorInterface *iface);
37 static void g_memory_monitor_dbus_initable_iface_init (GInitableIface *iface);
39 struct _GMemoryMonitorDBus
41 GObject parent_instance;
44 GCancellable *cancellable;
49 G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorDBus, g_memory_monitor_dbus, G_TYPE_OBJECT,
50 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
51 g_memory_monitor_dbus_initable_iface_init)
52 G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
53 g_memory_monitor_dbus_iface_init)
54 _g_io_modules_ensure_extension_points_registered ();
55 g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
61 g_memory_monitor_dbus_init (GMemoryMonitorDBus *dbus)
66 proxy_signal_cb (GDBusProxy *proxy,
67 const gchar *sender_name,
68 const gchar *signal_name,
70 GMemoryMonitorDBus *dbus)
74 if (g_strcmp0 (signal_name, "LowMemoryWarning") != 0)
76 if (parameters == NULL)
79 g_variant_get (parameters, "(y)", &level);
80 g_signal_emit_by_name (dbus, "low-memory-warning", level);
84 lmm_proxy_cb (GObject *source_object,
88 GMemoryMonitorDBus *dbus = user_data;
92 proxy = g_dbus_proxy_new_finish (res, &error);
95 g_debug ("Failed to create LowMemoryMonitor D-Bus proxy: %s",
101 dbus->signal_id = g_signal_connect (G_OBJECT (proxy), "g-signal",
102 G_CALLBACK (proxy_signal_cb), dbus);
108 lmm_appeared_cb (GDBusConnection *connection,
110 const gchar *name_owner,
113 GMemoryMonitorDBus *dbus = user_data;
115 g_dbus_proxy_new (connection,
116 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
118 "org.freedesktop.LowMemoryMonitor",
119 "/org/freedesktop/LowMemoryMonitor",
120 "org.freedesktop.LowMemoryMonitor",
127 lmm_vanished_cb (GDBusConnection *connection,
131 GMemoryMonitorDBus *dbus = user_data;
133 g_clear_signal_handler (&dbus->signal_id, dbus->proxy);
134 g_clear_object (&dbus->proxy);
138 g_memory_monitor_dbus_initable_init (GInitable *initable,
139 GCancellable *cancellable,
142 GMemoryMonitorDBus *dbus = G_MEMORY_MONITOR_DBUS (initable);
144 dbus->cancellable = g_cancellable_new ();
145 dbus->watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
146 "org.freedesktop.LowMemoryMonitor",
147 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
157 g_memory_monitor_dbus_finalize (GObject *object)
159 GMemoryMonitorDBus *dbus = G_MEMORY_MONITOR_DBUS (object);
161 g_cancellable_cancel (dbus->cancellable);
162 g_clear_object (&dbus->cancellable);
163 g_clear_signal_handler (&dbus->signal_id, dbus->proxy);
164 g_clear_object (&dbus->proxy);
165 g_clear_handle_id (&dbus->watch_id, g_bus_unwatch_name);
167 G_OBJECT_CLASS (g_memory_monitor_dbus_parent_class)->finalize (object);
171 g_memory_monitor_dbus_class_init (GMemoryMonitorDBusClass *nl_class)
173 GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
175 gobject_class->finalize = g_memory_monitor_dbus_finalize;
179 g_memory_monitor_dbus_iface_init (GMemoryMonitorInterface *monitor_iface)
184 g_memory_monitor_dbus_initable_iface_init (GInitableIface *iface)
186 iface->init = g_memory_monitor_dbus_initable_init;