Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/ui/dbus-clipboard.c
Line
Count
Source
1
/*
2
 * QEMU DBus display
3
 *
4
 * Copyright (c) 2021 Marc-André Lureau <marcandre.lureau@redhat.com>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "qemu/osdep.h"
25
#include "qemu/dbus.h"
26
#include "qemu/error-report.h"
27
#include "qemu/main-loop.h"
28
#include "qom/object_interfaces.h"
29
#include "system/system.h"
30
#include "qapi/error.h"
31
#include "trace.h"
32
33
#include "dbus.h"
34
35
0
#define MIME_TEXT_PLAIN_UTF8 "text/plain;charset=utf-8"
36
37
static void
38
dbus_clipboard_complete_request(
39
    DBusDisplay *dpy,
40
    GDBusMethodInvocation *invocation,
41
    QemuClipboardInfo *info,
42
    QemuClipboardType type)
43
0
{
44
0
    GVariant *v_data = g_variant_new_from_data(
45
0
        G_VARIANT_TYPE("ay"),
46
0
        info->types[type].data,
47
0
        info->types[type].size,
48
0
        TRUE,
49
0
        (GDestroyNotify)qemu_clipboard_info_unref,
50
0
        qemu_clipboard_info_ref(info));
51
52
0
    qemu_dbus_display1_clipboard_complete_request(
53
0
        dpy->clipboard, invocation,
54
0
        MIME_TEXT_PLAIN_UTF8, v_data);
55
0
}
56
57
static void
58
dbus_clipboard_update_info(DBusDisplay *dpy, QemuClipboardInfo *info)
59
0
{
60
0
    bool self_update = info->owner == &dpy->clipboard_peer;
61
0
    const char *mime[QEMU_CLIPBOARD_TYPE__COUNT + 1] = { 0, };
62
0
    DBusClipboardRequest *req;
63
0
    int i = 0;
64
65
0
    if (info->owner == NULL) {
66
0
        if (dpy->clipboard_proxy) {
67
0
            qemu_dbus_display1_clipboard_call_release(
68
0
                dpy->clipboard_proxy,
69
0
                info->selection,
70
0
                G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
71
0
        }
72
0
        return;
73
0
    }
74
75
0
    if (self_update || !info->has_serial) {
76
0
        return;
77
0
    }
78
79
0
    req = &dpy->clipboard_request[info->selection];
80
0
    if (req->invocation && info->types[req->type].data) {
81
0
        dbus_clipboard_complete_request(dpy, req->invocation, info, req->type);
82
0
        g_clear_object(&req->invocation);
83
0
        g_clear_handle_id(&req->timeout_id, g_source_remove);
84
0
        return;
85
0
    }
86
87
0
    if (info->types[QEMU_CLIPBOARD_TYPE_TEXT].available) {
88
0
        mime[i++] = MIME_TEXT_PLAIN_UTF8;
89
0
    }
90
91
0
    if (i > 0) {
92
0
        if (dpy->clipboard_proxy) {
93
0
            qemu_dbus_display1_clipboard_call_grab(
94
0
                dpy->clipboard_proxy,
95
0
                info->selection,
96
0
                info->serial,
97
0
                mime,
98
0
                G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
99
0
        }
100
0
    }
101
0
}
102
103
static void
104
dbus_clipboard_reset_serial(DBusDisplay *dpy)
105
0
{
106
0
    if (dpy->clipboard_proxy) {
107
0
        qemu_dbus_display1_clipboard_call_register(
108
0
            dpy->clipboard_proxy,
109
0
            G_DBUS_CALL_FLAGS_NONE,
110
0
            -1, NULL, NULL, NULL);
111
0
    }
112
0
}
113
114
static void
115
dbus_clipboard_notify(Notifier *notifier, void *data)
116
0
{
117
0
    DBusDisplay *dpy =
118
0
        container_of(notifier, DBusDisplay, clipboard_peer.notifier);
119
0
    QemuClipboardNotify *notify = data;
120
121
0
    switch (notify->type) {
122
0
    case QEMU_CLIPBOARD_UPDATE_INFO:
123
0
        dbus_clipboard_update_info(dpy, notify->info);
124
0
        return;
125
0
    case QEMU_CLIPBOARD_RESET_SERIAL:
126
0
        dbus_clipboard_reset_serial(dpy);
127
0
        return;
128
0
    case QEMU_CLIPBOARD_PEER_UPDATE:
129
        /* ignore */
