[glib/wip/hadess/add-memory-monitor] gio: Add GMemoryMonitor to monitor for low-memory



commit 8024ca9911a87338f3bb562a47570edd8e3e67e0
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Jul 25 15:52:09 2019 +0200

    gio: Add GMemoryMonitor to monitor for low-memory
    
    Add a memory monitor object, with D-Bus and Portal based
    implementations. The D-Bus implementation uses the Linux-only
    low-memory-monitor Freedesktop project.
    
    The API is simple, similarly to equivalent functionality on iOS and
    Android platforms.
    
    Prior art:
    https://developer.android.com/reference/android/content/ComponentCallbacks2.html#onTrimMemory(int)
    
https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle/responding_to_memory_warnings
    
    TODO:
    - Implement portal functionality
    - Verify and test non-sandboxed functionality
    - Do we want to implement "oom killability" into the API, eg. returning
      something from the signal handler to say "yes, you can kill this
      process if needed". Or it could be a separate API:
      
https://github.com/endlessm/chromium-browser/blob/45f610422da26b6bc60204b0eef2360ca52684cf/base/process/memory_linux.cc#L90

 docs/reference/gio/gio-docs.xml            |   1 +
 docs/reference/gio/gio-sections-common.txt |  17 ++++
 gio/gio-autocleanups.h                     |   1 +
 gio/gio.h                                  |   1 +
 gio/gioenums.h                             |  12 +++
 gio/giomodule.c                            |   8 ++
 gio/giotypes.h                             |   1 +
 gio/gmemorymonitor.c                       | 119 ++++++++++++++++++++++
 gio/gmemorymonitor.h                       |  60 +++++++++++
 gio/gmemorymonitordbus.c                   | 146 +++++++++++++++++++++++++++
 gio/gmemorymonitordbus.h                   |  31 ++++++
 gio/gmemorymonitorportal.c                 | 153 +++++++++++++++++++++++++++++
 gio/gmemorymonitorportal.h                 |  31 ++++++
 gio/meson.build                            |   4 +
 14 files changed, 585 insertions(+)
---
diff --git a/docs/reference/gio/gio-docs.xml b/docs/reference/gio/gio-docs.xml
index 0ce0e2d42..37705724a 100644
--- a/docs/reference/gio/gio-docs.xml
+++ b/docs/reference/gio/gio-docs.xml
@@ -232,6 +232,7 @@
         <xi:include href="xml/gremoteactiongroup.xml"/>
         <xi:include href="xml/gactiongroupexporter.xml"/>
         <xi:include href="xml/gdbusactiongroup.xml"/>
+        <xi:include hred="xml/gmemorymonitor.xml"/>
         <xi:include href="xml/gmenumodel.xml"/>
         <xi:include href="xml/gmenu.xml"/>
         <xi:include href="xml/gmenuexporter.xml"/>
diff --git a/docs/reference/gio/gio-sections-common.txt b/docs/reference/gio/gio-sections-common.txt
index bb483b0ce..12245ca55 100644
--- a/docs/reference/gio/gio-sections-common.txt
+++ b/docs/reference/gio/gio-sections-common.txt
@@ -4169,6 +4169,23 @@ G_DBUS_OBJECT_MANAGER_SERVER_GET_CLASS
 GDBusObjectManagerServerPrivate
 </SECTION>
 
+<SECTION>
+<FILE>gmemorymonitor</FILE>
+<TITLE>GMemoryMonitor</TITLE>
+GMemoryMonitor
+GMemoryMonitorFlags
+GMemoryMonitorInterface
+G_MEMORY_MONITOR_EXTENSION_POINT_NAME
+g_memory_monitor_get_default
+<SUBSECTION Standard>
+g_memory_monitor_get_type
+G_TYPE_MEMORY_MONITOR
+G_MEMORY_MONITOR
+G_IS_MEMORY_MONITOR
+G_MEMORY_MONITOR_GET_INTERFACE
+g_memory_connectivity_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gnetworkmonitor</FILE>
 <TITLE>GNetworkMonitor</TITLE>
