[gnome-remote-desktop] session: Move 'ReadMimeTypeContent' to clipboard class



commit 8105215c78387a5e39eedf1f462570e32fa446e5
Author: Pascal Nowack <Pascal Nowack gmx de>
Date:   Sun Apr 25 19:54:44 2021 +0200

    session: Move 'ReadMimeTypeContent' to clipboard class
    
    Reading the mime type content from a fd is a task for the clipboard.
    Handling the read() operation in an async way should therefore be done
    in grd-clipboard, as it will introduce more code, that is specific to
    the clipboard.
    
    So, move this part into grd-clipboard as another preparation for async
    read() operations.
    Functionality wise, nothing changes in this commit.
    grd_session_selection_read() will now return the read fd, instead of
    the read mime type content.

 src/grd-clipboard.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/grd-session.c   | 42 +++++----------------------------------
 src/grd-session.h   |  5 ++---
 3 files changed, 61 insertions(+), 43 deletions(-)
---
diff --git a/src/grd-clipboard.c b/src/grd-clipboard.c
index cbc4702..cd39d2a 100644
--- a/src/grd-clipboard.c
+++ b/src/grd-clipboard.c
@@ -21,6 +21,8 @@
 
 #include "grd-clipboard.h"
 
+#include <glib-unix.h>
+
 #include "grd-session.h"
 
 typedef struct _GrdClipboardPrivate
@@ -75,24 +77,73 @@ grd_clipboard_update_server_mime_type_list (GrdClipboard *clipboard,
   g_list_free (mime_type_tables);
 }
 
+static uint8_t *
+read_mime_type_content_sync (int       fd,
+                             uint32_t *size)
+{
+  GArray *data;
+
+  *size = 0;
+  if (fd == -1)
+    return NULL;
+
+  data = g_array_new (FALSE, TRUE, sizeof (uint8_t));
+  while (TRUE)
+    {
+      int len;
+      uint8_t buffer[1024];
+
+      len = read (fd, buffer, G_N_ELEMENTS (buffer));
+      if (len < 0)
+        {
+          if (errno == EAGAIN)
+            continue;
+
+          g_warning ("read() failed: %s", g_strerror (errno));
+          break;
+        }
+      else if (len == 0)
+        {
+          break;
+        }
+      else
+        {
+          g_array_append_vals (data, buffer, len);
+        }
+    }
+  close (fd);
+
+  if (data->len <= 0)
+    {
+      g_array_free (data, TRUE);
+      return NULL;
+    }
+
+  *size = data->len;
+
+  return (uint8_t *) g_array_free (data, FALSE);
+}
+
 void
 grd_clipboard_request_server_content_for_mime_type_async (GrdClipboard *clipboard,
                                                           GrdMimeType   mime_type)
 {
   GrdClipboardClass *klass = GRD_CLIPBOARD_GET_CLASS (clipboard);
   GrdClipboardPrivate *priv = grd_clipboard_get_instance_private (clipboard);
+  int fd;
   uint8_t *data;
   uint32_t size;
 
-  if (!klass->submit_requested_server_content)
-    return;
+  g_return_if_fail (klass->submit_requested_server_content);
 
   if (!priv->enabled)
     return;
 
   g_debug ("Clipboard[SelectionRead]: Requesting data from servers clipboard"
            " (mime type: %s)", grd_mime_type_to_string (mime_type));
-  data = grd_session_selection_read (priv->session, mime_type, &size);
+  fd = grd_session_selection_read (priv->session, mime_type);
+
+  data = read_mime_type_content_sync (fd, &size);
   if (data)
     g_debug ("Clipboard[SelectionRead]: Request successful");
   else
diff --git a/src/grd-session.c b/src/grd-session.c
index de3fff2..8eb46ee 100644
--- a/src/grd-session.c
+++ b/src/grd-session.c
@@ -324,10 +324,9 @@ acquire_fd_from_list (GUnixFDList  *fd_list,
   return fd;
 }
 
-uint8_t *
+int
 grd_session_selection_read (GrdSession  *session,
-                            GrdMimeType  mime_type,
-                            uint32_t    *size)
+                            GrdMimeType  mime_type)
 {
   GrdSessionPrivate *priv = grd_session_get_instance_private (session);
   g_autoptr (GError) error = NULL;
@@ -336,7 +335,6 @@ grd_session_selection_read (GrdSession  *session,
   int fd_idx;
   int fd;
   const char *mime_type_string;
-  GArray *data;
 
   mime_type_string = grd_mime_type_to_string (mime_type);
   if (!grd_dbus_remote_desktop_session_call_selection_read_sync (
@@ -344,7 +342,7 @@ grd_session_selection_read (GrdSession  *session,
          &fd_list, NULL, &error))
     {
       g_warning ("Failed to read selection: %s", error->message);
-      return NULL;
+      return -1;
     }
 
   g_variant_get (fd_variant, "h", &fd_idx);
@@ -352,40 +350,10 @@ grd_session_selection_read (GrdSession  *session,
   if (fd == -1)
     {
       g_warning ("Failed to acquire file descriptor: %s", error->message);
-      return NULL;
-    }
-
-  data = g_array_new (FALSE, TRUE, sizeof (uint8_t));
-  while (TRUE)
-    {
-      int len;
-      uint8_t buffer[1024];
-
-      len = read (fd, buffer, G_N_ELEMENTS (buffer));
-      if (len < 0)
-        {
-          if (errno == EAGAIN)
-            continue;
-
-          g_warning ("read() failed: %s", g_strerror (errno));
-          break;
-        }
-      else if (len == 0)
-        {
-          break;
-        }
-      else
-        {
-          g_array_append_vals (data, buffer, len);
-        }
+      return -1;
     }
 
-  if (data->len >= 0)
-    *size = data->len;
-
-  close (fd);
-
-  return (uint8_t *) g_array_free (data, FALSE);
+  return fd;
 }
 
 static void
diff --git a/src/grd-session.h b/src/grd-session.h
index f4457e3..cc0d136 100644
--- a/src/grd-session.h
+++ b/src/grd-session.h
@@ -110,9 +110,8 @@ void grd_session_disable_clipboard (GrdSession *session);
 void grd_session_set_selection (GrdSession *session,
                                 GList      *mime_type_tables);
 
-uint8_t *grd_session_selection_read (GrdSession  *session,
-                                     GrdMimeType  mime_type,
-                                     uint32_t    *size);
+int grd_session_selection_read (GrdSession  *session,
+                                GrdMimeType  mime_type);
 
 void grd_session_start (GrdSession *session);
 


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