130
0
        return;
131
0
    }
132
0
}
133
134
static void
135
dbus_clipboard_qemu_request(QemuClipboardInfo *info,
136
                            QemuClipboardType type)
137
0
{
138
0
    DBusDisplay *dpy = container_of(info->owner, DBusDisplay, clipboard_peer);
139
0
    g_autofree char *mime = NULL;
140
0
    g_autoptr(GVariant) v_data = NULL;
141
0
    g_autoptr(GError) err = NULL;
142
0
    const char *data = NULL;
143
0
    const char *mimes[] = { MIME_TEXT_PLAIN_UTF8, NULL };
144
0
    size_t n;
145
146
0
    trace_dbus_clipboard_qemu_request(type);
147
148
0
    if (type != QEMU_CLIPBOARD_TYPE_TEXT) {
149
        /* unsupported atm */
150
0
        return;
151
0
    }
152
153
0
    if (dpy->clipboard_proxy) {
154
0
        if (!qemu_dbus_display1_clipboard_call_request_sync(
155
0
                dpy->clipboard_proxy,
156
0
                info->selection,
157
0
                mimes,
158
0
                G_DBUS_CALL_FLAGS_NONE, -1, &mime, &v_data, NULL, &err)) {
159
0
            error_report("Failed to request clipboard: %s", err->message);
160
0
            return;
161
0
        }
162
163
0
        if (g_strcmp0(mime, MIME_TEXT_PLAIN_UTF8)) {
164
0
            error_report("Unsupported returned MIME: %s", mime);
165
0
            return;
166
0
        }
167
168
0
        data = g_variant_get_fixed_array(v_data, &n, 1);
169
0
        qemu_clipboard_set_data(&dpy->clipboard_peer, info, type,
170
0
                                n, data, true);
171
0
    }
172
0
}
173
174
static void
175
dbus_clipboard_request_cancelled(DBusClipboardRequest *req)
176
0
{
177
0
    if (!req->invocation) {
178
0
        return;
179
0
    }
180
181
0
    g_dbus_method_invocation_return_error(
182
0
        req->invocation,
183
0
        DBUS_DISPLAY_ERROR,
184
0
        DBUS_DISPLAY_ERROR_FAILED,
185
0
        "Cancelled clipboard request");
186
187
0
    g_clear_object(&req->invocation);
188
0
    g_clear_handle_id(&req->timeout_id, g_source_remove);
189
0
}
190
191
static void
192
dbus_clipboard_unregister_proxy(DBusDisplay *dpy)
193
0
{
194
0
    const char *name = NULL;
195
0
    GDBusConnection *connection = NULL;
196
0
    int i;
197
198
0
    for (i = 0; i < G_N_ELEMENTS(dpy->clipboard_request); ++i) {
199
0
        dbus_clipboard_request_cancelled(&dpy->clipboard_request[i]);
200
0
    }
201
202
0
    if (!dpy->clipboard_proxy) {
203
0
        return;
204
0
    }
205
206
0
    connection = g_dbus_proxy_get_connection(
207
0
        G_DBUS_PROXY(dpy->clipboard_proxy));
208
0
    if (connection) {
209
0
        g_signal_handlers_disconnect_by_data(connection, dpy);
210
0
    }
211
0
    g_signal_handlers_disconnect_by_data(dpy->clipboard_proxy, dpy);
212
213
0
    name = g_dbus_proxy_get_name(G_DBUS_PROXY(dpy->clipboard_proxy));
214
0
    trace_dbus_clipboard_unregister(name);
215
0
    g_clear_object(&dpy->clipboard_proxy);
216
0
}
217
218
static gboolean
219
dbus_clipboard_register(
220
    DBusDisplay *dpy,
221
    GDBusMethodInvocation *invocation)
222
0
{
223
0
    g_autoptr(GError) err = NULL;
224
0
    const char *name = NULL;
225
0
    GDBusConnection *connection = g_dbus_method_invocation_get_connection(invocation);
226
227
0
    if (dpy->clipboard_proxy) {
228
0
        g_dbus_method_invocation_return_error(
229
0
            invocation,
230
0
            DBUS_DISPLAY_ERROR,
231
0
            DBUS_DISPLAY_ERROR_FAILED,
232
0
            "Clipboard peer already registered!");
233
0
        return DBUS_METHOD_INVOCATION_HANDLED;
234
0
    }
235
236
0
    dpy->clipboard_proxy =
237
0
        qemu_dbus_display1_clipboard_proxy_new_sync(
238
0
            connection,
239
0
            G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
240
0
            g_dbus_method_invocation_get_sender(invocation),
241
0
            "/org/qemu/Display1/Clipboard",
242
0
            NULL,
243
0
            &err);
244
0
    if (!dpy->clipboard_proxy) {
245
0
        g_dbus_method_invocation_return_error(
246
0
            invocation,
247
0
            DBUS_DISPLAY_ERROR,
248
0
            DBUS_DISPLAY_ERROR_FAILED,
249
0
            "Failed to setup proxy: %s", err->message);
250
0
        return DBUS_METHOD_INVOCATION_HANDLED;
251
0
    }
252
253
0
    name = g_dbus_proxy_get_name(G_DBUS_PROXY(dpy->clipboard_proxy));
254
0
    trace_dbus_clipboard_register(name);
255
256
0
    g_object_connect(dpy->clipboard_proxy,
257
0
                     "swapped-signal::notify::g-name-owner",
258
0
                     dbus_clipboard_unregister_proxy, dpy,
259
0
                     NULL);
260
0
    g_object_connect(connection,
261
0
                     "swapped-signal::closed",
262
0
                     dbus_clipboard_unregister_proxy, dpy,
263
0
                     NULL);
264
0
    qemu_clipboard_reset_serial();
265
266
0
    qemu_dbus_display1_clipboard_complete_register(dpy->clipboard, invocation);
267
0
    return DBUS_METHOD_INVOCATION_HANDLED;
268
0
}
269
270
static gboolean
271
dbus_clipboard_check_caller(DBusDisplay *dpy, GDBusMethodInvocation *invocation)
272
0
{
273
0
    if (!dpy->clipboard_proxy ||
274
0
        g_strcmp0(g_dbus_proxy_get_name(G_DBUS_PROXY(dpy->clipboard_proxy)),
275
0
                  g_dbus_method_invocation_get_sender(invocation))) {
276
0
        g_dbus_method_invocation_return_error(
277
0
            invocation,
278
0
            DBUS_DISPLAY_ERROR,
279
0
            DBUS_DISPLAY_ERROR_FAILED,
280
0
            "Unregistered caller");
281
0
        return FALSE;
282
0
    }
283
284
0
    return TRUE;
285
0
}
286
287
static gboolean
288
dbus_clipboard_unregister(
289
    DBusDisplay *dpy,
290
    GDBusMethodInvocation *invocation)
291
0
{
292
0
    if (!dbus_clipboard_check_caller(dpy, invocation)) {
293
0
        return DBUS_METHOD_INVOCATION_HANDLED;
294
0
    }
295
296
0
    dbus_clipboard_unregister_proxy(dpy);
297
298
0
    qemu_dbus_display1_clipboard_complete_unregister(
299
0
        dpy->clipboard, invocation);
300
301
0
    return DBUS_METHOD_INVOCATION_HANDLED;
302
0
}
303
304
static gboolean
305
dbus_clipboard_grab(
306
    DBusDisplay *dpy,
307
    GDBusMethodInvocation *invocation,
308
    gint arg_selection,
309
    guint arg_serial,
310
    const gchar *const *arg_mimes)