diff --git a/gio/gio-autocleanups.h b/gio/gio-autocleanups.h
index ff407293f..044c85dc2 100644
--- a/gio/gio-autocleanups.h
+++ b/gio/gio-autocleanups.h
@@ -85,6 +85,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(GIOStream, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GLoadableIcon, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMemoryInputStream, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMemoryOutputStream, g_object_unref)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMemoryMonitor, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMenu, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMenuItem, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMenuModel, g_object_unref)
diff --git a/gio/gio.h b/gio/gio.h
index f4d934dac..6abaf2884 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -91,6 +91,7 @@
 #include <gio/gloadableicon.h>
 #include <gio/gmemoryinputstream.h>
 #include <gio/gmemoryoutputstream.h>
+#include <gio/gmemorymonitor.h>
 #include <gio/gmount.h>
 #include <gio/gmountoperation.h>
 #include <gio/gnativevolumemonitor.h>
diff --git a/gio/gioenums.h b/gio/gioenums.h
index 22fe7005c..3af772efb 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -1916,6 +1916,18 @@ typedef enum {
   G_NOTIFICATION_PRIORITY_URGENT
 } GNotificationPriority;
 
+/**
+ * GMemoryMonitorFlags:
+ * @G_MEMORY_MONITOR_FLAGS_NONE: No flags set.
+ *
+ * #GMemoryMonitorFlags qualify the low memory warnings sent by
+ * #GMemoryMonitor. There are no special flags for now.
+ *
+ * Since: 2.64
+ */
+typedef enum {
+  G_MEMORY_MONITOR_FLAGS_NONE        = 0
+} GMemoryMonitorFlags;
 /**
  * GNetworkConnectivity:
  * @G_NETWORK_CONNECTIVITY_LOCAL: The host is not configured with a
diff --git a/gio/giomodule.c b/gio/giomodule.c
index 1007abdbf..f2bffcb7b 100644
--- a/gio/giomodule.c
+++ b/gio/giomodule.c
@@ -42,6 +42,9 @@
 #include "gnotificationbackend.h"
 #include "ginitable.h"
 #include "gnetworkmonitor.h"
+#include "gmemorymonitor.h"
+#include "gmemorymonitorportal.h"
+#include "gmemorymonitordbus.h"
 #ifdef G_OS_WIN32
 #include "gregistrysettingsbackend.h"
 #endif
@@ -1025,6 +1028,9 @@ extern GType _g_network_monitor_netlink_get_type (void);
 extern GType _g_network_monitor_nm_get_type (void);
 #endif
 
+extern GType g_memory_monitor_dbus_get_type (void);
+extern GType g_memory_monitor_portal_get_type (void);
+
 #ifdef G_OS_UNIX
 extern GType g_fdo_notification_backend_get_type (void);
 extern GType g_gtk_notification_backend_get_type (void);
@@ -1235,6 +1241,7 @@ _g_io_modules_ensure_loaded (void)
       g_type_ensure (g_fdo_notification_backend_get_type ());
       g_type_ensure (g_gtk_notification_backend_get_type ());
       g_type_ensure (g_portal_notification_backend_get_type ());
+      g_type_ensure (g_memory_monitor_portal_get_type ());
       g_type_ensure (g_network_monitor_portal_get_type ());
       g_type_ensure (g_proxy_resolver_portal_get_type ());
 #endif
@@ -1253,6 +1260,7 @@ _g_io_modules_ensure_loaded (void)
       g_type_ensure (_g_socks4_proxy_get_type ());
       g_type_ensure (_g_socks5_proxy_get_type ());
       g_type_ensure (_g_dummy_tls_backend_get_type ());
+      g_type_ensure (g_memory_monitor_dbus_get_type ());
       g_type_ensure (g_network_monitor_base_get_type ());
 #ifdef HAVE_NETLINK
       g_type_ensure (_g_network_monitor_netlink_get_type ());
diff --git a/gio/giotypes.h b/gio/giotypes.h
index c9ad8dd90..6f6987eca 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -122,6 +122,7 @@ typedef struct _GLoadableIcon                 GLoadableIcon; /* Dummy typedef */
 typedef struct _GBytesIcon                    GBytesIcon;
 typedef struct _GMemoryInputStream            GMemoryInputStream;
 typedef struct _GMemoryOutputStream           GMemoryOutputStream;
