VirtualBox

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

Last change on this file since 100765 was 100676, checked in by vboxsync, 17 months ago

Shared Clipboard: HTTP transfers with multiple files are also working now. bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.7 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-x11.cpp 100676 2023-07-21 11:26:04Z 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 PSHCLEVENTPAYLOAD pPayload;
303 rc = ShClEventWait(pEvent, SHCL_TIMEOUT_DEFAULT_MS, &pPayload);
304 if (RT_SUCCESS(rc))
305 {
306 if (pPayload)
307 {
308 Assert(pPayload->cbData == sizeof(SHCLX11RESPONSE));
309 PSHCLX11RESPONSE pResp = (PSHCLX11RESPONSE)pPayload->pvData;
310
311 uint32_t const cbRead = pResp->Read.cbData;
312
313 size_t const cbToCopy = RT_MIN(cbData, cbRead);
314 if (cbToCopy) /* memcpy doesn't like 0 byte inputs. */
315 memcpy(pvData, pResp->Read.pvData, RT_MIN(cbData, cbRead));
316
317 LogRel2(("Shared Clipboard: Read %RU32 bytes host X11 clipboard data\n", cbRead));
318
319 *pcbActual = cbRead;
320
321 RTMemFree(pResp->Read.pvData);
322 pResp->Read.cbData = 0;
323
324 ShClPayloadFree(pPayload);
325 }
326 else /* No payload given; could happen on invalid / not-expected formats. */
327 *pcbActual = 0;
328 }
329 }
330
331 ShClEventRelease(pEvent);
332 }
333
334 if (RT_FAILURE(rc))
335 LogRel(("Shared Clipboard: Error reading host clipboard data from X11, rc=%Rrc\n", rc));
336
337 LogFlowFuncLeaveRC(rc);
338 return rc;
339}
340
341int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
342 SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
343{
344 RT_NOREF(pBackend, pClient, pCmdCtx, uFormat, pvData, cbData);
345
346 LogFlowFuncEnter();
347
348 /* Nothing to do here yet. */
349
350 LogFlowFuncLeave();
351 return VINF_SUCCESS;
352}
353
354/**
355 * @copydoc SHCLCALLBACKS::pfnReportFormats
356 *
357 * Reports clipboard formats to the guest.
358 */
359static DECLCALLBACK(int) shClSvcX11ReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser)
360{
361 RT_NOREF(pvUser);
362
363 LogFlowFunc(("pCtx=%p, fFormats=%#x\n", pCtx, fFormats));
364
365 int rc = VINF_SUCCESS;
366 PSHCLCLIENT pClient = pCtx->pClient;
367 AssertPtr(pClient);
368
369 rc = ShClSvcReportFormats(pClient, fFormats);
370
371 LogFlowFuncLeaveRC(rc);
372 return rc;
373}
374
375#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
376/**
377 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnCreated
378 *
379 * @thread Service main thread.
380 */
381static DECLCALLBACK(void) shClSvcX11TransferOnCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
382{
383 LogFlowFuncEnter();
384
385 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
386 AssertPtr(pCtx);
387
388 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
389 AssertPtr(pTransfer);
390
391 PSHCLCLIENT const pClient = pCtx->pClient;
392 AssertPtr(pClient);
393
394 /*
395 * Set transfer provider.
396 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
397 */
398
399 /* Set the interface to the local provider by default first. */
400 RT_ZERO(pClient->Transfers.Provider);
401 ShClTransferProviderLocalQueryInterface(&pClient->Transfers.Provider);
402
403 PSHCLTXPROVIDERIFACE pIface = &pClient->Transfers.Provider.Interface;
404
405 pClient->Transfers.Provider.enmSource = pClient->State.enmSource;
406 pClient->Transfers.Provider.pvUser = pClient;
407
408 switch (ShClTransferGetDir(pTransfer))
409 {
410 case SHCLTRANSFERDIR_FROM_REMOTE: /* Guest -> Host. */
411 {
412 pIface->pfnRootListRead = shClSvcTransferIfaceGHRootListRead;
413
414 pIface->pfnListOpen = shClSvcTransferIfaceGHListOpen;
415 pIface->pfnListClose = shClSvcTransferIfaceGHListClose;
416 pIface->pfnListHdrRead = shClSvcTransferIfaceGHListHdrRead;
417 pIface->pfnListEntryRead = shClSvcTransferIfaceGHListEntryRead;
418
419 pIface->pfnObjOpen = shClSvcTransferIfaceGHObjOpen;
420 pIface->pfnObjClose = shClSvcTransferIfaceGHObjClose;
421 pIface->pfnObjRead = shClSvcTransferIfaceGHObjRead;
422 break;
423 }
424
425 case SHCLTRANSFERDIR_TO_REMOTE: /* Host -> Guest. */
426 {
427 pIface->pfnRootListRead = shClSvcX11TransferIfaceHGRootListRead;
428 break;
429 }
430
431 default:
432 AssertFailed();
433 }
434
435 int rc = ShClTransferSetProvider(pTransfer, &pClient->Transfers.Provider); RT_NOREF(rc);
436
437 LogFlowFuncLeaveRC(rc);
438}
439
440/**
441 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialized
442 *
443 * For G->H: Starts the HTTP server if not done yet and registers the transfer with it.
444 * For H->G: Called on transfer intialization to populate the transfer's root list.
445 *
446 * @thread Service main thread.
447 */
448static DECLCALLBACK(void) shClSvcX11TransferOnInitCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
449{
450 LogFlowFuncEnter();
451
452# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
453 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
454 AssertPtr(pCtx);
455# endif
456
457 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
458 AssertPtr(pTransfer);
459
460 int rc;
461
462 switch (ShClTransferGetDir(pTransfer))
463 {
464 case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
465 {
466# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
467 /* We only need to start the HTTP server when we actually receive data from the remote (host). */
468 rc = ShClTransferHttpServerMaybeStart(&pCtx->X11.HttpCtx);
469# endif
470 break;
471 }
472
473 case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
474 {
475 rc = ShClTransferRootListRead(pTransfer); /* Calls shClSvcX11TransferIfaceHGRootListRead(). */
476 break;
477 }
478
479 default:
480 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
481 break;
482 }
483
484 LogFlowFuncLeaveRC(rc);
485}
486
487/**
488 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnDestroy
489 *
490 * This stops the HTTP server if not done yet.
491 *
492 * @thread Service main thread.
493 */
494static DECLCALLBACK(void) shClSvcX11TransferOnDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
495{
496 LogFlowFuncEnter();
497
498# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
499 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
500 AssertPtr(pCtx);
501
502 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
503 AssertPtr(pTransfer);
504
505 if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE)
506 ShClTransferHttpServerMaybeStop(&pCtx->X11.HttpCtx);
507# else
508 RT_NOREF(pCbCtx);
509# endif
510
511 LogFlowFuncLeave();
512}
513
514/**
515 * Unregisters a transfer from a HTTP server.
516 *
517 * This also stops the HTTP server if no active transfers are found anymore.
518 *
519 * @param pCtx Shared clipboard context to unregister transfer for.
520 * @param pTransfer Transfer to unregister.
521 *
522 * @thread Clipboard main thread.
523 */
524static void shClSvcX11HttpTransferUnregister(PSHCLCONTEXT pCtx, PSHCLTRANSFER pTransfer)
525{
526 if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE)
527 {
528# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
529 if (ShClTransferHttpServerIsInitialized(&pCtx->X11.HttpCtx.HttpServer))
530 {
531 ShClTransferHttpServerUnregisterTransfer(&pCtx->X11.HttpCtx.HttpServer, pTransfer);
532 ShClTransferHttpServerMaybeStop(&pCtx->X11.HttpCtx);
533 }
534# else
535 RT_NOREF(pCtx);
536# endif
537 }
538
539 //ShClTransferRelease(pTransfer);
540}
541
542/**
543 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnUnregistered
544 *
545 * Unregisters a (now) unregistered transfer from the HTTP server.
546 *
547 * @thread Clipboard main thread.
548 */
549static DECLCALLBACK(void) shClSvcX11TransferOnUnregisteredCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx, PSHCLTRANSFERCTX pTransferCtx)
550{
551 RT_NOREF(pTransferCtx);
552 shClSvcX11HttpTransferUnregister((PSHCLCONTEXT)pCbCtx->pvUser, pCbCtx->pTransfer);
553}
554#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
555
556/**
557 * @copydoc SHCLCALLBACKS::pfnOnRequestDataFromSource
558 *
559 * Requests clipboard data from the guest.
560 *
561 * @thread Called from X11 event thread.
562 */
563static DECLCALLBACK(int) shClSvcX11RequestDataFromSourceCallback(PSHCLCONTEXT pCtx,
564 SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
565{
566 RT_NOREF(pvUser);
567
568 LogFlowFunc(("pCtx=%p, uFmt=0x%x\n", pCtx, uFmt));
569
570 if (pCtx->fShuttingDown)
571 {
572 /* The shared clipboard is disconnecting. */
573 LogRel(("Shared Clipboard: Host requested guest clipboard data after guest had disconnected\n"));
574 return VERR_WRONG_ORDER;
575 }
576
577 PSHCLCLIENT const pClient = pCtx->pClient;
578 int rc = ShClSvcReadDataFromGuest(pClient, uFmt, ppv, pcb);
579 if (RT_FAILURE(rc))
580 return rc;
581
582 /*
583 * Note: We always return a generic URI list (as HTTP links) here.
584 * As we don't know which Atom target format was requested by the caller, the X11 clipboard codes needs
585 * to decide & transform the list into the actual clipboard Atom target format the caller wanted.
586 */
587#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
588 if (uFmt == VBOX_SHCL_FMT_URI_LIST)
589 {
590 PSHCLTRANSFER pTransfer;
591 rc = ShClSvcTransferCreate(pClient, SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
592 NIL_SHCLTRANSFERID /* Creates a new transfer ID */, &pTransfer);
593 if (RT_SUCCESS(rc))
594 {
595 /* Initialize the transfer on the host side. */
596 rc = ShClSvcTransferInit(pClient, pTransfer);
597 if (RT_FAILURE(rc))
598 ShClSvcTransferDestroy(pClient, pTransfer);
599 }
600
601 if (RT_SUCCESS(rc))
602 {
603 /* We have to wait for the guest reporting the transfer as being initialized.
604 * Only then we can start reading stuff. */
605 rc = ShClTransferWaitForStatus(pTransfer, SHCL_TIMEOUT_DEFAULT_MS, SHCLTRANSFERSTATUS_INITIALIZED);
606 if (RT_SUCCESS(rc))
607 {
608 rc = ShClTransferRootListRead(pTransfer);
609 if (RT_SUCCESS(rc))
610 {
611# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
612 /* As soon as we register the transfer with the HTTP server, the transfer needs to have its roots set. */
613 PSHCLHTTPSERVER const pHttpSrv = &pCtx->X11.HttpCtx.HttpServer;
614 rc = ShClTransferHttpServerRegisterTransfer(pHttpSrv, pTransfer);
615 if (RT_SUCCESS(rc))
616 {
617 char *pszURL = NULL;
618
619 uint64_t const cRoots = ShClTransferRootsCount(pTransfer);
620 for (uint32_t i = 0; i < cRoots; i++)
621 {
622 char *pszEntry = ShClTransferHttpServerGetUrlA(pHttpSrv, ShClTransferGetID(pTransfer), i /* Entry index */);
623 AssertPtrBreakStmt(pszEntry, rc = VERR_NO_MEMORY);
624
625 if (i > 0)
626 {
627 rc = RTStrAAppend(&pszURL, "\n"); /* Separate entries with a newline. */
628 AssertRCBreak(rc);
629 }
630
631 rc = RTStrAAppend(&pszURL, pszEntry);
632 AssertRCBreak(rc);
633
634 RTStrFree(pszEntry);
635 }
636
637 if (RT_SUCCESS(rc))
638 {
639 *ppv = pszURL;
640 *pcb = strlen(pszURL) + 1 /* Include terminator */;
641
642 LogFlowFunc(("URL is '%s'\n", pszURL));
643
644 /* ppv has ownership of pszURL. */
645 }
646 }
647# else
648 rc = VERR_NOT_SUPPORTED;
649# endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP */
650 }
651 }
652 }
653 }
654#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
655
656 if (RT_FAILURE(rc))
657 LogRel(("Shared Clipboard: Requesting X11 data in format %#x from guest failed with %Rrc\n", uFmt, rc));
658
659 LogFlowFuncLeaveRC(rc);
660 return rc;
661}
662
663#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
664/**
665 * Handles transfer status replies from the guest.
666 */
667int ShClBackendTransferHandleStatusReply(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer, SHCLSOURCE enmSource, SHCLTRANSFERSTATUS enmStatus, int rcStatus)
668{
669 RT_NOREF(pBackend, pClient, enmSource, rcStatus);
670
671 PSHCLCONTEXT pCtx = pClient->State.pCtx; RT_NOREF(pCtx);
672
673 if (ShClTransferGetDir(pTransfer) == SHCLTRANSFERDIR_FROM_REMOTE) /* Guest -> Host */
674 {
675 switch (enmStatus)
676 {
677 case SHCLTRANSFERSTATUS_INITIALIZED:
678 {
679#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP
680 int rc2 = ShClTransferHttpServerMaybeStart(&pCtx->X11.HttpCtx);
681 if (RT_SUCCESS(rc2))
682 {
683
684 }
685
686 if (RT_FAILURE(rc2))
687 LogRel(("Shared Clipboard: Registering HTTP transfer failed: %Rrc\n", rc2));
688#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS_HTTP */
689 break;
690 }
691
692 default:
693 break;
694 }
695 }
696
697 return VINF_SUCCESS;
698}
699
700
701/*********************************************************************************************************************************
702* Provider interface implementation *
703*********************************************************************************************************************************/
704
705/** @copydoc SHCLTXPROVIDERIFACE::pfnRootListRead */
706static DECLCALLBACK(int) shClSvcX11TransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx)
707{
708 LogFlowFuncEnter();
709
710 PSHCLCLIENT pClient = (PSHCLCLIENT)pCtx->pvUser;
711 AssertPtr(pClient);
712
713 AssertPtr(pClient->State.pCtx);
714 PSHCLX11CTX pX11 = &pClient->State.pCtx->X11;
715
716 PSHCLEVENT pEvent;
717 int rc = ShClEventSourceGenerateAndRegisterEvent(&pClient->EventSrc, &pEvent);
718 if (RT_SUCCESS(rc))
719 {
720 rc = ShClX11ReadDataFromX11Async(pX11, VBOX_SHCL_FMT_URI_LIST, UINT32_MAX, pEvent);
721 if (RT_SUCCESS(rc))
722 {
723 /* X supplies the data asynchronously, so we need to wait for data to arrive first. */
724 PSHCLEVENTPAYLOAD pPayload;
725 rc = ShClEventWait(pEvent, SHCL_TIMEOUT_DEFAULT_MS, &pPayload);
726 if (RT_SUCCESS(rc))
727 {
728 if (pPayload)
729 {
730 Assert(pPayload->cbData == sizeof(SHCLX11RESPONSE));
731 AssertPtr(pPayload->pvData);
732 PSHCLX11RESPONSE pResp = (PSHCLX11RESPONSE)pPayload->pvData;
733
734 rc = ShClTransferRootsInitFromStringList(pCtx->pTransfer,
735 (char *)pResp->Read.pvData, pResp->Read.cbData + 1 /* Include zero terminator */);
736 if (RT_SUCCESS(rc))
737 LogRel2(("Shared Clipboard: Host reported %RU64 X11 root entries for transfer to guest\n",
738 ShClTransferRootsCount(pCtx->pTransfer)));
739
740 RTMemFree(pResp->Read.pvData);
741 pResp->Read.cbData = 0;
742
743 ShClPayloadFree(pPayload);
744 pPayload = NULL;
745 }
746 else
747 rc = VERR_NO_DATA; /* No payload. */
748 }
749 }
750
751 ShClEventRelease(pEvent);
752 }
753 else
754 rc = VERR_SHCLPB_MAX_EVENTS_REACHED;
755
756 LogFlowFuncLeaveRC(rc);
757 return rc;
758}
759#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
760
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