311
0
{
312
0
    QemuClipboardSelection s = arg_selection;
313
0
    g_autoptr(QemuClipboardInfo) info = NULL;
314
315
0
    if (!dbus_clipboard_check_caller(dpy, invocation)) {
316
0
        return DBUS_METHOD_INVOCATION_HANDLED;
317
0
    }
318
319
0
    trace_dbus_clipboard_grab(arg_selection, arg_serial);
320
321
0
    if (s >= QEMU_CLIPBOARD_SELECTION__COUNT) {
322
0
        g_dbus_method_invocation_return_error(
323
0
            invocation,
324
0
            DBUS_DISPLAY_ERROR,
325
0
            DBUS_DISPLAY_ERROR_FAILED,
326
0
            "Invalid clipboard selection: %d", arg_selection);
327
0
        return DBUS_METHOD_INVOCATION_HANDLED;
328
0
    }
329
330
0
    info = qemu_clipboard_info_new(&dpy->clipboard_peer, s);
331
0
    if (g_strv_contains(arg_mimes, MIME_TEXT_PLAIN_UTF8)) {
332
0
        info->types[QEMU_CLIPBOARD_TYPE_TEXT].available = true;
333
0
    }
334
0
    info->serial = arg_serial;
335
0
    info->has_serial = true;
336
0
    if (qemu_clipboard_check_serial(info, true)) {
337
0
        qemu_clipboard_update(info);
338
0
    } else {
339
0
        trace_dbus_clipboard_grab_failed();
340
0
    }
341
342
0
    qemu_dbus_display1_clipboard_complete_grab(dpy->clipboard, invocation);
343
0
    return DBUS_METHOD_INVOCATION_HANDLED;
344
0
}
345
346
static gboolean
347
dbus_clipboard_release(
348
    DBusDisplay *dpy,
349
    GDBusMethodInvocation *invocation,
350
    gint arg_selection)
351
0
{
352
0
    if (!dbus_clipboard_check_caller(dpy, invocation)) {
353
0
        return DBUS_METHOD_INVOCATION_HANDLED;
354
0
    }
355
356
0
    qemu_clipboard_peer_release(&dpy->clipboard_peer, arg_selection);
357
358
0
    qemu_dbus_display1_clipboard_complete_release(dpy->clipboard, invocation);
359
0
    return DBUS_METHOD_INVOCATION_HANDLED;
360
0
}
361
362
static gboolean
363
dbus_clipboard_request_timeout(gpointer user_data)
364
0
{
365
0
    dbus_clipboard_request_cancelled(user_data);
366
0
    return G_SOURCE_REMOVE;
367
0
}
368
369
static gboolean
370
dbus_clipboard_request(
371
    DBusDisplay *dpy,
372
    GDBusMethodInvocation *invocation,
373
    gint arg_selection,
374
    const gchar *const *arg_mimes)