+typedef struct _GMemoryMonitor                GMemoryMonitor;
 
 /**
  * GMount:
diff --git a/gio/gmemorymonitor.c b/gio/gmemorymonitor.c
new file mode 100644
index 000000000..3b8caeeae
--- /dev/null
+++ b/gio/gmemorymonitor.c
@@ -0,0 +1,119 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2019 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "glib.h"
+#include "glibintl.h"
+
+#include "gmemorymonitor.h"
+#include "ginetaddress.h"
+#include "ginetsocketaddress.h"
+#include "ginitable.h"
+#include "gioenumtypes.h"
+#include "giomodule-priv.h"
+#include "gtask.h"
+
+/**
+ * SECTION:gmemorymonitor
+ * @title: GMemoryMonitor
+ * @short_description: Memory usage monitor
+ * @include: gio/gio.h
+ *
+ * #GMemoryMonitor will monitor system memory and tell the application
+ * when to free memory so as to leave more room for other applications.
+ * It is implemented on Linux using the [Low Memory 
Monitor](https://gitlab.freedesktop.org/hadess/low-memory-monitor/).
+ *
+ * There is also an implementation for use inside Flatpak sandboxes.
+ *
+ * Possible actions to take when the signal is received are:
+ * - Free caches
+ * - Save files that haven't been looked at in a while to disk, ready to be reopened when needed
+ * - Run a garbage collection cycle
+ * - Try and compress fragmented allocations
+ *
+ */
+
+/**
+ * GMemoryMonitor:
+ *
+ * #GMemoryMonitor monitors system memory and indicates when
+ * the system is low on memory.
+ *
+ * Since: 2.64
+ */
+
+/**
+ * GMemoryMonitorInterface:
+ * @g_iface: The parent interface.
+ * @low_memory_warning: the virtual function pointer for the
+ *  GMemoryMonitor::low-memory-warning signal.
+ *
+ * The virtual function table for #GMemoryMonitor.
+ *
+ * Since: 2.64
+ */
+
+G_DEFINE_INTERFACE_WITH_CODE (GMemoryMonitor, g_memory_monitor, G_TYPE_OBJECT,
+                              g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_INITABLE))
+
+enum {
+  LOW_MEMORY_WARNING,
+  LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+/**
+ * g_memory_monitor_get_default:
+ *
+ * Gets the default #GMemoryMonitor for the system.
+ *
+ * Returns: (transfer none): a #GMemoryMonitor
+ *
+ * Since: 2.64
+ */
+GMemoryMonitor *
+g_memory_monitor_get_default (void)
+{
+  return _g_io_module_get_default (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
+                                   "GIO_USE_MEMORY_MONITOR",
+                                   NULL);
+}
+
+static void
+g_memory_monitor_default_init (GMemoryMonitorInterface *iface)
+{
+  /**
+   * GMemoryMonitor::low-memory-warning:
+   * @monitor: a #GMemoryMonitor
+   *
+   * Emitted when the system is running low on free memory.
+   *
+   * Since: 2.64
+   */
+  signals[LOW_MEMORY_WARNING] =
+    g_signal_new (I_("low-memory-warning"),
+                  G_TYPE_MEMORY_MONITOR,
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (GMemoryMonitorInterface, low_memory_warning),
+                  NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 0,
+                  G_TYPE_NONE);
+
+}
diff --git a/gio/gmemorymonitor.h b/gio/gmemorymonitor.h
new file mode 100644
index 000000000..f62e73102
--- /dev/null
+++ b/gio/gmemorymonitor.h
@@ -0,0 +1,60 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2019 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __G_MEMORY_MONITOR_H__
+#define __G_MEMORY_MONITOR_H__
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+/**
+ * G_MEMORY_MONITOR_EXTENSION_POINT_NAME:
+ *
+ * Extension point for memory usage monitoring functionality.
+ * See [Extending GIO][extending-gio].
+ *
+ * Since: 2.64
+ */
+#define G_MEMORY_MONITOR_EXTENSION_POINT_NAME "gio-memory-monitor"
+
+#define G_TYPE_MEMORY_MONITOR             (g_memory_monitor_get_type ())
+#define G_MEMORY_MONITOR(o)               (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_MEMORY_MONITOR, 
GMemoryMonitor))
+#define G_IS_MEMORY_MONITOR(o)            (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_MEMORY_MONITOR))
+#define G_MEMORY_MONITOR_GET_INTERFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_MEMORY_MONITOR, 
GMemoryMonitorInterface))
+
+typedef struct _GMemoryMonitorInterface GMemoryMonitorInterface;
+
+struct _GMemoryMonitorInterface {
+  GTypeInterface g_iface;
+
+  void     (*low_memory_warning)  (GMemoryMonitor      *monitor);
+};
+
+GLIB_AVAILABLE_IN_2_32
+GType                g_memory_monitor_get_type              (void) G_GNUC_CONST;
+GLIB_AVAILABLE_IN_2_32
+GMemoryMonitor      *g_memory_monitor_get_default           (void);
+
+G_END_DECLS
+
+#endif /* __G_MEMORY_MONITOR_H__ */
diff --git a/gio/gmemorymonitordbus.c b/gio/gmemorymonitordbus.c
new file mode 100644
index 000000000..35e27c5a6
--- /dev/null
+++ b/gio/gmemorymonitordbus.c
@@ -0,0 +1,146 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2019 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gmemorymonitordbus.h"
+#include "gioerror.h"
+#include "ginitable.h"
+#include "giomodule-priv.h"
+#include "glibintl.h"
+#include "glib/gstdio.h"
+#include "gdbusproxy.h"
+
+static GInitableIface *parent_iface;
+static void g_memory_monitor_dbus_iface_init (GMemoryMonitorInterface *iface);
+static void g_memory_monitor_dbus_initable_iface_init (GInitableIface *iface);
+
+struct _GMemoryMonitorDbus
+{
+  GDBusProxy *proxy;
+  guint signal_id;
+};
+
+G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorDbus, g_memory_monitor_dbus, G_TYPE_MEMORY_MONITOR,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
+                                                g_memory_monitor_dbus_iface_init)
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+                                                g_memory_monitor_dbus_initable_iface_init)
+                         _g_io_modules_ensure_extension_points_registered ();
+                         g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
+                                                         g_define_type_id,
+                                                         "dbus",
+                                                         30))
+
+static void
+g_memory_monitor_dbus_init (GMemoryMonitorDbus *dbus)
+{
+}
+
+static void
+proxy_signal_cb (GDBusProxy         *proxy,
+                 const gchar        *sender_name,
+                 const gchar        *signal_name,
+                 GVariant           *parameters,
+                 GMemoryMonitorDbus *dbus)
+{
+  if (g_strcmp0 (signal_name, "LowMemoryWarning") != 0)
+    return;
+
+  g_signal_emit_by_name (dbus, "low-memory-warning");
+}
+
+static gboolean
+g_memory_monitor_dbus_initable_init (GInitable     *initable,
+                                     GCancellable  *cancellable,
+                                     GError       **error)
+{
+  GMemoryMonitorDbus *dbus = G_MEMORY_MONITOR_DBUS (initable);
+  GDBusProxy *proxy;
+  gchar *name_owner = NULL;
+
+  if (!parent_iface->init (initable, cancellable, error))
+    return FALSE;
+
+  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
+                                         NULL,
+                                         "org.freedesktop.LowMemoryMonitor",
+                                         "/org/freedesktop/LowMemoryMonitor",
+                                         "org.freedesktop.LowMemoryMonitor",
+                                         cancellable,
+                                         error);
+  if (!proxy)
+    return FALSE;
+
+  name_owner = g_dbus_proxy_get_name_owner (proxy);
+
+  if (!name_owner)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   _("low-memory-monitor not running"));
+      g_object_unref (proxy);
+      return FALSE;
+    }
+
+  g_free (name_owner);
+
+  dbus->signal_id = g_signal_connect (G_OBJECT (proxy), "g-signal",
+                                      G_CALLBACK (proxy_signal_cb), dbus);
+  dbus->proxy = proxy;
+
+  return TRUE;
+}
+
+static void
+g_memory_monitor_dbus_finalize (GObject *object)
+{
+  GMemoryMonitorDbus *dbus = G_MEMORY_MONITOR_DBUS (object);
+
+  if (dbus->proxy != NULL &&
+      dbus->signal_id != 0)
+    {
+      g_signal_handler_disconnect (dbus->proxy,
+                                   dbus->signal_id);
+      dbus->signal_id = 0;
+    }
+  g_clear_object (&dbus->proxy);
+
+  G_OBJECT_CLASS (g_memory_monitor_dbus_parent_class)->finalize (object);
+}
+
+static void
+g_memory_monitor_dbus_class_init (GMemoryMonitorDbusClass *nl_class)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
+
+  gobject_class->finalize = g_memory_monitor_dbus_finalize;
+}
+
+static void
+g_memory_monitor_dbus_iface_init (GMemoryMonitorInterface *monitor_iface)
+{
+}
+
+static void
+g_memory_monitor_dbus_initable_iface_init (GInitableIface *iface)
+{
+  parent_iface = g_type_interface_peek_parent (iface);
+
+  iface->init = g_memory_monitor_dbus_initable_init;
+}
diff --git a/gio/gmemorymonitordbus.h b/gio/gmemorymonitordbus.h
new file mode 100644
index 000000000..2fe2edf49
--- /dev/null
+++ b/gio/gmemorymonitordbus.h
@@ -0,0 +1,31 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2019 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __G_MEMORY_MONITOR_DBUS_H__
+#define __G_MEMORY_MONITOR_DBUS_H__
+
+#include <gmemorymonitor.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_MEMORY_MONITOR_DBUS         (g_memory_monitor_dbus_get_type ())
+G_DECLARE_FINAL_TYPE (GMemoryMonitorDbus, g_memory_monitor_dbus, G, MEMORY_MONITOR_DBUS, GObject)
+
+G_END_DECLS
+
+#endif /* __G_MEMORY_MONITOR_DBUS_H__ */
diff --git a/gio/gmemorymonitorportal.c b/gio/gmemorymonitorportal.c
new file mode 100644
index 000000000..283149ae8
--- /dev/null
+++ b/gio/gmemorymonitorportal.c
@@ -0,0 +1,153 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gmemorymonitorportal.h"
+#include "ginitable.h"
+#include "giomodule-priv.h"
+#include "xdp-dbus.h"
+#include "gportalsupport.h"
+
+static GInitableIface *parent_iface;
+static void g_memory_monitor_portal_iface_init (GMemoryMonitorInterface *iface);
+static void g_memory_monitor_portal_initable_iface_init (GInitableIface *iface);
+
+struct _GMemoryMonitorPortal
+{
+  GDBusProxy *proxy;
+  guint signal_id;
+};
+
+G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorPortal, g_memory_monitor_portal, G_TYPE_MEMORY_MONITOR,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
+                                                g_memory_monitor_portal_iface_init)
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+                                                g_memory_monitor_portal_initable_iface_init)
+                         _g_io_modules_ensure_extension_points_registered ();
+                         g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
+                                                         g_define_type_id,
+                                                         "portal",
+                                                         40))
+
+static void
+g_memory_monitor_portal_init (GMemoryMonitorPortal *portal)
+{
+}
+
+static void
+proxy_signal (GDBusProxy            *proxy,
+              const char            *sender,
+              const char            *signal,
+              GVariant              *parameters,
+              GMemoryMonitorPortal *portal)
+{
+  if (strcmp (signal, "LowMemoryWarning") != 0)
+    return;
+
+  g_signal_emit_by_name (portal, "low-memory-warning");
+}
+
+static gboolean
+g_memory_monitor_portal_initable_init (GInitable     *initable,
+                                        GCancellable  *cancellable,
+                                        GError       **error)
+{
+  GMemoryMonitorPortal *portal = G_MEMORY_MONITOR_PORTAL (initable);
+  GDBusProxy *proxy;
+  gchar *name_owner = NULL;
+
+  if (!parent_iface->init (initable, cancellable, error))
+    return FALSE;
+
+  if (!glib_should_use_portal ())
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Not using portals");
+      return FALSE;
+    }
+
+  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
+                                         NULL,
+                                         "org.freedesktop.portal.Desktop",
+                                         "/org/freedesktop/portal/desktop",
+                                         "org.freedesktop.portal.MemoryMonitor",
+                                         cancellable,
+                                         error);
+  if (!proxy)
+    return FALSE;
+
+  name_owner = g_dbus_proxy_get_name_owner (proxy);
+
+  if (!name_owner)
+    {
+      g_object_unref (proxy);
+      g_set_error (error,
+                   G_DBUS_ERROR,
+                   G_DBUS_ERROR_NAME_HAS_NO_OWNER,
+                   "Desktop portal not found");
+      return FALSE;
+    }
+
+  g_free (name_owner);
+
+  portal->signal_id = g_signal_connect (proxy, "g-signal",
+                                        G_CALLBACK (proxy_signal), portal);
+
+  portal->proxy = proxy;
+
+  return TRUE;
+}
+
+static void
+g_memory_monitor_portal_finalize (GObject *object)
+{
+  GMemoryMonitorPortal *portal = G_MEMORY_MONITOR_PORTAL (object);
+
+  if (portal->proxy != NULL &&
+      portal->signal_id != 0)
+    {
+      g_signal_handler_disconnect (portal->proxy,
+                                   portal->signal_id);
+      portal->signal_id = 0;
+    }
+  g_clear_object (&portal->proxy);
+
+  G_OBJECT_CLASS (g_memory_monitor_portal_parent_class)->finalize (object);
+}
+
+static void
+g_memory_monitor_portal_class_init (GMemoryMonitorPortalClass *nl_class)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
+
+  gobject_class->finalize  = g_memory_monitor_portal_finalize;
+}
+
+static void
+g_memory_monitor_portal_iface_init (GMemoryMonitorInterface *monitor_iface)
+{
+}
+
+static void
+g_memory_monitor_portal_initable_iface_init (GInitableIface *iface)
+{
+  parent_iface = g_type_interface_peek_parent (iface);
+
+  iface->init = g_memory_monitor_portal_initable_init;
+}
diff --git a/gio/gmemorymonitorportal.h b/gio/gmemorymonitorportal.h
new file mode 100644
index 000000000..1ee4e4b24
--- /dev/null
+++ b/gio/gmemorymonitorportal.h
@@ -0,0 +1,31 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2019 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __G_MEMORY_MONITOR_PORTAL_H__
+#define __G_MEMORY_MONITOR_PORTAL_H__
+
+#include <gmemorymonitor.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_MEMORY_MONITOR_PORTAL         (g_memory_monitor_portal_get_type ())
+G_DECLARE_FINAL_TYPE (GMemoryMonitorPortal, g_memory_monitor_portal, G, MEMORY_MONITOR_PORTAL, GObject)
+
+G_END_DECLS
+
+#endif /* __G_MEMORY_MONITOR_PORTAL_H__ */
diff --git a/gio/meson.build b/gio/meson.build
index 987d0f4c6..6876fcf19 100644
--- a/gio/meson.build
+++ b/gio/meson.build
@@ -393,6 +393,8 @@ if host_system != 'windows'
   portal_sources = [files(
     'gdocumentportal.c',
     'gopenuriportal.c',
+    'gmemorymonitordbus.c',
+    'gmemorymonitorportal.c',
     'gnetworkmonitorportal.c',
     'gproxyresolverportal.c',
     'gtrashportal.c',
@@ -528,6 +530,7 @@ gio_sources = files(
   'gloadableicon.c',
   'gmarshal-internal.c',
   'gmount.c',
+  'gmemorymonitor.c',
   'gmemoryinputstream.c',
   'gmemoryoutputstream.c',
   'gmountoperation.c',
@@ -671,6 +674,7 @@ gio_headers = files(
   'gmount.h',
   'gmemoryinputstream.h',
   'gmemoryoutputstream.h',
+  'gmemorymonitor.h',
   'gmountoperation.h',
   'gnativevolumemonitor.h',
   'gnetworkaddress.h',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]