VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc.cpp@ 80990

Last change on this file since 80990 was 80990, checked in by vboxsync, 5 years ago

Shared Clipboard/Transfers: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 68.7 KB
Line 
1/* $Id: VBoxSharedClipboardSvc.cpp 80990 2019-09-25 06:20:09Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Host service entry points.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_hostclip The Shared Clipboard Host Service
20 *
21 * The shared clipboard host service provides a proxy between the host's
22 * clipboard and a similar proxy running on a guest. The service is split
23 * into a platform-independent core and platform-specific backends. The
24 * service defines two communication protocols - one to communicate with the
25 * clipboard service running on the guest, and one to communicate with the
26 * backend. These will be described in a very skeletal fashion here.
27 *
28 * @section sec_hostclip_guest_proto The guest communication protocol
29 *
30 * The guest clipboard service communicates with the host service via HGCM
31 * (the host service runs as an HGCM service). The guest clipboard must
32 * connect to the host service before all else (Windows hosts currently only
33 * support one simultaneous connection). Once it has connected, it can send
34 * HGCM messages to the host services, some of which will receive replies from
35 * the host. The host can only reply to a guest message, it cannot initiate
36 * any communication. The guest can in theory send any number of messages in
37 * parallel (see the descriptions of the messages for the practice), and the
38 * host will receive these in sequence, and may reply to them at once
39 * (releasing the caller in the guest) or defer the reply until later.
40 *
41 * There are currently four messages defined. The first is
42 * VBOX_SHCL_FN_GET_HOST_MSG, which waits for a message from the
43 * host. If a host message is sent while the guest is not waiting, it will be
44 * queued until the guest requests it. The host code only supports a single
45 * simultaneous VBOX_SHCL_FN_GET_HOST_MSG call from one guest.
46 *
47 * The second guest message is VBOX_SHCL_FN_FORMATS, which tells
48 * the host that the guest has new clipboard data available. The third is
49 * VBOX_SHCL_FN_READ_DATA, which asks the host to send its
50 * clipboard data and waits until it arrives. The host supports at most one
51 * simultaneous VBOX_SHCL_FN_READ_DATA call from a guest - if a
52 * second call is made before the first has returned, the first will be
53 * aborted.
54 *
55 * The last guest message is VBOX_SHCL_FN_WRITE_DATA, which is
56 * used to send the contents of the guest clipboard to the host. This call
57 * should be used after the host has requested data from the guest.
58 *
59 * @section sec_hostclip_backend_proto The communication protocol with the
60 * platform-specific backend
61 *
62 * The initial protocol implementation (called protocol v0) was very simple,
63 * and could only handle simple data (like copied text and so on). It also
64 * was limited to two (2) fixed parameters at all times.
65 *
66 * Since VBox 6.1 a newer protocol (v1) has been established to also support
67 * file transfers. This protocol uses a (per-client) message queue instead
68 * (see VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD vs. VBOX_SHCL_GUEST_FN_GET_HOST_MSG).
69 *
70 * To distinguish the old (legacy) or new(er) protocol, the VBOX_SHCL_GUEST_FN_CONNECT
71 * message has been introduced. If an older guest does not send this message,
72 * an appropriate translation will be done to serve older Guest Additions (< 6.1).
73 *
74 * The protocol also support out-of-order messages by using so-called "context IDs",
75 * which are generated by the host. A context ID consists of a so-called "source event ID"
76 * and a so-called "event ID". Each HGCM client has an own, random, source event ID and
77 * generates non-deterministic event IDs so that the guest side does not known what
78 * comes next; the guest side has to reply with the same conext ID which was sent by
79 * the host request.
80 *
81 * Also see the protocol changelog at VBoxShClSvc.h.
82 *
83 * @section sec_uri_intro Transferring files
84 *
85 * Since VBox x.x.x transferring files via Shared Clipboard is supported.
86 * See the VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS define for supported / enabled
87 * platforms. This is called "Shared Clipboard transfers".
88 *
89 * Copying files / directories from guest A to guest B requires the host
90 * service to act as a proxy and cache, as we don't allow direct VM-to-VM
91 * communication. Copying from / to the host also is taken into account.
92 *
93 * At the moment a transfer is a all-or-nothing operation, e.g. it either
94 * completes orfails completely. There might be callbacks in the future
95 * to e.g. skip failing entries.
96 *
97 * Known limitations:
98 *
99 * - Support for VRDE (VRDP) is not implemented yet (see #9498).
100 * - Unicode support on Windows hosts / guests is not enabled (yet).
101 * - Symbolic links are not yet handled.
102 * - No support for ACLs yet.
103 * - No (maybe never) support for NT4.
104 *
105 * @section sec_transfers_areas Clipboard areas.
106 *
107 * For larger / longer transfers there might be file data
108 * temporarily cached on the host, which has not been transferred to the
109 * destination yet. Such a cache is called a "Shared Clipboard Area", which
110 * in turn is identified by a unique ID across all VMs running on the same
111 * host. To control the access (and needed cleanup) of such clipboard areas,
112 * VBoxSVC (Main) is used for this task. A Shared Clipboard client can register,
113 * unregister, attach to and detach from a clipboard area. If all references
114 * to a clipboard area are released, a clipboard area gets detroyed automatically
115 * by VBoxSVC.
116 *
117 * By default a clipboard area lives in the user's temporary directory in the
118 * sub folder "VirtualBox Shared Clipboards/clipboard-<ID>". VBoxSVC does not
119 * do any file locking in a clipboard area, but keeps the clipboard areas's
120 * directory open to prevent deletion by third party processes.
121 *
122 * @todo We might use some VFS / container (IPRT?) for this instead of the
123 * host's file system directly?
124 *
125 * @section sec_transfer_structure Transfer handling structure
126 *
127 * All structures / classes are designed for running on both, on the guest
128 * (via VBoxTray / VBoxClient) or on the host (host service) to avoid code
129 * duplication where applicable.
130 *
131 * Per HGCM client there is a so-called "transfer context", which in turn can have
132 * one or mulitple so-called "Shared Clipboard transfer" objects. At the moment we only support
133 * on concurrent Shared Clipboard transfer per transfer context. It's being used for reading from a
134 * source or writing to destination, depening on its direction. An Shared Clipboard transfer
135 * can have optional callbacks which might be needed by various implementations.
136 * Also, transfers optionally can run in an asynchronous thread to prevent
137 * blocking the UI while running.
138 *
139 * An Shared Clipboard transfer can maintain its own clipboard area; for the host service such
140 * a clipboard area is coupled to a clipboard area registered or attached with
141 * VBoxSVC. This is needed because multiple transfers from multiple VMs (n:n) can
142 * rely on the same clipboard area, so there needs a master keeping tracking of
143 * a clipboard area. To minimize IPC traffic only the minimum de/attaching is done
144 * at the moment. A clipboard area gets cleaned up (i.e. physically deleted) if
145 * no references are held to it anymore, or if VBoxSVC goes down.
146 *
147 * @section sec_transfer_providers Transfer providers
148 *
149 * For certain implementations (for example on Windows guests / hosts, using
150 * IDataObject and IStream objects) a more flexible approach reqarding reading /
151 * writing is needed. For this so-called transfer providers abstract the way of how
152 * data is being read / written in the current context (host / guest), while
153 * the rest of the code stays the same.
154 *
155 * @section sec_transfer_protocol Transfer protocol
156 *
157 * The host service issues commands which the guest has to respond with an own
158 * message to. The protocol itself is designed so that it has primitives to list
159 * directories and open/close/read/write file system objects.
160 *
161 * Note that this is different from the DnD approach, as Shared Clipboard transfers
162 * need to be deeper integrated within the host / guest OS (i.e. for progress UI),
163 * and this might require non-monolithic / random access APIs to achieve.
164 *
165 * As there can be multiple file system objects (fs objects) selected for transfer,
166 * a transfer can be queried for its root entries, which then contains the top-level
167 * elements. Based on these elements, (a) (recursive) listing(s) can be performed
168 * to (partially) walk down into directories and query fs object information. The
169 * provider provides appropriate interface for this, even if not all implementations
170 * might need this mechanism.
171 *
172 * An Shared Clipboard transfer has three stages:
173 * - 1. Announcement: An Shared Clipboard transfer-compatible format (currently only one format available)
174 * has been announced, the destination side creates a transfer object, which then,
175 * depending on the actual implementation, can be used to tell the OS that
176 * there is transfer (file) data available.
177 * At this point this just acts as a (kind-of) promise to the OS that we
178 * can provide (file) data at some later point in time.
179 *
180 * - 2. Initialization: As soon as the OS requests the (file) data, mostly triggered
181 * by the user starting a paste operation (CTRL + V), the transfer get initialized
182 * on the destination side, which in turn lets the source know that a transfer
183 * is going to happen.
184 *
185 * - 3. Transfer: At this stage the actual transfer from source to the destination takes
186 * place. How the actual transfer is structurized (e.g. which files / directories
187 * are transferred in which order) depends on the destination implementation. This
188 * is necessary in order to fulfill requirements on the destination side with
189 * regards to ETA calculation or other dependencies.
190 * Both sides can abort or cancel the transfer at any time.
191 */
192
193
194/*********************************************************************************************************************************
195* Header Files *
196*********************************************************************************************************************************/
197#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
198#include <VBox/log.h>
199
200#include <VBox/AssertGuest.h>
201#include <VBox/GuestHost/clipboard-helper.h>
202#include <VBox/HostServices/Service.h>
203#include <VBox/HostServices/VBoxClipboardSvc.h>
204#include <VBox/HostServices/VBoxClipboardExt.h>
205
206#include <iprt/alloc.h>
207#include <iprt/string.h>
208#include <iprt/assert.h>
209#include <iprt/critsect.h>
210#include <iprt/rand.h>
211
212#include <VBox/err.h>
213#include <VBox/vmm/ssm.h>
214
215#include "VBoxSharedClipboardSvc-internal.h"
216#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
217# include "VBoxSharedClipboardSvc-transfers.h"
218#endif
219
220using namespace HGCM;
221
222
223/*********************************************************************************************************************************
224* Prototypes *
225*********************************************************************************************************************************/
226static int sharedClipboardSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID);
227static int sharedClipboardSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState);
228static void sharedClipboardSvcClientStateReset(PSHCLCLIENTSTATE pClientState);
229
230
231/*********************************************************************************************************************************
232* Global Variables *
233*********************************************************************************************************************************/
234PVBOXHGCMSVCHELPERS g_pHelpers;
235
236static RTCRITSECT g_CritSect;
237static uint32_t g_uMode;
238
239/** Is the clipboard running in headless mode? */
240static bool g_fHeadless = false;
241
242/** Holds the service extension state. */
243SHCLEXTSTATE g_ExtState = { 0 };
244
245/** Global map of all connected clients. */
246ClipboardClientMap g_mapClients;
247
248/** Global list of all clients which are queued up (deferred return) and ready
249 * to process new commands. The key is the (unique) client ID. */
250ClipboardClientQueue g_listClientsDeferred;
251
252
253uint32_t sharedClipboardSvcGetMode(void)
254{
255 return g_uMode;
256}
257
258#ifdef UNIT_TEST
259/** Testing interface, getter for clipboard mode */
260uint32_t TestClipSvcGetMode(void)
261{
262 return sharedClipboardSvcGetMode();
263}
264#endif
265
266/** Getter for headless setting. Also needed by testcase. */
267bool VBoxSvcClipboardGetHeadless(void)
268{
269 return g_fHeadless;
270}
271
272static int sharedClipboardSvcModeSet(uint32_t uMode)
273{
274 int rc = VERR_NOT_SUPPORTED;
275
276 switch (uMode)
277 {
278 case VBOX_SHCL_MODE_OFF:
279 RT_FALL_THROUGH();
280 case VBOX_SHCL_MODE_HOST_TO_GUEST:
281 RT_FALL_THROUGH();
282 case VBOX_SHCL_MODE_GUEST_TO_HOST:
283 RT_FALL_THROUGH();
284 case VBOX_SHCL_MODE_BIDIRECTIONAL:
285 {
286 g_uMode = uMode;
287
288 rc = VINF_SUCCESS;
289 break;
290 }
291
292 default:
293 {
294 g_uMode = VBOX_SHCL_MODE_OFF;
295 break;
296 }
297 }
298
299 LogFlowFuncLeaveRC(rc);
300 return rc;
301}
302
303bool VBoxSvcClipboardLock(void)
304{
305 return RT_SUCCESS(RTCritSectEnter(&g_CritSect));
306}
307
308void VBoxSvcClipboardUnlock(void)
309{
310 int rc2 = RTCritSectLeave(&g_CritSect);
311 AssertRC(rc2);
312}
313
314/**
315 * Resets a client's state message queue.
316 *
317 * @param pClient Pointer to the client data structure to reset message queue for.
318 */
319void sharedClipboardSvcMsgQueueReset(PSHCLCLIENT pClient)
320{
321 LogFlowFuncEnter();
322
323 while (!pClient->queueMsg.isEmpty())
324 {
325 RTMemFree(pClient->queueMsg.last());
326 pClient->queueMsg.removeLast();
327 }
328}
329
330/**
331 * Allocates a new clipboard message.
332 *
333 * @returns Allocated clipboard message, or NULL on failure.
334 * @param uMsg Message type of message to allocate.
335 * @param cParms Number of HGCM parameters to allocate.
336 */
337PSHCLCLIENTMSG sharedClipboardSvcMsgAlloc(uint32_t uMsg, uint32_t cParms)
338{
339 PSHCLCLIENTMSG pMsg = (PSHCLCLIENTMSG)RTMemAlloc(sizeof(SHCLCLIENTMSG));
340 if (pMsg)
341 {
342 pMsg->paParms = (PVBOXHGCMSVCPARM)RTMemAllocZ(sizeof(VBOXHGCMSVCPARM) * cParms);
343 if (pMsg->paParms)
344 {
345 pMsg->cParms = cParms;
346 pMsg->uMsg = uMsg;
347
348 return pMsg;
349 }
350 }
351
352 RTMemFree(pMsg);
353 return NULL;
354}
355
356/**
357 * Frees a formerly allocated clipboard message.
358 *
359 * @param pMsg Clipboard message to free.
360 * The pointer will be invalid after calling this function.
361 */
362void sharedClipboardSvcMsgFree(PSHCLCLIENTMSG pMsg)
363{
364 if (!pMsg)
365 return;
366
367 if (pMsg->paParms)
368 RTMemFree(pMsg->paParms);
369
370 RTMemFree(pMsg);
371 pMsg = NULL;
372}
373
374/**
375 * Sets the VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT
376 * return parameters.
377 *
378 * @param pMsg Message to set return parameters to.
379 * @param paDstParms The peek parameter vector.
380 * @param cDstParms The number of peek parameters (at least two).
381 * @remarks ASSUMES the parameters has been cleared by clientMsgPeek.
382 */
383void sharedClipboardSvcMsgSetPeekReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
384{
385 Assert(cDstParms >= 2);
386 if (paDstParms[0].type == VBOX_HGCM_SVC_PARM_32BIT)
387 paDstParms[0].u.uint32 = pMsg->uMsg;
388 else
389 paDstParms[0].u.uint64 = pMsg->uMsg;
390 paDstParms[1].u.uint32 = pMsg->cParms;
391
392 uint32_t i = RT_MIN(cDstParms, pMsg->cParms + 2);
393 while (i-- > 2)
394 switch (pMsg->paParms[i - 2].type)
395 {
396 case VBOX_HGCM_SVC_PARM_32BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint32_t); break;
397 case VBOX_HGCM_SVC_PARM_64BIT: paDstParms[i].u.uint32 = ~(uint32_t)sizeof(uint64_t); break;
398 case VBOX_HGCM_SVC_PARM_PTR: paDstParms[i].u.uint32 = pMsg->paParms[i - 2].u.pointer.size; break;
399 }
400}
401
402/**
403 * Sets the VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD return parameters.
404 *
405 * This function does the necessary translation between the legacy protocol (v0) and the new protocols (>= v1),
406 * as messages are always stored as >= v1 messages in the message queue.
407 *
408 * @returns VBox status code.
409 * @param pMsg Message to set return parameters to.
410 * @param paDstParms The peek parameter vector.
411 * @param cDstParms The number of peek parameters (at least two).
412 */
413int sharedClipboardSvcMsgSetGetHostMsgOldReturn(PSHCLCLIENTMSG pMsg, PVBOXHGCMSVCPARM paDstParms, uint32_t cDstParms)
414{
415 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
416 AssertPtrReturn(paDstParms, VERR_INVALID_POINTER);
417 AssertReturn (cDstParms >= 2, VERR_INVALID_PARAMETER);
418
419 int rc = VINF_SUCCESS;
420
421 switch (pMsg->uMsg)
422 {
423 case VBOX_SHCL_HOST_MSG_QUIT:
424 {
425 HGCMSvcSetU32(&paDstParms[0], VBOX_SHCL_HOST_MSG_QUIT);
426 HGCMSvcSetU32(&paDstParms[1], 0 /* Not used */);
427 break;
428 }
429
430 case VBOX_SHCL_HOST_MSG_READ_DATA:
431 {
432 HGCMSvcSetU32(&paDstParms[0], VBOX_SHCL_HOST_MSG_READ_DATA);
433 AssertBreakStmt(pMsg->cParms >= 2, rc = VERR_INVALID_PARAMETER); /* Paranoia. */
434 uint32_t uFmt;
435 rc = HGCMSvcGetU32(&pMsg->paParms[1] /* uFormat */, &uFmt);
436 if (RT_SUCCESS(rc))
437 HGCMSvcSetU32(&paDstParms[1], uFmt);
438 break;
439 }
440
441 case VBOX_SHCL_HOST_MSG_FORMATS_REPORT:
442 {
443 HGCMSvcSetU32(&paDstParms[0], VBOX_SHCL_HOST_MSG_FORMATS_REPORT);
444 AssertBreakStmt(pMsg->cParms >= 2, rc = VERR_INVALID_PARAMETER); /* Paranoia. */
445 uint32_t uFmts;
446 rc = HGCMSvcGetU32(&pMsg->paParms[1] /* uFormats */, &uFmts);
447 if (RT_SUCCESS(rc))
448 HGCMSvcSetU32(&paDstParms[1], uFmts);
449 break;
450 }
451
452 default:
453 AssertFailed(); /* Not supported by legacy protocol. */
454 rc = VERR_NOT_SUPPORTED;
455 break;
456 }
457
458 LogFlowFuncLeaveRC(rc);
459 return rc;
460}
461
462/**
463 * Adds a new message to a client'S message queue.
464 *
465 * @returns IPRT status code.
466 * @param pClient Pointer to the client data structure to add new message to.
467 * @param pMsg Pointer to message to add. The queue then owns the pointer.
468 * @param fAppend Whether to append or prepend the message to the queue.
469 */
470int sharedClipboardSvcMsgAdd(PSHCLCLIENT pClient, PSHCLCLIENTMSG pMsg, bool fAppend)
471{
472 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
473
474 LogFlowFunc(("uMsg=%RU32 (%s), cParms=%RU32, fAppend=%RTbool\n",
475 pMsg->uMsg, VBoxShClHostMsgToStr(pMsg->uMsg), pMsg->cParms, fAppend));
476
477 if (fAppend)
478 pClient->queueMsg.append(pMsg);
479 else
480 pClient->queueMsg.prepend(pMsg);
481
482 /** @todo Catch / handle OOM? */
483
484 return VINF_SUCCESS;
485}
486
487/**
488 * Implements VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT and VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT.
489 *
490 * @returns VBox status code.
491 * @retval VINF_SUCCESS if a message was pending and is being returned.
492 * @retval VERR_TRY_AGAIN if no message pending and not blocking.
493 * @retval VERR_RESOURCE_BUSY if another read already made a waiting call.
494 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
495 *
496 * @param pClient The client state.
497 * @param hCall The client's call handle.
498 * @param cParms Number of parameters.
499 * @param paParms Array of parameters.
500 * @param fWait Set if we should wait for a message, clear if to return
501 * immediately.
502 */
503int sharedClipboardSvcMsgPeek(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[],
504 bool fWait)
505{
506 /*
507 * Validate the request.
508 */
509 ASSERT_GUEST_MSG_RETURN(cParms >= 2, ("cParms=%u!\n", cParms), VERR_WRONG_PARAMETER_COUNT);
510
511 uint64_t idRestoreCheck = 0;
512 uint32_t i = 0;
513 if (paParms[i].type == VBOX_HGCM_SVC_PARM_64BIT)
514 {
515 idRestoreCheck = paParms[0].u.uint64;
516 paParms[0].u.uint64 = 0;
517 i++;
518 }
519 for (; i < cParms; i++)
520 {
521 ASSERT_GUEST_MSG_RETURN(paParms[i].type == VBOX_HGCM_SVC_PARM_32BIT, ("#%u type=%u\n", i, paParms[i].type),
522 VERR_WRONG_PARAMETER_TYPE);
523 paParms[i].u.uint32 = 0;
524 }
525
526 /*
527 * Check restore session ID.
528 */
529 if (idRestoreCheck != 0)
530 {
531 uint64_t idRestore = g_pHelpers->pfnGetVMMDevSessionId(g_pHelpers);
532 if (idRestoreCheck != idRestore)
533 {
534 paParms[0].u.uint64 = idRestore;
535 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VERR_VM_RESTORED (%#RX64 -> %#RX64)\n",
536 pClient->State.uClientID, idRestoreCheck, idRestore));
537 return VERR_VM_RESTORED;
538 }
539 Assert(!g_pHelpers->pfnIsCallRestored(hCall));
540 }
541
542 /*
543 * Return information about the first message if one is pending in the list.
544 */
545 if (!pClient->queueMsg.isEmpty())
546 {
547 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
548 if (pFirstMsg)
549 {
550 sharedClipboardSvcMsgSetPeekReturn(pFirstMsg, paParms, cParms);
551 LogFlowFunc(("[Client %RU32] VBOX_SHCL_GUEST_FN_MSG_PEEK_XXX -> VINF_SUCCESS (idMsg=%u (%s), cParms=%u)\n",
552 pClient->State.uClientID, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg),
553 pFirstMsg->cParms));
554 return VINF_SUCCESS;
555 }
556 }
557
558 /*
559 * If we cannot wait, fail the call.
560 */
561 if (!fWait)
562 {
563 LogFlowFunc(("[Client %RU32] GUEST_MSG_PEEK_NOWAIT -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
564 return VERR_TRY_AGAIN;
565 }
566
567 /*
568 * Wait for the host to queue a message for this client.
569 */
570 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n",
571 pClient->State.uClientID), VERR_RESOURCE_BUSY);
572 pClient->Pending.hHandle = hCall;
573 pClient->Pending.cParms = cParms;
574 pClient->Pending.paParms = paParms;
575 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT;
576 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
577 return VINF_HGCM_ASYNC_EXECUTE;
578}
579
580/**
581 * Implements VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD.
582 *
583 * @returns VBox status code.
584 * @retval VINF_SUCCESS if a message was pending and is being returned.
585 * @retval VINF_HGCM_ASYNC_EXECUTE if message wait is pending.
586 *
587 * @param pClient The client state.
588 * @param hCall The client's call handle.
589 * @param cParms Number of parameters.
590 * @param paParms Array of parameters.
591 */
592int sharedClipboardSvcMsgGetOld(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
593{
594 int rc;
595
596 if (cParms != VBOX_SHCL_CPARMS_GET_HOST_MSG_OLD)
597 {
598 rc = VERR_INVALID_PARAMETER;
599 }
600 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* msg */
601 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT) /* formats */
602 {
603 rc = VERR_INVALID_PARAMETER;
604 }
605 else
606 {
607 if (!pClient->queueMsg.isEmpty())
608 {
609 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
610 AssertPtr(pFirstMsg);
611
612 LogFlowFunc(("[Client %RU32] uMsg=%RU32 (%s), cParms=%RU32\n",
613 pClient->State.uClientID, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg),
614 pFirstMsg->cParms));
615
616 rc = sharedClipboardSvcMsgSetGetHostMsgOldReturn(pFirstMsg, paParms, cParms);
617 if (RT_SUCCESS(rc))
618 {
619 AssertPtr(g_pHelpers);
620 rc = g_pHelpers->pfnCallComplete(hCall, rc);
621 if (rc != VERR_CANCELLED)
622 {
623 pClient->queueMsg.removeFirst();
624 sharedClipboardSvcMsgFree(pFirstMsg);
625
626 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
627 }
628 }
629 }
630 else
631 {
632 ASSERT_GUEST_MSG_RETURN(pClient->Pending.uType == 0, ("Already pending! (idClient=%RU32)\n",
633 pClient->State.uClientID), VERR_RESOURCE_BUSY);
634
635 pClient->Pending.hHandle = hCall;
636 pClient->Pending.cParms = cParms;
637 pClient->Pending.paParms = paParms;
638 pClient->Pending.uType = VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD;
639
640 rc = VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
641
642 LogFlowFunc(("[Client %RU32] Is now in pending mode...\n", pClient->State.uClientID));
643 }
644 }
645
646 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
647 return rc;
648}
649
650/**
651 * Implements VBOX_SHCL_GUEST_FN_MSG_GET.
652 *
653 * @returns VBox status code.
654 * @retval VINF_SUCCESS if message retrieved and removed from the pending queue.
655 * @retval VERR_TRY_AGAIN if no message pending.
656 * @retval VERR_BUFFER_OVERFLOW if a parmeter buffer is too small. The buffer
657 * size was updated to reflect the required size, though this isn't yet
658 * forwarded to the guest. (The guest is better of using peek with
659 * parameter count + 2 parameters to get the sizes.)
660 * @retval VERR_MISMATCH if the incoming message ID does not match the pending.
661 * @retval VINF_HGCM_ASYNC_EXECUTE if message was completed already.
662 *
663 * @param pClient The client state.
664 * @param hCall The client's call handle.
665 * @param cParms Number of parameters.
666 * @param paParms Array of parameters.
667 */
668int sharedClipboardSvcMsgGet(PSHCLCLIENT pClient, VBOXHGCMCALLHANDLE hCall, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
669{
670 /*
671 * Validate the request.
672 */
673 uint32_t const idMsgExpected = cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_32BIT ? paParms[0].u.uint32
674 : cParms > 0 && paParms[0].type == VBOX_HGCM_SVC_PARM_64BIT ? paParms[0].u.uint64
675 : UINT32_MAX;
676
677 /*
678 * Return information about the first message if one is pending in the list.
679 */
680 if (!pClient->queueMsg.isEmpty())
681 {
682 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
683 if (pFirstMsg)
684 {
685 LogFlowFunc(("First message is: %RU32 (%s), cParms=%RU32\n",
686 pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg), pFirstMsg->cParms));
687
688 ASSERT_GUEST_MSG_RETURN(pFirstMsg->uMsg == idMsgExpected || idMsgExpected == UINT32_MAX,
689 ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
690 pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg), pFirstMsg->cParms,
691 idMsgExpected, VBoxShClHostMsgToStr(idMsgExpected), cParms),
692 VERR_MISMATCH);
693 ASSERT_GUEST_MSG_RETURN(pFirstMsg->cParms == cParms,
694 ("idMsg=%u (%s) cParms=%u, caller expected %u (%s) and %u\n",
695 pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg), pFirstMsg->cParms,
696 idMsgExpected, VBoxShClHostMsgToStr(idMsgExpected), cParms),
697 VERR_WRONG_PARAMETER_COUNT);
698
699 /* Check the parameter types. */
700 for (uint32_t i = 0; i < cParms; i++)
701 ASSERT_GUEST_MSG_RETURN(pFirstMsg->paParms[i].type == paParms[i].type,
702 ("param #%u: type %u, caller expected %u (idMsg=%u %s)\n", i, pFirstMsg->paParms[i].type,
703 paParms[i].type, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg)),
704 VERR_WRONG_PARAMETER_TYPE);
705 /*
706 * Copy out the parameters.
707 *
708 * No assertions on buffer overflows, and keep going till the end so we can
709 * communicate all the required buffer sizes.
710 */
711 int rc = VINF_SUCCESS;
712 for (uint32_t i = 0; i < cParms; i++)
713 switch (pFirstMsg->paParms[i].type)
714 {
715 case VBOX_HGCM_SVC_PARM_32BIT:
716 paParms[i].u.uint32 = pFirstMsg->paParms[i].u.uint32;
717 break;
718
719 case VBOX_HGCM_SVC_PARM_64BIT:
720 paParms[i].u.uint64 = pFirstMsg->paParms[i].u.uint64;
721 break;
722
723 case VBOX_HGCM_SVC_PARM_PTR:
724 {
725 uint32_t const cbSrc = pFirstMsg->paParms[i].u.pointer.size;
726 uint32_t const cbDst = paParms[i].u.pointer.size;
727 paParms[i].u.pointer.size = cbSrc; /** @todo Check if this is safe in other layers...
728 * Update: Safe, yes, but VMMDevHGCM doesn't pass it along. */
729 if (cbSrc <= cbDst)
730 memcpy(paParms[i].u.pointer.addr, pFirstMsg->paParms[i].u.pointer.addr, cbSrc);
731 else
732 {
733 AssertMsgFailed(("#%u: cbSrc=%RU32 is bigger than cbDst=%RU32\n", i, cbSrc, cbDst));
734 rc = VERR_BUFFER_OVERFLOW;
735 }
736 break;
737 }
738
739 default:
740 AssertMsgFailed(("#%u: %u\n", i, pFirstMsg->paParms[i].type));
741 rc = VERR_INTERNAL_ERROR;
742 break;
743 }
744 if (RT_SUCCESS(rc))
745 {
746 /*
747 * Complete the message and remove the pending message unless the
748 * guest raced us and cancelled this call in the meantime.
749 */
750 AssertPtr(g_pHelpers);
751 rc = g_pHelpers->pfnCallComplete(hCall, rc);
752
753 LogFlowFunc(("[Client %RU32] pfnCallComplete -> %Rrc\n", pClient->State.uClientID, rc));
754
755 if (rc != VERR_CANCELLED)
756 {
757 pClient->queueMsg.removeFirst();
758 sharedClipboardSvcMsgFree(pFirstMsg);
759 }
760
761 return VINF_HGCM_ASYNC_EXECUTE; /* The caller must not complete it. */
762 }
763
764 LogFlowFunc(("[Client %RU32] Returning %Rrc\n", pClient->State.uClientID, rc));
765 return rc;
766 }
767 }
768
769 paParms[0].u.uint32 = 0;
770 paParms[1].u.uint32 = 0;
771 LogFlowFunc(("[Client %RU32] -> VERR_TRY_AGAIN\n", pClient->State.uClientID));
772 return VERR_TRY_AGAIN;
773}
774
775/**
776 * Wakes up a pending client (i.e. waiting for new messages).
777 *
778 * @returns VBox status code.
779 * @retval VINF_NO_CHANGE if the client is not in pending mode.
780 *
781 * @param pClient Client to wake up.
782 */
783int sharedClipboardSvcClientWakeup(PSHCLCLIENT pClient)
784{
785 int rc = VINF_NO_CHANGE;
786
787 if (pClient->Pending.uType)
788 {
789 LogFunc(("[Client %RU32] Waking up ...\n", pClient->State.uClientID));
790
791 rc = VINF_SUCCESS;
792
793 if (!pClient->queueMsg.isEmpty())
794 {
795 PSHCLCLIENTMSG pFirstMsg = pClient->queueMsg.first();
796 if (pFirstMsg)
797 {
798 LogFunc(("[Client %RU32] Current host message is %RU32 (%s), cParms=%RU32\n",
799 pClient->State.uClientID, pFirstMsg->uMsg, VBoxShClHostMsgToStr(pFirstMsg->uMsg),
800 pFirstMsg->cParms));
801
802 bool fDonePending = false;
803
804 if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT)
805 {
806 sharedClipboardSvcMsgSetPeekReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
807 fDonePending = true;
808 }
809 else if (pClient->Pending.uType == VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD) /* Legacy */
810 {
811 rc = sharedClipboardSvcMsgSetGetHostMsgOldReturn(pFirstMsg, pClient->Pending.paParms, pClient->Pending.cParms);
812 if (RT_SUCCESS(rc))
813 {
814 /* The old (legacy) protocol gets the message right when returning from peeking, so
815 * remove the actual message from our queue right now. */
816 pClient->queueMsg.removeFirst();
817 sharedClipboardSvcMsgFree(pFirstMsg);
818
819 fDonePending = true;
820 }
821 }
822
823 if (fDonePending)
824 {
825 rc = g_pHelpers->pfnCallComplete(pClient->Pending.hHandle, VINF_SUCCESS);
826
827 pClient->Pending.hHandle = NULL;
828 pClient->Pending.paParms = NULL;
829 pClient->Pending.cParms = 0;
830 pClient->Pending.uType = 0;
831 }
832 }
833 else
834 AssertFailed();
835 }
836 else
837 AssertMsgFailed(("Waking up client ID=%RU32 with no host message in queue is a bad idea\n", pClient->State.uClientID));
838
839 return rc;
840 }
841 else
842 LogFunc(("[Client %RU32] Not in pending state, skipping wakeup\n", pClient->State.uClientID));
843
844 return VINF_NO_CHANGE;
845}
846
847/**
848 * Requests to read clipboard data from the guest.
849 *
850 * @returns VBox status code.
851 * @param pClient Client to request to read data form.
852 * @param pDataReq Data request to send to the guest.
853 * @param puEvent Event ID for waiting for new data. Optional.
854 */
855int sharedClipboardSvcDataReadRequest(PSHCLCLIENT pClient, PSHCLDATAREQ pDataReq,
856 PSHCLEVENTID puEvent)
857{
858 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
859 AssertPtrReturn(pDataReq, VERR_INVALID_POINTER);
860 /* puEvent is optional. */
861
862 int rc;
863
864 PSHCLCLIENTMSG pMsgReadData = sharedClipboardSvcMsgAlloc(VBOX_SHCL_HOST_MSG_READ_DATA,
865 VBOX_SHCL_CPARMS_READ_DATA);
866 if (pMsgReadData)
867 {
868 const SHCLEVENTID uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
869
870 HGCMSvcSetU32(&pMsgReadData->paParms[0], VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID,
871 pClient->Events.uID, uEvent));
872 HGCMSvcSetU32(&pMsgReadData->paParms[1], pDataReq->uFmt);
873 HGCMSvcSetU32(&pMsgReadData->paParms[2], pClient->State.cbChunkSize);
874
875 rc = sharedClipboardSvcMsgAdd(pClient, pMsgReadData, true /* fAppend */);
876 if (RT_SUCCESS(rc))
877 {
878 rc = SharedClipboardEventRegister(&pClient->Events, uEvent);
879 if (RT_SUCCESS(rc))
880 {
881 rc = sharedClipboardSvcClientWakeup(pClient);
882 if (RT_SUCCESS(rc))
883 {
884 if (puEvent)
885 *puEvent = uEvent;
886 }
887 else
888 SharedClipboardEventUnregister(&pClient->Events, uEvent);
889 }
890 }
891 }
892 else
893 rc = VERR_NO_MEMORY;
894
895 LogFlowFuncLeaveRC(rc);
896 return rc;
897}
898
899int sharedClipboardSvcDataReadSignal(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
900 PSHCLDATABLOCK pData)
901{
902 SHCLEVENTID uEvent;
903 if (pClient->State.uProtocolVer == 0)
904 {
905 /* Protocol v0 did not have any context ID handling, so we ASSUME that the last event registered
906 * is the one we want to handle (as this all was a synchronous protocol anyway). */
907 uEvent = SharedClipboardEventGetLast(&pClient->Events);
908 }
909 else
910 uEvent = VBOX_SHCL_CONTEXTID_GET_EVENT(pCmdCtx->uContextID);
911
912 int rc = VINF_SUCCESS;
913
914 PSHCLEVENTPAYLOAD pPayload = NULL;
915 if (pData->cbData)
916 rc = SharedClipboardPayloadAlloc(uEvent, pData->pvData, pData->cbData, &pPayload);
917
918 if (RT_SUCCESS(rc))
919 {
920 rc = SharedClipboardEventSignal(&pClient->Events, uEvent, pPayload);
921 if (RT_FAILURE(rc))
922 SharedClipboardPayloadFree(pPayload);
923 }
924
925 LogFlowFuncLeaveRC(rc);
926 return rc;
927}
928
929int sharedClipboardSvcFormatsReport(PSHCLCLIENT pClient, PSHCLFORMATDATA pFormats)
930{
931 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
932 AssertPtrReturn(pFormats, VERR_INVALID_POINTER);
933
934 int rc;
935
936 PSHCLCLIENTMSG pMsg = sharedClipboardSvcMsgAlloc(VBOX_SHCL_HOST_MSG_FORMATS_REPORT, 3);
937 if (pMsg)
938 {
939 const SHCLEVENTID uEvent = SharedClipboardEventIDGenerate(&pClient->Events);
940
941 HGCMSvcSetU32(&pMsg->paParms[0], VBOX_SHCL_CONTEXTID_MAKE(pClient->State.uSessionID,
942 pClient->Events.uID, uEvent));
943 HGCMSvcSetU32(&pMsg->paParms[1], pFormats->uFormats);
944 HGCMSvcSetU32(&pMsg->paParms[2], 0 /* fFlags */);
945
946 rc = sharedClipboardSvcMsgAdd(pClient, pMsg, true /* fAppend */);
947 if (RT_SUCCESS(rc))
948 {
949#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
950 /* If this is an URI list, create a transfer locally and also tell the guest to create
951 * a transfer on the guest side. */
952 if (pFormats->uFormats & VBOX_SHCL_FMT_URI_LIST)
953 {
954 rc = sharedClipboardSvcTransferStart(pClient, SHCLTRANSFERDIR_WRITE, SHCLSOURCE_LOCAL,
955 NULL /* pTransfer */);
956 if (RT_FAILURE(rc))
957 LogRel(("Shared Clipboard: Initializing host write transfer failed with %Rrc\n", rc));
958 }
959 else
960 {
961#endif
962 rc = sharedClipboardSvcClientWakeup(pClient);
963
964#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
965 }
966#endif
967 }
968 }
969 else
970 rc = VERR_NO_MEMORY;
971
972 LogFlowFuncLeaveRC(rc);
973 return rc;
974}
975
976int sharedClipboardSvcGetDataWrite(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
977{
978 LogFlowFuncEnter();
979
980 if ( sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_GUEST_TO_HOST
981 && sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
982 {
983 return VERR_NOT_SUPPORTED;
984 }
985
986 int rc;
987
988 SHCLDATABLOCK dataBlock;
989 RT_ZERO(dataBlock);
990
991 SHCLCLIENTCMDCTX cmdCtx;
992 RT_ZERO(cmdCtx);
993
994 if (pClient->State.uProtocolVer == 0) /* Legacy protocol */
995 {
996 if (cParms < 2)
997 {
998 rc = VERR_INVALID_PARAMETER;
999 }
1000 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
1001 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* ptr */
1002 {
1003 rc = VERR_INVALID_PARAMETER;
1004 }
1005 else
1006 {
1007 rc = HGCMSvcGetU32(&paParms[0], &dataBlock.uFormat);
1008 if (RT_SUCCESS(rc))
1009 rc = HGCMSvcGetBuf(&paParms[1], &dataBlock.pvData, &dataBlock.cbData);
1010 }
1011 }
1012 else
1013 {
1014 if (cParms < VBOX_SHCL_CPARMS_WRITE_DATA)
1015 {
1016 rc = VERR_INVALID_PARAMETER;
1017 }
1018 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uContext */
1019 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uFormat */
1020 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* cbData */
1021 || paParms[3].type != VBOX_HGCM_SVC_PARM_PTR) /* pvData */
1022 {
1023 rc = VERR_INVALID_PARAMETER;
1024 }
1025 else
1026 {
1027 rc = HGCMSvcGetU32(&paParms[0], &cmdCtx.uContextID);
1028 if (RT_SUCCESS(rc))
1029 rc = HGCMSvcGetU32(&paParms[1], &dataBlock.uFormat);
1030 if (RT_SUCCESS(rc))
1031 rc = HGCMSvcGetBuf(&paParms[3], &dataBlock.pvData, &dataBlock.cbData);
1032
1033 /** @todo Handle the rest. */
1034 }
1035 }
1036
1037 if (RT_SUCCESS(rc))
1038 {
1039 if (g_ExtState.pfnExtension)
1040 {
1041 SHCLEXTPARMS parms;
1042 RT_ZERO(parms);
1043
1044 parms.uFormat = dataBlock.uFormat;
1045 parms.u.pvData = dataBlock.pvData;
1046 parms.cbData = dataBlock.cbData;
1047
1048 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof(parms));
1049 }
1050
1051 rc = SharedClipboardSvcImplWriteData(pClient, &cmdCtx, &dataBlock);
1052 }
1053
1054 LogFlowFuncLeaveRC(rc);
1055 return rc;
1056}
1057
1058int sharedClipboardSvcSetSource(PSHCLCLIENT pClient, SHCLSOURCE enmSource)
1059{
1060 if (!pClient) /* If no client connected (anymore), bail out. */
1061 return VINF_SUCCESS;
1062
1063 int rc = VINF_SUCCESS;
1064
1065 if (VBoxSvcClipboardLock())
1066 {
1067 pClient->State.enmSource = enmSource;
1068
1069 LogFlowFunc(("Source of client %RU32 is now %RU32\n", pClient->State.uClientID, pClient->State.enmSource));
1070
1071 VBoxSvcClipboardUnlock();
1072 }
1073
1074 LogFlowFuncLeaveRC(rc);
1075 return rc;
1076}
1077
1078static int svcInit(void)
1079{
1080 int rc = RTCritSectInit(&g_CritSect);
1081
1082 if (RT_SUCCESS(rc))
1083 {
1084 sharedClipboardSvcModeSet(VBOX_SHCL_MODE_OFF);
1085
1086 rc = SharedClipboardSvcImplInit();
1087
1088 /* Clean up on failure, because 'svnUnload' will not be called
1089 * if the 'svcInit' returns an error.
1090 */
1091 if (RT_FAILURE(rc))
1092 {
1093 RTCritSectDelete(&g_CritSect);
1094 }
1095 }
1096
1097 return rc;
1098}
1099
1100static DECLCALLBACK(int) svcUnload(void *)
1101{
1102 LogFlowFuncEnter();
1103
1104 SharedClipboardSvcImplDestroy();
1105
1106 RTCritSectDelete(&g_CritSect);
1107
1108 return VINF_SUCCESS;
1109}
1110
1111static DECLCALLBACK(int) svcDisconnect(void *, uint32_t u32ClientID, void *pvClient)
1112{
1113 RT_NOREF(u32ClientID, pvClient);
1114
1115 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1116
1117 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1118 AssertPtr(pClient);
1119
1120#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1121 PSHCLTRANSFER pTransfer = SharedClipboardTransferCtxGetTransfer(&pClient->TransferCtx, 0 /* Index*/);
1122 if (pTransfer)
1123 sharedClipboardSvcTransferAreaDetach(&pClient->State, pTransfer);
1124
1125 SharedClipboardTransferCtxDestroy(&pClient->TransferCtx);
1126#endif
1127
1128 SharedClipboardSvcImplDisconnect(pClient);
1129
1130 sharedClipboardSvcClientStateReset(&pClient->State);
1131 sharedClipboardSvcClientStateDestroy(&pClient->State);
1132
1133 SharedClipboardEventSourceDestroy(&pClient->Events);
1134
1135 ClipboardClientMap::iterator itClient = g_mapClients.find(u32ClientID);
1136 if (itClient != g_mapClients.end())
1137 {
1138 g_mapClients.erase(itClient);
1139 }
1140 else
1141 AssertFailed();
1142
1143 return VINF_SUCCESS;
1144}
1145
1146static DECLCALLBACK(int) svcConnect(void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
1147{
1148 RT_NOREF(fRequestor, fRestoring);
1149
1150 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1151 AssertPtr(pvClient);
1152
1153 /* Assign the client ID. */
1154 pClient->State.uClientID = u32ClientID;
1155
1156 /* Create the client's own event source. */
1157 int rc = SharedClipboardEventSourceCreate(&pClient->Events, 0 /* ID, ignored */);
1158 if (RT_SUCCESS(rc))
1159 {
1160 LogFlowFunc(("[Client %RU32] Using event source %RU32\n", u32ClientID, pClient->Events.uID));
1161
1162 /* Reset the client state. */
1163 sharedClipboardSvcClientStateReset(&pClient->State);
1164
1165 /* (Re-)initialize the client state. */
1166 rc = sharedClipboardSvcClientStateInit(&pClient->State, u32ClientID);
1167 if (RT_SUCCESS(rc))
1168 {
1169 rc = SharedClipboardSvcImplConnect(pClient, VBoxSvcClipboardGetHeadless());
1170#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1171 if (RT_SUCCESS(rc))
1172 rc = SharedClipboardTransferCtxInit(&pClient->TransferCtx);
1173#endif
1174 if (RT_SUCCESS(rc))
1175 {
1176 /* Assign weak pointer to client map .*/
1177 g_mapClients[u32ClientID] = pClient; /** @todo Handle OOM / collisions? */
1178
1179 /* For now we ASSUME that the first client ever connected is in charge for
1180 * communicating withe the service extension.
1181 *
1182 ** @todo This needs to be fixed ASAP w/o breaking older guest / host combos. */
1183 if (g_ExtState.uClientID == 0)
1184 g_ExtState.uClientID = u32ClientID;
1185 }
1186 }
1187 }
1188
1189 LogFlowFuncLeaveRC(rc);
1190 return rc;
1191}
1192
1193static DECLCALLBACK(void) svcCall(void *,
1194 VBOXHGCMCALLHANDLE callHandle,
1195 uint32_t u32ClientID,
1196 void *pvClient,
1197 uint32_t u32Function,
1198 uint32_t cParms,
1199 VBOXHGCMSVCPARM paParms[],
1200 uint64_t tsArrival)
1201{
1202 RT_NOREF(u32ClientID, pvClient, tsArrival);
1203
1204 int rc = VINF_SUCCESS;
1205
1206 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1207 AssertPtr(pClient);
1208
1209 LogFunc(("u32ClientID=%RU32 (proto %RU32), fn=%RU32 (%s), cParms=%RU32, paParms=%p\n",
1210 u32ClientID, pClient->State.uProtocolVer, u32Function, VBoxShClGuestMsgToStr(u32Function), cParms, paParms));
1211
1212#ifdef DEBUG
1213 uint32_t i;
1214
1215 for (i = 0; i < cParms; i++)
1216 {
1217 /** @todo parameters other than 32 bit */
1218 LogFunc((" paParms[%d]: type %RU32 - value %RU32\n", i, paParms[i].type, paParms[i].u.uint32));
1219 }
1220#endif
1221
1222 switch (u32Function)
1223 {
1224 case VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD:
1225 {
1226 rc = sharedClipboardSvcMsgGetOld(pClient, callHandle, cParms, paParms);
1227 break;
1228 }
1229
1230 case VBOX_SHCL_GUEST_FN_CONNECT:
1231 {
1232 if (cParms != VBOX_SHCL_CPARMS_CONNECT)
1233 {
1234 rc = VERR_INVALID_PARAMETER;
1235 }
1236 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uProtocolVer */
1237 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uProtocolFlags */
1238 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* cbChunkSize */
1239 || paParms[3].type != VBOX_HGCM_SVC_PARM_32BIT /* enmCompression */
1240 || paParms[4].type != VBOX_HGCM_SVC_PARM_32BIT) /* enmChecksumType */
1241 {
1242 rc = VERR_INVALID_PARAMETER;
1243 }
1244 else if (sharedClipboardSvcGetMode() == VBOX_SHCL_MODE_OFF)
1245 {
1246 rc = VERR_ACCESS_DENIED;
1247 }
1248 else
1249 {
1250 /* Update the protocol version and tell the guest. */
1251 pClient->State.uProtocolVer = 1;
1252
1253 LogFlowFunc(("Now using protocol v%RU32\n", pClient->State.uProtocolVer));
1254
1255 HGCMSvcSetU32(&paParms[0], pClient->State.uProtocolVer);
1256 HGCMSvcSetU32(&paParms[1], 0 /* Procotol flags, not used yet */);
1257 HGCMSvcSetU32(&paParms[2], pClient->State.cbChunkSize);
1258 HGCMSvcSetU32(&paParms[3], 0 /* Compression type, not used yet */);
1259 HGCMSvcSetU32(&paParms[4], 0 /* Checksum type, not used yet */);
1260
1261 rc = VINF_SUCCESS;
1262 }
1263
1264 break;
1265 }
1266
1267 case VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT:
1268 {
1269 rc = sharedClipboardSvcMsgPeek(pClient, callHandle, cParms, paParms, false /*fWait*/);
1270 break;
1271 }
1272
1273 case VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT:
1274 {
1275 rc = sharedClipboardSvcMsgPeek(pClient, callHandle, cParms, paParms, true /*fWait*/);
1276 break;
1277 }
1278
1279 case VBOX_SHCL_GUEST_FN_MSG_GET:
1280 {
1281 rc = sharedClipboardSvcMsgGet(pClient, callHandle, cParms, paParms);
1282 break;
1283 }
1284
1285 case VBOX_SHCL_GUEST_FN_FORMATS_REPORT:
1286 {
1287 uint32_t uFormats = 0;
1288
1289 if (pClient->State.uProtocolVer == 0)
1290 {
1291 if (cParms != 1)
1292 {
1293 rc = VERR_INVALID_PARAMETER;
1294 }
1295 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT) /* uFormats */
1296 {
1297 rc = VERR_INVALID_PARAMETER;
1298 }
1299 else
1300 {
1301 rc = HGCMSvcGetU32(&paParms[0], &uFormats);
1302 }
1303 }
1304 else
1305 {
1306 if (cParms != 3)
1307 {
1308 rc = VERR_INVALID_PARAMETER;
1309 }
1310 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uContextID */
1311 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uFormats */
1312 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT) /* fFlags */
1313 {
1314 rc = VERR_INVALID_PARAMETER;
1315 }
1316 else
1317 {
1318 rc = HGCMSvcGetU32(&paParms[1], &uFormats);
1319
1320 /** @todo Handle rest. */
1321 }
1322 }
1323
1324 if (RT_SUCCESS(rc))
1325 {
1326 if ( sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_GUEST_TO_HOST
1327 && sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
1328 {
1329 rc = VERR_ACCESS_DENIED;
1330 }
1331 else if (uFormats != VBOX_SHCL_FMT_NONE) /* Only announce formats if we actually *have* formats to announce! */
1332 {
1333 rc = sharedClipboardSvcSetSource(pClient, SHCLSOURCE_REMOTE);
1334 if (RT_SUCCESS(rc))
1335 {
1336 if (g_ExtState.pfnExtension)
1337 {
1338 SHCLEXTPARMS parms;
1339 RT_ZERO(parms);
1340
1341 parms.uFormat = uFormats;
1342
1343 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE,
1344 &parms, sizeof(parms));
1345 }
1346
1347 SHCLCLIENTCMDCTX cmdCtx;
1348 RT_ZERO(cmdCtx);
1349
1350 SHCLFORMATDATA formatData;
1351 RT_ZERO(formatData);
1352
1353 formatData.uFormats = uFormats;
1354 Assert(formatData.uFormats != VBOX_SHCL_FMT_NONE); /* Sanity. */
1355
1356 rc = SharedClipboardSvcImplFormatAnnounce(pClient, &cmdCtx, &formatData);
1357 }
1358 }
1359 }
1360
1361 break;
1362 }
1363
1364 case VBOX_SHCL_GUEST_FN_DATA_READ:
1365 {
1366 if (cParms != VBOX_SHCL_CPARMS_READ_DATA)
1367 {
1368 rc = VERR_INVALID_PARAMETER;
1369 }
1370 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
1371 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
1372 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* size */
1373 )
1374 {
1375 rc = VERR_INVALID_PARAMETER;
1376 }
1377 else
1378 {
1379 if ( sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_HOST_TO_GUEST
1380 && sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
1381 {
1382 rc = VERR_ACCESS_DENIED;
1383 break;
1384 }
1385
1386 uint32_t uFormat;
1387 rc = HGCMSvcGetU32(&paParms[0], &uFormat);
1388 if (RT_SUCCESS(rc))
1389 {
1390 void *pv;
1391 uint32_t cb;
1392 rc = HGCMSvcGetBuf(&paParms[1], &pv, &cb);
1393 if (RT_SUCCESS(rc))
1394 {
1395 uint32_t cbActual = 0;
1396
1397 /* If there is a service extension active, try reading data from it first. */
1398 if (g_ExtState.pfnExtension)
1399 {
1400 SHCLEXTPARMS parms;
1401 RT_ZERO(parms);
1402
1403 parms.uFormat = uFormat;
1404 parms.u.pvData = pv;
1405 parms.cbData = cb;
1406
1407 g_ExtState.fReadingData = true;
1408
1409 /* Read clipboard data from the extension. */
1410 rc = g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1411 &parms, sizeof(parms));
1412
1413 LogFlowFunc(("g_ExtState.fDelayedAnnouncement=%RTbool, g_ExtState.uDelayedFormats=0x%x\n",
1414 g_ExtState.fDelayedAnnouncement, g_ExtState.uDelayedFormats));
1415
1416 /* Did the extension send the clipboard formats yet?
1417 * Otherwise, do this now. */
1418 if (g_ExtState.fDelayedAnnouncement)
1419 {
1420 SHCLFORMATDATA formatData;
1421 RT_ZERO(formatData);
1422
1423 formatData.uFormats = g_ExtState.uDelayedFormats;
1424 Assert(formatData.uFormats != VBOX_SHCL_FMT_NONE); /* There better is *any* format here now. */
1425
1426 int rc2 = sharedClipboardSvcFormatsReport(pClient, &formatData);
1427 AssertRC(rc2);
1428
1429 g_ExtState.fDelayedAnnouncement = false;
1430 g_ExtState.uDelayedFormats = 0;
1431 }
1432
1433 g_ExtState.fReadingData = false;
1434
1435 if (RT_SUCCESS(rc))
1436 {
1437 cbActual = parms.cbData;
1438 }
1439 }
1440
1441 /* Note: The host clipboard *always* has precedence over the service extension above,
1442 * so data which has been read above might get overridden by the host clipboard eventually. */
1443
1444 SHCLCLIENTCMDCTX cmdCtx;
1445 RT_ZERO(cmdCtx);
1446
1447 /* Release any other pending read, as we only
1448 * support one pending read at one time. */
1449 if (RT_SUCCESS(rc))
1450 {
1451 SHCLDATABLOCK dataBlock;
1452 RT_ZERO(dataBlock);
1453
1454 dataBlock.pvData = pv;
1455 dataBlock.cbData = cb;
1456 dataBlock.uFormat = uFormat;
1457
1458 rc = SharedClipboardSvcImplReadData(pClient, &cmdCtx, &dataBlock, &cbActual);
1459 if (RT_SUCCESS(rc))
1460 HGCMSvcSetU32(&paParms[2], cbActual);
1461 }
1462 }
1463 }
1464 }
1465
1466 break;
1467 }
1468
1469 case VBOX_SHCL_GUEST_FN_DATA_WRITE:
1470 {
1471 rc = sharedClipboardSvcGetDataWrite(pClient, cParms, paParms);
1472 break;
1473 }
1474
1475 default:
1476 {
1477#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1478 rc = sharedClipboardSvcTransferHandler(pClient, callHandle, u32Function, cParms, paParms, tsArrival);
1479#else
1480 rc = VERR_NOT_IMPLEMENTED;
1481#endif
1482 break;
1483 }
1484 }
1485
1486 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
1487
1488 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1489 g_pHelpers->pfnCallComplete(callHandle, rc);
1490}
1491
1492/**
1493 * Initializes a Shared Clipboard service's client state.
1494 *
1495 * @returns VBox status code.
1496 * @param pClientState Client state to initialize.
1497 * @param uClientID Client ID (HGCM) to use for this client state.
1498 */
1499static int sharedClipboardSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID)
1500{
1501 LogFlowFuncEnter();
1502
1503 sharedClipboardSvcClientStateReset(pClientState);
1504
1505 /* Register the client. */
1506 pClientState->uClientID = uClientID;
1507
1508 return VINF_SUCCESS;
1509}
1510
1511/**
1512 * Destroys a Shared Clipboard service's client state.
1513 *
1514 * @returns VBox status code.
1515 * @param pClientState Client state to destroy.
1516 */
1517static int sharedClipboardSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState)
1518{
1519 RT_NOREF(pClientState);
1520
1521 LogFlowFuncEnter();
1522
1523 return VINF_SUCCESS;
1524}
1525
1526/**
1527 * Resets a Shared Clipboard service's client state.
1528 *
1529 * @param pClientState Client state to reset.
1530 */
1531static void sharedClipboardSvcClientStateReset(PSHCLCLIENTSTATE pClientState)
1532{
1533 LogFlowFuncEnter();
1534
1535 pClientState->uProtocolVer = 0;
1536 pClientState->cbChunkSize = _64K; /** Make this configurable. */
1537
1538#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1539 pClientState->Transfers.enmTransferDir = SHCLTRANSFERDIR_UNKNOWN;
1540#else
1541 RT_NOREF(pClientState);
1542#endif
1543}
1544
1545/*
1546 * We differentiate between a function handler for the guest and one for the host.
1547 */
1548static DECLCALLBACK(int) svcHostCall(void *,
1549 uint32_t u32Function,
1550 uint32_t cParms,
1551 VBOXHGCMSVCPARM paParms[])
1552{
1553 int rc = VINF_SUCCESS;
1554
1555 LogFlowFunc(("u32Function=%RU32 (%s), cParms=%RU32, paParms=%p\n",
1556 u32Function, VBoxShClHostFunctionToStr(u32Function), cParms, paParms));
1557
1558 switch (u32Function)
1559 {
1560 case VBOX_SHCL_HOST_FN_SET_MODE:
1561 {
1562 if (cParms != 1)
1563 {
1564 rc = VERR_INVALID_PARAMETER;
1565 }
1566 else
1567 {
1568 uint32_t u32Mode = VBOX_SHCL_MODE_OFF;
1569
1570 rc = HGCMSvcGetU32(&paParms[0], &u32Mode);
1571 if (RT_SUCCESS(rc))
1572 rc = sharedClipboardSvcModeSet(u32Mode);
1573 }
1574
1575 break;
1576 }
1577
1578 case VBOX_SHCL_HOST_FN_SET_HEADLESS:
1579 {
1580 if (cParms != 1)
1581 {
1582 rc = VERR_INVALID_PARAMETER;
1583 }
1584 else
1585 {
1586 uint32_t uHeadless;
1587 rc = HGCMSvcGetU32(&paParms[0], &uHeadless);
1588 if (RT_SUCCESS(rc))
1589 {
1590 g_fHeadless = RT_BOOL(uHeadless);
1591 LogRel(("Shared Clipboard: Service running in %s mode\n", g_fHeadless ? "headless" : "normal"));
1592 }
1593 }
1594 break;
1595 }
1596
1597 default:
1598 {
1599#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1600 rc = sharedClipboardSvcTransferHostHandler(u32Function, cParms, paParms);
1601#else
1602 rc = VERR_NOT_IMPLEMENTED;
1603#endif
1604 break;
1605 }
1606 }
1607
1608 LogFlowFuncLeaveRC(rc);
1609 return rc;
1610}
1611
1612#ifndef UNIT_TEST
1613/**
1614 * SSM descriptor table for the SHCLCLIENTSTATE structure.
1615 */
1616static SSMFIELD const s_aShClSSMClientState[] =
1617{
1618 SSMFIELD_ENTRY(SHCLCLIENTSTATE, uProtocolVer),
1619 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
1620 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
1621 SSMFIELD_ENTRY_TERM()
1622};
1623
1624/**
1625 * SSM descriptor table for the SHCLCLIENTURISTATE structure.
1626 */
1627static SSMFIELD const s_aShClSSMClientURIState[] =
1628{
1629 SSMFIELD_ENTRY(SHCLCLIENTTRANSFERSTATE, enmTransferDir),
1630 SSMFIELD_ENTRY_TERM()
1631};
1632
1633/**
1634 * SSM descriptor table for the header of the SHCLCLIENTMSG structure.
1635 * The actual message parameters will be serialized separately.
1636 */
1637static SSMFIELD const s_aShClSSMClientMsgHdr[] =
1638{
1639 SSMFIELD_ENTRY(SHCLCLIENTMSG, uMsg),
1640 SSMFIELD_ENTRY(SHCLCLIENTMSG, cParms),
1641 SSMFIELD_ENTRY_TERM()
1642};
1643
1644/**
1645 * SSM descriptor table for the VBOXSHCLMSGCTX structure.
1646 */
1647static SSMFIELD const s_aShClSSMClientMsgCtx[] =
1648{
1649 SSMFIELD_ENTRY(SHCLMSGCTX, uContextID),
1650 SSMFIELD_ENTRY_TERM()
1651};
1652#endif /* !UNIT_TEST */
1653
1654static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
1655{
1656 RT_NOREF(u32ClientID);
1657
1658#ifndef UNIT_TEST
1659 /*
1660 * When the state will be restored, pending requests will be reissued
1661 * by VMMDev. The service therefore must save state as if there were no
1662 * pending request.
1663 * Pending requests, if any, will be completed in svcDisconnect.
1664 */
1665 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1666
1667 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1668 AssertPtr(pClient);
1669
1670 /* Write Shared Clipboard saved state version. */
1671 SSMR3PutU32(pSSM, VBOX_SHCL_SSM_VER_1);
1672
1673 int rc = SSMR3PutStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
1674 AssertRCReturn(rc, rc);
1675
1676 rc = SSMR3PutStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientURIState[0], NULL);
1677 AssertRCReturn(rc, rc);
1678
1679 /* Serialize the client's internal message queue. */
1680 rc = SSMR3PutU64(pSSM, (uint64_t)pClient->queueMsg.size());
1681 AssertRCReturn(rc, rc);
1682
1683 for (size_t i = 0; i < pClient->queueMsg.size(); i++)
1684 {
1685 PSHCLCLIENTMSG pMsg = pClient->queueMsg.at(i);
1686 AssertPtr(pMsg);
1687
1688 rc = SSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
1689 AssertRCReturn(rc, rc);
1690
1691 rc = SSMR3PutStructEx(pSSM, &pMsg->Ctx, sizeof(SHCLMSGCTX), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
1692 AssertRCReturn(rc, rc);
1693
1694 for (uint32_t p = 0; p < pMsg->cParms; p++)
1695 {
1696 rc = HGCMSvcSSMR3Put(&pMsg->paParms[p], pSSM);
1697 AssertRCReturn(rc, rc);
1698 }
1699 }
1700
1701#else /* UNIT_TEST */
1702 RT_NOREF3(u32ClientID, pvClient, pSSM);
1703#endif /* UNIT_TEST */
1704 return VINF_SUCCESS;
1705}
1706
1707#ifndef UNIT_TEST
1708static int svcLoadStateV0(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
1709{
1710 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
1711
1712 uint32_t uMarker;
1713 int rc = SSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
1714 AssertRC(rc);
1715 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
1716
1717 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Client ID */
1718 AssertRCReturn(rc, rc);
1719
1720 bool fValue;
1721 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgQuit */
1722 AssertRCReturn(rc, rc);
1723
1724 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgReadData */
1725 AssertRCReturn(rc, rc);
1726
1727 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgFormats */
1728 AssertRCReturn(rc, rc);
1729
1730 uint32_t fFormats;
1731 rc = SSMR3GetU32(pSSM, &fFormats); /* u32RequestedFormat */
1732 AssertRCReturn(rc, rc);
1733
1734 rc = SSMR3GetU32(pSSM, &uMarker); /* End marker. */
1735 AssertRCReturn(rc, rc);
1736 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
1737
1738 return VINF_SUCCESS;
1739}
1740#endif /* UNIT_TEST */
1741
1742static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
1743{
1744#ifndef UNIT_TEST
1745 RT_NOREF(u32ClientID, uVersion);
1746
1747 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1748
1749 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1750 AssertPtr(pClient);
1751
1752 /* Restore the client data. */
1753 uint32_t lenOrVer;
1754 int rc = SSMR3GetU32(pSSM, &lenOrVer);
1755 AssertRCReturn(rc, rc);
1756 if (lenOrVer == VBOX_SHCL_SSM_VER_0)
1757 {
1758 return svcLoadStateV0(u32ClientID, pvClient, pSSM, uVersion);
1759 }
1760 else if (lenOrVer == VBOX_SHCL_SSM_VER_1)
1761 {
1762 rc = SSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
1763 AssertRCReturn(rc, rc);
1764
1765 rc = SSMR3GetStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientURIState[0], NULL);
1766 AssertRCReturn(rc, rc);
1767
1768 /* Load the client's internal message queue. */
1769 uint64_t cMsg;
1770 rc = SSMR3GetU64(pSSM, &cMsg);
1771 AssertRCReturn(rc, rc);
1772
1773 for (uint64_t i = 0; i < cMsg; i++)
1774 {
1775 PSHCLCLIENTMSG pMsg = (PSHCLCLIENTMSG)RTMemAlloc(sizeof(SHCLCLIENTMSG));
1776 AssertPtrReturn(pMsg, VERR_NO_MEMORY);
1777
1778 rc = SSMR3GetStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
1779 AssertRCReturn(rc, rc);
1780
1781 rc = SSMR3GetStructEx(pSSM, &pMsg->Ctx, sizeof(SHCLMSGCTX), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
1782 AssertRCReturn(rc, rc);
1783
1784 pMsg->paParms = (PVBOXHGCMSVCPARM)RTMemAllocZ(sizeof(VBOXHGCMSVCPARM) * pMsg->cParms);
1785 AssertPtrReturn(pMsg->paParms, VERR_NO_MEMORY);
1786
1787 for (uint32_t p = 0; p < pMsg->cParms; p++)
1788 {
1789 rc = HGCMSvcSSMR3Get(&pMsg->paParms[p], pSSM);
1790 AssertRCReturn(rc, rc);
1791 }
1792
1793 rc = sharedClipboardSvcMsgAdd(pClient, pMsg, true /* fAppend */);
1794 AssertRCReturn(rc, rc);
1795 }
1796 }
1797 else
1798 {
1799 LogRel(("Shared Clipboard: Unknown saved state version (%#x)\n", lenOrVer));
1800 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1801 }
1802
1803 /* Actual host data are to be reported to guest (SYNC). */
1804 SharedClipboardSvcImplSync(pClient);
1805
1806#else /* UNIT_TEST */
1807 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
1808#endif /* UNIT_TEST */
1809 return VINF_SUCCESS;
1810}
1811
1812static DECLCALLBACK(int) extCallback(uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
1813{
1814 RT_NOREF(pvData, cbData);
1815
1816 LogFlowFunc(("u32Function=%RU32\n", u32Function));
1817
1818 int rc = VINF_SUCCESS;
1819
1820 /* Figure out if the client in charge for the service extension still is connected. */
1821 ClipboardClientMap::const_iterator itClient = g_mapClients.find(g_ExtState.uClientID);
1822 if (itClient != g_mapClients.end())
1823 {
1824 PSHCLCLIENT pClient = itClient->second;
1825 AssertPtr(pClient);
1826
1827 switch (u32Function)
1828 {
1829 /* The service extension announces formats to the guest. */
1830 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1831 {
1832 LogFlowFunc(("VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE: g_ExtState.fReadingData=%RTbool\n", g_ExtState.fReadingData));
1833 if (g_ExtState.fReadingData)
1834 {
1835 g_ExtState.fDelayedAnnouncement = true;
1836 g_ExtState.uDelayedFormats = u32Format;
1837 }
1838 else if (u32Format != VBOX_SHCL_FMT_NONE)
1839 {
1840 SHCLFORMATDATA formatData;
1841 RT_ZERO(formatData);
1842
1843 formatData.uFormats = u32Format;
1844
1845 rc = sharedClipboardSvcFormatsReport(pClient, &formatData);
1846 }
1847
1848 break;
1849 }
1850
1851 /* The service extension wants read data from the guest. */
1852 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1853 {
1854 SHCLDATAREQ dataReq;
1855 RT_ZERO(dataReq);
1856
1857 dataReq.uFmt = u32Format;
1858 dataReq.cbSize = _64K; /** @todo Make this more dynamic. */
1859
1860 rc = sharedClipboardSvcDataReadRequest(pClient, &dataReq, NULL /* puEvent */);
1861 break;
1862 }
1863
1864 default:
1865 /* Just skip other messages. */
1866 break;
1867 }
1868 }
1869 else
1870 rc = VERR_NOT_FOUND;
1871
1872 LogFlowFuncLeaveRC(rc);
1873 return rc;
1874}
1875
1876static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
1877{
1878 LogFlowFunc(("pfnExtension=%p\n", pfnExtension));
1879
1880 SHCLEXTPARMS parms;
1881 RT_ZERO(parms);
1882
1883 if (pfnExtension)
1884 {
1885 /* Install extension. */
1886 g_ExtState.pfnExtension = pfnExtension;
1887 g_ExtState.pvExtension = pvExtension;
1888
1889 parms.u.pfnCallback = extCallback;
1890
1891 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
1892 }
1893 else
1894 {
1895 if (g_ExtState.pfnExtension)
1896 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
1897
1898 /* Uninstall extension. */
1899 g_ExtState.pfnExtension = NULL;
1900 g_ExtState.pvExtension = NULL;
1901 }
1902
1903 return VINF_SUCCESS;
1904}
1905
1906extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
1907{
1908 int rc = VINF_SUCCESS;
1909
1910 LogFlowFunc(("ptable=%p\n", pTable));
1911
1912 if (!pTable)
1913 {
1914 rc = VERR_INVALID_PARAMETER;
1915 }
1916 else
1917 {
1918 LogFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", pTable->cbSize, pTable->u32Version));
1919
1920 if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1921 || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
1922 {
1923 rc = VERR_INVALID_PARAMETER;
1924 }
1925 else
1926 {
1927 g_pHelpers = pTable->pHelpers;
1928
1929 pTable->cbClient = sizeof(SHCLCLIENT);
1930
1931 pTable->pfnUnload = svcUnload;
1932 pTable->pfnConnect = svcConnect;
1933 pTable->pfnDisconnect = svcDisconnect;
1934 pTable->pfnCall = svcCall;
1935 pTable->pfnHostCall = svcHostCall;
1936 pTable->pfnSaveState = svcSaveState;
1937 pTable->pfnLoadState = svcLoadState;
1938 pTable->pfnRegisterExtension = svcRegisterExtension;
1939 pTable->pfnNotify = NULL;
1940 pTable->pvService = NULL;
1941
1942 /* Service specific initialization. */
1943 rc = svcInit();
1944 }
1945 }
1946
1947 return rc;
1948}
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