[glib/portal: 8/17] network monitor portal: Respect sandbox setup



commit e8df162402d1bcae52f3f25a3fbd071a45ace4a9
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 21 21:05:57 2016 -0400

    network monitor portal: Respect sandbox setup
    
    When network is not available in the sandbox, there is
    no point in reporting accurately about the network
    status outside the sandbox. Just return 'no connection'
    in this case.

 gio/gnetworkmonitorportal.c |   43 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 36 insertions(+), 7 deletions(-)
---
diff --git a/gio/gnetworkmonitorportal.c b/gio/gnetworkmonitorportal.c
index 2ff68fe..655f787 100644
--- a/gio/gnetworkmonitorportal.c
+++ b/gio/gnetworkmonitorportal.c
@@ -39,6 +39,7 @@ enum
 struct _GNetworkMonitorPortalPrivate
 {
   XdpNetworkMonitor *proxy;
+  gboolean network_available;
 };
 
 G_DEFINE_TYPE_WITH_CODE (GNetworkMonitorPortal, g_network_monitor_portal, G_TYPE_NETWORK_MONITOR_BASE,
@@ -70,15 +71,22 @@ g_network_monitor_portal_get_property (GObject    *object,
   switch (prop_id)
     {
     case PROP_NETWORK_AVAILABLE:
-      g_value_set_boolean (value, xdp_network_monitor_get_available (nm->priv->proxy));
+      g_value_set_boolean (value,
+                           nm->priv->network_available &&
+                           xdp_network_monitor_get_available (nm->priv->proxy));
       break;
 
     case PROP_NETWORK_METERED:
-      g_value_set_boolean (value, xdp_network_monitor_get_metered (nm->priv->proxy));
+      g_value_set_boolean (value,
+                           nm->priv->network_available &&
+                           xdp_network_monitor_get_metered (nm->priv->proxy));
       break;
 
     case PROP_CONNECTIVITY:
-      g_value_set_enum (value, xdp_network_monitor_get_connectivity (nm->priv->proxy));
+      g_value_set_enum (value,
+                        nm->priv->network_available
+                        ? xdp_network_monitor_get_connectivity (nm->priv->proxy)
+                        : G_NETWORK_CONNECTIVITY_LOCAL);
       break;
 
     default:
@@ -88,11 +96,12 @@ g_network_monitor_portal_get_property (GObject    *object,
 }
 
 static void
-proxy_changed (XdpNetworkMonitor *proxy,
-               gboolean           available,
-               GNetworkMonitor   *monitor)
+proxy_changed (XdpNetworkMonitor     *proxy,
+               gboolean               available,
+               GNetworkMonitorPortal *nm)
 {
-  g_signal_emit_by_name (monitor, "network-changed", available);
+  if (nm->priv->network_available)
+    g_signal_emit_by_name (nm, "network-changed", available);
 }
 
 
@@ -117,6 +126,25 @@ should_use_portal (void)
 }
 
 static gboolean
+network_available_in_sandbox (void)
+{
+  char *path;
+  g_autoptr(GKeyFile) keyfile = g_key_file_new ();
+
+  path = g_strdup_printf ("/run/user/%d/flatpak-info", getuid());
+  if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL))
+    {
+      g_auto(GStrv) shared = NULL;
+
+      shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL);
+
+      return g_strv_contains ((const char * const *)shared, "network");
+    }
+
+  return TRUE;
+}
+
+static gboolean
 g_network_monitor_portal_initable_init (GInitable     *initable,
                                         GCancellable  *cancellable,
                                         GError       **error)
@@ -153,6 +181,7 @@ g_network_monitor_portal_initable_init (GInitable     *initable,
 
   g_signal_connect (G_OBJECT (proxy), "changed", G_CALLBACK (proxy_changed), nm);
   nm->priv->proxy = proxy;
+  nm->priv->network_available = network_available_in_sandbox ();
 
   return TRUE;
 }


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