VirtualBox

source: vbox/trunk/src/VBox/HostServices/DragAndDrop/VBoxDragAndDropSvc.cpp@ 85159

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

DnD/HostService: Improved verification of DragAndDropService::guestCall(). bugref:9777.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.0 KB
Line 
1/* $Id: VBoxDragAndDropSvc.cpp 85145 2020-07-09 09:59:22Z vboxsync $ */
2/** @file
3 * Drag and Drop Service.
4 */
5
6/*
7 * Copyright (C) 2011-2020 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/** @page pg_svc_dnd Drag and drop HGCM Service
19 *
20 * TODO
21 */
22
23
24/*********************************************************************************************************************************
25* Header Files *
26*********************************************************************************************************************************/
27#define LOG_GROUP LOG_GROUP_GUEST_DND
28#include <VBox/GuestHost/DragAndDrop.h>
29#include <VBox/GuestHost/DragAndDropDefs.h>
30#include <VBox/HostServices/Service.h>
31#include <VBox/HostServices/DragAndDropSvc.h>
32#include <VBox/AssertGuest.h>
33
34#include <VBox/err.h>
35
36#include <algorithm>
37#include <list>
38#include <map>
39
40#include "dndmanager.h"
41
42using namespace DragAndDropSvc;
43
44
45/*********************************************************************************************************************************
46* Service class declaration *
47*********************************************************************************************************************************/
48
49class DragAndDropClient : public HGCM::Client
50{
51public:
52
53 DragAndDropClient(uint32_t uClientID)
54 : HGCM::Client(uClientID)
55 {
56 RT_ZERO(m_SvcCtx);
57 }
58
59 virtual ~DragAndDropClient(void)
60 {
61 disconnect();
62 }
63
64public:
65
66 void disconnect(void);
67};
68
69/** Map holding pointers to drag and drop clients. Key is the (unique) HGCM client ID. */
70typedef std::map<uint32_t, DragAndDropClient*> DnDClientMap;
71
72/** Simple queue (list) which holds deferred (waiting) clients. */
73typedef std::list<uint32_t> DnDClientQueue;
74
75/**
76 * Specialized drag & drop service class.
77 */
78class DragAndDropService : public HGCM::AbstractService<DragAndDropService>
79{
80public:
81
82 explicit DragAndDropService(PVBOXHGCMSVCHELPERS pHelpers)
83 : HGCM::AbstractService<DragAndDropService>(pHelpers)
84 , m_pManager(NULL) {}
85
86protected:
87
88 int init(VBOXHGCMSVCFNTABLE *pTable);
89 int uninit(void);
90 int clientConnect(uint32_t u32ClientID, void *pvClient);
91 int clientDisconnect(uint32_t u32ClientID, void *pvClient);
92 void guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
93 int hostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
94
95 int modeSet(uint32_t u32Mode);
96 inline uint32_t modeGet(void) const { return m_u32Mode; };
97
98protected:
99
100 static DECLCALLBACK(int) progressCallback(uint32_t uStatus, uint32_t uPercentage, int rc, void *pvUser);
101
102protected:
103
104 /** Pointer to our DnD manager instance. */
105 DnDManager *m_pManager;
106 /** Map of all connected clients.
107 * The primary key is the (unique) client ID, the secondary value
108 * an allocated pointer to the DragAndDropClient class, managed
109 * by this service class. */
110 DnDClientMap m_clientMap;
111 /** List of all clients which are queued up (deferred return) and ready
112 * to process new commands. The key is the (unique) client ID. */
113 DnDClientQueue m_clientQueue;
114 /** Current drag and drop mode. */
115 uint32_t m_u32Mode;
116};
117
118
119/*********************************************************************************************************************************
120* Client implementation *
121*********************************************************************************************************************************/
122
123/**
124 * Called when the HGCM client disconnected on the guest side.
125 * This function takes care of the client's data cleanup and also lets the host
126 * know that the client has been disconnected.
127 *
128 */
129void DragAndDropClient::disconnect(void)
130{
131 LogFlowThisFunc(("uClient=%RU32\n", m_uClientID));
132
133 if (IsDeferred())
134 CompleteDeferred(VERR_INTERRUPTED);
135
136 /*
137 * Let the host know.
138 */
139 VBOXDNDCBDISCONNECTMSGDATA data;
140 RT_ZERO(data);
141 /** @todo Magic needed? */
142 /** @todo Add context ID. */
143
144 if (m_SvcCtx.pfnHostCallback)
145 {
146 int rc2 = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, GUEST_DND_DISCONNECT, &data, sizeof(data));
147 if (RT_FAILURE(rc2))
148 LogFlowFunc(("Warning: Unable to notify host about client %RU32 disconnect, rc=%Rrc\n", m_uClientID, rc2));
149 /* Not fatal. */
150 }
151}
152
153
154/*********************************************************************************************************************************
155* Service class implementation *
156*********************************************************************************************************************************/
157
158int DragAndDropService::init(VBOXHGCMSVCFNTABLE *pTable)
159{
160 /* Register functions. */
161 pTable->pfnHostCall = svcHostCall;
162 pTable->pfnSaveState = NULL; /* The service is stateless, so the normal */
163 pTable->pfnLoadState = NULL; /* construction done before restoring suffices */
164 pTable->pfnRegisterExtension = svcRegisterExtension;
165 pTable->pfnNotify = NULL;
166
167 /* Drag'n drop mode is disabled by default. */
168 modeSet(VBOX_DRAG_AND_DROP_MODE_OFF);
169
170 int rc = VINF_SUCCESS;
171
172 try
173 {
174 m_pManager = new DnDManager(&DragAndDropService::progressCallback, this);
175 }
176 catch(std::bad_alloc &)
177 {
178 rc = VERR_NO_MEMORY;
179 }
180
181 LogFlowFuncLeaveRC(rc);
182 return rc;
183}
184
185int DragAndDropService::uninit(void)
186{
187 LogFlowFuncEnter();
188
189 if (m_pManager)
190 {
191 delete m_pManager;
192 m_pManager = NULL;
193 }
194
195 DnDClientMap::iterator itClient = m_clientMap.begin();
196 while (itClient != m_clientMap.end())
197 {
198 delete itClient->second;
199 m_clientMap.erase(itClient);
200 itClient = m_clientMap.begin();
201 }
202
203 LogFlowFuncLeave();
204 return VINF_SUCCESS;
205}
206
207int DragAndDropService::clientConnect(uint32_t u32ClientID, void *pvClient)
208{
209 RT_NOREF1(pvClient);
210 if (m_clientMap.size() >= UINT8_MAX) /* Don't allow too much clients at the same time. */
211 {
212 AssertMsgFailed(("Maximum number of clients reached\n"));
213 return VERR_MAX_PROCS_REACHED;
214 }
215
216 int rc = VINF_SUCCESS;
217
218 /*
219 * Add client to our client map.
220 */
221 if (m_clientMap.find(u32ClientID) != m_clientMap.end())
222 rc = VERR_ALREADY_EXISTS;
223
224 if (RT_SUCCESS(rc))
225 {
226 try
227 {
228 DragAndDropClient *pClient = new DragAndDropClient(u32ClientID);
229 pClient->SetSvcContext(m_SvcCtx);
230 m_clientMap[u32ClientID] = pClient;
231 }
232 catch(std::bad_alloc &)
233 {
234 rc = VERR_NO_MEMORY;
235 }
236
237 if (RT_SUCCESS(rc))
238 {
239 /*
240 * Reset the message queue as soon as a new clients connect
241 * to ensure that every client has the same state.
242 */
243 if (m_pManager)
244 m_pManager->Reset();
245 }
246 }
247
248 LogFlowFunc(("Client %RU32 connected, rc=%Rrc\n", u32ClientID, rc));
249 return rc;
250}
251
252int DragAndDropService::clientDisconnect(uint32_t u32ClientID, void *pvClient)
253{
254 RT_NOREF1(pvClient);
255
256 /* Client not found? Bail out early. */
257 DnDClientMap::iterator itClient = m_clientMap.find(u32ClientID);
258 if (itClient == m_clientMap.end())
259 return VERR_NOT_FOUND;
260
261 /*
262 * Remove from waiters queue.
263 */
264 m_clientQueue.remove(u32ClientID);
265
266 /*
267 * Remove from client map and deallocate.
268 */
269 AssertPtr(itClient->second);
270 delete itClient->second;
271
272 m_clientMap.erase(itClient);
273
274 LogFlowFunc(("Client %RU32 disconnected\n", u32ClientID));
275 return VINF_SUCCESS;
276}
277
278int DragAndDropService::modeSet(uint32_t u32Mode)
279{
280#ifndef VBOX_WITH_DRAG_AND_DROP_GH
281 if ( u32Mode == VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST
282 || u32Mode == VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL)
283 {
284 m_u32Mode = VBOX_DRAG_AND_DROP_MODE_OFF;
285 return VERR_NOT_SUPPORTED;
286 }
287#endif
288
289 switch (u32Mode)
290 {
291 case VBOX_DRAG_AND_DROP_MODE_OFF:
292 case VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST:
293 case VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST:
294 case VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL:
295 m_u32Mode = u32Mode;
296 break;
297
298 default:
299 m_u32Mode = VBOX_DRAG_AND_DROP_MODE_OFF;
300 break;
301 }
302
303 return VINF_SUCCESS;
304}
305
306void DragAndDropService::guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
307 void *pvClient, uint32_t u32Function,
308 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
309{
310 RT_NOREF1(pvClient);
311 LogFlowFunc(("u32ClientID=%RU32, u32Function=%RU32, cParms=%RU32\n",
312 u32ClientID, u32Function, cParms));
313
314 /* Check if we've the right mode set. */
315 int rc = VERR_ACCESS_DENIED; /* Play safe. */
316 switch (u32Function)
317 {
318 case GUEST_DND_GET_NEXT_HOST_MSG:
319 {
320 if (modeGet() != VBOX_DRAG_AND_DROP_MODE_OFF)
321 {
322 rc = VINF_SUCCESS;
323 }
324 else
325 {
326 LogFlowFunc(("DnD disabled, deferring request\n"));
327 rc = VINF_HGCM_ASYNC_EXECUTE;
328 }
329 break;
330 }
331
332 /* New since protocol v2. */
333 case GUEST_DND_CONNECT:
334 {
335 /*
336 * Never block the initial connect call, as the clients do this when
337 * initializing and might get stuck if drag and drop is set to "disabled" at
338 * that time.
339 */
340 rc = VINF_SUCCESS;
341 break;
342 }
343 case GUEST_DND_HG_ACK_OP:
344 case GUEST_DND_HG_REQ_DATA:
345 case GUEST_DND_HG_EVT_PROGRESS:
346 {
347 if ( modeGet() == VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
348 || modeGet() == VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
349 {
350 rc = VINF_SUCCESS;
351 }
352 else
353 LogFlowFunc(("Host -> Guest DnD mode disabled, ignoring request\n"));
354 break;
355 }
356
357 case GUEST_DND_GH_ACK_PENDING:
358 case GUEST_DND_GH_SND_DATA_HDR:
359 case GUEST_DND_GH_SND_DATA:
360 case GUEST_DND_GH_SND_DIR:
361 case GUEST_DND_GH_SND_FILE_HDR:
362 case GUEST_DND_GH_SND_FILE_DATA:
363 case GUEST_DND_GH_EVT_ERROR:
364 {
365#ifdef VBOX_WITH_DRAG_AND_DROP_GH
366 if ( modeGet() == VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
367 || modeGet() == VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
368 {
369 rc = VINF_SUCCESS;
370 }
371 else
372#endif
373 LogFlowFunc(("Guest -> Host DnD mode disabled, ignoring request\n"));
374 break;
375 }
376
377 default:
378 /* Reach through to DnD manager. */
379 rc = VINF_SUCCESS;
380 break;
381 }
382
383#ifdef DEBUG_andy
384 LogFlowFunc(("Mode (%RU32) check rc=%Rrc\n", modeGet(), rc));
385#endif
386
387#define DO_HOST_CALLBACK(); \
388 if ( RT_SUCCESS(rc) \
389 && m_SvcCtx.pfnHostCallback) \
390 { \
391 rc = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, u32Function, &data, sizeof(data)); \
392 }
393
394 /*
395 * Lookup client.
396 */
397 DragAndDropClient *pClient = NULL;
398
399 DnDClientMap::iterator itClient = m_clientMap.find(u32ClientID);
400 if (itClient != m_clientMap.end())
401 {
402 pClient = itClient->second;
403 AssertPtr(pClient);
404 }
405 else
406 {
407 LogFunc(("Client %RU32 was not found\n", u32ClientID));
408 rc = VERR_NOT_FOUND;
409 }
410
411/* Verifies that an uint32 parameter has the expected buffer size set.
412 * Will set rc to VERR_INVALID_PARAMETER otherwise. See #9777. */
413#define VERIFY_BUFFER_SIZE_UINT32(a_ParmUInt32, a_SizeExpected) \
414{ \
415 uint32_t cbTemp = 0; \
416 rc = HGCMSvcGetU32(&a_ParmUInt32, &cbTemp); \
417 ASSERT_GUEST_STMT(RT_SUCCESS(rc) && cbTemp == a_SizeExpected, rc = VERR_INVALID_PARAMETER); \
418}
419
420 if (rc == VINF_SUCCESS) /* Note: rc might be VINF_HGCM_ASYNC_EXECUTE! */
421 {
422 LogFlowFunc(("Client %RU32: Protocol v%RU32\n", pClient->GetClientID(), pClient->GetProtocolVer()));
423
424 rc = VERR_INVALID_PARAMETER; /* Play safe. */
425
426 switch (u32Function)
427 {
428 /*
429 * Note: Older VBox versions with enabled DnD guest->host support (< 5.0)
430 * used the same message ID (300) for GUEST_DND_GET_NEXT_HOST_MSG and
431 * HOST_DND_GH_REQ_PENDING, which led this service returning
432 * VERR_INVALID_PARAMETER when the guest wanted to actually
433 * handle HOST_DND_GH_REQ_PENDING.
434 */
435 case GUEST_DND_GET_NEXT_HOST_MSG:
436 {
437 LogFlowFunc(("GUEST_DND_GET_NEXT_HOST_MSG\n"));
438 if (cParms == 3)
439 {
440 rc = m_pManager->GetNextMsgInfo(&paParms[0].u.uint32 /* uMsg */, &paParms[1].u.uint32 /* cParms */);
441 if (RT_FAILURE(rc)) /* No queued messages available? */
442 {
443 if (m_SvcCtx.pfnHostCallback) /* Try asking the host. */
444 {
445 VBOXDNDCBHGGETNEXTHOSTMSG data;
446 RT_ZERO(data);
447 data.hdr.uMagic = CB_MAGIC_DND_HG_GET_NEXT_HOST_MSG;
448 rc = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, u32Function, &data, sizeof(data));
449 if (RT_SUCCESS(rc))
450 {
451 paParms[0].u.uint32 = data.uMsg; /* uMsg */
452 paParms[1].u.uint32 = data.cParms; /* cParms */
453 /* Note: paParms[2] was set by the guest as blocking flag. */
454 }
455 }
456 else /* No host callback in place, so drag and drop is not supported by the host. */
457 rc = VERR_NOT_SUPPORTED;
458
459 if (RT_FAILURE(rc))
460 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
461
462 /* Some error occurred or no (new) messages available? */
463 if (RT_FAILURE(rc))
464 {
465 uint32_t fFlags = 0;
466 int rc2 = HGCMSvcGetU32(&paParms[2], &fFlags);
467 if ( RT_SUCCESS(rc2)
468 && fFlags) /* Blocking flag set? */
469 {
470 /* Defer client returning. */
471 rc = VINF_HGCM_ASYNC_EXECUTE;
472 }
473 else
474 rc = VERR_INVALID_PARAMETER;
475
476 LogFlowFunc(("Message queue is empty, returning %Rrc to guest\n", rc));
477 }
478 }
479 }
480 break;
481 }
482 case GUEST_DND_CONNECT:
483 {
484 LogFlowFunc(("GUEST_DND_CONNECT\n"));
485 if (cParms >= 2)
486 {
487 const uint8_t idxProto = cParms >= 3 ? 1 : 0;
488
489 VBOXDNDCBCONNECTMSGDATA data;
490 RT_ZERO(data);
491 data.hdr.uMagic = CB_MAGIC_DND_CONNECT;
492 if (cParms >= 3)
493 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
494 else /* Older protocols don't have a context ID. */
495 rc = VINF_SUCCESS;
496 if (RT_SUCCESS(rc))
497 rc = HGCMSvcGetU32(&paParms[idxProto], &data.uProtocol);
498 if (RT_SUCCESS(rc))
499 rc = HGCMSvcGetU32(&paParms[idxProto + 1], &data.uFlags);
500 if (RT_SUCCESS(rc))
501 {
502 unsigned uProtocolVer = 3; /* The protocol version we're going to use. */
503
504 /* Make sure we're only setting a protocl version we're supporting on the host. */
505 if (data.uProtocol > uProtocolVer)
506 data.uProtocol = uProtocolVer;
507
508 pClient->SetProtocolVer(data.uProtocol);
509
510 /* Return the highest protocol version we're supporting. */
511 paParms[idxProto].u.uint32 = data.uProtocol;
512
513 LogFlowFunc(("Client %RU32 is now using protocol v%RU32\n", pClient->GetClientID(), pClient->GetProtocolVer()));
514 DO_HOST_CALLBACK();
515 }
516 }
517 break;
518 }
519 case GUEST_DND_HG_ACK_OP:
520 {
521 LogFlowFunc(("GUEST_DND_HG_ACK_OP\n"));
522
523 VBOXDNDCBHGACKOPDATA data;
524 RT_ZERO(data);
525 data.hdr.uMagic = CB_MAGIC_DND_HG_ACK_OP;
526
527 switch (pClient->GetProtocolVer())
528 {
529 case 3:
530 {
531 if (cParms == 2)
532 {
533 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
534 if (RT_SUCCESS(rc))
535 rc = HGCMSvcGetU32(&paParms[1], &data.uAction); /* Get drop action. */
536 }
537 break;
538 }
539
540 case 2:
541 default:
542 {
543 if (cParms == 1)
544 rc = HGCMSvcGetU32(&paParms[0], &data.uAction); /* Get drop action. */
545 break;
546 }
547 }
548
549 DO_HOST_CALLBACK();
550 break;
551 }
552 case GUEST_DND_HG_REQ_DATA:
553 {
554 LogFlowFunc(("GUEST_DND_HG_REQ_DATA\n"));
555
556 VBOXDNDCBHGREQDATADATA data;
557 RT_ZERO(data);
558 data.hdr.uMagic = CB_MAGIC_DND_HG_REQ_DATA;
559
560 switch (pClient->GetProtocolVer())
561 {
562 case 3:
563 {
564 if (cParms == 3)
565 {
566 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
567 if (RT_SUCCESS(rc))
568 rc = HGCMSvcGetPv(&paParms[1], (void **)&data.pszFormat, &data.cbFormat);
569 VERIFY_BUFFER_SIZE_UINT32(paParms[2], data.cbFormat);
570 }
571 break;
572 }
573
574 case 2:
575 default:
576 {
577 if (cParms == 1)
578 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.pszFormat, &data.cbFormat);
579 break;
580 }
581 }
582
583 DO_HOST_CALLBACK();
584 break;
585 }
586 case GUEST_DND_HG_EVT_PROGRESS:
587 {
588 LogFlowFunc(("GUEST_DND_HG_EVT_PROGRESS\n"));
589
590 VBOXDNDCBHGEVTPROGRESSDATA data;
591 RT_ZERO(data);
592 data.hdr.uMagic = CB_MAGIC_DND_HG_EVT_PROGRESS;
593
594 switch (pClient->GetProtocolVer())
595 {
596 case 3:
597 {
598 if (cParms == 4)
599 {
600 rc = HGCMSvcGetU32(&paParms[0], &data.uStatus);
601 if (RT_SUCCESS(rc))
602 rc = HGCMSvcGetU32(&paParms[1], &data.uStatus);
603 if (RT_SUCCESS(rc))
604 rc = HGCMSvcGetU32(&paParms[2], &data.uPercentage);
605 if (RT_SUCCESS(rc))
606 rc = HGCMSvcGetU32(&paParms[3], &data.rc);
607 }
608 break;
609 }
610
611 case 2:
612 default:
613 {
614 if (cParms == 3)
615 {
616 rc = HGCMSvcGetU32(&paParms[0], &data.uStatus);
617 if (RT_SUCCESS(rc))
618 rc = HGCMSvcGetU32(&paParms[1], &data.uPercentage);
619 if (RT_SUCCESS(rc))
620 rc = HGCMSvcGetU32(&paParms[2], &data.rc);
621 }
622 break;
623 }
624 }
625
626 DO_HOST_CALLBACK();
627 break;
628 }
629#ifdef VBOX_WITH_DRAG_AND_DROP_GH
630 case GUEST_DND_GH_ACK_PENDING:
631 {
632 LogFlowFunc(("GUEST_DND_GH_ACK_PENDING\n"));
633
634 VBOXDNDCBGHACKPENDINGDATA data;
635 RT_ZERO(data);
636 data.hdr.uMagic = CB_MAGIC_DND_GH_ACK_PENDING;
637
638 switch (pClient->GetProtocolVer())
639 {
640 case 3:
641 {
642 if (cParms == 5)
643 {
644 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
645 if (RT_SUCCESS(rc))
646 rc = HGCMSvcGetU32(&paParms[1], &data.uDefAction);
647 if (RT_SUCCESS(rc))
648 rc = HGCMSvcGetU32(&paParms[2], &data.uAllActions);
649 if (RT_SUCCESS(rc))
650 rc = HGCMSvcGetPv(&paParms[3], (void**)&data.pszFormat, &data.cbFormat);
651 VERIFY_BUFFER_SIZE_UINT32(paParms[4], data.cbFormat);
652 }
653 break;
654 }
655
656 case 2:
657 default:
658 {
659 if (cParms == 3)
660 {
661 rc = HGCMSvcGetU32(&paParms[0], &data.uDefAction);
662 if (RT_SUCCESS(rc))
663 rc = HGCMSvcGetU32(&paParms[1], &data.uAllActions);
664 if (RT_SUCCESS(rc))
665 rc = HGCMSvcGetPv(&paParms[2], (void**)&data.pszFormat, &data.cbFormat);
666 }
667 break;
668 }
669 }
670
671 DO_HOST_CALLBACK();
672 break;
673 }
674 /* New since protocol v3. */
675 case GUEST_DND_GH_SND_DATA_HDR:
676 {
677 LogFlowFunc(("GUEST_DND_GH_SND_DATA_HDR\n"));
678 if (cParms == 12)
679 {
680 VBOXDNDCBSNDDATAHDRDATA data;
681 RT_ZERO(data);
682 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DATA_HDR;
683 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
684 if (RT_SUCCESS(rc))
685 rc = HGCMSvcGetU32(&paParms[1], &data.data.uFlags);
686 if (RT_SUCCESS(rc))
687 rc = HGCMSvcGetU32(&paParms[2], &data.data.uScreenId);
688 if (RT_SUCCESS(rc))
689 rc = HGCMSvcGetU64(&paParms[3], &data.data.cbTotal);
690 if (RT_SUCCESS(rc))
691 rc = HGCMSvcGetU32(&paParms[4], &data.data.cbMeta);
692 if (RT_SUCCESS(rc))
693 rc = HGCMSvcGetPv(&paParms[5], &data.data.pvMetaFmt, &data.data.cbMetaFmt);
694 VERIFY_BUFFER_SIZE_UINT32(paParms[6], data.data.cbMetaFmt);
695 if (RT_SUCCESS(rc))
696 rc = HGCMSvcGetU64(&paParms[7], &data.data.cObjects);
697 if (RT_SUCCESS(rc))
698 rc = HGCMSvcGetU32(&paParms[8], &data.data.enmCompression);
699 if (RT_SUCCESS(rc))
700 rc = HGCMSvcGetU32(&paParms[9], (uint32_t *)&data.data.enmChecksumType);
701 if (RT_SUCCESS(rc))
702 rc = HGCMSvcGetPv(&paParms[10], &data.data.pvChecksum, &data.data.cbChecksum);
703 VERIFY_BUFFER_SIZE_UINT32(paParms[11], data.data.cbChecksum);
704 LogFlowFunc(("fFlags=0x%x, cbTotalSize=%RU64, cObj=%RU64\n",
705 data.data.uFlags, data.data.cbTotal, data.data.cObjects));
706 DO_HOST_CALLBACK();
707 }
708 break;
709 }
710 case GUEST_DND_GH_SND_DATA:
711 {
712 LogFlowFunc(("GUEST_DND_GH_SND_DATA\n"));
713 switch (pClient->GetProtocolVer())
714 {
715 case 3:
716 {
717 if (cParms == 5)
718 {
719 VBOXDNDCBSNDDATADATA data;
720 RT_ZERO(data);
721 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DATA;
722 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
723 if (RT_SUCCESS(rc))
724 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.data.u.v3.pvData, &data.data.u.v3.cbData);
725 VERIFY_BUFFER_SIZE_UINT32(paParms[2], data.data.u.v3.cbData);
726 if (RT_SUCCESS(rc))
727 rc = HGCMSvcGetPv(&paParms[3], (void**)&data.data.u.v3.pvChecksum, &data.data.u.v3.cbChecksum);
728 VERIFY_BUFFER_SIZE_UINT32(paParms[4], data.data.u.v3.cbChecksum);
729 DO_HOST_CALLBACK();
730 }
731 break;
732 }
733
734 case 2:
735 default:
736 {
737 if (cParms == 2)
738 {
739 VBOXDNDCBSNDDATADATA data;
740 RT_ZERO(data);
741 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DATA;
742 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.data.u.v1.pvData, &data.data.u.v1.cbData);
743 if (RT_SUCCESS(rc))
744 rc = HGCMSvcGetU32(&paParms[1], &data.data.u.v1.cbTotalSize);
745 DO_HOST_CALLBACK();
746 }
747 break;
748 }
749 }
750 break;
751 }
752 case GUEST_DND_GH_SND_DIR:
753 {
754 LogFlowFunc(("GUEST_DND_GH_SND_DIR\n"));
755
756 VBOXDNDCBSNDDIRDATA data;
757 RT_ZERO(data);
758 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DIR;
759
760 switch (pClient->GetProtocolVer())
761 {
762 case 3:
763 {
764 if (cParms == 4)
765 {
766 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
767 if (RT_SUCCESS(rc))
768 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pszPath, &data.cbPath);
769 VERIFY_BUFFER_SIZE_UINT32(paParms[2], data.cbPath);
770 if (RT_SUCCESS(rc))
771 rc = HGCMSvcGetU32(&paParms[3], &data.fMode);
772 }
773 break;
774 }
775
776 case 2:
777 default:
778 {
779 if (cParms == 3)
780 {
781 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.pszPath, &data.cbPath);
782 VERIFY_BUFFER_SIZE_UINT32(paParms[1], data.cbPath);
783 if (RT_SUCCESS(rc))
784 rc = HGCMSvcGetU32(&paParms[2], &data.fMode);
785 }
786 break;
787 }
788 }
789
790 DO_HOST_CALLBACK();
791 break;
792 }
793 /* New since protocol v2 (>= VBox 5.0). */
794 case GUEST_DND_GH_SND_FILE_HDR:
795 {
796 LogFlowFunc(("GUEST_DND_GH_SND_FILE_HDR\n"));
797 if (cParms == 6)
798 {
799 VBOXDNDCBSNDFILEHDRDATA data;
800 RT_ZERO(data);
801 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_HDR;
802
803 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
804 if (RT_SUCCESS(rc))
805 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pszFilePath, &data.cbFilePath);
806 VERIFY_BUFFER_SIZE_UINT32(paParms[2], data.cbFilePath);
807 if (RT_SUCCESS(rc))
808 rc = HGCMSvcGetU32(&paParms[3], &data.fFlags);
809 if (RT_SUCCESS(rc))
810 rc = HGCMSvcGetU32(&paParms[4], &data.fMode);
811 if (RT_SUCCESS(rc))
812 rc = HGCMSvcGetU64(&paParms[5], &data.cbSize);
813
814 LogFlowFunc(("pszPath=%s, cbPath=%RU32, fMode=0x%x, cbSize=%RU64\n",
815 data.pszFilePath, data.cbFilePath, data.fMode, data.cbSize));
816 DO_HOST_CALLBACK();
817 }
818 break;
819 }
820 case GUEST_DND_GH_SND_FILE_DATA:
821 {
822 LogFlowFunc(("GUEST_DND_GH_SND_FILE_DATA\n"));
823
824 switch (pClient->GetProtocolVer())
825 {
826 /* Protocol v3 adds (optional) checksums. */
827 case 3:
828 {
829 if (cParms == 5)
830 {
831 VBOXDNDCBSNDFILEDATADATA data;
832 RT_ZERO(data);
833 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_DATA;
834
835 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
836 if (RT_SUCCESS(rc))
837 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pvData, &data.cbData);
838 VERIFY_BUFFER_SIZE_UINT32(paParms[2], data.cbData);
839 if (RT_SUCCESS(rc))
840 rc = HGCMSvcGetPv(&paParms[3], (void**)&data.u.v3.pvChecksum, &data.u.v3.cbChecksum);
841 VERIFY_BUFFER_SIZE_UINT32(paParms[4], data.u.v3.cbChecksum);
842
843 LogFlowFunc(("pvData=0x%p, cbData=%RU32\n", data.pvData, data.cbData));
844 DO_HOST_CALLBACK();
845 }
846 break;
847 }
848 /* Protocol v2 only sends the next data chunks to reduce traffic. */
849 case 2:
850 {
851 if (cParms == 3)
852 {
853 VBOXDNDCBSNDFILEDATADATA data;
854 RT_ZERO(data);
855 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_DATA;
856 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
857 if (RT_SUCCESS(rc))
858 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pvData, &data.cbData);
859 VERIFY_BUFFER_SIZE_UINT32(paParms[2], data.cbData);
860
861 LogFlowFunc(("cbData=%RU32, pvData=0x%p\n", data.cbData, data.pvData));
862 DO_HOST_CALLBACK();
863 }
864 break;
865 }
866 /* Protocol v1 sends the file path and attributes for every file chunk (!). */
867 default:
868 {
869 if (cParms == 5)
870 {
871 VBOXDNDCBSNDFILEDATADATA data;
872 RT_ZERO(data);
873 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_DATA;
874 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.u.v1.pszFilePath, &data.u.v1.cbFilePath);
875 VERIFY_BUFFER_SIZE_UINT32(paParms[1], data.u.v1.cbFilePath);
876 if (RT_SUCCESS(rc))
877 rc = HGCMSvcGetPv(&paParms[2], (void**)&data.pvData, &data.cbData);
878 VERIFY_BUFFER_SIZE_UINT32(paParms[3], data.cbData);
879 if (RT_SUCCESS(rc))
880 rc = HGCMSvcGetU32(&paParms[4], &data.u.v1.fMode);
881
882 LogFlowFunc(("pszFilePath=%s, cbData=%RU32, pvData=0x%p, fMode=0x%x\n",
883 data.u.v1.pszFilePath, data.cbData, data.pvData, data.u.v1.fMode));
884 DO_HOST_CALLBACK();
885 }
886 break;
887 }
888 }
889 break;
890 }
891 case GUEST_DND_GH_EVT_ERROR:
892 {
893 LogFlowFunc(("GUEST_DND_GH_EVT_ERROR\n"));
894
895 VBOXDNDCBEVTERRORDATA data;
896 RT_ZERO(data);
897 data.hdr.uMagic = CB_MAGIC_DND_GH_EVT_ERROR;
898
899 switch (pClient->GetProtocolVer())
900 {
901 case 3:
902 {
903 if (cParms == 2)
904 {
905 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
906 if (RT_SUCCESS(rc))
907 {
908 uint32_t rcOp;
909 rc = HGCMSvcGetU32(&paParms[1], &rcOp);
910 if (RT_SUCCESS(rc))
911 data.rc = rcOp;
912 }
913 }
914 break;
915 }
916
917 case 2:
918 default:
919 {
920 if (cParms == 1)
921 {
922 uint32_t rcOp;
923 rc = HGCMSvcGetU32(&paParms[0], &rcOp);
924 if (RT_SUCCESS(rc))
925 data.rc = (int32_t)rcOp;
926 }
927 break;
928 }
929 }
930
931 DO_HOST_CALLBACK();
932 break;
933 }
934#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
935
936 default:
937 {
938 /* All other messages are handled by the DnD manager. */
939 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
940 if (rc == VERR_NO_DATA) /* Manager has no new messsages? Try asking the host. */
941 {
942 if (m_SvcCtx.pfnHostCallback)
943 {
944 VBOXDNDCBHGGETNEXTHOSTMSGDATA data;
945 RT_ZERO(data);
946
947 data.hdr.uMagic = VBOX_DND_CB_MAGIC_MAKE(0 /* uFn */, 0 /* uVer */);
948
949 data.uMsg = u32Function;
950 data.cParms = cParms;
951 data.paParms = paParms;
952
953 rc = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, u32Function,
954 &data, sizeof(data));
955 if (RT_SUCCESS(rc))
956 {
957 cParms = data.cParms;
958 paParms = data.paParms;
959 }
960 else
961 {
962 /*
963 * In case the guest is too fast asking for the next message
964 * and the host did not supply it yet, just defer the client's
965 * return until a response from the host available.
966 */
967 LogFlowFunc(("No new messages from the host (yet), deferring request: %Rrc\n", rc));
968 rc = VINF_HGCM_ASYNC_EXECUTE;
969 }
970 }
971 else /* No host callback in place, so drag and drop is not supported by the host. */
972 rc = VERR_NOT_SUPPORTED;
973 }
974 break;
975 }
976 }
977 }
978
979#undef VERIFY_BUFFER_SIZE_UINT32
980
981 /*
982 * If async execution is requested, we didn't notify the guest yet about
983 * completion. The client is queued into the waiters list and will be
984 * notified as soon as a new event is available.
985 */
986 if (rc == VINF_HGCM_ASYNC_EXECUTE)
987 {
988 try
989 {
990 AssertPtr(pClient);
991 pClient->SetDeferred(callHandle, u32Function, cParms, paParms);
992 m_clientQueue.push_back(u32ClientID);
993 }
994 catch (std::bad_alloc &)
995 {
996 rc = VERR_NO_MEMORY;
997 /* Don't report to guest. */
998 }
999 }
1000 else if (pClient)
1001 pClient->Complete(callHandle, rc);
1002 else
1003 {
1004 AssertMsgFailed(("Guest call failed with %Rrc\n", rc));
1005 rc = VERR_NOT_IMPLEMENTED;
1006 }
1007
1008 LogFlowFunc(("Returning rc=%Rrc\n", rc));
1009}
1010
1011int DragAndDropService::hostCall(uint32_t u32Function,
1012 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1013{
1014 LogFlowFunc(("u32Function=%RU32, cParms=%RU32, cClients=%zu, cQueue=%zu\n",
1015 u32Function, cParms, m_clientMap.size(), m_clientQueue.size()));
1016
1017 int rc;
1018
1019 do
1020 {
1021 bool fSendToGuest = false; /* Whether to send the message down to the guest side or not. */
1022
1023 switch (u32Function)
1024 {
1025 case HOST_DND_SET_MODE:
1026 {
1027 if (cParms != 1)
1028 rc = VERR_INVALID_PARAMETER;
1029 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT)
1030 rc = VERR_INVALID_PARAMETER;
1031 else
1032 rc = modeSet(paParms[0].u.uint32);
1033 break;
1034 }
1035
1036 case HOST_DND_CANCEL:
1037 {
1038 LogFlowFunc(("Cancelling all waiting clients ...\n"));
1039
1040 /* Reset the message queue as the host cancelled the whole operation. */
1041 m_pManager->Reset();
1042
1043 rc = m_pManager->AddMsg(u32Function, cParms, paParms, true /* fAppend */);
1044 if (RT_FAILURE(rc))
1045 {
1046 AssertMsgFailed(("Adding new message of type=%RU32 failed with rc=%Rrc\n", u32Function, rc));
1047 break;
1048 }
1049
1050 /*
1051 * Wake up all deferred clients and tell them to process
1052 * the cancelling message next.
1053 */
1054 DnDClientQueue::iterator itQueue = m_clientQueue.begin();
1055 while (itQueue != m_clientQueue.end())
1056 {
1057 DnDClientMap::iterator itClient = m_clientMap.find(*itQueue);
1058 Assert(itClient != m_clientMap.end());
1059
1060 DragAndDropClient *pClient = itClient->second;
1061 AssertPtr(pClient);
1062
1063 int rc2 = pClient->SetDeferredMsgInfo(HOST_DND_CANCEL,
1064 /* Protocol v3+ also contains the context ID. */
1065 pClient->GetProtocolVer() >= 3 ? 1 : 0);
1066 pClient->CompleteDeferred(rc2);
1067
1068 m_clientQueue.erase(itQueue);
1069 itQueue = m_clientQueue.begin();
1070 }
1071
1072 Assert(m_clientQueue.empty());
1073
1074 /* Tell the host that everything went well. */
1075 rc = VINF_SUCCESS;
1076 break;
1077 }
1078
1079 case HOST_DND_HG_EVT_ENTER:
1080 {
1081 /* Reset the message queue as a new DnD operation just began. */
1082 m_pManager->Reset();
1083
1084 fSendToGuest = true;
1085 rc = VINF_SUCCESS;
1086 break;
1087 }
1088
1089 default:
1090 {
1091 fSendToGuest = true;
1092 rc = VINF_SUCCESS;
1093 break;
1094 }
1095 }
1096
1097 if (fSendToGuest)
1098 {
1099 if (modeGet() == VBOX_DRAG_AND_DROP_MODE_OFF)
1100 {
1101 /* Tell the host that a wrong drag'n drop mode is set. */
1102 rc = VERR_ACCESS_DENIED;
1103 break;
1104 }
1105
1106 if (m_clientMap.empty()) /* At least one client on the guest connected? */
1107 {
1108 /*
1109 * Tell the host that the guest does not support drag'n drop.
1110 * This might happen due to not installed Guest Additions or
1111 * not running VBoxTray/VBoxClient.
1112 */
1113 rc = VERR_NOT_SUPPORTED;
1114 break;
1115 }
1116
1117 rc = m_pManager->AddMsg(u32Function, cParms, paParms, true /* fAppend */);
1118 if (RT_FAILURE(rc))
1119 {
1120 AssertMsgFailed(("Adding new message of type=%RU32 failed with rc=%Rrc\n", u32Function, rc));
1121 break;
1122 }
1123
1124 /* Any clients in our queue ready for processing the next command? */
1125 if (m_clientQueue.empty())
1126 {
1127 LogFlowFunc(("All clients (%zu) busy -- delaying execution\n", m_clientMap.size()));
1128 break;
1129 }
1130
1131 uint32_t uClientNext = m_clientQueue.front();
1132 DnDClientMap::iterator itClientNext = m_clientMap.find(uClientNext);
1133 Assert(itClientNext != m_clientMap.end());
1134
1135 DragAndDropClient *pClient = itClientNext->second;
1136 AssertPtr(pClient);
1137
1138 /*
1139 * Check if this was a request for getting the next host
1140 * message. If so, return the message ID and the parameter
1141 * count. The message itself has to be queued.
1142 */
1143 uint32_t uMsgClient = pClient->GetMsgType();
1144
1145 uint32_t uMsgNext = 0;
1146 uint32_t cParmsNext = 0;
1147 int rcNext = m_pManager->GetNextMsgInfo(&uMsgNext, &cParmsNext);
1148
1149 LogFlowFunc(("uMsgClient=%RU32, uMsgNext=%RU32, cParmsNext=%RU32, rcNext=%Rrc\n",
1150 uMsgClient, uMsgNext, cParmsNext, rcNext));
1151
1152 if (RT_SUCCESS(rcNext))
1153 {
1154 if (uMsgClient == GUEST_DND_GET_NEXT_HOST_MSG)
1155 {
1156 rc = pClient->SetDeferredMsgInfo(uMsgNext, cParmsNext);
1157
1158 /* Note: Report the current rc back to the guest. */
1159 pClient->CompleteDeferred(rc);
1160 }
1161 /*
1162 * Does the message the client is waiting for match the message
1163 * next in the queue? Process it right away then.
1164 */
1165 else if (uMsgClient == uMsgNext)
1166 {
1167 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
1168
1169 /* Note: Report the current rc back to the guest. */
1170 pClient->CompleteDeferred(rc);
1171 }
1172 else /* Should not happen; cancel the operation on the guest. */
1173 {
1174 LogFunc(("Client ID=%RU32 in wrong state with uMsg=%RU32 (next message in queue: %RU32), cancelling\n",
1175 pClient->GetClientID(), uMsgClient, uMsgNext));
1176
1177 pClient->CompleteDeferred(VERR_CANCELLED);
1178 }
1179
1180 m_clientQueue.pop_front();
1181 }
1182
1183 } /* fSendToGuest */
1184
1185 } while (0); /* To use breaks. */
1186
1187 LogFlowFuncLeaveRC(rc);
1188 return rc;
1189}
1190
1191DECLCALLBACK(int) DragAndDropService::progressCallback(uint32_t uStatus, uint32_t uPercentage, int rc, void *pvUser)
1192{
1193 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
1194
1195 DragAndDropService *pSelf = static_cast<DragAndDropService *>(pvUser);
1196 AssertPtr(pSelf);
1197
1198 if (pSelf->m_SvcCtx.pfnHostCallback)
1199 {
1200 LogFlowFunc(("GUEST_DND_HG_EVT_PROGRESS: uStatus=%RU32, uPercentage=%RU32, rc=%Rrc\n",
1201 uStatus, uPercentage, rc));
1202
1203 VBOXDNDCBHGEVTPROGRESSDATA data;
1204 data.hdr.uMagic = CB_MAGIC_DND_HG_EVT_PROGRESS;
1205 data.uPercentage = RT_MIN(uPercentage, 100);
1206 data.uStatus = uStatus;
1207 data.rc = rc; /** @todo uin32_t vs. int. */
1208
1209 return pSelf->m_SvcCtx.pfnHostCallback(pSelf->m_SvcCtx.pvHostData,
1210 GUEST_DND_HG_EVT_PROGRESS,
1211 &data, sizeof(data));
1212 }
1213
1214 return VINF_SUCCESS;
1215}
1216
1217/**
1218 * @copydoc FNVBOXHGCMSVCLOAD
1219 */
1220extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
1221{
1222 return DragAndDropService::svcLoad(pTable);
1223}
1224
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