VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-x11.cpp@ 103131

Last change on this file since 103131 was 102818, checked in by vboxsync, 8 months ago

Shared Clipboard/Transfers: Use ShClTransferHttpConvertToStringList() -- removed duplicate code. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.6 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-x11.cpp 102818 2024-01-10 13:56:27Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Linux host.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
33#include <iprt/assert.h>
34#include <iprt/critsect.h>
35#include <iprt/env.h>
36#include <iprt/mem.h>
37#include <iprt/semaphore.h>
38#include <iprt/string.h>
39#include <iprt/asm.h>
40
41#include <VBox/GuestHost/SharedClipboard.h>
42#include <VBox/GuestHost/SharedClipboard-x11.h>
43#include <VBox/HostServices/VBoxClipboardSvc.h>
44#include <iprt/errcore.h>
45
46#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
47# include <VBox/GuestHost/SharedClipboard-transfers.h>
48# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
49# include <VBox/GuestHost/SharedClipboard-transfers.h>
50# endif
51#endif
52
53#include "VBoxSharedClipboardSvc-internal.h"
54#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
55# include "VBoxSharedClipboardSvc-transfers.h"
56#endif
57
58/* Number of currently extablished connections. */
59static volatile uint32_t g_cShClConnections;
60
61
62/*********************************************************************************************************************************
63* Structures and Typedefs *
64*********************************************************************************************************************************/
65/**
66 * Global context information used by the host glue for the X11 clipboard backend.
67 */
68struct SHCLCONTEXT
69{
70 /** This mutex is grabbed during any critical operations on the clipboard
71 * which might clash with others. */
72 RTCRITSECT CritSect;
73 /** X11 context data. */
74 SHCLX11CTX X11;
75 /** Pointer to the VBox host client data structure. */
76 PSHCLCLIENT pClient;
77 /** We set this when we start shutting down as a hint not to post any new
78 * requests. */
79 bool fShuttingDown;
80};
81
82
83/*********************************************************************************************************************************
84* Prototypes *
85*********************************************************************************************************************************/
86static DECLCALLBACK(int) shClSvcX11ReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser);
87static DECLCALLBACK(int) shClSvcX11RequestDataFromSourceCallback(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser);
88
89#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
90static DECLCALLBACK(void) shClSvcX11TransferOnCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
91static DECLCALLBACK(void) shClSvcX11TransferOnInitCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
92static DECLCALLBACK(void) shClSvcX11TransferOnDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx);
93static DECLCALLBACK(void) shClSvcX11TransferOnUnregisteredCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, PSHCLTRANSFERCTX pTransferCtx);
94
95static DECLCALLBACK(int) shClSvcX11TransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx);
96#endif
97
98
99/*********************************************************************************************************************************
100* Backend implementation *
101*********************************************************************************************************************************/
102int ShClBackendInit(PSHCLBACKEND pBackend, VBOXHGCMSVCFNTABLE *pTable)
103{
104 RT_NOREF(pBackend);
105
106 LogFlowFuncEnter();
107
108 /* Override the connection limit. */
109 for (uintptr_t i = 0; i < RT_ELEMENTS(pTable->acMaxClients); i++)
110 pTable->acMaxClients[i] = RT_MIN(VBOX_SHARED_CLIPBOARD_X11_CONNECTIONS_MAX, pTable->acMaxClients[i]);
111
112 RT_ZERO(pBackend->Callbacks);
113 /* Use internal callbacks by default. */
114 pBackend->Callbacks.pfnReportFormats = shClSvcX11ReportFormatsCallback;
115 pBackend->Callbacks.pfnOnRequestDataFromSource = shClSvcX11RequestDataFromSourceCallback;
116
117 return VINF_SUCCESS;
118}
119
120void ShClBackendDestroy(PSHCLBACKEND pBackend)
121{
122 RT_NOREF(pBackend);
123
124 LogFlowFuncEnter();
125}
126
127void ShClBackendSetCallbacks(PSHCLBACKEND pBackend, PSHCLCALLBACKS pCallbacks)
128{
129#define SET_FN_IF_NOT_NULL(a_Fn) \
130 if (pCallbacks->pfn##a_Fn) \
131 pBackend->Callbacks.pfn##a_Fn = pCallbacks->pfn##a_Fn;
132
133 SET_FN_IF_NOT_NULL(ReportFormats);
134 SET_FN_IF_NOT_NULL(OnRequestDataFromSource);
135
136#undef SET_FN_IF_NOT_NULL
137}
138
139/**
140 * @note On the host, we assume that some other application already owns
141 * the clipboard and leave ownership to X11.
142 */
143int ShClBackendConnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, bool fHeadless)
144{
145 int rc;
146
147 /* Check if maximum allowed connections count has reached. */
148 if (ASMAtomicIncU32(&g_cShClConnections) > VBOX_SHARED_CLIPBOARD_X11_CONNECTIONS_MAX)
149 {
150 ASMAtomicDecU32(&g_cShClConnections);
151 LogRel(("Shared Clipboard: maximum amount for client connections reached\n"));
152 return VERR_OUT_OF_RESOURCES;
153 }
154
155 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
156 if (pCtx)
157 {
158 rc = RTCritSectInit(&pCtx->CritSect);
159 if (RT_SUCCESS(rc))
160 {
161 rc = ShClX11Init(&pCtx->X11, &pBackend->Callbacks, pCtx, fHeadless);
162 if (RT_SUCCESS(rc))
163 {
164 pClient->State.pCtx = pCtx;
165 pCtx->pClient = pClient;
166
167#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
168 /*
169 * Set callbacks.
170 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
171 *
172 * Used for starting / stopping the HTTP server.
173 */
174 RT_ZERO(pClient->Transfers.Callbacks);
175
176 pClient->Transfers.Callbacks.pvUser = pCtx; /* Assign context as user-provided callback data. */
177 pClient->Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
178
179 pClient->Transfers.Callbacks.pfnOnCreated = shClSvcX11TransferOnCreatedCallback;
180 pClient->Transfers.Callbacks.pfnOnInitialized = shClSvcX11TransferOnInitCallback;
181 pClient->Transfers.Callbacks.pfnOnDestroy = shClSvcX11TransferOnDestroyCallback;
182 pClient->Transfers.Callbacks.pfnOnUnregistered = shClSvcX11TransferOnUnregisteredCallback;
183#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
184
185 rc = ShClX11ThreadStart(&pCtx->X11, true /* grab shared clipboard */);
186 if (RT_FAILURE(rc))
187 ShClX11Destroy(&pCtx->X11);
188 }
189
190 if (RT_FAILURE(rc))
191 RTCritSectDelete(&pCtx->CritSect);
192 }
193
194 if (RT_FAILURE(rc))
195 {
196 pClient->State.pCtx = NULL;
197 RTMemFree(pCtx);
198 }
199 }
200 else
201 rc = VERR_NO_MEMORY;
202
203 if (RT_FAILURE(rc))
204 {
205 /* Restore active connections count. */
206 ASMAtomicDecU32(&g_cShClConnections);
207 }
208
209 LogFlowFuncLeaveRC(rc);
210 return rc;
211}
212
213int ShClBackendSync(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
214{
215 RT_NOREF(pBackend);
216
217 LogFlowFuncEnter();
218
219 /* Tell the guest we have no data in case X11 is not available. If
220 * there is data in the host clipboard it will automatically be sent to
221 * the guest when the clipboard starts up. */
222 return ShClSvcReportFormats(pClient, VBOX_SHCL_FMT_NONE);
223}
224
225/**
226 * Shuts down the shared clipboard service and "disconnect" the guest.
227 * Note! Host glue code
228 */
229int ShClBackendDisconnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
230{
231 RT_NOREF(pBackend);
232
233 LogFlowFuncEnter();
234
235 PSHCLCONTEXT pCtx = pClient->State.pCtx;
236 AssertPtr(pCtx);
237
238 /* Drop the reference to the client, in case it is still there. This
239 * will cause any outstanding clipboard data requests from X11 to fail
240 * immediately. */
241 pCtx->fShuttingDown = true;
242
243 int rc = ShClX11ThreadStop(&pCtx->X11);
244 /** @todo handle this slightly more reasonably, or be really sure
245 * it won't go wrong. */
246 AssertRC(rc);
247
248 ShClX11Destroy(&pCtx->X11);
249 RTCritSectDelete(&pCtx->CritSect);
250
251 RTMemFree(pCtx);
252
253 /* Decrease active connections count. */
254 ASMAtomicDecU32(&g_cShClConnections);
255
256 LogFlowFuncLeaveRC(rc);
257 return rc;
258}
259
260/**
261 * Reports clipboard formats to the host clipboard.
262 */
263int ShClBackendReportFormats(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, SHCLFORMATS fFormats)
264{
265 RT_NOREF(pBackend);
266
267 int rc = ShClX11ReportFormatsToX11Async(&pClient->State.pCtx->X11, fFormats);
268
269 LogFlowFuncLeaveRC(rc);
270 return rc;
271}
272
273/**
274 * Reads data from the host clipboard.
275 *
276 * Schedules a request to the X11 event thread.
277 *
278 * @note We always fail or complete asynchronously.
279 */
280int ShClBackendReadData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx, SHCLFORMAT uFormat,
281 void *pvData, uint32_t cbData, uint32_t *pcbActual)
282{
283 RT_NOREF(pBackend);
284
285 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
286 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
287 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
288 AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
289
290 RT_NOREF(pCmdCtx);
291
292 LogFlowFunc(("pClient=%p, uFormat=%#x, pv=%p, cb=%RU32, pcbActual=%p\n",
293 pClient, uFormat, pvData, cbData, pcbActual));
294
295 PSHCLEVENT pEvent;
296 int rc = ShClEventSourceGenerateAndRegisterEvent(&pClient->EventSrc, &pEvent);
297 if (RT_SUCCESS(rc))
298 {
299 rc = ShClX11ReadDataFromX11Async(&pClient->State.pCtx->X11, uFormat, cbData, pEvent);
300 if (RT_SUCCESS(rc))
301 {
302 int rcEvent;
303 PSHCLEVENTPAYLOAD pPayload;
304 rc = ShClEventWaitEx(pEvent, SHCL_TIMEOUT_DEFAULT_MS, &rcEvent, &pPayload);
305 if (RT_SUCCESS(rc))
306 {
307 if (pPayload)
308 {
309 AssertReturn(pPayload->cbData == sizeof(SHCLX11RESPONSE), VERR_INVALID_PARAMETER);
310 AssertPtrReturn(pPayload->pvData, VERR_INVALID_POINTER);
311 PSHCLX11RESPONSE pResp = (PSHCLX11RESPONSE)pPayload->pvData;
312 AssertReturn(pResp->enmType == SHCLX11EVENTTYPE_READ, VERR_INVALID_PARAMETER);
313
314 uint32_t const cbRead = pResp->Read.cbData;
315
316 size_t const cbToCopy = RT_MIN(cbData, cbRead);
317 if (cbToCopy) /* memcpy doesn't like 0 byte inputs. */
318 memcpy(pvData, pResp->Read.pvData, RT_MIN(cbData, cbRead));
319
320 LogRel2(("Shared Clipboard: Read %RU32 bytes host X11 clipboard data\n", cbRead));
321
322 *pcbActual = cbRead;
323
324 RTMemFree(pResp->Read.pvData);
325 pResp->Read.cbData = 0;
326
327 ShClPayloadFree(pPayload);
328 }
329 else /* No payload given; could happen on invalid / not-expected formats. */
330 *pcbActual = 0;
331 }
332 else if (rc == VERR_SHCLPB_EVENT_FAILED)
333 rc = rcEvent;
334 }
335
336 ShClEventRelease(pEvent);
337 }
338
339 if (RT_FAILURE(rc))
340 LogRel(("Shared Clipboard: Error reading host clipboard data from X11, rc=%Rrc\n", rc));
341
342 LogFlowFuncLeaveRC(rc);
343 return rc;
344}
345
346int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
347 SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
348{
349 RT_NOREF(pBackend, pClient, pCmdCtx, uFormat, pvData, cbData);
350
351 LogFlowFuncEnter();
352
353 /* Nothing to do here yet. */
354
355 LogFlowFuncLeave();
356 return VINF_SUCCESS;
357}
358
359/**
360 * @copydoc SHCLCALLBACKS::pfnReportFormats
361 *
362 * Reports clipboard formats to the guest.
363 */
364static DECLCALLBACK(int) shClSvcX11ReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser)
365{
366 RT_NOREF(pvUser);
367
368 LogFlowFunc(("pCtx=%p, fFormats=%#x\n", pCtx, fFormats));
369
370 int rc = VINF_SUCCESS;
371 PSHCLCLIENT pClient = pCtx->pClient;
372 AssertPtr(pClient);
373
374 rc = ShClSvcReportFormats(pClient, fFormats);
375
376 LogFlowFuncLeaveRC(rc);
377 return rc;
378}
379
380#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
381/**
382 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnCreated
383 *
384 * @thread Service main thread.
385 */
386static DECLCALLBACK(void) shClSvcX11TransferOnCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
387{
388 LogFlowFuncEnter();
389
390 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
391 AssertPtr(pCtx);
392
393 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
394 AssertPtr(pTransfer);
395
396 PSHCLCLIENT const pClient = pCtx->pClient;
397 AssertPtr(pClient);
398
399 /*
400 * Set transfer provider.
401 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
402 */
403
404 /* Set the interface to the local provider by default first. */
405 RT_ZERO(pClient->Transfers.Provider);
406 ShClTransferProviderLocalQueryInterface(&pClient->Transfers.Provider);
407
408 PSHCLTXPROVIDERIFACE pIface = &pClient->Transfers.Provider.Interface;
409
410 pClient->Transfers.Provider.enmSource = pClient->State.enmSource;
411 pClient->Transfers.Provider.pvUser = pClient;
412
413 switch (ShClTransferGetDir(pTransfer))
414 {
415 case SHCLTRANSFERDIR_FROM_REMOTE: /* Guest -> Host. */
416 {
417 pIface->pfnRootListRead = shClSvcTransferIfaceGHRootListRead;
418
419 pIface->pfnListOpen = shClSvcTransferIfaceGHListOpen;
420 pIface->pfnListClose = shClSvcTransferIfaceGHListClose;
421 pIface->pfnListHdrRead = shClSvcTransferIfaceGHListHdrRead;
422 pIface->pfnListEntryRead = shClSvcTransferIfaceGHListEntryRead;
423
424 pIface->pfnObjOpen = shClSvcTransferIfaceGHObjOpen;
425 pIface->pfnObjClose = shClSvcTransferIfaceGHObjClose;
426 pIface->pfnObjRead = shClSvcTransferIfaceGHObjRead;
427 break;
428 }
429
430 case SHCLTRANSFERDIR_TO_REMOTE: /* Host -> Guest. */
431 {
432 pIface->pfnRootListRead = shClSvcX11TransferIfaceHGRootListRead;
433 break;
434 }
435
436 default:
437 AssertFailed();
438 }
439
440 int rc = ShClTransferSetProvider(pTransfer, &pClient->Transfers.Provider); RT_NOREF(rc);
441
442 LogFlowFuncLeaveRC(rc);
443}
444
445/**
446 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialized
447 *
448 * For G->H: Starts the HTTP server if not done yet and registers the transfer with it.
449 * For H->G: Called on transfer intialization to populate the transfer's root list.
450 *
451 * @thread Service main thread.
452 */
453static DECLCALLBACK(void) shClSvcX11TransferOnInitCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
454{
455 LogFlowFuncEnter();
456
457# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
458 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
459 AssertPtr(pCtx);
460# endif
461
462 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
463 AssertPtr(pTransfer);
464
465 int rc;
466
467 switch (ShClTransferGetDir(pTransfer))
468 {
469 case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
470 {
471# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
472 /* We only need to start the HTTP server when we actually receive data from the remote (host). */
473 rc = ShClTransferHttpServerMaybeStart(&pCtx->X11.HttpCtx);
474# endif
475 break;
476 }
477
478 case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
479 {
480 rc = ShClTransferRootListRead(pTransfer); /* Calls shClSvcX11TransferIfaceHGRootListRead(). */
481 break;
482 }
483
484 default:
485 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
486 break;
487 }
488
489 LogFlowFuncLeaveRC(rc);
490}
491
492/**
493 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnDestroy
494 *
495 * This stops the HTTP server if not done yet.
496 *
497 * @thread Service main thread.
498 */
499static DECLCALLBACK(void) shClSvcX11TransferOnDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
500{
501 LogFlowFuncEnter();
502
503# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
504 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
505 AssertPtr(pCtx);
506
507 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
508 AssertPtr(pTransfer);
509
510 if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE)
511 ShClTransferHttpServerMaybeStop(&pCtx->X11.HttpCtx);
512# else
513 RT_NOREF(pCbCtx);
514# endif
515
516 LogFlowFuncLeave();
517}
518
519/**
520 * Unregisters a transfer from a HTTP server.
521 *
522 * This also stops the HTTP server if no active transfers are found anymore.
523 *
524 * @param pCtx Shared clipboard context to unregister transfer for.
525 * @param pTransfer Transfer to unregister.
526 *
527 * @thread Clipboard main thread.
528 */
529static void shClSvcX11HttpTransferUnregister(PSHCLCONTEXT pCtx, PSHCLTRANSFER pTransfer)
530{
531 if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE)
532 {
533# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
534 if (ShClTransferHttpServerIsInitialized(&pCtx->X11.HttpCtx.HttpServer))
535 {
536 ShClTransferHttpServerUnregisterTransfer(&pCtx->X11.HttpCtx.HttpServer, pTransfer);
537 ShClTransferHttpServerMaybeStop(&pCtx->X11.HttpCtx);
538 }
539# else
540 RT_NOREF(pCtx);
541# endif
542 }
543
544 //ShClTransferRelease(pTransfer);
545}
546
547/**
548 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnUnregistered
549 *
550 * Unregisters a (now) unregistered transfer from the HTTP server.
551 *
552 * @thread Clipboard main thread.
553 */
554static DECLCALLBACK(void) shClSvcX11TransferOnUnregisteredCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, PSHCLTRANSFERCTX pTransferCtx)
555{
556 RT_NOREF(pTransferCtx);
557 shClSvcX11HttpTransferUnregister((PSHCLCONTEXT)pCbCtx->pvUser, pCbCtx->pTransfer);
558}
559#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
560
561/**
562 * @copydoc SHCLCALLBACKS::pfnOnRequestDataFromSource
563 *
564 * Requests clipboard data from the guest.
565 *
566 * @thread Called from X11 event thread.
567 */
568static DECLCALLBACK(int) shClSvcX11RequestDataFromSourceCallback(PSHCLCONTEXT pCtx,
569 SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
570{
571 RT_NOREF(pvUser);
572
573 LogFlowFunc(("pCtx=%p, uFmt=0x%x\n", pCtx, uFmt));
574
575 if (pCtx->fShuttingDown)
576 {
577 /* The shared clipboard is disconnecting. */
578 LogRel(("Shared Clipboard: Host requested guest clipboard data after guest had disconnected\n"));
579 return VERR_WRONG_ORDER;
580 }
581
582 PSHCLCLIENT const pClient = pCtx->pClient;
583 int rc = ShClSvcReadDataFromGuest(pClient, uFmt, ppv, pcb);
584 if (RT_FAILURE(rc))
585 return rc;
586
587 /*
588 * Note: We always return a generic URI list (as HTTP links) here.
589 * As we don't know which Atom target format was requested by the caller, the X11 clipboard codes needs
590 * to decide & transform the list into the actual clipboard Atom target format the caller wanted.
591 */
592#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
593 if (uFmt == VBOX_SHCL_FMT_URI_LIST)
594 {
595 PSHCLTRANSFER pTransfer;
596 rc = ShClSvcTransferCreate(pClient, SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
597 NIL_SHCLTRANSFERID /* Creates a new transfer ID */, &pTransfer);
598 if (RT_SUCCESS(rc))
599 {
600 /* Initialize the transfer on the host side. */
601 rc = ShClSvcTransferInit(pClient, pTransfer);
602 if (RT_FAILURE(rc))
603 ShClSvcTransferDestroy(pClient, pTransfer);
604 }
605
606 if (RT_SUCCESS(rc))
607 {
608 /* We have to wait for the guest reporting the transfer as being initialized.
609 * Only then we can start reading stuff. */
610 rc = ShClTransferWaitForStatus(pTransfer, SHCL_TIMEOUT_DEFAULT_MS, SHCLTRANSFERSTATUS_INITIALIZED);
611 if (RT_SUCCESS(rc))
612 {
613 rc = ShClTransferRootListRead(pTransfer);
614 if (RT_SUCCESS(rc))
615 {
616# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
617 /* As soon as we register the transfer with the HTTP server, the transfer needs to have its roots set. */
618 PSHCLHTTPSERVER const pHttpSrv = &pCtx->X11.HttpCtx.HttpServer;
619 rc = ShClTransferHttpServerRegisterTransfer(pHttpSrv, pTransfer);
620 if (RT_SUCCESS(rc))
621 {
622 char *pszData;
623 size_t cbData;
624 rc = ShClTransferHttpConvertToStringList(pHttpSrv, pTransfer, &pszData, &cbData);
625 if (RT_SUCCESS(rc))
626 {
627 *ppv = pszData;
628 *pcb = cbData;
629 /* ppv has ownership of pszData now. */
630 }
631 }
632# else
633 rc = VERR_NOT_SUPPORTED;
634# endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP */
635 }
636 }
637 }
638 }
639#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
640
641 if (RT_FAILURE(rc))
642 LogRel(("Shared Clipboard: Requesting X11 data in format %#x from guest failed with %Rrc\n", uFmt, rc));
643
644 LogFlowFuncLeaveRC(rc);
645 return rc;
646}
647
648#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
649/**
650 * Handles transfer status replies from the guest.
651 */
652int ShClBackendTransferHandleStatusReply(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer, SHCLSOURCE enmSource, SHCLTRANSFERSTATUS enmStatus, int rcStatus)
653{
654 RT_NOREF(pBackend, pClient, enmSource, rcStatus);
655
656 PSHCLCONTEXT pCtx = pClient->State.pCtx; RT_NOREF(pCtx);
657
658 if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE) /* Guest -> Host */
659 {
660 switch (enmStatus)
661 {
662 case SHCLTRANSFERSTATUS_INITIALIZED:
663 {
664#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
665 int rc2 = ShClTransferHttpServerMaybeStart(&pCtx->X11.HttpCtx);
666 if (RT_SUCCESS(rc2))
667 {
668
669 }
670
671 if (RT_FAILURE(rc2))
672 LogRel(("Shared Clipboard: Registering HTTP transfer failed: %Rrc\n", rc2));
673#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP */
674 break;
675 }
676
677 default:
678 break;
679 }
680 }
681
682 return VINF_SUCCESS;
683}
684
685
686/*********************************************************************************************************************************
687* Provider interface implementation *
688*********************************************************************************************************************************/
689
690/** @copydoc SHCLTXPROVIDERIFACE::pfnRootListRead */
691static DECLCALLBACK(int) shClSvcX11TransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx)
692{
693 LogFlowFuncEnter();
694
695 PSHCLCLIENT pClient = (PSHCLCLIENT)pCtx->pvUser;
696 AssertPtr(pClient);
697
698 AssertPtr(pClient->State.pCtx);
699 PSHCLX11CTX pX11 = &pClient->State.pCtx->X11;
700
701 PSHCLEVENT pEvent;
702 int rc = ShClEventSourceGenerateAndRegisterEvent(&pClient->EventSrc, &pEvent);
703 if (RT_SUCCESS(rc))
704 {
705 rc = ShClX11ReadDataFromX11Async(pX11, VBOX_SHCL_FMT_URI_LIST, UINT32_MAX, pEvent);
706 if (RT_SUCCESS(rc))
707 {
708 /* X supplies the data asynchronously, so we need to wait for data to arrive first. */
709 int rcEvent;
710 PSHCLEVENTPAYLOAD pPayload;
711 rc = ShClEventWaitEx(pEvent, SHCL_TIMEOUT_DEFAULT_MS, &rcEvent, &pPayload);
712 if (RT_SUCCESS(rc))
713 {
714 if (pPayload)
715 {
716 AssertReturn(pPayload->cbData == sizeof(SHCLX11RESPONSE), VERR_INVALID_PARAMETER);
717 AssertPtrReturn(pPayload->pvData, VERR_INVALID_POINTER);
718 PSHCLX11RESPONSE pResp = (PSHCLX11RESPONSE)pPayload->pvData;
719 AssertReturn(pResp->enmType == SHCLX11EVENTTYPE_READ, VERR_INVALID_PARAMETER);
720
721 rc = ShClTransferRootsInitFromStringList(pCtx->pTransfer,
722 (char *)pResp->Read.pvData, pResp->Read.cbData + 1 /* Include zero terminator */);
723 if (RT_SUCCESS(rc))
724 LogRel2(("Shared Clipboard: Host reported %RU64 X11 root entries for transfer to guest\n",
725 ShClTransferRootsCount(pCtx->pTransfer)));
726
727 RTMemFree(pResp->Read.pvData);
728 pResp->Read.cbData = 0;
729
730 ShClPayloadFree(pPayload);
731 pPayload = NULL;
732 }
733 else /* No payload given; could happen on invalid / not-expected formats. */
734 rc = VERR_NO_DATA;
735 }
736 else if (rc == VERR_SHCLPB_EVENT_FAILED)
737 rc = rcEvent;
738 }
739
740 ShClEventRelease(pEvent);
741 }
742 else
743 rc = VERR_SHCLPB_MAX_EVENTS_REACHED;
744
745 LogFlowFuncLeaveRC(rc);
746 return rc;
747}
748#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
749
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette