VirtualBox

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

Last change on this file since 80918 was 80918, 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.1 KB
Line 
1/* $Id: VBoxSharedClipboardSvc.cpp 80918 2019-09-20 10:27:30Z 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 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 (pFormats->uFormats & VBOX_SHCL_FMT_URI_LIST)
951 {
952 rc = sharedClipboardSvcTransferStart(pClient, SHCLTRANSFERDIR_WRITE, SHCLSOURCE_LOCAL,
953 NULL /* pTransfer */);
954 if (RT_FAILURE(rc))
955 LogRel(("Shared Clipboard: Initializing host write transfer failed with %Rrc\n", rc));
956 }
957#endif
958 if (RT_SUCCESS(rc))
959 rc = sharedClipboardSvcClientWakeup(pClient);
960 }
961 }
962 else
963 rc = VERR_NO_MEMORY;
964
965 LogFlowFuncLeaveRC(rc);
966 return rc;
967}
968
969int sharedClipboardSvcGetDataWrite(PSHCLCLIENT pClient, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
970{
971 LogFlowFuncEnter();
972
973 if ( sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_GUEST_TO_HOST
974 && sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
975 {
976 return VERR_NOT_SUPPORTED;
977 }
978
979 int rc;
980
981 SHCLDATABLOCK dataBlock;
982 RT_ZERO(dataBlock);
983
984 SHCLCLIENTCMDCTX cmdCtx;
985 RT_ZERO(cmdCtx);
986
987 if (pClient->State.uProtocolVer == 0) /* Legacy protocol */
988 {
989 if (cParms < 2)
990 {
991 rc = VERR_INVALID_PARAMETER;
992 }
993 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
994 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR) /* ptr */
995 {
996 rc = VERR_INVALID_PARAMETER;
997 }
998 else
999 {
1000 rc = HGCMSvcGetU32(&paParms[0], &dataBlock.uFormat);
1001 if (RT_SUCCESS(rc))
1002 rc = HGCMSvcGetBuf(&paParms[1], &dataBlock.pvData, &dataBlock.cbData);
1003 }
1004 }
1005 else
1006 {
1007 if (cParms < VBOX_SHCL_CPARMS_WRITE_DATA)
1008 {
1009 rc = VERR_INVALID_PARAMETER;
1010 }
1011 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uContext */
1012 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uFormat */
1013 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* cbData */
1014 || paParms[3].type != VBOX_HGCM_SVC_PARM_PTR) /* pvData */
1015 {
1016 rc = VERR_INVALID_PARAMETER;
1017 }
1018 else
1019 {
1020 rc = HGCMSvcGetU32(&paParms[0], &cmdCtx.uContextID);
1021 if (RT_SUCCESS(rc))
1022 rc = HGCMSvcGetU32(&paParms[1], &dataBlock.uFormat);
1023 if (RT_SUCCESS(rc))
1024 rc = HGCMSvcGetBuf(&paParms[3], &dataBlock.pvData, &dataBlock.cbData);
1025
1026 /** @todo Handle the rest. */
1027 }
1028 }
1029
1030 if (RT_SUCCESS(rc))
1031 {
1032 if (g_ExtState.pfnExtension)
1033 {
1034 SHCLEXTPARMS parms;
1035 RT_ZERO(parms);
1036
1037 parms.uFormat = dataBlock.uFormat;
1038 parms.u.pvData = dataBlock.pvData;
1039 parms.cbData = dataBlock.cbData;
1040
1041 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_WRITE, &parms, sizeof(parms));
1042 }
1043
1044 rc = SharedClipboardSvcImplWriteData(pClient, &cmdCtx, &dataBlock);
1045 }
1046
1047 LogFlowFuncLeaveRC(rc);
1048 return rc;
1049}
1050
1051int sharedClipboardSvcSetSource(PSHCLCLIENT pClient, SHCLSOURCE enmSource)
1052{
1053 if (!pClient) /* If no client connected (anymore), bail out. */
1054 return VINF_SUCCESS;
1055
1056 int rc = VINF_SUCCESS;
1057
1058 if (VBoxSvcClipboardLock())
1059 {
1060 pClient->State.enmSource = enmSource;
1061
1062 LogFlowFunc(("Source of client %RU32 is now %RU32\n", pClient->State.uClientID, pClient->State.enmSource));
1063
1064 VBoxSvcClipboardUnlock();
1065 }
1066
1067 LogFlowFuncLeaveRC(rc);
1068 return rc;
1069}
1070
1071static int svcInit(void)
1072{
1073 int rc = RTCritSectInit(&g_CritSect);
1074
1075 if (RT_SUCCESS(rc))
1076 {
1077 sharedClipboardSvcModeSet(VBOX_SHCL_MODE_OFF);
1078
1079 rc = SharedClipboardSvcImplInit();
1080
1081 /* Clean up on failure, because 'svnUnload' will not be called
1082 * if the 'svcInit' returns an error.
1083 */
1084 if (RT_FAILURE(rc))
1085 {
1086 RTCritSectDelete(&g_CritSect);
1087 }
1088 }
1089
1090 return rc;
1091}
1092
1093static DECLCALLBACK(int) svcUnload(void *)
1094{
1095 LogFlowFuncEnter();
1096
1097 SharedClipboardSvcImplDestroy();
1098
1099 RTCritSectDelete(&g_CritSect);
1100
1101 return VINF_SUCCESS;
1102}
1103
1104static DECLCALLBACK(int) svcDisconnect(void *, uint32_t u32ClientID, void *pvClient)
1105{
1106 RT_NOREF(u32ClientID, pvClient);
1107
1108 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1109
1110 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1111 AssertPtr(pClient);
1112
1113#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1114 PSHCLTRANSFER pTransfer = SharedClipboardTransferCtxGetTransfer(&pClient->TransferCtx, 0 /* Index*/);
1115 if (pTransfer)
1116 sharedClipboardSvcTransferAreaDetach(&pClient->State, pTransfer);
1117
1118 SharedClipboardTransferCtxDestroy(&pClient->TransferCtx);
1119#endif
1120
1121 SharedClipboardSvcImplDisconnect(pClient);
1122
1123 sharedClipboardSvcClientStateReset(&pClient->State);
1124 sharedClipboardSvcClientStateDestroy(&pClient->State);
1125
1126 SharedClipboardEventSourceDestroy(&pClient->Events);
1127
1128 ClipboardClientMap::iterator itClient = g_mapClients.find(u32ClientID);
1129 if (itClient != g_mapClients.end())
1130 {
1131 g_mapClients.erase(itClient);
1132 }
1133 else
1134 AssertFailed();
1135
1136 return VINF_SUCCESS;
1137}
1138
1139static DECLCALLBACK(int) svcConnect(void *, uint32_t u32ClientID, void *pvClient, uint32_t fRequestor, bool fRestoring)
1140{
1141 RT_NOREF(fRequestor, fRestoring);
1142
1143 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1144 AssertPtr(pvClient);
1145
1146 /* Assign the client ID. */
1147 pClient->State.uClientID = u32ClientID;
1148
1149 /* Create the client's own event source. */
1150 int rc = SharedClipboardEventSourceCreate(&pClient->Events, 0 /* ID, ignored */);
1151 if (RT_SUCCESS(rc))
1152 {
1153 LogFlowFunc(("[Client %RU32] Using event source %RU32\n", u32ClientID, pClient->Events.uID));
1154
1155 /* Reset the client state. */
1156 sharedClipboardSvcClientStateReset(&pClient->State);
1157
1158 /* (Re-)initialize the client state. */
1159 rc = sharedClipboardSvcClientStateInit(&pClient->State, u32ClientID);
1160 if (RT_SUCCESS(rc))
1161 {
1162 rc = SharedClipboardSvcImplConnect(pClient, VBoxSvcClipboardGetHeadless());
1163#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1164 if (RT_SUCCESS(rc))
1165 rc = SharedClipboardTransferCtxInit(&pClient->TransferCtx);
1166#endif
1167 if (RT_SUCCESS(rc))
1168 {
1169 /* Assign weak pointer to client map .*/
1170 g_mapClients[u32ClientID] = pClient; /** @todo Handle OOM / collisions? */
1171
1172 /* For now we ASSUME that the first client ever connected is in charge for
1173 * communicating withe the service extension.
1174 *
1175 ** @todo This needs to be fixed ASAP w/o breaking older guest / host combos. */
1176 if (g_ExtState.uClientID == 0)
1177 g_ExtState.uClientID = u32ClientID;
1178 }
1179 }
1180 }
1181
1182 LogFlowFuncLeaveRC(rc);
1183 return rc;
1184}
1185
1186static DECLCALLBACK(void) svcCall(void *,
1187 VBOXHGCMCALLHANDLE callHandle,
1188 uint32_t u32ClientID,
1189 void *pvClient,
1190 uint32_t u32Function,
1191 uint32_t cParms,
1192 VBOXHGCMSVCPARM paParms[],
1193 uint64_t tsArrival)
1194{
1195 RT_NOREF(u32ClientID, pvClient, tsArrival);
1196
1197 int rc = VINF_SUCCESS;
1198
1199 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1200 AssertPtr(pClient);
1201
1202 LogFunc(("u32ClientID=%RU32 (proto %RU32), fn=%RU32 (%s), cParms=%RU32, paParms=%p\n",
1203 u32ClientID, pClient->State.uProtocolVer, u32Function, VBoxShClGuestMsgToStr(u32Function), cParms, paParms));
1204
1205#ifdef DEBUG
1206 uint32_t i;
1207
1208 for (i = 0; i < cParms; i++)
1209 {
1210 /** @todo parameters other than 32 bit */
1211 LogFunc((" paParms[%d]: type %RU32 - value %RU32\n", i, paParms[i].type, paParms[i].u.uint32));
1212 }
1213#endif
1214
1215 switch (u32Function)
1216 {
1217 case VBOX_SHCL_GUEST_FN_GET_HOST_MSG_OLD:
1218 {
1219 rc = sharedClipboardSvcMsgGetOld(pClient, callHandle, cParms, paParms);
1220 break;
1221 }
1222
1223 case VBOX_SHCL_GUEST_FN_CONNECT:
1224 {
1225 if (cParms != VBOX_SHCL_CPARMS_CONNECT)
1226 {
1227 rc = VERR_INVALID_PARAMETER;
1228 }
1229 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uProtocolVer */
1230 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uProtocolFlags */
1231 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* cbChunkSize */
1232 || paParms[3].type != VBOX_HGCM_SVC_PARM_32BIT /* enmCompression */
1233 || paParms[4].type != VBOX_HGCM_SVC_PARM_32BIT) /* enmChecksumType */
1234 {
1235 rc = VERR_INVALID_PARAMETER;
1236 }
1237 else if (sharedClipboardSvcGetMode() == VBOX_SHCL_MODE_OFF)
1238 {
1239 rc = VERR_ACCESS_DENIED;
1240 }
1241 else
1242 {
1243 /* Update the protocol version and tell the guest. */
1244 pClient->State.uProtocolVer = 1;
1245
1246 LogFlowFunc(("Now using protocol v%RU32\n", pClient->State.uProtocolVer));
1247
1248 HGCMSvcSetU32(&paParms[0], pClient->State.uProtocolVer);
1249 HGCMSvcSetU32(&paParms[1], 0 /* Procotol flags, not used yet */);
1250 HGCMSvcSetU32(&paParms[2], pClient->State.cbChunkSize);
1251 HGCMSvcSetU32(&paParms[3], 0 /* Compression type, not used yet */);
1252 HGCMSvcSetU32(&paParms[4], 0 /* Checksum type, not used yet */);
1253
1254 rc = VINF_SUCCESS;
1255 }
1256
1257 break;
1258 }
1259
1260 case VBOX_SHCL_GUEST_FN_MSG_PEEK_NOWAIT:
1261 {
1262 rc = sharedClipboardSvcMsgPeek(pClient, callHandle, cParms, paParms, false /*fWait*/);
1263 break;
1264 }
1265
1266 case VBOX_SHCL_GUEST_FN_MSG_PEEK_WAIT:
1267 {
1268 rc = sharedClipboardSvcMsgPeek(pClient, callHandle, cParms, paParms, true /*fWait*/);
1269 break;
1270 }
1271
1272 case VBOX_SHCL_GUEST_FN_MSG_GET:
1273 {
1274 rc = sharedClipboardSvcMsgGet(pClient, callHandle, cParms, paParms);
1275 break;
1276 }
1277
1278 case VBOX_SHCL_GUEST_FN_FORMATS_REPORT:
1279 {
1280 uint32_t uFormats = 0;
1281
1282 if (pClient->State.uProtocolVer == 0)
1283 {
1284 if (cParms != 1)
1285 {
1286 rc = VERR_INVALID_PARAMETER;
1287 }
1288 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT) /* uFormats */
1289 {
1290 rc = VERR_INVALID_PARAMETER;
1291 }
1292 else
1293 {
1294 rc = HGCMSvcGetU32(&paParms[0], &uFormats);
1295 }
1296 }
1297 else
1298 {
1299 if (cParms != 3)
1300 {
1301 rc = VERR_INVALID_PARAMETER;
1302 }
1303 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* uContextID */
1304 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* uFormats */
1305 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT) /* fFlags */
1306 {
1307 rc = VERR_INVALID_PARAMETER;
1308 }
1309 else
1310 {
1311 rc = HGCMSvcGetU32(&paParms[1], &uFormats);
1312
1313 /** @todo Handle rest. */
1314 }
1315 }
1316
1317 if (RT_SUCCESS(rc))
1318 {
1319 if ( sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_GUEST_TO_HOST
1320 && sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
1321 {
1322 rc = VERR_ACCESS_DENIED;
1323 }
1324 else
1325 {
1326 rc = sharedClipboardSvcSetSource(pClient, SHCLSOURCE_REMOTE);
1327 if (RT_SUCCESS(rc))
1328 {
1329 if (g_ExtState.pfnExtension)
1330 {
1331 SHCLEXTPARMS parms;
1332 RT_ZERO(parms);
1333
1334 parms.uFormat = uFormats;
1335
1336 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE, &parms, sizeof(parms));
1337 }
1338
1339 SHCLCLIENTCMDCTX cmdCtx;
1340 RT_ZERO(cmdCtx);
1341
1342 SHCLFORMATDATA formatData;
1343 RT_ZERO(formatData);
1344
1345 formatData.uFormats = uFormats;
1346
1347 rc = SharedClipboardSvcImplFormatAnnounce(pClient, &cmdCtx, &formatData);
1348 }
1349 }
1350 }
1351
1352 break;
1353 }
1354
1355 case VBOX_SHCL_GUEST_FN_DATA_READ:
1356 {
1357 if (cParms != VBOX_SHCL_CPARMS_READ_DATA)
1358 {
1359 rc = VERR_INVALID_PARAMETER;
1360 }
1361 else if ( paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* format */
1362 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR /* ptr */
1363 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* size */
1364 )
1365 {
1366 rc = VERR_INVALID_PARAMETER;
1367 }
1368 else
1369 {
1370 if ( sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_HOST_TO_GUEST
1371 && sharedClipboardSvcGetMode() != VBOX_SHCL_MODE_BIDIRECTIONAL)
1372 {
1373 rc = VERR_ACCESS_DENIED;
1374 break;
1375 }
1376
1377 uint32_t uFormat;
1378 rc = HGCMSvcGetU32(&paParms[0], &uFormat);
1379 if (RT_SUCCESS(rc))
1380 {
1381 void *pv;
1382 uint32_t cb;
1383 rc = HGCMSvcGetBuf(&paParms[1], &pv, &cb);
1384 if (RT_SUCCESS(rc))
1385 {
1386 uint32_t cbActual = 0;
1387
1388 /* If there is a service extension active, try reading data from it first. */
1389 if (g_ExtState.pfnExtension)
1390 {
1391 SHCLEXTPARMS parms;
1392 RT_ZERO(parms);
1393
1394 parms.uFormat = uFormat;
1395 parms.u.pvData = pv;
1396 parms.cbData = cb;
1397
1398 g_ExtState.fReadingData = true;
1399
1400 /* Read clipboard data from the extension. */
1401 rc = g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_DATA_READ,
1402 &parms, sizeof(parms));
1403
1404 LogFlowFunc(("g_ExtState.fDelayedAnnouncement=%RTbool, g_ExtState.uDelayedFormats=0x%x\n",
1405 g_ExtState.fDelayedAnnouncement, g_ExtState.uDelayedFormats));
1406
1407 /* Did the extension send the clipboard formats yet?
1408 * Otherwise, do this now. */
1409 if (g_ExtState.fDelayedAnnouncement)
1410 {
1411 SHCLFORMATDATA formatData;
1412 RT_ZERO(formatData);
1413
1414 formatData.uFormats = g_ExtState.uDelayedFormats;
1415
1416 int rc2 = sharedClipboardSvcFormatsReport(pClient, &formatData);
1417 AssertRC(rc2);
1418
1419 g_ExtState.fDelayedAnnouncement = false;
1420 g_ExtState.uDelayedFormats = 0;
1421 }
1422
1423 g_ExtState.fReadingData = false;
1424
1425 if (RT_SUCCESS(rc))
1426 {
1427 cbActual = parms.cbData;
1428 }
1429 }
1430
1431 /* Note: The host clipboard *always* has precedence over the service extension above,
1432 * so data which has been read above might get overridden by the host clipboard eventually. */
1433
1434 SHCLCLIENTCMDCTX cmdCtx;
1435 RT_ZERO(cmdCtx);
1436
1437 /* Release any other pending read, as we only
1438 * support one pending read at one time. */
1439 if (RT_SUCCESS(rc))
1440 {
1441 SHCLDATABLOCK dataBlock;
1442 RT_ZERO(dataBlock);
1443
1444 dataBlock.pvData = pv;
1445 dataBlock.cbData = cb;
1446 dataBlock.uFormat = uFormat;
1447
1448 rc = SharedClipboardSvcImplReadData(pClient, &cmdCtx, &dataBlock, &cbActual);
1449 if (RT_SUCCESS(rc))
1450 HGCMSvcSetU32(&paParms[2], cbActual);
1451 }
1452 }
1453 }
1454 }
1455
1456 break;
1457 }
1458
1459 case VBOX_SHCL_GUEST_FN_DATA_WRITE:
1460 {
1461 rc = sharedClipboardSvcGetDataWrite(pClient, cParms, paParms);
1462 break;
1463 }
1464
1465 default:
1466 {
1467#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1468 rc = sharedClipboardSvcTransferHandler(pClient, callHandle, u32Function, cParms, paParms, tsArrival);
1469#else
1470 rc = VERR_NOT_IMPLEMENTED;
1471#endif
1472 break;
1473 }
1474 }
1475
1476 LogFlowFunc(("[Client %RU32] rc=%Rrc\n", pClient->State.uClientID, rc));
1477
1478 if (rc != VINF_HGCM_ASYNC_EXECUTE)
1479 g_pHelpers->pfnCallComplete(callHandle, rc);
1480}
1481
1482/**
1483 * Initializes a Shared Clipboard service's client state.
1484 *
1485 * @returns VBox status code.
1486 * @param pClientState Client state to initialize.
1487 * @param uClientID Client ID (HGCM) to use for this client state.
1488 */
1489static int sharedClipboardSvcClientStateInit(PSHCLCLIENTSTATE pClientState, uint32_t uClientID)
1490{
1491 LogFlowFuncEnter();
1492
1493 sharedClipboardSvcClientStateReset(pClientState);
1494
1495 /* Register the client. */
1496 pClientState->uClientID = uClientID;
1497
1498 return VINF_SUCCESS;
1499}
1500
1501/**
1502 * Destroys a Shared Clipboard service's client state.
1503 *
1504 * @returns VBox status code.
1505 * @param pClientState Client state to destroy.
1506 */
1507static int sharedClipboardSvcClientStateDestroy(PSHCLCLIENTSTATE pClientState)
1508{
1509 RT_NOREF(pClientState);
1510
1511 LogFlowFuncEnter();
1512
1513 return VINF_SUCCESS;
1514}
1515
1516/**
1517 * Resets a Shared Clipboard service's client state.
1518 *
1519 * @param pClientState Client state to reset.
1520 */
1521static void sharedClipboardSvcClientStateReset(PSHCLCLIENTSTATE pClientState)
1522{
1523 LogFlowFuncEnter();
1524
1525 pClientState->uProtocolVer = 0;
1526 pClientState->cbChunkSize = _64K; /** Make this configurable. */
1527
1528#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1529 pClientState->Transfers.enmTransferDir = SHCLTRANSFERDIR_UNKNOWN;
1530#else
1531 RT_NOREF(pClientState);
1532#endif
1533}
1534
1535/*
1536 * We differentiate between a function handler for the guest and one for the host.
1537 */
1538static DECLCALLBACK(int) svcHostCall(void *,
1539 uint32_t u32Function,
1540 uint32_t cParms,
1541 VBOXHGCMSVCPARM paParms[])
1542{
1543 int rc = VINF_SUCCESS;
1544
1545 LogFlowFunc(("u32Function=%RU32 (%s), cParms=%RU32, paParms=%p\n",
1546 u32Function, VBoxShClHostMsgToStr(u32Function), cParms, paParms));
1547
1548 switch (u32Function)
1549 {
1550 case VBOX_SHCL_HOST_FN_SET_MODE:
1551 {
1552 if (cParms != 1)
1553 {
1554 rc = VERR_INVALID_PARAMETER;
1555 }
1556 else
1557 {
1558 uint32_t u32Mode = VBOX_SHCL_MODE_OFF;
1559
1560 rc = HGCMSvcGetU32(&paParms[0], &u32Mode);
1561 if (RT_SUCCESS(rc))
1562 rc = sharedClipboardSvcModeSet(u32Mode);
1563 }
1564
1565 break;
1566 }
1567
1568 case VBOX_SHCL_HOST_FN_SET_HEADLESS:
1569 {
1570 if (cParms != 1)
1571 {
1572 rc = VERR_INVALID_PARAMETER;
1573 }
1574 else
1575 {
1576 uint32_t uHeadless;
1577 rc = HGCMSvcGetU32(&paParms[0], &uHeadless);
1578 if (RT_SUCCESS(rc))
1579 {
1580 g_fHeadless = RT_BOOL(uHeadless);
1581 LogRel(("Shared Clipboard: Service running in %s mode\n", g_fHeadless ? "headless" : "normal"));
1582 }
1583 }
1584 break;
1585 }
1586
1587 default:
1588 {
1589#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1590 rc = sharedClipboardSvcTransferHostHandler(u32Function, cParms, paParms);
1591#else
1592 rc = VERR_NOT_IMPLEMENTED;
1593#endif
1594 break;
1595 }
1596 }
1597
1598 LogFlowFuncLeaveRC(rc);
1599 return rc;
1600}
1601
1602#ifndef UNIT_TEST
1603/**
1604 * SSM descriptor table for the SHCLCLIENTSTATE structure.
1605 */
1606static SSMFIELD const s_aShClSSMClientState[] =
1607{
1608 SSMFIELD_ENTRY(SHCLCLIENTSTATE, uProtocolVer),
1609 SSMFIELD_ENTRY(SHCLCLIENTSTATE, cbChunkSize),
1610 SSMFIELD_ENTRY(SHCLCLIENTSTATE, enmSource),
1611 SSMFIELD_ENTRY_TERM()
1612};
1613
1614/**
1615 * SSM descriptor table for the SHCLCLIENTURISTATE structure.
1616 */
1617static SSMFIELD const s_aShClSSMClientURIState[] =
1618{
1619 SSMFIELD_ENTRY(SHCLCLIENTTRANSFERSTATE, enmTransferDir),
1620 SSMFIELD_ENTRY_TERM()
1621};
1622
1623/**
1624 * SSM descriptor table for the header of the SHCLCLIENTMSG structure.
1625 * The actual message parameters will be serialized separately.
1626 */
1627static SSMFIELD const s_aShClSSMClientMsgHdr[] =
1628{
1629 SSMFIELD_ENTRY(SHCLCLIENTMSG, uMsg),
1630 SSMFIELD_ENTRY(SHCLCLIENTMSG, cParms),
1631 SSMFIELD_ENTRY_TERM()
1632};
1633
1634/**
1635 * SSM descriptor table for the VBOXSHCLMSGCTX structure.
1636 */
1637static SSMFIELD const s_aShClSSMClientMsgCtx[] =
1638{
1639 SSMFIELD_ENTRY(SHCLMSGCTX, uContextID),
1640 SSMFIELD_ENTRY_TERM()
1641};
1642#endif /* !UNIT_TEST */
1643
1644static DECLCALLBACK(int) svcSaveState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM)
1645{
1646 RT_NOREF(u32ClientID);
1647
1648#ifndef UNIT_TEST
1649 /*
1650 * When the state will be restored, pending requests will be reissued
1651 * by VMMDev. The service therefore must save state as if there were no
1652 * pending request.
1653 * Pending requests, if any, will be completed in svcDisconnect.
1654 */
1655 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1656
1657 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1658 AssertPtr(pClient);
1659
1660 /* Write Shared Clipboard saved state version. */
1661 SSMR3PutU32(pSSM, VBOX_SHCL_SSM_VER_1);
1662
1663 int rc = SSMR3PutStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
1664 AssertRCReturn(rc, rc);
1665
1666 rc = SSMR3PutStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientURIState[0], NULL);
1667 AssertRCReturn(rc, rc);
1668
1669 /* Serialize the client's internal message queue. */
1670 rc = SSMR3PutU64(pSSM, (uint64_t)pClient->queueMsg.size());
1671 AssertRCReturn(rc, rc);
1672
1673 for (size_t i = 0; i < pClient->queueMsg.size(); i++)
1674 {
1675 PSHCLCLIENTMSG pMsg = pClient->queueMsg.at(i);
1676 AssertPtr(pMsg);
1677
1678 rc = SSMR3PutStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
1679 AssertRCReturn(rc, rc);
1680
1681 rc = SSMR3PutStructEx(pSSM, &pMsg->Ctx, sizeof(SHCLMSGCTX), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
1682 AssertRCReturn(rc, rc);
1683
1684 for (uint32_t p = 0; p < pMsg->cParms; p++)
1685 {
1686 rc = HGCMSvcSSMR3Put(&pMsg->paParms[p], pSSM);
1687 AssertRCReturn(rc, rc);
1688 }
1689 }
1690
1691#else /* UNIT_TEST */
1692 RT_NOREF3(u32ClientID, pvClient, pSSM);
1693#endif /* UNIT_TEST */
1694 return VINF_SUCCESS;
1695}
1696
1697#ifndef UNIT_TEST
1698static int svcLoadStateV0(uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
1699{
1700 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
1701
1702 uint32_t uMarker;
1703 int rc = SSMR3GetU32(pSSM, &uMarker); /* Begin marker. */
1704 AssertRC(rc);
1705 Assert(uMarker == UINT32_C(0x19200102) /* SSMR3STRUCT_BEGIN */);
1706
1707 rc = SSMR3Skip(pSSM, sizeof(uint32_t)); /* Client ID */
1708 AssertRCReturn(rc, rc);
1709
1710 bool fValue;
1711 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgQuit */
1712 AssertRCReturn(rc, rc);
1713
1714 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgReadData */
1715 AssertRCReturn(rc, rc);
1716
1717 rc = SSMR3GetBool(pSSM, &fValue); /* fHostMsgFormats */
1718 AssertRCReturn(rc, rc);
1719
1720 uint32_t fFormats;
1721 rc = SSMR3GetU32(pSSM, &fFormats); /* u32RequestedFormat */
1722 AssertRCReturn(rc, rc);
1723
1724 rc = SSMR3GetU32(pSSM, &uMarker); /* End marker. */
1725 AssertRCReturn(rc, rc);
1726 Assert(uMarker == UINT32_C(0x19920406) /* SSMR3STRUCT_END */);
1727
1728 return VINF_SUCCESS;
1729}
1730#endif /* UNIT_TEST */
1731
1732static DECLCALLBACK(int) svcLoadState(void *, uint32_t u32ClientID, void *pvClient, PSSMHANDLE pSSM, uint32_t uVersion)
1733{
1734#ifndef UNIT_TEST
1735 RT_NOREF(u32ClientID, uVersion);
1736
1737 LogFunc(("u32ClientID=%RU32\n", u32ClientID));
1738
1739 PSHCLCLIENT pClient = (PSHCLCLIENT)pvClient;
1740 AssertPtr(pClient);
1741
1742 /* Restore the client data. */
1743 uint32_t lenOrVer;
1744 int rc = SSMR3GetU32(pSSM, &lenOrVer);
1745 AssertRCReturn(rc, rc);
1746 if (lenOrVer == VBOX_SHCL_SSM_VER_0)
1747 {
1748 return svcLoadStateV0(u32ClientID, pvClient, pSSM, uVersion);
1749 }
1750 else if (lenOrVer == VBOX_SHCL_SSM_VER_1)
1751 {
1752 rc = SSMR3GetStructEx(pSSM, &pClient->State, sizeof(pClient->State), 0 /*fFlags*/, &s_aShClSSMClientState[0], NULL);
1753 AssertRCReturn(rc, rc);
1754
1755 rc = SSMR3GetStructEx(pSSM, &pClient->State.Transfers, sizeof(pClient->State.Transfers), 0 /*fFlags*/, &s_aShClSSMClientURIState[0], NULL);
1756 AssertRCReturn(rc, rc);
1757
1758 /* Load the client's internal message queue. */
1759 uint64_t cMsg;
1760 rc = SSMR3GetU64(pSSM, &cMsg);
1761 AssertRCReturn(rc, rc);
1762
1763 for (uint64_t i = 0; i < cMsg; i++)
1764 {
1765 PSHCLCLIENTMSG pMsg = (PSHCLCLIENTMSG)RTMemAlloc(sizeof(SHCLCLIENTMSG));
1766 AssertPtrReturn(pMsg, VERR_NO_MEMORY);
1767
1768 rc = SSMR3GetStructEx(pSSM, pMsg, sizeof(SHCLCLIENTMSG), 0 /*fFlags*/, &s_aShClSSMClientMsgHdr[0], NULL);
1769 AssertRCReturn(rc, rc);
1770
1771 rc = SSMR3GetStructEx(pSSM, &pMsg->Ctx, sizeof(SHCLMSGCTX), 0 /*fFlags*/, &s_aShClSSMClientMsgCtx[0], NULL);
1772 AssertRCReturn(rc, rc);
1773
1774 pMsg->paParms = (PVBOXHGCMSVCPARM)RTMemAllocZ(sizeof(VBOXHGCMSVCPARM) * pMsg->cParms);
1775 AssertPtrReturn(pMsg->paParms, VERR_NO_MEMORY);
1776
1777 for (uint32_t p = 0; p < pMsg->cParms; p++)
1778 {
1779 rc = HGCMSvcSSMR3Get(&pMsg->paParms[p], pSSM);
1780 AssertRCReturn(rc, rc);
1781 }
1782
1783 rc = sharedClipboardSvcMsgAdd(pClient, pMsg, true /* fAppend */);
1784 AssertRCReturn(rc, rc);
1785 }
1786 }
1787 else
1788 {
1789 LogRel(("Shared Clipboard: Unknown saved state version (%#x)\n", lenOrVer));
1790 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1791 }
1792
1793 /* Actual host data are to be reported to guest (SYNC). */
1794 SharedClipboardSvcImplSync(pClient);
1795
1796#else /* UNIT_TEST */
1797 RT_NOREF(u32ClientID, pvClient, pSSM, uVersion);
1798#endif /* UNIT_TEST */
1799 return VINF_SUCCESS;
1800}
1801
1802static DECLCALLBACK(int) extCallback(uint32_t u32Function, uint32_t u32Format, void *pvData, uint32_t cbData)
1803{
1804 RT_NOREF(pvData, cbData);
1805
1806 LogFlowFunc(("u32Function=%RU32\n", u32Function));
1807
1808 int rc = VINF_SUCCESS;
1809
1810 /* Figure out if the client in charge for the service extension still is connected. */
1811 ClipboardClientMap::const_iterator itClient = g_mapClients.find(g_ExtState.uClientID);
1812 if (itClient != g_mapClients.end())
1813 {
1814 PSHCLCLIENT pClient = itClient->second;
1815 AssertPtr(pClient);
1816
1817 switch (u32Function)
1818 {
1819 /* The service extension announces formats to the guest. */
1820 case VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE:
1821 {
1822 LogFlowFunc(("VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE: g_ExtState.fReadingData=%RTbool\n", g_ExtState.fReadingData));
1823 if (g_ExtState.fReadingData)
1824 {
1825 g_ExtState.fDelayedAnnouncement = true;
1826 g_ExtState.uDelayedFormats = u32Format;
1827 }
1828 else
1829 {
1830 SHCLFORMATDATA formatData;
1831 RT_ZERO(formatData);
1832
1833 formatData.uFormats = u32Format;
1834
1835 rc = sharedClipboardSvcFormatsReport(pClient, &formatData);
1836 }
1837
1838 break;
1839 }
1840
1841 /* The service extension wants read data from the guest. */
1842 case VBOX_CLIPBOARD_EXT_FN_DATA_READ:
1843 {
1844 SHCLDATAREQ dataReq;
1845 RT_ZERO(dataReq);
1846
1847 dataReq.uFmt = u32Format;
1848 dataReq.cbSize = _64K; /** @todo Make this more dynamic. */
1849
1850 rc = sharedClipboardSvcDataReadRequest(pClient, &dataReq, NULL /* puEvent */);
1851 break;
1852 }
1853
1854 default:
1855 /* Just skip other messages. */
1856 break;
1857 }
1858 }
1859 else
1860 rc = VERR_NOT_FOUND;
1861
1862 LogFlowFuncLeaveRC(rc);
1863 return rc;
1864}
1865
1866static DECLCALLBACK(int) svcRegisterExtension(void *, PFNHGCMSVCEXT pfnExtension, void *pvExtension)
1867{
1868 LogFlowFunc(("pfnExtension=%p\n", pfnExtension));
1869
1870 SHCLEXTPARMS parms;
1871 RT_ZERO(parms);
1872
1873 if (pfnExtension)
1874 {
1875 /* Install extension. */
1876 g_ExtState.pfnExtension = pfnExtension;
1877 g_ExtState.pvExtension = pvExtension;
1878
1879 parms.u.pfnCallback = extCallback;
1880
1881 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
1882 }
1883 else
1884 {
1885 if (g_ExtState.pfnExtension)
1886 g_ExtState.pfnExtension(g_ExtState.pvExtension, VBOX_CLIPBOARD_EXT_FN_SET_CALLBACK, &parms, sizeof(parms));
1887
1888 /* Uninstall extension. */
1889 g_ExtState.pfnExtension = NULL;
1890 g_ExtState.pvExtension = NULL;
1891 }
1892
1893 return VINF_SUCCESS;
1894}
1895
1896extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
1897{
1898 int rc = VINF_SUCCESS;
1899
1900 LogFlowFunc(("ptable=%p\n", pTable));
1901
1902 if (!pTable)
1903 {
1904 rc = VERR_INVALID_PARAMETER;
1905 }
1906 else
1907 {
1908 LogFunc(("ptable->cbSize = %d, ptable->u32Version = 0x%08X\n", pTable->cbSize, pTable->u32Version));
1909
1910 if ( pTable->cbSize != sizeof (VBOXHGCMSVCFNTABLE)
1911 || pTable->u32Version != VBOX_HGCM_SVC_VERSION)
1912 {
1913 rc = VERR_INVALID_PARAMETER;
1914 }
1915 else
1916 {
1917 g_pHelpers = pTable->pHelpers;
1918
1919 pTable->cbClient = sizeof(SHCLCLIENT);
1920
1921 pTable->pfnUnload = svcUnload;
1922 pTable->pfnConnect = svcConnect;
1923 pTable->pfnDisconnect = svcDisconnect;
1924 pTable->pfnCall = svcCall;
1925 pTable->pfnHostCall = svcHostCall;
1926 pTable->pfnSaveState = svcSaveState;
1927 pTable->pfnLoadState = svcLoadState;
1928 pTable->pfnRegisterExtension = svcRegisterExtension;
1929 pTable->pfnNotify = NULL;
1930 pTable->pvService = NULL;
1931
1932 /* Service specific initialization. */
1933 rc = svcInit();
1934 }
1935 }
1936
1937 return rc;
1938}
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