1
0
Fork 0

clipboard-manager: Cancel selection transfers

When there is a new owner but there is no matching mime type we clear
the saved mimetype and the saved clipboard but an outstanding async
meta_selection_transfer_async can set the saved clipboard.

Abort the async transfer when we have a new owner.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3678>
This commit is contained in:
Sebastian Wick 2024-04-15 15:29:23 +02:00 committed by Marge Bot
parent ce32d2b77a
commit cad9a65b07
2 changed files with 13 additions and 7 deletions

View file

@ -154,6 +154,7 @@ struct _MetaDisplay
GBytes *saved_clipboard; GBytes *saved_clipboard;
gchar *saved_clipboard_mimetype; gchar *saved_clipboard_mimetype;
MetaSelection *selection; MetaSelection *selection;
GCancellable *saved_clipboard_cancellable;
}; };
struct _MetaDisplayClass struct _MetaDisplayClass

View file

@ -65,23 +65,23 @@ mimetype_match (const char *mimetype,
static void static void
transfer_cb (MetaSelection *selection, transfer_cb (MetaSelection *selection,
GAsyncResult *result, GAsyncResult *result,
GOutputStream *output) GOutputStream *output_stream)
{ {
MetaDisplay *display = meta_selection_get_display (selection); MetaDisplay *display = meta_selection_get_display (selection);
GError *error = NULL; g_autoptr (GOutputStream) output = output_stream;
g_autoptr (GError) error = NULL;
if (!meta_selection_transfer_finish (selection, result, &error)) if (!meta_selection_transfer_finish (selection, result, &error))
{ {
g_warning ("Failed to store clipboard: %s", error->message); if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_error_free (error); g_warning ("Failed to store clipboard: %s", error->message);
g_object_unref (output);
return; return;
} }
g_output_stream_close (output, NULL, NULL); g_output_stream_close (output, NULL, NULL);
display->saved_clipboard = display->saved_clipboard =
g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (output)); g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (output));
g_object_unref (output);
} }
static void static void
@ -104,6 +104,8 @@ owner_changed_cb (MetaSelection *selection,
/* New selection source, find the best mimetype in order to /* New selection source, find the best mimetype in order to
* keep a copy of it. * keep a copy of it.
*/ */
g_cancellable_cancel (display->saved_clipboard_cancellable);
g_clear_object (&display->saved_clipboard_cancellable);
g_clear_object (&display->selection_source); g_clear_object (&display->selection_source);
g_clear_pointer (&display->saved_clipboard_mimetype, g_free); g_clear_pointer (&display->saved_clipboard_mimetype, g_free);
g_clear_pointer (&display->saved_clipboard, g_bytes_unref); g_clear_pointer (&display->saved_clipboard, g_bytes_unref);
@ -135,12 +137,13 @@ owner_changed_cb (MetaSelection *selection,
display->saved_clipboard_mimetype = g_strdup (best); display->saved_clipboard_mimetype = g_strdup (best);
g_list_free_full (mimetypes, g_free); g_list_free_full (mimetypes, g_free);
output = g_memory_output_stream_new_resizable (); output = g_memory_output_stream_new_resizable ();
display->saved_clipboard_cancellable = g_cancellable_new ();
meta_selection_transfer_async (selection, meta_selection_transfer_async (selection,
META_SELECTION_CLIPBOARD, META_SELECTION_CLIPBOARD,
display->saved_clipboard_mimetype, display->saved_clipboard_mimetype,
transfer_size, transfer_size,
output, output,
NULL, display->saved_clipboard_cancellable,
(GAsyncReadyCallback) transfer_cb, (GAsyncReadyCallback) transfer_cb,
output); output);
} }
@ -182,6 +185,8 @@ meta_clipboard_manager_shutdown (MetaDisplay *display)
{ {
MetaSelection *selection; MetaSelection *selection;
g_cancellable_cancel (display->saved_clipboard_cancellable);
g_clear_object (&display->saved_clipboard_cancellable);
g_clear_object (&display->selection_source); g_clear_object (&display->selection_source);
g_clear_pointer (&display->saved_clipboard, g_bytes_unref); g_clear_pointer (&display->saved_clipboard, g_bytes_unref);
g_clear_pointer (&display->saved_clipboard_mimetype, g_free); g_clear_pointer (&display->saved_clipboard_mimetype, g_free);