375
0
{
376
0
    QemuClipboardSelection s = arg_selection;
377
0
    QemuClipboardType type = QEMU_CLIPBOARD_TYPE_TEXT;
378
0
    QemuClipboardInfo *info = NULL;
379
380
0
    if (!dbus_clipboard_check_caller(dpy, invocation)) {
381
0
        return DBUS_METHOD_INVOCATION_HANDLED;
382
0
    }
383
384
0
    if (s >= QEMU_CLIPBOARD_SELECTION__COUNT) {
385
0
        g_dbus_method_invocation_return_error(
386
0
            invocation,
387
0
            DBUS_DISPLAY_ERROR,
388
0
            DBUS_DISPLAY_ERROR_FAILED,
389
0
            "Invalid clipboard selection: %d", arg_selection);
390
0
        return DBUS_METHOD_INVOCATION_HANDLED;
391
0
    }
392
393
0
    if (dpy->clipboard_request[s].invocation) {
394
0
        g_dbus_method_invocation_return_error(
395
0
            invocation,
396
0
            DBUS_DISPLAY_ERROR,
397
0
            DBUS_DISPLAY_ERROR_FAILED,
398
0
            "Pending request");
399
0
        return DBUS_METHOD_INVOCATION_HANDLED;
400
0
    }
401
402
0
    info = qemu_clipboard_info(s);
403
0
    if (!info || !info->owner || info->owner == &dpy->clipboard_peer) {
404
0
        g_dbus_method_invocation_return_error(
405
0
            invocation,
406
0
            DBUS_DISPLAY_ERROR,
407
0
            DBUS_DISPLAY_ERROR_FAILED,
408
0
            "Empty clipboard");
409
0
        return DBUS_METHOD_INVOCATION_HANDLED;
410
0
    }
411
412
0
    if (!g_strv_contains(arg_mimes, MIME_TEXT_PLAIN_UTF8) ||
413
0
        !info->types[type].available) {
414
0
        g_dbus_method_invocation_return_error(
415
0
            invocation,
416
0
            DBUS_DISPLAY_ERROR,
417
0
            DBUS_DISPLAY_ERROR_FAILED,
418
0
            "Unhandled MIME types requested");
419
0
        return DBUS_METHOD_INVOCATION_HANDLED;
420
0
    }
421
422
0
    if (info->types[type].data) {
423
0
        dbus_clipboard_complete_request(dpy, invocation, info, type);
424
0
    } else {
425
0
        qemu_clipboard_request(info, type);
426
427
0
        dpy->clipboard_request[s].invocation = g_object_ref(invocation);
428
0
        dpy->clipboard_request[s].type = type;
429
0
        dpy->clipboard_request[s].timeout_id =
430
0
            g_timeout_add_seconds(5, dbus_clipboard_request_timeout,
431
0
                                  &dpy->clipboard_request[s]);
432
0
    }
433
434
0
    return DBUS_METHOD_INVOCATION_HANDLED;
435
0
}
436
437
void
438
dbus_clipboard_fini(DBusDisplay *dpy)
439
0
{
440
0
    dbus_clipboard_unregister_proxy(dpy);
441
0
    qemu_clipboard_peer_unregister(&dpy->clipboard_peer);
442
0
    g_clear_object(&dpy->clipboard);
443
0
}
444
445
void
446
dbus_clipboard_init(DBusDisplay *dpy)
447
0
{
448
0
    g_autoptr(GDBusObjectSkeleton) clipboard = NULL;
449
450
0
    assert(!dpy->clipboard);
451
452
0
    clipboard = g_dbus_object_skeleton_new(DBUS_DISPLAY1_ROOT "/Clipboard");
453
0
    dpy->clipboard = qemu_dbus_display1_clipboard_skeleton_new();
454
0
    g_object_connect(dpy->clipboard,
455
0
                     "swapped-signal::handle-register",
456
0
                     dbus_clipboard_register, dpy,
457
0
                     "swapped-signal::handle-unregister",
458
0
                     dbus_clipboard_unregister, dpy,
459
0
                     "swapped-signal::handle-grab",
460
0
                     dbus_clipboard_grab, dpy,
461
0
                     "swapped-signal::handle-release",
462
0
                     dbus_clipboard_release, dpy,
463
0
                     "swapped-signal::handle-request",
464
0
                     dbus_clipboard_request, dpy,
465
0
                     NULL);
466
467
0
    g_dbus_object_skeleton_add_interface(
468
0
        G_DBUS_OBJECT_SKELETON(clipboard),
469
0
        G_DBUS_INTERFACE_SKELETON(dpy->clipboard));
470
0
    g_dbus_object_manager_server_export(dpy->server, clipboard);
471
0
    dpy->clipboard_peer.name = "dbus";
472
0
    dpy->clipboard_peer.notifier.notify = dbus_clipboard_notify;
473
0
    dpy->clipboard_peer.request = dbus_clipboard_qemu_request;
474
0
    qemu_clipboard_peer_register(&dpy->clipboard_peer);
475
0
}