LCOV - code coverage report
Current view: top level - glib/gio - gproxyresolverportal.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 26 72 36.1 %
Date: 2024-04-23 05:16:05 Functions: 9 13 69.2 %
Branches: 8 22 36.4 %

           Branch data     Line data    Source code
       1                 :            : /* GIO - GLib Input, Output and Streaming Library
       2                 :            :  *
       3                 :            :  * Copyright 2016 Red Hat, Inc.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       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.
      11                 :            :  *
      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.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see
      19                 :            :  * <http://www.gnu.org/licenses/>.
      20                 :            :  */
      21                 :            : 
      22                 :            : #include "config.h"
      23                 :            : 
      24                 :            : #include "xdp-dbus.h"
      25                 :            : #include "giomodule-priv.h"
      26                 :            : #include "gportalsupport.h"
      27                 :            : #include "gproxyresolverportal.h"
      28                 :            : 
      29                 :            : struct _GProxyResolverPortal {
      30                 :            :   GObject parent_instance;
      31                 :            : 
      32                 :            :   GXdpProxyResolver *resolver;
      33                 :            :   gboolean network_available;
      34                 :            : };
      35                 :            : 
      36                 :            : static void g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface);
      37                 :            : 
      38   [ +  +  +  -  :        243 : G_DEFINE_TYPE_WITH_CODE (GProxyResolverPortal, g_proxy_resolver_portal, G_TYPE_OBJECT,
                   +  + ]
      39                 :            :                          G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
      40                 :            :                                                 g_proxy_resolver_portal_iface_init)
      41                 :            :                          _g_io_modules_ensure_extension_points_registered ();
      42                 :            :                          g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
      43                 :            :                                                          g_define_type_id,
      44                 :            :                                                          "portal",
      45                 :            :                                                          90))
      46                 :            : 
      47                 :            : static gboolean
      48                 :          5 : ensure_resolver_proxy (GProxyResolverPortal *resolver)
      49                 :            : {
      50         [ -  + ]:          5 :   if (resolver->resolver)
      51                 :          0 :     return TRUE;
      52                 :            : 
      53         [ +  - ]:          5 :   if (!glib_should_use_portal ())
      54                 :          5 :     return FALSE;
      55                 :            : 
      56                 :          0 :   resolver->resolver = gxdp_proxy_resolver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
      57                 :            :                                                                    G_DBUS_PROXY_FLAGS_NONE,
      58                 :            :                                                                    "org.freedesktop.portal.Desktop",
      59                 :            :                                                                    "/org/freedesktop/portal/desktop",
      60                 :            :                                                                    NULL,
      61                 :            :                                                                    NULL);
      62                 :            : 
      63                 :          0 :   resolver->network_available = glib_network_available_in_sandbox ();
      64                 :            : 
      65                 :          0 :   return resolver->resolver != NULL;
      66                 :            : }
      67                 :            : 
      68                 :            : static void
      69                 :          5 : g_proxy_resolver_portal_init (GProxyResolverPortal *resolver)
      70                 :            : {
      71                 :          5 : }
      72                 :            : 
      73                 :            : static gboolean
      74                 :          5 : g_proxy_resolver_portal_is_supported (GProxyResolver *object)
      75                 :            : {
      76                 :          5 :   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
      77                 :            :   char *name_owner;
      78                 :            :   gboolean has_portal;
      79                 :            : 
      80         [ +  - ]:          5 :   if (!ensure_resolver_proxy (resolver))
      81                 :          5 :     return FALSE;
      82                 :            : 
      83                 :          0 :   name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (resolver->resolver));
      84                 :          0 :   has_portal = name_owner != NULL;
      85                 :          0 :   g_free (name_owner);
      86                 :            : 
      87                 :          0 :   return has_portal;
      88                 :            : }
      89                 :            : 
      90                 :            : static const char *no_proxy[2] = { "direct://", NULL };
      91                 :            : 
      92                 :            : static gchar **
      93                 :          0 : g_proxy_resolver_portal_lookup (GProxyResolver *proxy_resolver,
      94                 :            :                                 const gchar     *uri,
      95                 :            :                                 GCancellable    *cancellable,
      96                 :            :                                 GError         **error)
      97                 :            : {
      98                 :          0 :   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
      99                 :          0 :   char **proxy = NULL;
     100                 :            : 
     101                 :          0 :   ensure_resolver_proxy (resolver);
     102                 :          0 :   g_assert (resolver->resolver);
     103                 :            : 
     104         [ #  # ]:          0 :   if (!gxdp_proxy_resolver_call_lookup_sync (resolver->resolver,
     105                 :            :                                              uri,
     106                 :            :                                              &proxy,
     107                 :            :                                              cancellable,
     108                 :            :                                              error))
     109                 :          0 :     return NULL;
     110                 :            : 
     111         [ #  # ]:          0 :   if (!resolver->network_available)
     112                 :            :     {
     113                 :          0 :       g_strfreev (proxy);
     114                 :          0 :       proxy = g_strdupv ((gchar **)no_proxy);
     115                 :            :     }
     116                 :            : 
     117                 :          0 :   return proxy;
     118                 :            : }
     119                 :            : 
     120                 :            : static void
     121                 :          0 : lookup_done (GObject      *source,
     122                 :            :              GAsyncResult *result,
     123                 :            :              gpointer      data)
     124                 :            : {
     125                 :          0 :   GTask *task = data;
     126                 :          0 :   GError *error = NULL;
     127                 :          0 :   gchar **proxies = NULL;
     128                 :            : 
     129         [ #  # ]:          0 :   if (!gxdp_proxy_resolver_call_lookup_finish (GXDP_PROXY_RESOLVER (source),
     130                 :            :                                                &proxies,
     131                 :            :                                                result,
     132                 :            :                                                &error))
     133                 :          0 :     g_task_return_error (task, error);
     134                 :            :   else
     135                 :          0 :     g_task_return_pointer (task, proxies, NULL);
     136                 :            : 
     137                 :          0 :   g_object_unref (task);
     138                 :          0 : }
     139                 :            : 
     140                 :            : static void
     141                 :          0 : g_proxy_resolver_portal_lookup_async (GProxyResolver      *proxy_resolver,
     142                 :            :                                       const gchar         *uri,
     143                 :            :                                       GCancellable        *cancellable,
     144                 :            :                                       GAsyncReadyCallback  callback,
     145                 :            :                                       gpointer             user_data)
     146                 :            : {
     147                 :          0 :   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
     148                 :            :   GTask *task;
     149                 :            : 
     150                 :          0 :   ensure_resolver_proxy (resolver);
     151                 :          0 :   g_assert (resolver->resolver);
     152                 :            : 
     153                 :          0 :   task = g_task_new (proxy_resolver, cancellable, callback, user_data);
     154                 :          0 :   gxdp_proxy_resolver_call_lookup (resolver->resolver,
     155                 :            :                                    uri,
     156                 :            :                                    cancellable,
     157                 :            :                                    lookup_done,
     158                 :            :                                    g_object_ref (task));
     159                 :          0 :   g_object_unref (task);
     160                 :          0 : }
     161                 :            : 
     162                 :            : static gchar **
     163                 :          0 : g_proxy_resolver_portal_lookup_finish (GProxyResolver  *proxy_resolver,
     164                 :            :                                        GAsyncResult    *result,
     165                 :            :                                        GError         **error)
     166                 :            : {
     167                 :          0 :   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (proxy_resolver);
     168                 :          0 :   GTask *task = G_TASK (result);
     169                 :            :   char **proxies;
     170                 :            : 
     171                 :          0 :   proxies = g_task_propagate_pointer (task, error);
     172         [ #  # ]:          0 :   if (proxies == NULL)
     173                 :          0 :     return NULL;
     174                 :            : 
     175         [ #  # ]:          0 :   if (!resolver->network_available)
     176                 :            :     {
     177                 :          0 :       g_strfreev (proxies);
     178                 :          0 :       proxies = g_strdupv ((gchar **)no_proxy);
     179                 :            :     }
     180                 :            : 
     181                 :          0 :   return proxies;
     182                 :            : }
     183                 :            : 
     184                 :            : static void
     185                 :          5 : g_proxy_resolver_portal_finalize (GObject *object)
     186                 :            : {
     187                 :          5 :   GProxyResolverPortal *resolver = G_PROXY_RESOLVER_PORTAL (object);
     188                 :            : 
     189                 :          5 :   g_clear_object (&resolver->resolver);
     190                 :            : 
     191                 :          5 :   G_OBJECT_CLASS (g_proxy_resolver_portal_parent_class)->finalize (object);
     192                 :          5 : }
     193                 :            : 
     194                 :            : static void
     195                 :          5 : g_proxy_resolver_portal_class_init (GProxyResolverPortalClass *resolver_class)
     196                 :            : {
     197                 :            :   GObjectClass *object_class;
     198                 :            :  
     199                 :          5 :   object_class = G_OBJECT_CLASS (resolver_class);
     200                 :          5 :   object_class->finalize = g_proxy_resolver_portal_finalize;
     201                 :          5 : }
     202                 :            : 
     203                 :            : static void
     204                 :          5 : g_proxy_resolver_portal_iface_init (GProxyResolverInterface *iface)
     205                 :            : {
     206                 :          5 :   iface->is_supported = g_proxy_resolver_portal_is_supported;
     207                 :          5 :   iface->lookup = g_proxy_resolver_portal_lookup;
     208                 :          5 :   iface->lookup_async = g_proxy_resolver_portal_lookup_async;
     209                 :          5 :   iface->lookup_finish = g_proxy_resolver_portal_lookup_finish;
     210                 :          5 : }

Generated by: LCOV version 1.14