LCOV - code coverage report
Current view: top level - src/libvalent/device - valent-channel-service.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 88.8 % 214 190
Test Date: 2025-04-25 03:07:01 Functions: 100.0 % 19 19
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 58.1 % 93 54

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       2                 :             : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
       3                 :             : 
       4                 :             : #define G_LOG_DOMAIN "valent-channel-service"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <gio/gio.h>
       9                 :             : #include <json-glib/json-glib.h>
      10                 :             : #include <libpeas.h>
      11                 :             : #include <libvalent-core.h>
      12                 :             : 
      13                 :             : #include "valent-certificate.h"
      14                 :             : #include "valent-channel.h"
      15                 :             : #include "valent-device-common.h"
      16                 :             : #include "valent-packet.h"
      17                 :             : 
      18                 :             : #include "valent-channel-service.h"
      19                 :             : 
      20                 :             : /**
      21                 :             :  * ValentChannelService:
      22                 :             :  *
      23                 :             :  * An abstract base class for connection backends.
      24                 :             :  *
      25                 :             :  * `ValentChannelService` is a base class for plugins that implement an interface
      26                 :             :  * to negotiate connections with other devices.
      27                 :             :  *
      28                 :             :  * ## Implementation Notes
      29                 :             :  *
      30                 :             :  * Implementations may safely invoke [method@Valent.ChannelService.channel] from
      31                 :             :  * any thread; it is guaranteed to be emitted in the main thread.
      32                 :             :  *
      33                 :             :  * ## `.plugin` File
      34                 :             :  *
      35                 :             :  * Channel services have no special fields in the `.plugin` file.
      36                 :             :  *
      37                 :             :  * Since: 1.0
      38                 :             :  */
      39                 :             : 
      40                 :             : typedef struct
      41                 :             : {
      42                 :             :   GTlsCertificate *certificate;
      43                 :             :   const char      *id;
      44                 :             :   JsonNode        *identity;
      45                 :             :   char            *name;
      46                 :             :   GSettings       *settings;
      47                 :             : } ValentChannelServicePrivate;
      48                 :             : 
      49   [ +  +  +  - ]:         738 : G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ValentChannelService, valent_channel_service, VALENT_TYPE_EXTENSION);
      50                 :             : 
      51                 :             : typedef enum {
      52                 :             :   PROP_CERTIFICATE = 1,
      53                 :             :   PROP_ID,
      54                 :             :   PROP_IDENTITY,
      55                 :             : } ValentChannelServiceProperty;
      56                 :             : 
      57                 :             : static GParamSpec *properties[PROP_IDENTITY + 1] = { NULL, };
      58                 :             : 
      59                 :             : typedef enum {
      60                 :             :   CHANNEL,
      61                 :             : } ValentChannelServiceSignal;
      62                 :             : 
      63                 :             : static guint signals[CHANNEL + 1] = { 0, };
      64                 :             : 
      65                 :             : 
      66                 :             : typedef struct
      67                 :             : {
      68                 :             :   GRecMutex      lock;
      69                 :             :   GWeakRef       service;
      70                 :             :   ValentChannel *channel;
      71                 :             : } ChannelEmission;
      72                 :             : 
      73                 :             : static gboolean
      74                 :           3 : valent_channel_service_channel_main (gpointer data)
      75                 :             : {
      76                 :           3 :   ChannelEmission *emission = data;
      77                 :           6 :   g_autoptr (ValentChannelService) service = NULL;
      78                 :             : 
      79         [ -  + ]:           3 :   g_assert (VALENT_IS_MAIN_THREAD ());
      80                 :             : 
      81                 :           3 :   g_rec_mutex_lock (&emission->lock);
      82         [ +  - ]:           3 :   if ((service = g_weak_ref_get (&emission->service)) != NULL)
      83                 :           3 :     valent_channel_service_channel (service, emission->channel);
      84                 :             : 
      85                 :           3 :   g_weak_ref_clear (&emission->service);
      86         [ +  - ]:           3 :   g_clear_object (&emission->channel);
      87                 :           3 :   g_rec_mutex_unlock (&emission->lock);
      88                 :           3 :   g_rec_mutex_clear (&emission->lock);
      89                 :           3 :   g_clear_pointer (&emission, g_free);
      90                 :             : 
      91         [ +  - ]:           3 :   return G_SOURCE_REMOVE;
      92                 :             : }
      93                 :             : 
      94                 :             : /*
      95                 :             :  * Identity Packet Helpers
      96                 :             :  */
      97                 :             : static const char *
      98                 :          23 : get_chassis_type (void)
      99                 :             : {
     100                 :          23 :   static size_t guard = 0;
     101                 :          23 :   static char *chassis = NULL;
     102                 :             : 
     103   [ +  +  +  - ]:          23 :   if (g_once_init_enter (&guard))
     104                 :             :     {
     105                 :           6 :       g_autoptr (GDBusConnection) connection = NULL;
     106         [ +  + ]:           6 :       g_autoptr (GVariant) reply = NULL;
     107         [ -  + ]:           6 :       g_autofree char *str = NULL;
     108                 :           6 :       uint64_t type;
     109                 :             : 
     110                 :           6 :       connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
     111                 :             : 
     112         [ +  + ]:           6 :       if (connection == NULL)
     113                 :           5 :         VALENT_GOTO (dmi_fallback);
     114                 :             : 
     115                 :           1 :       reply = g_dbus_connection_call_sync (connection,
     116                 :             :                                            "org.freedesktop.hostname1",
     117                 :             :                                            "/org/freedesktop/hostname1",
     118                 :             :                                            "org.freedesktop.DBus.Properties",
     119                 :             :                                            "Get",
     120                 :             :                                            g_variant_new ("(ss)",
     121                 :             :                                                           "org.freedesktop.hostname1",
     122                 :             :                                                           "Chassis"),
     123                 :             :                                            G_VARIANT_TYPE ("(v)"),
     124                 :             :                                            G_DBUS_CALL_FLAGS_NONE,
     125                 :             :                                            -1,
     126                 :             :                                            NULL,
     127                 :             :                                            NULL);
     128                 :             : 
     129         [ -  + ]:           1 :       if (reply != NULL)
     130                 :             :         {
     131                 :           0 :           g_autoptr (GVariant) value = NULL;
     132                 :             : 
     133                 :           0 :           g_variant_get (reply, "(v)", &value);
     134                 :           0 :           g_variant_get (value, "s", &chassis);
     135                 :             : 
     136                 :             :           /* NOTE: "phone" is the KDE Connect deviceType */
     137         [ #  # ]:           0 :           if (g_str_equal (chassis, "handset"))
     138                 :           0 :             g_set_str (&chassis, "phone");
     139                 :             : 
     140         [ #  # ]:           0 :           VALENT_GOTO (leave);
     141                 :             :         }
     142                 :             : 
     143                 :             :     /* Fallback to DMI. See the SMBIOS Specification 3.0 section 7.4.1:
     144                 :             :      * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.0.0.pdf
     145                 :             :      */
     146                 :           1 :     dmi_fallback:
     147   [ +  -  +  - ]:          12 :       if (!g_file_get_contents ("/sys/class/dmi/id/chassis_type", &str, NULL, NULL) ||
     148                 :           6 :           !g_ascii_string_to_unsigned (str, 10, 0, G_MAXUINT64, &type, NULL))
     149                 :           6 :         type = 0x3;
     150                 :             : 
     151   [ +  -  -  -  :           6 :       switch (type)
                      - ]
     152                 :             :         {
     153                 :           6 :         case 0x3: /* Desktop */
     154                 :             :         case 0x4: /* Low Profile Desktop */
     155                 :             :         case 0x6: /* Mini Tower */
     156                 :             :         case 0x7: /* Tower */
     157                 :           6 :           g_set_str (&chassis, "desktop");
     158                 :           6 :           break;
     159                 :             : 
     160                 :           0 :         case 0x8: /* Portable */
     161                 :             :         case 0x9: /* Laptop */
     162                 :             :         case 0xA: /* Notebook */
     163                 :             :         case 0xE: /* Sub Notebook */
     164                 :           0 :           g_set_str (&chassis, "laptop");
     165                 :           0 :           break;
     166                 :             : 
     167                 :           0 :         case 0xB: /* Hand Held */
     168                 :           0 :           g_set_str (&chassis, "phone");
     169                 :           0 :           break;
     170                 :             : 
     171                 :           0 :         case 0x1E: /* Tablet */
     172                 :           0 :           g_set_str (&chassis, "tablet");
     173                 :           0 :           break;
     174                 :             : 
     175                 :           0 :         default:
     176                 :           0 :           g_set_str (&chassis, "desktop");
     177                 :             :         }
     178                 :             : 
     179                 :           6 :     leave:
     180                 :           6 :       g_once_init_leave (&guard, 1);
     181                 :             :     }
     182                 :             : 
     183                 :          23 :   return chassis;
     184                 :             : }
     185                 :             : 
     186                 :             : /**
     187                 :             :  * collect_capabilities:
     188                 :             :  * @info: a `PeasPluginInfo`
     189                 :             :  * @incoming: a `GHashTable`
     190                 :             :  * @outgoing: a `GHashTable`
     191                 :             :  *
     192                 :             :  * Collect the capabilities from @info and add them to @incoming and @outgoing,
     193                 :             :  * using g_hash_table_add() to coalesce duplicates.
     194                 :             :  */
     195                 :             : static inline void
     196                 :         125 : collect_capabilities (PeasPluginInfo *info,
     197                 :             :                       GHashTable     *incoming,
     198                 :             :                       GHashTable     *outgoing)
     199                 :             : {
     200                 :         125 :   const char *data = NULL;
     201                 :             : 
     202         [ +  + ]:         125 :   if ((data = peas_plugin_info_get_external_data (info, "DevicePluginIncoming")) != NULL)
     203                 :             :     {
     204                 :          68 :       g_autofree char **capabilities = NULL;
     205                 :             : 
     206                 :          68 :       capabilities = g_strsplit (data, ";", -1);
     207                 :             : 
     208         [ +  + ]:         199 :       for (unsigned int i = 0; capabilities[i] != NULL; i++)
     209                 :         131 :         g_hash_table_add (incoming, g_steal_pointer (&capabilities[i]));
     210                 :             :     }
     211                 :             : 
     212         [ +  + ]:         125 :   if ((data = peas_plugin_info_get_external_data (info, "DevicePluginOutgoing")) != NULL)
     213                 :             :     {
     214                 :          68 :       g_autofree char **capabilities = NULL;
     215                 :             : 
     216                 :          68 :       capabilities = g_strsplit (data, ";", -1);
     217                 :             : 
     218         [ +  + ]:         209 :       for (unsigned int i = 0; capabilities[i] != NULL; i++)
     219                 :         141 :         g_hash_table_add (outgoing, g_steal_pointer (&capabilities[i]));
     220                 :             :     }
     221                 :         125 : }
     222                 :             : 
     223                 :             : static void
     224                 :          26 : on_device_name_changed (GSettings            *settings,
     225                 :             :                         const char           *key_name,
     226                 :             :                         ValentChannelService *self)
     227                 :             : {
     228                 :          26 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
     229                 :          26 :   g_autofree char *name = NULL;
     230                 :             : 
     231         [ -  + ]:          26 :   g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (self));
     232                 :             : 
     233                 :          26 :   name = g_settings_get_string (settings, "name");
     234   [ +  -  +  + ]:          26 :   if (name == NULL || *name == '\0')
     235                 :             :     {
     236                 :           6 :       g_settings_set_string (settings, "name", g_get_host_name ());
     237                 :           6 :       return;
     238                 :             :     }
     239                 :             : 
     240         [ +  - ]:          20 :   if (g_set_str (&priv->name, name))
     241                 :             :     {
     242                 :          20 :       valent_object_lock (VALENT_OBJECT (self));
     243         [ -  + ]:          20 :       if (priv->identity)
     244                 :             :         {
     245                 :           0 :           JsonObject *body = valent_packet_get_body (priv->identity);
     246                 :           0 :           json_object_set_string_member (body, "deviceName", priv->name);
     247                 :           0 :           g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_IDENTITY]);
     248                 :             :         }
     249                 :          20 :       valent_object_unlock (VALENT_OBJECT (self));
     250                 :             :     }
     251                 :             : }
     252                 :             : 
     253                 :             : /* LCOV_EXCL_START */
     254                 :             : static void
     255                 :             : valent_channel_service_real_build_identity (ValentChannelService *service)
     256                 :             : {
     257                 :             :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
     258                 :             :   PeasEngine *engine;
     259                 :             :   g_autoptr (JsonBuilder) builder = NULL;
     260                 :             :   g_autoptr (GHashTable) incoming = NULL;
     261                 :             :   g_autoptr (GHashTable) outgoing = NULL;
     262                 :             :   GHashTableIter in_iter, out_iter;
     263                 :             :   const char *capability = NULL;
     264                 :             :   unsigned int n_plugins = 0;
     265                 :             : 
     266                 :             :   g_assert (VALENT_IS_CHANNEL_SERVICE (service));
     267                 :             : 
     268                 :             :   /* Filter the supported plugins and collect their capabilities */
     269                 :             :   incoming = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
     270                 :             :   outgoing = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
     271                 :             : 
     272                 :             :   engine = valent_get_plugin_engine ();
     273                 :             :   n_plugins = g_list_model_get_n_items (G_LIST_MODEL (engine));
     274                 :             : 
     275                 :             :   for (unsigned int i = 0; i < n_plugins; i++)
     276                 :             :     {
     277                 :             :       g_autoptr (PeasPluginInfo) info = NULL;
     278                 :             : 
     279                 :             :       info = g_list_model_get_item (G_LIST_MODEL (engine), i);
     280                 :             :       collect_capabilities (info, incoming, outgoing);
     281                 :             :     }
     282                 :             : 
     283                 :             :   /* Build the identity packet */
     284                 :             :   builder = json_builder_new ();
     285                 :             :   json_builder_begin_object (builder);
     286                 :             : 
     287                 :             :   /* Packet */
     288                 :             :   json_builder_set_member_name (builder, "id");
     289                 :             :   json_builder_add_int_value (builder, 0);
     290                 :             :   json_builder_set_member_name (builder, "type");
     291                 :             :   json_builder_add_string_value (builder, "kdeconnect.identity");
     292                 :             : 
     293                 :             :   /* Body */
     294                 :             :   json_builder_set_member_name (builder, "body");
     295                 :             :   json_builder_begin_object (builder);
     296                 :             : 
     297                 :             :   /* Metadata */
     298                 :             :   json_builder_set_member_name (builder, "deviceId");
     299                 :             :   json_builder_add_string_value (builder, priv->id);
     300                 :             :   json_builder_set_member_name (builder, "deviceName");
     301                 :             :   json_builder_add_string_value (builder, priv->name);
     302                 :             :   json_builder_set_member_name (builder, "deviceType");
     303                 :             :   json_builder_add_string_value (builder, get_chassis_type());
     304                 :             :   json_builder_set_member_name (builder, "protocolVersion");
     305                 :             :   json_builder_add_int_value (builder, VALENT_NETWORK_PROTOCOL_MAX);
     306                 :             : 
     307                 :             :   /* Incoming Capabilities */
     308                 :             :   json_builder_set_member_name (builder, "incomingCapabilities");
     309                 :             :   json_builder_begin_array (builder);
     310                 :             : 
     311                 :             :   g_hash_table_iter_init (&in_iter, incoming);
     312                 :             : 
     313                 :             :   while (g_hash_table_iter_next (&in_iter, (void **)&capability, NULL))
     314                 :             :     json_builder_add_string_value (builder, capability);
     315                 :             : 
     316                 :             :   json_builder_end_array (builder);
     317                 :             : 
     318                 :             :   /* Outgoing Capabilities */
     319                 :             :   json_builder_set_member_name (builder, "outgoingCapabilities");
     320                 :             :   json_builder_begin_array (builder);
     321                 :             : 
     322                 :             :   g_hash_table_iter_init (&out_iter, outgoing);
     323                 :             : 
     324                 :             :   while (g_hash_table_iter_next (&out_iter, (void **)&capability, NULL))
     325                 :             :     json_builder_add_string_value (builder, capability);
     326                 :             : 
     327                 :             :   json_builder_end_array (builder);
     328                 :             : 
     329                 :             :   /* End Body, Packet */
     330                 :             :   json_builder_end_object (builder);
     331                 :             :   json_builder_end_object (builder);
     332                 :             : 
     333                 :             : 
     334                 :             :   /* Store the identity */
     335                 :             :   valent_object_lock (VALENT_OBJECT (service));
     336                 :             :   g_clear_pointer (&priv->identity, json_node_unref);
     337                 :             :   priv->identity = json_builder_get_root (builder);
     338                 :             :   valent_object_unlock (VALENT_OBJECT (service));
     339                 :             : }
     340                 :             : 
     341                 :             : static void
     342                 :             : valent_channel_service_real_identify (ValentChannelService *service,
     343                 :             :                                       const char           *target)
     344                 :             : {
     345                 :             :   g_assert (VALENT_IS_CHANNEL_SERVICE (service));
     346                 :             : }
     347                 :             : /* LCOV_EXCL_STOP */
     348                 :             : 
     349                 :             : /*
     350                 :             :  * GObject
     351                 :             :  */
     352                 :             : static void
     353                 :          20 : valent_channel_service_constructed (GObject *object)
     354                 :             : {
     355                 :          20 :   ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
     356                 :          20 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
     357                 :             : 
     358                 :          20 :   G_OBJECT_CLASS (valent_channel_service_parent_class)->constructed (object);
     359                 :             : 
     360                 :          20 :   valent_object_lock (VALENT_OBJECT (self));
     361         [ +  - ]:          20 :   if (priv->certificate == NULL)
     362                 :             :     {
     363                 :          40 :       g_autoptr (ValentContext) context = NULL;
     364         [ +  - ]:          20 :       g_autoptr (GFile) config = NULL;
     365                 :             : 
     366                 :          20 :       context = valent_context_new (NULL, NULL, NULL);
     367                 :          20 :       config = valent_context_get_config_file (context, ".");
     368         [ +  - ]:          20 :       priv->certificate = valent_certificate_new_sync (g_file_peek_path (config),
     369                 :             :                                                        NULL);
     370                 :             :     }
     371                 :             : 
     372                 :          20 :   priv->id = valent_certificate_get_common_name (priv->certificate);
     373                 :          20 :   priv->settings = g_settings_new ("ca.andyholmes.Valent");
     374                 :          20 :   g_signal_connect_object (priv->settings,
     375                 :             :                            "changed::name",
     376                 :             :                            G_CALLBACK (on_device_name_changed),
     377                 :             :                            self,
     378                 :             :                            G_CONNECT_DEFAULT);
     379                 :          20 :   on_device_name_changed (priv->settings, NULL, self);
     380                 :          20 :   valent_object_unlock (VALENT_OBJECT (self));
     381                 :             : 
     382                 :          20 :   valent_channel_service_build_identity (self);
     383                 :          20 : }
     384                 :             : 
     385                 :             : static void
     386                 :          20 : valent_channel_service_finalize (GObject *object)
     387                 :             : {
     388                 :          20 :   ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
     389                 :          20 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
     390                 :             : 
     391                 :          20 :   valent_object_lock (VALENT_OBJECT (self));
     392         [ +  - ]:          20 :   g_clear_object (&priv->certificate);
     393         [ +  - ]:          20 :   g_clear_pointer (&priv->identity, json_node_unref);
     394         [ +  - ]:          20 :   g_clear_pointer (&priv->name, g_free);
     395         [ +  - ]:          20 :   g_clear_object (&priv->settings);
     396                 :          20 :   valent_object_unlock (VALENT_OBJECT (self));
     397                 :             : 
     398                 :          20 :   G_OBJECT_CLASS (valent_channel_service_parent_class)->finalize (object);
     399                 :          20 : }
     400                 :             : 
     401                 :             : static void
     402                 :           6 : valent_channel_service_get_property (GObject    *object,
     403                 :             :                                      guint       prop_id,
     404                 :             :                                      GValue     *value,
     405                 :             :                                      GParamSpec *pspec)
     406                 :             : {
     407                 :           6 :   ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
     408                 :           6 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
     409                 :             : 
     410   [ +  +  +  - ]:           6 :   switch ((ValentChannelServiceProperty)prop_id)
     411                 :             :     {
     412                 :             :     case PROP_CERTIFICATE:
     413                 :           1 :       valent_object_lock (VALENT_OBJECT (self));
     414                 :           1 :       g_value_set_object (value, priv->certificate);
     415                 :           1 :       valent_object_unlock (VALENT_OBJECT (self));
     416                 :           1 :       break;
     417                 :             : 
     418                 :             :     case PROP_ID:
     419                 :           1 :       valent_object_lock (VALENT_OBJECT (self));
     420                 :           1 :       g_value_set_string (value, priv->id);
     421                 :           1 :       valent_object_unlock (VALENT_OBJECT (self));
     422                 :           1 :       break;
     423                 :             : 
     424                 :             :     case PROP_IDENTITY:
     425                 :           4 :       valent_object_lock (VALENT_OBJECT (self));
     426                 :           4 :       g_value_set_boxed (value, priv->identity);
     427                 :           4 :       valent_object_unlock (VALENT_OBJECT (self));
     428                 :           4 :       break;
     429                 :             : 
     430                 :           0 :     default:
     431                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     432                 :             :     }
     433                 :           6 : }
     434                 :             : 
     435                 :             : static void
     436                 :          20 : valent_channel_service_set_property (GObject      *object,
     437                 :             :                                      guint         prop_id,
     438                 :             :                                      const GValue *value,
     439                 :             :                                      GParamSpec   *pspec)
     440                 :             : {
     441                 :          20 :   ValentChannelService *self = VALENT_CHANNEL_SERVICE (object);
     442                 :          20 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (self);
     443                 :             : 
     444         [ +  - ]:          20 :   switch ((ValentChannelServiceProperty)prop_id)
     445                 :             :     {
     446                 :             :     case PROP_CERTIFICATE:
     447                 :          20 :       valent_object_lock (VALENT_OBJECT (self));
     448                 :          20 :       priv->certificate = g_value_dup_object (value);
     449                 :          20 :       valent_object_unlock (VALENT_OBJECT (self));
     450                 :          20 :       break;
     451                 :             : 
     452                 :           0 :     case PROP_ID:
     453                 :             :     case PROP_IDENTITY:
     454                 :             :     default:
     455                 :           0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     456                 :             :     }
     457                 :          20 : }
     458                 :             : 
     459                 :             : static void
     460                 :          51 : valent_channel_service_class_init (ValentChannelServiceClass *klass)
     461                 :             : {
     462                 :          51 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     463                 :          51 :   ValentChannelServiceClass *service_class = VALENT_CHANNEL_SERVICE_CLASS (klass);
     464                 :             : 
     465                 :          51 :   object_class->constructed = valent_channel_service_constructed;
     466                 :          51 :   object_class->finalize = valent_channel_service_finalize;
     467                 :          51 :   object_class->get_property = valent_channel_service_get_property;
     468                 :          51 :   object_class->set_property = valent_channel_service_set_property;
     469                 :             : 
     470                 :          51 :   service_class->build_identity = valent_channel_service_real_build_identity;
     471                 :          51 :   service_class->identify = valent_channel_service_real_identify;
     472                 :             : 
     473                 :             :   /**
     474                 :             :    * ValentChannelService:certificate: (getter ref_certificate)
     475                 :             :    *
     476                 :             :    * The TLS certificate the service uses to authenticate with other devices.
     477                 :             :    */
     478                 :         102 :   properties [PROP_CERTIFICATE] =
     479                 :          51 :     g_param_spec_object ("certificate", NULL, NULL,
     480                 :             :                          G_TYPE_TLS_CERTIFICATE,
     481                 :             :                          (G_PARAM_READWRITE |
     482                 :             :                           G_PARAM_CONSTRUCT_ONLY |
     483                 :             :                           G_PARAM_EXPLICIT_NOTIFY |
     484                 :             :                           G_PARAM_STATIC_STRINGS));
     485                 :             : 
     486                 :             :   /**
     487                 :             :    * ValentChannelService:id: (getter dup_id)
     488                 :             :    *
     489                 :             :    * The local ID.
     490                 :             :    *
     491                 :             :    * This is the ID used to identify the local device, which should be unique
     492                 :             :    * among devices in a given network.
     493                 :             :    *
     494                 :             :    * This property is thread-safe. Emissions of [signal@GObject.Object::notify]
     495                 :             :    * are guaranteed to happen in the main thread.
     496                 :             :    *
     497                 :             :    * Since: 1.0
     498                 :             :    */
     499                 :         102 :   properties [PROP_ID] =
     500                 :          51 :     g_param_spec_string ("id", NULL, NULL,
     501                 :             :                          NULL,
     502                 :             :                          (G_PARAM_READABLE |
     503                 :             :                           G_PARAM_EXPLICIT_NOTIFY |
     504                 :             :                           G_PARAM_STATIC_STRINGS));
     505                 :             : 
     506                 :             :   /**
     507                 :             :    * ValentChannelService:identity: (getter ref_identity)
     508                 :             :    *
     509                 :             :    * The local identity packet.
     510                 :             :    *
     511                 :             :    * This is the identity packet sent by the [class@Valent.ChannelService]
     512                 :             :    * implementation to describe the local device.
     513                 :             :    *
     514                 :             :    * This property is thread-safe. Emissions of [signal@GObject.Object::notify]
     515                 :             :    * are guaranteed to happen in the main thread.
     516                 :             :    *
     517                 :             :    * Since: 1.0
     518                 :             :    */
     519                 :         102 :   properties [PROP_IDENTITY] =
     520                 :          51 :     g_param_spec_boxed ("identity", NULL, NULL,
     521                 :             :                         JSON_TYPE_NODE,
     522                 :             :                         (G_PARAM_READABLE |
     523                 :             :                          G_PARAM_EXPLICIT_NOTIFY |
     524                 :             :                          G_PARAM_STATIC_STRINGS));
     525                 :             : 
     526                 :          51 :   g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
     527                 :             : 
     528                 :             :   /**
     529                 :             :    * ValentChannelService::channel:
     530                 :             :    * @service: a `ValentChannelService`
     531                 :             :    * @channel: a `ValentChannel`
     532                 :             :    *
     533                 :             :    * Emitted when a new channel has been negotiated.
     534                 :             :    *
     535                 :             :    * In practice, when this is emitted a [class@Valent.DeviceManager] will
     536                 :             :    * ensure a [class@Valent.Device] exists to take ownership of @channel.
     537                 :             :    *
     538                 :             :    * Since: 1.0
     539                 :             :    */
     540                 :         102 :   signals [CHANNEL] =
     541                 :          51 :     g_signal_new ("channel",
     542                 :             :                   G_TYPE_FROM_CLASS (klass),
     543                 :             :                   G_SIGNAL_RUN_LAST,
     544                 :             :                   G_STRUCT_OFFSET (ValentChannelServiceClass, channel),
     545                 :             :                   NULL, NULL,
     546                 :             :                   g_cclosure_marshal_VOID__OBJECT,
     547                 :             :                   G_TYPE_NONE, 1, VALENT_TYPE_CHANNEL);
     548                 :          51 :   g_signal_set_va_marshaller (signals [CHANNEL],
     549                 :             :                               G_TYPE_FROM_CLASS (klass),
     550                 :             :                               g_cclosure_marshal_VOID__OBJECTv);
     551                 :          51 : }
     552                 :             : 
     553                 :             : static void
     554                 :          20 : valent_channel_service_init (ValentChannelService *self)
     555                 :             : {
     556                 :          20 : }
     557                 :             : 
     558                 :             : /**
     559                 :             :  * valent_channel_service_ref_certificate: (get-property certificate)
     560                 :             :  * @self: a `ValentChannelService`
     561                 :             :  *
     562                 :             :  * Get the TLS certificate for the service.
     563                 :             :  *
     564                 :             :  * Returns: (transfer full) (not nullable): the service TLS certificate
     565                 :             :  */
     566                 :             : GTlsCertificate *
     567                 :           8 : valent_channel_service_ref_certificate (ValentChannelService *service)
     568                 :             : {
     569                 :           8 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
     570                 :           8 :   GTlsCertificate *ret = NULL;
     571                 :             : 
     572         [ -  + ]:           8 :   g_return_val_if_fail (VALENT_IS_CHANNEL_SERVICE (service), NULL);
     573                 :             : 
     574                 :           8 :   valent_object_lock (VALENT_OBJECT (service));
     575                 :           8 :   ret = g_object_ref (priv->certificate);
     576                 :           8 :   valent_object_unlock (VALENT_OBJECT (service));
     577                 :             : 
     578                 :           8 :   return g_steal_pointer (&ret);
     579                 :             : }
     580                 :             : 
     581                 :             : /**
     582                 :             :  * valent_channel_service_dup_id: (get-property id)
     583                 :             :  * @service: a `ValentChannelService`
     584                 :             :  *
     585                 :             :  * Get the local ID.
     586                 :             :  *
     587                 :             :  * Returns: (transfer full) (not nullable): the service ID
     588                 :             :  *
     589                 :             :  * Since: 1.0
     590                 :             :  */
     591                 :             : char *
     592                 :          24 : valent_channel_service_dup_id (ValentChannelService *service)
     593                 :             : {
     594                 :          24 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
     595                 :          24 :   char *ret;
     596                 :             : 
     597         [ -  + ]:          24 :   g_return_val_if_fail (VALENT_IS_CHANNEL_SERVICE (service), NULL);
     598                 :             : 
     599                 :          24 :   valent_object_lock (VALENT_OBJECT (service));
     600         [ -  + ]:          24 :   ret = g_strdup (priv->id);
     601                 :          24 :   valent_object_unlock (VALENT_OBJECT (service));
     602                 :             : 
     603                 :          24 :   return g_steal_pointer (&ret);
     604                 :             : }
     605                 :             : 
     606                 :             : /**
     607                 :             :  * valent_channel_service_ref_identity: (get-property identity)
     608                 :             :  * @service: a `ValentChannelService`
     609                 :             :  *
     610                 :             :  * Get the local identity packet.
     611                 :             :  *
     612                 :             :  * Returns: (transfer full): a KDE Connect packet
     613                 :             :  *
     614                 :             :  * Since: 1.0
     615                 :             :  */
     616                 :             : JsonNode *
     617                 :          25 : valent_channel_service_ref_identity (ValentChannelService *service)
     618                 :             : {
     619                 :          25 :   ValentChannelServicePrivate *priv = valent_channel_service_get_instance_private (service);
     620                 :          25 :   JsonNode *ret;
     621                 :             : 
     622         [ -  + ]:          25 :   g_return_val_if_fail (VALENT_IS_CHANNEL_SERVICE (service), NULL);
     623                 :             : 
     624                 :          25 :   valent_object_lock (VALENT_OBJECT (service));
     625                 :          25 :   ret = json_node_ref (priv->identity);
     626                 :          25 :   valent_object_unlock (VALENT_OBJECT (service));
     627                 :             : 
     628                 :          25 :   return g_steal_pointer (&ret);
     629                 :             : }
     630                 :             : 
     631                 :             : /**
     632                 :             :  * valent_channel_service_build_identity: (virtual build_identity)
     633                 :             :  * @service: a `ValentChannelService`
     634                 :             :  *
     635                 :             :  * Rebuild the local KDE Connect identity packet.
     636                 :             :  *
     637                 :             :  * This method is called to rebuild the identity packet used to identify the
     638                 :             :  * host device to remote devices.
     639                 :             :  *
     640                 :             :  * Implementations that override [vfunc@Valent.ChannelService.build_identity]
     641                 :             :  * should chain-up first, then call [method@Valent.ChannelService.ref_identity]
     642                 :             :  * and modify that.
     643                 :             :  *
     644                 :             :  * Since: 1.0
     645                 :             :  */
     646                 :             : void
     647                 :          23 : valent_channel_service_build_identity (ValentChannelService *service)
     648                 :             : {
     649                 :          23 :   VALENT_ENTRY;
     650                 :             : 
     651         [ -  + ]:          23 :   g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (service));
     652                 :             : 
     653                 :          23 :   valent_object_lock (VALENT_OBJECT (service));
     654                 :          23 :   VALENT_CHANNEL_SERVICE_GET_CLASS (service)->build_identity (service);
     655                 :          23 :   valent_object_unlock (VALENT_OBJECT (service));
     656                 :             : 
     657                 :          23 :   VALENT_EXIT;
     658                 :             : }
     659                 :             : 
     660                 :             : /**
     661                 :             :  * valent_channel_service_identify: (virtual identify)
     662                 :             :  * @service: a `ValentChannelService`
     663                 :             :  * @target: (nullable): a target string
     664                 :             :  *
     665                 :             :  * Identify the host device to the network.
     666                 :             :  *
     667                 :             :  * This method is called to announce the availability of the host device to
     668                 :             :  * other devices.
     669                 :             :  *
     670                 :             :  * Implementations that override [vfunc@Valent.ChannelService.identify] may
     671                 :             :  * ignore @target or use it to address a particular device.
     672                 :             :  *
     673                 :             :  * Since: 1.0
     674                 :             :  */
     675                 :             : void
     676                 :           6 : valent_channel_service_identify (ValentChannelService *service,
     677                 :             :                                  const char           *target)
     678                 :             : {
     679                 :           6 :   VALENT_ENTRY;
     680                 :             : 
     681         [ -  + ]:           6 :   g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (service));
     682                 :             : 
     683                 :           6 :   VALENT_CHANNEL_SERVICE_GET_CLASS (service)->identify (service, target);
     684                 :             : 
     685                 :           6 :   VALENT_EXIT;
     686                 :             : }
     687                 :             : 
     688                 :             : /**
     689                 :             :  * valent_channel_service_channel:
     690                 :             :  * @service: a `ValentChannelService`
     691                 :             :  * @channel: a `ValentChannel`
     692                 :             :  *
     693                 :             :  * Emit [signal@Valent.ChannelService::channel] on @service.
     694                 :             :  *
     695                 :             :  * This method should only be called by implementations of
     696                 :             :  * [class@Valent.ChannelService].
     697                 :             :  *
     698                 :             :  * Since: 1.0
     699                 :             :  */
     700                 :             : void
     701                 :          11 : valent_channel_service_channel (ValentChannelService *service,
     702                 :             :                                 ValentChannel        *channel)
     703                 :             : {
     704                 :          11 :   ChannelEmission *emission;
     705                 :             : 
     706         [ -  + ]:          11 :   g_return_if_fail (VALENT_IS_CHANNEL_SERVICE (service));
     707         [ +  - ]:          11 :   g_return_if_fail (VALENT_IS_CHANNEL (channel));
     708                 :             : 
     709         [ +  + ]:          11 :   if G_LIKELY (VALENT_IS_MAIN_THREAD ())
     710                 :             :     {
     711                 :           8 :       g_signal_emit (G_OBJECT (service), signals [CHANNEL], 0, channel);
     712                 :           8 :       return;
     713                 :             :     }
     714                 :             : 
     715                 :           3 :   emission = g_new0 (ChannelEmission, 1);
     716                 :           3 :   g_rec_mutex_init (&emission->lock);
     717                 :           3 :   g_rec_mutex_lock (&emission->lock);
     718                 :           3 :   g_weak_ref_init (&emission->service, service);
     719                 :           3 :   emission->channel = g_object_ref (channel);
     720                 :           3 :   g_rec_mutex_unlock (&emission->lock);
     721                 :             : 
     722                 :           3 :   g_timeout_add (0, valent_channel_service_channel_main, emission);
     723                 :             : }
     724                 :             : 
        

Generated by: LCOV version 2.0-1